From patchwork Tue Sep 24 06:09:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13810169 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 7542ACF9C6B for ; Tue, 24 Sep 2024 06:20: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=dtqkBA88kwvMRDkdpXIuTPjK06Yw44S9bnA3B3TRVKU=; b=IY1YB7cVRGYroA4uJrjFSaFaKD 22gMtXRUTzlTyAX7tLdvwgn9po6ND36373A8OB3dmzsfe6gPQJyIJ9nHqfze0+PHuDK8y0diFCawF zh2ON8+Qau9zcxPUXpoWDhC6HWVMNoR+3MG/k2gYN8QC1UAxsp6G4xtLYUtP8xwtG6IYfwYZ+pUi9 DSUBHk6Umh7SQOnbusg53iG7zG85rePYrsN5WIhbhaZoSGxlB3gub7YRVic/vNTvsi7m8bTIhZqDL QE5OxE75pvkHJ1yjcl8dfHbb4mUb7vs6wdLIDQWFhH9QERRUTaphGGNizZmTOadPtJi4ARCH8KbeD MG3FI45A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ssytv-00000001EAJ-1MeA; Tue, 24 Sep 2024 06:19:55 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ssylU-00000001BxU-3OHz for linux-arm-kernel@lists.infradead.org; Tue, 24 Sep 2024 06:11:14 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2068bee21d8so51320565ad.2 for ; Mon, 23 Sep 2024 23:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727158272; x=1727763072; 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=dtqkBA88kwvMRDkdpXIuTPjK06Yw44S9bnA3B3TRVKU=; b=cxC5iF/RFl9w0krj8Onij069kEnnvghfqFL0fphtmnOLmEzVZRYE12FOLNEVIqEYMg wTqeboL5RIhiTonBad0j0PoTpy3USgBfUW+lgTNVPFNIYweE5nyLJLXN6Xvdy6RpAsnn i9glMS6Eyh0ZdC7N+RlZdmvWWyLIVIXYseqgVt97CJA9pl6ds2ADeSnzHZmqxa3qWxNp EkgXBk6tl/oulTaH3dNd9X7Bt5BbhYbRYUTG3SVVxTLAJE/QtypdEvAcBHYGDawsaDtM aL4B5rZ+9Zbup8jcs2kIUeBH/wH6tZe2tf/u/LbCceZHfbVMlErQqzRkXCT7YRpEcZJZ 9IHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727158272; x=1727763072; 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=dtqkBA88kwvMRDkdpXIuTPjK06Yw44S9bnA3B3TRVKU=; b=j0EGEV+vFIqqKfCl9hXX2CooNqfRPhN6UQiNgYTlVZ5/0ajp4ajP3aVmCAZXGYoasK qluiWmTdPzmB3ZMCkBtPcT2/tXcIUrclehtK+LXNUlmKYmNW5lvHpseEDbOeGw1/GMAw srMXaQbQlnc3ZEgJrwWbF9GeJ8Ery3gb12KJhOPRN72XGqa16Usb9hv0nvJwoE18EqVK k4fR6wftRQjW7QJU470qwaadE2rIWkuGb9GcWUJz2JaOGLQV3UZQ52IrvFCC+//W0ZJT AO4xG36+1y8ZK2QIUeebGeTxsdxQretyXZfhGUlM/3TDoveh+N0WwDjObVLdewjVFyLG UCUw== X-Forwarded-Encrypted: i=1; AJvYcCUnZfWjEHakmsFZeH+kqIcY5Z8hDmch8ElUmn2L6raeyxs7qus5IMCtNkSIlI7mDYjIk3+yiWCSZULDTzQLl5Zj@lists.infradead.org X-Gm-Message-State: AOJu0Yyc+nRfj9w1BhfNZ2H9rep+SdEZSf3G5JGc4kNqM1mwf3fpKuKM cNEhArIBrswfgEC1SYWyrrFxVBJ9SldCz266m4csK9E7TaMe9T6wuwMKAKkW4Uw= X-Google-Smtp-Source: AGHT+IGXhl7eW49ih0T9YK4dpESD/v76/2vHb7m+lE9WqCGFaYPlARnoD/c7DbHxV22Za7qr6kwfpA== X-Received: by 2002:a17:902:f54f:b0:205:755c:dde6 with SMTP id d9443c01a7336-208d83cc8b6mr212103045ad.30.1727158272107; Mon, 23 Sep 2024 23:11:12 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20af17229c9sm4344885ad.85.2024.09.23.23.11.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 23:11:11 -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 v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock() Date: Tue, 24 Sep 2024 14:09:59 +0800 Message-Id: <07d975c50fe09c246e087303b39998430b1a66bd.1727148662.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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240923_231112_875739_2EFE754C X-CRM114-Status: GOOD ( 13.17 ) 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 collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held. Signed-off-by: Qi Zheng --- mm/khugepaged.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6498721d4783a..8ab79c13d077f 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED)) pml = pmd_lock(mm, pmd); - start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl); + start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl); if (!start_pte) /* mmap_lock + page lock should prevent this */ goto abort; if (!pml) @@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, else if (ptl != pml) spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); + if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) + goto abort; + /* step 2: clear page table and adjust rmap */ for (i = 0, addr = haddr, pte = start_pte; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) { @@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, nr_ptes++; } - pte_unmap(start_pte); if (!pml) spin_unlock(ptl); @@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, /* step 4: remove empty page table */ if (!pml) { pml = pmd_lock(mm, pmd); - if (ptl != pml) + if (ptl != pml) { spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); + if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) { + spin_unlock(pml); + goto abort; + } + } } pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd); pmdp_get_lockless_sync(); if (ptl != pml) spin_unlock(ptl); + pte_unmap(start_pte); spin_unlock(pml); mmu_notifier_invalidate_range_end(&range);