From 464f4e7893c4a88b6bdf8c241358b91d4fc79942 Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sun, 20 Apr 2003 00:28:39 -0700 Subject: [PATCH] [PATCH] Disallow swapoff if there is insufficient memory From: Hugh Dickins First of three small "stop swapoff" patches based on 2.5.67-mm3: stop swapoff 1/3 vm_enough_memory? Before embarking upon swapoff, check vm_enough_memory. Mainly for consistency in the overcommit_memory 2 (strict accounting) case: fail with -ENOMEM if it wouldn't let the amount removed be committed. Will always succeed in the overcommit_memory 1 case, as it should in root-shoot-foot mode. In the overcommit_memory 0 case, well, I don't care much either way, so opted for the simplest code: no special case. Which means it could now fail at the start; but that's unlikely (case 0 is over-generous) and only when it would have got stuck later on anyway. --- mm/swapfile.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index d646f7cb5340c..6cc7a47b6eba1 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -1028,12 +1029,18 @@ asmlinkage long sys_swapoff(const char __user * specialfile) } prev = type; } - err = -EINVAL; if (type < 0) { + err = -EINVAL; + swap_list_unlock(); + goto out_dput; + } + if (vm_enough_memory(p->pages)) + vm_unacct_memory(p->pages); + else { + err = -ENOMEM; swap_list_unlock(); goto out_dput; } - if (prev < 0) { swap_list.head = p->next; } else { -- 2.53.0