From patchwork Thu Aug 22 07:13:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13772838 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 93EBBC5321E for ; Thu, 22 Aug 2024 07:15:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B682780015; Thu, 22 Aug 2024 03:15:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACAC980012; Thu, 22 Aug 2024 03:15:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F4BA80015; Thu, 22 Aug 2024 03:15:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 634D780012 for ; Thu, 22 Aug 2024 03:15:00 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 238DE1412BF for ; Thu, 22 Aug 2024 07:15:00 +0000 (UTC) X-FDA: 82479019560.25.C9BF329 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf27.hostedemail.com (Postfix) with ESMTP id 47AD640007 for ; Thu, 22 Aug 2024 07:14:58 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=eV5j5Vlm; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf27.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724310881; a=rsa-sha256; cv=none; b=DBxfAqaRMSbOFRvv68jIVRhwlGkZH0PV+0IntjqWylbYVjFYjObcPvpyHHnVE8NbMONSEh gcYIPiagpFE6nF71lKKx3Y2PasRg/9Wf+yGE+7libIqXG83HtBQaV6P4/KhT7w64hlA3k7 AgkV9cAXHLQLhI758gz0vViGP+4TLGw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=eV5j5Vlm; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf27.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.171 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=1724310881; 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=TKuz+eT/BjM4oBQXjKjArJ5u0hGAcRe4llal02NX1d4=; b=xo5lpFfuh8WQJpGgk9mbZ07PcD1BUljiD+AL84TdLZo1SV9ClDuFymnoa61xXv2ZnTEYid eK9E8Nlm+MMoU0Wit4GAmvTrprNWFIUIJ+u7rqR9J0BwUTMQEgii/ARQoBHOllf+tjUpHB Sv2QbFxJo8Wi4SLTt7x5/Velg8vYp5o= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2021c03c13aso3533285ad.1 for ; Thu, 22 Aug 2024 00:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724310897; x=1724915697; 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=TKuz+eT/BjM4oBQXjKjArJ5u0hGAcRe4llal02NX1d4=; b=eV5j5Vlmmzl5itiJGqqWi8UX/VuLF/LmeRCDFW/ELhMMtMG/K3Kuw3Hi8jlMrpgS/q KVWe14mXY/vZn8QB+1hmNK9IbnlO0f2l83jGB8AK20R0zHmD/rRzSXm2rKkO7eFfadjW ZPjd0Fdvwr/fwSLTjecZ78DVfSciRGt/r7+sv777x+fvM0gZtEEA9GRAZv/dI1M2QDYD XZoNW35x1nFEqEeBr+qQ9s9MmKgm75OVw5DqXCVPNSxWAj6WvgHzkAMgCFX/SZTXkVsH 39PnhayXQygiNHgCx2epbLAX3q1flzGUnVSfcI4HjrGeC7VUGb1QCiBbGQiyXTvceDIF lw4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724310897; x=1724915697; 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=TKuz+eT/BjM4oBQXjKjArJ5u0hGAcRe4llal02NX1d4=; b=fo1I1ykOMU4E7Hy37vadr6vnfY/i9Ug7kmMdedRndqoUqSnzG3v2PwHE5Vigw9oUtu FI7wnGsuCmdhb/DTwnvj1Df+FoftnVtiNAE2UznoYCPaTRwJknVSx461fpii+8kTeXgk 8W1MdDvpgX2XQ3IesVQRPcqhn4rg03UWJUBugZhvvE/sphiLo4/jusUuC5w3fDNMbNGg W5pBNFhKTMy4HRvQiBH5hMEcp9zMK5uMQ54fuNR513Br0bRTVmoQA/UxaDQszL07w770 UgWD17VKHpYBG/iDxGjD1t++dKBLGBDjFZnnxKDM9ux0gP3/sRoAvGcTZfTk8VU8H1MA 2piQ== X-Forwarded-Encrypted: i=1; AJvYcCVXLnJfyqC+YjhIE+Y448ORg0dUoe9O93VoMxJ97vaRYwHP/GMRKDLhwY88g7Pq7rhsw0XbdXd/2Q==@kvack.org X-Gm-Message-State: AOJu0YxZgXLUd8Ype3jvnQYnpzJ7+mUsUEsTu+MARUcifUdO2x7G+I7p 9i+PvulwRRREuV1N0vMc8EXQZRsHF+By7m9wvX9mPdS4pPa+XpWdizUbl11bSHY= X-Google-Smtp-Source: AGHT+IF6H0tDfgQqKcPaVFXqnNnvtvkdaP6bepcTCK17Vph5rD33YztktUCyS9gQ0weOULsun7jJQw== X-Received: by 2002:a17:902:e88d:b0:1ff:43a8:22f2 with SMTP id d9443c01a7336-2037fe188b8mr31159405ad.24.1724310896888; Thu, 22 Aug 2024 00:14:56 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 00:14:56 -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 12/14] mm: multi-gen LRU: walk_pte_range() use pte_offset_map_rw_nolock() Date: Thu, 22 Aug 2024 15:13:27 +0800 Message-Id: <0e2980e9a31d3503a5dfb350c04f4ede4d05220e.1724310149.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-Rspam-User: X-Rspamd-Queue-Id: 47AD640007 X-Rspamd-Server: rspam01 X-Stat-Signature: kpuq7ed7wy1cb71z17ehx8z67f9h3nya X-HE-Tag: 1724310898-322790 X-HE-Meta: U2FsdGVkX186tGkeSsD3S0lJDqQgWpHtFWm00tLMunooNoZf94F/gEBjtjfkmWdnFuD31v4Wjl8BzMu0F1t1g/ZUuwj4At84RLip8ft+UnAecY6mHsFAgU32rZaQ/CJEo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AEV3CScoU9bniwbdy5Q3d+GyNphfMRpGG/dCAssd826cWoSNHXN5E59TM3gLQ46UzujBoCkhy0FFZUUwImbnau5hDVjrbMJAjnNrkQy9z0sWChfgf0oZaZi6R8vfSm90p3U6vJ/TQIAlweZsk6nCv5fgQdbztiC/FOFQpxfinMqcSuJiqbftX7qvgHNsB/l10fSNpkDBycwnzSd0RufGXwDId3SD0Gey X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, 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 walk_pte_range(), we may modify the pte entry after holding the 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 ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pmd entry. Signed-off-by: Qi Zheng --- mm/vmscan.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 5dc96a843466a..89ef1ac8eb1a6 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3396,8 +3396,10 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); DEFINE_MAX_SEQ(walk->lruvec); int old_gen, new_gen = lru_gen_from_seq(max_seq); + pmd_t pmdval; - pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl); + pte = pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval, + &ptl); if (!pte) return false; if (!spin_trylock(ptl)) { @@ -3405,6 +3407,11 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, return false; } + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) { + pte_unmap_unlock(pte, ptl); + return false; + } + arch_enter_lazy_mmu_mode(); restart: for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) {