Kernel hacking: Pagefault on sx_xlock/sx_xunlock (linuxkpi mutex_lock/mutex_unlock)

From: Neel Chauhan <nc_at_FreeBSD.org>
Date: Thu, 10 Jun 2021 22:36:47 UTC
Hi freebsd-current@,

Keep in mind that I am a Ports committer, not a src one. However I have 
very recently gotten into hacking on drm-kmod and linuxkpi.

I am attempting to update drm-kmod to Linux 5.7, mainly to support my 
Intel TigerLake laptop, and I wanted something to do while I wait for 
GNOME dependencies to get updated in Ports (for GNOME 40).

When I kldload the i915kms module, I am getting a kernel panic. Namely a 
pagefault on sx_xlock/sx_xunlock (mutex_lock/mutex_unlock via linuxkpi) 
on both Intel WhiskeyLake (HP Spectre x360 13-ap0053dx) and TigerLake 
(HP Spectre x360 13t-aw200).

The respective trace is below (on Intel WhiskeyLake):

KDB: stack backtrace:
db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 
0xfffffe011359dde0
vpanic() at vpanic+0x181/frame 0xfffffe011359de30
panic() at panic+0x43/frame 0xfffffe011359de90
trap_fatal() at trap_fatal+0x387/frame 0xfffffe011359def0
trap_pfault() at trap_pfault+0x97/frame 0xfffffe011359df50
trap() at trap+0x294/frame 0xfffffe011359e060
calltrap() at calltrap+0x8/frame 0xfffffe011359e060
--- trap 0xc, rip = 0xffffffff80c224f7, rsp = 0xfffffe011359e130, rbp = 
0xfffffe011359e1c0 ---
_sx_xlock_hard() at _sx_xlock_hard+0x187/frame 0xfffffe011359e1c0
_sx_xlock() at _sx_xlock+0xba/frame 0xfffffe011359e200
intel_wakeref_wait_for_idle() at intel_wakeref_wait_for_idle+0xe5/frame 
0xfffffe011359e260
intel_engines_release() at intel_engines_release+0xab/frame 
0xfffffe011359e280
intel_gt_init() at intel_gt_init+0x151/frame 0xfffffe011359e330
i915_gem_init() at i915_gem_init+0x95/frame 0xfffffe011359e380
i915_driver_probe() at i915_driver_probe+0xecb/frame 0xfffffe011359e410
i915_pci_probe() at i915_pci_probe+0x3c/frame 0xfffffe011359e460
linux_pci_attach_device() at linux_pci_attach_device+0x2d6/frame 
0xfffffe011359e4b0
device_attach() at device_attach+0x3ca/frame 0xfffffe011359e4f0
device_probe_and_attach() at device_probe_and_attach+0x70/frame 
0xfffffe011359e520
bus_generic_driver_added() at bus_generic_driver_added+0x58/frame 
0xfffffe011359e540
devclass_driver_added() at devclass_driver_added+0x39/frame 
0xfffffe011359e580
devclass_add_driver() at devclass_add_driver+0x147/frame 
0xfffffe011359e5c0
_linux_pci_register_driver() at _linux_pci_register_driver+0xcf/frame 
0xfffffe011359e5f0
i915kms_evh() at i915kms_evh+0x44/frame 0xfffffe011359e600
module_register_init() at module_register_init+0xbd/frame 
0xfffffe011359e630
linker_load_module() at linker_load_module+0xc01/frame 
0xfffffe011359e940
kern_kldload() at kern_kldload+0x151/frame 0xfffffe011359e990
sys_kldload() at sys_kldload+0x5b/frame 0xfffffe011359e9c0
amd64_syscall() at amd64_syscall+0x12e/frame 0xfffffe011359eaf0
fast_syscall_common() at fast_syscall_common+0xf8/frame 
0xfffffe011359eaf0
--- syscall (304, FreeBSD ELF64, sys_kldload), rip = 0x8011a58fa, rsp = 
0x7fffffffe558, rbp = 0x7fffffffead0 ---
KDB: enter: panic
Uptime: 52m39s
Dumping 1132 out of 15985 MB:..2% (CTRL-C to abort) ..12% (CTRL-C to 
abort) ..22%..32%..41%..51%..61%..71%..81%..91%

