iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Will Deacon <will@kernel.org>
Cc: Lu Baolu <baolu.lu@linux.intel.com>,
	Joerg Roedel <joerg.roedel@amd.com>,
	Kevin Tian <kevin.tian@intel.com>,
	kernel test robot <lkp@intel.com>,
	patches@lists.linux.dev, Tina Zhang <tina.zhang@intel.com>
Subject: [PATCH] iommupt: Avoid a compiler bug with sw_bit
Date: Wed, 26 Nov 2025 15:21:27 -0400	[thread overview]
Message-ID: <0-v1-2e3b97d2d9b3+13c-iommupt_swbit_bugon_jgg@nvidia.com> (raw)

gcc 13, in some cases, gets confused if the __builtin_constant_p() is
inside the switch. It thinks that bitnr can have the value max+1 and
fails. Lift the check outside the switch to avoid it.

Fixes: ef7bfe5bbffd ("iommupt/x86: Support SW bits and permit PT_FEAT_DMA_INCOHERENT")
Fixes: 5448c1558f60 ("iommupt: Add the Intel VT-d second stage page table format")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202511242012.I7g504Ab-lkp@intel.com/
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/iommu/generic_pt/fmt/vtdss.h  | 8 ++++----
 drivers/iommu/generic_pt/fmt/x86_64.h | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

Seems to need a specific randconfig to fail, my normal ones don't, nor has 0
day found this on its randconfigs till now.

diff --git a/drivers/iommu/generic_pt/fmt/vtdss.h b/drivers/iommu/generic_pt/fmt/vtdss.h
index d9774848eb6f7f..50ffed9d0e508f 100644
--- a/drivers/iommu/generic_pt/fmt/vtdss.h
+++ b/drivers/iommu/generic_pt/fmt/vtdss.h
@@ -174,6 +174,9 @@ static inline unsigned int vtdss_pt_max_sw_bit(struct pt_common *common)
 
 static inline u64 vtdss_pt_sw_bit(unsigned int bitnr)
 {
+	if (__builtin_constant_p(bitnr) && bitnr > 10)
+		BUILD_BUG();
+
 	/* Bits marked Ignored in the specification */
 	switch (bitnr) {
 	case 0:
@@ -184,10 +187,7 @@ static inline u64 vtdss_pt_sw_bit(unsigned int bitnr)
 		return BIT_ULL(63);
 	/* Some bits in 9-3 are available in some entries */
 	default:
-		if (__builtin_constant_p(bitnr))
-			BUILD_BUG();
-		else
-			PT_WARN_ON(true);
+		PT_WARN_ON(true);
 		return 0;
 	}
 }
diff --git a/drivers/iommu/generic_pt/fmt/x86_64.h b/drivers/iommu/generic_pt/fmt/x86_64.h
index a86353f1481e5c..507abf2c934ccb 100644
--- a/drivers/iommu/generic_pt/fmt/x86_64.h
+++ b/drivers/iommu/generic_pt/fmt/x86_64.h
@@ -175,6 +175,9 @@ static inline unsigned int x86_64_pt_max_sw_bit(struct pt_common *common)
 
 static inline u64 x86_64_pt_sw_bit(unsigned int bitnr)
 {
+	if (__builtin_constant_p(bitnr) && bitnr > 12)
+		BUILD_BUG();
+
 	/* Bits marked Ignored/AVL in the specification */
 	switch (bitnr) {
 	case 0:
@@ -185,10 +188,7 @@ static inline u64 x86_64_pt_sw_bit(unsigned int bitnr)
 		return BIT_ULL((bitnr - 2) + 52);
 	/* Some bits in 8,6,4,3 are available in some entries */
 	default:
-		if (__builtin_constant_p(bitnr))
-			BUILD_BUG();
-		else
-			PT_WARN_ON(true);
+		PT_WARN_ON(true);
 		return 0;
 	}
 }

base-commit: 93d0836a0e54f678a3814cf07fa1339fe6cce4d7
-- 
2.43.0


             reply	other threads:[~2025-11-26 19:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-26 19:21 Jason Gunthorpe [this message]
2025-11-27 13:16 ` [PATCH] iommupt: Avoid a compiler bug with sw_bit Joerg Roedel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0-v1-2e3b97d2d9b3+13c-iommupt_swbit_bugon_jgg@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=joerg.roedel@amd.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=lkp@intel.com \
    --cc=patches@lists.linux.dev \
    --cc=robin.murphy@arm.com \
    --cc=tina.zhang@intel.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).