From patchwork Fri Jun 21 14:25:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13707659 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 11B32C27C4F for ; Fri, 21 Jun 2024 14:25:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EB286B0431; Fri, 21 Jun 2024 10:25:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 96F956B0432; Fri, 21 Jun 2024 10:25:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 775196B042D; Fri, 21 Jun 2024 10:25:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 502BF6B0428 for ; Fri, 21 Jun 2024 10:25:29 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 124CB80F74 for ; Fri, 21 Jun 2024 14:25:29 +0000 (UTC) X-FDA: 82255118778.08.2F41511 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id E2DC9180013 for ; Fri, 21 Jun 2024 14:25:26 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XQ0BTdFB; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1718979921; a=rsa-sha256; cv=none; b=78r3DkqphGxnA34rcccSHdmiW6LKhHb3imE+ffl2C0DVzI1bQ3sUzzxa7W6wOeFevIdgWU rbwP3LyBJVEc/Bu52tDFIcok4BgY1f6fxOqi9fnPziZjduTHUhHVSCTZA93u2xoJGrI6c6 1pGCzilXxDn9rPfPtKkdF7hzqspipRk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XQ0BTdFB; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1718979921; 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=FloNdHDAMCUmgBKRbs+OcXuVLR4NlkKUkgqw/Oorwhs=; b=SLI+25IEqEkSgd+iUTDU4OMQcGTTYjpnzpslXxiuFEbhe/1kQgRahFvS0D4kwa8fw/BO6a syvlcakGlU1l/ae5Zf12RVDulB5Y7qRdDnDOMKFzydmJfQwWFizIHP7mHMx3ATUeXV6IVX dC3N/tjcmUK3TnEXEF6PZw9jYPEQueY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718979926; 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=FloNdHDAMCUmgBKRbs+OcXuVLR4NlkKUkgqw/Oorwhs=; b=XQ0BTdFBI+ykbnJvxk9XobUoMX7glCe/nTMu5tQNPSjjOyg1Ib/oigX9Q8JKzR+5CZQmP2 1Fdz9zB7jCUYE+FcdlWp7f14Uqdxcif7+VXEWKGdOixpDyRnu1V8/KHe2wZ/UbUCwKRLJB 0gLfbVANPMK4lJiiB89H5H6lpRdx2a8= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-DnUxS-eaOiae2_EsbYnU-Q-1; Fri, 21 Jun 2024 10:25:22 -0400 X-MC-Unique: DnUxS-eaOiae2_EsbYnU-Q-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-43fd537e6a6so3296671cf.0 for ; Fri, 21 Jun 2024 07:25:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718979922; x=1719584722; 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=FloNdHDAMCUmgBKRbs+OcXuVLR4NlkKUkgqw/Oorwhs=; b=YJEpkLfmPF4RIqEgUNt5DpmgTlbO/pGQ02AtNhGW7gvq4/5h5+2+In/QhFeS6beHHe 9W1e9UoocejKasSUybTX0fnxwKtSwa2wXXtR3kxUx8oPA6NjS5dB0zWrA8Oulmw1moDs Px6unGuC9ERyGRvo7nBK+WFNqQPsSPWv5mQJr2x9UXl9hLp9zuIaDrPqYlVWzgKPOr61 WFPOUxhasIU585HT/MToGpFEs+rb064tj7IpR19pC5akg/FRllwS6NRaHWCGEa27bkvF Wo6RFd2Fxs+OcXnGYABIMr82oKjHM/zbFyoWJZoBVK8bEiOe+cr73VFwxAdTJTRJyru2 KTHQ== X-Forwarded-Encrypted: i=1; AJvYcCVo3+DhU7ZcRDYza5+boaRyQh/wjAbm+ChwmUhsEgvuAdjP+lM0oQrczID89kSWuhh2QgAIy81S27w35IrDQ9QvLRM= X-Gm-Message-State: AOJu0YzdwBpEfYZbQTNvDisVPCzKarsD4/FOoBa+jDtyD1q3dNOKLwmf XjYyECUYZeuub8no9iFHYI6pRepO/47IlXyDYhRhwU2CQZBIh0+nUN9POksQaxCHfgNRFHjw9N8 HUnKPf7UaTNsb+0L9z/2Bn4HZAEt4i0eLJGRZeYd08FkSkuks X-Received: by 2002:a05:620a:4725:b0:79b:b8ef:160f with SMTP id af79cd13be357-79bb8ef1a25mr832756885a.0.1718979922070; Fri, 21 Jun 2024 07:25:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHKznxYW2FLJ0xJwWKaEtD8Tk/BHH+OIQZWLkXgxtzT/k39w+HZ9hDMrU5i6fKaXRUQoupH+Q== X-Received: by 2002:a05:620a:4725:b0:79b:b8ef:160f with SMTP id af79cd13be357-79bb8ef1a25mr832752985a.0.1718979921429; Fri, 21 Jun 2024 07:25:21 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jun 2024 07:25:21 -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 Subject: [PATCH 7/7] mm/mprotect: fix dax pud handlings Date: Fri, 21 Jun 2024 10:25:04 -0400 Message-ID: <20240621142504.1940209-8-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: fkx33tsthobuegdiw4oj7gbugmikgc7u X-Rspamd-Queue-Id: E2DC9180013 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1718979926-555283 X-HE-Meta: U2FsdGVkX1/isRBzSSklgTHnuJyyvOlgFosZPutaLqdTCAZplMvrX439YbzeNfa1sPtfagsE6OVXGrkhqI0AJE8uZbyhZQhB3O3QaNC8CvM9um4SEgC12cFBiYZMiDoz4OHscTUCyXAdMjxVFD72OKvm7gUZSqImbPcEWpxjs5ifSh8USpl6ocg5/ulG1Pokg6namguBmgBmXaDLyd0FVwKTYOh2rbDXwEIa0Ik12iFnqfCyhyUHbDuarNKWz0LHODIvLFFTn4lhLV7PLHQata5wyQhKJPK95lc+COrQlIAhxnfz+5Vj6Al0u2TU8Th9Pw3HuMT6CYObCWk89AsTVZKdI6S6bZUwlVcMGVDoU1Rers555P+1baGRIjEDLrDn2jzZdpfTbsVsht6d2Xo6+rVGmQpWAR2+yFauOzABfF51z/q31asqpwCM+lzJhVrtrMLMYtH7W+HOkXEcVNdoXr9Af7fmZ37fdJ4fa3lBFVaHTzowiFzifyVni98gpLAt9trtwU5nLS6EhmMNMsTXQqZHT+QK5olZCus4J/JjHsIBoH7cXWdZJX9gBd8dBcW8RLNbjUuY0DQVlnQeVX+HVaRN1I8ryi6PbxdFPgo6Z45XZDeRyQzYy19lH575MzzccFcwGDF5c0J8s7YkaDL86CEsMA8x0WaDr/XTJE+gkCHQeRBHmGEre1afw/7QWRFV6204/23Zl6KJg3LnHgDPciE65INCzbj0fmkMFh/tZ5Ubb9IvTzCIBvyUsAGv0tM2TG8zrqo6p2B6vi5He+q26mtMiA2ZF6CbxOu8ZliF6zzIirf1npyqW+yJc2xsq4aDdpfUUCzACe72t6P/xydmbO53m8JWArgVl7ezVPJf9F403OsBy7BT4+qTa1JNwGtRSaxsJ61mNWj4chqw3KAhgCsrePnCjCbpSUmxIJopPp0Qwiz4Vas4+tPBhZW6uBuzIsI/A33s1MCf3JI6a6x gs56GJL7 A7EgTZVcAc+/M3r1OGPYYj1E74f2DXKQn61C3unYhy4s8V4ZDYGmIty1RJb/aUstSht5HJgJPZvYYOe85vrXUCtetEEkNMTfaDYK4T7iK7nuLbS5cNm+7dRfGx/xZvOU5uirA9yHo/h2ZPxnm+WzKD9tZMM/RfJY3iyUb3geUExio1zm1qgBxk9b2a3sO1sziEUM2RXPzapebUgabuxC3IjzzsVsY2xc5dmh/trvUrMSISHOJei8RQXRAG1Ef0H0xe/OkNcYKkHGQjb+kkNEgr1HBiF06TkrXcHaGN0d9evLWkAqdTZB7HHhEXlZmaGnZzqio9JmE8wUcYYpjOdXOHwAnk80Axup5InMfrF+vi19XkVf0pzjy9WaZxOwZOq3hehhgogXmXMIk8JEsGKXzaCQPg9uKlyzvvbftq/PoqK6heAEO9+QTjkAGwW+x7PyToEJmJ4X+7v767HfXjzzZN1srdszm41irmtS5/rVM1wVtnLNYYii7737SidXckLlooCl6L9I+qw6zRe4lkTtFHZPMhHXPmPLSUMPohy8hBE9CbS/8cCnSOMuF7A1OJLuf2VxqzXySc9l4lAc= 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: This is only relevant to the two archs that support PUD dax, aka, x86_64 and ppc64. PUD THPs do not yet exist elsewhere, and hugetlb PUDs do not count in this case. DAX have had PUD mappings for years, but change protection path never worked. When the path is triggered in any form (a simple test program would be: call mprotect() on a 1G dev_dax mapping), the kernel will report "bad pud". This patch should fix that. The new change_huge_pud() tries to keep everything simple. For example, it doesn't optimize write bit as that will need even more PUD helpers. It's not too bad anyway to have one more write fault in the worst case once for 1G range; may be a bigger thing for each PAGE_SIZE, though. Neither does it support userfault-wp bits, as there isn't such PUD mappings that is supported; file mappings always need a split there. The same to TLB shootdown: the pmd path (which was for x86 only) has the trick of using _ad() version of pmdp_invalidate*() which can avoid one redundant TLB, but let's also leave that for later. Again, the larger the mapping, the smaller of such effect. Another thing worth mention is this path needs to be careful on handling "retry" event for change_huge_pud() (where it can return 0): it isn't like change_huge_pmd(), as the pmd version is safe with all conditions handled in change_pte_range() later, thanks to Hugh's new pte_offset_map_lock(). In short, change_pte_range() is simply smarter than change_pmd_range() now after the shmem thp collapse rework. For that reason, change_pud_range() will need proper retry if it races with something else when a huge PUD changed from under us. Cc: Dan Williams Cc: Matthew Wilcox Cc: Dave Jiang Cc: Hugh Dickins Cc: Kirill A. Shutemov Cc: Vlastimil Babka Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: Michael Ellerman Cc: Aneesh Kumar K.V Cc: Oscar Salvador Cc: x86@kernel.org Cc: linuxppc-dev@lists.ozlabs.org Fixes: a00cc7d9dd93 ("mm, x86: add support for PUD-sized transparent hugepages") Fixes: 27af67f35631 ("powerpc/book3s64/mm: enable transparent pud hugepage") Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 24 +++++++++++++++++++ mm/huge_memory.c | 52 +++++++++++++++++++++++++++++++++++++++++ mm/mprotect.c | 40 ++++++++++++++++++++++++------- 3 files changed, 108 insertions(+), 8 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 212cca384d7e..b46673689f19 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -345,6 +345,17 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, unsigned long address); +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags); +#else +static inline int +change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) { return 0; } +#endif + #define split_huge_pud(__vma, __pud, __address) \ do { \ pud_t *____pud = (__pud); \ @@ -588,6 +599,19 @@ static inline int next_order(unsigned long *orders, int prev) { return 0; } + +static inline void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, + unsigned long address) +{ +} + +static inline int change_huge_pud(struct mmu_gather *tlb, + struct vm_area_struct *vma, pud_t *pudp, + unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) +{ + return 0; +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline int split_folio_to_list_to_order(struct folio *folio, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0fffaa58a47a..af8d83f4ce02 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2091,6 +2091,53 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, return ret; } +/* + * Returns: + * + * - 0: if pud leaf changed from under us + * - 1: if pud can be skipped + * - HPAGE_PUD_NR: if pud was successfully processed + */ +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) +{ + struct mm_struct *mm = vma->vm_mm; + pud_t oldpud, entry; + spinlock_t *ptl; + + tlb_change_page_size(tlb, HPAGE_PUD_SIZE); + + /* NUMA balancing doesn't apply to dax */ + if (cp_flags & MM_CP_PROT_NUMA) + return 1; + + /* + * Huge entries on userfault-wp only works with anonymous, while we + * don't have anonymous PUDs yet. + */ + if (WARN_ON_ONCE(cp_flags & MM_CP_UFFD_WP_ALL)) + return 1; + + ptl = __pud_trans_huge_lock(pudp, vma); + if (!ptl) + return 0; + + /* + * Can't clear PUD or it can race with concurrent zapping. See + * change_huge_pmd(). + */ + oldpud = pudp_invalidate(vma, addr, pudp); + entry = pud_modify(oldpud, newprot); + set_pud_at(mm, addr, pudp, entry); + tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE); + + spin_unlock(ptl); + return HPAGE_PUD_NR; +} +#endif + #ifdef CONFIG_USERFAULTFD /* * The PT lock for src_pmd and dst_vma/src_vma (for reading) are locked by @@ -2319,6 +2366,11 @@ void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, spin_unlock(ptl); mmu_notifier_invalidate_range_end(&range); } +#else +void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, + unsigned long address) +{ +} #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, diff --git a/mm/mprotect.c b/mm/mprotect.c index fb8bf3ff7cd9..694f13b83864 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -425,29 +425,53 @@ static inline long change_pud_range(struct mmu_gather *tlb, unsigned long end, pgprot_t newprot, unsigned long cp_flags) { struct mmu_notifier_range range; - pud_t *pud; + pud_t *pudp, pud; unsigned long next; long pages = 0, ret; range.start = 0; - pud = pud_offset(p4d, addr); + pudp = pud_offset(p4d, addr); do { +again: next = pud_addr_end(addr, end); - ret = change_prepare(vma, pud, pmd, addr, cp_flags); - if (ret) - return ret; - if (pud_none_or_clear_bad(pud)) + ret = change_prepare(vma, pudp, pmd, addr, cp_flags); + if (ret) { + pages = ret; + break; + } + + pud = READ_ONCE(*pudp); + if (pud_none(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, + + if (pud_leaf(pud)) { + if ((next - addr != PUD_SIZE) || + pgtable_split_needed(vma, cp_flags)) { + __split_huge_pud(vma, pudp, addr); + goto again; + } else { + ret = change_huge_pud(tlb, vma, pudp, + addr, newprot, cp_flags); + if (ret == 0) + goto again; + /* huge pud was handled */ + if (ret == HPAGE_PUD_NR) + pages += HPAGE_PUD_NR; + continue; + } + } + + pages += change_pmd_range(tlb, vma, pudp, addr, next, newprot, cp_flags); - } while (pud++, addr = next, addr != end); + } while (pudp++, addr = next, addr != end); if (range.start) mmu_notifier_invalidate_range_end(&range);