From patchwork Wed Sep 4 08:40:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13790098 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 6A0B1CA0ED3 for ; Wed, 4 Sep 2024 08:41:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2A596B02C5; Wed, 4 Sep 2024 04:41:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB1CC8D0239; Wed, 4 Sep 2024 04:41:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2C8D6B02C8; Wed, 4 Sep 2024 04:41:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id AF5586B02C5 for ; Wed, 4 Sep 2024 04:41:52 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6E2BAA0E01 for ; Wed, 4 Sep 2024 08:41:52 +0000 (UTC) X-FDA: 82526412864.15.646BE0B Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf02.hostedemail.com (Postfix) with ESMTP id 93E268000C for ; Wed, 4 Sep 2024 08:41:50 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=VMo8FuaS; spf=pass (imf02.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.172 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=1725439262; a=rsa-sha256; cv=none; b=lxc6i6aLGd8YBaDrY65YKzVVcak8gE14eMEzQ9c3Rca3GDOeIefWMLNBxiSSbcDnqQ48dG YKG03/1qhMB6039Ub5t8c4wLxdyqSfvGWz4HW69i/I66Hz4b2y2TwVkuYqNI730J3g+53B Bx+RYZ2GCmgLsxVBlSBQg/pDfvR7Jp0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=VMo8FuaS; spf=pass (imf02.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.172 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=1725439262; 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=DklgHNcyYpWTok/ex913aKZBAOiPUnESXBkqa0TTMVk=; b=T4GXdcLqrZLz9MqB4fN1c2dE2vMdvyI+U6IHXPKKRWlZfDEOFsV+xFKT42WkK35bYr5htt TEOar8cMKcle/kMbs4Bw1FNuxuBHEzFOOMfZFyXdKYK79evr0SmXCvwKJ/f38niCK6GhOT /z/EfijOovtsxkjVRJl/PHxykI0IU1A= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2059204f448so20046305ad.0 for ; Wed, 04 Sep 2024 01:41:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1725439309; x=1726044109; 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=DklgHNcyYpWTok/ex913aKZBAOiPUnESXBkqa0TTMVk=; b=VMo8FuaSklXP7Xuhzq1GicQ6c8sGeWTo/xQW3CIvlyzw9SEWaHo0KcfDMD2ZbS4Y7J 8EhtDux1N3M55zXamH5K+DGE41xYDNyGLAdKgcHpO6foDdTGjlnqlkqmUTCgm9BPchm5 6JHHoKzzEuaBnlgDqk27bT8H+RwJvhjUxc9qtytD3r4unka4KxERdo7acY9s0ae2UJ3s UnkJwEVoJb3qzB6vD8xoh6MUJdZlmvwyef0GUC3VWtte4GxFZUFEDuohkrBDm1+cuPxg QHPECMniWUB0M2xiD+psHAmEE/YOd/M+FlUZlLQHdXURAJwostCpFiGezFXEYTNh45hb wytA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725439309; x=1726044109; 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=DklgHNcyYpWTok/ex913aKZBAOiPUnESXBkqa0TTMVk=; b=o6v3CblAbkCAHBDI15qQPGthSIE0Fo5drvek1rh7qgNFhNtjL2NuO9tRldoBnF7y/t ihWd83NOSY61MLKezuconVC+OBu94EiiD48joaeWcZIySSwh9/rrEAQPIJudNoqybQgY QS1RYK/JtwK0MAVnw9It322JAVx26jIX/JCkF/oZzJOtfPriNJbgqvAjqtlXcd/QK2q7 7IrORudpUz4v3zoSczULdQMcKhghByCTqaQutyw30YCeE6kJwifwcJF3SUTruUDeo/Vq 2YoB92suEHvGZS0wbgVjc+PcoGy5taB5e/lSjlwUMSdRFNPebPDZP0FlaSyyn6dyD9Y4 3FKA== X-Forwarded-Encrypted: i=1; AJvYcCVif1I/r+pLob1UYAJRmcZDNhLMyEul9S8JLbWyBHvFg6G8fSiF1TEbXofycBs5izQvrOiKBCgEUw==@kvack.org X-Gm-Message-State: AOJu0YxJAuoHrU90i4njYJiIxMKS87MsG7lfApKIjAin/Au3M8UZkiYl dYMo9AinOW5lhdMbvXvYB8mcFnk4+m/HHMiyq8LxOtMYGcbkCPDovZDrf/Q5kr8= X-Google-Smtp-Source: AGHT+IErn+gfx0jNqN8oYReDzi5ALFGdG/LuObb3FXvUpU+trUkKEzs/YdM2MbdzLHq+FDgFhXw6+A== X-Received: by 2002:a17:902:d50a:b0:205:6c25:fe12 with SMTP id d9443c01a7336-2056c25ffb5mr96453155ad.34.1725439309345; Wed, 04 Sep 2024 01:41:49 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.242]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206ae95a51csm9414045ad.117.2024.09.04.01.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 01:41:49 -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 v3 10/14] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock() Date: Wed, 4 Sep 2024 16:40:18 +0800 Message-Id: <20240904084022.32728-11-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20240904084022.32728-1-zhengqi.arch@bytedance.com> References: <20240904084022.32728-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Stat-Signature: z5duycmh3fnmof6fhmaxtoz1rwj6bqif X-Rspamd-Queue-Id: 93E268000C X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1725439310-203101 X-HE-Meta: U2FsdGVkX1/CSyuSGFnoOgIwOutiXxHCFBHdqNAIF2fd5uXmICk88oDPt/a8hRuURhK5SMnz80fr+RRWw8j0f1U+wx0sxTLlD7/ND1nGTQxjYhRXkRHKca5rW2wp/WLbZ/2MNuSKz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tz1eosKGNJ6qQmDb8mq0dNkyDrAfHkbgh2GF4/LubCnraWwJpBzdXXK8yoEXiwxhYa27Vxc8Ns1p5gbhCu2PBMHYZv3LD3FjIdvYqnsFUfRgIVojmLy90BA88QUrXBkVysq17oKt5ovrKhMFo0XOtnjxkeUpowLZ+2yQhWeEbetRGlJtyiYZeA+HQdXe6aQt2MBOWFbQFUt7eJmwYaOrEUDCmMZRk+7A== 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 | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index ae5cc42aa2087..f1d73fd448708 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); @@ -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);