From patchwork Wed Aug 21 08:18:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13771058 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 7D881C52D6F for ; Wed, 21 Aug 2024 08:31:05 +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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=vRX+G2sDqNZJfT5q+AQSulD7Yb b9pib2D33sieocm0S3cnFFm8YRizQrNkd5QFRUk3ylmw+1KaLmd1oq46UuQg7j4ziFlFpVyXvsjpf GANNolv000nHYorRALh6AdH4Mdr5G0gKZPy4I4yr2rVFvuNjvD0ppiFebffEO3cJGbAIvvefuAKEx cEbNQtV5wrZ0AUCcT79fEtWiwMtzyxeP+TPLXz+bTOnD3ShaLgkLBg+vn9N+3bPuBPuaa56TQrYsu RKpAlFZ5391oH4g3F84dyQfbxAtNpZXdUObhwgrej8acqfEqkfSn7dWAmKrEHltyb3LPDYpcf6qPi EY8Lthtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sggjx-000000084YR-3D8h; Wed, 21 Aug 2024 08:30:49 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sggaU-000000082Kj-0SKO for linux-arm-kernel@lists.infradead.org; Wed, 21 Aug 2024 08:21:04 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-20203988f37so46225005ad.1 for ; Wed, 21 Aug 2024 01:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724228461; x=1724833261; 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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=kdmP2nKZPyEuHf0p+Iq8XHMSgDaS5s46zAkYviWH5ObzYq5zNq2nXVCMJNUN9/DQbU rkC/l0TDbOSDacFiCGAbcM4ik/oCon/cwoyK9VNk6DsYPRjZXrJ5g1yUf5WzxcqV3Fe1 umGD8SokaT1DzlnDCm9oSKH2I3XsbSnkPvmT5OGM4vHlBf67P16yxHQdVJ5xY45hTnEx kBzcd6bTc+3tEOBH04DSakfTFheJDXR0CQI7eyVLIOXhGJXC7uoaG2HBUbKe7kNjSCEa qlL9lyjg1ToBfaB6qRjS0V2pTk/q+QD85n8vGK3wXg680o1N5v6vJZgzXzTJDr3uLFHN QdGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724228461; x=1724833261; 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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=V1+pAjN6ReBNJKnbbYJ2Rg+TKUdXrp0R9L+dpKTK59G5Em333Ji+pfH/6bytL7fcMO mK7v9Ar6FaYytgeq+hX2k9NZ8VYAsCspCQ+kXrYEGmixo89PjTzji8Moy4RPhHBYE6uG 7SfCZituqjI6PfyApDudNqljmyQDg0RbQAFTKu9/76cvyxj0BNphtkruTzEpp2Tr4yvI 5TAy5zopcUqs9JxEBVH3r/iWjEvcC2P4AcHSnWoJjDdnlNROOXsfzAg9FR+UmSjo8aWs IZFJ+q7XDiYwWPijPWBcIQzeiMPy36k1mnWHRrl3aqN/7CW7RIHxbYxlxy+ElPovRLXf QmiQ== X-Forwarded-Encrypted: i=1; AJvYcCUM4WZ0YKwo62UXg7ConGLE3qkyBrP+UmiJXUh+nqyGvU8JurFjdcYPxICs7CtiM9T6mHlnqHqMr7yp0uVi0NIL@lists.infradead.org X-Gm-Message-State: AOJu0YzaNWO9qIJapyH2XkUwRj2gCsTgZDa2Yq2aHdLnvvHBcWPz1XXA 96rFMtp36fSOAgNVkqwsMi7WKa+pjsamb8fasgI9yjujdp+9pLN8JEedNVlcShs= X-Google-Smtp-Source: AGHT+IGVCW7LuSOfGhGGSqonUfZfQEf1Y51LpMxCXHDPBrQX3ltYUdmnoycYhvy3ErzvacrcFVYd1w== X-Received: by 2002:a17:90b:3851:b0:2d3:d239:24c2 with SMTP id 98e67ed59e1d1-2d5e9b99f57mr1469884a91.19.1724228461120; Wed, 21 Aug 2024 01:21:01 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9049b0sm1091453a91.17.2024.08.21.01.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 01:21:00 -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 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 14/14] mm: khugepaged: retract_page_tables() use pte_offset_map_maywrite_nolock() Date: Wed, 21 Aug 2024 16:18:57 +0800 Message-Id: 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-20240821_012102_164553_F45C6F3D X-CRM114-Status: GOOD ( 12.71 ) 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 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_maywrite_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 8fcad0b368a08..821c840b5b593 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_maywrite_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