All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, tglx@linutronix.de,
	Jaroslav Kysela <perex@perex.cz>,
	linux-usb@vger.kernel.org
Subject: [8/9] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb()
Date: Wed, 20 Jun 2018 16:39:22 +0200	[thread overview]
Message-ID: <20180620143922.nkcazvbcrmkyvm2c@linutronix.de> (raw)

On 2018-06-20 14:51:12 [+0200], Takashi Iwai wrote:
> On Tue, 19 Jun 2018 23:55:20 +0200,
> Sebastian Andrzej Siewior wrote:
> > -			(*purb)->transfer_buffer =
> > -				kmalloc_array(subs->maxpacksize,
> > -					      nr_of_packs(), GFP_KERNEL);
> > -			if (NULL == (*purb)->transfer_buffer) {
> > +			len = subs->maxpacksize * nr_of_packs();
> > +			buf = kmalloc(len, GFP_KERNEL);
> 
> I'd keep kmalloc_array() as is, and just put subs->maxpacksize *
> nr_of_packs() in usb_fill_int_urb().  Otherwise it's a step backward.

but then we end up with two multiplications. What about pulling the
overflow-mul macro out of malloc_array() and doing this instead:

----------- >8

Subject: [PATCH 8/9 v2] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb()

Using usb_fill_int_urb() helps to find code which initializes an
URB. A grep for members of the struct (like ->complete) reveal lots
of other things, too.

The "&& !(*purb)->transfer_buffer" check has been removed because the
URB has been freshly allocated a few lines above so ->transfer_buffer
has to be NULL here.
The `dev' and `transfer_size' assignments have been moved from
usX2Y_urbs_start() to usX2Y_urbs_allocate() because they don't change
overtime.
The multiplication via check_mul_overflow() has been extracted from
kmalloc_array() to avoid two multiplication (one with overflow check and
one without for the length argument). This requires to change the type
`maxpacksize' to int so there is only one type involved in the
multiplication.

Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Takashi Iwai <tiwai@suse.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 sound/usb/usx2y/usbusx2y.h      |  2 +-
 sound/usb/usx2y/usbusx2yaudio.c | 38 ++++++++++++++++-----------------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/sound/usb/usx2y/usbusx2y.h b/sound/usb/usx2y/usbusx2y.h
index e0f77172ce8f..2bec412589b4 100644
--- a/sound/usb/usx2y/usbusx2y.h
+++ b/sound/usb/usx2y/usbusx2y.h
@@ -56,7 +56,7 @@ struct snd_usX2Y_substream {
 	struct snd_pcm_substream *pcm_substream;
 
 	int			endpoint;		
-	unsigned int		maxpacksize;		/* max packet size in bytes */
+	int			maxpacksize;		/* max packet size in bytes */
 
 	atomic_t		state;
 #define state_STOPPED	0
diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
index 2b833054e3b0..e5580cb59858 100644
--- a/sound/usb/usx2y/usbusx2yaudio.c
+++ b/sound/usb/usx2y/usbusx2yaudio.c
@@ -425,33 +425,35 @@ static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
 	/* allocate and initialize data urbs */
 	for (i = 0; i < NRURBS; i++) {
 		struct urb **purb = subs->urb + i;
+		void *buf = NULL;
+		int len = 0;
+
 		if (*purb) {
 			usb_kill_urb(*purb);
 			continue;
 		}
 		*purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
-		if (NULL == *purb) {
-			usX2Y_urbs_release(subs);
-			return -ENOMEM;
-		}
-		if (!is_playback && !(*purb)->transfer_buffer) {
+		if (NULL == *purb)
+			goto err_free;
+
+		if (!is_playback) {
 			/* allocate a capture buffer per urb */
-			(*purb)->transfer_buffer =
-				kmalloc_array(subs->maxpacksize,
-					      nr_of_packs(), GFP_KERNEL);
-			if (NULL == (*purb)->transfer_buffer) {
-				usX2Y_urbs_release(subs);
-				return -ENOMEM;
-			}
+			if (check_mul_overflow(subs->maxpacksize,
+					       nr_of_packs(),
+					       &len))
+				goto err_free;
+			buf = kmalloc(len, GFP_KERNEL);
+			if (!buf)
+				goto err_free;
 		}
-		(*purb)->dev = dev;
-		(*purb)->pipe = pipe;
+		usb_fill_int_urb(*purb, dev, pipe, buf, len,
+				 i_usX2Y_subs_startup, subs, 1);
 		(*purb)->number_of_packets = nr_of_packs();
-		(*purb)->context = subs;
-		(*purb)->interval = 1;
-		(*purb)->complete = i_usX2Y_subs_startup;
 	}
 	return 0;
+err_free:
+	usX2Y_urbs_release(subs);
+	return -ENOMEM;
 }
 
 static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
@@ -485,12 +487,10 @@ static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
 			unsigned long pack;
 			if (0 == i)
 				atomic_set(&subs->state, state_STARTING3);
-			urb->dev = usX2Y->dev;
 			for (pack = 0; pack < nr_of_packs(); pack++) {
 				urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
 				urb->iso_frame_desc[pack].length = subs->maxpacksize;
 			}
-			urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 
 			if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
 				snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
 				err = -EPIPE;

WARNING: multiple messages have this Message-ID (diff)
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, linux-usb@vger.kernel.org,
	tglx@linutronix.de
Subject: Re: [PATCH 8/9] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb()
Date: Wed, 20 Jun 2018 16:39:22 +0200	[thread overview]
Message-ID: <20180620143922.nkcazvbcrmkyvm2c@linutronix.de> (raw)
In-Reply-To: <s5hr2l1mxj3.wl-tiwai@suse.de>

On 2018-06-20 14:51:12 [+0200], Takashi Iwai wrote:
> On Tue, 19 Jun 2018 23:55:20 +0200,
> Sebastian Andrzej Siewior wrote:
> > -			(*purb)->transfer_buffer =
> > -				kmalloc_array(subs->maxpacksize,
> > -					      nr_of_packs(), GFP_KERNEL);
> > -			if (NULL == (*purb)->transfer_buffer) {
> > +			len = subs->maxpacksize * nr_of_packs();
> > +			buf = kmalloc(len, GFP_KERNEL);
> 
> I'd keep kmalloc_array() as is, and just put subs->maxpacksize *
> nr_of_packs() in usb_fill_int_urb().  Otherwise it's a step backward.

but then we end up with two multiplications. What about pulling the
overflow-mul macro out of malloc_array() and doing this instead:

----------- >8

Subject: [PATCH 8/9 v2] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb()

Using usb_fill_int_urb() helps to find code which initializes an
URB. A grep for members of the struct (like ->complete) reveal lots
of other things, too.

The "&& !(*purb)->transfer_buffer" check has been removed because the
URB has been freshly allocated a few lines above so ->transfer_buffer
has to be NULL here.
The `dev' and `transfer_size' assignments have been moved from
usX2Y_urbs_start() to usX2Y_urbs_allocate() because they don't change
overtime.
The multiplication via check_mul_overflow() has been extracted from
kmalloc_array() to avoid two multiplication (one with overflow check and
one without for the length argument). This requires to change the type
`maxpacksize' to int so there is only one type involved in the
multiplication.

Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Takashi Iwai <tiwai@suse.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 sound/usb/usx2y/usbusx2y.h      |  2 +-
 sound/usb/usx2y/usbusx2yaudio.c | 38 ++++++++++++++++-----------------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/sound/usb/usx2y/usbusx2y.h b/sound/usb/usx2y/usbusx2y.h
index e0f77172ce8f..2bec412589b4 100644
--- a/sound/usb/usx2y/usbusx2y.h
+++ b/sound/usb/usx2y/usbusx2y.h
@@ -56,7 +56,7 @@ struct snd_usX2Y_substream {
 	struct snd_pcm_substream *pcm_substream;
 
 	int			endpoint;		
-	unsigned int		maxpacksize;		/* max packet size in bytes */
+	int			maxpacksize;		/* max packet size in bytes */
 
 	atomic_t		state;
 #define state_STOPPED	0
diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
index 2b833054e3b0..e5580cb59858 100644
--- a/sound/usb/usx2y/usbusx2yaudio.c
+++ b/sound/usb/usx2y/usbusx2yaudio.c
@@ -425,33 +425,35 @@ static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
 	/* allocate and initialize data urbs */
 	for (i = 0; i < NRURBS; i++) {
 		struct urb **purb = subs->urb + i;
+		void *buf = NULL;
+		int len = 0;
+
 		if (*purb) {
 			usb_kill_urb(*purb);
 			continue;
 		}
 		*purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
-		if (NULL == *purb) {
-			usX2Y_urbs_release(subs);
-			return -ENOMEM;
-		}
-		if (!is_playback && !(*purb)->transfer_buffer) {
+		if (NULL == *purb)
+			goto err_free;
+
+		if (!is_playback) {
 			/* allocate a capture buffer per urb */
-			(*purb)->transfer_buffer =
-				kmalloc_array(subs->maxpacksize,
-					      nr_of_packs(), GFP_KERNEL);
-			if (NULL == (*purb)->transfer_buffer) {
-				usX2Y_urbs_release(subs);
-				return -ENOMEM;
-			}
+			if (check_mul_overflow(subs->maxpacksize,
+					       nr_of_packs(),
+					       &len))
+				goto err_free;
+			buf = kmalloc(len, GFP_KERNEL);
+			if (!buf)
+				goto err_free;
 		}
-		(*purb)->dev = dev;
-		(*purb)->pipe = pipe;
+		usb_fill_int_urb(*purb, dev, pipe, buf, len,
+				 i_usX2Y_subs_startup, subs, 1);
 		(*purb)->number_of_packets = nr_of_packs();
-		(*purb)->context = subs;
-		(*purb)->interval = 1;
-		(*purb)->complete = i_usX2Y_subs_startup;
 	}
 	return 0;
+err_free:
+	usX2Y_urbs_release(subs);
+	return -ENOMEM;
 }
 
 static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
@@ -485,12 +487,10 @@ static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
 			unsigned long pack;
 			if (0 == i)
 				atomic_set(&subs->state, state_STARTING3);
-			urb->dev = usX2Y->dev;
 			for (pack = 0; pack < nr_of_packs(); pack++) {
 				urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
 				urb->iso_frame_desc[pack].length = subs->maxpacksize;
 			}
-			urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 
 			if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
 				snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
 				err = -EPIPE;
-- 
2.17.1

             reply	other threads:[~2018-06-20 14:39 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-20 14:39 Sebastian Andrzej Siewior [this message]
2018-06-20 14:39 ` [PATCH 8/9] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb() Sebastian Andrzej Siewior
  -- strict thread matches above, loose matches on Subject: below --
2018-06-22 10:14 [6/9] ALSA: usb: caiaq: " Sebastian Andrzej Siewior
2018-06-22 10:14 ` [PATCH 6/9] " Sebastian Andrzej Siewior
2018-06-22 10:01 [6/9] " Daniel Mack
2018-06-22 10:01 ` [PATCH 6/9] " Daniel Mack
2018-06-22  8:49 [6/9] " Takashi Iwai
2018-06-22  8:49 ` [PATCH 6/9] " Takashi Iwai
2018-06-21 21:16 [6/9] " Sebastian Andrzej Siewior
2018-06-21 21:16 ` [PATCH 6/9] " Sebastian Andrzej Siewior
2018-06-21 20:19 [6/9] " Daniel Mack
2018-06-21 20:19 ` [PATCH 6/9] " Daniel Mack
2018-06-21 20:18 [5/9] ALSA: usb: caiaq: audio: use irqsave() in USB's complete callback Daniel Mack
2018-06-21 20:18 ` [PATCH 5/9] " Daniel Mack
2018-06-20 16:20 [8/9] ALSA: usx2y: usbusx2yaudio: use usb_fill_int_urb() Takashi Iwai
2018-06-20 16:20 ` [PATCH 8/9] " Takashi Iwai
2018-06-20 15:47 [8/9] " Sebastian Andrzej Siewior
2018-06-20 15:47 ` [PATCH 8/9] " Sebastian Andrzej Siewior
2018-06-20 15:38 [8/9] " Sebastian Andrzej Siewior
2018-06-20 15:38 ` [PATCH 8/9] " Sebastian Andrzej Siewior
2018-06-20 14:52 [8/9] " Takashi Iwai
2018-06-20 14:52 ` [PATCH 8/9] " Takashi Iwai
2018-06-20 14:00 [4/9,v2] ALSA: usb-audio: " Takashi Iwai
2018-06-20 14:00 ` [PATCH 4/9 v2] " Takashi Iwai
2018-06-20 13:56 [4/9,v2] " Takashi Iwai
2018-06-20 13:56 ` [PATCH 4/9 v2] " Takashi Iwai
2018-06-20 13:52 [4/9,v2] " Sebastian Andrzej Siewior
2018-06-20 13:52 ` [PATCH 4/9 v2] " Sebastian Andrzej Siewior
2018-06-20 13:21 [4/9,v2] " Takashi Iwai
2018-06-20 13:21 ` [PATCH 4/9 v2] " Takashi Iwai
2018-06-20 12:51 [8/9] ALSA: usx2y: usbusx2yaudio: " Takashi Iwai
2018-06-20 12:51 ` [PATCH 8/9] " Takashi Iwai
2018-06-20  9:38 [4/9,v2] ALSA: usb-audio: " Sebastian Andrzej Siewior
2018-06-20  9:38 ` [PATCH 4/9 v2] " Sebastian Andrzej Siewior
2018-06-20  8:23 [4/9] " Sergei Shtylyov
2018-06-20  8:23 ` [PATCH 4/9] " Sergei Shtylyov
2018-06-19 21:55 [9/9] ALSA: usx2y: usx2yhwdeppcm: " Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 9/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [8/9] ALSA: usx2y: usbusx2yaudio: " Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 8/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [7/9] ALSA: usb-midi: use irqsave() in USB's complete callback Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 7/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [6/9] ALSA: usb: caiaq: use usb_fill_int_urb() Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 6/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [5/9] ALSA: usb: caiaq: audio: use irqsave() in USB's complete callback Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 5/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [4/9] ALSA: usb-audio: use usb_fill_int_urb() Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 4/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [3/9] ALSA: ua101: " Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 3/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [2/9] ALSA: line6: " Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 2/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 [1/9] ALSA: 6fire: " Sebastian Andrzej Siewior
2018-06-19 21:55 ` [PATCH 1/9] " Sebastian Andrzej Siewior
2018-06-19 21:55 ALSA: use irqsave() in URB completion + usb_fill_int_urb Sebastian Andrzej Siewior
2018-06-20 12:55 ` Takashi Iwai

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=20180620143922.nkcazvbcrmkyvm2c@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tglx@linutronix.de \
    --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 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.