From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37BD6C43334 for ; Fri, 17 Jun 2022 07:34:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 881696B0071; Fri, 17 Jun 2022 03:33:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8311A6B0073; Fri, 17 Jun 2022 03:33:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D11A6B0074; Fri, 17 Jun 2022 03:33:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5D77B6B0071 for ; Fri, 17 Jun 2022 03:33:59 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3B9BD20A00 for ; Fri, 17 Jun 2022 07:33:59 +0000 (UTC) X-FDA: 79586913798.07.F6091EE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 77834180085 for ; Fri, 17 Jun 2022 07:33:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655451237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U1oqihi25ZAmVelpomlVi58IVrIjPbxaxEmOpvII5S8=; b=B8A97pCii7NEWSKf2lGX9oBIYJ244C/hG4zz4qFeB37pMsFISycC784r81+czgpKIkXIk2 jSjuklp6uY/RyXWyDZBtis62PpDhQK2raomADtNDavcIER7eAmWAoTtGDpVbA8P2G1wHXC iWSxfHxr91f+7MlsQRnH6dNi9nzReJc= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-126-AJtdhzNKOF6mLleAhSNd7w-1; Fri, 17 Jun 2022 03:33:54 -0400 X-MC-Unique: AJtdhzNKOF6mLleAhSNd7w-1 Received: by mail-wr1-f72.google.com with SMTP id s7-20020adfbc07000000b0021a558c09b4so315784wrg.4 for ; Fri, 17 Jun 2022 00:33:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=U1oqihi25ZAmVelpomlVi58IVrIjPbxaxEmOpvII5S8=; b=jxI0I2rwX49Uw+swVjbczPYTo0c4H8qTuEW9Lh5bjXeY5JHoosSuC0GwUiJHHkHRQs SF80hLKqPDeOQh6nBJozJuBP1oGmkBRlDA4aUVH9coaQNYdX3BzLZSuwTsPhf/DV/N/M BhgIGfDIUbRk/ozT1A/LNBjgA44JmqVmsVOwxnoGgfYOuBiJRxti/8TijECtHetuaJGq sus4KrPjE6HidPRSeyA70AaDiX5sN56oaErVrWiL2M62m/fs0NYrI0Uppbf/XjK+kFXv sS0HpJLsTWLkv5bbNH7w/YOxMD1X1XIfly5lRFl4wR4nmXuZMNbBpKE2uCrdrI+a2s9K ZWsw== X-Gm-Message-State: AJIora8GxgIfcARkks7SfAqkdqa+CsDqjkTBlfORpctkeynFUtr+R+B2 Hr1UAaLm0EwatrXCbKVxZiWslGulv1xANlU96MWVgfTq4NfYWOthGJ3KTFREVi6kaEF7cxXMqxe CAdKrRVFtU+o= X-Received: by 2002:adf:f704:0:b0:211:7eed:4412 with SMTP id r4-20020adff704000000b002117eed4412mr8050531wrp.165.1655451233022; Fri, 17 Jun 2022 00:33:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sVPZa04KCjXqes8L1HH3PmDvnvIn7RrOQ/UXO776OYHW4fRBWpuzuSN29EGefiVQ7Hf0C2sA== X-Received: by 2002:adf:f704:0:b0:211:7eed:4412 with SMTP id r4-20020adff704000000b002117eed4412mr8050508wrp.165.1655451232694; Fri, 17 Jun 2022 00:33:52 -0700 (PDT) Received: from ?IPV6:2003:cb:c70a:7e00:bb5b:b526:5b76:5824? (p200300cbc70a7e00bb5bb5265b765824.dip0.t-ipconnect.de. [2003:cb:c70a:7e00:bb5b:b526:5b76:5824]) by smtp.gmail.com with ESMTPSA id ay1-20020a05600c1e0100b0039c60e33702sm4516697wmb.16.2022.06.17.00.33.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Jun 2022 00:33:52 -0700 (PDT) Message-ID: <76e468b4-c6ac-426c-7ec9-99c620e08cda@redhat.com> Date: Fri, 17 Jun 2022 09:33:51 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 To: Miaohe Lin , akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20220608144031.829-1-linmiaohe@huawei.com> <20220608144031.829-2-linmiaohe@huawei.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 1/3] mm/swapfile: make security_vm_enough_memory_mm() work as expected In-Reply-To: <20220608144031.829-2-linmiaohe@huawei.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655451238; a=rsa-sha256; cv=none; b=hDQHPnBrqN3BLFXj6/gtIyzN11dEFKkV/IrvhFZ7bZI9TuZt78dRgTqA4lKQVikTiF/Qcw dZMwz2fYTnWoTNlxgOV0p9XPAnMoJ5QgnFBQ3bANO1KTf5SyElB+Bj2KUrkPeAviMfNdNA llniWCATIH7TXFIVkgL98ekql7jrV9A= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=B8A97pCi; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf16.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655451238; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=U1oqihi25ZAmVelpomlVi58IVrIjPbxaxEmOpvII5S8=; b=LNVBZ8RFjiCZLeMJZbptsIoI5pJ1SPqgmWe33tBdDi5K6l1AhBLXGXGNvkpysOAg0Oklww 1Xtxb8YWVIWghEq47haYvCxXAycLayImPxal7izSOOtDikGhaKWkzGpNntEY2ljp/zxkv/ SW/uG4h0ERs804PyT+1M0Cd/Mw6MEyU= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=B8A97pCi; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf16.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-Rspam-User: X-Stat-Signature: c4hx1oaowmn4zyym7mf6wajza9hcah8c X-Rspamd-Queue-Id: 77834180085 X-Rspamd-Server: rspam08 X-HE-Tag: 1655451238-766401 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 08.06.22 16:40, Miaohe Lin wrote: > security_vm_enough_memory_mm() checks whether a process has enough memory > to allocate a new virtual mapping. And total_swap_pages is considered as > available memory while swapoff tries to make sure there's enough memory > that can hold the swapped out memory. But total_swap_pages contains the > swap space that is being swapoff. So security_vm_enough_memory_mm() will > success even if there's no memory to hold the swapped out memory because s/success/succeed/ > total_swap_pages always greater than or equal to p->pages. > > In order to fix it, p->pages should be retracted from total_swap_pages s/retracted/subtracted/ > first and then check whether there's enough memory for inuse swap pages. > > Signed-off-by: Miaohe Lin > --- > mm/swapfile.c | 10 +++++++--- > 1 file changed, 7 insertions(+), 3 deletions(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index ec4c1b276691..d2bead7b8b70 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -2398,6 +2398,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) > struct filename *pathname; > int err, found = 0; > unsigned int old_block_size; > + unsigned int inuse_pages; > > if (!capable(CAP_SYS_ADMIN)) > return -EPERM; > @@ -2428,9 +2429,13 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) > spin_unlock(&swap_lock); > goto out_dput; > } > - if (!security_vm_enough_memory_mm(current->mm, p->pages)) > - vm_unacct_memory(p->pages); > + > + total_swap_pages -= p->pages; > + inuse_pages = READ_ONCE(p->inuse_pages); > + if (!security_vm_enough_memory_mm(current->mm, inuse_pages)) > + vm_unacct_memory(inuse_pages); > else { > + total_swap_pages += p->pages; That implies that whenever we fail in security_vm_enough_memory_mm(), that other concurrent users might see a wrong total_swap_pages. Assume 4 GiB memory and 8 GiB swap. Let's assume 10 GiB are in use. Temporarily, we'd have CommitLimit 4 GiB Committed_AS 10 GiB Not sure if relevant, but I wonder if it could be avoided somehow? Apart from that, LGTM. -- Thanks, David / dhildenb