From patchwork Thu Aug 22 07:13:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13772843 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 BE60CC5321E for ; Thu, 22 Aug 2024 07:15:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F381E80019; Thu, 22 Aug 2024 03:15:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC2AD80017; Thu, 22 Aug 2024 03:15:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC66F80019; Thu, 22 Aug 2024 03:15:11 -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 A58A180017 for ; Thu, 22 Aug 2024 03:15:11 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 63F32C1313 for ; Thu, 22 Aug 2024 07:15:11 +0000 (UTC) X-FDA: 82479020022.04.51D7206 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf30.hostedemail.com (Postfix) with ESMTP id 859A880005 for ; Thu, 22 Aug 2024 07:15:09 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ghycqUH8; spf=pass (imf30.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.179 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=1724310828; 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=3XLeH1tU+payUby5GohREupClIvRGZdbeLsp/8eA5FY=; b=7RucurS0I/C53HPAOz/5nUP1xa/Roj8deowa/ElFojcHgIAlRmpexIjaqNgoeVrxzHryUa bqaCBqm9f13VZcSar5uEXwOFKpSXIRPsno8OTpA3dTy+AOYn6xnMTTxyLRFKvDkKTq2LrD eDLzu+dbF2d6ud6GwaBhqg6Mqbcl0VY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724310828; a=rsa-sha256; cv=none; b=4JJzlcaJP9KKjhkzjgj8p/leMuFc5UU+7oeOLzzztyCZslrF67EkBvMohcGVrc++1CwZNl 3ZLyBrMbAJEuJYuWGRNXZRpdZ45M25+9lYVhasgnOXXPuTMHc29fmglNsBkn6HTHsquMLJ UYs38nD/3euFQ4cwaSJc9tPGeZgOyj0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ghycqUH8; spf=pass (imf30.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7142448aaf9so338314b3a.1 for ; Thu, 22 Aug 2024 00:15:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724310908; x=1724915708; 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=3XLeH1tU+payUby5GohREupClIvRGZdbeLsp/8eA5FY=; b=ghycqUH8+Ii2t8ghynmqCUWL4IF713smQOxyQgeTV1JWWKx/P9yT/jGlLqPMSGCBeo 1OZgD0lzPRKVgNX4Q17H771Z7vLjcyu2XBSaddJnCWVgM1YaO3YTI7igsvBO98igkJXR J5U9am2KbCDQJkoT79PW1cH8wbtV1hqBEAMv3KpKVqGpakhWhwZwQzsv82JwE92g8adH eaxp5iL3YMAc0pgl+S7Z34bpqcdQoPb+CAfKOhq51H8zjBsiXO1j0kgCOLM6y8KPAcMQ KhoZsM4JYYmxqFn3lbtymfNP7xKQQUKHI2mn4q0jtW336JAvADcJ/Zq2foNO9UneiDNr XJNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724310908; x=1724915708; 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=3XLeH1tU+payUby5GohREupClIvRGZdbeLsp/8eA5FY=; b=DWdwk0GIZSuMcv+syxM3jW19b5iXA2byV08wqjNsdzksQHPCBOVrGWb41KeCY4KoMY ZbLhk24IC/MmEdRqtDv1cVa2cyGID2uaKgkopIF7uUPUbjiZfwAtTJ6WZBp8KWVu2DpO gM28mczZH8NXqW3TiCmIaj4TJdNGk78x/DCgfeftBbTXHJVb3W4zwo5LXzwtNC+V+wLL WSSHFcJb8LBqxB5Ip+GI5ImZFDi0wD1wlLv3Th7Xs28i+P6A1iBob9ADp3GS3bfYLbTP WV8MPUcCHxXQt0uMx9B5nf7a05fnL50nVfhMzziWiuE9sCd6bNmC0WwT2G0kTOi4FQYD ENfA== X-Forwarded-Encrypted: i=1; AJvYcCWZNjhTofxDgywD8rMNCJqDigshlysTNs3LbjODxyPv0RipH8x8VQIO90eCeOLPXAGC3nJNWrce/g==@kvack.org X-Gm-Message-State: AOJu0Yz2qBtc57pwONdtkRGWpsieg1CgwIiybvIpXVLTtdtZJ/p4yaKl fNpfWi+f3NWnwJE+nYzvXrW7BDA3Y9FmLV10ioNjKzPAY4BumapQox8U1PDvszPmLz9NRwOCqlR DhSQ= X-Google-Smtp-Source: AGHT+IGjO58kdb6JjGpOfA0UnXFn2ByeOkp/uIZzxQt7YtlV+9PVIdBNBOEj4fOrvZDCpXHiWUn49Q== X-Received: by 2002:aa7:8244:0:b0:710:d294:a2b with SMTP id d2e1a72fcca58-7143176773amr3032765b3a.8.1724310908168; Thu, 22 Aug 2024 00:15:08 -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.15.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 00:15:07 -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 14/14] mm: khugepaged: retract_page_tables() use pte_offset_map_rw_nolock() Date: Thu, 22 Aug 2024 15:13:29 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 7ttawarwk4xc8yoaq6q99yeb66ta1h8k X-Rspamd-Queue-Id: 859A880005 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724310909-90157 X-HE-Meta: U2FsdGVkX19TKmuFVRMH0bAFPEb+VCaGBff8PiFVj0sdH2s6z4LRZiHnDiGco3/47AUmlRiAB68s2QfUK0yO10xfc4P7fgUU2iBhCO13tnGbRVQO0tqMbko2HrqBZGBMQdmFEp4Aiu9jZQ9y5V72cDshDVpIx+ojVrDBbhDRYRESFAHbplSRs2ZU0hFTQT+rj/N2xfFA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DW7IWE8Mom2JRhTF69ostDwDhXjJOWA4MLTaiNytCo93kl54cc1m4FWj2hFpQaCC4iFv2rrdHAymcOHVvbTyjRvlCQMrEeao5uhdO93PMrZrX4Pb2q8ZexVtXp61YntihSXnAXohU/PgqOA93h92ag7z6WCZ9tz8bhaQ+KgIF6INzrI 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 retract_page_tables(), we may modify the pmd entry after acquiring the pml and ptl, so we should also check whether the pmd entry is stable. Using pte_offset_map_rw_nolock() + pmd_same() to do it. Signed-off-by: Qi Zheng --- mm/khugepaged.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 15d3f7f3c65f2..799412041d262 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1723,6 +1723,7 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) spinlock_t *pml; spinlock_t *ptl; bool skipped_uffd = false; + pte_t *pte; /* * Check vma->anon_vma to exclude MAP_PRIVATE mappings that @@ -1758,11 +1759,25 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) addr, addr + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); + pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pgt_pmd, &ptl); + if (!pte) { + mmu_notifier_invalidate_range_end(&range); + continue; + } + pml = pmd_lock(mm, pmd); - ptl = pte_lockptr(mm, pmd); if (ptl != pml) spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); + if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) { + pte_unmap_unlock(pte, ptl); + if (ptl != pml) + spin_unlock(pml); + mmu_notifier_invalidate_range_end(&range); + continue; + } + pte_unmap(pte); + /* * Huge page lock is still held, so normally the page table * must remain empty; and we have already skipped anon_vma