From patchwork Thu Aug 22 07:13:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13772835 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 E01A1C3DA4A for ; Thu, 22 Aug 2024 07:14:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 489256B00C4; Thu, 22 Aug 2024 03:14:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 413B06B00DE; Thu, 22 Aug 2024 03:14:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 265466B00D7; Thu, 22 Aug 2024 03:14:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 03C046B00BD for ; Thu, 22 Aug 2024 03:14:47 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C1F461A055A for ; Thu, 22 Aug 2024 07:14:47 +0000 (UTC) X-FDA: 82479019014.10.8FFBD9C Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by imf17.hostedemail.com (Postfix) with ESMTP id 021B840004 for ; Thu, 22 Aug 2024 07:14:45 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=UuoXXJrK; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724310820; a=rsa-sha256; cv=none; b=MwfCj25uhs7LrHY7vySsqunEhsB9OcaC37xmlat1Mg2VfZdhwCUeZ5TggeRhB1e/BuVZ+R hhUAtauZK4WInxAQruka1HbVYzv5ogmQfEUXMVlxrr0kYZv+fhLQxUt71jS/e0gWa8qn7D 3+FnGUPHvR4AIP9m9Of1LLhjJdLK76Y= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=UuoXXJrK; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.167.170 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=1724310820; 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=Crbz/R3rJcbpbcovfp9fh4ozsAEMZSaAxrx7zvZfRRw=; b=vYQe3chGM0sYDqXKtgnJoz3e6pv9AQ4mj1rbcYPARAJMthjMdjXGXX3SidvQX+7sgyd/Zn UE1w6znS1e+ZpWyZD05xT0tVPQLV5yh8z+HFr+E5oL6gqV5vMmv+Mu9E7ZjWogLnMbZDFl 9j8e0P8tEOW/tK5n3tQP7Q0ypZRLkFg= Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3dd16257b87so212308b6e.2 for ; Thu, 22 Aug 2024 00:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724310885; x=1724915685; 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=Crbz/R3rJcbpbcovfp9fh4ozsAEMZSaAxrx7zvZfRRw=; b=UuoXXJrKYGm778yaWIRzZCkabDrPJ5rk9UCNPtRc+5KQ50zM5YqDvxQ8DkOc3GClGw V8QN0PehdKqQFEcf++ys826d8Ehhg3k7ZkTQrtz+vHzl05fsey2tKL23UkOurCEn5qMn 2aUD4PRVTXzBDpDpCbkd7tfIKIt221/j/IHUL5oI+5bQM0HHTWw5Q4pxMG4zMiDz1zQf h/YRsn8Np8MjlFMFE33wlI67UGRXes2W//fOu3JKjP/Ow1G5sOpDteNvkZcjz8quEf+4 73c7Br7bs46OctmkcMiAf86m5rXv478tE5GZ30ZZrKnPPcReKFx3qnx9hUUQzdsMSUWU eNLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724310885; x=1724915685; 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=Crbz/R3rJcbpbcovfp9fh4ozsAEMZSaAxrx7zvZfRRw=; b=sAjcwW4RlbFwMCduzK8o4OxtETeTjp9dM9WGWlQHmbH/GyzlYIzobUubOlKIrCbZH6 ZyD2dMxT3O5G+xSEIG4SFyKk/M70krNTUlJpHdy/2h3xcSbAs7tznh5TOj2rCvMabmRm TqOqFXrGcXIhIIAdjPAPpGIgbWPO70wbBB/gJxkjQkNKiSBRX6npvNRIMkXv/VDrdsnc ksTJ3RJBSn2j3ohKF31LyUdeFmRQUIiUx9jzp8T8a+nns0JGQ+abNlj5WOp8DAhgDPyn CxjO+hib3LTOwTQrV3cFQGjccqjWYy2inQQLpLmMVbUI8w6Zq9enaKkfjYHtCh1o9jZr um5A== X-Forwarded-Encrypted: i=1; AJvYcCXDcnRvHSpSty/wUrayPhmdcayxo5xuR/HgIQqbOzUEpxQCD/4m5fP7qTyLbS58w3TnBKISgM8bwQ==@kvack.org X-Gm-Message-State: AOJu0YzPL5Qtu8fRH04K7ZGWGgs22TvYbLaBxLY4/y9+a0UypM+ymz/k 4EzZlv1lq2aGNoDaLGrFQuZ2zT5s38sHmWkI2rTT5TD/VGbuva+CigYbeRrJFXM= X-Google-Smtp-Source: AGHT+IF73IAzwqsx0nomJCVJryAV7cndKc0EgZ0+2TYoDy25bfQlq+9JSwXFUBQmZZcxXSkt111lxw== X-Received: by 2002:a05:6808:d46:b0:3d9:29c1:be5c with SMTP id 5614622812f47-3de194fbf2bmr5329807b6e.12.1724310885053; Thu, 22 Aug 2024 00:14:45 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([240e:473:c90:f96:d029:ea8a:4e6d:d272]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac994a3sm695095a12.16.2024.08.22.00.14.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 00:14:44 -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 v2 10/14] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock() Date: Thu, 22 Aug 2024 15:13:25 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 021B840004 X-Stat-Signature: 619hc5j7g3ugpfkibfoqwsiti7mwjbm5 X-Rspam-User: X-HE-Tag: 1724310885-57771 X-HE-Meta: U2FsdGVkX18wdPOhMWq+RABi5Hu/ra2txxD0pFpMLmdLlscUBZ/R2LFICdddIUc6fVBvCjLxf/YzbA602QAPOcf0FLWZBL4c1b7PSs0pSh39CbJhaXcz9l0UupLoohiMpoWsuDla8eSZlj/J00Coo7yLmag1b5jbgW1qz6UMZ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L3UHxbD5KWUuhu4jsBLYyNvBsOjZOAzsIClXepoPtzuMimjtgc420s8Nm8v5Tdw7Ugr0gr7QHs1Q3buRzf0N4wpWudOQtMmm/m5VG8MWTcDS6JOagUDQ9lJOAUK/joKeuXYSykvVtpslX2T5LkHgg3F08mafz+p9EzhqBKro2wEh//jmhvikg 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 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..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);