In the Linux kernel, the following vulnerability has been resolved:
powerpc/setmemory: Avoid spinlock recursion in changepage_attr()
Commit 1f9ad21c3b38 ("powerpc/mm: Implement setmemory() routines") included a spinlock() to changepageattr() in order to safely perform the three step operations. But then commit 9f7853d7609d ("powerpc/mm: Fix setmemory*() against concurrent accesses") modify it to use pte_update() and do the operation safely against concurrent access.
In the meantime, Maxime reported some spinlock recursion.
[ 15.351649] BUG: spinlock recursion on CPU#0, kworker/0:2/217 [ 15.357540] lock: initmm+0x3c/0x420, .magic: dead4ead, .owner: kworker/0:2/217, .ownercpu: 0 [ 15.366563] CPU: 0 PID: 217 Comm: kworker/0:2 Not tainted 5.15.0+ #523 [ 15.373350] Workqueue: events dofreeinit [ 15.377615] Call Trace: [ 15.380232] [e4105ac0] [800946a4] dorawspinlock+0xf8/0x120 (unreliable) [ 15.387340] [e4105ae0] [8001f4ec] changepageattr+0x40/0x1d4 [ 15.393413] [e4105b10] [801424e0] _applytopagerange+0x164/0x310 [ 15.400009] [e4105b60] [80169620] freepcpprepare+0x1e4/0x4a0 [ 15.406045] [e4105ba0] [8016c5a0] freeunrefpage+0x40/0x2b8 [ 15.411979] [e4105be0] [8018724c] kasandepopulatevmallocpte+0x6c/0x94 [ 15.418989] [e4105c00] [801424e0] _applytopagerange+0x164/0x310 [ 15.425451] [e4105c50] [80187834] kasanreleasevmalloc+0xbc/0x134 [ 15.431898] [e4105c70] [8015f7a8] _purgevmaparealazy+0x4e4/0xdd8 [ 15.438560] [e4105d30] [80160d10] vmunmapaliases.part.0+0x17c/0x24c [ 15.445283] [e4105d60] [801642d0] _vunmap+0x2f0/0x5c8 [ 15.450684] [e4105db0] [800e32d0] dofreeinit+0x68/0x94 [ 15.456181] [e4105dd0] [8005d094] processonework+0x4bc/0x7b8 [ 15.462283] [e4105e90] [8005d614] workerthread+0x284/0x6e8 [ 15.468227] [e4105f00] [8006aaec] kthread+0x1f0/0x210 [ 15.473489] [e4105f40] [80017148] retfromkernelthread+0x14/0x1c
Remove the read / modify / write sequence to make the operation atomic and remove the spinlock() in changepage_attr().
To do the operation atomically, we can't use pte modification helpers anymore. Because all platforms have different combination of bits, it is not easy to use those bits directly. But all have the PAGEKERNEL_{RO/ROX/RW/RWX} set of flags. All we need it to compare two sets to know which bits are set or cleared.
For instance, by comparing PAGEKERNELROX and _PAGEKERNEL_RO you know which bit gets cleared and which bit get set when changing exec permission.