From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A2BA145A1F for ; Wed, 5 Aug 2026 14:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785938644; cv=none; b=JMFPg1e4xxfqOPNwwqg8iZdpnzwt0fK+25iT+5jzFloj3E6hatWXg/I6xelwK3HBPo+tbd8boV8551gzyr2O3le0rIpHTS7/nMDdYGbvS3REkV4wAtsPNJaEP1GFIWjUDST4bGVN1SYoiwlPWLvw+wHtu4S/suejwlWuNa08m7U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785938644; c=relaxed/simple; bh=J9vevVK59M30gKddYbTn+jIAlRJ0bmS1ltk61GV8o8c=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=cKwetWBCh9cAWqRysTvVBKiiroUhJKxqp3F/xMb/w5hxmHOLVaHJE/FFyQVkwMRhB0KdxzWHc92wpmKRw1UWHZP1vcUoAdhciaSMOWBgo60CDR8vbbNq3XY35OvhcVlv88Pqa082/368qC9EK6F/AaO8sVKJrGAZ1v83n9nw9Nc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=CCwmOWpy; arc=none smtp.client-ip=91.218.175.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="CCwmOWpy" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1785938639; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3edvd3dy8zAvDPiRD9lc2wWrXHckBePO/lOHEDKXTko=; b=CCwmOWpyKpqkSVk7yi3u1dr8+hXeNZV0eyFUIo9EjS+NF7L5U6pXs/R2PVcGW7j9njlVWp 1SVVRhnnILVB4IOOB89/hP4JEzIKDp0OeqS7kDFMGCnu2lLKcl6ySxXUR+9rLTPBXI/0Ce GmAavqbT9lT6ozHmSntJ8v7XvV0LYnc= From: Thorsten Blum To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: Thorsten Blum , linux-kernel@vger.kernel.org Subject: [PATCH v2] x86/fpu: Use vmemdup_user() in xstateregs_set() Date: Wed, 5 Aug 2026 16:03:45 +0200 Message-ID: <20260805140346.149561-2-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2159; i=thorsten.blum@linux.dev; h=from:subject; bh=J9vevVK59M30gKddYbTn+jIAlRJ0bmS1ltk61GV8o8c=; b=owGbwMvMwCUWt7pQ4caZUj3G02pJDFnFTodmlvtE83hOvR/7ryy+pTk4sitK+ObSiblBO3R4l Saf6PrQUcrCIMbFICumyPJg1o8ZvqU1lZtMInbCzGFlAhnCwMUpABPx8WZk2L56cc3qK6kL53Dm vDnlrWE9V0xn286qXf8WCV3Vm3dsETfD/6Sm9NgKkUnHVy3d5rlhy307ztKie12b3pyXmjfn1ZE ZrLwA X-Developer-Key: i=thorsten.blum@linux.dev; a=openpgp; fpr=1D60735E8AEF3BE473B69D84733678FD8DFEEAD4 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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. Signed-off-by: Thorsten Blum --- Changes in v2: - Add performance analysis and selftest result to the changelog (Ingo) - v1: https://lore.kernel.org/r/20260720195534.70111-3-thorsten.blum@linux.dev/ --- arch/x86/kernel/fpu/regset.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 0986c2200adc..8dc9de732c78 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -3,7 +3,8 @@ * FPU register's regset abstraction, for ptrace, core dumps, etc. */ #include -#include +#include +#include #include #include @@ -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; }