Skip to content

Commit ad0a2e4

Browse files
ubizjakIngo Molnar
authored and
Ingo Molnar
committed
locking/atomic, xen: Use sync_try_cmpxchg() instead of sync_cmpxchg()
Use sync_try_cmpxchg() instead of sync_cmpxchg(*ptr, old, new) == old in clear_masked_cond(), clear_linked() and gnttab_end_foreign_access_ref_v1(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg), improving the cmpxchg loop in gnttab_end_foreign_access_ref_v1() from: 174: eb 0e jmp 184 <...> 176: 89 d0 mov %edx,%eax 178: f0 66 0f b1 31 lock cmpxchg %si,(%rcx) 17d: 66 39 c2 cmp %ax,%dx 180: 74 11 je 193 <...> 182: 89 c2 mov %eax,%edx 184: 89 d6 mov %edx,%esi 186: 66 83 e6 18 and $0x18,%si 18a: 74 ea je 176 <...> to: 614: 89 c1 mov %eax,%ecx 616: 66 83 e1 18 and $0x18,%cx 61a: 75 11 jne 62d <...> 61c: f0 66 0f b1 0a lock cmpxchg %cx,(%rdx) 621: 75 f1 jne 614 <...> No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Juergen Gross <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stefano Stabellini <[email protected]> Cc: Oleksandr Tyshchenko <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: [email protected]
1 parent 636d6a8 commit ad0a2e4

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

drivers/xen/events/events_fifo.c

+12-14
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,20 @@ static bool evtchn_fifo_is_masked(evtchn_port_t port)
226226
*/
227227
static bool clear_masked_cond(volatile event_word_t *word)
228228
{
229-
event_word_t new, old, w;
229+
event_word_t new, old;
230230

231-
w = *word;
231+
old = *word;
232232

233233
do {
234-
if (!(w & (1 << EVTCHN_FIFO_MASKED)))
234+
if (!(old & (1 << EVTCHN_FIFO_MASKED)))
235235
return true;
236236

237-
if (w & (1 << EVTCHN_FIFO_PENDING))
237+
if (old & (1 << EVTCHN_FIFO_PENDING))
238238
return false;
239239

240-
old = w & ~(1 << EVTCHN_FIFO_BUSY);
240+
old = old & ~(1 << EVTCHN_FIFO_BUSY);
241241
new = old & ~(1 << EVTCHN_FIFO_MASKED);
242-
w = sync_cmpxchg(word, old, new);
243-
} while (w != old);
242+
} while (!sync_try_cmpxchg(word, &old, new));
244243

245244
return true;
246245
}
@@ -259,17 +258,16 @@ static void evtchn_fifo_unmask(evtchn_port_t port)
259258

260259
static uint32_t clear_linked(volatile event_word_t *word)
261260
{
262-
event_word_t new, old, w;
261+
event_word_t new, old;
263262

264-
w = *word;
263+
old = *word;
265264

266265
do {
267-
old = w;
268-
new = (w & ~((1 << EVTCHN_FIFO_LINKED)
269-
| EVTCHN_FIFO_LINK_MASK));
270-
} while ((w = sync_cmpxchg(word, old, new)) != old);
266+
new = (old & ~((1 << EVTCHN_FIFO_LINKED)
267+
| EVTCHN_FIFO_LINK_MASK));
268+
} while (!sync_try_cmpxchg(word, &old, new));
271269

272-
return w & EVTCHN_FIFO_LINK_MASK;
270+
return old & EVTCHN_FIFO_LINK_MASK;
273271
}
274272

275273
static void consume_one_event(unsigned cpu, struct evtchn_loop_ctrl *ctrl,

drivers/xen/grant-table.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,14 @@ EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
427427

428428
static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref)
429429
{
430-
u16 flags, nflags;
431-
u16 *pflags;
430+
u16 *pflags = &gnttab_shared.v1[ref].flags;
431+
u16 flags;
432432

433-
pflags = &gnttab_shared.v1[ref].flags;
434-
nflags = *pflags;
433+
flags = *pflags;
435434
do {
436-
flags = nflags;
437435
if (flags & (GTF_reading|GTF_writing))
438436
return 0;
439-
} while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
437+
} while (!sync_try_cmpxchg(pflags, &flags, 0));
440438

441439
return 1;
442440
}

0 commit comments

Comments
 (0)