Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1548054pxf; Fri, 26 Mar 2021 09:36:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwukAcjUpbFUmEJBtAi95vgSb+MHXvF97dwqOy7YlwPesjBhxRWQDaUvWvU+ahFTC+BHZhO X-Received: by 2002:a05:6402:1d92:: with SMTP id dk18mr15973321edb.161.1616776577024; Fri, 26 Mar 2021 09:36:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616776577; cv=none; d=google.com; s=arc-20160816; b=J3JLA4YuHE8G5P0HSTNtzsHlFqNn4Mym4hr1/rKHSYM5rjUvYf1xu6YPaUEjnFBew7 XXPpBKDDx0GGKdCbkm/DXaPYfoq0RpKxgPmoYWa8QGxGsUrPhUj558M4Wu/cLzArkLp3 3UtwzMnD9ZESPZwqCIeUjYJmIp74n/JGjYLudRWU/+ISyq6kotvfUDRFtyWpjccAygN7 kQUqab4/IIH8CdAIPQlNgre8RyyjOnyv5NkCINdYPSGyL1QE5XpWgbEu5ZBo0XzqbX6x 6RLKzOI/iadDmOZNCkcA9ckaf/R9JcsSClkFiEkRBtN6njv2L7lCBOA6uHWgWlaOy+P2 HlVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=rr+9Nmmd9Te7QF0knbE1qBYAGxHmadtN1JXjZbkqZxk=; b=qQuc4ZhliItsOEkXrgolqJZ+xAH4lPVUCDFlPNbW9moCZ5Df+LqAO2bo4aXi8V40TS pGpwE6fbCMmjhz4qqmYiDZJn1FFlpEetNkGKHoz2f6kHVMIAFtZNd+fDSMFQLBj3/V7K k55dQu3YlH84xXvaXc8DcxQPxsLGS/K9iTZaVO4C6GIKbWRwFbrlHmhTmTPVMNd3VnlL VdItLcstXC3P/56r2hiSdrXFYsBVhv9DhRiSBFwBDDOV5rMSNVQfklzU8zicM4dbhSYU nfCjOfGVdcbRwJ+hl761qt8BebXEp5LrPZOnWPHAcl8I2QEuQiSAiimf7EkSW2RxU/Jo MgjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TE3vVFc1; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc19si6575387edb.127.2021.03.26.09.35.53; Fri, 26 Mar 2021 09:36:17 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=TE3vVFc1; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230106AbhCZQe5 (ORCPT + 99 others); Fri, 26 Mar 2021 12:34:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229871AbhCZQen (ORCPT ); Fri, 26 Mar 2021 12:34:43 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A3AEC0613AA for ; Fri, 26 Mar 2021 09:34:43 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id y1so8057301ljm.10 for ; Fri, 26 Mar 2021 09:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rr+9Nmmd9Te7QF0knbE1qBYAGxHmadtN1JXjZbkqZxk=; b=TE3vVFc1TEOPacZvHQoUhpJ4RFZnJn0IqpEwL6eK2EYUf4O44EU6XvJ6AbWS08goSx P5xMh/HVT/bm7S033DJcbue+y7VFTW/OrEofqktcL9dcVNyARonQGWf9KVlzGuHB23Cp +C8zocaS/guougrUBHPdafevJ0NI7KIcpMVctP5yC0RBuIkZGSGT6nrWdZc7GhLnbAY+ zi3lSBJEEqbb8sEQo8cuqAlEDwwUrgM0hHfGFMXF92oaCXNmFEssywe6ExHY3vwcfidR b6KK3mYvWGLDrSWnTw2ebo4jjVVr3pABFWGe9QYlpU1EJHdcOS9ONUB7AxsM1qhxpOyV niNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rr+9Nmmd9Te7QF0knbE1qBYAGxHmadtN1JXjZbkqZxk=; b=OjRVCCZjwz+B4dj3txjvhMNfDJfEb+G95h3XuxjBluQBoUiXKtSeYg5xxAIJjeA9Md I6O/yi2Z6gvbg1ixfnaL357ohxafEoKa6dX+zlQAu3N3OqL05ZXRiLpa2eqd7pP6YGvA RmNLJ5MCzpSwtELSqS8wJNLulSvMOQn1p5mdlPWtyvodlv9vL7svQOOt/aZkpykffal9 w3nslsC/exnzgQHqBuUsKRQBsOJZgC1UrohyFNIwS8EaLawUG1HU8704hDU+VSv4Mmv/ RZ82MDd3yY/4V1iEpZatU4gPTuYx24QIUnj3bZyzFYKXgGoxjwnfdfLDTbelLFiNbKrM we9A== X-Gm-Message-State: AOAM530Y/10EtZZfETH+EvfGBeFa70/48M7RDzIQbaWmUQmqLdWF4La7 hUbzpF1khKV5AFqWSpRJ9nwDr0/m5QkQ/DtB1tYaKg== X-Received: by 2002:a2e:9bcd:: with SMTP id w13mr9378810ljj.43.1616776481296; Fri, 26 Mar 2021 09:34:41 -0700 (PDT) MIME-Version: 1.0 References: <20210221185637.19281-1-chang.seok.bae@intel.com> <20210221185637.19281-15-chang.seok.bae@intel.com> In-Reply-To: <20210221185637.19281-15-chang.seok.bae@intel.com> From: Jann Horn Date: Fri, 26 Mar 2021 17:34:14 +0100 Message-ID: Subject: Re: [PATCH v4 14/22] x86/fpu/xstate: Expand the xstate buffer on the first use of dynamic user state To: "Chang S. Bae" Cc: Borislav Petkov , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , "the arch/x86 maintainers" , Len Brown , Dave Hansen , jing2.liu@intel.com, "Ravi V. Shankar" , kernel list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Feb 21, 2021 at 7:56 PM Chang S. Bae wrote: > Intel's Extended Feature Disable (XFD) feature is an extension of the XSAVE > architecture. XFD allows the kernel to enable a feature state in XCR0 and > to receive a #NM trap when a task uses instructions accessing that state. > In this way, Linux can defer allocating the large XSAVE buffer until tasks > need it. > > XFD introduces two MSRs: IA32_XFD to enable/disable the feature and > IA32_XFD_ERR to assist the #NM trap handler. Both use the same > state-component bitmap format, used by XCR0. > > Use this hardware capability to find the right time to expand the xstate > buffer. Introduce two sets of helper functions for that: > > 1. The first set is primarily for interacting with the XFD hardware: > xdisable_setbits() > xdisable_getbits() > xdisable_switch() > > 2. The second set is for managing the first-use status and handling #NM > trap: > xfirstuse_enabled() > xfirstuse_not_detected() > > The #NM handler induces the xstate buffer expansion to save the first-used > states. [...] > diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c > index 7f5aec758f0e..821a7f408ad4 100644 > --- a/arch/x86/kernel/traps.c > +++ b/arch/x86/kernel/traps.c [...] > +static __always_inline bool handle_xfirstuse_event(struct fpu *fpu) > +{ > + bool handled = false; > + u64 event_mask; [...] > + if (alloc_xstate_buffer(fpu, event_mask)) > + return handled; [...] > +} > + > DEFINE_IDTENTRY(exc_device_not_available) > { > unsigned long cr0 = read_cr0(); > > + if (handle_xfirstuse_event(¤t->thread.fpu)) > + return; What happens if handle_xfirstuse_event() fails because vmalloc() failed in alloc_xstate_buffer()? I think that should probably kill the task with something like force_sig() - but as far as I can tell, at the moment, it will instead end up at die(), which should only be used for kernel bugs. > + > #ifdef CONFIG_MATH_EMULATION > if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) { > struct math_emu_info info = { }; > -- > 2.17.1 > >