alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Mack <daniel@caiaq.de>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, Greg KH <gregkh@suse.de>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	USB list <linux-usb@vger.kernel.org>,
	Kernel development list <linux-kernel@vger.kernel.org>,
	Chris Wright <chrisw@sous-sol.org>,
	iommu@lists.linux-foundation.org,
	Andi Kleen <andi@firstfloor.org>,
	Alan Stern <stern@rowland.harvard.edu>,
	Pedro Ribeiro <pedrib@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	David Woodhouse <dwmw2@infradead.org>
Subject: Re: USB transfer_buffer allocations on 64bit systems
Date: Fri, 7 May 2010 09:48:55 +0200	[thread overview]
Message-ID: <20100507074855.GF30801@buzzloop.caiaq.de> (raw)
In-Reply-To: <t2r74fd948d1004191716l49dedc2p25a27da39bcc614a@mail.gmail.com>

On Tue, Apr 20, 2010 at 01:16:58AM +0100, Pedro Ribeiro wrote:
> On 15 April 2010 16:20, Alan Stern <stern@rowland.harvard.edu> wrote:
> > On Thu, 15 Apr 2010, Pedro Ribeiro wrote:
> >
> >> I enabled  CONFIG_DMAR_BROKEN_GFX_WA=y and the result is the same. A
> >> delay in the boot process and usb devices don't work properly,
> >> including my USB mouse.
> >>
> >> Strange, since you have the same platform as me. The extra usb devices
> >> you were seeing are because of my docking station - but it makes no
> >> difference whether I'm docked or not for the purposes of the original
> >> bug or this situation right now. The dmesg I'm attaching is without
> >> the computer being docked.
> >
> > It's not possible to determine the reason for the timeout errors
> > between timestamps 16 and 53 from the small amount of debugging
> > information in the log.  Clearly something is going wrong with the
> > communication between the computer and the EHCI controller.  And
> > clearly the kernel config changes are responsible.
> >
> > But I don't know what to do to track it down any farther.
> >
> > Alan Stern
> >
> >
> 
> I guess this is pretty much a dead end until anyone else can reproduce it!

Hmm, I think I finally found the reason for all this confusion. No idea
why I didn't come up with the following explanation any earlier.

The problem is again (summarized):

On 64bit machines, with 4GB or more, the allocated buffers for USB
transfers might be beyond the 32bit boundary. In this case, the IOMMU
should take care and install DMA bounce buffer to copy over the buffer
before the transfer actually happens. The problem is, however, that this
copy mechanism takes place when the URB with its associated buffer is
submitted, not when the EHCI will actually do the transfer.

In the particular case of audio drivers, though, the contents of the
buffers are likely to change after the submission. What we do here
is that we map the audio stream buffers which are used by ALSA to
the output URBs, so they're filled asychronously. Once the buffer is
actually sent out on the bus, it is believed to contain proper audio
date. If it doesn't, that's due to too tight audio timing or other
problems. This breaks once buffers are magically bounced in the
background.

So - long story short: these audio buffers need to be DMA coherent.

The patch below does that, and Pedro excessively tested this patch for
weeks now :) It was just the final explanation _why_ it does the right
thing that was yet missing.

If nobody has objections, can we still squeeze it into .34?

Thanks,
Daniel


>From 5672ca44b9b4617f6c29c88409da13b1bf475547 Mon Sep 17 00:00:00 2001
From: Daniel Mack <daniel@caiaq.de>
Date: Wed, 7 Apr 2010 01:03:09 +0200
Subject: [PATCH] ALSA: usb/caiaq: use usb_buffer_alloc()

Use usb_buffer_alloc() and usb_buffer_free() for transfer buffers.
We need DMA-coherent memory in this case as buffer contents are likely
to be modified after the URB was submitted, because the URB buffers
are mapped to the audio streams.

On x86_64, buffers allocated with kmalloc() may be beyond the boundaries
of 32bit accessible memory, and DMA bounce buffers will live at other
locations, unaccessible by the driver, and hence outside of the audio
buffer mapping.

Signed-off-by: Daniel Mack <daniel@caiaq.de>
Tested-by: Pedro Ribeiro <pedrib@gmail.com>
Cc: Takashi Iwai <tiwai@suse.de>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Chris Wright <chrisw@sous-sol.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Greg KH <gregkh@suse.de>
Cc: iommu@lists.linux-foundation.org
Cc: Kernel development list <linux-kernel@vger.kernel.org>
Cc: USB list <linux-usb@vger.kernel.org>
Cc: stable@kernel.org
---
 sound/usb/caiaq/audio.c |   57 ++++++++++++++++++++++++++--------------------
 1 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index 4328cad..adbeefd 100644
