Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1428459rwd; Thu, 8 Jun 2023 18:32:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ZsKVeXLPKAcN+bpCGWoAaPdY2xpBbuek2xIg+iHusSvej09u8Zos4QK8/AXPiz3+pJUcU X-Received: by 2002:a92:d304:0:b0:33d:1072:361b with SMTP id x4-20020a92d304000000b0033d1072361bmr275343ila.21.1686274359641; Thu, 08 Jun 2023 18:32:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686274359; cv=none; d=google.com; s=arc-20160816; b=SoBGexKoZbu8P578MoQGoZIVUvd6RHWz+xtPFkdjOScAxF8y0vQvf+Z7mb0vLhK2LE 6mPLNBC+QGqhUn1shx3QOvNsdEAxVL4oETr6XZJGpNbhHrDB3lUrTiaIYt7bUQ9yWl0j aUn+YeZjjUiAUrq4/zs/1F0Ug+axywQfUy5YPLTZItoXA4fB/Me8Kw8xS3YS435bKOyx RaLFeaGJAuDL7auB3gnSKCTIlDSwQ2i+QE0YYmrl/dGNmA9mg0kbrshsEA++fJoO8qpN D23Ku4DbbQkzJWgMl2VbCax6S133987CLeWpjfCpGP+moyZoD2qxR0J1hic9sa0F4ygc Zwew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=9VYOFAJpcVHpYrrS1TQZcIVdYEqSjI0GrwCgA5USRTU=; b=pr1xZZ8FO9z3wh9McnyGyVNAo5SFCt+bss7Bf9QXWAPyu16YYrGJRdjndvkDV6qtmk UF5/7wPo2dxlE5j0HYcDVVE10xU9KevzTmgzzF1YoxGzIMxsLCNgdRHsxzoVoA2LGyXo CnYeS5uJB46w/YT3nWt2cEpOkPYUvIEataOpli3TyowaJewgcvYn9fpYCP2LbCzW9Byu TC0RKZBacjNRc21lbWdRG21mWcTRb9HgSjuwCC/Uvu890gsH21MDtOqsASNAD1q55COh DQqGWymGwx0yLGvV7coI7GAJ7/sfXdMLmqx3HszIVtm312WxEfypYleS1iMI0wNQ2x1c wtXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=qGqUUKrU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v202-20020a6361d3000000b00542bea60d27si1802406pgb.39.2023.06.08.18.32.25; Thu, 08 Jun 2023 18:32:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=qGqUUKrU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237417AbjFIBPw (ORCPT + 99 others); Thu, 8 Jun 2023 21:15:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229458AbjFIBPv (ORCPT ); Thu, 8 Jun 2023 21:15:51 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 011A2184 for ; Thu, 8 Jun 2023 18:15:48 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id d75a77b69052e-3f9d8aa9025so314401cf.0 for ; Thu, 08 Jun 2023 18:15:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686273348; x=1688865348; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=9VYOFAJpcVHpYrrS1TQZcIVdYEqSjI0GrwCgA5USRTU=; b=qGqUUKrUhXZqdfIpdRpru1enURAlAM37w3JnFCBdqcN5NOVKva/5ZB+Wx1AncWuaoB 4NOGT/sluCqCWe9k46AbMdrfO/y7S/uoPCSIv3YcbyPtfcHQLmMhf87/VSB5iKcTyY2z DvH2xbXgY53bvpfsWKoRD42G3UAVdO/UJ+NkrWhrUDvDf4DfgkGc7vx+86uzLhaB2Vdl OBAdZ3Qvm5QESmYlbn3dnRPJ/xpFtDzek+36j2PNxi4y2bmdWvVJycdWxpou775822Zd vv/wKkp98uy9cbO4mRL/ywca8CgLJugp1BRNkerDGGNXhj+j6x2eNtUbygfDvk9GNFDY 1+3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686273348; x=1688865348; 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=9VYOFAJpcVHpYrrS1TQZcIVdYEqSjI0GrwCgA5USRTU=; b=Icv9/FZFuPUNs31zosXQhizhDMouvqCGP6C+6z4ybduYeYfS9FUuRcqjtYVGBTWaZs Eart5unOrjk/xdStmc82tnDo0Qbs7urWpnvuKkg6gZRm24Bkcj4UAZDSPEKitOyUQrBt CBBwCz1+LyZUtzgaiNiu4vEZJh8UVIPcuwta8zvXyLbJVPcuf1EStT7dffWi4D3sSg3R Ig6NkAIo+uxSKzmaElzqPIFn6FIwbEVVrniaPx7oFNLzeoBiaRA43gKUeeq4NtOHeG9U gvtra5VT3xe41IxaIXUSh9HdytNZT0VJyT2g05TaEl1IaT09Pm1quDWkt0zHnJtsiGVi FYFw== X-Gm-Message-State: AC+VfDygGNirJyjv3y0l0pEs35ebzrsmxj2uknHVBi/tqB+uwRcBFyIS Jyed0TDyT0IaIPKK7FOWohjgmw== X-Received: by 2002:ac8:57d2:0:b0:3f6:9a18:e67c with SMTP id w18-20020ac857d2000000b003f69a18e67cmr42699qta.66.1686273348055; Thu, 08 Jun 2023 18:15:48 -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 u15-20020a25840f000000b00b9e2ef25f1asm583095ybk.44.2023.06.08.18.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:15:47 -0700 (PDT) Date: Thu, 8 Jun 2023 18:15:43 -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 08/32] mm/page_vma_mapped: pte_offset_map_nolock() not pte_lockptr() In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org map_pte() use pte_offset_map_nolock(), to make sure of the ptl belonging to pte, even if pmd entry is then changed racily: page_vma_mapped_walk() use that instead of getting pte_lockptr() later, or restart if map_pte() found no page table. Signed-off-by: Hugh Dickins --- mm/page_vma_mapped.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 947dc7491815..2af734274073 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -13,16 +13,28 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw) return false; } -static bool map_pte(struct page_vma_mapped_walk *pvmw) +static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) { if (pvmw->flags & PVMW_SYNC) { /* Use the stricter lookup */ pvmw->pte = pte_offset_map_lock(pvmw->vma->vm_mm, pvmw->pmd, pvmw->address, &pvmw->ptl); - return true; + *ptlp = pvmw->ptl; + return !!pvmw->pte; } - pvmw->pte = pte_offset_map(pvmw->pmd, pvmw->address); + /* + * It is important to return the ptl corresponding to pte, + * in case *pvmw->pmd changes underneath us; so we need to + * return it even when choosing not to lock, in case caller + * proceeds to loop over next ptes, and finds a match later. + * Though, in most cases, page lock already protects this. + */ + pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd, + pvmw->address, ptlp); + if (!pvmw->pte) + return false; + if (pvmw->flags & PVMW_MIGRATION) { if (!is_swap_pte(*pvmw->pte)) return false; @@ -51,7 +63,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw) } else if (!pte_present(*pvmw->pte)) { return false; } - pvmw->ptl = pte_lockptr(pvmw->vma->vm_mm, pvmw->pmd); + pvmw->ptl = *ptlp; spin_lock(pvmw->ptl); return true; } @@ -156,6 +168,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) struct vm_area_struct *vma = pvmw->vma; struct mm_struct *mm = vma->vm_mm; unsigned long end; + spinlock_t *ptl; pgd_t *pgd; p4d_t *p4d; pud_t *pud; @@ -257,8 +270,11 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) step_forward(pvmw, PMD_SIZE); continue; } - if (!map_pte(pvmw)) + if (!map_pte(pvmw, &ptl)) { + if (!pvmw->pte) + goto restart; goto next_pte; + } this_pte: if (check_pte(pvmw)) return true; @@ -281,7 +297,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) } while (pte_none(*pvmw->pte)); if (!pvmw->ptl) { - pvmw->ptl = pte_lockptr(mm, pvmw->pmd); + pvmw->ptl = ptl; spin_lock(pvmw->ptl); } goto this_pte; -- 2.35.3