Skip to content

Commit df316ab

Browse files
ubizjakhtejun
authored andcommitted
workqueue: Use atomic_try_cmpxchg_relaxed() in tryinc_node_nr_active()
Use try_cmpxchg() family of locking primitives instead of cmpxchg(*ptr, old, new) == old. The x86 CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after CMPXCHG (and related move instruction in front of CMPXCHG). Also, try_cmpxchg() implicitly assigns old *ptr value to "old" when CMPXCHG fails. There is no need to re-read the value in the loop. The generated assembly improves from: 3f7: 44 8b 0a mov (%rdx),%r9d 3fa: eb 12 jmp 40e <...> 3fc: 8d 79 01 lea 0x1(%rcx),%edi 3ff: 89 c8 mov %ecx,%eax 401: f0 0f b1 7a 04 lock cmpxchg %edi,0x4(%rdx) 406: 39 c1 cmp %eax,%ecx 408: 0f 84 83 00 00 00 je 491 <...> 40e: 8b 4a 04 mov 0x4(%rdx),%ecx 411: 41 39 c9 cmp %ecx,%r9d 414: 7f e6 jg 3fc <...> to: 256b: 45 8b 08 mov (%r8),%r9d 256e: 41 8b 40 04 mov 0x4(%r8),%eax 2572: 41 39 c1 cmp %eax,%r9d 2575: 7e 10 jle 2587 <...> 2577: 8d 78 01 lea 0x1(%rax),%edi 257a: f0 41 0f b1 78 04 lock cmpxchg %edi,0x4(%r8) 2580: 75 f0 jne 2572 <...> No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Lai Jiangshan <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent fc2898e commit df316ab

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

kernel/workqueue.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,17 +1690,14 @@ static void __pwq_activate_work(struct pool_workqueue *pwq,
16901690
static bool tryinc_node_nr_active(struct wq_node_nr_active *nna)
16911691
{
16921692
int max = READ_ONCE(nna->max);
1693+
int old = atomic_read(&nna->nr);
16931694

1694-
while (true) {
1695-
int old, tmp;
1696-
1697-
old = atomic_read(&nna->nr);
1695+
do {
16981696
if (old >= max)
16991697
return false;
1700-
tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1);
1701-
if (tmp == old)
1702-
return true;
1703-
}
1698+
} while (!atomic_try_cmpxchg_relaxed(&nna->nr, &old, old + 1));
1699+
1700+
return true;
17041701
}
17051702

17061703
/**

0 commit comments

Comments
 (0)