From patchwork Thu Oct 17 09:47:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13839728 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 ED1D3D2126D for ; Thu, 17 Oct 2024 09:47:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F1256B0093; Thu, 17 Oct 2024 05:47:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79FBA6B0095; Thu, 17 Oct 2024 05:47:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 640326B0096; Thu, 17 Oct 2024 05:47:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 45A4B6B0093 for ; Thu, 17 Oct 2024 05:47:58 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 283D01210D4 for ; Thu, 17 Oct 2024 09:47:48 +0000 (UTC) X-FDA: 82682617248.20.441F0EA Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf29.hostedemail.com (Postfix) with ESMTP id 4C425120005 for ; Thu, 17 Oct 2024 09:47:43 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=UvVGY00C; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf29.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729158428; a=rsa-sha256; cv=none; b=kzlNUS05jAXNoW5mMZKPSs7c9BIjq2ZtcdsPb/PZRDnbxkt44yHwtQmSBZwyJQFEgJzA/G rZgayeK1TAqkySIjaFoDR5uhSIxyf3chIBJwDijUoS1B17z/66RtwdK7M/7jvVqTaJJZtl BEeUM5ZBABFKtGDr4G9oE5ukL9q8Q9M= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=UvVGY00C; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf29.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729158428; 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=+7VdqwStWQj5e7m9yAII5NEsGFIvs7nS0Am+cBAHNsQ=; b=Ol0buwqTMNQyu5V1HDCUsW/jwQSGoiYOvENZp+1FV9JR7DIvS3yS9Le+YLkuyvFUXrRvNS Vca3GAEu2t17okzb9hKgqoesvcO0EwmWG6SIRpvMiU5mkM1QJCrjRaej14EVBfuaO5fKY3 T+LLwxCnfJxnUbw0zTn2JCTIQFa4MOw= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7ea8de14848so548332a12.2 for ; Thu, 17 Oct 2024 02:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1729158475; x=1729763275; 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=+7VdqwStWQj5e7m9yAII5NEsGFIvs7nS0Am+cBAHNsQ=; b=UvVGY00CFHPLCJtxusD6J6nCj1nnaq5OhfIdZYG4ZUMJvPdGLm/PfNflBaUVX8tFYa ArtN0wBWxEuMoBgQ3xpsBg0RZhqDva6NdPLC+6C0OVaFHxx+PQjRT6B7MDmhi0b5PhoJ q/UMPlw+zrDyuS+eD5bFV/A7ry3Gmb8Iu8o6LZiCw3wm2DjdCvOu6m/nSloAQVjZwgk1 /PftHdbDURuC1ayPVd/oepdn/llrS2KIDczf84nsLrUQdWMIhOwsaYKIddRe5O5D47EW bibiY0LHDGa3UAR4qLU5hXHhM6iVaH8xrNDC1IyufXZA4uWBjEkNdW//l12ssaRl2gkj 1Dxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729158475; x=1729763275; 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=+7VdqwStWQj5e7m9yAII5NEsGFIvs7nS0Am+cBAHNsQ=; b=TwA6Mw9SLXhAdv/2tfrDHwGfn0yC8qFQVmbj0L0C9YphnaQD6d+2Q1E+KGxPMig0zK R6jyam8aKjNfEgEQRpfHcvni6kiF0zpud1sNy1IDYfmxIgKOWFovlLPwoMOn4wxDCBoj tzdfn1f+viK6JqdNidA4sR6PrplyepBCqVAJJw43juCSPBQs13M4fr1/LfxUv/kLBR/X OblY3YWjMu0xiNUkXwkR13wGn8JMa2ZbNpDIrBZjlNko1YRC5HHeGjA7MgM33M1lulEG 2cahb0hC+D1Ic5g5wxwJ6pL0tAnb3PvQmf1acxAFYUBk+AgwF+P6qV3xeCgKtN82JbKh 8+0g== X-Gm-Message-State: AOJu0Yx1hNkKkOaD7bKtvjMjBEzrqqnaRxm8AKhu1Jt7SGDR66fN79zf WjD5hui/GIPj0DGo+vq33CNL2WkkBvJy0ixqLh5tweMqdzb2fBPfNxhkzknPScM= X-Google-Smtp-Source: AGHT+IHzuiiCIun9pa4mZiHfpa9E3frsNbxHgaXVoXJzu+vwbmm9rxbIGaUhA7zByM5nDjwu8FuQmQ== X-Received: by 2002:a05:6a21:e8a:b0:1d9:181f:e6d8 with SMTP id adf61e73a8af0-1d9181fe778mr4119688637.31.1729158474948; Thu, 17 Oct 2024 02:47:54 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e774a4218sm4385365b3a.120.2024.10.17.02.47.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 02:47:54 -0700 (PDT) From: Qi Zheng To: david@redhat.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, jannh@google.com, peterx@redhat.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, Qi Zheng Subject: [PATCH v1 1/7] mm: khugepaged: retract_page_tables() use pte_offset_map_lock() Date: Thu, 17 Oct 2024 17:47:20 +0800 Message-Id: <258de4356bdcc01bce0ff1f6c29b2b64a4211494.1729157502.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-Rspam-User: X-Rspamd-Queue-Id: 4C425120005 X-Rspamd-Server: rspam01 X-Stat-Signature: 9mrmkzfk3bjnn47fsjwxxxi5y4end78e X-HE-Tag: 1729158463-768521 X-HE-Meta: U2FsdGVkX189ifysJDQH6yt44sOXBtsIlOJn2jIiGPQ8YyqkJpBP4MkcyMZGXN11tJ0JggnBfeMUtz3odbj2MdAmHaqHKZiSOD/svZ3qsaMoi4zHrvfQHsexk5n30LeZwfGjRBENWm+dNQ4DsGmhMcJD+S389OPK25nPJrlP690QSTSTNjrN+eQJJCrgZCL+kPhk5N+ld4zOS97AUwi35PpFNvzx50LkU4NXX5tLIznnG5yIK8ViZrSO6MojHtxIbu7Xrhfgwsx80ap8R8MiQ7vj51QKB8/SWASG0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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_lock() to do it, and then we can also remove the calling of the pte_lockptr(). Signed-off-by: Qi Zheng --- mm/khugepaged.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 94feb85ce996c..b4f49d323c8d9 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 @@ -1757,9 +1758,15 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) mmu_notifier_invalidate_range_start(&range); pml = pmd_lock(mm, pmd); - ptl = pte_lockptr(mm, pmd); + pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + if (!pte) { + spin_unlock(pml); + mmu_notifier_invalidate_range_end(&range); + continue; + } if (ptl != pml) spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); + pte_unmap(pte); /* * Huge page lock is still held, so normally the page table