From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 205BD2E1EEE for ; Tue, 25 Nov 2025 08:52:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764060750; cv=none; b=IO2Tubn3SKQVaCOPR6LI9eWX3oCBY1n8JPgwgNAlvV+EAISVXIeznyK8EJaHLSI85VeCxxkivL8Otx97PxqOqQkfX0NzXt4UTEpIke8iKFrE+l3sQ1IbN3ISQL8oUiMheZl75RlQEBXWH0BVR/Bw9ugaLjvkSzslpYV7Qwb5b38= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764060750; c=relaxed/simple; bh=WACZF/HwQACWdEWXrmIS+prCAYLYXVe/fT2oiuOjUjc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=CEyIXVBKEYf5uXhBoNipU0/QJo64jLpz6Axk+lHfCL0FxYdmbtJ824xl4darl9HqUQth20XmeK8qAVkbRSAyTc4st1U6y5tWKO1D1y/OyGaUklaR8UES/u0lRdme4UyWCTgIXsoZvkIOMf/myHaVvPlZ+Ek4mghObUY64TA1jeE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jlWqvyTf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jlWqvyTf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A900C116D0; Tue, 25 Nov 2025 08:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1764060749; bh=WACZF/HwQACWdEWXrmIS+prCAYLYXVe/fT2oiuOjUjc=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=jlWqvyTfEjzLn7bT1JbFiKyTwqaxXFDroS868o8NNusJwRiTK+Sps76NoS0cLLPvc f0Zez5qO/g7UB3ZtE2LfiYzfMJwZrRFFfIvfQV3VBfQOBUdfZpNVioCAybcC4IRzIY LNhKW3xwaKQ3dlvcyzflVtWWnLfpJFAcKi+1MyMiWGp94SxVAwDhapEMqMZteLOLF6 j/6pttGboL2g6ZZLWd6UAiOZOwkHAesMCVAVmmZegLPh3bVgNpifhyjup4oWXLRZNq Jm4/S4AOfkg9xz2wfe4M4kkA1VrtxrD011ACQXUYgsRwS1aDd3UyMpqsyRlAASJaz9 r+C9omdYQxaew== Message-ID: <2f708eae-7d4b-43b0-83f0-7c2d98b294e6@kernel.org> Date: Tue, 25 Nov 2025 09:52:22 +0100 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 v2 2/4] mm/huge_memory: replace can_split_folio() with direct refcount calculation To: Zi Yan Cc: Lorenzo Stoakes , Andrew Morton , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Miaohe Lin , Naoya Horiguchi , Wei Yang , Balbir Singh , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20251122025529.1562592-1-ziy@nvidia.com> <20251122025529.1562592-3-ziy@nvidia.com> <33A929D1-7438-43C1-AA4A-398183976F8F@nvidia.com> <34bafd06-250a-4019-8b34-5ddeedea1cb3@kernel.org> <66C159D8-D267-4B3B-9384-1CE94533990E@nvidia.com> From: "David Hildenbrand (Red Hat)" Content-Language: en-US In-Reply-To: <66C159D8-D267-4B3B-9384-1CE94533990E@nvidia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit >> >>> >>> Like: >>> >>> if (folio_test_anon(folio)) { >>> /* One reference per page from the swapcache. */ >>> ref_count += folio_test_swapcache(folio) << order; >>> } else { >>> /* One reference per page from shmem in the swapcache. */ >>> ref_count += folio_test_swapcache(folio) << order; >>> /* One reference per page from the pagecache. */ >>> ref_count += !!folio->mapping << order; >>> /* One reference from PG_private. */ >>> ref_count += folio_test_private(folio); >>> } >>> >>> or simplified into >>> >>> if (!folio_test_anon(folio)) { >>> /* One reference per page from the pagecache. */ >>> ref_count += !!folio->mapping << order; >>> /* One reference from PG_private. */ >>> ref_count += folio_test_private(folio); >>> } >>> /* One reference per page from the swapcache (anon or shmem). */ >>> ref_count += folio_test_swapcache(folio) << order; >>> ? >> >> That is incorrect I think due to swapcache being able to give false positives (PG_owner_priv_1). > > Got it. So it should be: > > if (folio_test_anon(folio)) { > /* One reference per page from the swapcache. */ > ref_count += folio_test_swapcache(folio) << order; > } else { > /* One reference per page from shmem in the swapcache. */ > ref_count += (folio_test_swapbacked (folio) && > folio_test_swapcache(folio)) << order; > /* One reference per page from the pagecache. */ > ref_count += !!folio->mapping << order; > /* One reference from PG_private. */ > ref_count += folio_test_private(folio); > } Interestingly, I think we would then also take proper care of anon folios in the swapcache that are not anon yet. See __read_swap_cache_async(). I wonder if we can clean that up a bit, to highlight that PG_private etc do not apply. if (folio_test_anon(folio)) { /* One reference per page from the swapcache. */ ref_count += folio_test_swapcache(folio) << order; } else if (folio_test_swapbacked (folio) && folio_test_swapcache(folio)) { /* to-be-anon or shmem folio in the swapcache (!folio->mapping) */ ref_count += 1ul << order; VM_WAN_ON_ONCE(folio->mapping); } else { /* One reference per page from the pagecache. */ ref_count += !!folio->mapping << order; /* One reference from PG_private. */ ref_count += folio_test_private(folio); } Or maybe simply: if (folio_test_swapbacked (folio) && folio_test_swapcache(folio)) { /* * (to-be) anon or shmem (!folio->mapping) folio in the swapcache: * One reference per page from the swapcache. */ ref_count += 1 << order; VM_WAN_ON_ONCE(!folio_test_anon(folio) && folio->mapping); } else if (!folio_test_anon(folio)) { /* One reference per page from the pagecache. */ ref_count += !!folio->mapping << order; /* One reference from PG_private. */ ref_count += folio_test_private(folio); } > > I wonder if we should have folio_test_shmem_in_swapcache() instead. Interestingly, thinking about it, I think it would also match to-be anon folios and anon folios. folio_in_swapcache() maybe ? > > BTW, this page flag reuse is really confusing. Yes ... > I see PG_checked is > PG_owner_priv_1 too and __folio_migrate_mapping() uses folio_test_swapcache() > to decide the number of i_pages entries. Wouldn’t that cause any issue? Maybe at that point all false positives were ruled out? It is horrible TBH. -- Cheers David