--- a/sound/usb/caiaq/audio.c
+++ b/sound/usb/caiaq/audio.c
@@ -552,46 +552,47 @@ static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
 	}
 
 	for (i = 0; i < N_URBS; i++) {
-		urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
-		if (!urbs[i]) {
+		struct urb *u = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
+		if (!u) {
 			log("unable to usb_alloc_urb(), OOM!?\n");
 			*ret = -ENOMEM;
 			return urbs;
 		}
 
-		urbs[i]->transfer_buffer =
-			kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
-		if (!urbs[i]->transfer_buffer) {
-			log("unable to kmalloc() transfer buffer, OOM!?\n");
+		urbs[i] = u;
+		u->dev = usb_dev;
+		u->pipe = pipe;
+		u->transfer_buffer_length =
+				FRAMES_PER_URB * BYTES_PER_FRAME;
+		u->context = &dev->data_cb_info[i];
+		u->interval = 1;
+		u->transfer_flags = URB_ISO_ASAP;
+		u->number_of_packets = FRAMES_PER_URB;
+		u->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
+					read_completed : write_completed;
+		u->transfer_buffer = usb_alloc_coherent(usb_dev,
+							u->transfer_buffer_length,
+							GFP_KERNEL, &u->transfer_dma);
+		if (!u->transfer_buffer) {
+			log("usb_alloc_coherent() failed, OOM!?\n");
 			*ret = -ENOMEM;
 			return urbs;
 		}
 
 		for (frame = 0; frame < FRAMES_PER_URB; frame++) {
 			struct usb_iso_packet_descriptor *iso =
-				&urbs[i]->iso_frame_desc[frame];
+					&u->iso_frame_desc[frame];
 
 			iso->offset = BYTES_PER_FRAME * frame;
 			iso->length = BYTES_PER_FRAME;
 		}
-
-		urbs[i]->dev = usb_dev;
-		urbs[i]->pipe = pipe;
-		urbs[i]->transfer_buffer_length = FRAMES_PER_URB
-						* BYTES_PER_FRAME;
-		urbs[i]->context = &dev->data_cb_info[i];
-		urbs[i]->interval = 1;
-		urbs[i]->transfer_flags = URB_ISO_ASAP;
-		urbs[i]->number_of_packets = FRAMES_PER_URB;
-		urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
-					read_completed : write_completed;
 	}
 
 	*ret = 0;
 	return urbs;
 }
 
-static void free_urbs(struct urb **urbs)
+static void free_urbs(struct usb_device *usb_dev, struct urb **urbs)
 {
 	int i;
 
@@ -603,7 +604,10 @@ static void free_urbs(struct urb **urbs)
 			continue;
 
 		usb_kill_urb(urbs[i]);
-		kfree(urbs[i]->transfer_buffer);
+		usb_free_coherent(usb_dev,
+				  urbs[i]->transfer_buffer_length,
+				  urbs[i]->transfer_buffer,
+				  urbs[i]->transfer_dma);
 		usb_free_urb(urbs[i]);
 	}
 
@@ -613,6 +617,7 @@ static void free_urbs(struct urb **urbs)
 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
 {
 	int i, ret;
+	struct usb_device *usb_dev = dev->chip.dev;
 
 	dev->n_audio_in  = max(dev->spec.num_analog_audio_in,
 			       dev->spec.num_digital_audio_in) /
@@ -689,15 +694,15 @@ int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
 	dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
 	if (ret < 0) {
 		kfree(dev->data_cb_info);
-		free_urbs(dev->data_urbs_in);
+		free_urbs(usb_dev, dev->data_urbs_in);
 		return ret;
 	}
 
 	dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
 	if (ret < 0) {
 		kfree(dev->data_cb_info);
-		free_urbs(dev->data_urbs_in);
-		free_urbs(dev->data_urbs_out);
+		free_urbs(usb_dev, dev->data_urbs_in);
+		free_urbs(usb_dev, dev->data_urbs_out);
 		return ret;
 	}
 
@@ -706,10 +711,12 @@ int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
 
 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
 {
+	struct usb_device *usb_dev = dev->chip.dev;
+
 	debug("%s(%p)\n", __func__, dev);
 	stream_stop(dev);
-	free_urbs(dev->data_urbs_in);
-	free_urbs(dev->data_urbs_out);
+	free_urbs(usb_dev, dev->data_urbs_in);
+	free_urbs(usb_dev, dev->data_urbs_out);
 	kfree(dev->data_cb_info);
 }
 
-- 
1.7.1

       reply	other threads:[~2010-05-07  7:49 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <g2h74fd948d1004141820ladc1941eo732d059dd678df0a@mail.gmail.com>
     [not found] ` <Pine.LNX.4.44L0.1004151114490.1562-100000@iolanthe.rowland.org>
     [not found]   ` <t2r74fd948d1004191716l49dedc2p25a27da39bcc614a@mail.gmail.com>
2010-05-07  7:48     ` Daniel Mack [this message]
2010-05-07  9:47       ` USB transfer_buffer allocations on 64bit systems Clemens Ladisch
2010-05-07 10:24         ` Daniel Mack
2010-05-07 14:51           ` [alsa-devel] " Alan Stern
2010-05-10  2:50             ` FUJITA Tomonori
2010-05-10  9:21               ` David Woodhouse
     [not found]                 ` <1273483265.372.3383.camel-uXGAPMMVk8bAQYKIod7YupZV94DADvEd@public.gmane.org>
2010-05-10 14:58                   ` [alsa-devel] " Alan Stern
     [not found]                     ` <Pine.LNX.4.44L0.1005101049100.1626-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-05-11  1:06                       ` FUJITA Tomonori
     [not found]                         ` <20100511100637D.fujita.tomonori-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org>
2010-05-11 14:00                           ` Alan Stern
2010-05-11 14:22                             ` FUJITA Tomonori
2010-05-11 14:24                             ` Konrad Rzeszutek Wilk
2010-05-11 14:38                               ` FUJITA Tomonori
2010-05-11 15:04                                 ` Alan Stern
2010-05-11 15:34                                   ` FUJITA Tomonori
     [not found]           ` <20100507102408.GM30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-05-10 14:31             ` Konrad Rzeszutek Wilk
     [not found]         ` <4BE3E1B9.5020602-P6GI/4k7KOmELgA04lAiVw@public.gmane.org>
2010-05-07 11:42           ` Oliver Neukum
     [not found]             ` <201005071342.34923.oneukum-l3A5Bk7waGM@public.gmane.org>
2010-05-07 11:47               ` Oliver Neukum
2010-05-07 11:58                 ` Daniel Mack
     [not found]                   ` <20100507115810.GN30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-05-07 14:45                     ` [alsa-devel] " Alan Stern
2010-04-07  9:06 Daniel Mack
2010-04-07 14:59 ` Alan Stern
2010-04-07 15:11   ` Daniel Mack
2010-04-07 15:31     ` Greg KH
2010-04-07 15:35       ` Daniel Mack
2010-04-07 15:51         ` Greg KH
     [not found]           ` <20100407155122.GA13974-U8xfFu+wG4EAvxtiuMwx3w@public.gmane.org>
2010-04-07 16:04             ` Alan Stern
2010-04-08  6:09         ` Oliver Neukum
     [not found]           ` <201004080809.11756.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>
2010-04-08 11:07             ` Daniel Mack
2010-04-07 15:55       ` Alan Stern
2010-04-07 16:16         ` Daniel Mack
2010-04-07 16:47           ` Alan Stern
2010-04-07 17:55           ` Takashi Iwai
2010-04-07 17:59             ` Daniel Mack
2010-04-07 18:06               ` Takashi Iwai
2010-04-07 19:13             ` Alan Stern
     [not found]               ` <Pine.LNX.4.44L0.1004071452560.5760-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-04-07 23:59                 ` Robert Hancock
2010-04-08  0:33               ` Greg KH
2010-04-09  0:01                 ` Robert Hancock
     [not found]                   ` <4BBE6E57.6020600-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2010-04-09 16:50                     ` Sarah Sharp
2010-04-09 23:38                       ` Robert Hancock
2010-04-10  8:34                         ` Daniel Mack
2010-04-07 17:52         ` Takashi Iwai
2010-04-07 15:46     ` Alan Stern
2010-04-08  6:12       ` Oliver Neukum
     [not found]         ` <201004080812.04419.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>
2010-04-08 16:59           ` Alan Stern
2010-04-08 21:24             ` Oliver Neukum
2010-04-08 22:20               ` Alan Stern
2010-04-09  6:04                 ` Oliver Neukum
     [not found]                   ` <201004090804.36213.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>
2010-04-09 14:41                     ` Alan Stern
     [not found]                       ` <Pine.LNX.4.44L0.1004091033150.1852-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-04-09 14:50                         ` Oliver Neukum
     [not found]                           ` <201004091650.31488.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>
2010-04-09 15:15                             ` Alan Stern
     [not found]                               ` <Pine.LNX.4.44L0.1004091114500.1852-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-04-09 20:51                                 ` Oliver Neukum
2010-04-09 21:21                                   ` Alan Stern
     [not found]   ` <Pine.LNX.4.44L0.1004071036060.1779-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-04-07 16:54     ` Oliver Neukum
     [not found]       ` <201004071854.55530.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>
2010-04-07 17:00         ` Daniel Mack
2010-04-07 23:55     ` Robert Hancock
     [not found]       ` <4BBD1B6F.3000205-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2010-04-08  2:10         ` Alan Stern
2010-04-08  7:30           ` Daniel Mack
     [not found]             ` <20100408073041.GO30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-08 16:57               ` Alan Stern
2010-04-08 17:17                 ` Pedro Ribeiro
     [not found]                 ` <Pine.LNX.4.44L0.1004081245330.1720-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2010-04-08 23:13                   ` Pedro Ribeiro
2010-04-09 16:01                     ` Alan Stern
2010-04-09 18:09                       ` Daniel Mack
     [not found]                         ` <20100409180942.GK30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-09 18:19                           ` Pedro Ribeiro
     [not found]                             ` <w2r74fd948d1004091119j9f33d8a6kc1824d9243abf38b-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-04-09 19:34                               ` Alan Stern
2010-04-09 20:14                                 ` Daniel Mack
2010-04-10 12:49                             ` Daniel Mack
     [not found]                               ` <20100410124912.GP30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-10 13:21                                 ` Pedro Ribeiro
2010-04-12  8:59   ` Andi Kleen
2010-04-12 11:14     ` Daniel Mack
     [not found]       ` <20100412111439.GU30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-12 11:53         ` Andi Kleen
2010-04-12 12:11           ` Pedro Ribeiro
     [not found]             ` <q2z74fd948d1004120511hebf19eaauc41ee9ed25dea19e-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-04-12 12:12               ` Andi Kleen
2010-04-12 12:32                 ` Daniel Mack
2010-04-12 12:47                   ` Andi Kleen
2010-04-12 12:54                     ` Daniel Mack
2010-04-12 15:43                       ` Andi Kleen
     [not found]                         ` <20100412154323.GP18855-qrUzlfsMFqo/4alezvVtWx2eb7JE58TQ@public.gmane.org>
2010-04-12 16:17                           ` Alan Stern
2010-04-12 16:29                             ` Andi Kleen
2010-04-12 16:57                               ` Alan Stern
2010-04-12 17:15                                 ` Daniel Mack
     [not found]                                   ` <20100412171507.GB30801-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-12 17:22                                     ` Andi Kleen
2010-04-12 17:56                                       ` Daniel Mack
2010-04-13 18:22                                 ` Daniel Mack
     [not found]                                   ` <20100413182233.GR30807-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-13 23:46                                     ` Pedro Ribeiro
2010-04-14 10:09                                       ` Daniel Mack
2010-04-14 10:47                                         ` Pedro Ribeiro
2010-04-14 11:02                                           ` Pedro Ribeiro
     [not found]                                           ` <t2w74fd948d1004140347k3447bffapb73856eacddfde55-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2010-04-14 14:08                                             ` Alan Stern
2010-04-14 16:36                                               ` Daniel Mack
     [not found]                                                 ` <20100414163637.GV30807-ahpEBR4enfnCULTFXS99ULNAH6kLmebB@public.gmane.org>
2010-04-14 17:21                                                   ` Pedro Ribeiro
2010-04-15  7:35                                                     ` Daniel Mack

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=20100507074855.GF30801@buzzloop.caiaq.de \
    --to=daniel@caiaq.de \
    --cc=akpm@linux-foundation.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=andi@firstfloor.org \
    --cc=chrisw@sous-sol.org \
    --cc=dwmw2@infradead.org \
    --cc=gregkh@suse.de \
    --cc=iommu@lists.linux-foundation.org \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pedrib@gmail.com \
    --cc=stern@rowland.harvard.edu \
    --cc=tiwai@suse.de \
    /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).