Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1498770pxv; Sat, 10 Jul 2021 06:11:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysdHvL7eJC5So44k31c7NCTlIoXI8Sh9e3Vm5lyDiKgaC+75PxFblMoR/RDCTq6corTXtD X-Received: by 2002:a92:364f:: with SMTP id d15mr31078172ilf.26.1625922682651; Sat, 10 Jul 2021 06:11:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625922682; cv=none; d=google.com; s=arc-20160816; b=zWZagDoEKfLKhkWuNQPPDAWrEqG29XOWwgWCJxj/4lFiGPNMWGBSjxB8SI2QrPmsZw 1nh6RfqqufB8jDvJanObJCKMP39MhP4bCrvtZaR6eM3vqm/AvxeqMekvE5bK9TtEjT9U 2C1AlrPBIaHWAvX285AS5i298pyD04ITtlfztnfR8B3xkmYl46/G+FnE8slzZcAqg10R nrwsF2EvSP+/x97bk1rn8ET+MkYYFZB56lcRkymTkrSywEVog7w5AL5WMAEJRi99DmPi 3iwkW6PEH6Ez/i6UfUgdR2zV4GeovqmC+BFLMYHY36rIcKJFPZrVNYNnpaZAMd53UvRY ea9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=qcntPc8r+kjENepKJBSF/Iu4DgFlwmW3l3qqgUZASos=; b=SuyNohX9766szKshSe5M6aQmhFkVfF2em1m6mp2xAzTtGpO3YW+btB4UcJxM8eDCFf AXJ7czBgKLq8QOdWSHFenzH6wg2F/YKv4QIofIgwPED7ZDUtrp2XeGvAt+M8jyEayzIX 3h7JLatiYeLB6L1A5sPipqHjeSJM77aWHr01/AS/geFu4VQVmsy8fGqx5RohRl8WjYbi 0qG9i4caC0CzWiAOMc8o+MmXa0VSlpinUXVf4C6/uESktvH0AHUgJBceXZj755M15OU6 iJvn97d6v20SzWAg2+fCBxTvehHGSKdHucMwPNQ2+L/qh915kjzjvVN6hjNdTXW0HceA Bwmg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c26si10417816ioo.54.2021.07.10.06.10.40; Sat, 10 Jul 2021 06:11:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232674AbhGJNMP (ORCPT + 99 others); Sat, 10 Jul 2021 09:12:15 -0400 Received: from mga09.intel.com ([134.134.136.24]:49951 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231327AbhGJNLz (ORCPT ); Sat, 10 Jul 2021 09:11:55 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10040"; a="209793913" X-IronPort-AV: E=Sophos;i="5.84,229,1620716400"; d="scan'208";a="209793913" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jul 2021 06:09:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,229,1620716400"; d="scan'208";a="488742387" Received: from chang-linux-3.sc.intel.com ([172.25.66.175]) by FMSMGA003.fm.intel.com with ESMTP; 10 Jul 2021 06:09:08 -0700 From: "Chang S. Bae" To: bp@suse.de, luto@kernel.org, tglx@linutronix.de, mingo@kernel.org, x86@kernel.org Cc: len.brown@intel.com, dave.hansen@intel.com, thiago.macieira@intel.com, jing2.liu@intel.com, ravi.v.shankar@intel.com, linux-kernel@vger.kernel.org, chang.seok.bae@intel.com Subject: [PATCH v7 13/26] x86/fpu/xstate: Support ptracer-induced XSTATE buffer expansion Date: Sat, 10 Jul 2021 06:03:00 -0700 Message-Id: <20210710130313.5072-14-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210710130313.5072-1-chang.seok.bae@intel.com> References: <20210710130313.5072-1-chang.seok.bae@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ptrace() may update XSTATE data before the target task has taken an XFD fault and expanded the XSTATE buffer. Detect this case and allocate a sufficient buffer to support the request. Also, disable the (now unnecessary) associated first-use fault. Signed-off-by: Chang S. Bae Reviewed-by: Len Brown Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org --- Changes from v5: * Adjusted to use 'tmpbuf' for the new base code. Changes from v4: * Improved the condition check for the expansion. * Simplified the XSTATE_BV retrieval. * Updated the code comment. Changes from v3: * Removed 'no functional changes' in the changelog. (Borislav Petkov) Changes from v2: * Updated the changelog with task->fpu removed. (Borislav Petkov) * Updated the code comments. --- arch/x86/kernel/fpu/regset.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 73d7d7b489fe..244e672c3e3d 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -163,6 +163,30 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, } } + /* + * When a ptracer attempts to write any dynamic user state in the + * target buffer but not sufficiently allocated, it dynamically + * expands the buffer. + * + * Check if the expansion is possibly needed. + */ + if (xfeatures_mask_user_dynamic && + ((fpu->state_mask & xfeatures_mask_user_dynamic) != xfeatures_mask_user_dynamic)) { + u64 state_mask; + + /* Retrieve XSTATE_BV. */ + memcpy(&state_mask, (kbuf ?: tmpbuf) + offsetof(struct xregs_state, header), + sizeof(u64)); + + /* Expand the xstate buffer based on the XSTATE_BV. */ + state_mask &= xfeatures_mask_user_dynamic; + if (state_mask) { + ret = alloc_xstate_buffer(fpu, state_mask); + if (ret) + goto out; + } + } + fpu_force_restore(fpu); ret = copy_uabi_from_kernel_to_xstate(fpu, kbuf ?: tmpbuf); -- 2.17.1