Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5654312pxb; Mon, 28 Mar 2022 15:33:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPvoDp5wMlX5nGZM2zaZSW8T67/xVKhWdLgd57rU0UlRkd6y23tAdsmkEUyHRpHl+yJENU X-Received: by 2002:a05:6122:88a:b0:332:6a9c:902b with SMTP id 10-20020a056122088a00b003326a9c902bmr10259562vkf.40.1648506828937; Mon, 28 Mar 2022 15:33:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648506828; cv=none; d=google.com; s=arc-20160816; b=BvbN+u5Utkkeu+9DzA0ehXu9r4NQhQByi5YR6UMG2s6GnE0E0CY+7EMMyO8QFAUS5E dasVNbs3m9+mDZCYTKIA2NpUJf6s3pcXmnj8A5M0V8UnAIaaYgkC7GXBRcZf8/5qc04U SLhYV8chuZFu60roXqg0MK4mmhWLQqCRjnbPdaZUusSs0+rNXTs6GcvClpUn8gYR231H oHmaj7+CtRJpB081/e4iMFHob++E4zWYbupSgr/O6i7IoRhB2J3pPprKgs7N0tnIujub PXOO0usaC3s2ZbUmUEHcXIpCRRHKvJvkL0aACfI+y/PTZERwLDf2tqMblT4+RX0h658u 7fcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=7jiQi0QWhnZscVOAnkzHFyqDUSzKwYEoPntptVT51RM=; b=fdll5kPf8gyC9GAeLNRNfd+Qme2YazSdKHpB4mOJQ2oyqrzMZbuGYxZbK+l6S9y3h7 Z6NEYcLW/7TKkPvk7b4gqEeW3WrwLFVpEj8SUKBtw6qQzfK+fnymXNE449dhal/NUv1O GGpo5cgFoS1LdHzm0qEt3N0Hbn/rsdBCVWUz/JDemQeJ761XE2pbVD/3EVscHLlFF5NH YN10qIqsnhFAbPKis4qIB214/HKBSO8QOA5bGIhxgd19fYmiptvHWmth0Ox57egSFUfs X3OJyQJtK4ykqHNWc+tLws2uE4HQ1tAW1H+wZB3ojLl5Dxe84AKi8NATEcwBoE9nYlWn q4cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=KeEUa5rB; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=HwiNzNBP; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id r38-20020a9f3629000000b00359ab89cad1si3393088uad.176.2022.03.28.15.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 15:33:48 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=KeEUa5rB; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=HwiNzNBP; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DFCC91D97DF; Mon, 28 Mar 2022 14:44:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245291AbiC1SpK (ORCPT + 99 others); Mon, 28 Mar 2022 14:45:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234951AbiC1SpI (ORCPT ); Mon, 28 Mar 2022 14:45:08 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 929CEFFF for ; Mon, 28 Mar 2022 11:43:26 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1648493002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7jiQi0QWhnZscVOAnkzHFyqDUSzKwYEoPntptVT51RM=; b=KeEUa5rBvEGAFy7q3gZ42iI6G2809OCplOwFSOJVH9kY1w0/TPxPcfEKK5HefLK9d0U8iq LOwPdQ6DFTOJkoKxHcZj/WhhSSgGHnAOGvy30VNoanrIpaYRbp8PQ1G4sWNA/A0vecoWHJ whCXVTyWioyJXUEay2RJBrvmbiarqJ/IY5eRAPy4ABW+nccmCgO3dRyraCnnCLo2riBSop 4FY/OF0OalEpfR2wnWcgsgqJbU11ZAF3g25xnZvqZpxzYhdjIce8gXVFwoszj0KTW/Z4Nn r917FIwe8g9JFyi/6mAyLdFsAHRgN840N8Zx61fkHZ3ZSqC6OQ3Nkl+bRcnLzA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1648493002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7jiQi0QWhnZscVOAnkzHFyqDUSzKwYEoPntptVT51RM=; b=HwiNzNBPBdXBPflq4oWoQqsbiVumUsrRE0vc8bSlo741ZrxyJ9uYRaF29GrwtnZerRX2Lb rPItsfG0/vrydYBg== To: LKML Cc: x86@kernel.org, "Chang S. Bae" Subject: [patch V2 7/7] x86/fpu/xstate: Consolidate size calculations In-Reply-To: <20220324134623.736990161@linutronix.de> References: <20220324134548.432837672@linutronix.de> <20220324134623.736990161@linutronix.de> Date: Mon, 28 Mar 2022 20:43:21 +0200 Message-ID: <87o81pgbp2.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use the offset calculation to do the size calculation which avoids yet another series of CPUID instructions for each invocation. Signed-off-by: Thomas Gleixner --- V2: Fix the FP/SSE only case which missed to take the xstate header into account. Reported-by: kernel test robot --- arch/x86/kernel/fpu/xstate.c | 49 +++++++------------------------------------ 1 file changed, 8 insertions(+), 41 deletions(-) --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -385,25 +385,6 @@ static void __init setup_init_fpu_buf(vo fxsave(&init_fpstate.regs.fxsave); } -static int xfeature_uncompacted_offset(int xfeature_nr) -{ - u32 eax, ebx, ecx, edx; - - /* - * Only XSAVES supports supervisor states and it uses compacted - * format. Checking a supervisor state's uncompacted offset is - * an error. - */ - if (XFEATURE_MASK_SUPERVISOR_ALL & BIT_ULL(xfeature_nr)) { - WARN_ONCE(1, "No fixed offset for xstate %d\n", xfeature_nr); - return -1; - } - - CHECK_XFEATURE(xfeature_nr); - cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx); - return ebx; -} - int xfeature_size(int xfeature_nr) { u32 eax, ebx, ecx, edx; @@ -581,29 +562,15 @@ static bool __init check_xstate_against_ static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted) { - unsigned int size = FXSAVE_SIZE + XSAVE_HDR_SIZE; - int i; + unsigned int topmost = fls64(xfeatures) - 1; + unsigned int offset = xstate_offsets[topmost]; - for_each_extended_xfeature(i, xfeatures) { - /* Align from the end of the previous feature */ - if (xfeature_is_aligned64(i)) - size = ALIGN(size, 64); - /* - * In compacted format the enabled features are packed, - * i.e. disabled features do not occupy space. - * - * In non-compacted format the offsets are fixed and - * disabled states still occupy space in the memory buffer. - */ - if (!compacted) - size = xfeature_uncompacted_offset(i); - /* - * Add the feature size even for non-compacted format - * to make the end result correct - */ - size += xfeature_size(i); - } - return size; + if (topmost <= XFEATURE_SSE) + return sizeof(struct xregs_state); + + if (compacted) + offset = xfeature_get_offset(xfeatures, topmost); + return offset + xstate_sizes[topmost]; } /*