All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guennadi Liakhovetski <lg@denx.de>
To: Paul Mundt <lethal@linux-sh.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	adrian@newgolddream.dyndns.info, lkmladrian@gmail.com,
	linux-kernel@vger.kernel.org, linux-sh@vger.kernel.org,
	penberg@cs.helsinki.fi, dbaryshkov@gmail.com,
	penguin-kernel@i-love.sakura.ne.jp, hannes@cmpxchg.org
Subject: Re: [PATCH] dma: fix up broken comparison in dma_alloc_from_coherent
Date: Wed, 28 Jan 2009 08:36:57 +0000	[thread overview]
Message-ID: <Pine.LNX.4.64.0901280913090.4926@axis700.grange> (raw)
In-Reply-To: <20090127225458.GA8756@linux-sh.org>

On Wed, 28 Jan 2009, Paul Mundt wrote:

> On Tue, Jan 27, 2009 at 01:48:31PM -0800, Andrew Morton wrote:
> > On Wed, 21 Jan 2009 17:11:19 +0900
> > Paul Mundt <lethal@linux-sh.org> wrote:
> > 
> > > @@ -118,31 +118,32 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size,
> > >  	mem = dev->dma_mem;
> > >  	if (!mem)
> > >  		return 0;
> > > -	if (unlikely(size > mem->size))
> > > - 		return 0;
> > > +
> > > +	*ret = NULL;
> > > +
> > > +	if (unlikely(size > (mem->size << PAGE_SHIFT)))
> > > +		goto err;
> > 
> > Looks a bit broken on 64-bit.

Not related to the 64-bit dangers, but using bitmap_find_free_region() in 
dma_alloc_from_coherent() breaks in most non-spectacular ways again and 
again. This loop and test in bitmap_find_free_region()

	for (pos = 0; pos < bits; pos += (1 << order))
		if (__reg_op(bitmap, pos, order, REG_OP_ISFREE))
			break;
	if (pos = bits)
		return -ENOMEM;

can only return an error (-ENOMEM) if bits is a multiple of (1 << order), 
which is, for instance, true, if bits is (also) a power of 2. Which 
doesn't seem to be necessarily the case with dma_alloc_from_coherent(). 
Where shall this one be fixed - in bitmap or in DMA? The correct test in 
bitmap code seems to be

	if (pos + (1 << order) > bits)
		return -ENOMEM;

and I don't see a way to fix this in dma. Checking afterwards is too late 
- the current bitmap_find_free_region() will (with a bit of luck) quietly 
overwrite data beyond bits.

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.

DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office@denx.de

WARNING: multiple messages have this Message-ID (diff)
From: Guennadi Liakhovetski <lg@denx.de>
To: Paul Mundt <lethal@linux-sh.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	adrian@newgolddream.dyndns.info, lkmladrian@gmail.com,
	linux-kernel@vger.kernel.org, linux-sh@vger.kernel.org,
	penberg@cs.helsinki.fi, dbaryshkov@gmail.com,
	penguin-kernel@i-love.sakura.ne.jp, hannes@cmpxchg.org
Subject: Re: [PATCH] dma: fix up broken comparison in dma_alloc_from_coherent
Date: Wed, 28 Jan 2009 09:36:57 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.64.0901280913090.4926@axis700.grange> (raw)
In-Reply-To: <20090127225458.GA8756@linux-sh.org>

On Wed, 28 Jan 2009, Paul Mundt wrote:

> On Tue, Jan 27, 2009 at 01:48:31PM -0800, Andrew Morton wrote:
> > On Wed, 21 Jan 2009 17:11:19 +0900
> > Paul Mundt <lethal@linux-sh.org> wrote:
> > 
> > > @@ -118,31 +118,32 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size,
> > >  	mem = dev->dma_mem;
> > >  	if (!mem)
> > >  		return 0;
> > > -	if (unlikely(size > mem->size))
> > > - 		return 0;
> > > +
> > > +	*ret = NULL;
> > > +
> > > +	if (unlikely(size > (mem->size << PAGE_SHIFT)))
> > > +		goto err;
> > 
> > Looks a bit broken on 64-bit.

Not related to the 64-bit dangers, but using bitmap_find_free_region() in 
dma_alloc_from_coherent() breaks in most non-spectacular ways again and 
again. This loop and test in bitmap_find_free_region()

	for (pos = 0; pos < bits; pos += (1 << order))
		if (__reg_op(bitmap, pos, order, REG_OP_ISFREE))
			break;
	if (pos == bits)
		return -ENOMEM;

can only return an error (-ENOMEM) if bits is a multiple of (1 << order), 
which is, for instance, true, if bits is (also) a power of 2. Which 
doesn't seem to be necessarily the case with dma_alloc_from_coherent(). 
Where shall this one be fixed - in bitmap or in DMA? The correct test in 
bitmap code seems to be

	if (pos + (1 << order) > bits)
		return -ENOMEM;

and I don't see a way to fix this in dma. Checking afterwards is too late 
- the current bitmap_find_free_region() will (with a bit of luck) quietly 
overwrite data beyond bits.

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.

DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office@denx.de

  reply	other threads:[~2009-01-28  8:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-20 21:48 [PATCH] dma: fix up broken comparison in dma_alloc_from_coherent Adrian McMenamin
2009-01-20 21:55 ` [PATCH] dma: fix up broken comparison in Adrian McMenamin
2009-01-20 21:55   ` [PATCH] dma: fix up broken comparison in dma_alloc_from_coherent Adrian McMenamin
2009-01-21  3:39   ` Paul Mundt
2009-01-21  3:39     ` Paul Mundt
2009-01-21  8:11     ` Paul Mundt
2009-01-21  8:11       ` Paul Mundt
2009-01-21  8:29       ` Guennadi Liakhovetski
2009-01-21  8:29         ` Guennadi Liakhovetski
2009-01-21  8:30         ` Paul Mundt
2009-01-21  8:30           ` Paul Mundt
2009-01-27 21:48       ` [PATCH] dma: fix up broken comparison in Andrew Morton
2009-01-27 21:48         ` [PATCH] dma: fix up broken comparison in dma_alloc_from_coherent Andrew Morton
2009-01-27 22:54         ` Paul Mundt
2009-01-27 22:54           ` Paul Mundt
2009-01-28  8:36           ` Guennadi Liakhovetski [this message]
2009-01-28  8:36             ` Guennadi Liakhovetski
2009-02-06 12:22             ` [PATCH] fix broken size test in bitmap_find_free_region() Guennadi Liakhovetski
2009-02-06 12:22               ` Guennadi Liakhovetski
2009-02-06 21:29               ` Andrew Morton
2009-02-06 21:29                 ` Andrew Morton
2009-02-06 22:30                 ` Guennadi Liakhovetski
2009-02-06 22:30                   ` Guennadi Liakhovetski

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=Pine.LNX.4.64.0901280913090.4926@axis700.grange \
    --to=lg@denx.de \
    --cc=adrian@newgolddream.dyndns.info \
    --cc=akpm@linux-foundation.org \
    --cc=dbaryshkov@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=lethal@linux-sh.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=lkmladrian@gmail.com \
    --cc=penberg@cs.helsinki.fi \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.