mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Bottomley <James.Bottomley@SteelEye.com>
To: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Cc: linux-kernel@vger.kernel.org, jayalk@intworks.biz,
	akpm@linux-foundation.org
Subject: Re: [PATCH] dma_declare_coherent_memory wrong allocation
Date: Thu, 26 Apr 2007 09:47:47 -0400	[thread overview]
Message-ID: <1177595268.6284.116.camel@mulgrave.il.steeleye.com> (raw)
In-Reply-To: <Pine.LNX.4.60.0704232239250.12939@poirot.grange>

On Mon, 2007-04-23 at 22:45 +0200, Guennadi Liakhovetski wrote:
> On Mon, 23 Apr 2007, James Bottomley wrote:
> 
> > On Mon, 2007-04-23 at 00:45 +0200, Guennadi Liakhovetski wrote:
> > > Right, thinko. How about using his:
> > > 
> > > +	int pages = DIV_ROUND_UP(size, PAGE_SIZE);
> > 
> > Actually, no ... this has to be size >> PAGE_SHIFT.  The reason being
> > that the  allocator is designed to allocate pages out of a device memory
> > buffer.  If the size isn't a multiple of page size, we have to round
> > down (we can't allocate the last page if the memory we have is only part
> > of a page).
> > 
> > I suppose if you want to catch the unlikely nutcase where size <
> > PAGE_SIZE you could
> > 
> > if (unlikely(pages == 0))
> > 	goto out;
> > 
> > > +	int bitmap_size = BITS_TO_LONGS(pages) * BYTES_PER_LONG;
> > 
> > This is fine, except the BYTES_PER_LONG.  Traditionally, we do this with
> > sizeof(long).  The only reason to have a #define for it is if it has to
> > be used in a macro (the compiler does sizeof() not the preprocessor).
> > 
> > How about just a simple
> > 
> > int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
> 
> Ok, version 3, small, and hopefully, beautiful:
> 
> dma_declare_coherent_memory() allocates a bitmap 1 bit per page, it 
> calculates the bitmap size based on size of long, but allocates bytes...
> Thanks to James Bottomley for clarifications and corrections.
> 
> Signed-off-by: G. Liakhovetski <g.liakhovetski@gmx.de>

Looks perfect to me.

Acked-by: James Bottomley <James.Bottomley@SteelEye.com>

> diff --git a/arch/cris/arch-v32/drivers/pci/dma.c b/arch/cris/arch-v32/drivers/pci/dma.c
> index 70d3bf0..832fc63 100644
> --- a/arch/cris/arch-v32/drivers/pci/dma.c
> +++ b/arch/cris/arch-v32/drivers/pci/dma.c
> @@ -76,7 +76,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
>  {
>  	void __iomem *mem_base;
>  	int pages = size >> PAGE_SHIFT;
> -	int bitmap_size = (pages + 31)/32;
> +	int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
>  
>  	if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
>  		goto out;
> diff --git a/arch/i386/kernel/pci-dma.c b/arch/i386/kernel/pci-dma.c
> index 3ebcea0..30b754f 100644
> --- a/arch/i386/kernel/pci-dma.c
> +++ b/arch/i386/kernel/pci-dma.c
> @@ -77,7 +77,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
>  {
>  	void __iomem *mem_base = NULL;
>  	int pages = size >> PAGE_SHIFT;
> -	int bitmap_size = (pages + 31)/32;
> +	int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
>  
>  	if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
>  		goto out;


      reply	other threads:[~2007-04-26 13:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-13 18:08 Guennadi Liakhovetski
2007-04-21  7:08 ` Andrew Morton
2007-04-21 11:01   ` Guennadi Liakhovetski
2007-04-22 16:52 ` James Bottomley
2007-04-22 22:45   ` Guennadi Liakhovetski
2007-04-22 22:49     ` Guennadi Liakhovetski
2007-04-23 15:58     ` James Bottomley
2007-04-23 20:45       ` Guennadi Liakhovetski
2007-04-26 13:47         ` James Bottomley [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=1177595268.6284.116.camel@mulgrave.il.steeleye.com \
    --to=james.bottomley@steeleye.com \
    --cc=akpm@linux-foundation.org \
    --cc=g.liakhovetski@gmx.de \
    --cc=jayalk@intworks.biz \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®