Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drgn helpers and script for locking primitives #387

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

imran-kn
Copy link
Contributor

This change set includes helpers for following locking primitives:

  1. Mutexes
  2. Semaphores
  3. Read-write semaphores
  4. OSQ locks

Since osq_lock/unlock functions are not available to modules,. I could not add test cases for these and hence I have put all osq helpers in a script under contrib. Besides osq helpers the contrib script also contains helper functions for above mentioned other 3 locking primitives. These helper functions take a lock address as argument and dump various info pertaining to these locks. I have included some examples in commit for reference.

P.S: I also have per-cpu rwsem helpers almost ready but will add them after some more testing.

imran-kn added 15 commits March 15, 2024 15:10
Sometimes it is useful to get cpu that owns a given per-cpu variable.
For example osq locks and qspinlocks use per-cpu entities when a CPU
needs to spin, while waiting to acquire the lock. This happens when
there are multiple spinners for a given lock.
In such cases, this helper can let us know the spinners for a given
qspinlock or osq lock.

Signed-off-by: Imran Khan <[email protected]>
These don't have tets cases so can't go under helpers.
I have put these under contrib so that they are available
for use.

  python3 -m drgn -s vmlinux -c vmcore contrib/locks.py --help

  usage: locks.py [-h] lock_type info_type [locks ...]

  positional arguments:
    lock_type   type of lock i.e mutex. semaphore, rwsemaphore etc.
    info_type   "owner" or "waiter" or "all"
    locks       list of lock addresses

  options:
    -h, --help  show this help message and exit

For example following command will give us call stack for owner
waiters of specified mutex(es):

  python3 -m drgn -s vmlinux -c vmcore contrib/locks.py mutex all ffffffffc0143400
  ..................
  Dumping call stack for waiter of mutex: ffffffffc0143400

  call stack for pid: 215
  #0  context_switch (kernel/sched/core.c:5238:2)
  osandov#1  __schedule (kernel/sched/core.c:6551:8)
  osandov#2  schedule (kernel/sched/core.c:6627:3)
  osandov#3  schedule_preempt_disabled (kernel/sched/core.c:6686:2)
  osandov#4  __mutex_lock_common (kernel/locking/mutex.c:679:3)
  osandov#5  __mutex_lock (kernel/locking/mutex.c:747:9)
  osandov#6  0xffffffffc01411f6
  ..................

  call stack for pid: 216
  #0  context_switch (kernel/sched/core.c:5238:2)
  osandov#1  __schedule (kernel/sched/core.c:6551:8)
  osandov#2  schedule (kernel/sched/core.c:6627:3)
  osandov#3  schedule_preempt_disabled (kernel/sched/core.c:6686:2)
  osandov#4  __mutex_lock_common (kernel/locking/mutex.c:679:3)
  osandov#5  __mutex_lock (kernel/locking/mutex.c:747:9)
  osandov#6  0xffffffffc014112b
  ..................

  Dumping call stack for owner of mutex: ffffffffc0143400

  call stack for pid: 214
  #0  delay_tsc (arch/x86/lib/delay.c:79:3)
  osandov#1  0xffffffffc0141308
  ..................

  Signed-off-by: Imran Khan <[email protected]>
python3 -m drgn -s vmlinux -c vmcore contrib/locks.py --help

usage: locks.py [-h] lock_type info_type [locks ...]

positional arguments:
lock_type   type of lock i.e mutex. semaphore, rwsemaphore etc.
info_type   "owner" or "waiter" or "all"
locks       list of lock addresses

options:
-h, --help  show this help message and exit

For example following command will give us call stack for owner
waiters of specified mutex(es):

python3 -m drgn -s vmlinux -c vmcore contrib/locks.py semaphore waiter ffffffffc0097340
Dumping call stack for waiter(s) of semaphore: ffffffffc0097340

call stack for pid: 1178
 #0  context_switch (kernel/sched/core.c:2811:2)
 osandov#1  __schedule (kernel/sched/core.c:3387:8)
 osandov#2  schedule (kernel/sched/core.c:3431:3)
 osandov#3  schedule_timeout (kernel/time/timer.c:1724:3)
 osandov#4  __down_common (kernel/locking/semaphore.c:221:13)
 osandov#5  __down (kernel/locking/semaphore.c:238:2)
 osandov#6  down (kernel/locking/semaphore.c:62:3)
 osandov#7  0xffffffffc0095045

..................

 call stack for pid: 1180
 #0  context_switch (kernel/sched/core.c:2811:2)
 osandov#1  __schedule (kernel/sched/core.c:3387:8)
 osandov#2  schedule (kernel/sched/core.c:3431:3)
 osandov#3  schedule_timeout (kernel/time/timer.c:1724:3)
 osandov#4  __down_common (kernel/locking/semaphore.c:221:13)
 osandov#5  __down (kernel/locking/semaphore.c:238:2)
 osandov#6  down (kernel/locking/semaphore.c:62:3)
 osandov#7  0xffffffffc0095045
 .............

