mm: improve folio refcount scalability
Subsystems (2)
cover mm: improve folio refcount scalability
This patch was previously posted as an RFC and received positive, but little, feedback. So I decided to fix remaining drawbacks and repost it as non-RFC patch. Overall logic, as well as performance, remained the same.
Intro
=====
This patch optimizes small file read performance and overall folio refcount scalability by refactoring page_ref_add_unless [core of folio_try_get]. This is alternative approach to previous attempts to fix small read performance by avoiding refcount bumps [1][2].
Overview
========
Current refcount implementation is using zero counter as locked (dead/frozen) state, which required CAS loop for increments to avoid temporary unlocks in try_get functions. These CAS loops became a serialization point for otherwise scalable and fast read side.
Proposed implementation separates "locked" logic from the counting, allowing the use of optimistic fetch_add() instead of CAS. For more details, please refer to the commit message of the patch itself.
Proposed logic maintains the same public API as before, including all existing memory barrier guarantees.
Performance
===========
Performance was measured using a simple custom benchmark based on will-it-scale[3]. This benchmark spawns N pinned threads/processes that execute the following loop:
``
char buf[]
fd = open(/* same file in tmpfs */);
while (true) {
pread(fd, buf, /* read size = */ 64, /* offset = */0)
}``
While this is a synthetic load, it does highlight existing issue and doesn't differ a lot from benchmarking in [2] patch.
This benchmark measures operations per second in the inner loop and the results across all workers. Performance was tested on top of v6.15 kernel[4] on two platforms. Since threads and processes showed similar performance on both systems, only the thread results are provided below. The performance improvement scales linearly between the CPU counts shown.
Platform 1: 2 x E5-2690 v3, 12C/12T each [disabled SMT]
#threads | vanilla | patched | boost (%)
1 | 1343381 | 1344401 | +0.1
2 | 2186160 | 2455837 | +12.3
5 | 5277092 | 6108030 | +15.7
10 | 5858123 | 7506328 | +28.1
12 | 6484445 | 8137706 | +25.5
/* Cross socket NUMA */
14 | 3145860 | 4247391 | +35.0
16 | 2350840 | 4262707 | +81.3
18 | 2378825 | 4121415 | +73.2
20 | 2438475 | 4683548 | +92.1
24 | 2325998 | 4529737 | +94.7Platform 2: 2 x AMD EPYC 9654, 96C/192T each [enabled SMT]
#threads | vanilla | patched | boost (%)
1 | 1077276 | 1081653 | +0.4
5 | 4286838 | 4682513 | +9.2
10 | 1698095 | 1902753 | +12.1
20 | 1662266 | 1921603 | +15.6
49 | 1486745 | 1828926 | +23.0
97 | 1617365 | 2052635 | +26.9
/* Cross socket NUMA */
105 | 1368319 | 1798862 | +31.5
136 | 1008071 | 1393055 | +38.2
168 | 879332 | 1245210 | +41.6
/* SMT */
193 | 905432 | 1294833 | +43.0
289 | 851988 | 1313110 | +54.1
353 | 771288 | 1347165 | +74.7[1] https://lore.kernel.org/linux-mm/CAHk-=wj00-nGmXEkxY=-=Z_qP6kiGUziSFvxHJ9N-cLWry5zpA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://github.com/antonblanchard/will-it-scale
[4] There were no changes to page_ref.h between v6.15 and v6.18 or any
significant performance changes on the read side in mm/filemap.c
---
Changes since RFC:
- Drop refactoring patch (sent separately)
- Replace single CAS with CAS loop in failure path to improve robustness
Based on quick re-evaluation, this didn't affect performance because only cold code was changed, so I kept RFC results.
Link to RFC: https://lore.foxido.dev/linux-mm/[email protected]
---
Gladyshev Ilya (1):
mm: implement page refcount locking via dedicated bit
include/linux/page-flags.h | 5 ++++- include/linux/page_ref.h | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/1 mm: implement page refcount locking via dedicated bit
The current atomic-based page refcount implementation treats zero counter as dead and requires a compare-and-swap loop in folio_try_get() to prevent incrementing a dead refcount. This CAS loop acts as a serialization point and can become a significant bottleneck during high-frequency file read operations.
This patch introduces FOLIO_LOCKED_BIT to distinguish between a (temporary) zero refcount and a locked (dead/frozen) state. Because now incrementing counter doesn't affect it's locked/unlocked state, it is possible to use an optimistic atomic_add_return() in page_ref_add_unless_zero() that operates independently of the locked bit. The locked state is handled after the increment attempt, eliminating the need for the CAS loop.
If locked state is detected after atomic_add(), pageref counter will be reset using CAS loop, eliminating theoretical possibility of overflow.
--- include/linux/page-flags.h | 5 ++++- include/linux/page_ref.h | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-)
include/linux/page-flags.h +4 -1
@@ -196,6 +196,9 @@ enum pageflags {#define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1) +/* Most significant bit in page refcount */ +#define PAGEREF_LOCKED_BIT (1 << 31) +#ifndef __GENERATING_BOUNDS_H#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
@@ -257,7 +260,7 @@ static __always_inline bool page_count_writable(const struct page *page)* The refcount check also prevents modification attempts to other (r/o) * tail pages that are not fake heads. */ -if (!atomic_read_acquire (&page->_refcount)) +if (atomic_read_acquire (&page->_refcount) &PAGEREF_LOCKED_BIT )return false;return page_fixed_fake_head (page) == page;
include/linux/page_ref.h +24 -4
@@ -64,7 +64,12 @@ static inline void __page_ref_unfreeze(struct page *page, int v)static inline int page_ref_count (const struct page *page) { -return atomic_read (&page->_refcount); +int val =atomic_read (&page->_refcount); + +if (unlikely (val &PAGEREF_LOCKED_BIT )) +return 0 ; + +return val; }/**
@@ -176,6 +181,9 @@ static inline int page_ref_sub_and_test(struct page *page, int nr) {int ret =atomic_sub_and_test (nr, &page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page, -nr, ret);return ret;
@@ -204,6 +212,9 @@ static inline int page_ref_dec_and_test(struct page *page) {int ret =atomic_dec_and_test (&page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page,-1 , ret);return ret;
@@ -228,14 +239,23 @@ static inline int folio_ref_dec_return(struct folio *folio)return page_ref_dec_return (&folio->page); } +#define _PAGEREF_LOCKED_LIMIT ((1 << 30) | PAGEREF_LOCKED_BIT) +static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT ); + }rcu_read_unlock ();if (page_ref_tracepoint_active (page_ref_mod_unless))
@@ -271,7 +291,7 @@ static inline bool folio_ref_try_add(struct folio *folio, int count)static inline int page_ref_freeze (struct page *page,int count) { -int ret =likely (atomic_cmpxchg (&page->_refcount, count,0 ) == count); +int ret =likely (atomic_cmpxchg (&page->_refcount, count,PAGEREF_LOCKED_BIT ) == count);if (page_ref_tracepoint_active (page_ref_freeze))__page_ref_freeze (page, count, ret);
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
This all made mu brain hurt a little :)
include/linux/page_ref.h static inline int page_ref_sub_and_test(struct page *page, int nr) · line 186
Quoted 9 lines
/**
include/linux/page_ref.h +3
-0@@ -176,6 +181,9 @@ static inline int page_ref_sub_and_test(struct page *page, int nr) {int ret =atomic_sub_and_test (nr, &page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +
It took me a while to figure out why this can't be just an atomic_or():
even though concurrent page_ref_add_unless_zero() would see a 0 after incrementing it to 1 to back off, there could be yet another concurrent page_ref_add_unless_zero() that would see the transition from 1 to 2 and continue.
What is the performance impact on doing the additional atomic_cmpxchg_relaxed() whenever we free a page, in particular, for anonymous memory where we mostly have just a single reference that we drop during munmap() etc?
Quoted 25 lines
include/linux/page_ref.h +6
-0if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page, -nr, ret);return ret;@@ -204,6 +212,9 @@ static inline int page_ref_dec_and_test(struct page *page) {int ret =atomic_dec_and_test (&page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page,-1 , ret);return ret;@@ -228,14 +239,23 @@ static inline int folio_ref_dec_return(struct folio *folio)return page_ref_dec_return (&folio->page); } +#define _PAGEREF_LOCKED_LIMIT ((1 << 30) | PAGEREF_LOCKED_BIT) +static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;rcu_read_lock ();
include/linux/page_ref.h static inline int folio_ref_dec_return(struct folio *folio) · line 257
Quoted 10 lines
include/linux/page_ref.h +7
-2/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT );
I assume we can't do an atomic_dec(), because we might have concurrent unfreezing (or similar things) happening that overwrote whatever was in there.
Is it really correct to replace _PAGEREF_LOCKED_LIMIT by PAGEREF_LOCKED_BIT, dropping some unrelated references? I assume the reasoning is that we treat any references with PAGEREF_LOCKED_BIT set as irrelevant and can get overwritten any time.
I was wondering is whether page_ref_freeze() could actually leave the references set, and only set the PAGEREF_LOCKED_BIT bit, whereby page_ref_unfreeze() would only clear the PAGEREF_LOCKED_BIT bit.
Similarly, the set_page_refcounted() could add a reference and clear the PAGEREF_LOCKED_BIT. That'd be more expensive on the allocation path ... and not sure if that would really help to turn this atomic_cmpxchg_relaxed() into an simpler atomic_dec() my brain could more easily understand :)
I think this patch needs a lot more documentation around what the PAGEREF_LOCKED_BIT means, and how this interacts with e.g., the set_page_count() in set_page_refcounted().
In general, I like this!
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
Quoted 24 lines
Quoted 24 lines
include/linux/page_ref.h +6
-0if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page, -nr, ret);return ret;@@ -204,6 +212,9 @@ static inline int page_ref_dec_and_test(struct page *page) {int ret =atomic_dec_and_test (&page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page,-1 , ret);return ret;@@ -228,14 +239,23 @@ static inline int folio_ref_dec_return(struct folio *folio)return page_ref_dec_return (&folio->page); } +#define _PAGEREF_LOCKED_LIMIT ((1 << 30) | PAGEREF_LOCKED_BIT) +static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;
Quoted 11 lines
include/linux/page_ref.h static inline int folio_ref_dec_return(struct folio *folio) · line 257
Quoted 11 lines
include/linux/page_ref.h +7
-2rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT );
It's still early here, but I think there is a problem.
Please bear with me :)
val = atomic_add_return(nr, &page->_refcount); ret = !(val & PAGEREF_LOCKED_BIT);
Implies that can grab a reference whenever the locked-bit is not set.
Including when the refcount is 0.
Now, that works fine when racing with concurrent freeing, where we are just able to decrement the refcount, but yet have to set the PAGEREF_LOCKED_BIT bit.
But, what about any pages that don't have the PAGEREF_LOCKED_BIT set, but have the refcount at 0 permanently?
That's, for example, the case for any pages where we do an explicit set_page_count(page, 0);
For example, all pages we add to the page allocator through __free_pages_core().
That means, that someone could easily grab a reference to such pages, including tail pages of allocated compound pages where the refcount is still 0 -- or pages allocated with a frozen refcount where we don't ever do the set_page_refcount(1) in the buddy.
Bad things will happen when that wrongly page_ref_add_unless_zero() obtained reference is dropped again to free that page.
You'd have to make sure that there is no way we can achieve refcount == 0 without going through page_ref_dec_and_test(), when actually freeing a page.
One piece of the puzzle is handling set_page_count(p, 0) I think. But I suspect that there might be other places where we don't even have the set_page_count().
See vmemmap_get_tail() in
https://lore.kernel.org/r/[email protected] for example, where we know the refcount is 0, because we allocated the page holding memmap with __GFP_ZERO.
For example, I think you'd have to make sure that *any* pages in the buddy have their refcount set to PAGEREF_LOCKED_BIT, not 0.
So unless I am missing soemthing, this is broken an requires a lot of care to make sure that refcount==0 is handled everywhere accordingly.
Signature · 3 lines
Cheers, David
Re: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
This is combined reply to both of your emails, hope you don't mind
Quoted 19 lines
This all made mu brain hurt a little 🙂
include/linux/page_ref.h static inline int page_ref_sub_and_test(struct page *page, int nr) · line 186
Quoted 9 lines
/**
include/linux/page_ref.h +3
-0@@ -176,6 +181,9 @@ static inline int page_ref_sub_and_test(struct page *page, int nr) {int ret =atomic_sub_and_test (nr, &page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +It took me a while to figure out why this can't be just an
atomic_or():
even though concurrentpage_ref_add_unless_zero()would see a 0 after incrementing it to 1 to back off, there could be yet another concurrentpage_ref_add_unless_zero()that would see the transition from 1 to 2 and continue.
Quoted 4 lines
What is the performance impact on doing the additional
atomic_cmpxchg_relaxed()whenever we free a page, in particular, for anonymous memory where we mostly have just a single reference that we drop duringmunmap()etc?
I'll try to measure some numbers. Theoretically speaking, in low-contention scenario you will exclusively own cacheline after atomic_sub, so relaxed CAS should be cheap.
Quoted 24 lines
Quoted 24 lines
include/linux/page_ref.h +6
-0if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page, -nr, ret);return ret;@@ -204,6 +212,9 @@ static inline int page_ref_dec_and_test(struct page *page) {int ret =atomic_dec_and_test (&page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page,-1 , ret);return ret;@@ -228,14 +239,23 @@ static inline int folio_ref_dec_return(struct folio *folio)return page_ref_dec_return (&folio->page); } +#define _PAGEREF_LOCKED_LIMIT ((1 << 30) | PAGEREF_LOCKED_BIT) +static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;
Quoted 15 lines
include/linux/page_ref.h static inline int folio_ref_dec_return(struct folio *folio) · line 257
Quoted 11 lines
include/linux/page_ref.h +7
-2rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT );I assume we can't do an
atomic_dec(), because we might have concurrent unfreezing (or similar things) happening that overwrote whatever was in there.
Not only that, but also you probably don't want to handle "atomic_dec() returned 0" situations.
Quoted 4 lines
Is it really correct to replace
_PAGEREF_LOCKED_LIMITbyPAGEREF_LOCKED_BIT, dropping some unrelated references? I assume the reasoning is that we treat any references withPAGEREF_LOCKED_BITset as irrelevant and can get overwritten any time.
Locked refcount doesn't contain any "references" except failed optimistic increments, so you are right that they don't hold any semantic meaning. The only reason to clear them is to prevent overflow, so we are doing it only if it is absolutely required.
Quoted 10 lines
I was wondering is whether
page_ref_freeze()could actually leave the references set, and only set thePAGEREF_LOCKED_BITbit, wherebypage_ref_unfreeze()would only clear thePAGEREF_LOCKED_BITbit.Similarly, the
set_page_refcounted()could add a reference and clear thePAGEREF_LOCKED_BIT. That'd be more expensive on the allocation path ... and not sure if that would really help to turn thisatomic_cmpxchg_relaxed()into an simpleratomic_dec()my brain could more easily understand 🙂
I believe resetting whole counter via single CAS (CPU-wise) is cheaper than atomic_dec() for each individual attempt.
Quoted 4 lines
I think this patch needs a lot more documentation around what the
PAGEREF_LOCKED_BITmeans, and how this interacts with e.g., theset_page_count()inset_page_refcounted().
Yep, I'll fix this
Quoted 33 lines
In general, I like this!
Quoted 6 lines
include/linux/page_ref.h +1
-0static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;rcu_read_lock(); /* avoid writing to the vmemmap area being remapped */include/linux/page_ref.h static inline int folio_ref_dec_return(struct folio *folio) · line 257
Quoted 9 lines
include/linux/page_ref.h +7
-2-if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT );It's still early here, but I think there is a problem.
Please bear with me 🙂
val = atomic_add_return(nr, &page->_refcount); ret = !(val & PAGEREF_LOCKED_BIT);Implies that can grab a reference whenever the locked-bit is not set.
Including when the refcount is 0.
Now, that works fine when racing with concurrent freeing, where we are just able to decrement the refcount, but yet have to set the
PAGEREF_LOCKED_BITbit.
Quoted 8 lines
But, what about any pages that don't have the
PAGEREF_LOCKED_BITset, but have the refcount at 0 permanently?That's, for example, the case for any pages where we do an explicit
set_page_count(page, 0);For example, all pages we add to the page allocator through
__free_pages_core().
You are right that refcount = 0 is tricky. However, for a bad outcome you will need:
1. Some external reference to this page, through which you try to increment the refcount;
2. set_page_count(0) somewhere between freeing and "it is safe to alloc" state.
So adding new pages with zeroed refcount to allocator is safe, because there are no external references. Zeroing tail page's refcount is safe, unless someone actually tries to increment its refcount (and this is bug).
Generally, the only unsafe set_page_count() (or any other zeroing) will be in allocator itself between freeing and allocating. Or maybe I missed something, and this approach is indeed incorrect
Probably we can think of some debug checks to prevent bugs in "safe" scenarious
Quoted 22 lines
That means, that someone could easily grab a reference to such pages, including tail pages of allocated compound pages where the refcount is still 0 -- or pages allocated with a frozen refcount where we don't ever do the
set_page_refcount(1)in the buddy.Bad things will happen when that wrongly
page_ref_add_unless_zero()obtained reference is dropped again to free that page.You'd have to make sure that there is no way we can achieve refcount == 0 without going through
page_ref_dec_and_test(), when actually freeing a page.One piece of the puzzle is handling
set_page_count(p, 0)I think. But I suspect that there might be other places where we don't even have theset_page_count().See
vmemmap_get_tail()in
https://lore.kernel.org/r/[email protected] for example, where we know the refcount is 0, because we allocated the page holding memmap with__GFP_ZERO.
Quoted 6 lines
For example, I think you'd have to make sure that *any* pages in the buddy have their refcount set to
PAGEREF_LOCKED_BIT, not 0.So unless I am missing soemthing, this is broken an requires a lot of care to make sure that refcount==0 is handled everywhere accordingly.
---
Ilya
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
rcu_read_lock();
/* avoid writing to the vmemmap area being remapped */ - if (page_count_writable(page))
- ret =atomic_add_unless(&page->_refcount, nr, 0); + if (page_count_writable(page)) {
+ val =atomic_add_return(nr, &page->_refcount);
+ ret = !(val &PAGEREF_LOCKED_BIT);
+
+ /* Undoatomic_add()if counter is locked and scary big */ + while (unlikely((unsigned int)val >= _PAGEREF_LOCKED_LIMIT)) + val = atomic_cmpxchg_relaxed(&page->_refcount, val,
Quoted 8 lines
PAGEREF_LOCKED_BIT);Quoted 4 lines
I assume we can't do an
atomic_dec(), because we might have concurrent unfreezing (or similar things) happening that overwrote whatever was in there.Not only that, but also you probably don't want to handle "
atomic_dec()returned 0" situations.
Right, and then we'd have to free the page here instead, which is rather awkward.
Quoted 5 lines
Quoted 5 lines
I was wondering is whether
page_ref_freeze()could actually leave the references set, and only set thePAGEREF_LOCKED_BITbit, wherebypage_ref_unfreeze()would only clear thePAGEREF_LOCKED_BITbit.
Quoted 9 lines
Quoted 5 lines
Similarly, the
set_page_refcounted()could add a reference and clear thePAGEREF_LOCKED_BIT. That'd be more expensive on the allocation path ... and not sure if that would really help to turn thisatomic_cmpxchg_relaxed()into an simpleratomic_dec()my brain could more easily understand 🙂I believe resetting whole counter via single CAS (CPU-wise) is cheaper than
atomic_dec()for each individual attempt.
Right, that's what I meant with "more expensive".
[...]
Quoted 10 lines
Quoted 3 lines
For example, all pages we add to the page allocator through
__free_pages_core().You are right that refcount = 0 is tricky. However, for a bad outcome you will need:
1. Some external reference to this page, through which you try to increment the refcount;
2.set_page_count(0)somewhere between freeing and "it is safe to alloc" state.
That is way, way, way too dodgy.
What you should likely do is
(a) Make set_page_count(0) set it to PAGEREF_LOCKED_BIT
(b) Make any places that might skip set_page_count(0) to use it
(c) Document this all extremely thoroughly.
Alternatively, disallow set_page_count(0) ccompletely and add something like "initialize_page_as_frozen()" or sth. like that.
A refcount of 0 must really only be temporarily allowed in the system when transitioning from 0 to PAGEREF_LOCKED_BIT. Otherwise this just screams for trouble.
Quoted 11 lines
So adding new pages with zeroed refcount to allocator is safe, because there are no external references. Zeroing tail page's refcount is safe, unless someone actually tries to increment its refcount (and this is bug).
Generally, the only unsafe
set_page_count()(or any other zeroing) will be in allocator itself between freeing and allocating. Or maybe I missed something, and this approach is indeed incorrectProbably we can think of some debug checks to prevent bugs in "safe" scenarious
Just take a look at do_migrate_range(), where we do a
page = pfn_to_page(pfn);
folio = page_folio(page);
if (!folio_try_get(folio))
continue;
If that is a page that was added to the buddy (set_page_count(0)) but either (a) not allocated yet or (b) allocated as frozen that's a problem.
We really rely on
(1) Frozen pages
(2) Buddy pages
(3) Tail pages (compound or non-compoud)
To have a refcount of 0 that *reliably* makes folio_try_get() etc. fail.
Anything else is just playing with fire.
Signature · 3 lines
Cheers, David
Re: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines
For example, all pages we add to the page allocator through
__free_pages_core().You are right that refcount = 0 is tricky. However, for a bad outcome you will need:
1. Some external reference to this page, through which you try to increment the refcount;
2.set_page_count(0)somewhere between freeing and "it is safe to alloc" state.
Quoted 7 lines
That is way, way, way too dodgy.
What you should likely do is
(a) Make
set_page_count(0)set it toPAGEREF_LOCKED_BIT
(b) Make any places that might skipset_page_count(0)to use it
(c) Document this all extremely thoroughly.
Okay, I agree. My reasoning was more about "it's not _that_ bad if we miss something outside the allocator" than "everything is fine as it is", but maybe it is really that bad...
Quoted 2 lines
Alternatively, disallow
set_page_count(0)ccompletely and add something like "initialize_page_as_frozen()" or sth. like that.
(Below are my thoughts, not active proposal)
Actually, we can keep "zero is locked" property if we change scheme a little:
If we invert locked bit so that 0 means locked, then we don't care about memset() / GFP_ZERO / set_page(0). But then there is extra bit on each active refcount, and (un)masking will be required on each refcount read/set/etc operation.
We can place locked bit in the highest bit like now, or move it to the lowest. This doesn't make any difference except for how masking is performed (and or shift). I can't really tell which approach is faster (and more optimizable after inline).
This approach doesn't look appealing to me, mostly because of redundant masking. So for now I will try to add proper initialization / documentation in all risky places.
Quoted 32 lines
Quoted 10 lines
So adding new pages with zeroed refcount to allocator is safe, because there are no external references. Zeroing tail page's refcount is safe, unless someone actually tries to increment its refcount (and this is bug).
Generally, the only unsafe
set_page_count()(or any other zeroing) will be in allocator itself between freeing and allocating. Or maybe I missed something, and this approach is indeed incorrectProbably we can think of some debug checks to prevent bugs in "safe" scenarious
Just take a look at
do_migrate_range(), where we do apage =
pfn_to_page(pfn);
folio =page_folio(page);if (!folio_try_get(folio))
continue;If that is a page that was added to the buddy (
set_page_count(0)) but either (a) not allocated yet or (b) allocated as frozen that's a problem.We really rely on
(1) Frozen pages
(2) Buddy pages
(3) Tail pages (compound or non-compoud)To have a refcount of 0 that *reliably* makes
folio_try_get()etc. fail.Anything else is just playing with fire.
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/1] mm: implement page refcount locking via dedicated bit
Quoted 32 lines · Gladyshev Ilya
Quoted 8 lines
Quoted 7 lines
You are right that refcount = 0 is tricky. However, for a bad outcome you will need:
1. Some external reference to this page, through which you try to increment the refcount;
2.set_page_count(0)somewhere between freeing and "it is safe to alloc" state.Quoted 7 lines
That is way, way, way too dodgy.
What you should likely do is
(a) Make
set_page_count(0)set it toPAGEREF_LOCKED_BIT
(b) Make any places that might skipset_page_count(0)to use it
(c) Document this all extremely thoroughly.Okay, I agree. My reasoning was more about "it's not _that_ bad if we miss something outside the allocator" than "everything is fine as it is", but maybe it is really that bad...
Quoted 2 lines
Alternatively, disallow
set_page_count(0)ccompletely and add something like "initialize_page_as_frozen()" or sth. like that.(Below are my thoughts, not active proposal)
Actually, we can keep "zero is locked" property if we change scheme a little:
If we invert locked bit so that 0 means locked, then we don't care about
memset()/GFP_ZERO/set_page(0). But then there is extra bit on each active refcount, and (un)masking will be required on each refcount read/ set/etc operation.
Quoted 8 lines
We can place locked bit in the highest bit like now, or move it to the lowest. This doesn't make any difference except for how masking is performed (and or shift). I can't really tell which approach is faster (and more optimizable after inline).
This approach doesn't look appealing to me, mostly because of redundant masking. So for now I will try to add proper initialization / documentation in all risky places.
I agree.
refcount == 0 will only be allowed temporarily while transitioning to the frozen state.
In fact, you should then rename PAGEREF_LOCKED_BIT to PAGEREF_FROZEN_BIT and page_ref_add_unless_zero() to page_ref_add_unless_frozen.
That then matches the terminology we use for inactive refcounts (page_ref_freeze(), alloc_frozen_pages()).
And it will be a lot easier to comprehend compared to using a different "locking" terminology when traditionally the "page lock" is something completely different.
Signature · 3 lines
Cheers, David
Andrew Morton
maintainerQuoted 35 lines · On Thu, 26 Feb 2026 16:27:22 +0000 Gladyshev Ilya…
This patch was previously posted as an RFC and received positive, but little, feedback. So I decided to fix remaining drawbacks and repost it as non-RFC patch. Overall logic, as well as performance, remained the same.
Intro
=====
This patch optimizes small file read performance and overall folio refcount scalability by refactoringpage_ref_add_unless[core offolio_try_get]. This is alternative approach to previous attempts to fix small read performance by avoiding refcount bumps [1][2].Overview
========
Current refcount implementation is using zero counter as locked (dead/frozen) state, which required CAS loop for increments to avoid temporary unlocks intry_getfunctions. These CAS loops became a serialization point for otherwise scalable and fast read side.Proposed implementation separates "locked" logic from the counting, allowing the use of optimistic
fetch_add()instead of CAS. For more details, please refer to the commit message of the patch itself.Proposed logic maintains the same public API as before, including all existing memory barrier guarantees.
Performance
===========
Performance was measured using a simple custom benchmark based on will-it-scale[3]. This benchmark spawns N pinned threads/processes that execute the following loop:
``
char buf[]
fd =open(/* same file in tmpfs */);
Quoted 6 lines
while (true) { pread(fd, buf, /* read size = */ 64, /* offset = */0) }``
While this is a synthetic load, it does highlight existing issue and doesn't differ a lot from benchmarking in [2] patch.
Well it's nice to see the performance benefits from Kiryl's ill-fated patch
(https://lore.kernel.org/linux-mm/[email protected]/)
And this approach looks far simpler.
I'll paste the single patch below for others - I think it's not desirable to prepare a [0/N] for a single-patch "series"!
Thanks, I'll await reviewer feedback for a couple of days then I'll look at adding this to linux-next for some runtime testing.
Quoted 171 lines
This benchmark measures operations per second in the inner loop and the results across all workers. Performance was tested on top of v6.15 kernel[4] on two platforms. Since threads and processes showed similar performance on both systems, only the thread results are provided below. The performance improvement scales linearly between the CPU counts shown.
Platform 1: 2 x E5-2690 v3, 12C/12T each [disabled SMT]
#threads | vanilla | patched | boost (%) 1 | 1343381 | 1344401 | +0.1 2 | 2186160 | 2455837 | +12.3 5 | 5277092 | 6108030 | +15.7 10 | 5858123 | 7506328 | +28.1 12 | 6484445 | 8137706 | +25.5 /* Cross socket NUMA */ 14 | 3145860 | 4247391 | +35.0 16 | 2350840 | 4262707 | +81.3 18 | 2378825 | 4121415 | +73.2 20 | 2438475 | 4683548 | +92.1 24 | 2325998 | 4529737 | +94.7Platform 2: 2 x AMD EPYC 9654, 96C/192T each [enabled SMT]
#threads | vanilla | patched | boost (%) 1 | 1077276 | 1081653 | +0.4 5 | 4286838 | 4682513 | +9.2 10 | 1698095 | 1902753 | +12.1 20 | 1662266 | 1921603 | +15.6 49 | 1486745 | 1828926 | +23.0 97 | 1617365 | 2052635 | +26.9 /* Cross socket NUMA */ 105 | 1368319 | 1798862 | +31.5 136 | 1008071 | 1393055 | +38.2 168 | 879332 | 1245210 | +41.6 /* SMT */ 193 | 905432 | 1294833 | +43.0 289 | 851988 | 1313110 | +54.1 353 | 771288 | 1347165 | +74.7[1] https://lore.kernel.org/linux-mm/CAHk-=wj00-nGmXEkxY=-=Z_qP6kiGUziSFvxHJ9N-cLWry5zpA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://github.com/antonblanchard/will-it-scale
[4] There were no changes topage_ref.hbetween v6.15 and v6.18 or any
significant performance changes on the read side inmm/filemap.cThe current atomic-based page refcount implementation treats zero counter as dead and requires a compare-and-swap loop in
folio_try_get()to prevent incrementing a dead refcount. This CAS loop acts as a serialization point and can become a significant bottleneck during high-frequency file read operations.This patch introduces
FOLIO_LOCKED_BITto distinguish between a (temporary) zero refcount and a locked (dead/frozen) state. Because now incrementing counter doesn't affect it's locked/unlocked state, it is possible to use an optimisticatomic_add_return()inpage_ref_add_unless_zero()that operates independently of the locked bit. The locked state is handled after the increment attempt, eliminating the need for the CAS loop.If locked state is detected after
atomic_add(), pageref counter will be reset using CAS loop, eliminating theoretical possibility of overflow.Co-developed-by Gorbunov Ivan <[email protected]>Signed-off-by Gorbunov Ivan <[email protected]>Signed-off-by Gladyshev Ilya <[email protected]>--- include/linux/page-flags.h | 5 ++++- include/linux/page_ref.h | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-)include/linux/page-flags.h +4
-1@@ -196,6 +196,9 @@ enum pageflags {#define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1) +/* Most significant bit in page refcount */ +#define PAGEREF_LOCKED_BIT (1 << 31) +#ifndef __GENERATING_BOUNDS_H#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP @@ -257,7 +260,7 @@ static __always_inline bool page_count_writable(const struct page *page)* The refcount check also prevents modification attempts to other (r/o) * tail pages that are not fake heads. */ -if (!atomic_read_acquire (&page->_refcount)) +if (atomic_read_acquire (&page->_refcount) &PAGEREF_LOCKED_BIT )return false;return page_fixed_fake_head (page) == page;include/linux/page_ref.h +24
-4@@ -64,7 +64,12 @@ static inline void __page_ref_unfreeze(struct page *page, int v)static inline int page_ref_count (const struct page *page) { -return atomic_read (&page->_refcount); +int val =atomic_read (&page->_refcount); + +if (unlikely (val &PAGEREF_LOCKED_BIT )) +return 0 ; + +return val; }/** @@ -176,6 +181,9 @@ static inline int page_ref_sub_and_test(struct page *page, int nr) {int ret =atomic_sub_and_test (nr, &page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page, -nr, ret);return ret;@@ -204,6 +212,9 @@ static inline int page_ref_dec_and_test(struct page *page) {int ret =atomic_dec_and_test (&page->_refcount); +if (ret) + ret = !atomic_cmpxchg_relaxed (&page->_refcount,0 ,PAGEREF_LOCKED_BIT ); +if (page_ref_tracepoint_active (page_ref_mod_and_test))__page_ref_mod_and_test (page,-1 , ret);return ret;@@ -228,14 +239,23 @@ static inline int folio_ref_dec_return(struct folio *folio)return page_ref_dec_return (&folio->page); } +#define _PAGEREF_LOCKED_LIMIT ((1 << 30) | PAGEREF_LOCKED_BIT) +static inline bool page_ref_add_unless_zero (struct page *page,int nr) {bool ret = false; +int val;rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page)) - ret =atomic_add_unless (&page->_refcount, nr,0 ); +if (page_count_writable (page)) { + val =atomic_add_return (nr, &page->_refcount); + ret = !(val &PAGEREF_LOCKED_BIT ); + +/* Undo atomic_add() if counter is locked and scary big */ +while (unlikely ((unsigned int )val >= _PAGEREF_LOCKED_LIMIT)) + val =atomic_cmpxchg_relaxed (&page->_refcount, val,PAGEREF_LOCKED_BIT ); + }rcu_read_unlock ();if (page_ref_tracepoint_active (page_ref_mod_unless))@@ -271,7 +291,7 @@ static inline bool folio_ref_try_add(struct folio *folio, int count)static inline int page_ref_freeze (struct page *page,int count) { -int ret =likely (atomic_cmpxchg (&page->_refcount, count,0 ) == count); +int ret =likely (atomic_cmpxchg (&page->_refcount, count,PAGEREF_LOCKED_BIT ) == count);if (page_ref_tracepoint_active (page_ref_freeze))__page_ref_freeze (page, count, ret);
Quoted 6 lines · Andrew Morton
Well it's nice to see the performance benefits from Kiryl's ill-fated patch
(https://lore.kernel.org/linux-mm/[email protected]/)And this approach looks far simpler.
This attempt does something completely different, in that it doesn't actually remove any atomics at all.
Quite the opposite, in fact. It adds *new* atomics - just in a different place.
But if it helps performance, that is certainly still interesting.
It's basically saying that it's not the atomic op itself that is so expensive, it's literally just the "read + cmpxchg" in atomic_add_unless() that makes for most of the expense.
And that, in turn, is probably due the fact that the read in that loop first tries to make the cacheline shared, and then the cmpxchg has to turn the shared cacheline exclusive, so you have two cache ops - and possibly then many more because of bouncing due to this all.
Fine, I can believe that.
But if it's purely about the cacheline shared/exclusive behavior, I think there's a much simpler patch
That much more simple patch is something we've done before: do *not* read the old value before the cmpxchg loop. Do the cmpxchg with a default value, and if we guessed wrong, just do the extra loop iteration.
This attached patch is ENTIRELY UNTESTED. I might have gotten something wrong. A quick look at the assembler seems to say it generates that expected code (gcc is not great at this), with the loop being
mov $0x1,%eax
lea 0x34(%rdi),%rdx
lea 0x1(%rax),%ecx lock cmpxchg %ecx,(%rdx)
...
ie the first time through we just assume the count is one.
And yes, that assumption may be wrong, but at least we don't go through the shared state, and since we got the cacheline for exclusive the first time around the loop, the second time around we will get it right.
What do the numbers look with this much simpler patch? (All assuming I didn't screw some logic up and get some conditional the wrong way around - please check me).
Linus
include/linux/page_ref.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
include/linux/page_ref.h +12 -2
@@ -234,8 +234,18 @@ static inline bool page_ref_add_unless(struct page *page, int nr, int u)rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page, u)) - ret =atomic_add_unless (&page->_refcount, nr, u); +if (page_count_writable (page, u)) { +/* Assume count == 1, don't read it! */ +int old =1 ; +for (;;) { +if (atomic_try_cmpxchg (&page->_refcount, &old, old+1 )) { + ret = true; +break ; + } +if (unlikely (!old)) +break ; + } + }rcu_read_unlock ();if (page_ref_tracepoint_active (page_ref_mod_unless))
Quoted 2 lines · Linus Torvalds
This attached patch is ENTIRELY UNTESTED.
Here's a slightly cleaned up and further simplified version, which is also actually tested, although only in the "it boots for me" sense.
It generates good code at least with clang:
.LBB76_7:
movl $1, %eax
.LBB76_8:
leal 1(%rax), %ecx
lock cmpxchgl %ecx, 52(%rdi)
sete %cl
je .LBB76_10
testl %eax, %eax
jne .LBB76_8.LBB76_10:
which actually looks both simple and fairly optimal for that sequence.
Of course, since this is very much about cacheline access patterns, actual performance will depend on random microarchitectural issues (and not just the CPU core, but the whole memory subsystem).
Can somebody with a good - and relevant - benchmark system try this out?
Linus
include/linux/page_ref.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
include/linux/page_ref.h +9 -2
@@ -234,8 +234,15 @@ static inline bool page_ref_add_unless(struct page *page, int nr, int u)rcu_read_lock ();/* avoid writing to the vmemmap area being remapped */ -if (page_count_writable (page, u)) - ret =atomic_add_unless (&page->_refcount, nr, u); +if (page_count_writable (page, u)) { +/* Assume count == 1, don't read it! */ +int old =1 ; +do { + ret =atomic_try_cmpxchg (&page->_refcount, &old, old+1 ); +if (likely (ret)) +break ; + }while (old); + }rcu_read_unlock ();if (page_ref_tracepoint_active (page_ref_mod_unless))
Quoted 23 lines · Linus Torvalds
Quoted 2 lines · Linus Torvalds
This attached patch is ENTIRELY UNTESTED.
Here's a slightly cleaned up and further simplified version, which is also actually tested, although only in the "it boots for me" sense.
It generates good code at least with clang:
.LBB76_7:
movl $1, %eax
.LBB76_8:leal 1(%rax), %ecx lock cmpxchgl %ecx, 52(%rdi) sete %cl je .LBB76_10 testl %eax, %eax jne .LBB76_8.LBB76_10:
which actually looks both simple and fairly optimal for that sequence.
Quoted 7 lines
Of course, since this is very much about cacheline access patterns, actual performance will depend on random microarchitectural issues (and not just the CPU core, but the whole memory subsystem).
Can somebody with a good - and relevant - benchmark system try this out?
Linus
Here are some perhaps interesting numbers from an extremely synthetic benchmark[1] I wrote just now:
note: xadd_bench is lock addl, cmpxchg is the typical load + lock cmpxchg loop, and optimistic_cmpxchg_benchmark is similar to what you wrote, where we assume 1 and only later do we do the actual loop. I also don't claim this is representative of page cache performance, but this is quite a lot simpler to set up and play around with.
On my zen4 AMD Ryzen 7 PRO 7840U laptop:
------------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------------
xadd_bench/threads:1 2.76 ns 2.76 ns 250435782 xadd_bench/threads:4 42.1 ns 42.1 ns 15969296 xadd_bench/threads:8 84.8 ns 84.8 ns 8920800 xadd_bench/threads:16 226 ns 211 ns 2446928 cmpxchg_bench/threads:1 3.12 ns 3.12 ns 220339301 cmpxchg_bench/threads:4 51.1 ns 51.1 ns 12372808 cmpxchg_bench/threads:8 112 ns 112 ns 6228056 cmpxchg_bench/threads:16 679 ns 648 ns 930832 optimistic_cmpxchg_bench/threads:1 2.95 ns 2.95 ns 233704391 optimistic_cmpxchg_bench/threads:4 56.2 ns 56.2 ns 11780588 optimistic_cmpxchg_bench/threads:8 140 ns 140 ns 4606440 optimistic_cmpxchg_bench/threads:16 789 ns 746 ns 806400
Here we can see that the optimistic cmpxchg still can't match the xadd/lock addl performance in single-thread, and degrades quickly and worse than straight up cmpxchg under load (perhaps presumably because of the cmpxchg miss).
On our internal large 160-core Intel(R) Xeon(R) CPU E7-8891 v4 (older uarch, sad) machine:
------------------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------------------
xadd_bench/threads:1 13.6 ns 13.6 ns 51445934 xadd_bench/threads:4 41.4 ns 166 ns 4211940 xadd_bench/threads:8 30.3 ns 242 ns 2190488 xadd_bench/threads:16 37.3 ns 596 ns 1162336 xadd_bench/threads:64 24.9 ns 1376 ns 640000 xadd_bench/threads:128 27.3 ns 3108 ns 1054592 cmpxchg_bench/threads:1 17.9 ns 17.9 ns 38992029 cmpxchg_bench/threads:4 54.8 ns 219 ns 3431076 cmpxchg_bench/threads:8 39.0 ns 312 ns 1698712 cmpxchg_bench/threads:16 62.2 ns 994 ns 530672 cmpxchg_bench/threads:64 28.5 ns 1479 ns 665280 cmpxchg_bench/threads:128 17.2 ns 1838 ns 517376 optimistic_cmpxchg_bench/threads:1 13.6 ns 13.6 ns 51384286 optimistic_cmpxchg_bench/threads:4 70.2 ns 281 ns 2585092 optimistic_cmpxchg_bench/threads:8 58.1 ns 465 ns 1598592 optimistic_cmpxchg_bench/threads:16 106 ns 1694 ns 420832 optimistic_cmpxchg_bench/threads:64 30.8 ns 1767 ns 499264 optimistic_cmpxchg_bench/threads:128 39.3 ns 4632 ns 447104
Here, optimistic seems to match xadd in single-threaded, but then very quickly degrades. In general optimistic_cmpxchg seems to degrade worse than cmpxchg, but there is a lot of variance here (and other users lightly using it) so results (particularly those with higher thread counts) should be taken with a grain of salt (for example, lock add scaling dratistically worse than cmpxchg seems to be a fluke).
TL;DR I don't think the idea quite works, particularly when a folio is under contention, because if you have traffic on a cacheline then you certainly have a couple of threads trying to grab a refcount. And doing two cmpxchgs just increases traffic and pessimises things. Also perhaps worth noting that neither solution scales in any way.
[1] https://gist.github.com/heatd/2a6e6c778c3cfd4aa6804b2d598c7a4c (excuse my C++)
Signature · 1 line
Pedro
Quoted 4 lines · Pedro Falcato
Here we can see that the optimistic cmpxchg still can't match the xadd/lock addl performance in single-thread, and degrades quickly and worse than straight up cmpxchg under load (perhaps presumably because of the cmpxchg miss).
Ok, thanks for doing the numbers. I'm (obviously) a bit surprised at how badly cmpxchg does - it used to be noticeably worse than "lock add" even for the non-contention case, but I thought that had long since been fixed.
Clearly that's just not the case - and I had just been overly optimistic that the "first cmpxchg failed, but second one gets the value without losing the cacheline in between" would work reliably.
Ho humm. Maybe that "locked" flag is the best we can do.
Linus
Quoted 2 lines · Linus Torvalds
Ho humm. Maybe that "locked" flag is the best we can do.
Coming back to this after having spent a few days mulling it all.
End result: I do think that the locked bit is the right solution, and you can add my "Acked-by" to the series.
I do have a couple of requests (the ack is not conditional on these, but having looked at the patch it's what my reactions were):
(a) please rename "page_ref_add_unless_zero()" and friends, since
that's not what they do any more. Now it is "page_ref_add_unless_locked()"
(b) verify and add a comment somewhere that the PAGEREF_LOCKED_BIT on
a free'd folio is never removed until the page ref is set to 1 by the next allocation by set_pages_refcounted()
I'm asking for that (b) because if it is ever set back to zero, this all fails, because the fixup for the atomic_add_return() might do a atomic_dec() - if it never saw the PAGEREF_LOCKED_BIT - and free a page twice after all.
(And if I missed some reason why (b) isn't true, please holler).
Anyway, I like it. My initial reaction was that it was complicated, but in the end I think that was just because it was done a bit differently from what I was used to in how we did the DEAD flag for dentries, but I think this is actually the better model (*).
Linus
(*) For dentries we use that lockref thing which has its own complications with the spinlock acting as the lock bit for the refcount, and it uses the cmpxchg loop to synchronize things because it wants that lock to be absolute. But I think the page count approach is actually better in being more flexible.