Skip to content

Commit

Permalink
locking/percpu-rwsem: Trigger contention tracepoints only if contended
Browse files Browse the repository at this point in the history
We mistakenly always fire lock contention tracepoints in the writer path,
while it should be conditional on the trylock result.

Signed-off-by: Namhyung Kim <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Reviewed-by: Waiman Long <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
namhyung authored and Ingo Molnar committed Feb 28, 2024
1 parent f22f713 commit f3e3620
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions kernel/locking/percpu-rwsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ static bool readers_active_check(struct percpu_rw_semaphore *sem)

void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
{
bool contended = false;

might_sleep();
rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE);

/* Notify readers to take the slow path. */
rcu_sync_enter(&sem->rss);
Expand All @@ -234,8 +235,11 @@ void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
* Try set sem->block; this provides writer-writer exclusion.
* Having sem->block set makes new readers block.
*/
if (!__percpu_down_write_trylock(sem))
if (!__percpu_down_write_trylock(sem)) {
trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE);
percpu_rwsem_wait(sem, /* .reader = */ false);
contended = true;
}

/* smp_mb() implied by __percpu_down_write_trylock() on success -- D matches A */

Expand All @@ -247,7 +251,8 @@ void __sched percpu_down_write(struct percpu_rw_semaphore *sem)

/* Wait for all active readers to complete. */
rcuwait_wait_event(&sem->writer, readers_active_check(sem), TASK_UNINTERRUPTIBLE);
trace_contention_end(sem, 0);
if (contended)
trace_contention_end(sem, 0);
}
EXPORT_SYMBOL_GPL(percpu_down_write);

Expand Down

0 comments on commit f3e3620

Please sign in to comment.