diff mbox series

[v3,3/3] mm: page_counters: initialize usage using ATOMIC_LONG_INIT() macro

Message ID 20240726203110.1577216-4-roman.gushchin@linux.dev (mailing list archive)
State New
Headers show
Series mm: memcg: page counters optimizations | expand

Commit Message

Roman Gushchin July 26, 2024, 8:31 p.m. UTC
When a page_counter structure is initialized, there is no need to
use an atomic set operation to initialize the usage counter because
at this point the structure is not visible to anybody else.
ATOMIC_LONG_INIT() is what should be used in such cases.

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
---
 include/linux/page_counter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Shakeel Butt July 26, 2024, 9:37 p.m. UTC | #1
On Fri, Jul 26, 2024 at 08:31:10PM GMT, Roman Gushchin wrote:
> When a page_counter structure is initialized, there is no need to
> use an atomic set operation to initialize the usage counter because
> at this point the structure is not visible to anybody else.
> ATOMIC_LONG_INIT() is what should be used in such cases.
> 
> Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>

Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Johannes Weiner July 26, 2024, 11:11 p.m. UTC | #2
On Fri, Jul 26, 2024 at 08:31:10PM +0000, Roman Gushchin wrote:
> When a page_counter structure is initialized, there is no need to
> use an atomic set operation to initialize the usage counter because
> at this point the structure is not visible to anybody else.
> ATOMIC_LONG_INIT() is what should be used in such cases.
> 
> Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
> ---
>  include/linux/page_counter.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
> index cf837d0f8ed1..5da11392b382 100644
> --- a/include/linux/page_counter.h
> +++ b/include/linux/page_counter.h
> @@ -53,7 +53,7 @@ static inline void page_counter_init(struct page_counter *counter,
>  				     struct page_counter *parent,
>  				     bool protection_support)
>  {
> -	atomic_long_set(&counter->usage, 0);
> +	counter->usage = (atomic_long_t)ATOMIC_LONG_INIT(0);

Pretty cool that ATOMIC_LONG_INIT() return value needs a cast to
atomic_long_t! ^_^

Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Andrew Morton July 28, 2024, 8:54 p.m. UTC | #3
On Fri, 26 Jul 2024 19:11:10 -0400 Johannes Weiner <hannes@cmpxchg.org> wrote:

> On Fri, Jul 26, 2024 at 08:31:10PM +0000, Roman Gushchin wrote:
> > When a page_counter structure is initialized, there is no need to
> > use an atomic set operation to initialize the usage counter because
> > at this point the structure is not visible to anybody else.
> > ATOMIC_LONG_INIT() is what should be used in such cases.
> > 
> > Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
> > ---
> >  include/linux/page_counter.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
> > index cf837d0f8ed1..5da11392b382 100644
> > --- a/include/linux/page_counter.h
> > +++ b/include/linux/page_counter.h
> > @@ -53,7 +53,7 @@ static inline void page_counter_init(struct page_counter *counter,
> >  				     struct page_counter *parent,
> >  				     bool protection_support)
> >  {
> > -	atomic_long_set(&counter->usage, 0);
> > +	counter->usage = (atomic_long_t)ATOMIC_LONG_INIT(0);
> 
> Pretty cool that ATOMIC_LONG_INIT() return value needs a cast to
> atomic_long_t! ^_^

That's because this wicked patch passed in an `int'. 

	counter->usage = ATOMIC_LONG_INIT((atomic_long_t)0);
diff mbox series

Patch

diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
index cf837d0f8ed1..5da11392b382 100644
--- a/include/linux/page_counter.h
+++ b/include/linux/page_counter.h
@@ -53,7 +53,7 @@  static inline void page_counter_init(struct page_counter *counter,
 				     struct page_counter *parent,
 				     bool protection_support)
 {
-	atomic_long_set(&counter->usage, 0);
+	counter->usage = (atomic_long_t)ATOMIC_LONG_INIT(0);
 	counter->max = PAGE_COUNTER_MAX;
 	counter->parent = parent;
 	counter->protection_support = protection_support;