x86/fpu: Use vmemdup_user() in xstateregs_set()
PATCH v2 1/1 x86/fpu: Use vmemdup_user() in xstateregs_set()
Replace the open-coded vmalloc() and copy_from_user() with vmemdup_user() to simplify xstateregs_set().
vmemdup_user() returns an ERR_PTR() on failure, preserving the existing -ENOMEM and -EFAULT error codes. Since vmemdup_user() is backed by kvmalloc(), use kvfree() to free the buffer instead.
Return early on error and drop the obsolete out label.
Performance should be similar, and xstateregs_set() is not a hot path. Page alignment should not matter because the buffer is only used as a memcpy() source in copy_uabi_from_kernel_to_xstate().
The allocation behavior differs because vmemdup_user() uses GFP_USER, which adds __GFP_HARDWALL and may affect cpuset allocations.
tools/testing/selftests/x86/avx_64 passed on an Alder Lake CPU.
---
Changes in v2:
- Add performance analysis and selftest result to the changelog (Ingo)
- v1: https://lore.kernel.org/r/[email protected]/
--- arch/x86/kernel/fpu/regset.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/regset.c +6 -11
@@ -3,7 +3,8 @@* FPU register's regset abstraction, for ptrace, core dumps, etc. */ #include <linux/sched/task_stack.h> -#include <linux/vmalloc.h> +#include <linux/slab.h> +#include <linux/string.h> #include <asm/fpu/api.h> #include <asm/fpu/signal.h>
@@ -157,21 +158,15 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,return -EFAULT ;if (!kbuf) { - tmpbuf =vmalloc (count); -if (!tmpbuf) -return -ENOMEM ; - -if (copy_from_user (tmpbuf, ubuf, count)) { - ret = -EFAULT ; - goto out; - } + tmpbuf =vmemdup_user (ubuf, count); +if (IS_ERR (tmpbuf)) +return PTR_ERR (tmpbuf); }fpu_force_restore (fpu); ret =copy_uabi_from_kernel_to_xstate (fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru); -out: -vfree (tmpbuf); +kvfree (tmpbuf);return ret; }
Quoted 18 lines · Thorsten Blum
Replace the open-coded
vmalloc()andcopy_from_user()withvmemdup_user()to simplifyxstateregs_set().
vmemdup_user()returns anERR_PTR()on failure, preserving the existing-ENOMEMand-EFAULTerror codes. Sincevmemdup_user()is backed bykvmalloc(), usekvfree()to free the buffer instead.Return early on error and drop the obsolete out label.
Performance should be similar, and
xstateregs_set()is not a hot path. Page alignment should not matter because the buffer is only used as amemcpy()source incopy_uabi_from_kernel_to_xstate().The allocation behavior differs because
vmemdup_user()usesGFP_USER, which adds__GFP_HARDWALLand may affect cpuset allocations.
tools/testing/selftests/x86/avx_64passed on an Alder Lake CPU.
Quoted 8 lines
Signed-off-by Thorsten Blum <[email protected]>---
Changes in v2:
- Add performance analysis and selftest result to the changelog (Ingo)
- v1: https://lore.kernel.org/r/[email protected]/--- arch/x86/kernel/fpu/regset.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
Hi Ingo,
Could you please give this another look now that I've added the performance analysis to the changelog? Is there anything else missing?
Thanks,
Thorsten
Quoted 40 lines
arch/x86/kernel/fpu/regset.c +6
-11@@ -3,7 +3,8 @@* FPU register's regset abstraction, for ptrace, core dumps, etc. */ #include <linux/sched/task_stack.h> -#include <linux/vmalloc.h> +#include <linux/slab.h> +#include <linux/string.h> #include <asm/fpu/api.h> #include <asm/fpu/signal.h> @@ -157,21 +158,15 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,return -EFAULT ;if (!kbuf) { - tmpbuf =vmalloc (count); -if (!tmpbuf) -return -ENOMEM ; - -if (copy_from_user (tmpbuf, ubuf, count)) { - ret = -EFAULT ; - goto out; - } + tmpbuf =vmemdup_user (ubuf, count); +if (IS_ERR (tmpbuf)) +return PTR_ERR (tmpbuf); }fpu_force_restore (fpu); ret =copy_uabi_from_kernel_to_xstate (fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru); -out: -vfree (tmpbuf); +kvfree (tmpbuf);return ret; }