kernel/fork.c: VM accounting bugfix (2.6.11-rc3-bk5)
Subsystems (3)
PATCH 1/1 kernel/fork.c: VM accounting bugfix (2.6.11-rc3-bk5)
[Aargh! Missing Signed-off-by.]
Unless I'm missing something, in kernel/fork.c, dup_mmap():
if (security_vm_enough_memory(len)) goto fail_nomem; /* ... */ fail_nomem: retval = -ENOMEM; vm_unacct_memory(charge);
/* ... */
If security_vm_enough_memory() fails there, then we vm_unacct_memory() that we never accounted (if security_vm_enough_memory() fails, no memory is accounted).
If it is in fact a bug, a simple but largely untested patch (against 2.6.11-rc3-bk5) is included.
Mark F. Haigh
[email protected]
kernel/fork.c renamed from kernel/fork.c.orig +4 -2
@@ -193,8 +193,10 @@ charge =0 ;if (mpnt->vm_flags &VM_ACCOUNT ) {unsigned int len = (mpnt->vm_end - mpnt->vm_start) >>PAGE_SHIFT ; -if (security_vm_enough_memory (len)) - goto fail_nomem; +if (security_vm_enough_memory (len)) { + retval = -ENOMEM ; + goto out; + } charge = len; } tmp =kmem_cache_alloc (vm_area_cachep,SLAB_KERNEL );
Hi Mark,
Quoted 10 lines · * Mark F. Haigh ([email protected]) wrote:
[Aargh! Missing Signed-off-by.]
Unless I'm missing something, in
kernel/fork.c,dup_mmap():if (security_vm_enough_memory(len)) goto fail_nomem; /* ... */ fail_nomem: retval = -ENOMEM; vm_unacct_memory(charge);
Quoted 5 lines
/* ... */
If
security_vm_enough_memory()fails there, then wevm_unacct_memory()that we never accounted (ifsecurity_vm_enough_memory()fails, no memory is accounted).
You missed one subtle point. That failure case actually unaccts 0 pages (note the use of charge). Not the nicest, but I believe correct.
thanks,
-chris
Signature · 1 line
Linux Security Modules http://lsm.immunix.org http://lsm.bkbits.net
Quoted 8 lines · Chris Wright
Quoted 4 lines · * Mark F. Haigh ([email protected]) wrote:
If
security_vm_enough_memory()fails there, then wevm_unacct_memory()that we never accounted (ifsecurity_vm_enough_memory()fails, no memory is accounted).You missed one subtle point. That failure case actually unaccts 0 pages (note the use of charge). Not the nicest, but I believe correct.
Not quite: Mark's patch is worse than unnecessary, it's wrong.
dup_mmap's charge starts out at 0 and gets added to each time around the loop through vmas; if security_vm_enough_memory fails at any point in that loop, we need to vm_unacct_memory the charge already accumulated.
Hugh
Quoted 3 lines · * Hugh Dickins ([email protected]) wrote:
dup_mmap's charge starts out at 0 and gets added to each time around the loop through vmas; if
security_vm_enough_memoryfails at any point in that loop, we need tovm_unacct_memorythe charge already accumulated.
If that's the requirement, then it's broken as is, because there is nothing accumulating. len is re-determined each pass, and charge is reset each pass. But I think that it's ok, as we only care about the last pass. If dup_mmap() fails part way through, the cleanup path should call unaccount for the (potentially) accounted by not fully setup vma then call exit_mmap() and clear all the vmas that got accounted for already. Either way, Mark's patch is not needed, and I don't think anything needs patching in this area. Hugh, do you agree?
thanks,
-chris
Signature · 1 line
Linux Security Modules http://lsm.immunix.org http://lsm.bkbits.net
Quoted 8 lines · Chris Wright
Quoted 3 lines · * Hugh Dickins ([email protected]) wrote:
dup_mmap's charge starts out at 0 and gets added to each time around the loop through vmas; if
security_vm_enough_memoryfails at any point in that loop, we need tovm_unacct_memorythe charge already accumulated.If that's the requirement, then it's broken as is, because there is nothing accumulating. len is re-determined each pass, and charge is reset each pass.
You're right, it's me who's wrong, sorry. I was remembering how it was before 2.6.7, when Oleg pointed out that it was doubly unaccounting (and it's probably me who was guilty of that double unaccounting too).
Quoted 4 lines
But I think that it's ok, as we only care about the
last pass. Ifdup_mmap()fails part way through, the cleanup path should call unaccount for the (potentially) accounted by not fully setup vma then callexit_mmap()and clear all the vmas that got accounted for already.
Yes, that was Oleg's point when he fixed it.
Quoted 2 lines
Either way, Mark's patch is not needed, and I don't think anything needs patching in this area. Hugh, do you agree?
Yes I agree - thanks for clearing that up, Chris.
Hugh
Chris Wright wrote:
<snip>
Quoted 4 lines
You missed one subtle point. That failure case actually unaccts 0 pages (note the use of charge). Not the nicest, but I believe correct.
Right. I did miss that. Thanks for the explanations, Chris and Hugh, I appreciate it.
Mark F. Haigh
[email protected]