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: 13772877 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 44C95C3DA4A for ; Thu, 22 Aug 2024 07:24:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=TKuz+eT/BjM4oBQXjKjArJ5u0hGAcRe4llal02NX1d4=; b=swE3VXBzCGVYtgAURsL2Ci3jGp JuKmtwb1Q6J7Wt6hip/UaRvX0/130VKRu29q41zCXdjYWenInscoC8vrS4APSMi6teNsqtVRuPpAB DV+kUettf746hYsBGC0T+NIpe60Aq4TUHmpuDNc/cUUaZC22zA+wr4kwFKMg06ZdXn/Yd42LSBODk gTfYwKXJ/u9lj8+lcm2tJns4pUi871irHU5ycFBZ80tyqNYshXNmPX43VNyntsLc1bLkxbajLPbX1 j2STt2e4SC9IR1BeeKfnm9eAmAJ/sepWJHLKHdiAs8F9px32akVRrwsou5686jJZ5IWnvJSi+px6x ZiyqyfAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh2As-0000000BqwL-0p0E; Thu, 22 Aug 2024 07:24:02 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh225-0000000BnSy-3iY9 for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 07:14:59 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-20202df1c2fso10661875ad.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=lists.infradead.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=gyFV6xLgFegC1I1XsynOjzCcm/LZomG7EfDdbqMdtA1tA+hcq+Gx1AzsJfbfRmlW6a dfQ1L+EHdoNpXxhnB6cff6lHsYRUQWcF+1zfi/VIhKnwlZfcIhSOJvxrmUkB5JxDZdE9 AOf0M43dSFbeWrqp0hC4a4m0Eb2SgWP91QUy0k+EZhHzhu7mfRcEq7WKfQz8LSmnxMKO iE91dwUXsSfMqZEkeEcssufjut/RHHhUxzjhwDK7el9xSKUuxTZyyR2mpTnBjZWZ7pPS q+kclYjWbiQ/Yyxob5pjGYWgNZua6tanRe1bvbRGr1QviNW0Iyja1ibNy+d8B7dEhUuy 6G9A== 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=ZrsS/l0IdDeLMy8fNBG7vyetl3bc4yeB9eBfDZ3YIC1a90XUbhJSyydNuEjvkVZJ/s NcFGyyMtQI0ChvOj0Uuv1h8BRA7u4ste8Tzg91n1kIDJPsWnY3ngVMQdSCKUj/0lLrRr BqPmr9Ofg87c9bSXwgUc/Aw/hpT2Pdw47dBwCWH15GZ7oEQlN8N2xmmAt370Q072HDtI lfoahnsa8mT9RitB6gezlYRKjtZFYbLD33ISL0lUYRfzi75+YU+BWDomljtXOS/Vmn9e NToXVcHwa3orUpMRskQixmn2Ekm7nF7zzB6V3a93zQ2gDRt8DZSY7PvUIiz+JcvhKdrn y6Ww== X-Forwarded-Encrypted: i=1; AJvYcCWJlyiFL3QcP9RK+qsLzrKckLB5oem9kDP5XbXsnqOCxwNCBJG8rMUXhP2YtOnO/ewhT31CF/v5ohbO2x0dEE8k@lists.infradead.org X-Gm-Message-State: AOJu0YxDjlE1Gs2wJ3xglcCeYKMtHYWEsPbi551TpuTAI7GPYOoEX1Y0 4+MphPz3TkzjPz5IkxiSAnlWjlwAMRB8BfCKilqxO9IU80JF7FN+SNKcl4y7g4o= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_001458_040243_A345977B X-CRM114-Status: GOOD ( 12.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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) {