From patchwork Tue Sep 24 06:10:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13810132 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 C03B2CF9C6B for ; Tue, 24 Sep 2024 06:11:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57AA36B009F; Tue, 24 Sep 2024 02:11:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 502806B00A0; Tue, 24 Sep 2024 02:11:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A3636B00A1; Tue, 24 Sep 2024 02:11:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 188406B009F for ; Tue, 24 Sep 2024 02:11:34 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8EB1DC1808 for ; Tue, 24 Sep 2024 06:11:33 +0000 (UTC) X-FDA: 82598610066.18.89D77FA Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf06.hostedemail.com (Postfix) with ESMTP id BFC20180006 for ; Tue, 24 Sep 2024 06:11:31 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=WT+R5YZ8; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727158277; a=rsa-sha256; cv=none; b=p6LILTCyYN8WOV5VcZbuZFpfBvdo0IVxLdttB/l4Ak8fwc0w2KLPfpNzu/eO3u+yb0mf1T TlG3LjqjnQnDo6i5Xwz+6KLKzojJ+uiSJSAhLe2czOF28slc0yGXXEZG2+J5q2v2yb+po/ 9/DI2rgVl4pCFSErFBWy3wn62J7tAcM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=WT+R5YZ8; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727158277; 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=FxMRr8GFLSu76sONoxW9925XRbgCGcQv9BKiL/bcjLY=; b=wDi2JU/blrxadqZRG4JDj3UxjhzsdLO54vEEhefqK/2UdYzMMs34n2RH/0fTMRpjcnJj9A 04odsGBpca+vJisUtgJPdxVXsVx5BxIdkSRSf92Btia6KvT6ByKFF+b3QS82dTqjXwQTI3 UNHRM+Sh2+k1LQCXvLOTisVPFlBvISI= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2053f6b8201so44772395ad.2 for ; Mon, 23 Sep 2024 23:11:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727158290; x=1727763090; 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=FxMRr8GFLSu76sONoxW9925XRbgCGcQv9BKiL/bcjLY=; b=WT+R5YZ8nYQwwOGYgWXACGLKGdUqIyjcrhcwuJ3M1zdZsJ2NWlDDYXOLIBWAYUqgK8 Gmlvv8HtrVBYosw7o/l1mAqJGZ+Kj+KtaNoC4et6QwXN/aoKNVdwU1HRwts+q8/4tkit nFQLxdT0COFmfAiWvrkoXK/Mmg9d4NfCy0brd0o4htp5jZVu3e8JUWni35VAq9UX77Q/ ydmB+TANTymz0wxlfX1idkvU7JY1kQscoJkVGGxPnxPEtgtqK0ZgLhrRaVPc2jLKDkzm TusGR1TiHkNTiFH5aAa4R3c38Xwp48GOAmjD3PIQcbAGGFYDDA1wMuDSXQ6N/JH0WEvS HyiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727158290; x=1727763090; 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=FxMRr8GFLSu76sONoxW9925XRbgCGcQv9BKiL/bcjLY=; b=n+bTZ1ZOTizjZHhfHdBxwz/+i46ChH1FYXGLRNd/I3Qk32mo/V6gsWXkDri9ea4lOD YVQ3lQV+eGl0GMP4mNeEvi06Wy8b2P7aB+MgmNV3uQeKyVABnoUIRmU1t3jfGaakwh/Y VCmZVHBfPqKw/3DPGUreDEgrxcWHLqzhI5ZO1yDbUPj/zrN38DoSRTnC+vzj4TSHPszB 9rkMRovruY99CV4Dep7BJSwcq1tMKyRk43DSrF7wEhu6LrTALgVPORJZMF3PVuOKORz9 TAjEM2sq997mlT+1p1LjHs6iTJajLYBHE/EE+9aGzPBq/2Ye/FBRKtj8+JHVuaTfv9BE qcvQ== X-Forwarded-Encrypted: i=1; AJvYcCXRr5KSWiEI0gsj+FWh2eTFJ+qtUYjnYvKPky9JZt8ftHEsckyyBaxjb7lD0j3cT0BwHBNuPGfGow==@kvack.org X-Gm-Message-State: AOJu0Yz+u/nJL3QqPvtq6L6wEJE93z3mq4GmxUxPyHmhv8i5WUiJDhel EEEZw2CIRfwXrsu/2yETZnc3y04PllBJua9nqwD6Z0Og2sucgZQzjHytSnxyUnw= X-Google-Smtp-Source: AGHT+IGuU8VFNPNElLsd/e1qu4NWf2AelC2dd7GFKvCMDCyRlgITelf6+XB9J1CmvT70UykrnIRqmQ== X-Received: by 2002:a17:902:f542:b0:201:f70a:7492 with SMTP id d9443c01a7336-208d98603b2mr202721885ad.53.1727158290456; Mon, 23 Sep 2024 23:11:30 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20af17229c9sm4344885ad.85.2024.09.23.23.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 23:11:30 -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, christophe.leroy2@cs-soprasteria.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 v4 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock() Date: Tue, 24 Sep 2024 14:10:02 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 8kyrzxz5hwmz8o6gwejgggnzosozak7p X-Rspamd-Queue-Id: BFC20180006 X-Rspamd-Server: rspam02 X-HE-Tag: 1727158291-56258 X-HE-Meta: U2FsdGVkX1/XfGU5n3cStVryuSy0KdNycYrK7gFKKZ8Js/9Va8aP5ftloMzf9cmGs/cshuKk0Fr9YtEKk9E0RmyDCTvX3UxUW9hvKUQtvFKgsP6fwv9HO4M2eOHs88dkz/sY92iSLjg9fHrxI9Cw1C+UwQAkXVSZGhqIlB4vHcMO6/ZiTNFpEgST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XMVR4gkmKM3bifP10YNch4FI/uIPqICW3i9pGhxEtj+J0agptd7+VXzSRyiX+58lx/0M4RVFIt0+OdhtfdVtUC/5LgOmp5MDwOagUGdB6fj1nloqb7uSsyF9A/wJqraJWvuNDU82t9F2lUCAayB+mrQ== 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_rw_nolock(). At this time, 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 | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index ae5cc42aa2087..6410f29b37c1b 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_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd, + pvmw->address, &pmdval, ptlp); if (!pvmw->pte) return false; + *pmdvalp = pmdval; ptent = ptep_get(pvmw->pte); @@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) } else if (!pte_present(ptent)) { return false; } + spin_lock(*ptlp); + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) { + pte_unmap_unlock(pvmw->pte, *ptlp); + goto again; + } pvmw->ptl = *ptlp; - spin_lock(pvmw->ptl); + return true; } @@ -278,7 +287,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 +316,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);