public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dma-direct: Optimize get_required_mask
@ 2022-12-14 18:00 Luben Tuikov
  2022-12-14 20:57 ` Robin Murphy
  0 siblings, 1 reply; 3+ messages in thread
From: Luben Tuikov @ 2022-12-14 18:00 UTC (permalink / raw)
  To: iommu
  Cc: Luben Tuikov, Christoph Hellwig, Robin Murphy, Alex Deucher,
	Christian König, Linux Kernel Mailing List

Optimize dma_direct_get_required_mask(), in that we don't need to multiply by
two if we don't subtract 1 from the exponent. That is,

    (1 << (n - 1)) * 2 - 1 <==>
    2^(n-1) * 2^1 - 1       = (by rule of exponents)
    2^n - 1                 <==>
    (1 << n) - 1.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Alex Deucher <Alexander.Deucher@amd.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: iommu@lists.linux.dev
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Signed-off-by: Luben Tuikov <luben.tuikov@amd.com>
---
 kernel/dma/direct.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 63859a101ed831..bb416a3949dac0 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -41,7 +41,7 @@ u64 dma_direct_get_required_mask(struct device *dev)
 	phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT;
 	u64 max_dma = phys_to_dma_direct(dev, phys);
 
-	return (1ULL << (fls64(max_dma) - 1)) * 2 - 1;
+	return (1ULL << fls64(max_dma)) - 1;
 }
 
 static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,

base-commit: e2ca6ba6ba0152361aa4fcbf6067db71b2c7a770
-- 
2.39.0


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

* Re: [PATCH] dma-direct: Optimize get_required_mask
  2022-12-14 18:00 [PATCH] dma-direct: Optimize get_required_mask Luben Tuikov
@ 2022-12-14 20:57 ` Robin Murphy
  2022-12-15  4:30   ` Luben Tuikov
  0 siblings, 1 reply; 3+ messages in thread
From: Robin Murphy @ 2022-12-14 20:57 UTC (permalink / raw)
  To: Luben Tuikov, iommu
  Cc: Christoph Hellwig, Alex Deucher, Christian König,
	Linux Kernel Mailing List

On 2022-12-14 18:00, Luben Tuikov wrote:
> Optimize dma_direct_get_required_mask(), in that we don't need to multiply by
> two if we don't subtract 1 from the exponent. That is,
> 
>      (1 << (n - 1)) * 2 - 1 <==>
>      2^(n-1) * 2^1 - 1       = (by rule of exponents)
>      2^n - 1                 <==>
>      (1 << n) - 1.

...except when n==64 (for the actual code below), in which case the 
result of the shift becomes undefined.

Robin.

> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Robin Murphy <robin.murphy@arm.com>
> Cc: Alex Deucher <Alexander.Deucher@amd.com>
> Cc: Christian König <christian.koenig@amd.com>
> Cc: iommu@lists.linux.dev
> Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
> Signed-off-by: Luben Tuikov <luben.tuikov@amd.com>
> ---
>   kernel/dma/direct.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> index 63859a101ed831..bb416a3949dac0 100644
> --- a/kernel/dma/direct.c
> +++ b/kernel/dma/direct.c
> @@ -41,7 +41,7 @@ u64 dma_direct_get_required_mask(struct device *dev)
>   	phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT;
>   	u64 max_dma = phys_to_dma_direct(dev, phys);
>   
> -	return (1ULL << (fls64(max_dma) - 1)) * 2 - 1;
> +	return (1ULL << fls64(max_dma)) - 1;
>   }
>   
>   static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
> 
> base-commit: e2ca6ba6ba0152361aa4fcbf6067db71b2c7a770

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

* Re: [PATCH] dma-direct: Optimize get_required_mask
  2022-12-14 20:57 ` Robin Murphy
@ 2022-12-15  4:30   ` Luben Tuikov
  0 siblings, 0 replies; 3+ messages in thread
From: Luben Tuikov @ 2022-12-15  4:30 UTC (permalink / raw)
  To: Robin Murphy, iommu
  Cc: Christoph Hellwig, Alex Deucher, Christian König,
	Linux Kernel Mailing List

On 2022-12-14 15:57, Robin Murphy wrote:
> On 2022-12-14 18:00, Luben Tuikov wrote:
>> Optimize dma_direct_get_required_mask(), in that we don't need to multiply by
>> two if we don't subtract 1 from the exponent. That is,
>>
>>      (1 << (n - 1)) * 2 - 1 <==>
>>      2^(n-1) * 2^1 - 1       = (by rule of exponents)
>>      2^n - 1                 <==>
>>      (1 << n) - 1.
> 
> ...except when n==64 (for the actual code below), in which case the 
> result of the shift becomes undefined.

Oh, right, for bit 63 being set. Forgot about that one. Good call.

Thanks,
Luben


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

end of thread, other threads:[~2022-12-15  4:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-14 18:00 [PATCH] dma-direct: Optimize get_required_mask Luben Tuikov
2022-12-14 20:57 ` Robin Murphy
2022-12-15  4:30   ` Luben Tuikov

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