Skip to contentSkip to conversation

locking/percpu-rwsem: Trigger contention tracepoints only if contended

PATCH Applied Namhyung Kim 4 participants 7 messages Nov 8, 2023 – Feb 28, 2024 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/percpu-rwsem: Trigger contention tracepoints only if contended

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

---
 kernel/locking/percpu-rwsem.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -3
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#1 lore raw

Ping!

Quoted 51 lines · Namhyung Kim

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

---
 kernel/locking/percpu-rwsem.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -4
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
--

2.42.0.869.gea05f2083d-goog

#2 lore raw

Hello,

Quoted 7 lines · Namhyung Kim

Ping!

Quoted 3 lines · Namhyung Kim

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

Can anybody take a look at this? This makes a large noise in the lock contention result.

Thanks,
Namhyung

Quoted 48 lines
Quoted 48 lines
---
 kernel/locking/percpu-rwsem.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -4
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
--

2.42.0.869.gea05f2083d-goog

reviewer Reviewed-by
#3 lore raw
Quoted 58 lines · Namhyung Kim

Hello,

Quoted 5 lines · Namhyung Kim

Ping!

Quoted 2 lines · Namhyung Kim

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

Can anybody take a look at this? This makes a large noise in the lock contention result.

Thanks,
Namhyung

Quoted 44 lines
Quoted 44 lines
---
  kernel/locking/percpu-rwsem.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -3
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

--
2.42.0.869.gea05f2083d-goog

Yes, that makes sense. Sorry for missing this patch.

#4 lore raw
Quoted 61 lines · Waiman Long
Quoted 58 lines · Namhyung Kim

Hello,

Quoted 5 lines · Namhyung Kim

Ping!

Quoted 2 lines · Namhyung Kim

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

Can anybody take a look at this? This makes a large noise in the lock contention result.

Thanks,
Namhyung

Quoted 44 lines
Quoted 44 lines
---
  kernel/locking/percpu-rwsem.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -3
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

--
2.42.0.869.gea05f2083d-goog

Yes, that makes sense. Sorry for missing this patch.

Thanks for your review.
Namhyung

Applied
#5 lore raw
In reply to Waiman Long
Quoted 60 lines · Waiman Long
Quoted 58 lines · Namhyung Kim

Hello,

Quoted 5 lines · Namhyung Kim

Ping!

Quoted 2 lines · Namhyung Kim

It mistakenly fires lock contention tracepoints always in the writer path. It should be conditional on the try lock result.

Can anybody take a look at this? This makes a large noise in the lock contention result.

Thanks,
Namhyung

Quoted 44 lines
Quoted 44 lines
---
  kernel/locking/percpu-rwsem.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -3
index 185bd1c906b0..6083883c4fe0 100644
@@ -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);
@@ -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 */
 
@@ -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);
 
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

--
2.42.0.869.gea05f2083d-goog

Yes, that makes sense. Sorry for missing this patch.

Applied to tip:locking/core, thanks guys!

Ingo

merge bot Applied
#6 lore raw
In reply to Namhyung Kim
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     f3e3620f1a97fcd02a5f3606fa63888dbcffd82c
Gitweb:        https://git.kernel.org/tip/f3e3620f1a97fcd02a5f3606fa63888dbcffd82c
Author:        Namhyung Kim <[email protected]>
AuthorDate:    Wed, 08 Nov 2023 13:53:22 -08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 28 Feb 2024 13:10:29 +01:00

locking/percpu-rwsem: Trigger contention tracepoints only if contended

We mistakenly always fire lock contention tracepoints in the writer path, while it should be conditional on the trylock result.

---
 kernel/locking/percpu-rwsem.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/locking/percpu-rwsem.c +8 -3
index 185bd1c..6083883 100644
@@ -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);
@@ -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 */
 
@@ -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);