From patchwork Sat Oct 7 16:47:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13412422 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 BAA72E95A67 for ; Sat, 7 Oct 2023 16:47:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D03C26B02C0; Sat, 7 Oct 2023 12:47:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB3CE6B02C1; Sat, 7 Oct 2023 12:47:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B53FA6B02C3; Sat, 7 Oct 2023 12:47:56 -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 A3CE86B02C0 for ; Sat, 7 Oct 2023 12:47:56 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 72C52C029F for ; Sat, 7 Oct 2023 16:47:56 +0000 (UTC) X-FDA: 81319247352.05.8CE8BC4 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by imf17.hostedemail.com (Postfix) with ESMTP id A60EA40005 for ; Sat, 7 Oct 2023 16:47:54 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=P7Fwq3Tn; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.48 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=1696697274; 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=oPzbdkXzux7hGqpntQrbYwU9cFGZ3zqwfLnYy+s7r78=; b=dHgb1uk7MtIFyPdwLD034PM8tzy+gWhkSId1wD7MabjE0RJoTxP8DdUIs3MOMo+nt8Bw/g m8jA4AV7WxrHhn+7RDvx4b46SuD2PJML9JWyh0ZJrGqMaDPqtpnlFAoQdn7TLpEl8IXjF0 7SlVdA0cN0uYcnWdlYFsvveijr118Dc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=P7Fwq3Tn; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696697274; a=rsa-sha256; cv=none; b=YaaR0OVdOtLegYg1GzyCJ3e1WO1QCcWI5q6gn6sFlXwfa3cJfZybxJNDvUSJp1vMbO2Pi0 crxECVP4uJ/WIu+6SiT9klTmkXI7MvAOR3OQM8YPDJ0wQiwWm539gvKGiWfjpQXlx8t6CX Z9Dx/I6n+D+UPYgPVSTVz5n4MKcDdwo= Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-307d20548adso2888793f8f.0 for ; Sat, 07 Oct 2023 09:47:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696697273; x=1697302073; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=oPzbdkXzux7hGqpntQrbYwU9cFGZ3zqwfLnYy+s7r78=; b=P7Fwq3TnkcRx9tQTK0OEbKaXg3kCCqKjM979ZMOvIPspIXZ2c3vj5TRDA/aetYRE7D UZN/z637GkEJSTxL8RwhMCjxu9ifnCiK68ImfIpKURbqYFGWKLiYbkjj0i2jwTXdHQKk TE1OyvzXAT5IUz33e9UTxzZYEgYMlQ2aEGatQIbMGRvjPU/iFBDlx9bv39t1wp8pC1ym XhWfGxHmGhcl8zdb9BEM6CO3U7/JLZvtgOMLwa6SzKTzyyVgTLTcP0uvaqEQQhlv0XC+ c/prLWrCKn6VodOrGqzaD9FSfpvdzKuwQZNqf59vmPrzJRArXQfYPxZNVMi/gUV9qDqg VYxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696697273; x=1697302073; 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=oPzbdkXzux7hGqpntQrbYwU9cFGZ3zqwfLnYy+s7r78=; b=w5Ehb2R+1Qp8ydii1s64Qb71QE43D1NFUXyraAhHKUHcGESEDitq/uKDhdjcqe3SIO KKN/sxJMDr+cou+iqttsjv2/zYyfEDuvwTpc13AWyzprtJpNrqXL7x1r82SBw41n7Hlm NUiFf89cDGPI65n1aXCVGWepWACEIxKSZSS8Ti0uUlxbx9vvDUXiYFl5x7Vw8ct2aLRW sEpLSblJEoaMakdF+k3j8P3idqIzFIORf28hSMp+LKi8x6UY1BDqgcGMIKLUx3hTNicG FsJlGMKjLc+NbnPTEIrffyPlrjBis4nzqzUfMzDvcWJKa+DaZFu52z/7n6SKxmsgfhyL bvVw== X-Gm-Message-State: AOJu0YwejMLkObwK+hA4DAfYDIylZ7t8KN9Jg6WQxmg9PF9TzSPW37/w MipyZXKKX7zY2XBqCgtk5GZfRzaZ1hE= X-Google-Smtp-Source: AGHT+IE0iF7TNrIoG00cme4DOEVEjXbhmV7pVKEgVWJYcUTViq8s5rmvK0WqRhGIqmNsNVq19Mvtfw== X-Received: by 2002:a05:6000:186:b0:31f:c033:686c with SMTP id p6-20020a056000018600b0031fc033686cmr8884147wrx.49.1696697272334; Sat, 07 Oct 2023 09:47:52 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id x14-20020a5d54ce000000b0031773a8e5c4sm4694181wrv.37.2023.10.07.09.47.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 09:47:51 -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 v2] mm/mprotect: allow unfaulted VMAs to be unaccounted on mprotect() Date: Sat, 7 Oct 2023 17:47:48 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: A60EA40005 X-Rspam-User: X-Stat-Signature: e48k7g4pmfxy96mg94n4ftcdf3xiydsh X-Rspamd-Server: rspam01 X-HE-Tag: 1696697274-533584 X-HE-Meta: U2FsdGVkX19QPQ3YdR9d/ZycezP8vcltZBzirNZX8VWECOObZWuaQWgWU7l6vnrjPelzR/FwhMYuzjRL3LY/hS+fcWpJXmssdNDefPUp/DlHAQ++ztywUl+btBSJSlio3xsvTc6PUBXoiuyRbAiaQXxIkZzViVS1YHW9ZpQ77tzF4z+CAe2VzBRzwUOED495N7ee230WB63juKFN6VlYY42678dEtZ9s+9IRfx6WN5F0hHHbRPU0kd7XW8pWSZFNMlqefMat3cjlH394CU8SwySQw3v2ER5HqgGOxoF0Hcp4h/H1Nt/vBwMkxY+6YDKnYiVYbYYAmEtAqYaqSteQVrKi+oGTHkSSxP3vhbIp6QbKbNYfuuMPPu7Q9X0A+eRgjIylgaWLuWzzfmeCEw/7UyTsym3T0UPlhPVLV7/QKwMiVT1UQGZ3Jq/9jDfsONwvi+0FaKcA9/5CKuituP9WZS6Caokxbilt5lOSMr9F3bNSvpVu4HwJ31b7lkSaKGisQzhuouAnB/sDv5GYtuLH/5iIIGglL3N78FIh9KNttY7hpy42UAuoY/k/q29OFEefUS0Voy/9z4CpknPY1v8f6EgJOOVdTS+jBzIKx9BkQP4hqMTr5BESBJHA3ntQcfS8EwlPGzFMrz7yz9EAX0uoEHuhUiEnBlqr8HWYbwpQCAl8SnjtFyLJygdpYNBGAjeArs2MK15IzFU8n3HrvJ1s5MBfVwwegiZtKzLXm22WPkfJUPkDjnSMHbyLIDWOIPT7HeqK4+Vu0BiK9bKGOAuZFRVCVlIz9xSHGAFcI5/np93OoUHQriXqgOYTUAxGI3J+jQzlTPpLjoHz+UHbZ+9Mlp7k3ADC33YT5msssHDw+0K7ZcHGu3nZicTX3nvBWrEmIprcJ+wESh7DcqEIKud41q1x8HsbgDBRHDw4BAHKj7h77gLhxiAtqvWtblhysLLgT1NUasVV3162stOdc5F 4vkoFMOM 4YXaHQZnHwX62OVb/9xMpTo6KyCxOO8AHIr1USIAonuVu4OflKEQ7SxQzwMo1ea1nMJPFIkLKuX5nKJDP5WiFfP/V3MYIGvGjBtG6ig53jBDoadDmLodOqBeN7KnfA5myRkr0X2dBZ2EPaL4fuWxuI/jJahPlMmD1t7hjCAdQvSfwcfi3hNPAzqvcqwzax/14z0DdxOwvcAjKRwDC6hLgmTmvtcpyyDvdB+C8TQRvJYALWh+6JSxf7i/XzN4Cf898SMd2bhvaSlruF1IXjGwnwN10B76hHlVyEiLnMyJoZ8w+AQhXQLcciOBlp5WMl/jXYzsGdBdWWc8QcqGC22MhVPNCWB0TL7DoBdRZeZTCOPxmOKT/NAc9L4q6EJixepNPS3AnYckQF0HVq+pW084kk4hiR1i/+bPoRMUZXlGUwxPbLb5QEU/4SQzqrebl8Stt1+afN3RKOQTVMV07VC+QFOKmknYaQCZf1FYcL9bP7cmun+c= 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 are conservative and account all pages or none at all. Acked-by: Vlastimil Babka Signed-off-by: Lorenzo Stoakes Acked-by: Mike Rapoport (IBM) --- v2: - Minor spelling correction. v1: https://lore.kernel.org/all/20230626204612.106165-1-lstoakes@gmail.com/ mm/mprotect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index b94fbb45d5c7..10685ec35c5e 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -608,8 +608,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 to be 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. */ @@ -623,6 +626,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; } /* @@ -653,6 +659,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.