Signed-off-by: Imran Khan <[email protected]>
Also include its test.

Signed-off-by: Imran Khan <[email protected]>
Add some ready-made helpers for rwsem and make the script
modular so that options can be added/removed for one lock
type, independently from other lock types.

Some example of using the script have been given below:

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py -h
usage: drgn script to dump lock information [-h] {mutex,semaphore,rwsem} ...

options:
  -h, --help            show this help message and exit

subcommands:
  {mutex,semaphore,rwsem}
    mutex               get mutex info.
    semaphore           get semaphore info.
    rwsem               get read-write semaphore info.

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py semaphore -h
usage: drgn script to dump lock information semaphore [-h] [--info | --waiter-list | --waiter-callstack] [locks ...]

positional arguments:
  locks               list of lock addresses

options:
  -h, --help          show this help message and exit
  --info              dump given semaphore's info like waiter(s) etc.
  --waiter-list       provide a list, of waiters of given semaphore(s)
  --waiter-callstack  provide callstack of all waiters of given semaphore(s)

python3 -m drgn -s vmlinux -c vmcore contrib/locks.py semaphore --waiter-list ffffffffc0097340
  The waiters of semaphore: ffffffffc0097340 are as follows:
    (struct task_struct *)0xffff992afc618000 pid: 1178 state: D
    (struct task_struct *)0xffff992afc7ae200 pid: 1181 state: D
    (struct task_struct *)0xffff992afc61ee40 pid: 1179 state: D
    (struct task_struct *)0xffff992afc619880 pid: 1180 state: D

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py semaphore --waiter-list ffffffffc0262340
  The waiters of semaphore: ffffffffc0262340 are as follows:
    (struct task_struct *)0xffff96c3fc6ec980 pid: 1178 state: S
    (struct task_struct *)0xffff96c3fc6eee40 pid: 1177 state: S
    (struct task_struct *)0xffff96c3fc5a55c0 pid: 1175 state: S
    (struct task_struct *)0xffff96c3fc6ee200 pid: 1176 state: S

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py semaphore --waiter-callstack ffffffffc0262340
Dumping call stack for waiter(s) of semaphore: ffffffffc0262340

call stack for pid: 1178
 #0  context_switch (kernel/sched/core.c:2811:2)
 osandov#1  __schedule (kernel/sched/core.c:3387:8)
 osandov#2  schedule (kernel/sched/core.c:3431:3)
 osandov#3  schedule_timeout (kernel/time/timer.c:1724:3)
 osandov#4  __down_common (kernel/locking/semaphore.c:221:13)
 osandov#5  __down_interruptible (kernel/locking/semaphore.c:243:9)
 osandov#6  down_interruptible (kernel/locking/semaphore.c:85:12)
 osandov#7  0xffffffffc0260045
.....................

call stack for pid: 1176
 #0  context_switch (kernel/sched/core.c:2811:2)
 osandov#1  __schedule (kernel/sched/core.c:3387:8)
 osandov#2  schedule (kernel/sched/core.c:3431:3)
 osandov#3  schedule_timeout (kernel/time/timer.c:1724:3)
 osandov#4  __down_common (kernel/locking/semaphore.c:221:13)
 osandov#5  __down_interruptible (kernel/locking/semaphore.c:243:9)
 osandov#6  down_interruptible (kernel/locking/semaphore.c:85:12)
 osandov#7  0xffffffffc0260045
call stack for pid: 1176
.....................

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py mutex -h
usage: drgn script to dump lock information mutex [-h] [--info | --waiter-list | --waiter-callstack | --owner-callstack] [locks ...]

positional arguments:
  locks               list of lock addresses

options:
  -h, --help          show this help message and exit
  --info              dump given mutex's info like owner, waiter(s) etc.
  --waiter-list       provide a list, of waiters of given mutex(es)
  --waiter-callstack  provide callstack of all waiters of given mutex(es)
  --owner-callstack   provide callstack of owner of given mutex(es)

python3 -m drgn -s vmlinux -c vmcore contrib/locks.py mutex --info ffffffffc0143400
  mutex: ffffffffc0143400 is owned by (struct task_struct *)0xffffa1fe42393900 pid: 214 state: R
  The waiters of mutex: ffffffffc0143400 are as follows:
    (struct task_struct *)0xffffa1fe42395580 pid: 215 state: D
    (struct task_struct *)0xffffa1fe42a58000 pid: 216 state: D
    (struct task_struct *)0xffffa1fe42389c80 pid: 217 state: D

