From patchwork Wed Aug 21 08:18:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13770993 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 EC7CDC52D7C for ; Wed, 21 Aug 2024 08:20:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A72E6B00D1; Wed, 21 Aug 2024 04:20:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 755AE6B00D2; Wed, 21 Aug 2024 04:20:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F80C6B00D3; Wed, 21 Aug 2024 04:20:38 -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 3B5886B00D1 for ; Wed, 21 Aug 2024 04:20:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EC4391401A9 for ; Wed, 21 Aug 2024 08:20:37 +0000 (UTC) X-FDA: 82475556114.24.A42B81C Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf26.hostedemail.com (Postfix) with ESMTP id 136A9140027 for ; Wed, 21 Aug 2024 08:20:35 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=QxH4vD5U; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724228373; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MvwQXaavnjACpbuRjCPShfvJkJjZwFPa7EtFYmq5Q9U=; b=hKY5FeunuEilAL88ZDbQ6L7UBhGBEzwvx87x8+poBZNrDomBJ00eBK82lzmfVX7BCJucSL 6vO0hkfRf9yxQ1Ejlr/Th82FNfYdt0XyeD5QP/eRcdPaX0fIhG+Q2+emHDslOUxMEE8Oto L5WLFbGge3IpAPk9OXvqt1Q6i5m1m8k= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=QxH4vD5U; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724228373; a=rsa-sha256; cv=none; b=U4geRUhBBhhpDm5VofS5Nxu4G/e5Z0OjP1whJaB5MCrBT+XDQadbp5lv8/9fkLJg6Sv6Y3 DrEjrZC2rslsCIbQ8CI4jgpuRLqsfMC6vRem9IjJGMSJwRMC/RcgjSLm06J3iT14m2BfBX +UvxyqVSKZ8LAmiOFz2T3ugKTYUcngs= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7c3e1081804so4237100a12.3 for ; Wed, 21 Aug 2024 01:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724228435; x=1724833235; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MvwQXaavnjACpbuRjCPShfvJkJjZwFPa7EtFYmq5Q9U=; b=QxH4vD5UjPAXhUpoT2T+Jv16XfUUmrqC6e5N7ogf1maM8rDGs3AEseU/WgcPEmNqxw f3ViU2Ef14EP1t1JElQfbNxXbRL3FhDRmE9sKjdAgKSUcITaRY/eEHtTEcdYVE/H2Ylg gbPx09gyyxN9Z6ottYHQhbfpZP+IPkefRAmKGq3w4jCY+szOtPQUufkqfOmThZfv5RI4 zhkkw//Gwk6yxCo6lJU7UEIHhIM2xEnThzrtSEpVFprpeXY9iIO3V5b1S7x6piMyzKO1 l7buWeDh+l7QxtfbCtQ54yQ3IMKLz5Cf9Q6s4x0FLzf8MGdJDogt7xVh2rChOdk0ckuG XiEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724228435; x=1724833235; 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=MvwQXaavnjACpbuRjCPShfvJkJjZwFPa7EtFYmq5Q9U=; b=ngbw5Pggdep6M5PZdyA5Y2CHyPMaxNXuS/xZefTL8CZaU//AOevrMmlqnYZffHnm12 M3DedxkHUi6asdgdTC1dFAaRMyQp25sBX6wO3I7c3rqGHGRWX2o6Z1m3ZbydCvAhPCgw 6lpAq6xAF3v9OG4TgBqfrG7z+NzGuS48touNgeUPSF7G+PfrErCR8lvs1E+kVrYHUksB 3tPJtFUmwEUv8lZtjOCiL+aXznA5lXyRrPejBBGPUTLH8RrIAzfVxwQcoEZGynbH4naq awIjjiSHe4/jhZpvQInng0jN/58lg8wPmjJa3jhZ2Xs1sveC5sp63QBmm/aqoRpg1HxR qBwg== X-Forwarded-Encrypted: i=1; AJvYcCWYoGj1b+llVT6nMjTORccBDDoPQN5ZPnPy9AuwtFy8oKUd6ChNEPM1tkZ21mhbQF6rSEKWcp/pvA==@kvack.org X-Gm-Message-State: AOJu0YxtwB37wqWIPat3rYQCZrLmwU+2ddlCbu2hHO1sOGW97AIrAjwZ 6yOAmrrebqr9V3i1YHtHPiSk6mvxreQBD9fClsXXQeF5JLM1l84SO0R827a/nd4= X-Google-Smtp-Source: AGHT+IF3P/4155uMvmGYpoowuXrhy2v6aHwudYM9PJFMCLkX9j2rCBHO6+Z7X1wkaLdhxTlExnQxpQ== X-Received: by 2002:a17:90b:3b41:b0:2d3:b5ca:dedf with SMTP id 98e67ed59e1d1-2d5e9a0db5cmr1626393a91.17.1724228434854; Wed, 21 Aug 2024 01:20:34 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9049b0sm1091453a91.17.2024.08.21.01.20.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 01:20:34 -0700 (PDT) From: Qi Zheng To: david@redhat.com, hughd@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, rppt@kernel.org, vishal.moola@gmail.com, peterx@redhat.com, ryan.roberts@arm.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, Qi Zheng Subject: [PATCH 10/14] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_maywrite_nolock() Date: Wed, 21 Aug 2024 16:18:53 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: gshfpqnoukjm36betdq4xffbnsr5zuws X-Rspam-User: X-Rspamd-Queue-Id: 136A9140027 X-Rspamd-Server: rspam02 X-HE-Tag: 1724228435-685371 X-HE-Meta: U2FsdGVkX1+8nKnQ1bER+NlC3W5J+ei730LPM0TwZduvVaGR4vSn9GtIzS8ISdTyxyooafGD4+rDmMMN8AvlGRhyECYOzuZXrY4ofFc+K7dQ8EXDm8uJFWr6kDrIHcY1jzNOdyRtgPCzhnicyJGttVdINl8teHLAoqkQFSkrXt9ogWWLyFBSYA3TTi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w/O5+9Va8AFnIpA8urE4CyWxTXVj6nA7nRNvZn4dqWrIadOHvOsRiCgNkTlSbzJ8ILOvHPj5NKOex9F31Mjjmptf37cGxYMtkIhgho0Jg0jF5hf0+O/6ttHqlQDmE1FxbOy0oJjN3HDUE0mAjSOuEboeN2adUjxKX23m+ekW 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: In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_maywrite_nolock(). At this time, the write lock of mmap_lock is not held, and the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd. Signed-off-by: Qi Zheng --- mm/page_vma_mapped.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index ae5cc42aa2087..da806f3a5047d 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw) return false; } -static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) +static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, + spinlock_t **ptlp) { pte_t ptent; + pmd_t pmdval; if (pvmw->flags & PVMW_SYNC) { /* Use the stricter lookup */ @@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) return !!pvmw->pte; } +again: /* * It is important to return the ptl corresponding to pte, * in case *pvmw->pmd changes underneath us; so we need to @@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) * 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); + pvmw->pte = pte_offset_map_maywrite_nolock(pvmw->vma->vm_mm, pvmw->pmd, + pvmw->address, &pmdval, ptlp); if (!pvmw->pte) return false; + *pmdvalp = pmdval; ptent = ptep_get(pvmw->pte); @@ -69,6 +73,12 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) } pvmw->ptl = *ptlp; spin_lock(pvmw->ptl); + + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) { + spin_unlock(pvmw->ptl); + goto again; + } + return true; } @@ -278,7 +288,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) step_forward(pvmw, PMD_SIZE); continue; } - if (!map_pte(pvmw, &ptl)) { + if (!map_pte(pvmw, &pmde, &ptl)) { if (!pvmw->pte) goto restart; goto next_pte; @@ -307,6 +317,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) if (!pvmw->ptl) { pvmw->ptl = ptl; spin_lock(pvmw->ptl); + if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) { + pte_unmap_unlock(pvmw->pte, pvmw->ptl); + pvmw->ptl = NULL; + pvmw->pte = NULL; + goto restart; + } } goto this_pte; } while (pvmw->address < end);