Message ID | 20230117100821.10116-1-suravee.suthikulpanit@amd.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1672658wrn; Tue, 17 Jan 2023 02:16:49 -0800 (PST) X-Google-Smtp-Source: AMrXdXsNcnKvoGfUJsKDIhLQQhJSplqbj1IECzdzO+ybkr0mCMNNqMDur6VYWYfBYe65grbhPBia X-Received: by 2002:a05:6a21:3a96:b0:ad:e5e8:cfe8 with SMTP id zv22-20020a056a213a9600b000ade5e8cfe8mr2634239pzb.48.1673950608902; Tue, 17 Jan 2023 02:16:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1673950608; cv=pass; d=google.com; s=arc-20160816; b=JtrSML8B49i0BokI6q3O4LCEZ2xMR6ZY2KBmnzAf+UqL8pki9DyL6bz+90CbAWoU8i nB7AlaTdxBRBXEyHtJm82KpTChReb11+TSyB2DUKZJYh8mOKOQhj23vQCHqEi82VGjyt MXn45sEYjfFec2LdPPMkPt1SNSqzTXVhU3jgNh3+iiR9rA4WQBPaznOGORBGbvsp5Xi+ yRXJX6MnIqHUi1UuPiFcvRmcwScGxp0IDFDMWUP/IIahZ+wWiwdIyRr8MsQHhHSwu9Is hFT1jgmrv91woXNk9cJaVmDPVopSZih83d4es9gbaxRge7wEE8L1naPV/iR2jXI+Z7bO NQPw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=364Y0Tn4vWEFu3Lz8Rz9DEx5PLzJloKI1hF4jmspYFc=; b=Ae2gA8G3uzIzSPR5x6yxXn+tPxVhNfgp5AwS0M0igdQr0LL/mD0fbKkLv8g8iBfjZ5 wCu0bQ3Leg/JAAPnpPEvqK1aOxu2zuNNc/kdgTheTIluuII0LtwDYB47Z0FL68mQjSdJ /zJDMQUBmnO2/gjgE86fgBfajldrnkYHMGK3o8UpPbZF5j/0M5nYSJJBbw8gEWwnC6pf Tzp2QDGyonpqcNzrT6zwFqNR9rq94nGLmR0f7bTsSCXzNqua7VOZlKyOtsLyuWWJZedL x6enltHKoTOTT0LcAIjkQNAGlPbMsJgVnmp8PdCWI0D58fiSX/Cal8l26PazrjJmYFuA FrJA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=fxCNwU1z; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m13-20020a63ed4d000000b004a58ff2e054si32951763pgk.834.2023.01.17.02.16.36; Tue, 17 Jan 2023 02:16:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=fxCNwU1z; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236492AbjAQKIv (ORCPT <rfc822;pfffrao@gmail.com> + 99 others); Tue, 17 Jan 2023 05:08:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236598AbjAQKIi (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 17 Jan 2023 05:08:38 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2044.outbound.protection.outlook.com [40.107.92.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 166642DE45; Tue, 17 Jan 2023 02:08:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YMgpebInUd6BKcW50Oaxz1ZdZQIcmyNZTvQN52+dfA6rVd6oSmZSAh7UKlzoRVx49LUrVQcp40b0atJ4I/9jDp44k1ubMwlayd+3Z7qAa0fQ04PqpjZP48ipHcCOp7LeHKLS3AoCNDQk4i00lzxq5m4X3psqZH0juZZtD6zr+gzAhC+tTaXZ/vJAOFB/9gg0ntTHkFkE2oeFivacJYn3Q7EwK5viEvsuRhcQWMaOzyTTBnYahYvbjW/42YIedA+Txdm+wZgx2+30T1IBQLP8mOcnN03Q+prMC6BHzYK+WyllnBUUM0bSE/DOn40udd99UeTAh4KB4TmhCYO/2su4bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=364Y0Tn4vWEFu3Lz8Rz9DEx5PLzJloKI1hF4jmspYFc=; b=Oz/m2/Pd05h9wzWyK/b4Yf0zsMlNlzfATrJAt2CbbEZJNGsh9SunvpuYXm4dOrIDSauWNDeMEp76QmXAyVSp0N2WcghwkSBs8PbxrHaT7nRvsinLA+wcMTV3BdsawflecI4ERD+BcTkpdh2ei5gz1i6wLKQhVZhkqA2y4HgGn5OMzd1xL/QcPrqUj4Fh2gdQUgJJCwr3CgFKkLjpQ9DxyZQm3Cff8pxQ2tZQ/UFbCTBp0B9diqSWxCPTGpY1n0u7EZT2zXp1rPXWw8VWFxtrBnwJ6DZL0X3tUTPcZ1NqQY2gWw8yURFlEPeM5mXansFLxRRoAXyqEUgDS1Q6vLaWXQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=364Y0Tn4vWEFu3Lz8Rz9DEx5PLzJloKI1hF4jmspYFc=; b=fxCNwU1zSA4BO45VC381TlF8h/fz2g0NmxoHjGP/FTPx4/0rVXrAtSVXOfBtzI0pc2+clt9NbQQBJFm7UpUDX8fqyceNYYYtCcksRHe8LIiYABXxhQOm4/7Qtqr4ail2PNp9cdvGpzx0+lfZ+xgQuGu0/hVvu8kBAhK+0Fx+2Yk= Received: from DM6PR01CA0017.prod.exchangelabs.com (2603:10b6:5:296::22) by PH7PR12MB6394.namprd12.prod.outlook.com (2603:10b6:510:1fe::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.18; Tue, 17 Jan 2023 10:08:34 +0000 Received: from DM6NAM11FT070.eop-nam11.prod.protection.outlook.com (2603:10b6:5:296:cafe::f5) by DM6PR01CA0017.outlook.office365.com (2603:10b6:5:296::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.19 via Frontend Transport; Tue, 17 Jan 2023 10:08:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT070.mail.protection.outlook.com (10.13.173.51) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6002.13 via Frontend Transport; Tue, 17 Jan 2023 10:08:34 +0000 Received: from ruby-95f9host.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 17 Jan 2023 04:08:32 -0600 From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> To: <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org> CC: <seanjc@google.com>, <alejandro.j.jimenez@oracle.com>, <mlevitsk@redhat.com>, <pbonzini@redhat.com>, <jon.grimm@amd.com>, <vasant.hegde@amd.com>, <kishon.vijayabraham@amd.com>, Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Subject: [PATCH] KVM: SVM: Modify AVIC GATag to support max number of 512 vCPUs Date: Tue, 17 Jan 2023 04:08:21 -0600 Message-ID: <20230117100821.10116-1-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT070:EE_|PH7PR12MB6394:EE_ X-MS-Office365-Filtering-Correlation-Id: 725c395e-4498-4e94-4841-08daf872cb3c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yBF5H9shXHi2YS52+rDtiHq4fGRi1eZrPlh/08uwq7rJ0wMLVvbjb9f1dc5BHTAM59mXNkMnsdjmw5LDkXM6gnufJ6wh6GN1xKrljotKnFuFWHLeZ+LX1TzF93rekpM/SaWZxuxyW5uOw5Aq1/dChIXmbUcSZ9VHKIjQ1SeQrGmodSgvEyq6/NsHoF18g27uZCy4ud24f9m4uYpAHPbbdCXfNOyXUS7z5vb0fcZgfOjzAt90P5zJr/rT4ipHm+/BIzcGxC2cii5PisVZzNQDWG27EZef2otG7w/BTOtFQ0mM3PvYtluONWDwyg5w3wYA98kKDGrLxWC2dnGbVvMc1NufHMjtAEBQnmQFTFlf7nmq6PrBewX4+Yu8yJEGBti3dDMoUv6V10GbI1AjK6eVuZSx/wZRbtOW9wHc0k6IpI8K10GJXO3J6StyIT9FpXt8rkqL6m7NM3Q1aZWzWyqgyLOA6H2ex5I8xhNUQNzPh0/qdDK1ieX6gmBvyiTlYP8LdVNqR1GCteelcHF/KpKDJpN3XNnLNiNQ2q++RKjki81g0IDAKuUR9A9/ffqSB1gxkY8RGCf4hQsozTQYeoNxdLZcTjXnB9HLYvtooXVE11y3Bmr/f4VI1QW5jHyrazB2b4eJFv13lXw+qmQcfP/POoMR1X25B1LO3B2JJ53LgkSCpyAXDCFu3nr4lhLXfLd81WfySprhojjgydmP0+gmnJx82r8gW/aY+DbZuFP4zCM= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199015)(36840700001)(40470700004)(46966006)(36756003)(356005)(8676002)(70206006)(70586007)(110136005)(4326008)(83380400001)(54906003)(41300700001)(40460700003)(82740400003)(86362001)(36860700001)(7696005)(26005)(186003)(6666004)(81166007)(316002)(478600001)(1076003)(44832011)(2906002)(426003)(5660300002)(8936002)(40480700001)(2616005)(82310400005)(336012)(16526019)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jan 2023 10:08:34.5407 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 725c395e-4498-4e94-4841-08daf872cb3c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT070.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6394 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755264433169317154?= X-GMAIL-MSGID: =?utf-8?q?1755264433169317154?= |
Series |
KVM: SVM: Modify AVIC GATag to support max number of 512 vCPUs
|
|
Commit Message
Suravee Suthikulpanit
Jan. 17, 2023, 10:08 a.m. UTC
AVIC GATag is managed by the SVM driver, and is used by the IOMMU driver
to program the AMD IOMMU IRTE to provide reference back to SVM in case
the IOMMU cannot inject interrupt to the non-running vCPU. In such case,
the IOMMU hardware notify the IOMMU driver by creating GALog entry with
the corresponded GATag. The IOMMU driver processes the GALog entry and
notifies SVM to schedule in the target vCPU.
Currently, SVM uses 8-bit vCPU ID and 24-bit VM ID to encode 32-bit GATag.
Since x2AVIC supports upto 512 vCPUs, it requires to use at least 9-bit
vCPU ID.
Therefore, modify the GATag enconding to use the number of bits required
to support the maximum vCPUs.
Reported-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
arch/x86/include/asm/svm.h | 3 ++-
arch/x86/kvm/svm/avic.c | 9 ++++-----
2 files changed, 6 insertions(+), 6 deletions(-)
Comments
Hi Sean / Paolo, Do you have any concerns or questions regarding this patch? Thanks, Suravee On 1/17/2023 5:08 PM, Suravee Suthikulpanit wrote: > AVIC GATag is managed by the SVM driver, and is used by the IOMMU driver > to program the AMD IOMMU IRTE to provide reference back to SVM in case > the IOMMU cannot inject interrupt to the non-running vCPU. In such case, > the IOMMU hardware notify the IOMMU driver by creating GALog entry with > the corresponded GATag. The IOMMU driver processes the GALog entry and > notifies SVM to schedule in the target vCPU. > > Currently, SVM uses 8-bit vCPU ID and 24-bit VM ID to encode 32-bit GATag. > Since x2AVIC supports upto 512 vCPUs, it requires to use at least 9-bit > vCPU ID. > > Therefore, modify the GATag enconding to use the number of bits required > to support the maximum vCPUs. > > Reported-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com> > Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> > --- > arch/x86/include/asm/svm.h | 3 ++- > arch/x86/kvm/svm/avic.c | 9 ++++----- > 2 files changed, 6 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h > index 0361626841bc..6738faf155e4 100644 > --- a/arch/x86/include/asm/svm.h > +++ b/arch/x86/include/asm/svm.h > @@ -256,7 +256,8 @@ enum avic_ipi_failure_cause { > AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, > }; > > -#define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(9, 0) > +#define AVIC_PHYSICAL_MAX_INDEX_BITS 9 > +#define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(AVIC_PHYSICAL_MAX_INDEX_BITS, 0) > > /* > * For AVIC, the max index allowed for physical APIC ID > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index dd0e41d454a7..b1f8f51bbbd9 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -28,16 +28,15 @@ > #include "svm.h" > > /* AVIC GATAG is encoded using VM and VCPU IDs */ > -#define AVIC_VCPU_ID_BITS 8 > -#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) > +#define AVIC_VCPU_ID_MASK ((1 << AVIC_PHYSICAL_MAX_INDEX_BITS) - 1) > > -#define AVIC_VM_ID_BITS 24 > +#define AVIC_VM_ID_BITS (32 - AVIC_PHYSICAL_MAX_INDEX_BITS) > #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) > #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) > > -#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ > +#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_PHYSICAL_MAX_INDEX_BITS) | \ > (y & AVIC_VCPU_ID_MASK)) > -#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) > +#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_PHYSICAL_MAX_INDEX_BITS) & AVIC_VM_ID_MASK) > #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) > > static bool force_avic;
TL;DR: I'm going to post v2 of this along with some other fixes+hardening. On Tue, Jan 17, 2023, Suravee Suthikulpanit wrote: > AVIC GATag is managed by the SVM driver, and is used by the IOMMU driver s/SVM driver/KVM > to program the AMD IOMMU IRTE to provide reference back to SVM in case s/SVM/KVM We do sometimes use "VMX" and "SVM" to refer to KVM, but usually only when differentiating betwen "KVM x86" and "KVM <vendor>". In most cases I don't think it would matter, but in this particular case, since the GATag is kinda sorta consumed by hardware, but IIUC is purely software-defined, knowing whether "SVM" means "KVM" or "SVM the architecture" is an important distinction. > the IOMMU cannot inject interrupt to the non-running vCPU. In such case, > the IOMMU hardware notify the IOMMU driver by creating GALog entry with Definitely a nit, but I would probably omit the info about the IOMMU driver. That information matters if someone is trying to understand _all_ of the pieces, but for this specific bug I think it just ends up introducing noise. > the corresponded GATag. The IOMMU driver processes the GALog entry and > notifies SVM to schedule in the target vCPU. > > Currently, SVM uses 8-bit vCPU ID and 24-bit VM ID to encode 32-bit GATag. > Since x2AVIC supports upto 512 vCPUs, it requires to use at least 9-bit "up to" Nit, x2AVIC doesn't "require" anything. What matters is what KVM allows. I get what you're saying, but I want to cleanly separate "what's allowed by the spec" and "what does KVM actually do". > vCPU ID. > > Therefore, modify the GATag enconding to use the number of bits required s/enconding/encoding > to support the maximum vCPUs. Thank you for the explanation of how this all works! IIUC, this is missing one key point though: the GATag is 100% software-defined and never interpreted by hardware. I.e. KVM can shove whatever it wants into the tag. And while I'm nitpicking, please lead with the "what". For the changelog as a whole, some maintainers/subsystems prefer leading with the "why", but I strongly prefer that changelogs state what the patch actually does and then provide the background/justification. Copy-pasting a prior copy-paste (I really need to save this as a Vim macro formletter) : To some extent, it's a personal preference, e.g. I : find it easier to understand the details (why something is a problem) if I have : the extra context of how a problem is fixed (or: what code was broken). : : But beyond personal preference, there are less subjective reasons for stating : what a patch does before diving into details. First and foremost, what code is : actually being changed is the most important information, and so that information : should be easy to find. Changelogs that bury the "what's actually changing" in a : one-liner after 3+ paragraphs of background make it very hard to find that information. : : Maybe for initial review one could argue that "what's the bug" is more important, : but for skimming logs and git archeology, the gory details matter less and less. : E.g. when doing a series of "git blame", the details of each change along the way : are useless, the details only matter for the culprit; I just want to quickly : determine whether or not a commit might be of interest. : : Another argument for stating "what's changing" first is that it's almost always : possible to state "what's changing" in a single sentence. Conversely, all but the : most simple bugs require multiple sentences or paragraphs to fully describe the : problem. If both the "what's changing" and "what's the bug" are super short then : the order doesn't matter. But if one is shorter (almost always the "what's changing), : then covering the shorter one first is advantageous because it's less of an : inconvenience for readers/reviewers that have a strict ordering preference. E.g. : having to skip one sentence to get to the stuff you care about is less painful than : me having to skip three paragraphs to get to the stuff that I care about. [*] https://lore.kernel.org/all/YurKx+gFAWPvj35L@google.com > Reported-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com> > Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> > --- > arch/x86/include/asm/svm.h | 3 ++- > arch/x86/kvm/svm/avic.c | 9 ++++----- > 2 files changed, 6 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h > index 0361626841bc..6738faf155e4 100644 > --- a/arch/x86/include/asm/svm.h > +++ b/arch/x86/include/asm/svm.h > @@ -256,7 +256,8 @@ enum avic_ipi_failure_cause { > AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, > }; > > -#define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(9, 0) Isn't the existing mask wrong? The high bit is inclusive, i.e. this is defining a mask of 10 bits, not 9. Actually, neither 10 nor 9 bits is correct if we are going by the most recent APM, i.e. the mask should be 8:0 if we want to tie this to what is actually defined in the architecture. All the addresses point to 4-Kbyte aligned data structures. Bits 11:0 are reserved (except for offset 0F8h) and should be set to zero. The lower 8 bits of offset 0F8h are used for the field AVIC_PHYSICAL_MAX_INDEX. VMRUN fails with #VMEXIT(VMEXIT_INVALID) if AVIC_PHYSICAL_MAX_INDEX is greater than 255 in xAVIC mode or greater than 511 in x2AVIC mode. Looking through the discussion history of the x2AVIC enabling, this appears to be an off-by-one goof, i.e. not a deliberate speculation on how bits 11:9 will be used. > +#define AVIC_PHYSICAL_MAX_INDEX_BITS 9 This name is misleading/wrong. As above, the high bit is inclusive. If we wanted to specify the high bit, it would be something like MAX_INDEX_MAX_BIT, which is pretty awful. Ha! We don't need a separate define. const_hweight.h provides compile-time hweight, a.k.a. popcount, macros. We can use that to compute the shift of the VM ID portion of the GATag, then we don't need to come up with a name. I'll post the below as v2 on top of the AVIC_PHYSICAL_MAX_INDEX_MASK fix. diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ca684979e90d..326341a22153 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -27,19 +27,29 @@ #include "irq.h" #include "svm.h" -/* AVIC GATAG is encoded using VM and VCPU IDs */ -#define AVIC_VCPU_ID_BITS 8 -#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) +/* + * Encode the arbitrary VM ID and the vCPU's default APIC ID, i.e the vCPU ID, + * into the GATag so that KVM can retrieve the correct vCPU from a GALog entry + * if an interrupt can't be delivered, e.g. because the vCPU isn't running. + * + * For the vCPU ID, use however many bits are currently allowed for the max + * guest physical APIC ID (limited by the size of the physical ID table), and + * use whatever bits remain to assign arbitrary AVIC IDs to VMs. Note, the + * size of the GATag is defined by hardware (32 bits), but is an opaque value + * as far as hardware is concerned. + */ +#define AVIC_VCPU_ID_MASK AVIC_PHYSICAL_MAX_INDEX_MASK -#define AVIC_VM_ID_BITS 24 -#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) -#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) +#define AVIC_VM_ID_SHIFT HWEIGHT32(AVIC_PHYSICAL_MAX_INDEX_MASK) +#define AVIC_VM_ID_MASK (GENMASK(31, AVIC_VM_ID_SHIFT) >> AVIC_VM_ID_SHIFT) -#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ +#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VM_ID_SHIFT) | \ (y & AVIC_VCPU_ID_MASK)) -#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) +#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VM_ID_SHIFT) & AVIC_VM_ID_MASK) #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) +static_assert(AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) == -1u); + static bool force_avic; module_param_unsafe(force_avic, bool, 0444);
diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 0361626841bc..6738faf155e4 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -256,7 +256,8 @@ enum avic_ipi_failure_cause { AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, }; -#define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(9, 0) +#define AVIC_PHYSICAL_MAX_INDEX_BITS 9 +#define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(AVIC_PHYSICAL_MAX_INDEX_BITS, 0) /* * For AVIC, the max index allowed for physical APIC ID diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index dd0e41d454a7..b1f8f51bbbd9 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -28,16 +28,15 @@ #include "svm.h" /* AVIC GATAG is encoded using VM and VCPU IDs */ -#define AVIC_VCPU_ID_BITS 8 -#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) +#define AVIC_VCPU_ID_MASK ((1 << AVIC_PHYSICAL_MAX_INDEX_BITS) - 1) -#define AVIC_VM_ID_BITS 24 +#define AVIC_VM_ID_BITS (32 - AVIC_PHYSICAL_MAX_INDEX_BITS) #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) -#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ +#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_PHYSICAL_MAX_INDEX_BITS) | \ (y & AVIC_VCPU_ID_MASK)) -#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) +#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_PHYSICAL_MAX_INDEX_BITS) & AVIC_VM_ID_MASK) #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) static bool force_avic;