From nobody Thu Apr 2 20:14:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 D7BC33A9002 for ; Thu, 26 Mar 2026 18:17:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774549074; cv=none; b=CUK+mvCmZ64HhiZL1YOQch91WI5yY51ZtJusBJtK6OSvoRUAP5+n5EKKdebUSHUzBDApePYBXdnc0MjnRwqmW+O9bXyZI6WuCHlXyXCsTChbLPa5C6jLLICdry6NF8hz58wXgg4J/rtmO5lkkAMolnLnZBr8giC8yhdTqBlYgkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774549074; c=relaxed/simple; bh=DmwSj31cAqA8fbZFdzkQEEIbD8DZuAQfK9Iu7JvJLPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LzAQUCaUP0QxhoL4YbiNWJ2MBSGVr4bGUG4+nCIPUgiAlKSWQ2gOVaFfGjArwEY3QhzGs1a5e6su2dg8jQ0ht1WX2iSGaElVpy8ZWwWYP78UTUYQ4r+0HEgicAUmJG5B0AN6ayyd2Wit/i8ZPEGDRYqfFXjtSjs7jXEwoEu4+Dw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ODH0o7lI; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=CADiJYEG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ODH0o7lI"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="CADiJYEG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774549068; 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: in-reply-to:in-reply-to:references:references; bh=piO1nL/pS5Yr4rvE1o1jj+wOaY1ORSkY1ednhzkEqCo=; b=ODH0o7lIzpQ6DEXeR61GZlUyqO50Su2fal0BMJEx6ODLFK/3GrgRXERwWMDhtMsZu8q0Mt psGsc6NVagN7bnb0KDG7AGvULWUNuIL9S1iFks02ckMRI9RmyU3NH3zvw/sUBAvW26du/W dpeQEVLV9sRk8zaLqVangf+2hQmOKrU= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-231-JGXSWQraNF2t3Z5V2ww97A-1; Thu, 26 Mar 2026 14:17:47 -0400 X-MC-Unique: JGXSWQraNF2t3Z5V2ww97A-1 X-Mimecast-MFC-AGG-ID: JGXSWQraNF2t3Z5V2ww97A_1774549066 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4836abfc742so10050205e9.0 for ; Thu, 26 Mar 2026 11:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1774549065; x=1775153865; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=piO1nL/pS5Yr4rvE1o1jj+wOaY1ORSkY1ednhzkEqCo=; b=CADiJYEGAzoys8nmTekiDrMuOMRExeF9Zz/83VjrdihvVgokndZEXaAk3yiUMXciuv 909brPO/j63dU5K/no7mG0k9gd70FpNgVbmHt+kf6oXFskyCPStgUzrZw6u7JIJ8kfhL gap9G7kvB+1gGz0B2LPMohABKE/XjR1Msc31G263MqfTwpl8WNK7YE5aCrpu15serXj1 SJt/64yBbAe5OMrwqTbPBmQ43xqOkc99RzE8EjoKRTYkEc5Tl9WDUjH3o7fULHqeJzsE icmv6xYQDFVQnKtVD4+CnzKrYCXlT3pia3mY9KpdY8QclvSN5ZEX1h6wL7QhcMD/Xl7Q y7TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774549065; x=1775153865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=piO1nL/pS5Yr4rvE1o1jj+wOaY1ORSkY1ednhzkEqCo=; b=lQqPdwi0bOm0u3AVTJVxIDxK2X8ZCoRxtXBKjSsBtf8u60B6KVhtMg5bsb6yCUj1ey /sVBZXRjU7okL9hi4n3hqh7jrZhH1lPGUh8wg8KGhKezU1aHmBJzcj4bh7ws855T2XY4 oJz1Btfy07fImpqdfxd26qtyQDG1zs/5lE6ngf/pCSLUsrq42fSXzaiQVAp0ldPaRx/L Wsj0f6cNhwJj7Wo32rrBjEQmlYxqWCAgTknnyx6OHGokxENhrKYr2Dbc6eVWgSqoxsb5 WLIiMeQT6aRaYRd6sdmWVu5M1HmKhz5QMRsm4gcNhoyTR1eCaH91LYwLfb5izPIj4oH9 SsCQ== X-Gm-Message-State: AOJu0YzGo37AIpfuNdBKvrmRfBXeMkZZQpjxouS5jhfEhNVvynEKd5uC 5RT67Z1Fv6CnfnES7zqVG1Y5yKH8VLHbkRRJ+4snS22eC74FPHQaxbFS8AzDXZwVK/o6n/AjLJP wTi1qTJcS73N/c5VLXNhADorZygNR2NjX8z1xR0Bkf4x4mUAs0ot/r/iN8A734QIdhlyZolPoHS RBCgov7xTz5a4nTIujFif/iUjZFX6Jn2udBc1/n9NyCUxgUNbEHA== X-Gm-Gg: ATEYQzzwLwlG1c/cjaxORxqakikENsbdLwjcrvO/pfghBnjiT6Ddrh3C+Vcl56+53Pv t1KZjjUco+xUP4zzWUj86948QnwYbeEzLR/tblB9kEyvCaUqZJWXRCYesTcOH95OELhkUe8+5r3 ONc89dCv18Cfpt9m1fF24B/9Rgi/lTTEiWMCWXSSW8SBANcSRXwyPrFNv+UCp+Kefm8sooLTnWG WsDI/fhW+tmAg2AJPkU9swnICWITQYXNOe860z63V+RFnP9dRJNkAaMdVotBulIGL5q5mn048BM 8x+LUn3bxNn+bwckD7RnccJ/9RptEVqnlboSp7DZ9eGNjIAggwIj0t9StVDxVZNRFIJ8KozWEPb qC+4eVkQbCAJF4C2gu30fxna6NAFkpQiKgEh1f62hdCdPSrDKNrO6Vv79/pGE1qHfkDGzj2ak8u FXVhRsXoe+koIhhsNZi/TqWPN/ X-Received: by 2002:a05:600c:4254:b0:487:2092:b2e0 with SMTP id 5b1f17b1804b1-4872092b38bmr32523225e9.1.1774549064808; Thu, 26 Mar 2026 11:17:44 -0700 (PDT) X-Received: by 2002:a05:600c:4254:b0:487:2092:b2e0 with SMTP id 5b1f17b1804b1-4872092b38bmr32522575e9.1.1774549063983; Thu, 26 Mar 2026 11:17:43 -0700 (PDT) Received: from [192.168.10.48] ([151.49.85.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48722c9f58fsm75858805e9.11.2026.03.26.11.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2026 11:17:42 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Jon Kohler , Nikunj A Dadhania , Amit Shah , Sean Christopherson , Marcelo Tosatti Subject: [PATCH 07/24] KVM: x86/mmu: rename and clarify BYTE_MASK Date: Thu, 26 Mar 2026 19:17:05 +0100 Message-ID: <20260326181723.218115-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260326181723.218115-1-pbonzini@redhat.com> References: <20260326181723.218115-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The BYTE_MASK macro is the central point of the black magic in update_permission_bitmask(). Rename it to something that relates to how it is used, and add a comment explaining how it works. Using shifts instead of powers of two was actually suggested by David Hildenbrand back in 2017 for clarity[1] but I evidently forgot his suggestion when applying to kvm.git. [1] https://lore.kernel.org/kvm/e4b5df86-31ae-2f4e-0666-393753e256df@redhat= .com/ Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 55 ++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index b922a8b00057..170952a840db 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5517,29 +5517,53 @@ reset_ept_shadow_zero_bits_mask(struct kvm_mmu *con= text, bool execonly) max_huge_page_level); } =20 -#define BYTE_MASK(access) \ - ((1 & (access) ? 2 : 0) | \ - (2 & (access) ? 4 : 0) | \ - (3 & (access) ? 8 : 0) | \ - (4 & (access) ? 16 : 0) | \ - (5 & (access) ? 32 : 0) | \ - (6 & (access) ? 64 : 0) | \ - (7 & (access) ? 128 : 0)) - +/* + * Build a mask with all combinations of PTE access rights that + * include the given access bit. The mask can be queried with + * "mask & (1 << access)", where access is a combination of + * ACC_* bits. + * + * By mixing and matching multiple masks returned by ACC_BITS_MASK, + * update_permission_bitmask() builds what is effectively a + * two-dimensional array of bools. The second dimension is + * provided by individual bits of permissions[pfec >> 1], and + * logical &, | and ~ operations operate on all the 8 possible + * combinations of ACC_* bits. + */ +#define ACC_BITS_MASK(access) \ + ((1 & (access) ? 1 << 1 : 0) | \ + (2 & (access) ? 1 << 2 : 0) | \ + (3 & (access) ? 1 << 3 : 0) | \ + (4 & (access) ? 1 << 4 : 0) | \ + (5 & (access) ? 1 << 5 : 0) | \ + (6 & (access) ? 1 << 6 : 0) | \ + (7 & (access) ? 1 << 7 : 0)) =20 static void update_permission_bitmask(struct kvm_mmu *mmu, bool ept) { unsigned byte; =20 - const u8 x =3D BYTE_MASK(ACC_EXEC_MASK); - const u8 w =3D BYTE_MASK(ACC_WRITE_MASK); - const u8 u =3D BYTE_MASK(ACC_USER_MASK); + const u8 x =3D ACC_BITS_MASK(ACC_EXEC_MASK); + const u8 w =3D ACC_BITS_MASK(ACC_WRITE_MASK); + const u8 u =3D ACC_BITS_MASK(ACC_USER_MASK); =20 bool cr4_smep =3D is_cr4_smep(mmu); bool cr4_smap =3D is_cr4_smap(mmu); bool cr0_wp =3D is_cr0_wp(mmu); bool efer_nx =3D is_efer_nx(mmu); =20 + /* + * In hardware, page fault error codes are generated (as the name + * suggests) on any kind of page fault. permission_fault() and + * paging_tmpl.h already use the same bits after a successful page + * table walk, to indicate the kind of access being performed. + * + * However, PFERR_PRESENT_MASK and PFERR_RSVD_MASK are never set here, + * exactly because the page walk is successful. PFERR_PRESENT_MASK is + * removed by the shift, while PFERR_RSVD_MASK is repurposed in + * permission_fault() to indicate accesses that are *not* subject to + * SMAP restrictions. + */ for (byte =3D 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) { unsigned pfec =3D byte << 1; =20 @@ -5586,10 +5610,9 @@ static void update_permission_bitmask(struct kvm_mmu= *mmu, bool ept) * - The access is supervisor mode * - If implicit supervisor access or X86_EFLAGS_AC is clear * - * Here, we cover the first four conditions. - * The fifth is computed dynamically in permission_fault(); - * PFERR_RSVD_MASK bit will be set in PFEC if the access is - * *not* subject to SMAP restrictions. + * Here, we cover the first four conditions. The fifth + * is computed dynamically in permission_fault() and + * communicated by setting PFERR_RSVD_MASK. */ if (cr4_smap) smapf =3D (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf; --=20 2.53.0