locking/lockdep: Disable KASAN instrumentation of lockdep.c
Waiman Long
reviewerv2 cover locking/lockdep: Disable KASAN instrumentation of lockdep.c
It is found that disabling KASAN instrumentation when compiling lockdep.c can significantly improve the performance of RT debug kernel while the performance benefit of non-RT debug kernel is relatively modest.
This series also include a patch to add locking events to the rtmutex slow paths for better analysis of the different performance behavior between RT and non-RT debug kernels.
Waiman Long (2):
locking/lock_events: Add locking events for rtmutex slow paths locking/lockdep: Disable KASAN instrumentation of lockdep.c
kernel/locking/Makefile | 3 ++- kernel/locking/lock_events_list.h | 21 +++++++++++++++++++++ kernel/locking/rtmutex.c | 29 ++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 6 deletions(-)
Signature · 1 line
2.48.1
Waiman Long
reviewerPATCH v2 1/2 locking/lock_events: Add locking events for rtmutex slow paths
Add locking events for rtlock_slowlock() and rt_mutex_slowlock() for profiling the slow path behavior of rt_spin_lock() and rt_mutex_lock().
--- kernel/locking/lock_events_list.h | 21 +++++++++++++++++++++ kernel/locking/rtmutex.c | 29 ++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-)
kernel/locking/lock_events_list.h +21 -0
@@ -67,3 +67,24 @@ LOCK_EVENT(rwsem_rlock_handoff) /* # of read lock handoffs */ LOCK_EVENT(rwsem_wlock) /* # of write locks acquired */ LOCK_EVENT(rwsem_wlock_fail) /* # of failed write lock acquisitions */ LOCK_EVENT(rwsem_wlock_handoff) /* # of write lock handoffs */ + +/* + * Locking events for rtlock_slowlock() + */ +LOCK_EVENT(rtlock_slowlock) /* # of rtlock_slowlock() calls */ +LOCK_EVENT(rtlock_slow_acq1) /* # of locks acquired after wait_lock */ +LOCK_EVENT(rtlock_slow_acq2) /* # of locks acquired in for loop */ +LOCK_EVENT(rtlock_slow_sleep) /* # of sleeps */ +LOCK_EVENT(rtlock_slow_wake) /* # of wakeup's */ + +/* + * Locking events for rt_mutex_slowlock() + */ +LOCK_EVENT(rtmutex_slowlock) /* # of rt_mutex_slowlock() calls */ +LOCK_EVENT(rtmutex_slow_block) /* # of rt_mutex_slowlock_block() calls */ +LOCK_EVENT(rtmutex_slow_acq1) /* # of locks acquired after wait_lock */ +LOCK_EVENT(rtmutex_slow_acq2) /* # of locks acquired at the end */ +LOCK_EVENT(rtmutex_slow_acq3) /* # of locks acquired in *block() */ +LOCK_EVENT(rtmutex_slow_sleep) /* # of sleeps */ +LOCK_EVENT(rtmutex_slow_wake) /* # of wakeup's */ +LOCK_EVENT(rtmutex_deadlock) /* # of rt_mutex_handle_deadlock()'s */
kernel/locking/rtmutex.c +24 -5
@@ -27,6 +27,7 @@#include <trace/events/lock.h> #include "rtmutex_common.h" +#include "lock_events.h" #ifndef WW_RT # define build_ww_mutex () (false)
@@ -1612,10 +1613,13 @@ static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,struct task_struct *owner;int ret =0 ; +lockevent_inc (rtmutex_slow_block);for (;;) {/* Try to acquire the lock: */ -if (try_to_take_rt_mutex (lock, current, waiter)) +if (try_to_take_rt_mutex (lock, current, waiter)) { +lockevent_inc (rtmutex_slow_acq3);break ; + }if (timeout && !timeout->task) { ret = -ETIMEDOUT ;
@@ -1638,8 +1642,10 @@ static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock, owner =NULL ;raw_spin_unlock_irq_wake (&lock->wait_lock, wake_q); -if (!owner || !rtmutex_spin_on_owner (lock, waiter, owner)) +if (!owner || !rtmutex_spin_on_owner (lock, waiter, owner)) { +lockevent_inc (rtmutex_slow_sleep);rt_mutex_schedule (); + }raw_spin_lock_irq (&lock->wait_lock);set_current_state (state);
@@ -1694,6 +1700,7 @@ static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,int ret;lockdep_assert_held (&lock->wait_lock); +lockevent_inc (rtmutex_slowlock);/* Try to acquire the lock again: */ if (try_to_take_rt_mutex (lock, current,NULL )) {
@@ -1701,6 +1708,7 @@ static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,__ww_mutex_check_waiters (rtm, ww_ctx, wake_q);ww_mutex_lock_acquired (ww, ww_ctx); } +lockevent_inc (rtmutex_slow_acq1);return 0 ; }
@@ -1719,10 +1727,12 @@ static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,__ww_mutex_check_waiters (rtm, ww_ctx, wake_q);ww_mutex_lock_acquired (ww, ww_ctx); } +lockevent_inc (rtmutex_slow_acq2); }else {__set_current_state (TASK_RUNNING );remove_waiter (lock, waiter);rt_mutex_handle_deadlock (ret, chwalk, lock, waiter); +lockevent_inc (rtmutex_deadlock); }/*
@@ -1751,6 +1761,7 @@ static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock, &waiter, wake_q);debug_rt_mutex_free_waiter (&waiter); +lockevent_cond_inc (rtmutex_slow_wake, !wake_q_empty (wake_q));return ret; }
@@ -1823,9 +1834,12 @@ static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock,struct task_struct *owner;lockdep_assert_held (&lock->wait_lock); +lockevent_inc (rtlock_slowlock); -if (try_to_take_rt_mutex (lock, current,NULL )) +if (try_to_take_rt_mutex (lock, current,NULL )) { +lockevent_inc (rtlock_slow_acq1);return ; + }rt_mutex_init_rtlock_waiter (&waiter);
@@ -1838,8 +1852,10 @@ static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock,for (;;) {/* Try to acquire the lock again */ -if (try_to_take_rt_mutex (lock, current, &waiter)) +if (try_to_take_rt_mutex (lock, current, &waiter)) { +lockevent_inc (rtlock_slow_acq2);break ; + }if (&waiter ==rt_mutex_top_waiter (lock)) owner =rt_mutex_owner (lock);
@@ -1847,8 +1863,10 @@ static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock, owner =NULL ;raw_spin_unlock_irq_wake (&lock->wait_lock, wake_q); -if (!owner || !rtmutex_spin_on_owner (lock, &waiter, owner)) +if (!owner || !rtmutex_spin_on_owner (lock, &waiter, owner)) { +lockevent_inc (rtlock_slow_sleep);schedule_rtlock (); + }raw_spin_lock_irq (&lock->wait_lock);set_current_state (TASK_RTLOCK_WAIT );
@@ -1865,6 +1883,7 @@ static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock,debug_rt_mutex_free_waiter (&waiter);trace_contention_end (lock,0 ); +lockevent_cond_inc (rtlock_slow_wake, !wake_q_empty (wake_q)); } static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
Signature · 1 line
2.48.1
Waiman Long
reviewerPATCH v2 2/2 locking/lockdep: Disable KASAN instrumentation of lockdep.c
In reply to Waiman LongBoth KASAN and LOCKDEP are commonly enabled in building a debug kernel. Each of them can significantly slow down the speed of a debug kernel. Enabling KASAN instrumentation of the LOCKDEP code will further slow thing down.
Since LOCKDEP is a high overhead debugging tool, it will never get enabled in a production kernel. The LOCKDEP code is also pretty mature and is unlikely to get major changes. There is also a possibility of recursion similar to KCSAN. As the small advantage of enabling KASAN instrumentation to catch potential memory access error is probably not worth the drawback of further slowing down a debug kernel, disable KASAN instrumentation to enable a debug kernel to gain some performance back.
With debug kernels with both LOCKDEP and KASAN enabled running on a 2-socket 128-thread x86-64 system and a 80-core arm64 system, the times (real and sys with the time command) to do a parallel kernel build are shown below.
Kernel type Real Time Sys Time
----------- --------- --------
x86-64:
Non-debug kernel 9m38.528s 304m17.007s
Debug kernel before patch 16m38.765s 1086m34.930s
Debug kernel after patch 16m4.758s 1025m26.335s
Before/after % change -3.4% -5.6%
Non-debug RT kernel 11m32.804s 121m52.835s
Debug RT kernel before patch 59m29.618s 1772m30.699s
Debug RT kernel after patch 37m47.089s 937m56.856s
Before/after % change -36.5% -47.1%
arm64:
Debug RT kernel before patch 46m9.385s 676m13.605s
Debug RT kernel after patcha 33m41.428s 436m3.430s
Before/after % change -27.0% -35.5%
It looks like the KASAN instrumentation overhead is less on arm64. While the performance benefit for non-RT debug kernel is modest, the performance gain for RT debug kernel is significant.
Looking at the RT kernel locking event data for the x86-64 system, we have
Event type Non-debug Debug before patch Debug after patch ---------- --------- ------------------ ----------------- rtlock_slowlock 66,593,828 2,868,760,165 2,832,990,386 rtlock_slow_acq1 43,705,130 2,833,575,907 2,800,928,283 rtlock_slow_acq2 22,888,698 35,177,418 32,055,592
rtlock_slow_sleep 22,568,560 29,206,559 27,833,274
rtmutex_slowlock 468,207 560,080 549,080
rtmutex_slow_acq1 11,840 67,208 39,353
rtmutex_slow_block 456,367 492,872 509,727 rtmutex_slow_sleep 258,071 208,019 220,480
The profile of the debug kernel before and after patch are similar. Compared with the non-debug kernel, the number of rtlock_slowlock() has increased significantly by more than 40x. That means the corresponding wait_lock has to be acquired that many more times with the associated lockdep overhead. The average lock nesting depth will also be higher. The non-RT debug kernel doesn't have this extra overhead.
--- kernel/locking/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/locking/Makefile +2 -1
@@ -5,7 +5,8 @@ KCOV_INSTRUMENT := n obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o -# Avoid recursion lockdep -> sanitizer -> ... -> lockdep. +# Avoid recursion lockdep -> sanitizer -> ... -> lockdep & improve performance. +KASAN_SANITIZE_lockdep.o := n KCSAN_SANITIZE_lockdep.o := n ifdef CONFIG_FUNCTION_TRACER
Signature · 1 line
2.48.1