From patchwork Wed Sep 4 08:40:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13790206 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 5F706CA0ED3 for ; Wed, 4 Sep 2024 09:19:31 +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=ylbiZHnndQyBxRrEPbsYoqCd5Ue19pmhRhZr0okk+70=; b=a8foyKxDCZr1RHhS1QzlP1/7fT 7rJs28Pqx02lbUJK/yGIWiI/i51vy/q/93u819oKTObxSBcwYWG96Jq9b90D7MOeHuQqT+5F6pcZP 9Nl1OEDcRw10wqLH4OWzdijA5dtLdfVT9Tnh7xahJL4aJELA8i2l8Ctn0/C84/HtlBlZGNe0uKJs9 Wu6j4BFG3fjAHzKmT7SM7Hmc4XfsMPu9DZUK+wCqBKvsvNnGdrdbVG6yhX8S4Ru+DpMFsqrtlueqc uq+/g4adCP6wSwi5Aqvo2IGO61RU8NaU927ikUV/2KWODMxa34gJraXAh9iwi35V9O8GLr75Ha/YU K4Otcm9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1slmAZ-00000003dfo-3M0h; Wed, 04 Sep 2024 09:19:19 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sllai-00000003ToD-3Y35 for linux-arm-kernel@lists.infradead.org; Wed, 04 Sep 2024 08:42:18 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-20543fdb7acso28484505ad.1 for ; Wed, 04 Sep 2024 01:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1725439336; x=1726044136; 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=ylbiZHnndQyBxRrEPbsYoqCd5Ue19pmhRhZr0okk+70=; b=WT5N1pe2G513tFMmhl+Auv6oQoowuHX8dQFLOrLksMhnqahdF5aoIsNf0xqFFoKs2d L4+/H8fdDqRHpfh+E6bO7YaS6UWMejc1eEHVZkkSBi0N+VFVt8OQuzxYPbTA5OEeyQFB cE51iPhD1gTd6O5LnsBY82iF5kaa0APbH72nDJ8SCh1km4GDp0kZmZnIsmb0BbV0j+E/ G51qdsdr8rGIc4kxkjedoWJN5ZZ3+5vkN23a93j/eN9frg+iGj5JltHSm3uAjhcW0TaI 750okV18S0y8sLhelceVRhezVvsckXpnj+SryZjuEOuH0ihJBmJjv85h9+aFbP1c/0gr 9ShA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725439336; x=1726044136; 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=ylbiZHnndQyBxRrEPbsYoqCd5Ue19pmhRhZr0okk+70=; b=ZE2PXNCBfIavRitvSHRemKPMsI30KZ4oBH/6lDVdNq8YBRgkM1jcYJVz3vjfucQKbH sqidZuzlOsTWwf7qGoo0cMnrLVurJgaPoRo5nI5bDYfl/yMn0I30oeDUz0Wu/J31m/Wk 9Jf/jVxr9MXx4/yf8H7SiUloUPb/MgXi75KNpSh7V32k9jgHczPYLlyyAcWNFzS8+68p w1HXbPGwn8r+rkIGwVwsLykMg5CRHFuMpMdh3fNDXoZBQzSST+S6jWt6Hknlurx/2VJX IATkyoYOLjeQWjXFfAO7JaZ/Cc/SiQq1x86sRn23t0tA1bKmjf55npU9QIUinI8Yf6jW yk6Q== X-Forwarded-Encrypted: i=1; AJvYcCWlLK0xjRI7SdoAOcrUdRYYqopNrMOhRSVv/0gudlHRLS8616Pu3r5N7NTsg5sX3kz9NjK7xy7Y4sFV4y/bFi++@lists.infradead.org X-Gm-Message-State: AOJu0Yw9ddgqgC5dd2AuD0JB0piidHhK190Qd2hCx7E3cKrKwO53gQGh EH47br/siumFMboggOzn0Ow9pvrOK2HCmlQfuLgzy5M/yyV4v3l3fZLdSGF75lE= X-Google-Smtp-Source: AGHT+IGRRbNTDVHBE5FEDByUohEYiQ4MupDiF5X1fYOKyMaX8J1f74Xy7lHxJ4SbBUityp3jftUwsg== X-Received: by 2002:a17:902:e848:b0:206:c43f:7896 with SMTP id d9443c01a7336-206c43f8dd5mr2410565ad.21.1725439336115; Wed, 04 Sep 2024 01:42:16 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.242]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206ae95a51csm9414045ad.117.2024.09.04.01.42.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 01:42:15 -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 v3 14/14] mm: khugepaged: retract_page_tables() use pte_offset_map_rw_nolock() Date: Wed, 4 Sep 2024 16:40:22 +0800 Message-Id: <20240904084022.32728-15-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20240904084022.32728-1-zhengqi.arch@bytedance.com> References: <20240904084022.32728-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240904_014216_903299_AF00A4CB X-CRM114-Status: GOOD ( 13.06 ) 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_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 a117d35f33aee..318cc3eefb040 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1724,6 +1724,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 @@ -1759,11 +1760,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