From patchwork Thu Sep 26 06:46:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13812866 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 DD216CCF9E9 for ; Thu, 26 Sep 2024 06:48:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7494A6B00B3; Thu, 26 Sep 2024 02:48:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FA156B00B4; Thu, 26 Sep 2024 02:48:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 599BF6B00B5; Thu, 26 Sep 2024 02:48:07 -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 38A876B00B3 for ; Thu, 26 Sep 2024 02:48:07 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E1B04413FE for ; Thu, 26 Sep 2024 06:48:06 +0000 (UTC) X-FDA: 82605959772.03.766EBA6 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf26.hostedemail.com (Postfix) with ESMTP id 290C8140005 for ; Thu, 26 Sep 2024 06:48:05 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bCaJwtqK; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.160.178 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=1727333224; 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=RsEgu/iD0T6JQDFamAgFHiL1Lm0nKVk+wAfYQhZRThE=; b=4Hlo8wn56YX1mBnNa7AUDEipPreNG15vJjW+V/KFiDFN9WVdotrV2I+kXmSFtsfJx8TJJC 9Q2MSpKMguK6O1A+7UeckmUNCHMd3ZGJG8v5Nob6GjEIhT6oUboLNTVwAj5+jrPyQZGxFQ 1xwa6gA9nKj+UUR277HrpDuWzRzhB2w= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bCaJwtqK; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.160.178 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=1727333224; a=rsa-sha256; cv=none; b=ipQoYY0ZL8xtbbMM8suOoBnyWdcbfxTlIyjUddGaaau+MuBnMPfldas6zKp4BP7Gq7k/JO xP6qCSj3DIhCMoWMv1pdcAE6wOikdggJHnVpNdM6Iq5wAR9M8J4MnTEK/DpZaKWE15BN+e 1UHOy3eI+W16DBKnUH8Q8HfMicvwBsQ= Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-4581ec0e00eso4039641cf.3 for ; Wed, 25 Sep 2024 23:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727333284; x=1727938084; 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=RsEgu/iD0T6JQDFamAgFHiL1Lm0nKVk+wAfYQhZRThE=; b=bCaJwtqKOe6KkKkgl46ElXba2GFeJfcj6d3n59iTOUcP9w31YqIXCA5cnTyzE3keRm busEVGko5+4T/6ecCZ2Tk/mOIWIRAeQiKSzc+kUJxZTUjmaMfC4PAB0aDfCLxyuUMOhm nAsykNEE5jQJSAGscOnPKb9MxrdIeKFkhhsf14BtTIJV+SGzqTI1EleVFo+P3yK1cqyy 4ipSHUcucPNjuTijvUYpUBCk9/aPI7ROkbC7+8Q98gfeW+45yf6pEyS/ItaWsqOE3rqh ofv90YIBJXF434WwLqkBMndGO9ijXojK1sEr40a5MRqUtwnu2Virtr+p3D8IiWHZcrtV 8C0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727333284; x=1727938084; 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=RsEgu/iD0T6JQDFamAgFHiL1Lm0nKVk+wAfYQhZRThE=; b=wKYZYEx64vTG4RJzWf6r4ZEeKKNh07yfoM2XhxTmkEFbpp8c1qCNkY4gx1oAvwTcPJ FBUJaI5bIxmIRIxCoU4XIGAaYfLG8okkNyKoEY4usF8sjdmZ8Qv8f5Rd5szow/SmH8FU MY5BTUP0aCTtryOh9jDCm1BnUV7GZUtL9EcvycjkJY7xJrFs/cIQHRavDh0TrjFaK9Tz FCtskYUFNrybR0I359IYAB61THI9efaoOZmD3PWHdX8Kc0b7DBgmIbGAIz+PAktgRg7z 0tOPWkz25rDQb0Hw6typWgrJMGY7j/YXOQvkoPV51vbHXUiqxsZ6lr5yi6zsJk2EsS82 4uuA== X-Forwarded-Encrypted: i=1; AJvYcCVPhltq7cIRHuhQk7tV+n/0t8u9s9TgSsinQMFVjOxvlGn8vLhpJ3fIlOs8jx8+Ez6+IceiRv5fSA==@kvack.org X-Gm-Message-State: AOJu0Yz8CYhmvxcMRV+gxYVoy8POya/HNKHlCxtlro6FIMzDqzjYXt5d +KYktHiBYIs3SqoNzn5wQkT35vBZT0M2ECwMt8oi2qjDDXpYnYL2tyaptHniirw= X-Google-Smtp-Source: AGHT+IGAlBKd7aKCt6XxIcAXM/Sjs0v2+e+7/droKiEQYGe4LhoKPN6bWAepHInU8bkNuwlluGjQYA== X-Received: by 2002:ac8:7d4c:0:b0:458:4aec:2749 with SMTP id d75a77b69052e-45b5e045e56mr87889061cf.57.1727333284339; Wed, 25 Sep 2024 23:48:04 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45b5257ff1esm23024611cf.38.2024.09.25.23.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2024 23:48:03 -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 v5 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock() Date: Thu, 26 Sep 2024 14:46:23 +0800 Message-Id: <2620a48f34c9f19864ab0169cdbf253d31a8fcaa.1727332572.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 290C8140005 X-Stat-Signature: xjwumkgipfp3t15fzc8qbqz3imcir44h X-HE-Tag: 1727333284-914021 X-HE-Meta: U2FsdGVkX1/CwLnv6E5vSZ7gsLfjzQAvTY5hDV410ThkWhKrLlEDNE5odJR0XvuvTvJE7e6WMXWWZQX/xB7U9s87kUiO3llo6xBsRCpSIAa3BptnJwwkm+3go5HZkWK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0yZKrNTU6Ero8Kp76agSdYwHmvUXBRLmZetTEIS/cAfqHc6dAo8099y4rcZQ0LHWtCUmpFQ+ddswEv1KIHwEOC17OH0+MjNpXvlmjc0ZDntWowr6hg1PvTRdIVcadHstu9yIJnT46hNaJUuOcKc4FjgIdrCp+nJtLD/nqdb0GyLzllZMCkjMV5ivwAPPZDWdMq6LCPquxEdXk8j980g== 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 Reviewed-by: Muchun Song --- mm/page_vma_mapped.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index ae5cc42aa2087..ab1671e71cb2d 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -13,7 +13,8 @@ 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; @@ -25,6 +26,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,8 +34,8 @@ 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, pmdvalp, ptlp); if (!pvmw->pte) return false; @@ -67,8 +69,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(*pmdvalp, pmdp_get_lockless(pvmw->pmd)))) { + pte_unmap_unlock(pvmw->pte, *ptlp); + goto again; + } pvmw->ptl = *ptlp; - spin_lock(pvmw->ptl); + return true; } @@ -278,7 +285,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; @@ -305,8 +312,13 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) } while (pte_none(ptep_get(pvmw->pte))); if (!pvmw->ptl) { + spin_lock(ptl); + if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) { + pte_unmap_unlock(pvmw->pte, ptl); + pvmw->pte = NULL; + goto restart; + } pvmw->ptl = ptl; - spin_lock(pvmw->ptl); } goto this_pte; } while (pvmw->address < end);