From patchwork Thu Oct 31 08:13:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13857704 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 6190EE68958 for ; Thu, 31 Oct 2024 08:14:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1FD46B008A; Thu, 31 Oct 2024 04:14:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C56A06B0096; Thu, 31 Oct 2024 04:14:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1E5E6B0095; Thu, 31 Oct 2024 04:14:16 -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 901DD6B0096 for ; Thu, 31 Oct 2024 04:14:16 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 529D7161221 for ; Thu, 31 Oct 2024 08:14:16 +0000 (UTC) X-FDA: 82733184660.14.B8BAB5F Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf22.hostedemail.com (Postfix) with ESMTP id 72775C000F for ; Thu, 31 Oct 2024 08:13:41 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=edx4yHPM; spf=pass (imf22.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.180 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=1730362295; 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=ipuWYZPCHxpoUpiQDdIApzlSejCgHBDIxG7j4EZzxr4=; b=To48849lnZ0I5IrMXOH8/F5yzDUtgTGRLwNDeTuYzPchgcHKC9FdOes/WLkQcyqU+q8zT3 aruQPzeEy2nMg7nWasnD73BkhpW47T/7LFxVLFerCOgDwuyUcrmUM407vsUbnqr61LxNGI OHyj4TXNkKD8OuzL1ujdGBLQ/ChKexM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=edx4yHPM; spf=pass (imf22.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.180 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=1730362295; a=rsa-sha256; cv=none; b=pqc76Kw87acJlvrTApIoQp5sa0W+FXCZEDC1+AP5/Y/EGWVbOk9aQ9OLlRiPwGYtUwNkIb FE2tWa+6aCTg3EEJd4cm7hFKEHpj0JGLR+kK1BFBvgR8WV8y9OdrpEQpjIb/8sJAdGRGyp 12d4mKnIsgS2nQ6BWXYj1WADu2hP1K8= Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7ea7e250c54so600635a12.0 for ; Thu, 31 Oct 2024 01:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1730362453; x=1730967253; 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=ipuWYZPCHxpoUpiQDdIApzlSejCgHBDIxG7j4EZzxr4=; b=edx4yHPMvNYIbj9znbHKovq4Y1XN0JQQXlARbjUdsLw4y/8GqQ4KIVQw/9iCV3dEdC XvzGG8bLIZcscbwLMdtj4uS6rRr3UWOCukXzPzLgfqmpzksGF2tPMw3UwZNNS/fod21L g3xP9QgpbZodP4O4uE7t2KORmP2OQYUxxPFr8XwbSXUpKiDf8th5ksK7C4WjcUomC8r0 IuegpCNlc7X/6NOWuM/1ijmcuUVC0Czy7Yxfoz1yFBDocgNxqw1yPg42qF1ou9eF/loU x5e9/UAs6gWWJMhGpJJqtSbiEQ3ZSapfaqxivOF7ThADKnovxOlReZRgOAApWgPiW8ZS voow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730362453; x=1730967253; 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=ipuWYZPCHxpoUpiQDdIApzlSejCgHBDIxG7j4EZzxr4=; b=aLX7Jsbv+m9deoHwPI3/34q/hMea2NBpiVysQpKHGaHSZzKPwm0cpH1yk+rbr2Mmz8 xNdC7SQp5CGk5N1zgDR9X9//unZAqyoxNNXZKKXztlnCYio1IqRsGBMYiIYTNC1eBWGh 8PpyNjdXaxlGwSBIYgMnhtIgavmcZrKkZNkHw9qz1vqFuhxsden0TyfD+hL75upX5Amd DxUh65EQN6+oKT2GaE/ebc7IUqkkBWCX8wKRMaL2WOTt46UNRixBz3m4qmt/TaUrJReC JXgHQ2c9zP9q+2XPJfxUozWHVP6tI4Ia5DQkvH5aYR25xygx0vC7c6iD8IHRp0EaQjMk 57gw== X-Gm-Message-State: AOJu0Yw/0ua0DXoMWD6dgEKUoxvZnKXkuDETEMwa0KeZDjnLhmrmrHDl efKB1oIfltlrCiKZrtrUqtYuuGqmSHufZ6NUKWJaSnMQpOTNEJq0wny7/1Od/Ok= X-Google-Smtp-Source: AGHT+IGhQTI8wahkjaT4dlfz/gtxWygzYcya76y9Il/Jpy4crLp7RHv4SMqkN227j2dZrXUEfJpRWQ== X-Received: by 2002:a05:6a21:3947:b0:1d9:3456:b71e with SMTP id adf61e73a8af0-1d9a83c1926mr23625663637.12.1730362453050; Thu, 31 Oct 2024 01:14:13 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9201b2586sm5163996a91.0.2024.10.31.01.14.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 01:14:12 -0700 (PDT) From: Qi Zheng To: david@redhat.com, jannh@google.com, hughd@google.com, willy@infradead.org, mgorman@suse.de, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, zokeefe@google.com, rientjes@google.com, peterx@redhat.com, catalin.marinas@arm.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, Qi Zheng Subject: [PATCH v2 1/7] mm: khugepaged: retract_page_tables() use pte_offset_map_rw_nolock() Date: Thu, 31 Oct 2024 16:13:17 +0800 Message-Id: <4c3f4aa29f38c013c4529a43bce846a3edd31523.1730360798.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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 72775C000F X-Stat-Signature: 9g4qj81gwtx5objp7fudun5yag64e39t X-Rspam-User: X-HE-Tag: 1730362421-413358 X-HE-Meta: U2FsdGVkX18gBnk6ixDy4BxxyMJuRmVaf8zYRJrBaGLgeht80cziDBTxIF4Erqtak/Le0zithHg4BGauOeApzs7QKtUemcL9MNG3E9P5ABZMvhs6GJhgl6xjfJu1axNvpxQvbe+6Fk3gL319KPtNKz5MH2S5ynztpwmBC+DX4z07ay6oDOKzAxKoU7SAz7UEVLAEglIzrhtjApKjhLpgiXvVn3qlYNWM4Yjmyp1PeoZa8etyUAyJmv8sPltGDIfJLc6IqaNtt1mFihm9Kq1E+fkQdRdekhYrXpzqelUHLX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, 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, and then we can also remove the calling of the pte_lockptr(). 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 6f8d46d107b4b..6d76dde64f5fb 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1721,6 +1721,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 @@ -1756,11 +1757,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