From patchwork Mon Jun 26 20:46:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13293484 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 C78EBEB64D7 for ; Mon, 26 Jun 2023 20:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 373E58D0002; Mon, 26 Jun 2023 16:46:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D53B8D0001; Mon, 26 Jun 2023 16:46:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1751A8D0002; Mon, 26 Jun 2023 16:46:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 015898D0001 for ; Mon, 26 Jun 2023 16:46:19 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C45F9AFA52 for ; Mon, 26 Jun 2023 20:46:19 +0000 (UTC) X-FDA: 80946081678.30.627D7B9 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by imf06.hostedemail.com (Postfix) with ESMTP id C69CA18000E for ; Mon, 26 Jun 2023 20:46:17 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Z1uS+Wpd; spf=pass (imf06.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687812377; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=2gaBnNp5IBEywyARaUu76L5/AmouMcYydCft2iETs/w=; b=HT6D1AZ7SX8ovkYuFurTiwt5mkn4eRsX9pbi9YNzsVg00Dl+g0s/VqStj7IQMJXF7Xq1pi 9n2kzmtxbtn1sz65jmHY+m4kwaefu5HteiALpS9W9/j7lMqqm00xItmPKam0ptFRV5+4r9 8inkv1Ce8JrSXOuECBC812zrZspP4ho= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687812377; a=rsa-sha256; cv=none; b=tCJZGIBeMvxeY2ZFrhZvlkF+KnT7Pta1KE3uHbd8QDYiE1ckxopP+u8Ky9zep7WUU/dLpo oxTaR9zEtev4EbRUndnVKt659K+dmyRFqcN2rN+StZYoOJmV5+PpHt6HTfxJR43pQJBo+p 99YpE9a39xfGkKvUL/QS2Tp8+G7QGhE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Z1uS+Wpd; spf=pass (imf06.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-3fa99742bd1so12472285e9.3 for ; Mon, 26 Jun 2023 13:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687812376; x=1690404376; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2gaBnNp5IBEywyARaUu76L5/AmouMcYydCft2iETs/w=; b=Z1uS+WpdhZxDdhL8sris3m1/L9QbHq0SHLWsGQwnXANleHoAyDYRhYh1WRtfU5y/TW x1kAYzGFzqawHRlabWEV4rsr9Wctw4yhgdFlRQMukZgOXD6RH+VPQDrCidrf/QR7u636 edAgMbP0v0zoNZdDbdx6WuWMOhPbi3Z02IZtc7OXr+3g9A+Z1L8gNs99ErJQept0HmnH XHuTVLZ7knD8orstCeXrk43WVXUo4jXZzIoytpmEEkSljL7sYRjkGbKX2KH2XNLQ2XDF NxMPxeSC83ITv0h3cLQGqzFefWq1sapQyQDVA3IopTwUfkNmvlHUV9EYhL5da4DzA5f3 vsmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687812376; x=1690404376; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2gaBnNp5IBEywyARaUu76L5/AmouMcYydCft2iETs/w=; b=g8+wzPiJnEZqhE4xOlAcEpaWuDybSELEDwqPy4d3HyfEWYUdorfIJCFkWRskSscs9O vNXNFeoZrZpq0XUx9vbqK97Do7dN/E69U60Edbpy8BZC18DuhkFzi0SVufwhnrmWIP61 AQiRRP+7ODd2IZ/z8Tiw8QXxJ64dkv4lPWlyYgFBJDqSepNesi+CxWXLwqf8jyJUur2c /t6/rkQkuybTAQ/k17+t0zrOxWF09HZO/Ko67mDoR170DzhVStzoVkEmFcJze6Nu6BQ/ NhqDUyo3tOz/MDKFHYWkcd7KKfOm16oyqHxHRrzWD1p9i5i6icZnPf0OIcq7ByqhBj7a py8A== X-Gm-Message-State: AC+VfDxXoZP5TbLBAVHk5JlWc75REwb1zk2MKNOocrZ2Kpyvfo+KiS4h qTH8RCPnKIQWaqp0/jtDoHddcu//pLM= X-Google-Smtp-Source: ACHHUZ4HxYglAVFiYj8XS1l6Cqiegev83efJRSlZtOe9Cet1VjcWI6sSriaz0XBPEDjrnLJze1RASQ== X-Received: by 2002:a7b:c7c5:0:b0:3fa:9554:fb23 with SMTP id z5-20020a7bc7c5000000b003fa9554fb23mr2756440wmk.21.1687812375494; Mon, 26 Jun 2023 13:46:15 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id d11-20020a1c730b000000b003fb416d732csm1111726wmb.6.2023.06.26.13.46.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jun 2023 13:46:14 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Rapoport , David Hildenbrand , "Liam R . Howlett" , Vlastimil Babka , Lorenzo Stoakes Subject: [PATCH] mm/mprotect: allow unfaulted VMAs to be unaccounted on mprotect() Date: Mon, 26 Jun 2023 21:46:12 +0100 Message-ID: <20230626204612.106165-1-lstoakes@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Stat-Signature: f57hq97yconoyad34awcaqtkidrndgy3 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C69CA18000E X-Rspam-User: X-HE-Tag: 1687812377-515687 X-HE-Meta: U2FsdGVkX1/nuY8vxDR/n3BRuNfIhHSYNEnrnlexJCdAW2CSU5pgpzOEEgcOvFKERNmT9u2FO3a7rB79Y8ffCYuCfEkbgdEbIhHo2hcz0ppfJQs9lmERqSNKfLt5TwmGYQw9AQE+9BR0LALqoRYqpGP+uDc0uwS01ttvJ1wlgjBhHtv7+ADsYorId5CWp8a273K6MZEdMY1NUKmQA0acECTbkIx2R2nzPjXFgU+qRzi9faX1P6duxpcNS1LLZvgdWeYDoM0SMnQEH1mPffsYGLY5LlZa0cSiLPlDExdKceqGLWfKnrTZBLsnp8nLFbWMnr8hp5ZWZFXkCa6U3pwiDj/w0g3595KfFo4Ko4z96jJ21CiUFgMlnugVtQmYBM6DKFVhvq3k+c5rOGsdfaWL0xyDJzJi+am/Kqzbs/tqQmmB8ioOPetgeUlvhowS6IKMcCBNsawcEjO6NGeJE0SByotyhlqQibvVH53C994FlR7WsV+5oe1jeCpWlDWh0Wes4mmyqIHtUAFAS6Fv0zwljceY3v4PRluGq2YD+ko9ra2MdsYG6F8HYDUl3YQRfdzegnXgtcVWbFAApu9lM6qpLfhYqOz7GIzgUYCGPaEQ+Pp8KDT0Zw9pjFvcatXTKw/HVZ6EN5dChd3YwRnX7Au5nCv6m+wHrFxI+qtdOI4pGLDrQq1gq8Hi4xmd5q0pSe9Mai7MJOsgi9cpBkWs6vlchNzw7nG8TBZVYqG0xxpgyhCUD62crRccZtg3qgknophmjIwkYrLNblL+nduc/YYUSYY2AlaWxF3GHZwNrlWRRkbT8/4dxpEuKKSPp/b0sFyK8bLTjunDIkCqV1bQX5cWdWeQ9/1zx6fhRlpYtOBPIDXLMFYAFY1lPPb5koszU2IxD0mnff2S9SsLeLLR81aqmJI5c5IDFMlY7YZ/czaiwrK8frEBaG6EIxjc5Z/R96eX/mzX/2FoSJ7AEckgUAf mjC+ZFqV U/9EcIAB2F/ck12kydumworWATo/PUoDe/td0Q2Hl3JZQ/mgHT4f/ygeBGO5vATBYGfd7Yg+LfIQavj0xiVhSHgk0F5KrA23WMh/U0VaejYJmSl/g/vAfZ+fruXRi1L77X8xgUQ5+9OXxq2txjVK+5ie+hyIOqHP8eE1okIDTwjZQQqdbesj5SaxQr6EWnuWAXavPJP+0SFvRCNLAAy9iLsDzWk9f4+loNWydHF+1Iy5X2hI6xbV7pGsIA2iWlwRZFdTRmgTOzzIhz7FtWPUJFkcVK5yzyJfxk2Y1NXR9MZssjMmOMKxZ/k0pREyzaLaMmBhP/BVa6pISQZwKumUefMEAGQTbT/BUyvxmjo9VcdFgcu+9KOXHAjfaX3HmAXeIViflpl8apxltZltD0zJcEURmUBdZEKDp7yQ/3HS3Rf7vVII= 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: When mprotect() is used to make unwritable VMAs writable, they have the VM_ACCOUNT flag applied and memory accounted accordingly. If the VMA has had no pages faulted in and is then made unwritable once again, it will remain accounted for, despite not being capable of extending memory usage. Consider:- ptr = mmap(NULL, page_size * 3, PROT_READ, MAP_ANON | MAP_PRIVATE, -1, 0); mprotect(ptr + page_size, page_size, PROT_READ | PROT_WRITE); mprotect(ptr + page_size, page_size, PROT_READ); The first mprotect() splits the range into 3 VMAs and the second fails to merge the three as the middle VMA has VM_ACCOUNT set and the others do not, rendering them unmergeable. This is unnecessary, since no pages have actually been allocated and the middle VMA is not capable of utilising more memory, thereby introducing unnecessary VMA fragmentation (and accounting for more memory than is necessary). Since we cannot efficiently determine which pages map to an anonymous VMA, we have to be very conservative - determining whether any pages at all have been faulted in, by checking whether vma->anon_vma is NULL. We can see that the lack of anon_vma implies that no anonymous pages are present as evidenced by vma_needs_copy() utilising this on fork to determine whether page tables need to be copied. The only place where anon_vma is set NULL explicitly is on fork with VM_WIPEONFORK set, however since this flag is intended to cause the child process to not CoW on a given memory range, it is right to interpret this as indicating the VMA has no faulted-in anonymous memory mapped. If the VMA was forked without VM_WIPEONFORK set, then anon_vma_fork() will have ensured that a new anon_vma is assigned (and correctly related to its parent anon_vma) should any pages be CoW-mapped. The overall operation is safe against races as we hold a write lock against mm->mmap_lock. If we could efficiently look up the VMA's faulted-in pages then we would unaccount all those pages not yet faulted in. However as the original comment alludes this simply isn't currently possible, so we remain conservative and account all pages or none at all. Signed-off-by: Lorenzo Stoakes Acked-by: Vlastimil Babka --- mm/mprotect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 6f658d483704..9461c936082b 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -607,8 +607,11 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, /* * If we make a private mapping writable we increase our commit; * but (without finer accounting) cannot reduce our commit if we - * make it unwritable again. hugetlb mapping were accounted for - * even if read-only so there is no need to account for them here + * make it unwritable again except in the anonymous case where no + * anon_vma has yet been assigned. + * + * hugetlb mapping were accounted for even if read-only so there is + * no need to account for them here. */ if (newflags & VM_WRITE) { /* Check space limits when area turns into data. */ @@ -622,6 +625,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, return -ENOMEM; newflags |= VM_ACCOUNT; } + } else if ((oldflags & VM_ACCOUNT) && vma_is_anonymous(vma) && + !vma->anon_vma) { + newflags &= ~VM_ACCOUNT; } /* @@ -652,6 +658,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, } success: + if ((oldflags & VM_ACCOUNT) && !(newflags & VM_ACCOUNT)) + vm_unacct_memory(nrpages); + /* * vm_flags and vm_page_prot are protected by the mmap_lock * held in write mode.