Merged yesterday to the Linux 6.19 Git codebase was the “core/uaccess” pull that introduces new scoped user-mode access with auto-cleanup functionality. This can reduce the number of speculation barriers encountered when needing to access user-mode memory and thereby avoiding some of the performance penalties incurred by speculation barriers.
Intel Fellow Thomas Gleixner pursued this scoped user access functionality for the Linux kernel in an effort to avoid the need for some speculation barriers to cut-down on the performance overhead in the kernel’s hot code paths.
Gleixner explained in the core/uaccess pull request of this new kernel functionality:
“Scoped user mode access with auto cleanup
Access to user mode memory can be required in hot code paths, but if it has to be done with user controlled pointers, the access is shielded with a speculation barrier, so that the CPU cannot speculate around the address range check. Those speculation barriers impact performance quite significantly. This can be avoided by “masking” the provided pointer so it is guaranteed to be in the valid user memory access range and otherwise to point to a guaranteed unpopulated address space. This has to be done without branches so it creates an address dependency for the access, which the CPU cannot speculate ahead.
This results in repeating and error prone programming patterns:
if (can_do_masked_user_access())
from = masked_user_read_access_begin((from));
else if (!user_read_access_begin(from, sizeof(*from)))
return -EFAULT;
unsafe_get_user(val, from, Efault);
user_read_access_end();
return 0;
Efault:
user_read_access_end();
return -EFAULT;which can be replaced with scopes and automatic cleanup:
scoped_user_read_access(from, Efault)
unsafe_get_user(val, from, Efault);
return 0;
Efault:
return -EFAULT;”
The kernel’s x86 futex and select code are among the users adapted to using scoped user access as part of this merge.
