From patchwork Wed Jul 3 21:29:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13722799 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 75C77C2BD09 for ; Wed, 3 Jul 2024 21:29:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85A356B008C; Wed, 3 Jul 2024 17:29:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DFC86B0092; Wed, 3 Jul 2024 17:29:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E3FC6B0093; Wed, 3 Jul 2024 17:29:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3D6CC6B008C for ; Wed, 3 Jul 2024 17:29:33 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E4025140A9C for ; Wed, 3 Jul 2024 21:29:32 +0000 (UTC) X-FDA: 82299732984.09.6EDF4ED Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id CBFCC4000F for ; Wed, 3 Jul 2024 21:29:30 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="iyegTmQ/"; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720042147; 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=VpPwnVRxGccST937wjacfAT3ndiJzOREHKlh4Jm8zSE=; b=VZohN9q32WJvL4xGHjsrQ0t0JqMuV9egnFGqJv49ogaJm4uPxyPpgR4HZSOE2ecEkH414l lgMjjwNNPQjV9+cBtouzD/CQQJrwLBCKzJoOdK2nFojiWR60mFRYK+JmNnC/XPMhOEX//X TMU0lyLlCZcugCFYeYdRk2O+NIKbRjY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720042147; a=rsa-sha256; cv=none; b=xNT3hs8p3v/KexEIfPNeA2kMYVU1vShfKpRuODjE4A5IJ4Wy/4UcfFoxI71xIi+bMfTiZD UznPGTTrw04xebxtMDmENzx0t0uOC600Q5ZOnn9zVehW3D7poI4pqKgwktQZLSlkku2KOC cargu+O67q7j/9q/P6jbMzMYCvefEzI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="iyegTmQ/"; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720042170; 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=VpPwnVRxGccST937wjacfAT3ndiJzOREHKlh4Jm8zSE=; b=iyegTmQ/+x//h1iLh/xkzxuRmTa0+vAZP4ASN1cINCV5f2zK6uVPV8byCH2inkTtA1VVCj ctFCfqluv8ijojQ7RkBONQMVRCE9gvKDTICF9LQ6B2NVZQILZ5KH+wZe6KlmB/rjvgsCNc S05SrR0SKPfhiuPMyqzjHaWba4rDS84= Received: from mail-oo1-f71.google.com (mail-oo1-f71.google.com [209.85.161.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-669-MYDrrYEwOHSX9txzf9KJtw-1; Wed, 03 Jul 2024 17:29:29 -0400 X-MC-Unique: MYDrrYEwOHSX9txzf9KJtw-1 Received: by mail-oo1-f71.google.com with SMTP id 006d021491bc7-5c21b6db74dso1147837eaf.3 for ; Wed, 03 Jul 2024 14:29:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720042168; x=1720646968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VpPwnVRxGccST937wjacfAT3ndiJzOREHKlh4Jm8zSE=; b=PcKjamLUgRMXGrTKLpP24VKB1XhXJIUyvG28iaPS9UC57/tyRS4gyMPGH+mb6zo3YA tdLRvkNshnJ05qMt1T3LzgWg9K4rY5iGCzmQovvRSzMZ/NHsB/GEA7MF+ra9OMWKow54 TBtk+QabjP0MHppK0l3abIqyAdZ9U+5iOidD06hElNkgM0A9XMKHkVa3CAaZAaoKfMMZ EG+/Lx4cJggYKLhk8TUE7ATvbjP9uHuSAltq7PvpqpEurWdDSXhZzbC4HdEQSeZBlgx3 9Ey38y7FgqelG2GO3NOywVsPYf7EAEyh+IQo1BTf8udgxtn82x/OEF+4PlfY4KxKtWN+ Ukow== X-Forwarded-Encrypted: i=1; AJvYcCVQonNRvPFmHu7K0w3oVVpFna4JlIBNa3p/bSxc77c/0eUnAZ3xGCI5ERuY4Qti7PMtIYcrqou+akS+liSc30wlwb8= X-Gm-Message-State: AOJu0YyxVuQ2uKcI94APxuLvj/TWTNuMhRk3E4kpyzbAZpfbv5cHxFV9 QqUUjHxFDPEv7b+rKjom5Et1FYoirx1+1Y0eU4JRQTZNc3X7y+F6jjToP52cK1Nu4ZJFeLK2oVT +l/N1VOoStYrQYEScLLM6dzIrbxVdPOw9G7iu5SqXej80j/+v X-Received: by 2002:a05:6870:f112:b0:25e:15e1:35c6 with SMTP id 586e51a60fabf-25e15e13956mr2491359fac.0.1720042168250; Wed, 03 Jul 2024 14:29:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFy7qnRAv+DeYDujdDipaXvn8J32B6JV16U/qU1i2FGfDDVRUJs42vciaW1uTjUnBxFRGp+Ow== X-Received: by 2002:a05:6870:f112:b0:25e:15e1:35c6 with SMTP id 586e51a60fabf-25e15e13956mr2491322fac.0.1720042167878; Wed, 03 Jul 2024 14:29:27 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4465143eb1csm54337481cf.57.2024.07.03.14.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 14:29:27 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Matthew Wilcox , Mel Gorman , Dave Jiang , linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Rik van Riel , Vlastimil Babka , Nicholas Piggin , Christophe Leroy , Andrew Morton , Huang Ying , Oscar Salvador , "Aneesh Kumar K . V" , Thomas Gleixner , Dave Hansen , x86@kernel.org, Ingo Molnar , "Kirill A . Shutemov" , Dan Williams , Borislav Petkov , peterx@redhat.com, Hugh Dickins , Rick P Edgecombe , kvm@vger.kernel.org, Sean Christopherson , Paolo Bonzini , David Rientjes Subject: [PATCH v2 3/8] mm/mprotect: Push mmu notifier to PUDs Date: Wed, 3 Jul 2024 17:29:13 -0400 Message-ID: <20240703212918.2417843-4-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240703212918.2417843-1-peterx@redhat.com> References: <20240703212918.2417843-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: CBFCC4000F X-Stat-Signature: qq9ehi7y8hfa4be3jku6m3pkzjnhz3hg X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1720042170-956880 X-HE-Meta: U2FsdGVkX19VCZ7JHSVaEez0KNoLYjzLrn6owz4wpoM6JBDDwS4AYcYA6vwm7vKu+qMi9oz1fV9tF0k98ZUoP0lNL1XKIX1YmdGYhIyXCJmaNJ+rEW5piB6e8n9TJvHSqcEKBU6+3qQ3IGqNZeKSA3Aja9FP3SVdsWUTwBRzJ/XRCH1io0PJ/4Vakjdpc+8rGB1ZFC0BdObkvTgoWsdOPFDTFKZiqIFFJbCYqjggtQfslSADHMOaMi1jkHpQ247Sef8JHOZ+b8Yx45RPvCDVcHI60BVQ7LKPrUUqEkpkcH9ioUMeeQJefkSAJ/P2g/XiWuqZCjPYfPKoTFcLuD9koKR/mi3/DHIyWGqZD98sh/zGugYzKeWs6k0lQl74AdH+H72zhfxXnvF+kmqIPZpHpKVfXIv4aeCIXjYl4AKZlHH8qEsUKaub5kOmyHaGGJvwgQZ9xWglax0wSBb2/IAtJ1G1sFAfHcrx0vGJwUyrIJYdo7phOPzWW5AAMoTPoStv/dIjjlLi6b5g3wJ1QmIkAnkyBXCYKodIdgOPivuIu4mNRErYV7bdp0gUoO7yO80/0Vi74BDKiWCWI9ZcWkqisZXz7nRkozGhfMwiM82ISEnMqEnmeKE9s+KTFx0/i0fhBrZUQYESJK46yAO9zTuohxCD2jYr+3nBpofJ88s/9yy216XGJfQ8XGKL58NGyyXeehUziuVfxjlTVlfDFPCiYoVC+cHPcqf9bRaseuTfcqWnN1WaWjJDeGZBnlOh8sUFvMGYyNUa52Q4rsdesyTWLqXea+7i+Kd/XW6yxxCBpoRh//qAFNBXmgKOEpuWC7wjUYib4CzGBGHE0Ea8aPxQm+6o1a/wrfAllww0IgGHeXkXXnSbaRgHuhSZDus1ZTL3j6OTofUTsanRNdH3tv2b7Bu2yOke8cUZi8T4ta5fb34thBrSG/+HclyEnWbHvrvFd/7fBkY+VDDzPx13D7a e3GOmMad vPpXp7j/8y5PwDlO9y+PQ6UhtCxN6Z+uvlfJhartzM1a7Z4SCqoLcf/hQ3i4DBIn6W7LAAUzn+BpteTzndbeysUH0vUGwok8UzbkVWAHd7rC8BaR6TdS3G9glmoifE7JZb8B6fWLyQiPdek54cdtTyW0wtsGb7ISkZVWNIbRlLDg0sblezxuWGM4O2d55uK2j24wvK1jyxzlBEsllnxSMoxPaWSh3wWhcN75MaQkHcFuDwuchy43xDe6pXAHVDDjT5OJhtKM8wvVtpGo0ElUe9tqWjZxUkID4wMVcLZ5gGqBVYB0lp7R8Gdhc6FJp1kOdxpcm6MajRU1Q7WPGgP6itNHzbh6cx8b4KJo9+Wbzpk6irm1zYEaU+FvzcjV0QgDQ6UvPi5I0WFryXy9a0L0jlaG2ZuhRdfzdEGGa/7EqquL6EteioKBaxMF3BVqg54cXXgznLPPLkC8PdoUS8OqEvBItGto+BsyWZDny 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: List-Subscribe: List-Unsubscribe: mprotect() does mmu notifiers in PMD levels. It's there since 2014 of commit a5338093bfb4 ("mm: move mmu notifier call from change_protection to change_pmd_range"). At that time, the issue was that NUMA balancing can be applied on a huge range of VM memory, even if nothing was populated. The notification can be avoided in this case if no valid pmd detected, which includes either THP or a PTE pgtable page. Now to pave way for PUD handling, this isn't enough. We need to generate mmu notifications even on PUD entries properly. mprotect() is currently broken on PUD (e.g., one can easily trigger kernel error with dax 1G mappings already), this is the start to fix it. To fix that, this patch proposes to push such notifications to the PUD layers. There is risk on regressing the problem Rik wanted to resolve before, but I think it shouldn't really happen, and I still chose this solution because of a few reasons: 1) Consider a large VM that should definitely contain more than GBs of memory, it's highly likely that PUDs are also none. In this case there will have no regression. 2) KVM has evolved a lot over the years to get rid of rmap walks, which might be the major cause of the previous soft-lockup. At least TDP MMU already got rid of rmap as long as not nested (which should be the major use case, IIUC), then the TDP MMU pgtable walker will simply see empty VM pgtable (e.g. EPT on x86), the invalidation of a full empty region in most cases could be pretty fast now, comparing to 2014. 3) KVM has explicit code paths now to even give way for mmu notifiers just like this one, e.g. in commit d02c357e5bfa ("KVM: x86/mmu: Retry fault before acquiring mmu_lock if mapping is changing"). It'll also avoid contentions that may also contribute to a soft-lockup. 4) Stick with PMD layer simply don't work when PUD is there... We need one way or another to fix PUD mappings on mprotect(). Pushing it to PUD should be the safest approach as of now, e.g. there's yet no sign of huge P4D coming on any known archs. Cc: kvm@vger.kernel.org Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Rientjes Cc: Rik van Riel Signed-off-by: Peter Xu --- mm/mprotect.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 21172272695e..fb8bf3ff7cd9 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -363,9 +363,6 @@ static inline long change_pmd_range(struct mmu_gather *tlb, pmd_t *pmd; unsigned long next; long pages = 0; - struct mmu_notifier_range range; - - range.start = 0; pmd = pmd_offset(pud, addr); do { @@ -383,14 +380,6 @@ static inline long change_pmd_range(struct mmu_gather *tlb, if (pmd_none(*pmd)) goto next; - /* invoke the mmu notifier if the pmd is populated */ - if (!range.start) { - mmu_notifier_range_init(&range, - MMU_NOTIFY_PROTECTION_VMA, 0, - vma->vm_mm, addr, end); - mmu_notifier_invalidate_range_start(&range); - } - _pmd = pmdp_get_lockless(pmd); if (is_swap_pmd(_pmd) || pmd_trans_huge(_pmd) || pmd_devmap(_pmd)) { if ((next - addr != HPAGE_PMD_SIZE) || @@ -428,9 +417,6 @@ static inline long change_pmd_range(struct mmu_gather *tlb, cond_resched(); } while (pmd++, addr = next, addr != end); - if (range.start) - mmu_notifier_invalidate_range_end(&range); - return pages; } @@ -438,10 +424,13 @@ static inline long change_pud_range(struct mmu_gather *tlb, struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr, unsigned long end, pgprot_t newprot, unsigned long cp_flags) { + struct mmu_notifier_range range; pud_t *pud; unsigned long next; long pages = 0, ret; + range.start = 0; + pud = pud_offset(p4d, addr); do { next = pud_addr_end(addr, end); @@ -450,10 +439,19 @@ static inline long change_pud_range(struct mmu_gather *tlb, return ret; if (pud_none_or_clear_bad(pud)) continue; + if (!range.start) { + mmu_notifier_range_init(&range, + MMU_NOTIFY_PROTECTION_VMA, 0, + vma->vm_mm, addr, end); + mmu_notifier_invalidate_range_start(&range); + } pages += change_pmd_range(tlb, vma, pud, addr, next, newprot, cp_flags); } while (pud++, addr = next, addr != end); + if (range.start) + mmu_notifier_invalidate_range_end(&range); + return pages; }