From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75FD8225416 for ; Mon, 4 May 2026 03:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866810; cv=none; b=u3JcA7W3sRl6d9uDDvvZilouB4PtaD5Hh5iyDncBLxBRvYQa1tL4fARKqj8BR/w9L6/MIe7ux5r1F2XFjtxFtXLXYo884UgYIjowB7o6yIqmlHxNX1OdAo7clJQTCObBXj5+Z/UPYzAL1tewm/4j+iFSColWwark71oH4mGylJ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866810; c=relaxed/simple; bh=R0AUDzSWQxPWg+a126fEPwXl/Ajgx4AVi7lR7GVW0QY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ZpsCV3ky70BfeqrvkW0cPTVwTtaOMuuIdJPiAUI3gFRb/CGrbWEKF6M6jkEHstXJLWg68YVH7m9I2K0Kifcbozd349YrCKQYlXgA8vSzM/vW6+nI73tkWP7wycFdy4Um7bX889/eI5kPgZxtX0UABy+rOm0lhADWTQKtTLK/9Ok= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=DggAExdJ; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=Iw+mjYYN; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DggAExdJ"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="Iw+mjYYN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777866808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nuwaC1QCoVaWPCWlh6YJVeUyAviJpTjgkGZ69+fjPic=; b=DggAExdJ9t/QzWWM4gwTcQqK9557AFouNO/DfqYftcycfZ4Dp6gVThzaghN1hK+YAVPRD0 unu/NQAO80gPQi9wTUBhzaH9nuhL5Tx/dQ5jMk/EqfR2n47uZEvyagTJPo+vCxzO8tmX+J pAo1YZBQ714pb1pXxTg5S+H6Ah5fJek= Received: from mail-yx1-f71.google.com (mail-yx1-f71.google.com [74.125.224.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-y0S-8LYONdaFjJEinzZwtg-1; Sun, 03 May 2026 23:53:27 -0400 X-MC-Unique: y0S-8LYONdaFjJEinzZwtg-1 X-Mimecast-MFC-AGG-ID: y0S-8LYONdaFjJEinzZwtg_1777866807 Received: by mail-yx1-f71.google.com with SMTP id 956f58d0204a3-64956932a51so4496845d50.0 for ; Sun, 03 May 2026 20:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1777866807; x=1778471607; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nuwaC1QCoVaWPCWlh6YJVeUyAviJpTjgkGZ69+fjPic=; b=Iw+mjYYN5BC8xxMjjX1BgJXn2asgCReostFQ1D9G9cwoCWp4a8fu6FdWM6t3MqsVAk +koDhi6A+0OdY+mHv0rP/K9zf84JLJL0qv1w/1Kk/AqqMDA6vB1TZF1kyYNbFZeXBk+F 97AeuEaUHJdX9qJIpmMTKUPa0evbDIdWZLL9JKEPO2eMhBqok92LLsl7Xc408aGKkNSp CV/Hzm0NV/WgJOBU25+0yvnvjIPbn9fA4kh4pnHj7b4pC+4cKvkRoLxWA57IoLSv2gYl ipjcdRzvZpLtg8uW01GPEiai86ntrnGyEECZbCKEpDSuNKX+FFdSbosXRptWaUrtNQTO GU6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777866807; x=1778471607; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nuwaC1QCoVaWPCWlh6YJVeUyAviJpTjgkGZ69+fjPic=; b=RSgm1WXqmuBXPGrlmIKcyTS50ULfmtYQhPDwm/edZKeabVzbFsrUrAhK8W+MJ6YZbC VeKttO42cMrU18tqu5EXGEs1Hokob4xTqAxMtpd8stIniVhuQNt05gbLqc25Ch9Odebt YeJo6bUwon0X4yu/X7Bngqu3TNlX6xjDcElETVlo1SjCBOknN9CasA/RukFIWM0Pi1oN uH9wqMHhQB2D++AxV3KyP4hk3/LLwpWpTgit8QeakSsMxQ7vPu2YvBUxLuUkwkxFCTVO WRfuudZTPu7xSj0lILI2dP2nZEJkwltsWusj/n9P/tq54j1TRv9KNuzF3kEOY0VF88U9 6Myg== X-Forwarded-Encrypted: i=1; AFNElJ+cNCzYTidchNuBHAlClxsohBheOmet5XOprL+WG2kWj6NM9UwvP8bsar7reI3M07zVCzP8oYzm6dp2MuI=@vger.kernel.org X-Gm-Message-State: AOJu0YxDqrvCjCrL5Zs5YYNBvo1fM++k5YNe3v4++pWS5HNqA+R1iMrs 4jBJ6ELOZVAf+6xNcamubGQqpBwRXqni82WLnnnMJXz8oDU3tmbOI29O0LgMUdWcw3RdMB9R9Ff Vxdk9LhVXlbsgpjqokQtlu+aRLKaXq4S4QdXBf1BeeTrDojyHb8nfS6KBLvV1w2CXtg== X-Gm-Gg: AeBDietPj6jC8yOfB+1yVKGevuJK032H5vlj/WZ7AdnxWGHue2UENfZt8F16XszHf6T HQJKS53qPwEg3uq+r0z+wloEUZbwguL7sD216e9pGzjOQS00HUjmbU1Rvb/wCoD1Z1rVJS9JPiA c3+Ydor6fyAIMyoXHCIAj5w8P8lKKKuipZmD9fuBDYwEwb8AUOrD3MmF5vFhgfjUdZ/1CKtX7Aj bJMiG3t4i8Z2JKHkqh+7sNx80u/yX91vGQO/dspkPx1ZE8JAQ+XX99yC0OEffzArmqc5VgrpAOv ldVDh+MOFXzDqY7lIQzqNDLWFoa2XNMGWuCr3zF35sss34kiAjBYVU20U/WYo1h9h2YdMnbzKdo i4/HM0GsxbwYkthzFoZd+f9Zz7xsQf6FqVQ2OFWe5asGBXrryEzqcSZtyuMmY5bHoL7A= X-Received: by 2002:a05:690e:13cb:b0:651:be50:af09 with SMTP id 956f58d0204a3-65c3d96ca98mr7541977d50.3.1777866806632; Sun, 03 May 2026 20:53:26 -0700 (PDT) X-Received: by 2002:a05:690e:13cb:b0:651:be50:af09 with SMTP id 956f58d0204a3-65c3d96ca98mr7541955d50.3.1777866806149; Sun, 03 May 2026 20:53:26 -0700 (PDT) Received: from [192.168.178.105] (78-0-206-107.adsl.net.t-com.hr. [78.0.206.107]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-65c2e8b2e55sm4980740d50.20.2026.05.03.20.53.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 03 May 2026 20:53:25 -0700 (PDT) Message-ID: Date: Sun, 3 May 2026 21:53:57 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 02/14] mm/khugepaged: add folio dirty check after try_to_unmap() To: Zi Yan , Andrew Morton , David Hildenbrand , "Matthew Wilcox (Oracle)" , Song Liu Cc: Chris Mason , David Sterba , Alexander Viro , Christian Brauner , Jan Kara , Lorenzo Stoakes , Baolin Wang , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Shuah Khan , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org References: <20260429152924.727124-1-ziy@nvidia.com> <20260429152924.727124-3-ziy@nvidia.com> Content-Language: en-US From: Nico Pache In-Reply-To: <20260429152924.727124-3-ziy@nvidia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/29/26 9:29 AM, Zi Yan wrote: > This check ensures the correctness of read-only PMD folio collapse > after it is enabled for all FSes supporting PMD pagecache folios and > replaces READ_ONLY_THP_FOR_FS. > > READ_ONLY_THP_FOR_FS only supports read-only fd and uses mapping->nr_thps > and inode->i_writecount to prevent any write to read-only to-be-collapsed > folios. In upcoming commits, READ_ONLY_THP_FOR_FS will be removed and the > aforementioned mechanism will go away too. To ensure khugepaged functions > as expected after the changes, skip if any folio is dirty after > try_to_unmap(), since a dirty folio at that point means this read-only > folio can get writes between try_to_unmap() and try_to_unmap_flush() via > cached TLB entries and khugepaged does not support writable pagecache folio > collapse yet. > > Signed-off-by: Zi Yan > Reviewed-by: Baolin Wang > Acked-by: David Hildenbrand (Arm) LGTM Reviewed-by: Nico Pache > --- > mm/khugepaged.c | 28 ++++++++++++++++++++++++---- > 1 file changed, 24 insertions(+), 4 deletions(-) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index 6808f2b48d864..71209a72195ab 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -2327,8 +2327,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, > } > } else if (folio_test_dirty(folio)) { > /* > - * khugepaged only works on read-only fd, > - * so this page is dirty because it hasn't > + * This page is dirty because it hasn't > * been flushed since first write. There > * won't be new dirty pages. > * > @@ -2386,8 +2385,8 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, > if (!is_shmem && (folio_test_dirty(folio) || > folio_test_writeback(folio))) { > /* > - * khugepaged only works on read-only fd, so this > - * folio is dirty because it hasn't been flushed > + * khugepaged only works on clean file-backed folios, > + * so this folio is dirty because it hasn't been flushed > * since first write. > */ > result = SCAN_PAGE_DIRTY_OR_WRITEBACK; > @@ -2431,6 +2430,27 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, > goto out_unlock; > } > > + /* > + * At this point, the folio is locked and unmapped. If the PTE > + * was dirty, try_to_unmap() has transferred the dirty bit to > + * the folio and we must not collapse it into a clean > + * file-backed folio. > + * > + * If the folio is clean here, no one can write it until we > + * drop the folio lock. A write through a stale TLB entry came > + * from a clean PTE and must fault because the PTE has been > + * cleared; the fault path has to take the folio lock before > + * installing a writable mapping. Buffered write paths also > + * have to take the folio lock before modifying file contents > + * without a mapping, typically via write_begin_get_folio(). > + */ > + if (!is_shmem && folio_test_dirty(folio)) { > + result = SCAN_PAGE_DIRTY_OR_WRITEBACK; > + xas_unlock_irq(&xas); > + folio_putback_lru(folio); > + goto out_unlock; > + } > + > /* > * Accumulate the folios that are being collapsed. > */