Fundamentals 4 min read

Linus Torvalds Merges Patch Optimizing copy_from_user() by Removing barrier_nospec() for a 2.6% Performance Boost

Linus Torvalds merged a kernel patch that modifies the 64‑bit copy_from_user() implementation to avoid the slow barrier_nospec() call, achieving a 2.6% performance improvement in the "will it scale" benchmark and will be included in the upcoming Linux 6.12 stable release.

IT Services Circle
IT Services Circle
IT Services Circle
Linus Torvalds Merges Patch Optimizing copy_from_user() by Removing barrier_nospec() for a 2.6% Performance Boost

Linus Torvalds merged a patch on Wednesday that, by changing only a few lines of code, delivers a 2.6% performance gain in Intel's "will it scale" per‑thread‑ops benchmark.

The patch’s goal is to avoid using barrier_nospec() in the 64‑bit copy_from_user() function, which copies data from user space to kernel space.

Linus explained that the barrier_nospec() call is slow and that, when an address is invalid, pointer masking can force the user pointer to all‑ones.

“The barrier_nospec() function in 64‑bit copy_from_user() runs very slowly. If the address is invalid, pointer masking can be used to force the user pointer to all 1s.”

The overall code changes are shown below:

@@ -38,6 +38,7 @@
#else
#define can_do_masked_user_access() 0
#define masked_user_access_begin(src) NULL
+ #define mask_user_address(src) (src)
#endif
/*
@@ -159,19 +160,27 @@ _inline_copy_from_user(void *to, const void __user *from, unsigned long n)
{
unsigned long res = n;
might_fault();
-  if (!should_fail_usercopy() && likely(access_ok(from, n))) {
+  if (should_fail_usercopy())
+    goto fail;
+  if (can_do_masked_user_access())
+    from = mask_user_address(from);
+  else {
+    if (!access_ok(from, n))
+      goto fail;
/*
* Ensure that bad access_ok() speculation will not
* lead to nasty side effects *after* the copy is
* finished:
*/
barrier_nospec();
-    instrument_copy_from_user_before(to, from, n);
-    res = raw_copy_from_user(to, from, n);
-    instrument_copy_from_user_after(to, from, n, res);
}
-  if (unlikely(res))
-    memset(to + (n - res), 0, res);
+  instrument_copy_from_user_before(to, from, n);
+  res = raw_copy_from_user(to, from, n);
+  instrument_copy_from_user_after(to, from, n, res);
+  if (likely(!res))
+    return 0;
+fail:
+  memset(to + (n - res), 0, res);
return res;
}
extern __must_check unsigned long

Although it is unclear how this tiny optimization will affect other workloads, any reduction of barrier_nospec overhead is beneficial for the kernel.

The patch has been merged into the Linux kernel Git repository and will become part of the Linux 6.12 stable release slated for late November.

For more details, see the official commit announcement: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0fc810ae3ae110f9e2fcccce80fc8c8d62f97907

performanceKernellinuxcopy_from_userPatch
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.