__curthread () at /usr/src/sys/amd64/include/pcpu_aux.h:55
55		__asm("movq %%gs:%P1,%0" : "=r" (td) : "n" (offsetof(struct pcpu,
(kgdb) #0  __curthread () at /usr/src/sys/amd64/include/pcpu_aux.h:55
#1  doadump (textdump=textdump@entry=1)
     at /usr/src/sys/kern/kern_shutdown.c:399
#2  0xffffffff80c17830 in kern_reboot (howto=260)
     at /usr/src/sys/kern/kern_shutdown.c:486
#3  0xffffffff80c17c90 in vpanic (fmt=<optimized out>, ap=<optimized 
out>)
     at /usr/src/sys/kern/kern_shutdown.c:919
#4  0xffffffff80c179e3 in panic (fmt=<unavailable>)
     at /usr/src/sys/kern/kern_shutdown.c:843
#5  0xffffffff810d3df7 in trap_fatal (frame=0xfffffe011359e070, 
eva=1088)
     at /usr/src/sys/amd64/amd64/trap.c:943
#6  0xffffffff810d3e97 in trap_pfault 
(frame=frame@entry=0xfffffe011359e070,
     usermode=false, signo=<optimized out>, signo@entry=0x0,
     ucode=<optimized out>, ucode@entry=0x0)
     at /usr/src/sys/amd64/amd64/trap.c:760
#7  0xffffffff810d3464 in trap (frame=0xfffffe011359e070)
     at /usr/src/sys/amd64/amd64/trap.c:438
#8  <signal handler called>
#9  _sx_xlock_hard (sx=sx@entry=0xfffffe013de90188, x=<optimized out>,
     opts=<optimized out>, opts@entry=0, file=<optimized out>,
     file@entry=0xffffffff831af870 
"/root/drm-kmod/drivers/gpu/drm/i915/intel_wakeref.h", line=<optimized 
out>, line@entry=195)
     at /usr/src/sys/kern/kern_sx.c:686
#10 0xffffffff80c2230a in _sx_xlock (sx=sx@entry=0xfffffe013de90188,
     opts=opts@entry=0,
     file=0xffffffff831af870 
"/root/drm-kmod/drivers/gpu/drm/i915/intel_wakeref.h", 
line=line@entry=195) at /usr/src/sys/kern/kern_sx.c:332
#11 0xffffffff8310cbf5 in intel_wakeref_unlock_wait 
(wf=0xfffffe013de90180)
     at /root/drm-kmod/drivers/gpu/drm/i915/intel_wakeref.h:195
#12 intel_wakeref_wait_for_idle (wf=wf@entry=0xfffffe013de90180)
     at /root/drm-kmod/drivers/gpu/drm/i915/intel_wakeref.c:122
#13 0xffffffff8315793b in intel_engines_release (
     gt=gt@entry=0xfffffe013821a7e0)
     at /root/drm-kmod/drivers/gpu/drm/i915/gt/intel_engine_cs.c:418
#14 0xffffffff8315fa91 in intel_gt_init (gt=gt@entry=0xfffffe013821a7e0)
     at /root/drm-kmod/drivers/gpu/drm/i915/gt/intel_gt.c:624
#15 0xffffffff83078da5 in i915_gem_init (
     dev_priv=dev_priv@entry=0xfffffe0138215000)
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_gem.c:1129
#16 0xffffffff830755eb in i915_driver_modeset_probe 
(i915=0xfffffe0138215000)
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_drv.c:302
#17 i915_driver_probe (pdev=pdev@entry=0xfffff802462f6480,
     ent=ent@entry=0xffffffff831c5f90 <pciidlist+6656>)
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_drv.c:1022
#18 0xffffffff8308747c in i915_pci_probe (pdev=0xfffff802462f6480,
     ent=0xffffffff831c5f90 <pciidlist+6656>)
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_pci.c:977
#19 0xffffffff80e7ddf6 in linux_pci_attach_device (dev=<optimized out>,
     pdrv=0xffffffff831df6b8 <i915_pci_driver>,
     id=0xffffffff831c5f90 <pciidlist+6656>, pdev=0xfffff802462f6480)
     at /usr/src/sys/compat/linuxkpi/common/src/linux_pci.c:371
#20 0xffffffff80c5303a in DEVICE_ATTACH (dev=0xfffff80003c57600)
     at ./device_if.h:195
#21 device_attach (dev=dev@entry=0xfffff80003c57600)
     at /usr/src/sys/kern/subr_bus.c:2996
#22 0xffffffff80c52be0 in device_probe_and_attach (
     dev=dev@entry=0xfffff80003c57600) at 
/usr/src/sys/kern/subr_bus.c:2953
#23 0xffffffff80c54ab8 in bus_generic_driver_added 
(dev=0xfffff80003c5db00,
     driver=<optimized out>) at /usr/src/sys/kern/subr_bus.c:4136
#24 0xffffffff80c50839 in BUS_DRIVER_ADDED (_dev=0xfffff80003c5db00,
     _driver=0xffffffff831df700 <i915_pci_driver+72>) at ./bus_if.h:210
#25 devclass_driver_added (dc=dc@entry=0xfffff80003de0300,
     driver=driver@entry=0xffffffff831df700 <i915_pci_driver+72>)
     at /usr/src/sys/kern/subr_bus.c:1079
#26 0xffffffff80c50777 in devclass_add_driver (
     dc=dc@entry=0xfffff80003de0300,
     driver=driver@entry=0xffffffff831df700 <i915_pci_driver+72>,
     pass=pass@entry=2147483647,
     dcp=dcp@entry=0xffffffff831df730 <i915_pci_driver+120>)
     at /usr/src/sys/kern/subr_bus.c:1155
#27 0xffffffff80e7e7bf in _linux_pci_register_driver (pdrv=<optimized 
out>,
     pdrv@entry=0xfffff802b18dee00, dc=0xfffff80003de0300)
     at /usr/src/sys/compat/linuxkpi/common/src/linux_pci.c:535
#28 0xffffffff80e7e9aa in linux_pci_register_drm_driver (pdrv=<optimized 
out>)
     at /usr/src/sys/compat/linuxkpi/common/src/linux_pci.c:592
#29 0xffffffff83087404 in i915_init ()
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_pci.c:1045
#30 i915kms_evh (m=<optimized out>, e=<optimized out>, a=<optimized 
out>)
     at /root/drm-kmod/drivers/gpu/drm/i915/i915_pci.c:1086
#31 0xffffffff80bf2b1d in module_register_init (
     arg=0xffffffff831df6a0 <i915kms_md>)
     at /usr/src/sys/kern/kern_module.c:123
#32 0xffffffff80be2941 in linker_file_sysinit (lf=0xfffff802b1370180)
     at /usr/src/sys/kern/kern_linker.c:236
#33 linker_load_file (filename=<optimized out>, result=<optimized out>)
     at /usr/src/sys/kern/kern_linker.c:461
#34 linker_load_module (kldname=<optimized out>, kldname@entry=0x0,
     modname=0xfffff80003d3b800 "i915kms",
     modname@entry=0xfffffe0116f271e0 "\300\060\255\"",
     parent=parent@entry=0x0, verinfo=verinfo@entry=0x0, lfpp=<optimized 
out>,
     lfpp@entry=0xfffffe011359e960) at 
/usr/src/sys/kern/kern_linker.c:2179
#35 0xffffffff80be43c1 in kern_kldload (td=<optimized out>,
     td@entry=0xfffffe0116f271e0, file=<optimized out>,
     file@entry=0xfffff80003d3b800 "i915kms",
     fileid=fileid@entry=0xfffffe011359e9a4)
     at /usr/src/sys/kern/kern_linker.c:1138
#36 0xffffffff80be454b in sys_kldload (td=0xfffffe0116f271e0,
     uap=<optimized out>) at /usr/src/sys/kern/kern_linker.c:1161
#37 0xffffffff810d47be in syscallenter (td=<optimized out>)
     at /usr/src/sys/amd64/amd64/../../kern/subr_syscall.c:189
#38 amd64_syscall (td=0xfffffe0116f271e0, traced=0)
     at /usr/src/sys/amd64/amd64/trap.c:1184
#39 <signal handler called>
#40 0x00000008011a58fa in ?? ()

Does anybody know why this is happening and how I would fix/resolve it? 
I'm really more-or-less a kernel newbie (yes, I did some driver and 
TCP/IP hacking before but not to this scale).

The GitHub branch is here: 
https://github.com/neelchauhan/drm-kmod/tree/5.7-wip

Note: If you wish to run the branch, you need an up-to-date CURRENT and 
also need to apply these patches to /usr/src:

  * https://reviews.freebsd.org/D30705
  * https://reviews.freebsd.org/D30706

-Neel (nc@)