Linux IOMMU Development
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Tina Zhang <tina.zhang@intel.com>, iommu@lists.linux.dev
Cc: baolu.lu@linux.intel.com
Subject: Re: [PATCH 4/4] iommu/vt-d: Replace BUG()/BUG_ON() with WARN_ON/WARN_ON_ONCE()
Date: Wed, 29 Mar 2023 21:40:10 +0800	[thread overview]
Message-ID: <2bc952b2-311a-cac2-d9ba-223a124bdf74@linux.intel.com> (raw)
In-Reply-To: <20230329124654.2698853-5-tina.zhang@intel.com>

On 2023/3/29 20:46, Tina Zhang wrote:
> Remove legacy BUG()/BUG_ON() and turn them into WARN_ON/WARN_ON_ONCE()
> with errors.

Yes. WARN() is better than BUG().

Best regards,
baolu

> 
> Signed-off-by: Tina Zhang <tina.zhang@intel.com>
> ---
>   drivers/iommu/intel/dmar.c  |  6 ++++--
>   drivers/iommu/intel/iommu.c | 39 ++++++++++++++++++++++++-------------
>   2 files changed, 29 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
> index 7c77a59889ea..39b0bad000ea 100644
> --- a/drivers/iommu/intel/dmar.c
> +++ b/drivers/iommu/intel/dmar.c
> @@ -127,7 +127,8 @@ dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
>   	struct pci_dev *tmp;
>   	struct dmar_pci_notify_info *info;
>   
> -	BUG_ON(dev->is_virtfn);
> +	if (WARN_ON(dev->is_virtfn))
> +		return NULL;
>   
>   	/*
>   	 * Ignore devices that have a domain number higher than what can
> @@ -264,7 +265,8 @@ int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
>   						   get_device(dev));
>   				return 1;
>   			}
> -		BUG_ON(i >= devices_cnt);
> +		if (WARN_ON_ONCE(i >= devices_cnt))
> +			return -EINVAL;
>   	}
>   
>   	return 0;
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 286bb17973a4..20f7a9b3a6a7 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -391,6 +391,14 @@ static inline int domain_pfn_supported(struct dmar_domain *domain,
>   	return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
>   }
>   
> +static inline bool domain_pfn_range_valid(struct dmar_domain *domain,
> +					  unsigned long start_pfn,
> +					  unsigned long last_pfn)
> +{
> +	return (!domain_pfn_supported(domain, last_pfn)) ? false :
> +		(start_pfn > last_pfn) ? false : true;
> +}
> +
>   /*
>    * Calculate the Supported Adjusted Guest Address Widths of an IOMMU.
>    * Refer to 11.4.2 of the VT-d spec for the encoding of each bit of
> @@ -915,7 +923,8 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
>   	int level = agaw_to_level(domain->agaw);
>   	int offset;
>   
> -	BUG_ON(!domain->pgd);
> +	if (WARN_ON_ONCE(!domain->pgd))
> +		return NULL;
>   
>   	if (!domain_pfn_supported(domain, pfn))
>   		/* Address beyond IOMMU's addressing capabilities. */
> @@ -1005,9 +1014,8 @@ static void dma_pte_clear_range(struct dmar_domain *domain,
>   	unsigned int large_page;
>   	struct dma_pte *first_pte, *pte;
>   
> -	BUG_ON(!domain_pfn_supported(domain, start_pfn));
> -	BUG_ON(!domain_pfn_supported(domain, last_pfn));
> -	BUG_ON(start_pfn > last_pfn);
> +	if (WARN_ON_ONCE(!domain_pfn_range_valid(domain, start_pfn, last_pfn))
> +		return;
>   
>   	/* we don't need lock here; nobody else touches the iova range */
>   	do {
> @@ -1166,9 +1174,8 @@ static void dma_pte_clear_level(struct dmar_domain *domain, int level,
>   static void domain_unmap(struct dmar_domain *domain, unsigned long start_pfn,
>   			 unsigned long last_pfn, struct list_head *freelist)
>   {
> -	BUG_ON(!domain_pfn_supported(domain, start_pfn));
> -	BUG_ON(!domain_pfn_supported(domain, last_pfn));
> -	BUG_ON(start_pfn > last_pfn);
> +	if (WARN_ON_ONCE(!domain_pfn_range_valid(domain, start_pfn, last_pfn))
> +		return;
>   
>   	/* we don't need lock here; nobody else touches the iova range */
>   	dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
> @@ -1272,7 +1279,7 @@ static void __iommu_flush_context(struct intel_iommu *iommu,
>   			| DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
>   		break;
>   	default:
> -		BUG();
> +		WARN_ON_ONCE(1);
>   	}
>   	val |= DMA_CCMD_ICC;
>   
> @@ -1308,7 +1315,7 @@ static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
>   		val_iva = size_order | addr;
>   		break;
>   	default:
> -		BUG();
> +		WARN_ON_ONCE(1);
>   	}
>   	/* Note: set drain read/write */
>   #if 0
> @@ -1508,7 +1515,8 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
>   	uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
>   	u16 did = domain_id_iommu(domain, iommu);
>   
> -	BUG_ON(pages == 0);
> +	if (WARN_ON_ONCE(pages == 0))
> +		return;
>   
>   	if (ih)
>   		ih = 1 << 6;
> @@ -1930,7 +1938,8 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
>   	pr_debug("Set context mapping for %02x:%02x.%d\n",
>   		bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
>   
> -	BUG_ON(!domain->pgd);
> +	if (WARN_ON_ONCE(!domain->pgd))
> +		return -EINVAL;
>   
>   	spin_lock(&iommu->lock);
>   	ret = -ENOMEM;
> @@ -2183,7 +2192,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
>   	phys_addr_t pteval;
>   	u64 attr;
>   
> -	BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
> +	if (WARN_ON_ONCE(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1)))
> +		return -EINVAL;
>   
>   	if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
>   		return -EINVAL;
> @@ -4339,8 +4349,9 @@ static size_t intel_iommu_unmap(struct iommu_domain *domain,
>   
>   	/* Cope with horrid API which requires us to unmap more than the
>   	   size argument if it happens to be a large-page mapping. */
> -	BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level,
> -			       GFP_ATOMIC));
> +	if (WARN_ON_ONCE(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level,
> +					 GFP_ATOMIC)))
> +		return 0;
>   
>   	if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
>   		size = VTD_PAGE_SIZE << level_to_offset_bits(level);


      reply	other threads:[~2023-03-29 13:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 12:46 [PATCH 0/4] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
2023-03-29 12:46 ` [PATCH 1/4] iommu/vt-d: Fix null pointer access in invalidate_range callback Tina Zhang
2023-03-29 13:30   ` Baolu Lu
2023-03-29 13:40     ` Zhang, Tina
2023-03-29 13:51       ` Baolu Lu
2023-03-29 12:46 ` [PATCH 2/4] iommu/vt-d: Fix operand size in bitwise operation Tina Zhang
2023-03-29 13:32   ` Baolu Lu
2023-03-29 13:44     ` Zhang, Tina
2023-03-29 12:46 ` [PATCH 3/4] iommu/vt-d: Fix quoted string split across lines Tina Zhang
2023-03-29 13:38   ` Baolu Lu
2023-03-29 23:40     ` Zhang, Tina
2023-03-29 12:46 ` [PATCH 4/4] iommu/vt-d: Replace BUG()/BUG_ON() with WARN_ON/WARN_ON_ONCE() Tina Zhang
2023-03-29 13:40   ` Baolu Lu [this message]

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=2bc952b2-311a-cac2-d9ba-223a124bdf74@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=tina.zhang@intel.com \
    /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