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: 13772879 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 CD8A1C531DF for ; Thu, 22 Aug 2024 07:25:44 +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=3XLeH1tU+payUby5GohREupClIvRGZdbeLsp/8eA5FY=; b=piU7DMt+x5fr51kpiLeBoXwmwY 3d2hC+aokKYEtyMN883h+56lE0V27tD6Pr45YOVgJofMaDEA4RsoqzsNUnRwfInD3xwgRBrh/DKLN KZ2fAFL0/LZ7yBdnHfoK97HX38d2UpebTKDui22zkAfqld5hMuZZ7lLsld59s40oYsWLfmkLuvOwM IHY7AT2AXYRbimlKyKnBjwn1VzFWI7Q7GeIMRqn3Q/4KZ8ynJt7a21zBxozxtIBZBmYy7pqNLsPCf mxyQk2brrWClR9vF374l7hNTVZ5jLMZ1R0STqCx83UfG9FnJak0bai+f88n9ZdiBuReaeLw5V40XI /eXUKdhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh2CO-0000000BrRM-1Iqe; Thu, 22 Aug 2024 07:25:36 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh22H-0000000Bnbk-0ScM for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 07:15:10 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-714261089c1so332092b3a.0 for ; Thu, 22 Aug 2024 00:15:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724310908; x=1724915708; 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=3XLeH1tU+payUby5GohREupClIvRGZdbeLsp/8eA5FY=; b=UoiNMxWMHnRRmzLYphH/qXukT70LwkogG4AsUhsvMsGC1VcZNomh0zP/bVWIXEElr1 q6XHiAQJ3vdFzr+zGU9wKlCxGcPVoIJoYSxvZHFf+u9/YRwsnEqXWCSJFg0DvFvuQCsT N3BzPxYxDoG3tAjfHusRu66JWuUpL7s6TgUGBF7pCg52+fk8bT3tMnHupr7F7RC2T+7Q ZxAsKVOhbu+bmtxK3b1VUCFWgGjLF18B9Zsg5/yv7ANjCVvYBx9id0w4ropaJ256eCO0 dHp4PcxA8S6je035FY9cXvg8yXoq+zXZ4SXram9qTG0fs/KDhnKs5GVzrF83knmOcD55 6n1w== 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=TSYQ3Pxm26wMx9u2h+lo/hDJaxxZ2HPet52LQfHEwoiRLVO1KHvwp77yZuxD+T7v8Q VbsMesYz2ec8J810K5olynNXME7iPOFrOmczgvYFOi0IRtz56F+6VPEpI5amIJPnxEvn PQGjSVnjWIWtn+ixwdnOBSAXQhERWGcYMVQbpjDyJt6AHejC/NnZeLNTWa4TRv0Q2IKI xoDr6C+I+n2OLO3ElScZPG9zcjOznCrnJTDiTgQ2ibUsJu9kyJHGZs2ZlyEI5VHgAgy+ k13NmOV+u1aVvi3SL/QmkYItAni01vM0mi/kHXA6gzY1A6h7ExCnFBHO5JOzcnKMqPSs cojQ== X-Forwarded-Encrypted: i=1; AJvYcCWV0jnwCHsnqNiLF1MlN3YedPQOFB14GjBOmvmmeg4FkLzTAvRRBYokzmJ5Ei5mAyqp2hxXtF+eujNpZQUV/3uG@lists.infradead.org X-Gm-Message-State: AOJu0YzgUHgI+GVYo127kNV4TdBanOQ1jYkh4kb4b1LgyUiX2cleqvUl EEup6FZiCMi+FHPBZsJRKCQ1jQH6meziL+4YghwqjoQw+tV30A4YOKghI6a9dkA= 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_001509_311404_BB5646EA X-CRM114-Status: GOOD ( 12.89 ) 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 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