python3 -m drgn -s vmlinux -c vmcore contrib/locks.py mutex --waiter-list ffffffffc0143400
  The waiters of mutex: ffffffffc0143400 are as follows:
    (struct task_struct *)0xffffa1fe42395580 pid: 215 state: D
    (struct task_struct *)0xffffa1fe42a58000 pid: 216 state: D
    (struct task_struct *)0xffffa1fe42389c80 pid: 217 state: D

python3 -m drgn -s vmlinux -c vmcore contrib/locks.py mutex --waiter-callstack ffffffffc0143400
Dumping call stack for waiter of mutex: ffffffffc0143400

call stack for pid: 215
 #0  context_switch (kernel/sched/core.c:5238:2)
 osandov#1  __schedule (kernel/sched/core.c:6551:8)
 osandov#2  schedule (kernel/sched/core.c:6627:3)
 osandov#3  schedule_preempt_disabled (kernel/sched/core.c:6686:2)
 osandov#4  __mutex_lock_common (kernel/locking/mutex.c:679:3)
 osandov#5  __mutex_lock (kernel/locking/mutex.c:747:9)
 osandov#6  0xffffffffc01411f6
........................

python3 -m drgn -s vmlinux -c vmcore-multiple-readers contrib/locks.py rwsem -h
usage: drgn script to dump lock information rwsem [-h] [--info | --waiter-list | --waiter-callstack | --owner-callstack] [locks ...]

positional arguments:
  locks               list of lock addresses

options:
  -h, --help          show this help message and exit
  --info              dump given rwsem's info like owner, waiter(s) etc.
  --waiter-list       provide a list, of waiters of given rwsem(s)
  --waiter-callstack  provide callstack of all waiters of given rwsem(s)
  --owner-callstack   provide callstack of owner of given rwsem(s)

python3 -m drgn -s vmlinux -c vmcore-reader-writer-reader-reader contrib/locks.py rwsem --info ffffffffc036d3c0
  rwsem: ffffffffc036d3c0 is owned by one or more readers.
  The waiters of rwsem are as follows:
    (struct task_struct *)0xffff9e593d4e3100: (pid)1175: type: down_write state: D
    (struct task_struct *)0xffff9e593d4e1880: (pid)1176: type: down_read state: D
    (struct task_struct *)0xffff9e593d4e6200: (pid)1177: type: down_read state: D

python3 -m drgn -s vmlinux -c vmcore-reader-writer-reader-reader contrib/locks.py rwsem --waiter-callstack ffffffffc036d3c0
Dumping call stack for waiter of rwsem: ffffffffc036d3c0

call stack for pid: 1175
 #0  context_switch (kernel/sched/core.c:2814:2)
 osandov#1  __schedule (kernel/sched/core.c:3389:8)
 osandov#2  schedule (kernel/sched/core.c:3433:3)
 osandov#3  __rwsem_down_write_failed_common (kernel/locking/rwsem-xadd.c:588:4)
 osandov#4  call_rwsem_down_write_failed+0x13/0x1f (arch/x86/lib/rwsem.S:105)
 osandov#5  __down_write (./arch/x86/include/asm/rwsem.h:126:2)
 osandov#6  down_write (kernel/locking/rwsem.c:56:2)
 osandov#7  0xffffffffc036b2b6
................................

python3 -m drgn -s vmlinux -c vmcore-writer-reader-reader-reader contrib/locks.py rwsem --info ffffffffc02033c0
  rwsem: ffffffffc02033c0 owned by writer (struct task_struct *)0xffff9162fdb58c40  (pid)1173  (state)R
  The waiters of rwsem are as follows:
    (struct task_struct *)0xffff9162fdb5ee40: (pid)1174: type: down_read state: D
    (struct task_struct *)0xffff9162fd8355c0: (pid)1175: type: down_read state: D
    (struct task_struct *)0xffff9162fd836200: (pid)1176: type: down_read state: D

For newer kernels number of read owners can be obtained:

python3 -m drgn -s vmlinux -c vmcore-multiple-readers contrib/locks.py rwsem --info ffffffffc00c9340
  rwsem: ffffffffc00c9340 is owned by 2 reader(s).
  There are no waiters for rwsem: ffffffffc00c9340.

Signed-off-by: Imran Khan <[email protected]>
Since osq_lock/unlock functions are not available to modules,
I could not provide test cases for these helpers and hence
I have put these under contrib.

These helpers are useful in high sys usage cases where high sys usage is due to
optimistic spinning on sleeping locks like rwsems and/or mutexes.

In such cases we can use the script as follows to get the CPUs
which are optimistically spinning to acquire the rwsem or mutex:

