linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Stezenbach <js@linuxtv.org>
To: Jiri Kosina <jkosina@suse.cz>
Cc: Florian Mickler <florian@mickler.org>,
	Andy Walls <awalls@md.metrocast.net>,
	mchehab@infradead.org, linux-media@vger.kernel.org,
	linux-kernel@vger.kernel.org, tskd2@yahoo.co.jp, liplianin@me.by,
	g.marco@freenet.de, aet@rasterburn.org, pb@linuxtv.org,
	mkrufky@linuxtv.org, nick@nick-andrew.net, max@veneto.com,
	janne-dvb@grunau.be, Oliver Neukum <oliver@neukum.org>,
	Greg Kroah-Hartman <greg@kroah.com>,
	"Rafael J. Wysocki" <rjw@sisk.pl>,
	Joerg Roedel <joerg.roedel@amd.com>,
	James Bottomley <James.Bottomley@HansenPartnership.com>
Subject: Re: [PATCH 0/6] get rid of on-stack dma buffers
Date: Tue, 22 Mar 2011 13:33:11 +0100	[thread overview]
Message-ID: <20110322123311.GA24812@linuxtv.org> (raw)
In-Reply-To: <alpine.LRH.2.00.1103221157510.8710@twin.jikos.cz>

On Tue, Mar 22, 2011 at 11:59:32AM +0100, Jiri Kosina wrote:
> On Mon, 21 Mar 2011, Florian Mickler wrote:
> 
> > To be blunt, I'm not shure I fully understand the requirements myself. 
> > But as far as I grasped it, the main problem is that we need memory 
> > which the processor can see as soon as the device has scribbled upon it. 
> > (think caches and the like)
> > 
> > Somewhere down the line, the buffer to usb_control_msg get's to be a 
> > parameter to dma_map_single which is described as part of the DMA API in 
> > Documentation/DMA-API.txt
> > 
> > The main point I filter out from that is that the memory has to begin
> > exactly at a cache line boundary... 
> > 
> > I guess (not verified), that the dma api takes sufficient precautions to 
> > abort the dma transfer if a timeout happens.  So freeing _should_ not be 
> > an issue. (At least, I would expect big fat warnings everywhere if that 
> > were the case)
> > 
> > I cc'd some people that hopefully will correct me if I'm wrong...
> 
> It all boils down to making sure that you don't free the memory which is 
> used as target of DMA transfer.
> 
> This is very likely to hit you when DMA memory region is on stack, but 
> blindly just converting this to kmalloc()/kfree() isn't any better if you 
> don't make sure that all the DMA transfers have been finished and device 
> will not be making any more to that particular memory region.

I think it is important that one cache line is not shared between
a DMA buffer and other objects, especially on architectures where
cache coherency is managed in software (ARM, MIPS etc.).  If you
use kmalloc() for the DMA buffer that is guaranteed.
(E.g. DMA API will do writeback/invalidate before the DMA starts, but
if the CPU accesses a variable which is next to the buffer
while DMA is still pending then the whole cacheline is read back into
the cache.  If the CPU is then trying to read the DMA buffer after
the DMA finished it will see stale data from the cache.  You lose.)

It depends on the device doing DMA if it needs special alignment.
If you meet its alignment requirements, and wait for the end of the DMA before
returning, and make sure the buffer does not share cache lines with
neighbouring objects on the stack, then you can use DMA buffers on
stack.  In practice it's simpler and much less error prone to use kmalloc().

Regarding usb_control_msg(), since it is a synchronous API which
waits for the end of the transfer I'm relatively sure there is no
DMA pending when it returns, even if it aborts with timeout or any
other error code.


Johannes

  reply	other threads:[~2011-03-22 13:17 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-21 18:33 [PATCH 0/6] get rid of on-stack dma buffers Florian Mickler
2011-03-21 18:33 ` [PATCH 1/6] [media] a800: " Florian Mickler
2011-03-21 18:33 ` [PATCH 2/6 v2] [media] vp7045: " Florian Mickler
2011-03-21 18:33 ` [PATCH 3/6] [media] friio: " Florian Mickler
2011-03-21 18:33 ` [PATCH 4/6] [media] dw2102: get rid of on-stack dma buffer Florian Mickler
2011-03-21 18:33 ` [PATCH 5/6] [media] m920x: get rid of on-stack dma buffers Florian Mickler
2011-03-21 18:33 ` [PATCH 6/6] [media] opera1: get rid of on-stack dma buffer Florian Mickler
2011-03-21 19:26 ` [PATCH 0/6] get rid of on-stack dma buffers Andy Walls
2011-03-21 21:03   ` Florian Mickler
2011-03-22 10:44     ` Roedel, Joerg
     [not found]       ` <AANLkTimXobrwc-XHgoVN1dD5NCTde64dykbyvtJMo229@mail.gmail.com>
2011-03-22 13:12         ` Oliver Neukum
2011-03-22 14:27       ` Florian Mickler
2011-03-22 10:59     ` Jiri Kosina
2011-03-22 12:33       ` Johannes Stezenbach [this message]
2011-03-22 13:35     ` James Bottomley
2011-03-22 14:02       ` Florian Mickler
2011-03-22 20:15       ` David Miller

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=20110322123311.GA24812@linuxtv.org \
    --to=js@linuxtv.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=aet@rasterburn.org \
    --cc=awalls@md.metrocast.net \
    --cc=florian@mickler.org \
    --cc=g.marco@freenet.de \
    --cc=greg@kroah.com \
    --cc=janne-dvb@grunau.be \
    --cc=jkosina@suse.cz \
    --cc=joerg.roedel@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=liplianin@me.by \
    --cc=max@veneto.com \
    --cc=mchehab@infradead.org \
    --cc=mkrufky@linuxtv.org \
    --cc=nick@nick-andrew.net \
    --cc=oliver@neukum.org \
    --cc=pb@linuxtv.org \
    --cc=rjw@sisk.pl \
    --cc=tskd2@yahoo.co.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).