Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp162577iog; Fri, 17 Jun 2022 00:46:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sYbtRW40iFkMlgXVRDQHTMhKZd3yuZWqVcwnCE5T6dI8jFaPBWG9ovuPocQ6NDYvYNuaw0 X-Received: by 2002:a05:6402:11d1:b0:433:4a09:3f49 with SMTP id j17-20020a05640211d100b004334a093f49mr10625633edw.357.1655451986110; Fri, 17 Jun 2022 00:46:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655451986; cv=none; d=google.com; s=arc-20160816; b=OWezjJ2rshfNpXNj3igvnxPL59wAyuFCwbBxLviTXmvBeqBi7d5cS2JdByIJV+nByn KyJ0H2zT1pTj6RHeJLJLPgtUgO/QHbK9HExN3mwXjMkN070HP/9RVSNfy+lrPREjdox/ IsbMZeXYZE7a0CVVf8O7qq5rxVQdIcEvLPZiiWB6suwcBTWgG6Gem7vg7lQKiiNjQT28 x2hQxEkizeWtF7/ZuQfwrMcQqfGtunQFPWH52FfNcNnJfih1x61gE7K6cix1VNsPsIi5 IPUtcfPrSjVAvPcq5Egvt/RKzuV7UP6jyhQ8DCQetj4/u29NqNO5o3RLqUE8JfiHq65n CHsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=U1oqihi25ZAmVelpomlVi58IVrIjPbxaxEmOpvII5S8=; b=mCwAog10FfUPGl0+1VV8KrtcYhV8ffolOAw/1qlmtmRwleKRLXPWNAWTx9yOXN51nE V7T5fmffRx7NDkH3jHZixdkoASZYNz65jAfeBy15K7wdDvIEdOWRiuZEl3S3faHMxGHi 06AWKsOX1nhxGnL7I2BtVKFtP+m1mber+zBUBBmnM30NyfXxTBl7idaE3kJ6q10iniOc SzYpuotPKp9avopUUr43PlRXOcFXY1xjp0fcCqLWQ4VrAGqhr9CwX5loztRCFgh3MxHU 8HwNMe21/GFUNMZvxWeH+nTkjuhSXAijNIkg6Rgsb7EXPBgJxyZeUhs4OjOs0EA9E/yd 6TeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WzCNcaqZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a05640250ca00b00435201d9705si5143507edb.602.2022.06.17.00.45.59; Fri, 17 Jun 2022 00:46:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WzCNcaqZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380539AbiFQHeD (ORCPT + 99 others); Fri, 17 Jun 2022 03:34:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380228AbiFQHd6 (ORCPT ); Fri, 17 Jun 2022 03:33:58 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 750714A3C1 for ; Fri, 17 Jun 2022 00:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655451236; 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=WzCNcaqZeanjtJ6ygXM2xVqpmpEGKmB9T/Zpf2ACAPmNavvM5B6YM7oWu7yp6uc1vu3s9S teUkViSkziiBZqorVVAYpY7lvv2S/1cOUsnFcHVgXDw+WohWmC3g4wDrOxTuz8hwv83zn6 at6oh96tf8W7R31iI3NxN5s2emkZRVc= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-125-ThgQpbt9O8GnTmCBYhvxqQ-1; Fri, 17 Jun 2022 03:33:54 -0400 X-MC-Unique: ThgQpbt9O8GnTmCBYhvxqQ-1 Received: by mail-wm1-f71.google.com with SMTP id m23-20020a05600c3b1700b0039c6e3c169aso1863607wms.2 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=O8hIrk0w5rb7Re4hguagVtyA151CTudpB2p4hxQIwgb68q8fKS/9ykLx0Xafqandze utnEjsOjpdiJlx9tfVRZGUcWB0IbrJzFq+f8uFnADFk8Ry0NLpqieWZBMNIuZ0870BjI JYigNcXacktTnZX9X26F4HIFhup02NhSiLSqF3Dor+e3Fzkl5iFaSzVP+h9pCuWMedOQ ALZeTr0IhLjj4CKmplKHQR42Ep8yEVBXLdnSevfdIQHakD+OhpKJ8X27fgOqwCf3hmOb +JhMhbtqhU7iC/CkBOljNKh5/sj6WBTscYjsAEK3q4zn7MERirZfsVzepfZQhirC8oge cxXQ== X-Gm-Message-State: AJIora/GF575Y100O5u5gKoBtsGecE9tvTtwKpcURY4cOGuAlaLbZypm WJ8sTDTEb+sT1LsPRUnPt/RH76x1vtCURwnJWCEILmAnZsf7Ywep+8Ox4vE/RIg2oorNgKoSbyT jesnM397NX5Da7wS3sOM9l9y0 X-Received: by 2002:adf:f704:0:b0:211:7eed:4412 with SMTP id r4-20020adff704000000b002117eed4412mr8050532wrp.165.1655451233022; Fri, 17 Jun 2022 00:33:53 -0700 (PDT) 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 Content-Language: en-US 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> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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