From patchwork Wed Aug 21 08:18:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13770997 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 162E3C52D6F for ; Wed, 21 Aug 2024 08:21:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A23A6B00D8; Wed, 21 Aug 2024 04:21:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 951596B00DA; Wed, 21 Aug 2024 04:21:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F2D76B00DB; Wed, 21 Aug 2024 04:21:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6000F6B00D8 for ; Wed, 21 Aug 2024 04:21:04 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 28BF440A01 for ; Wed, 21 Aug 2024 08:21:04 +0000 (UTC) X-FDA: 82475557248.05.355D22B Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf20.hostedemail.com (Postfix) with ESMTP id 513C31C0022 for ; Wed, 21 Aug 2024 08:21:02 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=lGyk0mzY; spf=pass (imf20.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.44 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=1724228383; 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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=e2P622kJ9gRaesobclLaX9yYk2i7S91mWJd+HFTrgOh65DkOARXSAoMX3B41UdGQoSB1LG H7SNq+Npki3zuxRoad4sHc4dKsV8rP59hOzITJdZidut9wWsZee/CCLtAXIvLJKjinUrtK kDxJEN3lG6askcHqk9wA/dgskP74iHQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724228383; a=rsa-sha256; cv=none; b=OWQq/rUBFR/hBQnqXJvTvPM74FE7j+Uop4ZPX9QDtfFH/5S9IFt2uHrCS5jlw9uQNybK/1 xjUhT8I6kGID/M1uuXkJpz2CHER5A5/I7Sc2CW9s5fiYflpiWuJVT4dMDYvqaFnnU5ar40 4xXrb3Y+sQc4ecGqsOHJPeffeWYwK1E= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=lGyk0mzY; spf=pass (imf20.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2d3d662631aso4423362a91.1 for ; Wed, 21 Aug 2024 01:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724228461; x=1724833261; 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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=lGyk0mzYHQPRG3NCU+9CS4JOtwk1TZVW7RkIMZhSfZ2N8sP+SdXn82jUKKZvzyPyKi P0Oiu3jp+hCcpbBz2ILwYk2Tex7XENkq8X+yI/Zsb+2sEjPq6ExBofVyxBsYJdIDB2BS IzgLcC1DjiAb3ckh3q7v7tFyur9iByATwB0udYuSS4FMvDKBHTiw19twoSIcIietExBM r8XyslPSQLkEyVCSNNd9bClzfhb92r3o6skPLwSSUP9YS6k7gAic4Gm8dwxqm5VHfSCN KKmcwYfpNiarEomDWBsNpRmtj4WAUVhc/CbeTszOShzggbG84Vv2SzRqYGaAzweFO6Zm OI0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724228461; x=1724833261; 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=9nMMKQBovMbpHaGn8Lvg/undANxxFw84odzUdkp5IAc=; b=k+KdizD8x7WVh3cvtla3XDqfqj4Jpy/Lg6M0t/iERAUWRLH1K9zX4YhZAj4T574rGq OmTxwOhnIxHpt6WCBLmT8xh6xFTH5bSf1MIyS8QHieGBc105HHcgCigYfs+vsXwyFL8p CRGv6jOy9VbVTN4IokFeL/NA2if3xxkah9oTc6n16ouQX1QUMhBCAQ4D9hXDSrjyR+gl hp3dvUzI+hMZ9s+AqnootDCUng10H6yJd65M56jVAMLdlomecynaET9pb75QcDuiAnj/ sZhmkFx9+r6t0sQhBEn01Fv7IXFvKpKJ9IPPt5fLqvYxWjBdTnnrq1eJKSEXXOsyi+WB TU5A== X-Forwarded-Encrypted: i=1; AJvYcCUFPgeXqVXUHTUQ5vmew4cQGnow9ZlTNTvQZp9IaV0rnxfZBrmgHCW3/yOOh/hKb70h+7OUbcLR1A==@kvack.org X-Gm-Message-State: AOJu0Yy4pbYVcgy9zAdjG48VnQIa2mTUF2J53Aj0G/Cp2m5JAO33BF60 0RTKTLLB0oOo9WuNL5v+fNOc3tUQzzNt5fbL46e9sSEc40fGAkZ7WxCW7XvBfu4= X-Google-Smtp-Source: AGHT+IGVCW7LuSOfGhGGSqonUfZfQEf1Y51LpMxCXHDPBrQX3ltYUdmnoycYhvy3ErzvacrcFVYd1w== X-Received: by 2002:a17:90b:3851:b0:2d3:d239:24c2 with SMTP id 98e67ed59e1d1-2d5e9b99f57mr1469884a91.19.1724228461120; Wed, 21 Aug 2024 01:21:01 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9049b0sm1091453a91.17.2024.08.21.01.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 01:21:00 -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 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 14/14] mm: khugepaged: retract_page_tables() use pte_offset_map_maywrite_nolock() Date: Wed, 21 Aug 2024 16:18:57 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 513C31C0022 X-Stat-Signature: eitr4xm35hbsphxeso4pg7hsgy6su8rn X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724228462-861508 X-HE-Meta: U2FsdGVkX18LxsIQAsEjp3sHg3s6xwwc4LDCOQknXep2vLKMoCDiIRqKo1mAyuAvuoXzFKqKNXsf3G2qvBr8Gp9lohp7AMwe704zx/oQDg8MZnN3EZAMUCFVFZVIkFSbmWq027I2uvAjBQQoc9Ej6U34cCvwZYZvzsguwxg47/uTKjoquv6LjMXWSJdD2o6W7vIxRR3DJFLMACrgcklwksGFSRa79l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+rs3WoOGAIBxuos68zLG+ecRmIPlEAt7/LwdJ6dNO+D9ZUNM6+sDTqD41BaVQxEzPBVPbjaULxggC//FFwMtSdb86yUwbk/gG4jSMvAr4v8kf+V7PRf57JfuJkYmiPzoOP+WHJ0C 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 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_maywrite_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 8fcad0b368a08..821c840b5b593 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 @@ -1756,11 +1757,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_maywrite_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