From patchwork Fri Jun 9 01:30:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 13273082 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 9BA56C7EE29 for ; Fri, 9 Jun 2023 01:30:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3288E8E0002; Thu, 8 Jun 2023 21:30:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D87A8E0001; Thu, 8 Jun 2023 21:30:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1790A8E0002; Thu, 8 Jun 2023 21:30:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 092EF8E0001 for ; Thu, 8 Jun 2023 21:30:56 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C6FAFAF074 for ; Fri, 9 Jun 2023 01:30:55 +0000 (UTC) X-FDA: 80881480470.16.545F584 Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) by imf07.hostedemail.com (Postfix) with ESMTP id EBB1F40019 for ; Fri, 9 Jun 2023 01:30:53 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=gJAMMyka; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.128.177 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686274254; a=rsa-sha256; cv=none; b=RI361BZo1xgAVsw4WC+K1N1YvZ1dCPKxiybYzQ/v+Iq0F5kvIUdIEQFV39Tzq7lJZtWVhC 0+nI+ugBS0lyrgEF25b3Q8asKIOglgPzjlrD7RrXVgURC8Ck9O4hTTyMb9b7T8WSrSgAiL xOu8gg6+AQnyO66Vfiwpet03ivlcelk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=gJAMMyka; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.128.177 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686274254; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7GAfzbPNj/2rvKRevzngE24N05kydgFSIoMtFyLCrv4=; b=y4nzi4lm4ZjG/IoRcRh8rHNJjKkxmS5n3f3oXQF+eThhq0rT4nJBhOvRM7EA2BA8VpiX5W pGojoeaRAxI/jw3AqpeoniOf9QNzNnZVDIv2i3KEbHaHt6PUB0sq1mjbVHm6go7oxMaHyH ngJRTQL0foDIQC3FW8MPUQ+igGbJ/nI= Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-565bd368e19so10851897b3.1 for ; Thu, 08 Jun 2023 18:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686274253; x=1688866253; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=7GAfzbPNj/2rvKRevzngE24N05kydgFSIoMtFyLCrv4=; b=gJAMMykaIEU9PkjyCIdSz3ODq0ftmm75epqTVf1DDwNHXYM11vOXndav/A+j1kRFEc 9zEcP31A1kA13aWMOjns1R75xfAWzNDi1gHKk68ewQgq0whuvZDJ2IOLlB+uSUVd5Hp8 svpukbwsNHcvaTpeSeeB57vhMGo3aoLUGSbQC4vDY82UJZYh6UFZthAmKEZpoO6CrQsj zIurR6IMYJtElj3ek8vexr06I8pOIvm9uDnN6jHks/XrVIB5jV9tJeQ4OkFBFlzOLvfn TFupD/LP2R58aC1XHY0D8ruvxKPXB9C8uw3aB2kdOGhPoyncXso5dO85F2PIfWP2s5CT /ipQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686274253; x=1688866253; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7GAfzbPNj/2rvKRevzngE24N05kydgFSIoMtFyLCrv4=; b=AWclyB37ba4uUf18vfF2DNc7hIvWL4aPPnX5F/Qdq4D2cw4ipLic/hzSMtsVoLdIM6 IqHiyW1+eepcjZB4kAfLfraAHpP6+9YaH8DzLvRdUjIPQzRDF/3AR4qlkrvcrqQWYRta yyOg5SkXWdFTNNU6BwbenZwEGSd5ffrmcJeBur+J7gyCyuovnQmHdUKUGan8e2ifXJkq JeEgGfIvpTZgmNzhWOt8Gg+YZ9ybhy7bablBpuY+oJqfDQ5A4sK8C4HQp9iVpunxKEVu DN77af2URMuhHcTsPZTwPDzmDNQ76nulRyW541NIi3Ty6svR3f1FfMc9m119Vb5ziP8u UuyA== X-Gm-Message-State: AC+VfDyrWGNz9wPMq3tLYnW2zJxFX+jUyhUiYLfugDWeOIPA2ObLRg0V hhXuihlFTXR8g3j++6scdx1I2Q== X-Google-Smtp-Source: ACHHUZ4zYAZPyewfaoixPaut1Ay+u+IP9P5gVJt0aeUL3XpkhLRU1BODxkde89jUMQkygUDst/++Nw== X-Received: by 2002:a0d:ea8b:0:b0:559:d3a0:4270 with SMTP id t133-20020a0dea8b000000b00559d3a04270mr1322987ywe.34.1686274252865; Thu, 08 Jun 2023 18:30:52 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id y206-20020a817dd7000000b0055a881abfc3sm277355ywc.135.2023.06.08.18.30.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:30:52 -0700 (PDT) Date: Thu, 8 Jun 2023 18:30:48 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Jason Gunthorpe , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 18/32] mm/mprotect: delete pmd_none_or_clear_bad_unless_trans_huge() In-Reply-To: Message-ID: <725a42a9-91e9-c868-925-e3a5fd40bb4f@google.com> References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: EBB1F40019 X-Stat-Signature: 5g6ckapysswx11zzyewh74z9pa3qs4pc X-HE-Tag: 1686274253-333391 X-HE-Meta: U2FsdGVkX1+DfPJJrM590xCiJav1fQfODU1Yh6SBuWHQB+tCnR6ESd2MnU8a2ngj4s3SIAk8NM3PmLS54bFRJspJ+2svfGuW4Pi21txVkYn/t2U44A3w/0uH+QCPLO7x1LlFk3u/Cc4okeirGTEFs9dfyv+eTdXMJNk+czGeY2U9NgjMBebUV5Knuruhb2y1U/xFMVRpoH+YLsNhG6LHxRZqST99vv3PfikZ4xpmdGd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embPDAxuBO71RDEZNLQYQBZeMDJt7tX01s7AUwEQ2bHLO+WqafIm3oFMxnQKzRVfhuqNi/aF4QQ/JfwwlQRTxwDI29OrAVJg87b90aGwAmNdcjdPCHOFYrWE7S3GGQ== 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: change_pmd_range() had special pmd_none_or_clear_bad_unless_trans_huge(), required to avoid "bad" choices when setting automatic NUMA hinting under mmap_read_lock(); but most of that is already covered in pte_offset_map() now. change_pmd_range() just wants a pmd_none() check before wasting time on MMU notifiers, then checks on the read-once _pmd value to work out what's needed for huge cases. If change_pte_range() returns -EAGAIN to retry if pte_offset_map_lock() fails, nothing more special is needed. Signed-off-by: Hugh Dickins --- mm/mprotect.c | 74 ++++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 57 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index c5a13c0f1017..64e1df0af514 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -93,22 +93,9 @@ static long change_pte_range(struct mmu_gather *tlb, bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; tlb_change_page_size(tlb, PAGE_SIZE); - - /* - * Can be called with only the mmap_lock for reading by - * prot_numa so we must check the pmd isn't constantly - * changing from under us from pmd_none to pmd_trans_huge - * and/or the other way around. - */ - if (pmd_trans_unstable(pmd)) - return 0; - - /* - * The pmd points to a regular pte so the pmd can't change - * from under us even if the mmap_lock is only hold for - * reading. - */ pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); + if (!pte) + return -EAGAIN; /* Get target node for single threaded private VMAs */ if (prot_numa && !(vma->vm_flags & VM_SHARED) && @@ -301,26 +288,6 @@ static long change_pte_range(struct mmu_gather *tlb, return pages; } -/* - * Used when setting automatic NUMA hinting protection where it is - * critical that a numa hinting PMD is not confused with a bad PMD. - */ -static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd) -{ - pmd_t pmdval = pmdp_get_lockless(pmd); - - if (pmd_none(pmdval)) - return 1; - if (pmd_trans_huge(pmdval)) - return 0; - if (unlikely(pmd_bad(pmdval))) { - pmd_clear_bad(pmd); - return 1; - } - - return 0; -} - /* * Return true if we want to split THPs into PTE mappings in change * protection procedure, false otherwise. @@ -398,7 +365,8 @@ static inline long change_pmd_range(struct mmu_gather *tlb, pmd = pmd_offset(pud, addr); do { long ret; - + pmd_t _pmd; +again: next = pmd_addr_end(addr, end); ret = change_pmd_prepare(vma, pmd, cp_flags); @@ -406,16 +374,8 @@ static inline long change_pmd_range(struct mmu_gather *tlb, pages = ret; break; } - /* - * Automatic NUMA balancing walks the tables with mmap_lock - * held for read. It's possible a parallel update to occur - * between pmd_trans_huge() and a pmd_none_or_clear_bad() - * check leading to a false positive and clearing. - * Hence, it's necessary to atomically read the PMD value - * for all the checks. - */ - if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) && - pmd_none_or_clear_bad_unless_trans_huge(pmd)) + + if (pmd_none(*pmd)) goto next; /* invoke the mmu notifier if the pmd is populated */ @@ -426,7 +386,8 @@ static inline long change_pmd_range(struct mmu_gather *tlb, mmu_notifier_invalidate_range_start(&range); } - if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { + _pmd = pmdp_get_lockless(pmd); + if (is_swap_pmd(_pmd) || pmd_trans_huge(_pmd) || pmd_devmap(_pmd)) { if ((next - addr != HPAGE_PMD_SIZE) || pgtable_split_needed(vma, cp_flags)) { __split_huge_pmd(vma, pmd, addr, false, NULL); @@ -441,15 +402,10 @@ static inline long change_pmd_range(struct mmu_gather *tlb, break; } } else { - /* - * change_huge_pmd() does not defer TLB flushes, - * so no need to propagate the tlb argument. - */ - int nr_ptes = change_huge_pmd(tlb, vma, pmd, + ret = change_huge_pmd(tlb, vma, pmd, addr, newprot, cp_flags); - - if (nr_ptes) { - if (nr_ptes == HPAGE_PMD_NR) { + if (ret) { + if (ret == HPAGE_PMD_NR) { pages += HPAGE_PMD_NR; nr_huge_updates++; } @@ -460,8 +416,12 @@ static inline long change_pmd_range(struct mmu_gather *tlb, } /* fall through, the trans huge pmd just split */ } - pages += change_pte_range(tlb, vma, pmd, addr, next, - newprot, cp_flags); + + ret = change_pte_range(tlb, vma, pmd, addr, next, newprot, + cp_flags); + if (ret < 0) + goto again; + pages += ret; next: cond_resched(); } while (pmd++, addr = next, addr != end);