public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST
@ 2009-08-02  8:45 Julia Lawall
  2009-08-04 22:09 ` [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation Fenghua Yu
  2009-08-05 23:22 ` [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Yu, Fenghua
  0 siblings, 2 replies; 4+ messages in thread
From: Julia Lawall @ 2009-08-02  8:45 UTC (permalink / raw)
  To: fenghua.yu, tony.luck, linux-ia64, linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

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>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 arch/ia64/kernel/time.c             |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/ia64/kernel/time.c b/arch/ia64/kernel/time.c
index 4990495..aaa6651 100644
--- a/arch/ia64/kernel/time.c
+++ b/arch/ia64/kernel/time.c
@@ -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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation
  2009-08-02  8:45 [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Julia Lawall
@ 2009-08-04 22:09 ` Fenghua Yu
  2009-08-05  7:59   ` David Woodhouse
  2009-08-05 23:22 ` [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Yu, Fenghua
  1 sibling, 1 reply; 4+ messages in thread
From: Fenghua Yu @ 2009-08-04 22:09 UTC (permalink / raw)
  To: David Woodhouse, Tony Luck; +Cc: iommu, linux-ia64, linux-kernel, Fenghua Yu

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.

Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>

---

 drivers/pci/intel-iommu.c |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iommu.c
index bec29ed..54ee63d 100644
--- a/drivers/pci/intel-iommu.c
+++ b/drivers/pci/intel-iommu.c
@@ -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,

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation
  2009-08-04 22:09 ` [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation Fenghua Yu
@ 2009-08-05  7:59   ` David Woodhouse
  0 siblings, 0 replies; 4+ messages in thread
From: David Woodhouse @ 2009-08-05  7:59 UTC (permalink / raw)
  To: Fenghua Yu; +Cc: Tony Luck, iommu, linux-ia64, linux-kernel

On Tue, 2009-08-04 at 15:09 -0700, Fenghua Yu wrote:
> 
> -       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?

diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iommu.c
index ebc9b8d..ed6109d 100644
--- a/drivers/pci/intel-iommu.c
+++ b/drivers/pci/intel-iommu.c
@@ -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,


-- 
dwmw2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* RE: [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST
  2009-08-02  8:45 [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Julia Lawall
  2009-08-04 22:09 ` [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation Fenghua Yu
@ 2009-08-05 23:22 ` Yu, Fenghua
  1 sibling, 0 replies; 4+ messages in thread
From: Yu, Fenghua @ 2009-08-05 23:22 UTC (permalink / raw)
  To: 'Julia Lawall', Luck, Tony,
	'linux-ia64@vger.kernel.org',
	'linux-kernel@vger.kernel.org',
	'kernel-janitors@vger.kernel.org'

>-----Original Message-----
>From: Julia Lawall [mailto:julia@diku.dk]
>Sent: Sunday, August 02, 2009 1:45 AM
>To: Yu, Fenghua; Luck, Tony; linux-ia64@vger.kernel.org; linux-
>kernel@vger.kernel.org; kernel-janitors@vger.kernel.org
>Subject: [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST
>
>From: Julia Lawall <julia@diku.dk>
>
>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>
>
>Signed-off-by: Julia Lawall <julia@diku.dk>
>
>---
> arch/ia64/kernel/time.c             |   10 ++++++----
> 1 files changed, 6 insertions(+), 4 deletions(-)
>
>diff --git a/arch/ia64/kernel/time.c b/arch/ia64/kernel/time.c
>index 4990495..aaa6651 100644
>--- a/arch/ia64/kernel/time.c
>+++ b/arch/ia64/kernel/time.c
>@@ -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

Acked-by: Fenghua Yu <Fenghua.yu@intel.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-08-05 23:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-02  8:45 [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Julia Lawall
2009-08-04 22:09 ` [PATCH 1/4] Bug Fix drivers/pci/intel-iommu.c: correct sglist size calculation Fenghua Yu
2009-08-05  7:59   ` David Woodhouse
2009-08-05 23:22 ` [PATCH 3/15] arch/ia64: Use DIV_ROUND_CLOSEST Yu, Fenghua

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox