[PATCH] VM system in 2.4.16 doesn't try hard enough for user

S. Parker (linux@sparker.net)
Wed, 05 Dec 2001 17:54:44 -0800


--=====================_37142000==_
Content-Type: text/plain; charset="us-ascii"; format=flowed

Hi,

I'm interested in comments and feedback on this patch.

Attached below is "memstride.c", a simple program to exercise a process which
wishes to grow to the largest size of available VM the system can handle,
scribble in it all. Actually, scribble in it all several times.

Under at least 2.4.14 -> 2.4.16, the VM system *always* over-commits to
memstride, even on an otherwise idle system, and ends up killing it.
This is wrong. It should be possible for memstride to be told when
it has over-stepped the size of the system's total VM resources, by
having sbrk() return -1 (out of memory).

Also attached is my proposed fix for this problem. It has the following
changes:

1. Do a better job estimating how much VM is available
vm_enough_memory() was changed to take the sum of all free RAM
and all free swap, subtract up to 1/8th of physical RAM (but not
more than 16MB) as a reserve for system buffers to prevent deadlock,
and compare this to the request. If the VM request is <= the
available free stuff, then we're set.

2. Be willing to sleep for memory chunks larger than 8 pages.
__alloc_pages had an uncommented piece of code, that I couldn't
see any reason to have. It doesn't matter how big the piece of
memory is--if we're low, and it's a sleepable request, we should
sleep. Now it does. (Can anyone explain to me why this coded was
added originally??)

The combination of these two changes makes it so that memstride passes.
Although memstride is a contrived example, it was contrived to parallel
behavior that was seen in many situations, with many different real
processes in normal use. This fix allows those uncontrived programs
to run reliably.

--=====================_37142000==_
Content-Type: text/plain; name="memstride.c";
x-mac-type="42494E41"; x-mac-creator="74747874"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="memstride.c"

I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3lzL3RpbWUuaD4KI2luY2x1ZGUgPHN5cy91
c2VyLmg+CiNpbmNsdWRlIDxzeXMvcmVzb3VyY2UuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPgoKc2Nh
bl9tZW0oaW50ICpiYXNlLCBpbnQgc2l6ZSkKewoJaW50IHN1bSA9IDA7CgoJd2hpbGUgKHNpemUg
PiAwKSB7CgkJc3VtICs9ICpiYXNl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XNhZ2VfcmVwb3J0KCZydSwg
U1oyUEcodG90X3NpemUpKTsKCWdldHJ1c2FnZShSVVNBR0VfU0VMRiwgJnJ1KTsKCXByaW50Zigi
Tm93IHNjYW4gbWVtb3J5Li4uIHRvdGFsIHNpemU6ICVkIHBhZ2VzLyVkLiBieXRlc1xuIiwgU1oy
UEcodG90X3NpemUpLCB0b3Rfc2l6ZSk7Cglmb3IgKGxvb3BzID0gNTsgbG9vcHMgPiAwOyBsb29w
cy0tKSB7CgkJc2Nhbl9tZW0oYmFzZSwgdG90X3NpemUpOwoJCS8qdXNhZ2VfcmVwb3J0KCZydSwg
U1oyUEcodG90X3NpemUpKTsqLwoJCS8qZ2V0cnVzYWdlKFJVU0FHRV9TRUxGLCAmcnUpOyovCgkJ
cHJpbnRmKCIlZCBzY2FucyByZW1haW5pbmcuLi5cbiIsIGxvb3BzKTsKCX0KCXByaW50ZigiT3Zl
cmFsbCB1c2FnZTogICglZCBwYWdlcyBpbiB0aGlzIHJ1bilcbiIsIFNaMlBHKHRvdF9zaXplKSk7
CgltZW1zZXQoJnJ1LCAwLCBzaXplb2YgKHJ1KSk7Cgl1c2FnZV9yZXBvcnQoJnJ1LCBTWjJQRyh0
b3Rfc2l6ZSkpOwp9Cg==
--=====================_37142000==_
Content-Type: text/plain; name="DiffsVm2.txt";
x-mac-type="42494E41"; x-mac-creator="74747874"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="DiffsVm2.txt"

ZGlmZiAtciAtYyBnZW5lcmljLTIuNC4xNi9tbS9tbWFwLmMgdm0tbGFwZG9nL21tL21tYXAuYwoq
KiogZ2VuZXJpYy0yLjQuMTYvbW0vbW1hcC5jCVN1biBOb3YgIDQgMTA6MTc6MjAgMjAwMQotLS0g
dm0tbGFwZG9nL21tL21tYXAuYwlTdW4gRGVjICAyIDIyOjEwOjQ2IDIwMDEKKioqKioqKioqKioq
KioqCioqKiA2Miw5MyAqKioqCiAgCSAqLwogIAogIAl1bnNpZ25lZCBsb25n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X0KICAKISAJLyogRG9uJ3QgbGV0IGJpZy1vcmRlciBhbGxvY2F0aW9ucyBs
b29wICovCiEgCWlmIChvcmRlciA+IDMpCiEgCQlyZXR1cm4gTlVMTDsKICAKICAJLyogWWllbGQg
Zm9yIGtzd2FwZCwgYW5kIHRyeSBhZ2FpbiAqLwogIAljdXJyZW50LT5wb2xpY3kgfD0gU0NIRURf
WUlFTEQ7Ci0tLSAzODksNDA1IC0tLS0KICAJCX0KICAJfQogIAohIAkvKgohIAkgKiBTbyB0aGlz
IGlzIGEgc2xlZXAtYWJsZSByZXF1ZXN0IGZvciBtZW1vcnktLXdlJ3JlIHByZXN1bWluZwohIAkg
KiB0aGF0IHZtX2Vub3VnaF9tZW1vcnkoKSBoYXNuJ3QgaW5kaWNhdGVkIG1vcmUgYXZhaWxhYmxl
IFZNCiEgCSAqIHRoYW4gd2UgYWN0dWFsbHkgaGF2ZSwgb3IgdGhpcyBwcm9jZXNzIGNvdWxkIGVu
ZCB1cCBsb29waW5nCiEgCSAqIGhlcmUuICBPbiB0aGUgb3RoZXIgaGFuZCwgdGhhdCdzIGZhciBs
ZXNzIGRldHJpbWVudGFsIHRoYW4KISAJICoga2lsbGluZyBwcm9jZXNzZXMhCiEgCSAqCiEgCSAq
IE1vcmUgaWRlYWwgd291bGQgYmUgaWYgdm1fZW5vdWdoX21lbW9yeSgpIGRlY3JlYXNlZCB0aGUK
ISAJICogYXZhaWxhYmxlIG1lbW9yeSBjb3VudHMsIGJ1dCBhbGxvd2VkICp0aGF0KiByZXF1ZXN0
IHRvCiEgCSAqIGNsYWltIHRob3NlIHBhZ2VzIGFzIGlmIHRoZXkgd2VyZSBmcmVlLgohIAkgKi8K
ICAKICAJLyogWWllbGQgZm9yIGtzd2FwZCwgYW5kIHRyeSBhZ2FpbiAqLwogIAljdXJyZW50LT5w
b2xpY3kgfD0gU0NIRURfWUlFTEQ7Cg==
--=====================_37142000==_
Content-Type: text/plain; charset="us-ascii"; format=flowed

Cheers,

~sparker

--=====================_37142000==_--

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/