mm, list_lru: refactor the locking code
Subsystems (2)
PATCH 1/1 mm, list_lru: refactor the locking code
From: Kairui Song <[email protected]>
Cocci is confused by the try lock then release RCU and return logic here. So separate the try lock part out into a standalone helper. The code is easier to follow too.
No feature change, fixes:
cocci warnings: (new ones prefixed by >>)
Quoted 2 lines
Quoted 2 lines
mm/list_lru.c:82:3-9: preceding lock on line 77
mm/list_lru.c:82:3-9: preceding lock on line 77
mm/list_lru.c:82:3-9: preceding lock on line 75 mm/list_lru.c:82:3-9: preceding lock on line 75
--- mm/list_lru.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)
mm/list_lru.c +19 -15
@@ -60,30 +60,34 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)return &lru->node[nid].lru; } +static inline bool lock_list_lru (struct list_lru_one *l,bool irq) +{ +if (irq) +spin_lock_irq (&l->lock); +else +spin_lock (&l->lock); +if (unlikely (READ_ONCE (l->nr_items) ==LONG_MIN )) { +if (irq) +spin_unlock_irq (&l->lock); +else +spin_unlock (&l->lock); +return false; + } +return true; +} +static inline struct list_lru_one *lock_list_lru_of_memcg (struct list_lru *lru,int nid,struct mem_cgroup *memcg,bool irq,bool skip_empty) {struct list_lru_one *l; -long nr_items;rcu_read_lock (); again: l =list_lru_from_memcg_idx (lru, nid,memcg_kmem_id (memcg)); -if (likely (l)) { -if (irq) -spin_lock_irq (&l->lock); -else -spin_lock (&l->lock); - nr_items =READ_ONCE (l->nr_items); -if (likely (nr_items !=LONG_MIN )) { -rcu_read_unlock (); -return l; - } -if (irq) -spin_unlock_irq (&l->lock); -else -spin_unlock (&l->lock); +if (likely (l) &&lock_list_lru (l, irq)) { +rcu_read_unlock (); +return l; }/* * Caller may simply bail out if raced with reparenting or
Signature · 1 line
2.49.0
Quoted 66 lines · Kairui Song
From: Kairui Song <[email protected]>
Cocci is confused by the try lock then release RCU and return logic here. So separate the try lock part out into a standalone helper. The code is easier to follow too.
No feature change, fixes:
cocci warnings: (new ones prefixed by >>)
Quoted 2 lines
Quoted 2 lines
mm/list_lru.c:82:3-9: preceding lock on line 77
mm/list_lru.c:82:3-9: preceding lock on line 77mm/list_lru.c:82:3-9: preceding lock on line 75 mm/list_lru.c:82:3-9: preceding lock on line 75Reported-by kernel test robot <[email protected]>Reported-by Julia Lawall <[email protected]>Signed-off-by Kairui Song <[email protected]>--- mm/list_lru.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)mm/list_lru.c +16
-11@@ -60,30 +60,34 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)return &lru->node[nid].lru; } +static inline bool lock_list_lru (struct list_lru_one *l,bool irq) +{ +if (irq) +spin_lock_irq (&l->lock); +else +spin_lock (&l->lock); +if (unlikely (READ_ONCE (l->nr_items) ==LONG_MIN )) { +if (irq) +spin_unlock_irq (&l->lock); +else +spin_unlock (&l->lock); +return false; + } +return true; +} +static inline struct list_lru_one *lock_list_lru_of_memcg (struct list_lru *lru,int nid,struct mem_cgroup *memcg,bool irq,bool skip_empty) {struct list_lru_one *l; -long nr_items;rcu_read_lock (); again: l =list_lru_from_memcg_idx (lru, nid,memcg_kmem_id (memcg)); -if (likely (l)) { -if (irq) -spin_lock_irq (&l->lock); -else -spin_lock (&l->lock); - nr_items =READ_ONCE (l->nr_items); -if (likely (nr_items !=LONG_MIN )) { -rcu_read_unlock (); -return l; - }
mm/list_lru.c list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx) · line 93
Quoted 10 lines
mm/list_lru.c +3
-4-if (irq) -spin_unlock_irq (&l->lock); -else -spin_unlock (&l->lock); +if (likely (l) &&lock_list_lru (l, irq)) { +rcu_read_unlock (); +return l; }/* * Caller may simply bail out if raced with reparenting or
And the code readability has also been improved.
Thanks!
Quoted 15 lines · Kairui Song
From: Kairui Song <[email protected]>
Cocci is confused by the try lock then release RCU and return logic here. So separate the try lock part out into a standalone helper. The code is easier to follow too.
No feature change, fixes:
cocci warnings: (new ones prefixed by >>)
Quoted 2 lines
Quoted 2 lines
mm/list_lru.c:82:3-9: preceding lock on line 77
mm/list_lru.c:82:3-9: preceding lock on line 77
Quoted 7 lines
mm/list_lru.c:82:3-9: preceding lock on line 75
mm/list_lru.c:82:3-9: preceding lock on line 75Reported-by kernel test robot <[email protected]>Reported-by Julia Lawall <[email protected]>Signed-off-by Kairui Song <[email protected]>
Thanks.
Hi Kairui,
Quoted 11 lines · On Tue, 27 May 2025 02:06:38 +0800 Kairui Song <[email protected]> wrote:
From: Kairui Song <[email protected]>
Cocci is confused by the try lock then release RCU and return logic here. So separate the try lock part out into a standalone helper. The code is easier to follow too.
No feature change, fixes:
cocci warnings: (new ones prefixed by >>)
Quoted 2 lines
Quoted 2 lines
mm/list_lru.c:82:3-9: preceding lock on line 77
mm/list_lru.c:82:3-9: preceding lock on line 77
Quoted 7 lines
mm/list_lru.c:82:3-9: preceding lock on line 75 mm/list_lru.c:82:3-9: preceding lock on line 75
Reported-by kernel test robot <[email protected]>Reported-by Julia Lawall <[email protected]>Signed-off-by Kairui Song <[email protected]>
Quoted 15 lines
--- mm/list_lru.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)mm/list_lru.c +3
-0@@ -60,30 +60,34 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)return &lru->node[nid].lru; } +static inline bool lock_list_lru (struct list_lru_one *l,bool irq) +{ +if (irq)
mm/list_lru.c list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx) · line 75
Quoted 10 lines
mm/list_lru.c +10
-0+spin_lock_irq (&l->lock); + else + spin_lock(&l->lock); +if (unlikely (READ_ONCE (l->nr_items) ==LONG_MIN )) { +if (irq) +spin_unlock_irq (&l->lock); +else +spin_unlock (&l->lock); +return false; + }
I'd prefer 'if (likely(...)) return true;' to reduce indentation and stop wondering what goes to the likely case earlier. But that's my personal preferrence that shouldn't block this.
Quoted 21 lines
mm/list_lru.c +3
-9+return true; +} +static inline struct list_lru_one *lock_list_lru_of_memcg (struct list_lru *lru,int nid,struct mem_cgroup *memcg,bool irq,bool skip_empty) {struct list_lru_one *l; -long nr_items;rcu_read_lock (); again: l =list_lru_from_memcg_idx (lru, nid,memcg_kmem_id (memcg)); -if (likely (l)) { -if (irq) -spin_lock_irq (&l->lock); -else -spin_lock (&l->lock); - nr_items =READ_ONCE (l->nr_items); -if (likely (nr_items !=LONG_MIN )) { -rcu_read_unlock ();
mm/list_lru.c list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx) · line 91
Quoted 10 lines
mm/list_lru.c +3
-6-return l; - } -if (irq) -spin_unlock_irq (&l->lock); -else -spin_unlock (&l->lock); +if (likely (l) &&lock_list_lru (l, irq)) { +rcu_read_unlock (); +return l; }
Much easier to read, indeed :)
Quoted 4 lines
mm/list_lru.c +0
-0/* * Caller may simply bail out if raced with reparenting or Signature · 1 line
2.49.0
Thanks,
SJ
[...]