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: 13810129 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 7D78ACF9C71 for ; Tue, 24 Sep 2024 06:11:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3EB66B0099; Tue, 24 Sep 2024 02:11:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC68D6B009A; Tue, 24 Sep 2024 02:11:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D41186B009B; Tue, 24 Sep 2024 02:11:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B2D1E6B0099 for ; Tue, 24 Sep 2024 02:11:15 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4B7F3141810 for ; Tue, 24 Sep 2024 06:11:15 +0000 (UTC) X-FDA: 82598609310.07.F2A7405 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf19.hostedemail.com (Postfix) with ESMTP id 70B431A000F for ; Tue, 24 Sep 2024 06:11:13 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=BRuIzasA; spf=pass (imf19.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727158116; 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=dtqkBA88kwvMRDkdpXIuTPjK06Yw44S9bnA3B3TRVKU=; b=6odYzg6DYOko/HiMxoCfXr3N+Y25DGUFR8pp1TM/4Fd2EI/oQ+ez9QsuqyVG9CN3JVORFu CnhoI/URVKJ1DfR2TGbnwogDeZYfOBcon9j3OwPWIBWGlyJ21zoOu8TsdfywzntZaJ7xBz RqnvU76ppgiUePAUIBVV4s/DYCGqxfs= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=BRuIzasA; spf=pass (imf19.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727158116; a=rsa-sha256; cv=none; b=nMQji2Qa7IuCjM92dLqrRxp6/RLh8qKFhtovJacK4erHbMtY5fIwXuunBVHBl3mDNJO9Cc B+/jIe6E9J/BG1bOauJRsvrYgLV4CdYgIfCe0juiEhhcHGOFBB6kt6wtqaLLTxbx0i/+0r MZlCRmTK+fnDNyO7xLVQmkKo7ORYQ8E= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-206e614953aso49438485ad.1 for ; Mon, 23 Sep 2024 23:11:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727158272; x=1727763072; 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=dtqkBA88kwvMRDkdpXIuTPjK06Yw44S9bnA3B3TRVKU=; b=BRuIzasADsWYMWpZxGf0jO99iPZmsxsDX1IcN6BBieHMCeNPhED3wgSFgfcXFMtuX7 Xo1bMlFB2gyuCXtRTloIll+VApPrTmcXSO8TTq5RP1mYAItfU7wSNRBKzI/xgAb0j0tS GvFHiIdZtts0/ZkoHuaiHQBYAhT3Ua0/YkvNo4TrvXueTXKACqldaDCYOFb9LNyHpqnT 2l7Vy4t8Snj1wvXQNHFkf9Zaf2k2TI1w8iVz7np2XjrqJn80i4j8dzj+QKNes09f6FXc q31Z0t+dwJ4PpTvkzx+2b1Nl41glvf0tORs64oHzDyuECghEcUFlvByzCwVAN2d3ppx0 GAhA== 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=eMhUSPdlAM+8RqQAx+/uN2wSHugl9omNmRIMDEhS5FAYNl5qy4Bd7wjziWX2d8CfuK 3m5tXdtBoUKPc1uQ0QKK6x1sy9+W36vQkE8WITTvVaf7ACAOIvcvOG9LYL7hLXfLoCcL +OS0bICO8YD+KDFnZYAYcTvtAynueunIPbxzQLxDRvuH2/pUjWd07hlektJnjA3YIGnD T3jjdn5i1ZzFatfbVvt6dSPCVukXQxqwKRqgZ3VfDi5Ddm/NPQcKUZRoIYTC+6XimWhB yuYbD5u9V3k9gG+bjcbsUYsRgpFYNzv/tHq+AH04lcOKvxEUbkI7FxAH363zsfMRTnLC O/tg== X-Forwarded-Encrypted: i=1; AJvYcCWwFVYu9LoHT+SxIsxyFPFo+AmMe/hWyKm5ITVwiFMrelj0P+F/GqlZywUO+OURKkNO1++k/+5TCA==@kvack.org X-Gm-Message-State: AOJu0YysLFJRNwaFHOLHpUyVrH1JC1WSUV9dT1Zc3l3uJpDv0Yj5vvIf 8kxxMgQ441hHCQuCy0vxwCjStGsdf1c1Cp+cXY68FK69dSfuCpHUBHBr9COl37I= 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 70B431A000F X-Stat-Signature: 377hqqhpw38xhqrms1yggiwoeh9jbsxk X-Rspam-User: X-HE-Tag: 1727158273-204345 X-HE-Meta: U2FsdGVkX1+XMFiP3mWMdWX42rw4q2H6lqei+VfFZYZfZD+yw5LcEwd3Wiky0aWt5s6pTh6BqX64CQtu8Wy80J5YqBfdehAwt9jgQedQlAiLGuLSHapQRaIzTX274EYgvJ2+mYOHh5P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Qse87T+dNW9Q37R++pPlyX2b57qN5M+jmn9AwRySaH31e+Pyow91jKmVgCmLrcbcv2Xkh1VndHolDWrxDhsHgyMN77XMZ5dB2ZVE22sUec/xdrdbVIjdvk0M3YVbt6IIN+W5lJ70FzqGwPY/D6YxDsKvgK9aDWhferXNsM9jgdEQoRdlHTDsLY8h/rUji5AEK9CuniWysWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=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 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);