From patchwork Fri Jun 21 14:25:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13707654 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 4A711C27C4F for ; Fri, 21 Jun 2024 14:25:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 622F58D0174; Fri, 21 Jun 2024 10:25:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32C708D0175; Fri, 21 Jun 2024 10:25:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3CBC8D0176; Fri, 21 Jun 2024 10:25:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D14D18D0174 for ; Fri, 21 Jun 2024 10:25:18 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 85854160F86 for ; Fri, 21 Jun 2024 14:25:18 +0000 (UTC) X-FDA: 82255118316.04.A85EDF2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 660E42000C for ; Fri, 21 Jun 2024 14:25:16 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A4XGJQWf; spf=pass (imf03.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=1718979908; 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=iBb8I2lmDT9WwVPda/2HvzMEZGB7JJAXLDpH36J5OJOFhX6BB+4hjGqdgIz3TIzrTlVgnk Wv/SbLiWPA48Mqj7qY9XAw3NRnfmD3ZBaY8r6IhTriiZRJ2MYwaU8p5ksSfHtUQwUU2M3Z qfQDbEd6iQ2af3trBZsJCxrt9WWBCWw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A4XGJQWf; spf=pass (imf03.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718979908; a=rsa-sha256; cv=none; b=ZGFWoTmS0nuUC+NbyECZz60hOV5CdvrIFvyF6uxjXiSxNc0sppo3JLnPAKJ8eTt+zO33S5 c0xq/HakuAa1o4DiTqj/1F+1ZcSqn0ULZm2AV1Mh0b+mfEDdHA0vykWsoc9vpNJukswIyw wqFVjopUzt7UH8kmxPnwXTDfJyXZjug= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718979915; 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=A4XGJQWf1W3bYUzhFwiq5AApcOb+kPKpusr4oguxDixyLpSw2lfhCJYz26/lsriMvP5aYK GcG7WNAqaaeDdTGmswkUaRJ3ZGpJ8DiQ2NPGpaQdKZYHKQgw9Ngnz1xTORWtRVfuS2Y0l0 ZA/nSt6B4+eyKYgSuQorh9/hedpqX+4= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-FxUGhy4hPka5dWdSzub6Aw-1; Fri, 21 Jun 2024 10:25:14 -0400 X-MC-Unique: FxUGhy4hPka5dWdSzub6Aw-1 Received: by mail-ot1-f69.google.com with SMTP id 46e09a7af769-6f961a1baeeso476272a34.2 for ; Fri, 21 Jun 2024 07:25:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718979913; x=1719584713; 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=gDky9othnBYac4bM76dmsykYpwjC4nqhzhkqfpOspr4T3y4ql9O9p5ICMdY01kOjzl SPjUQrebvyJ4wbrf+nIYNkpk9iXHvt6lkUdRiT9laQmNnRH/IRsICgS69vBoXwEOpHq+ 2nHI5CyYxP10YdrnnrULQIPH6R5XeCCTIA+t+g9H50g2+ctcMtLCPgakWdxGK4d9uY8y /KxCGBOEAOxQ3zBM3BmBai4Gj0gM0mm5YwnpyaM7G/92T+RnpY+aWtgQzQ248ZPfHJ1J cxd6UBMj73ZuCaPD6ZJWG2qqytpCT4mclC7FV3VaQdZy3J2LQDvlrH2aeX5VoO1k2E44 F/TA== X-Forwarded-Encrypted: i=1; AJvYcCWCCwVyLxV63HXTw2Ng1HssI3GaFpPINzXVLnCDt1x4X/iKGAMjGjr3aWU6nUDHNkuf50/r5dFHsAnETe4aq1RO/po= X-Gm-Message-State: AOJu0Ywh+WDbc7tyVSulb71Kw6eRQIeQZnutzPBZiRfpZtI09RJITnCc gZza+v2NTJiZJnAdgZvPYhZkwjMja1lv9+m4q0ZlqWxhwgTvvZ0ZgR9ndxSoSOedi7qIhbUJgDY DNCo1InU/np5EYIUkOs4OxuCwg5G21K9jQVfaAbOtX2f6VYQC X-Received: by 2002:a4a:c60f:0:b0:5bd:af39:c9d9 with SMTP id 006d021491bc7-5c1ad9093ebmr9601658eaf.0.1718979913413; Fri, 21 Jun 2024 07:25:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFna6HwoEVcDVzacrmJp70i2Ye9mZl03svl3PALStAHNV8VRqUBmNMnAivUzvRTiL6Q97gHJQ== X-Received: by 2002:a4a:c60f:0:b0:5bd:af39:c9d9 with SMTP id 006d021491bc7-5c1ad9093ebmr9601627eaf.0.1718979912731; Fri, 21 Jun 2024 07:25:12 -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 af79cd13be357-79bce944cb2sm90564785a.125.2024.06.21.07.25.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jun 2024 07:25:12 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: x86@kernel.org, Borislav Petkov , Dave Jiang , "Kirill A . Shutemov" , Ingo Molnar , Oscar Salvador , peterx@redhat.com, Matthew Wilcox , Vlastimil Babka , Dan Williams , Andrew Morton , Hugh Dickins , Michael Ellerman , Dave Hansen , Thomas Gleixner , linuxppc-dev@lists.ozlabs.org, Christophe Leroy , Rik van Riel , Mel Gorman , "Aneesh Kumar K . V" , Nicholas Piggin , Huang Ying , kvm@vger.kernel.org, Sean Christopherson , Paolo Bonzini , David Rientjes Subject: [PATCH 3/7] mm/mprotect: Push mmu notifier to PUDs Date: Fri, 21 Jun 2024 10:25:00 -0400 Message-ID: <20240621142504.1940209-4-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240621142504.1940209-1-peterx@redhat.com> References: <20240621142504.1940209-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: mf6kqjp885p81rmsyb1kx113z3toegzh X-Rspam-User: X-Rspamd-Queue-Id: 660E42000C X-Rspamd-Server: rspam02 X-HE-Tag: 1718979916-768109 X-HE-Meta: U2FsdGVkX18A2b6I7cS+p4axSYlPNmFIFxOgbWzxAEe6a8Kgd+6xCCuQvdQgxQkfLr05IUMBO2E/Lik5i1eN0XBF5bXvRhj8nhaGr/HJCgwrGE3FhUj9mOJK2kphu6ikHY2GE/GkKgrKCDsMdei6wq/ixNFJzY5HGw+w65y/SkjQgaMp+W4NFaGAbA/I4EIXusj+yxN6FV7muSsbfDrA/l49V1vR8+5tR4yU4Oq8+XxUNNt2GqkkW+ZtV6wn1Kuum3J8ztZyTbK7dxLTz0MKwr8WMjsLHPRSIdxVcw1FAaYSuSarNAw9HInOiVEhqAxL/Q1sXaTJPYqkm+BpRkzGZpF+BV96VIUXMjD/Bno2TKBiaxzc8/Wu/S3gzMsbIyh6XWYNm8+v9E3hUthNqI4q2HXpW1lca1z3vsvzWx/Csh9iK1pTCXdxo+1lHnR1TArPpnlTr/8oCv6TbEv4VhZjQyziwrP5FCdWJYgHqVzPXGZeHgLQBXk5jFKXzxDy71ue5UDyf9YtKk8iFdT9GBxBLKQQm39nLarwCt7l1hRi0E3cJxpD/S4jtLpS4OyTJQUrG0XTH+kLAqS87iYKdJtPJPL30KKh4YF3lRD3rOlfZRtvzNGkMNJPL79nOVebOZermN5ckGZegGb8++sb/cKqV8ulgIV1SJJ1HXMeD7nZylnNlZCxLqvLLjMnWyW6N6s6ddZWuHcMkUJF2E1VdGmie/hMPwKO5FEzp6p71ER8cr+W9msPiaCbzvW/Yb4yyRc0/iuVUh/wkvSqVOvo716lDisXXNBoJ0G6umKWxRkSKRG/akQ3mQCzXkHEurDY7Q/2VuF9WaYS5s6dsueT49q3Ix48Nb5AdCxL4rQvP6V607+ux/wxlzqHeqdJhI27GoMkyV7RJy2OEkIfd/v6ScrZ3PwUhSYqf49mbrinnjTbxG/KHxY0qRdT3P7YkaAreLIwU/X9aC8UqUJ/VIRTlF9 KxKpmDIB iLzCa/+HukXSthAEodIT0L6HaiObQi74HNWhmKGbw02N2/vt+dUwBt3aLi3rbgZrT92adaqCddrCFHJSsTkOkRvV4Wt1Md+jm1iD2CxOxHhGq9ietZ6GDUkXWzsKNqXIhgBDYMA9bWHBNmm0Crkw/PoWIqLzscUswAPma3wZrXwWJf5V49k4zcvcov/QiubMxWm2vvRYEmunKKZIGOuCuZ0fkhN5WNPHS4F/USfi7E3UwjW+yBDvCpC/veqhEvg3VzIF2wROK4jeEpavRrg77gZ0TnOaOF8p3eZJ+VBIO9SavAIB56Vb7bNSZua7Jfs7TeP31DAaXRzrcuHFs1+PV9WCYkrGulq66JOuyO99GVNRquYdasxsd43n3aCyOQZ2eX7WctNuq0s79go/Xg6/U8tMKv+31d5qpX8oMfRhMpSVck/gJeYgdS+MsSqz8s/SIspmZJ+9VfG1oL/YKV6Vrg+9iTHL1x1wZGQt9iQB2OhqRszvIhqjNQUCdQQlJ2IyHZ77Dugl0KNDXBhuulWkxtHmSl6JcWtAmWAR0jFUvwJzGagWKzBLXMZAfn/ZdZDa/Zm1w 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; }