python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py osq --dump
There are spinners on one or more osq_lock
CPU(s): [21, 17, 59, 67, 6, 0, 28, 53, 4, 41, 55, 38, 63, 23, 27, 19, 36] are spinning on same osq_lock
CPU(s): [42, 56, 35, 18, 22, 3, 1] are spinning on same osq_lock
CPU(s): [70, 44, 9] are spinning on same osq_lock
CPU(s): [16, 26] are spinning on same osq_lock

Since an osq_lock belongs to a sleeping lock, the CPUs spinning on same osq_lock
are spinning to acquire the same sleeping lock.
Above vmcore was obtained with a fork-bomb test.

Signed-off-by: Imran Khan <[email protected]>
For example:

python3 -m drgn -s vmlinux -c vmcore-writer-reader-reader-reader contrib/locks.py rwsem --spinner-callstack ffffffffc03083c0
rwsem: ffffffffc03083c0 has 1 spinners and their call-stack is as follows:

call stack for pid: 239
 #0  __read_once_size (./include/linux/compiler.h:268:2)
 osandov#1  arch_atomic64_read (./arch/x86/include/asm/atomic64_64.h:22:9)
 osandov#2  atomic64_read (./include/asm-generic/atomic-instrumented.h:837:9)
 osandov#3  atomic_long_read (./include/asm-generic/atomic-long.h:28:9)
 osandov#4  rwsem_owner_flags (kernel/locking/rwsem.c:298:24)
 osandov#5  rwsem_spin_on_owner (kernel/locking/rwsem.c:737:9)
 osandov#6  rwsem_optimistic_spin (kernel/locking/rwsem.c:812:17)
 osandov#7  rwsem_down_read_slowpath (kernel/locking/rwsem.c:1018:6)
 osandov#8  __down_read_killable (kernel/locking/rwsem.c:1366:14)
 osandov#9  down_read_killable (kernel/locking/rwsem.c:1532:6)
 osandov#10 0xffffffffc030622c
.................

Signed-off-by: Imran Khan <[email protected]>
For example:
python3 -m drgn -s vmlinux -c vmcore-1 contrib/locks.py mutex --spinner-callstack ffffffffc02af340
mutex: ffffffffc02af340 has 4 spinners and their call-stack is as follows:

call stack for pid: 250
 #0  __read_once_size (./include/linux/compiler.h:268:2)
 osandov#1  osq_lock (kernel/locking/osq_lock.c:137:10)
 osandov#2  mutex_optimistic_spin (kernel/locking/mutex.c:667:8)
 osandov#3  __mutex_lock_common (kernel/locking/mutex.c:971:6)
 osandov#4  __mutex_lock (kernel/locking/mutex.c:1109:9)
 osandov#5  0xffffffffc02ad045
 ......................

call stack for pid: 251
 #0  __read_once_size (./include/linux/compiler.h:268:2)
 osandov#1  osq_lock (kernel/locking/osq_lock.c:137:10)
 osandov#2  mutex_optimistic_spin (kernel/locking/mutex.c:667:8)
 osandov#3  __mutex_lock_common (kernel/locking/mutex.c:971:6)
 osandov#4  __mutex_lock (kernel/locking/mutex.c:1109:9)
 osandov#5  0xffffffffc02ad045
 .....................

call stack for pid: 248
 #0  __read_once_size (./include/linux/compiler.h:268:2)
 osandov#1  osq_lock (kernel/locking/osq_lock.c:137:10)
 osandov#2  mutex_optimistic_spin (kernel/locking/mutex.c:667:8)
 osandov#3  __mutex_lock_common (kernel/locking/mutex.c:971:6)
 osandov#4  __mutex_lock (kernel/locking/mutex.c:1109:9)
 osandov#5  0xffffffffc02ad045
.....................

call stack for pid: 249
 #0  __read_once_size (./include/linux/compiler.h:268:2)
 osandov#1  arch_atomic64_read (./arch/x86/include/asm/atomic64_64.h:22:9)
 osandov#2  atomic64_read (./include/asm-generic/atomic-instrumented.h:837:9)
 osandov#3  atomic_long_read (./include/asm-generic/atomic-long.h:28:9)
 osandov#4  __mutex_owner (kernel/locking/mutex.c:75:32)
 osandov#5  mutex_spin_on_owner (kernel/locking/mutex.c:566:9)
 osandov#6  mutex_optimistic_spin (kernel/locking/mutex.c:683:8)
 osandov#7  __mutex_lock_common (kernel/locking/mutex.c:971:6)
 osandov#8  __mutex_lock (kernel/locking/mutex.c:1109:9)
 osandov#9  0xffffffffc02ad045
....................

Signed-off-by: Imran Khan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant