Kernel panic when using pmap_extract_and_hold() to check physical-to-virtual address mapping

From: Friedrich Doku <friedrichdoku2030_at_u.northwestern.edu>
Date: Wed, 19 Nov 2025 17:51:02 UTC
Hello,

We want to see if a specific physical address is mapped into the virtual
address space of a user space process. We are trying to do this from the
kernel, but we are running into issues with trying to use pmap_extract,
specifically we get the following kernel panic:

panic: mtx_lock() of spin mutex (invalid)

The pmap pointer comes from:

   1. pfind(target_pid) - gets the process structure
   2. p->p_vmspace - gets the vmspace from the process
   3. vmspace_pmap(p->p_vmspace) - extracts the pmap from vmspace

Then I'm iterating through vm_map entries with VM_MAP_ENTRY_FOREACH() and
calling pmap_extract_and_hold(pmap, va, VM_PROT_READ) for each virtual
address.

The crash happens when calling pmap_extract_and_hold(). I suspect it's
trying to acquire pmap locks that conflict with something.

I'm trying to find which virtual address maps to a given physical address
in a user space process. I'm doing this from a kernel module via sysctl
handler.

Best,
Friedy