Skip to contentSkip to conversation

drivers/iommu/intel/iommu.c:5894 intel_iommu_unmap() warn: variable dereferenced before check 'gather' (see line 5885)

PATCH New Dan Carpenter 1 participant 1 message Sep 10, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 20910/30000 drivers/iommu/intel/iommu.c:5894 intel_iommu_unmap() warn: variable dereferenced before check 'gather' (see line 5885)

tree:   https://gitee.com/openeuler/kernel.git OLK-5.10
head:   3e6d44ea60eb8609ba59273c12276e3236cd76b2
commit: 004c9426acfc9d9dcb19ec63f0f6aa2f754a06ee [20910/30000] iommu/vt-d: Avoid superfluous IOTLB tracking in lazy mode
config: x86_64-randconfig-161-20240906 (https://download.01.org/0day-ci/archive/20240909/[email protected]/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
drivers/iommu/intel/iommu.c:5894 intel_iommu_unmap() warn: variable dereferenced before check 'gather' (see line 5885)

Old smatch warnings:
drivers/iommu/intel/iommu.c:951 device_to_iommu() error: we previously assumed 'pdev' could be null (see line 924) drivers/iommu/intel/iommu.c:2471 __domain_mapping() error: we previously assumed 'sg' could be null (see line 2462)

vim +/gather +5894 drivers/iommu/intel/iommu.c

5009065d38c954 drivers/iommu/intel-iommu.c Ohad Ben-Cohen  2011-11-10  5866  static size_t intel_iommu_unmap(struct iommu_domain *domain,
56f8af5e9d38f1 drivers/iommu/intel-iommu.c Will Deacon     2019-07-02  5867  				unsigned long iova, size_t size,
56f8af5e9d38f1 drivers/iommu/intel-iommu.c Will Deacon     2019-07-02  5868  				struct iommu_iotlb_gather *gather)
38717946425792 drivers/pci/intel-iommu.c   Kay, Allen M    2008-09-09  5869  {
00a77deb0f2f2c drivers/iommu/intel-iommu.c Joerg Roedel    2015-03-26  5870  	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
ea8ea460c9ace6 drivers/iommu/intel-iommu.c David Woodhouse 2014-03-05  5871  	unsigned long start_pfn, last_pfn;
4a2e7b3a540ffb drivers/iommu/intel/iommu.c Tom Murphy      2020-11-24  5872  	int level = 0;
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5873  
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5874  	/* Cope with horrid API which requires us to unmap more than the
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5875  	   size argument if it happens to be a large-page mapping. */
dc02e46e8d0234 drivers/iommu/intel-iommu.c Joerg Roedel    2015-08-13  5876  	BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5877  
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5878  	if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5879  		size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4b99d3527041be drivers/pci/intel-iommu.c   Sheng Yang      2009-07-08  5880  
ea8ea460c9ace6 drivers/iommu/intel-iommu.c David Woodhouse 2014-03-05  5881  	start_pfn = iova >> VTD_PAGE_SHIFT;
ea8ea460c9ace6 drivers/iommu/intel-iommu.c David Woodhouse 2014-03-05  5882  	last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
ea8ea460c9ace6 drivers/iommu/intel-iommu.c David Woodhouse 2014-03-05  5883  
4a2e7b3a540ffb drivers/iommu/intel/iommu.c Tom Murphy      2020-11-24  5884  	gather->freelist = domain_unmap(dmar_domain, start_pfn,
                                                                                ^^^^^^^^^^^^^^^^

This code assumes gather is a valid pointer

4a2e7b3a540ffb drivers/iommu/intel/iommu.c Tom Murphy      2020-11-24 @5885  					last_pfn, gather->freelist);
                                                                                                                          ^^^^^^^^^^^^^^^^
fe40f1e020d092 drivers/pci/intel-iommu.c   Weidong Han     2008-12-08  5886  
163cc52ccd2cc5 drivers/pci/intel-iommu.c   David Woodhouse 2009-06-28  5887  	if (dmar_domain->max_addr == iova + size)
163cc52ccd2cc5 drivers/pci/intel-iommu.c   David Woodhouse 2009-06-28  5888  		dmar_domain->max_addr = iova;
b146a1c9f7f1fe drivers/pci/intel-iommu.c   Joerg Roedel    2010-01-20  5889  
004c9426acfc9d drivers/iommu/intel/iommu.c Jacob Pan       2023-02-16  5890  	/*
004c9426acfc9d drivers/iommu/intel/iommu.c Jacob Pan       2023-02-16  5891  	 * We do not use page-selective IOTLB invalidation in flush queue,
004c9426acfc9d drivers/iommu/intel/iommu.c Jacob Pan       2023-02-16  5892  	 * so there is no need to track page and sync iotlb.
004c9426acfc9d drivers/iommu/intel/iommu.c Jacob Pan       2023-02-16  5893  	 */
004c9426acfc9d drivers/iommu/intel/iommu.c Jacob Pan       2023-02-16 @5894  	if (!(gather && gather->queued))
                                                                                      ^^^^^^

Checked too late

4a2e7b3a540ffb drivers/iommu/intel/iommu.c Tom Murphy      2020-11-24  5895  		iommu_iotlb_gather_add_page(domain, gather, iova, size);
4a2e7b3a540ffb drivers/iommu/intel/iommu.c Tom Murphy      2020-11-24  5896  
5cf0a76fa2179d drivers/iommu/intel-iommu.c David Woodhouse 2014-03-19  5897  	return size;
38717946425792 drivers/pci/intel-iommu.c   Kay, Allen M    2008-09-09  5898  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki