Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gorbunov Ivan <gorbunov.ivan@h-partners.com>
To: Zi Yan <ziy@nvidia.com>
Cc: <david@kernel.org>, <Liam.Howlett@oracle.com>,
	<akpm@linux-foundation.org>, <apopple@nvidia.com>,
	<baolin.wang@linux.alibaba.com>, <gladyshev.ilya1@h-partners.com>,
	<harry.yoo@oracle.com>, <kirill@shutemov.name>,
	<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
	<lorenzo.stoakes@oracle.com>, <mhocko@suse.com>,
	<muchun.song@linux.dev>, <rppt@kernel.org>, <surenb@google.com>,
	<torvalds@linuxfoundation.org>, <vbabka@suse.cz>,
	<willy@infradead.org>, <yuzhao@google.com>,
	<artem.kuzin@huawei.com>
Subject: Re: [PATCH v2 1/2] mm: drop page refcount zero state semantics
Date: Mon, 4 May 2026 19:14:38 +0300	[thread overview]
Message-ID: <616a62b0-680f-4a86-8ca9-75bba10684dd@h-partners.com> (raw)
In-Reply-To: <53FA8505-E7C0-404B-B7F5-3EBE89B64E0C@nvidia.com>

On 4/23/2026 10:32 PM, Zi Yan wrote:
> On 20 Apr 2026, at 4:01, Gorbunov Ivan wrote:
> 
>> This patch proposes 2 changes
>> 1) Deprecate invariant that the value stored in reference count of frozen page is 0
>> (Getter functions folio_ref_count/page_ref_count must still return 0 for frozen pages)
>> 2) Allow modification operations like page_ref_add to be used only with
>>     pages with owners
> 
> Should we also ban calling set_page_count() except init_page_count() and
> set_page_count_as_frozen()? So that no one can manipulate page refcount
> randomly. The same applies to folio_set_count().
> 
> All set_page_count(..., 0) are converted to set_page_count_as_frozen().
> All set_page_count(..., 1) are converted to init_page_count().
> 
> We might need an init_folio_count() for folio_set_count(..., 1) users
> and folio_set_count() has no other use.
> 
> I see set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); from
> mm/page_frag_cache.c and it could be converted to page_ref_unfreeze(),
> since above it there is free_frozen_page(), which makes me think the
> page is frozen.
> 

Overall I agree, but this is out of scope for this patch main change. 
This could be handled as an independent patch later.

>>   static inline int page_ref_dec_return(struct page *page)
>>   {
>>   	int ret = atomic_dec_return(&page->_refcount);
>> +	VM_BUG_ON(__page_count_is_frozen(ret + 1));
>>
>>   	if (page_ref_tracepoint_active(page_ref_mod_and_return))
>>   		__page_ref_mod_and_return(page, -1, ret);
> 
> VM_WARN_ON_ONCE() might be better?
> 

With "locking via dedicated bit" patch modification, any function which 
drop reference counter to zero changes the behavior of 
refcount(page_add_unless would succeed and etc.). This could lead to 
hazardous results. Therefore, we decided to prohibit that completely and 
make zero value only possible as middle state during dec_and_test.


> 
>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>> index 65e702fade61..27734cf795da 100644
>> --- a/mm/page_alloc.c
>> +++ b/mm/page_alloc.c
>> @@ -1639,14 +1639,14 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,
>>   		for (loop = 0; loop < nr_pages; loop++, p++) {
>>   			VM_WARN_ON_ONCE(PageReserved(p));
>>   			__ClearPageOffline(p);
>> -			set_page_count(p, 0);
>> +			set_page_count_as_frozen(p);
>>   		}
>>
>>   		adjust_managed_page_count(page, nr_pages);
>>   	} else {
>>   		for (loop = 0; loop < nr_pages; loop++, p++) {
>>   			__ClearPageReserved(p);
>> -			set_page_count(p, 0);
>> +			set_page_count_as_frozen(p);
>>   		}
>>
>>   		/* memblock adjusts totalram_pages() manually. */
>> -- 
> 
> Not sure about these two, they freeze p and p goes into buddy without
> unfreeze to refcount==0. But at the beginning, you said there are two
> states:
> 1) Unfrozen page which right now has no explicit owner
> 2) Frozen page
> 
> 1) means free pages in buddy. But the above change brings frozen pages
> into buddy, mixing the two states into one. Is that intended?


Since state 1 only appears during dec_and_test, pages in the buddy 
allocator are considered frozen. So there is no mixing of states.


  reply	other threads:[~2026-05-04 16:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-20  8:01 [PATCH v2 0/2] mm: improve folio refcount scalability Gorbunov Ivan
2026-04-20  8:01 ` [PATCH v2 1/2] mm: drop page refcount zero state semantics Gorbunov Ivan
2026-04-23 18:07   ` Bjorn Helgaas
2026-04-23 19:32   ` Zi Yan
2026-05-04 16:14     ` Gorbunov Ivan [this message]
2026-04-20  8:01 ` [PATCH v2 2/2] mm: implement page refcount locking via dedicated bit Gorbunov Ivan
2026-04-23 18:24   ` Matthew Wilcox
2026-04-23 18:31     ` Linus Torvalds
2026-04-23 19:20     ` David Hildenbrand (Arm)
2026-04-23 19:37   ` Zi Yan
2026-04-20 10:07 ` [syzbot ci] Re: mm: improve folio refcount scalability syzbot ci
2026-04-20 12:29   ` Gorbunov Ivan
2026-04-20 13:21     ` syzbot ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=616a62b0-680f-4a86-8ca9-75bba10684dd@h-partners.com \
    --to=gorbunov.ivan@h-partners.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=artem.kuzin@huawei.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=gladyshev.ilya1@h-partners.com \
    --cc=harry.yoo@oracle.com \
    --cc=kirill@shutemov.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=torvalds@linuxfoundation.org \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    --cc=yuzhao@google.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox