Skip to contentSkip to conversation

arch/ia64: Use DIV_ROUND_CLOSEST

PATCH Applied Julia Lawall 3 participants 4 messages Aug 2, 2009 – Aug 5, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 3/15 arch/ia64: Use DIV_ROUND_CLOSEST

From: Julia Lawall <[email protected]>

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d but is perhaps more readable.

The semantic patch that makes this change is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@haskernel@
@@

#include <linux/kernel.h>

@depends on haskernel@
expression x,__divisor;
@@

- (((x) + ((__divisor) / 2)) / (__divisor))
+ DIV_ROUND_CLOSEST(x,__divisor)
// </smpl>

---
 arch/ia64/kernel/time.c             |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
arch/ia64/kernel/time.c +4 -4
index 4990495..aaa6651 100644
@@ -314,7 +314,7 @@ ia64_init_itm (void)
 
 	itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
 
-	local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
+	local_cpu_data->itm_delta = DIV_ROUND_CLOSEST(itc_freq, HZ);
 	printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
 	       "ITC freq=%lu.%03luMHz", smp_processor_id(),
 	       platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
@@ -330,9 +330,11 @@ ia64_init_itm (void)
 
 	local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
 	local_cpu_data->itc_freq = itc_freq;
-	local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
-	local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
-					+ itc_freq/2)/itc_freq;
+	local_cpu_data->cyc_per_usec +		DIV_ROUND_CLOSEST(itc_freq, USEC_PER_SEC);
+	local_cpu_data->nsec_per_cyc +		DIV_ROUND_CLOSEST(NSEC_PER_SEC << IA64_NSEC_PER_CYC_SHIFT,
+				  itc_freq);
 
 	if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
 #ifdef CONFIG_SMP
#1 lore raw

PATCH 1/4 Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation

When calculating a scatter gather list size in intel_map_sg(), the size of an sg entry should be based on sg->addr, sg->offset, and sg->length instead of just sg->offset and sg->length.

And the size of a scatter gather list should be passed to domain_sg_mapping() directly because it has been aligned to VTD_PAGE_SIZE already.

Because of the issue, system can not boot when PAGE_SIZE>VTD_PAGE_SIZE e.g. on ia64 platforms.

---

 drivers/pci/intel-iommu.c |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)
drivers/pci/intel-iommu.c +16 -12
index bec29ed..54ee63d 100644
@@ -1645,6 +1645,15 @@ static int domain_context_mapped(struct pci_dev *pdev)
 					     tmp->devfn);
 }
 
+/* Returns a number of VTD pages, but aligned to MM page size */
+static inline unsigned long aligned_nrpages(unsigned long host_addr,
+					    size_t size)
+{
+	host_addr &= ~PAGE_MASK;
+	return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
+}
+
+
 static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
 			    struct scatterlist *sg, unsigned long phys_pfn,
 			    unsigned long nr_pages, int prot)
@@ -1672,7 +1681,8 @@ static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
 		uint64_t tmp;
 
 		if (!sg_res) {
-			sg_res = (sg->offset + sg->length + VTD_PAGE_SIZE - 1) >> VTD_PAGE_SHIFT;
+			dma_addr_t addr = sg_phys(sg);
+			sg_res = aligned_nrpages(addr, sg->length);
 			sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
 			sg->dma_length = sg->length;
 			pteval = page_to_phys(sg_page(sg)) | prot;
@@ -2411,14 +2425,6 @@ error:
 	return ret;
 }
 
-/* Returns a number of VTD pages, but aligned to MM page size */
-static inline unsigned long aligned_nrpages(unsigned long host_addr,
-					    size_t size)
-{
-	host_addr &= ~PAGE_MASK;
-	return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
-}
-
 /* This takes a number of _MM_ pages, not VTD pages */
 static struct iova *intel_alloc_iova(struct device *dev,
 				     struct dmar_domain *domain,
@@ -2861,8 +2868,10 @@ static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int ne
 
 	iommu = domain_get_iommu(domain);
 
-	for_each_sg(sglist, sg, nelems, i)
-		size += aligned_nrpages(sg->offset, sg->length);
+	for_each_sg(sglist, sg, nelems, i) {
+		dma_addr_t addr = sg_phys(sg);
+		size += aligned_nrpages(addr, sg->length);
+	}
 
 	iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size),
 				pdev->dma_mask);
@@ -2883,7 +2892,7 @@ static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int ne
 
 	start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
 
-	ret = domain_sg_mapping(domain, start_vpfn, sglist, mm_to_dma_pfn(size), prot);
+	ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
 	if (unlikely(ret)) {
 		/*  clear the page */
 		dma_pte_clear_range(domain, start_vpfn,
#2 lore raw

Re: [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation

Review

Quoted 7 lines · Fenghua Yu
(quoted diff) +4 -2
-       for_each_sg(sglist, sg, nelems, i)
-               size += aligned_nrpages(sg->offset, sg->length);
+       for_each_sg(sglist, sg, nelems, i) {
+               dma_addr_t addr = sg_phys(sg);
+               size += aligned_nrpages(addr, sg->length);
+       }

What's the point in this part? We mask off the high bits of the first argument to aligned_nrpages() -- we're only interested in the offset within the (mm) page. So sg->offset is fine; we don't need to use sg_phys(), surely? And this is in code where we're desperately trying to shave off as many cycles as we can...

This version of the patch should suffice, yes?

drivers/pci/intel-iommu.c +11 -10
index ebc9b8d..ed6109d 100644
@@ -1648,6 +1648,15 @@ static int domain_context_mapped(struct pci_dev *pdev)
 					     tmp->devfn);
 }
 
+/* Returns a number of VTD pages, but aligned to MM page size */
+static inline unsigned long aligned_nrpages(unsigned long host_addr,
+					    size_t size)
+{
+	host_addr &= ~PAGE_MASK;
+	return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
+}
+
+
 static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
 			    struct scatterlist *sg, unsigned long phys_pfn,
 			    unsigned long nr_pages, int prot)
@@ -1675,7 +1684,7 @@ static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
 		uint64_t tmp;
 
 		if (!sg_res) {
-			sg_res = (sg->offset + sg->length + VTD_PAGE_SIZE - 1) >> VTD_PAGE_SHIFT;
+			sg_res = aligned_nrpages(sg->offset, sg->length);
 			sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
 			sg->dma_length = sg->length;
 			pteval = page_to_phys(sg_page(sg)) | prot;
@@ -2415,14 +2424,6 @@ error:
 	return ret;
 }
 
-/* Returns a number of VTD pages, but aligned to MM page size */
-static inline unsigned long aligned_nrpages(unsigned long host_addr,
-					    size_t size)
-{
-	host_addr &= ~PAGE_MASK;
-	return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
-}
-
 /* This takes a number of _MM_ pages, not VTD pages */
 static struct iova *intel_alloc_iova(struct device *dev,
 				     struct dmar_domain *domain,
@@ -2875,7 +2876,7 @@ static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int ne
 
 	start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
 
-	ret = domain_sg_mapping(domain, start_vpfn, sglist, mm_to_dma_pfn(size), prot);
+	ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
 	if (unlikely(ret)) {
 		/*  clear the page */
 		dma_pte_clear_range(domain, start_vpfn,
Signature · 1 line
dwmw2
Acked-by on v1 3/15
#3 lore raw
In reply to Julia Lawall
Quoted 62 lines

-----Original Message-----
From: Julia Lawall [mailto:[email protected]]
Sent: Sunday, August 02, 2009 1:45 AM
To: Yu, Fenghua; Luck, Tony; [email protected]; linux- [email protected]; [email protected]
Subject: [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST

From: Julia Lawall <[email protected]>

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d but is perhaps more readable.

The semantic patch that makes this change is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@haskernel@
@@

#include <linux/kernel.h>

@depends on haskernel@
expression x,__divisor;
@@

- (((x) + ((__divisor) / 2)) / (__divisor))
+ DIV_ROUND_CLOSEST(x,__divisor)
// </smpl>

---
arch/ia64/kernel/time.c | 10 ++++++----

1 files changed, 6 insertions(+), 4 deletions(-)
arch/ia64/kernel/time.c +1 -1
index 4990495..aaa6651 100644
@@ -314,7 +314,7 @@ ia64_init_itm (void)
 
 	itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
 
-	local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
+	local_cpu_data->itm_delta = DIV_ROUND_CLOSEST(itc_freq, HZ);
 	printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
 	       "ITC freq=%lu.%03luMHz", smp_processor_id(),
 	       platform_base_freq / 1000000, (platform_base_freq / 1000) %

1000,

arch/ia64/kernel/time.c +3 -3
@@ -330,9 +330,11 @@ ia64_init_itm (void)
 
 	local_cpu_data->proc_freq >(platform_base_freq*proc_ratio.num)/proc_ratio.den;
 	local_cpu_data->itc_freq = itc_freq;
-	local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) /
 USEC_PER_SEC;
-	local_cpu_data->nsec_per_cyc >((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
-					+ itc_freq/2)/itc_freq;
+	local_cpu_data->cyc_per_usec >+		DIV_ROUND_CLOSEST(itc_freq, USEC_PER_SEC);
+	local_cpu_data->nsec_per_cyc >+		DIV_ROUND_CLOSEST(NSEC_PER_SEC << IA64_NSEC_PER_CYC_SHIFT,
+				  itc_freq);
 
 	if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
Quoted 1 line

#ifdef CONFIG_SMP