Skip to contentSkip to conversation

Revert "usb: gadget: u_audio: add real feedback implementation"

PATCH Superseded Ferry Toth 5 participants 15 messages Aug 24, 2021 – Aug 27, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ferry Toth

author
#0 lore raw

PATCH 1/3 Revert "usb: gadget: u_audio: add real feedback implementation"

This reverts commit e89bb4288378b85c82212b60dc98ecda6b3d3a70.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

---
 .../ABI/testing/configfs-usb-gadget-uac2      |   1 -
 Documentation/usb/gadget-testing.rst          |   1 -
 drivers/usb/gadget/function/f_uac2.c          |   9 +-
 drivers/usb/gadget/function/u_audio.c         | 124 ++----------------
 drivers/usb/gadget/function/u_audio.h         |   9 --
 drivers/usb/gadget/function/u_uac2.h          |   2 -
 6 files changed, 10 insertions(+), 136 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +0 -1
index 26fb8e9b4e61..e7e59d7fb12f 100644
@@ -9,7 +9,6 @@ Description:
 		c_srate    capture sampling rate
 		c_ssize    capture sample size (bytes)
 		c_sync     capture synchronization type (async/adaptive)
-		fb_max     maximum extra bandwidth in async mode
 		p_chmask   playback channel mask
 		p_srate    playback sampling rate
 		p_ssize    playback sample size (bytes)
Documentation/usb/gadget-testing.rst +0 -1
index 9d6276f82774..f5a12667bd41 100644
@@ -729,7 +729,6 @@ The uac2 function provides these attributes in its function directory:
 	c_srate		capture sampling rate
 	c_ssize		capture sample size (bytes)
 	c_sync		capture synchronization type (async/adaptive)
-	fb_max          maximum extra bandwidth in async mode
 	p_chmask	playback channel mask
 	p_srate		playback sampling rate
 	p_ssize		playback sample size (bytes)
drivers/usb/gadget/function/f_uac2.c +1 -8
index ae29ff2b2b68..321e6c05ba93 100644
@@ -584,11 +584,8 @@ static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
 		ssize = uac2_opts->c_ssize;
 	}
 
-	if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
-		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
-
 	max_size_bw = num_channels(chmask) * ssize *
-		DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
+		((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1);
 	ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
 						    max_size_ep));
 
@@ -960,7 +957,6 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	agdev->params.c_srate = uac2_opts->c_srate;
 	agdev->params.c_ssize = uac2_opts->c_ssize;
 	agdev->params.req_number = uac2_opts->req_number;
-	agdev->params.fb_max = uac2_opts->fb_max;
 	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
 	if (ret)
 		goto err_free_descs;
@@ -1333,7 +1329,6 @@ UAC2_ATTRIBUTE(c_srate);
 UAC2_ATTRIBUTE_SYNC(c_sync);
 UAC2_ATTRIBUTE(c_ssize);
 UAC2_ATTRIBUTE(req_number);
-UAC2_ATTRIBUTE(fb_max);
 
 static struct configfs_attribute *f_uac2_attrs[] = {
 	&f_uac2_opts_attr_p_chmask,
@@ -1344,7 +1339,6 @@ static struct configfs_attribute *f_uac2_attrs[] = {
 	&f_uac2_opts_attr_c_ssize,
 	&f_uac2_opts_attr_c_sync,
 	&f_uac2_opts_attr_req_number,
-	&f_uac2_opts_attr_fb_max,
 	NULL,
 };
 
@@ -1384,7 +1378,6 @@ static struct usb_function_instance *afunc_alloc_inst(void)
 	opts->c_ssize = UAC2_DEF_CSSIZE;
 	opts->c_sync = UAC2_DEF_CSYNC;
 	opts->req_number = UAC2_DEF_REQ_NUM;
-	opts->fb_max = UAC2_DEF_FB_MAX;
 	return &opts->func_inst;
 }
 
drivers/usb/gadget/function/u_audio.c +9 -115

202 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.h +0 -9
index a218cdf771fe..53e6baf55cbf 100644
@@ -11,14 +11,6 @@
 
 #include <linux/usb/composite.h>
 
-/*
- * Same maximum frequency deviation on the slower side as in
- * sound/usb/endpoint.c. Value is expressed in per-mil deviation.
- * The maximum deviation on the faster side will be provided as
- * parameter, as it impacts the endpoint required bandwidth.
- */
-#define FBACK_SLOW_MAX	250
-
 struct uac_params {
 	/* playback */
 	int p_chmask;	/* channel mask */
@@ -31,7 +23,6 @@ struct uac_params {
 	int c_ssize;	/* sample size */
 
 	int req_number; /* number of preallocated requests */
-	int fb_max;	/* upper frequency drift feedback limit per-mil */
 };
 
 struct g_audio {
drivers/usb/gadget/function/u_uac2.h +0 -2
index 179d3ef6a195..13589c3c805c 100644
@@ -23,7 +23,6 @@
 #define UAC2_DEF_CSSIZE 2
 #define UAC2_DEF_CSYNC		USB_ENDPOINT_SYNC_ASYNC
 #define UAC2_DEF_REQ_NUM 2
-#define UAC2_DEF_FB_MAX 5
 
 struct f_uac2_opts {
 	struct usb_function_instance	func_inst;
@@ -35,7 +34,6 @@ struct f_uac2_opts {
 	int				c_ssize;
 	int				c_sync;
 	int				req_number;
-	int				fb_max;
 	bool				bound;
 
 	struct mutex			lock;
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH 2/3 Revert "usb: gadget: f_uac2: add adaptive sync support for capture"

This reverts commit 40c73b30546e759bedcec607fedc2d4be954508f.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

---
 .../ABI/testing/configfs-usb-gadget-uac2      |   1 -
 Documentation/usb/gadget-testing.rst          |   1 -
 drivers/usb/gadget/function/f_uac2.c          | 100 ++----------------
 drivers/usb/gadget/function/u_uac2.h          |   2 -
 4 files changed, 9 insertions(+), 95 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-uac2 +0 -1
index e7e59d7fb12f..d4356c8b8cd6 100644
@@ -8,7 +8,6 @@ Description:
 		c_chmask   capture channel mask
 		c_srate    capture sampling rate
 		c_ssize    capture sample size (bytes)
-		c_sync     capture synchronization type (async/adaptive)
 		p_chmask   playback channel mask
 		p_srate    playback sampling rate
 		p_ssize    playback sample size (bytes)
Documentation/usb/gadget-testing.rst +0 -1
index f5a12667bd41..2085e7b24eeb 100644
@@ -728,7 +728,6 @@ The uac2 function provides these attributes in its function directory:
 	c_chmask	capture channel mask
 	c_srate		capture sampling rate
 	c_ssize		capture sample size (bytes)
-	c_sync		capture synchronization type (async/adaptive)
 	p_chmask	playback channel mask
 	p_srate		playback sampling rate
 	p_ssize		playback sample size (bytes)
drivers/usb/gadget/function/f_uac2.c +9 -91

172 lines not on this page · raw message

drivers/usb/gadget/function/u_uac2.h +0 -2
index 13589c3c805c..b5035711172d 100644
@@ -21,7 +21,6 @@
 #define UAC2_DEF_CCHMASK 0x3
 #define UAC2_DEF_CSRATE 64000
 #define UAC2_DEF_CSSIZE 2
-#define UAC2_DEF_CSYNC		USB_ENDPOINT_SYNC_ASYNC
 #define UAC2_DEF_REQ_NUM 2
 
 struct f_uac2_opts {
@@ -32,7 +31,6 @@ struct f_uac2_opts {
 	int				c_chmask;
 	int				c_srate;
 	int				c_ssize;
-	int				c_sync;
 	int				req_number;
 	bool				bound;
 
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v1 2/3] Revert "usb: gadget: f_uac2: add adaptive sync support for capture"

Quoted 27 lines · Ferry Toth

This reverts commit 40c73b30546e759bedcec607fedc2d4be954508f.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Quoted 4 lines

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

Signed-off-by?

Signature · 1 line
balbi
author
#3 lore raw

PATCH 3/3 Revert "usb: gadget: f_uac2/u_audio: add feedback endpoint support"

In reply to Ferry Toth

This reverts commit 24f779dac8f3efb9629adc0e486914d93dc45517.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

---
 drivers/usb/gadget/function/f_uac2.c  |  49 +----------
 drivers/usb/gadget/function/u_audio.c | 119 +-------------------------
 drivers/usb/gadget/function/u_audio.h |   3 -
 3 files changed, 3 insertions(+), 168 deletions(-)
drivers/usb/gadget/function/f_uac2.c +1 -48
index 5d63244ba319..7aa4c8bc5a1a 100644
@@ -240,7 +240,7 @@ static struct usb_interface_descriptor std_as_out_if1_desc = {
 	.bDescriptorType = USB_DT_INTERFACE,
 
 	.bAlternateSetting = 1,
-	.bNumEndpoints = 2,
+	.bNumEndpoints = 1,
 	.bInterfaceClass = USB_CLASS_AUDIO,
 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
 	.bInterfaceProtocol = UAC_VERSION_2,
@@ -317,37 +317,6 @@ static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
 	.wLockDelay = 0,
 };
 
-/* STD AS ISO IN Feedback Endpoint */
-static struct usb_endpoint_descriptor fs_epin_fback_desc = {
-	.bLength = USB_DT_ENDPOINT_SIZE,
-	.bDescriptorType = USB_DT_ENDPOINT,
-
-	.bEndpointAddress = USB_DIR_IN,
-	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
-	.wMaxPacketSize = cpu_to_le16(3),
-	.bInterval = 1,
-};
-
-static struct usb_endpoint_descriptor hs_epin_fback_desc = {
-	.bLength = USB_DT_ENDPOINT_SIZE,
-	.bDescriptorType = USB_DT_ENDPOINT,
-
-	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
-	.wMaxPacketSize = cpu_to_le16(4),
-	.bInterval = 4,
-};
-
-static struct usb_endpoint_descriptor ss_epin_fback_desc = {
-	.bLength = USB_DT_ENDPOINT_SIZE,
-	.bDescriptorType = USB_DT_ENDPOINT,
-
-	.bEndpointAddress = USB_DIR_IN,
-	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
-	.wMaxPacketSize = cpu_to_le16(4),
-	.bInterval = 4,
-};
-
-
 /* Audio Streaming IN Interface - Alt0 */
 static struct usb_interface_descriptor std_as_in_if0_desc = {
 	.bLength = sizeof std_as_in_if0_desc,
@@ -462,7 +431,6 @@ static struct usb_descriptor_header *fs_audio_desc[] = {
 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
 	(struct usb_descriptor_header *)&fs_epout_desc,
 	(struct usb_descriptor_header *)&as_iso_out_desc,
-	(struct usb_descriptor_header *)&fs_epin_fback_desc,
 
 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -493,7 +461,6 @@ static struct usb_descriptor_header *hs_audio_desc[] = {
 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
 	(struct usb_descriptor_header *)&hs_epout_desc,
 	(struct usb_descriptor_header *)&as_iso_out_desc,
-	(struct usb_descriptor_header *)&hs_epin_fback_desc,
 
 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -525,7 +492,6 @@ static struct usb_descriptor_header *ss_audio_desc[] = {
 	(struct usb_descriptor_header *)&ss_epout_desc,
 	(struct usb_descriptor_header *)&ss_epout_desc_comp,
 	(struct usb_descriptor_header *)&as_iso_out_desc,
-	(struct usb_descriptor_header *)&ss_epin_fback_desc,
 
 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
@@ -602,26 +568,22 @@ static void setup_headers(struct f_uac2_opts *opts,
 	struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
 	struct usb_endpoint_descriptor *epout_desc;
 	struct usb_endpoint_descriptor *epin_desc;
-	struct usb_endpoint_descriptor *epin_fback_desc;
 	int i;
 
 	switch (speed) {
 	case USB_SPEED_FULL:
 		epout_desc = &fs_epout_desc;
 		epin_desc = &fs_epin_desc;
-		epin_fback_desc = &fs_epin_fback_desc;
 		break;
 	case USB_SPEED_HIGH:
 		epout_desc = &hs_epout_desc;
 		epin_desc = &hs_epin_desc;
-		epin_fback_desc = &hs_epin_fback_desc;
 		break;
 	default:
 		epout_desc = &ss_epout_desc;
 		epin_desc = &ss_epin_desc;
 		epout_desc_comp = &ss_epout_desc_comp;
 		epin_desc_comp = &ss_epin_desc_comp;
-		epin_fback_desc = &ss_epin_fback_desc;
 	}
 
 	i = 0;
@@ -649,7 +611,6 @@ static void setup_headers(struct f_uac2_opts *opts,
 			headers[i++] = USBDHDR(epout_desc_comp);
 
 		headers[i++] = USBDHDR(&as_iso_out_desc);
-		headers[i++] = USBDHDR(epin_fback_desc);
 	}
 	if (EPIN_EN(opts)) {
 		headers[i++] = USBDHDR(&std_as_in_if0_desc);
@@ -883,12 +844,6 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 			return -ENODEV;
 		}
-		agdev->in_ep_fback = usb_ep_autoconfig(gadget,
-						       &fs_epin_fback_desc);
-		if (!agdev->in_ep_fback) {
-			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-			return -ENODEV;
-		}
 	}
 
 	if (EPIN_EN(uac2_opts)) {
@@ -912,10 +867,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 				le16_to_cpu(ss_epout_desc.wMaxPacketSize));
 
 	hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
-	hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
 	hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
 	ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
-	ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
 	ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
 
 	setup_descriptor(uac2_opts);
drivers/usb/gadget/function/u_audio.c +2 -117

161 lines not on this page · raw message

drivers/usb/gadget/function/u_audio.h +0 -3
index 53e6baf55cbf..5ea6b86f1fda 100644
@@ -30,10 +30,7 @@ struct g_audio {
 	struct usb_gadget *gadget;
 
 	struct usb_ep *in_ep;
-
 	struct usb_ep *out_ep;
-	/* feedback IN endpoint corresponding to out_ep */
-	struct usb_ep *in_ep_fback;
 
 	/* Max packet size for all in_ep possible speeds */
 	unsigned int in_ep_maxpsize;
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v1 3/3] Revert "usb: gadget: f_uac2/u_audio: add feedback endpoint support"

Quoted 27 lines · Ferry Toth

This reverts commit 24f779dac8f3efb9629adc0e486914d93dc45517.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Quoted 4 lines

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

Signed-off-by?

Signature · 1 line
balbi
#5 lore raw
In reply to Ferry Toth
Quoted 27 lines · Ferry Toth

This reverts commit e89bb4288378b85c82212b60dc98ecda6b3d3a70.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Quoted 4 lines

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

this should be Signed-off-by ;-)

Signature · 1 line
balbi
#6 lore raw

Hi,
Op 25-08-2021 om 07:53 schreef Felipe Balbi:

Quoted 30 lines
Quoted 27 lines · Ferry Toth

This reverts commit e89bb4288378b85c82212b60dc98ecda6b3d3a70.

The commit is part of a series with commit 24f779dac8f3efb9629adc0e486914d93dc45517 causing a BUG on dwc3 hardware, at least on Intel Merrifield platform when configured through configfs:

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]

Pavel's suggestion to add
echo "adaptive" > functions/uac2.usb0/c_sync to the configfs script resolves the issue.
Thinh suggests "the crash is probably because of f_uac2 prematurely freeing feedback request before its completion. usb_ep_dequeue() is asynchronous. dwc2() may treat it as a synchronous call so you didn't get a crash."

Quoted 7 lines
Quoted 4 lines

Revert as this is a regression and the kernel shouldn't crash depending on configuration parameters.

this should be Signed-off-by ;-)

Indeed. It probably was until I re-worded the commit text.

Will resend tonight v2.

#7 lore raw

PATCH 1/1 usb: gadget: f_uac2: fixup feedback endpoint stop

In reply to Ferry Toth

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
 dwc3_remove_requests.constprop.0+0x12f/0x170
 __dwc3_gadget_ep_disable+0x7a/0x160
 dwc3_gadget_ep_disable+0x3d/0xd0
 usb_ep_disable+0x1c/0x70
 u_audio_stop_capture+0x79/0x120 [u_audio]
 afunc_set_alt+0x73/0x80 [usb_f_uac2]
 composite_setup+0x224/0x1b90 [libcomposite]

The issue happens only when the gadget is using the sync type "async", not "adaptive". This indicates that problem is likely coming from the feedback endpoint, which is only used with async synchronization mode.

Update the feedback endpoint free function to release the endpoint the same way it is done for the data endpoint.

---

Hi Ferry,

Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

 drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/usb/gadget/function/u_audio.c +11 -4
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct usb_ep *ep,
 	int status = req->status;
 
 	/* i/f shutting down */
-	if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+	if (!prm->fb_ep_enabled) {
+		kfree(req->buf);
+		usb_ep_free_request(ep, req);
+		return;
+	}
+
+	if (req->status == -ESHUTDOWN)
 		return;
 
 	/*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
 	prm->fb_ep_enabled = false;
 
 	if (prm->req_fback) {
-		usb_ep_dequeue(ep, prm->req_fback);
-		kfree(prm->req_fback->buf);
-		usb_ep_free_request(ep, prm->req_fback);
+		if (usb_ep_dequeue(ep, prm->req_fback)) {
+			kfree(prm->req_fback->buf);
+			usb_ep_free_request(ep, prm->req_fback);
+		}
 		prm->req_fback = NULL;
 	}
 
Signature · 1 line
2.33.0
Tested-by on v1 1/1
#8 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Hi,

Op 25-08-2021 om 11:21 schreef Jerome Brunet:

Quoted 23 lines

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]

The issue happens only when the gadget is using the sync type "async", not "adaptive". This indicates that problem is likely coming from the feedback endpoint, which is only used with async synchronization mode.

Update the feedback endpoint free function to release the endpoint the same way it is done for the data endpoint.

Quoted 8 lines

---

Hi Ferry,

Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

Tested this evening and confirming that this resolves my issue. I can't say much about the code itself, maybe Thinh?

Would be great if we could get this in instead of reverting the series.

Quoted 36 lines
  drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
drivers/usb/gadget/function/u_audio.c +11 -4
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct usb_ep *ep,
  	int status = req->status;
 
  	/* i/f shutting down */
-	if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+	if (!prm->fb_ep_enabled) {
+		kfree(req->buf);
+		usb_ep_free_request(ep, req);
+		return;
+	}
+
+	if (req->status == -ESHUTDOWN)
  		return;
 
  	/*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
  	prm->fb_ep_enabled = false;
 
  	if (prm->req_fback) {
-		usb_ep_dequeue(ep, prm->req_fback);
-		kfree(prm->req_fback->buf);
-		usb_ep_free_request(ep, prm->req_fback);
+		if (usb_ep_dequeue(ep, prm->req_fback)) {
+			kfree(prm->req_fback->buf);
+			usb_ep_free_request(ep, prm->req_fback);
+		}
  		prm->req_fback = NULL;
  	}
 
#9 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Quoted 6 lines · Ferry Toth

Hi,

Op 25-08-2021 om 11:21 schreef Jerome Brunet:

Quoted 3 lines

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

The issue isn't hardware specific.

Quoted 13 lines
Quoted 13 lines
BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]
Quoted 5 lines
Quoted 5 lines

The issue happens only when the gadget is using the sync type "async", not
"adaptive". This indicates that problem is likely coming from the feedback
endpoint, which is only used with async synchronization mode.

This does not describe the actual problem. The problem is that the usb_ep_dequeue() can be an asynchronous call, and we can't free the request until its completion (from cancellation).

Quoted 8 lines
Quoted 8 lines

Update the feedback endpoint free function to release the endpoint the same
way it is done for the data endpoint.

---

Quoted 8 lines
Quoted 5 lines

  Hi Ferry,

  Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

Tested this evening and confirming that this resolves my issue. I can't say much about the code itself, maybe Thinh?

Sure. I can take a look.

Quoted 8 lines

Would be great if we could get this in instead of reverting the series.

Quoted 3 lines

  drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----

  1 file changed, 11 insertions(+), 4 deletions(-)
Quoted 11 lines

drivers/usb/gadget/function/u_audio.c static void u_audio_iso_fback_complete(struct · line 233

Quoted 11 lines
drivers/usb/gadget/function/u_audio.c +1 -1
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct
 usb_ep *ep,
       int status = req->status;
         /* i/f shutting down */
-    if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+    if (!prm->fb_ep_enabled) {

prm->fb_ep_enabled is not protected. Potential race problem here?

Quoted 14 lines
Quoted 14 lines
drivers/usb/gadget/function/u_audio.c +6 -2
+        kfree(req->buf);
+        usb_ep_free_request(ep, req);
+        return;
+    }
+
+    if (req->status == -ESHUTDOWN)
           return;
         /*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct
 uac_rtd_params *prm, struct usb_ep *ep)
       prm->fb_ep_enabled = false;
         if (prm->req_fback) {
-        usb_ep_dequeue(ep, prm->req_fback);
-        kfree(prm->req_fback->buf);
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/u_audio.c +4 -1
-        usb_ep_free_request(ep, prm->req_fback);
+        if (usb_ep_dequeue(ep, prm->req_fback)) {
+            kfree(prm->req_fback->buf);
+            usb_ep_free_request(ep, prm->req_fback);
+        }
           prm->req_fback = NULL;
       }

On a separate note, I notice that f_uac2 only queues a single feedback request at a time for isoc endpoint? Even though the interval is 1ms, this will easily cause data drop.

Also, you're ignoring other request error status and still processing bogus data on request completion? That doesn't seem right.

BR,
Thinh

#10 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Quoted 9 lines · Thinh Nguyen
Quoted 6 lines · Ferry Toth

Hi,

Op 25-08-2021 om 11:21 schreef Jerome Brunet:

Quoted 3 lines

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

The issue isn't hardware specific.

While the actual bug isnt, the report was (given the issue did not show up during initial testing but did show on Ferry's HW)
Merely citing the bug report from Ferry here

Quoted 20 lines
Quoted 14 lines
Quoted 14 lines
BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]
Quoted 5 lines
Quoted 5 lines

The issue happens only when the gadget is using the sync type "async", not
"adaptive". This indicates that problem is likely coming from the feedback
endpoint, which is only used with async synchronization mode.

Quoted 3 lines

This does not describe the actual problem. The problem is that the usb_ep_dequeue() can be an asynchronous call, and we can't free the request until its completion (from cancellation).

Indeed. I was not sure at the time.

Quoted 28 lines
Quoted 9 lines
Quoted 9 lines

Update the feedback endpoint free function to release the endpoint the same
way it is done for the data endpoint.

---

Quoted 8 lines
Quoted 5 lines

  Hi Ferry,

  Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

Tested this evening and confirming that this resolves my issue. I can't say much about the code itself, maybe Thinh?

Sure. I can take a look.

Quoted 8 lines

Would be great if we could get this in instead of reverting the series.

Quoted 3 lines

  drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----

  1 file changed, 11 insertions(+), 4 deletions(-)
Quoted 13 lines
Quoted 11 lines

drivers/usb/gadget/function/u_audio.c static void u_audio_iso_fback_complete(struct · line 233

Quoted 11 lines
drivers/usb/gadget/function/u_audio.c +1 -1
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct
 usb_ep *ep,
       int status = req->status;
         /* i/f shutting down */
-    if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+    if (!prm->fb_ep_enabled) {

prm->fb_ep_enabled is not protected. Potential race problem here?

Given how the variable is used, I don't think so.
Could you please detail ?

(I don't think this is really related to the current problem though)

Quoted 28 lines
Quoted 15 lines
Quoted 15 lines
drivers/usb/gadget/function/u_audio.c +6 -2
+        kfree(req->buf);
+        usb_ep_free_request(ep, req);
+        return;
+    }
+
+    if (req->status == -ESHUTDOWN)
           return;
         /*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct
 uac_rtd_params *prm, struct usb_ep *ep)
       prm->fb_ep_enabled = false;
         if (prm->req_fback) {
-        usb_ep_dequeue(ep, prm->req_fback);
-        kfree(prm->req_fback->buf);
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/u_audio.c +4 -1
-        usb_ep_free_request(ep, prm->req_fback);
+        if (usb_ep_dequeue(ep, prm->req_fback)) {
+            kfree(prm->req_fback->buf);
+            usb_ep_free_request(ep, prm->req_fback);
+        }
           prm->req_fback = NULL;
       }

On a separate note, I notice that f_uac2 only queues a single feedback request at a time for isoc endpoint? Even though the interval is 1ms, this will easily cause data drop.

Quoted 2 lines

Also, you're ignoring other request error status and still processing bogus data on request completion? That doesn't seem right.

Gadget is sendind the feedback data, not processing it. Every data sent is OK. Yes, packets can be missed with the current implementation, meaning the feedback value is not reported as often as initially intended. On slower HW, packets are also missed with 2 requests queued, not only on the feedback endpoint but also on the playback endpoint. Picking the approriate value is not straight forward. For the feedback endpoint it isn't big deal because, according to the spec, if the feedback is not sent, the host shall assume the value hasn't changed. This why the whole thing works as it is.

I admit things still aren't perfect, but there is progress ...

Quoted 3 lines

BR,
Thinh

#11 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Quoted 11 lines · Jerome Brunet
Quoted 8 lines · Thinh Nguyen
Quoted 6 lines · Ferry Toth

Hi,

Op 25-08-2021 om 11:21 schreef Jerome Brunet:

Quoted 3 lines

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

Quoted 5 lines
Quoted 1 line

The issue isn't hardware specific.

While the actual bug isnt, the report was (given the issue did not show up during initial testing but did show on Ferry's HW)
Merely citing the bug report from Ferry here

Ok

Quoted 69 lines
Quoted 21 lines
Quoted 15 lines
Quoted 15 lines

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]
Quoted 5 lines
Quoted 5 lines

The issue happens only when the gadget is using the sync type "async", not
"adaptive". This indicates that problem is likely coming from the feedback
endpoint, which is only used with async synchronization mode.

Quoted 3 lines

This does not describe the actual problem. The problem is that the usb_ep_dequeue() can be an asynchronous call, and we can't free the request until its completion (from cancellation).

Indeed. I was not sure at the time.

Quoted 28 lines
Quoted 9 lines
Quoted 9 lines

Update the feedback endpoint free function to release the endpoint the same
way it is done for the data endpoint.

---

Quoted 8 lines
Quoted 5 lines

  Hi Ferry,

  Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

Tested this evening and confirming that this resolves my issue. I can't say much about the code itself, maybe Thinh?

Sure. I can take a look.

Quoted 8 lines

Would be great if we could get this in instead of reverting the series.

Quoted 3 lines

  drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----

  1 file changed, 11 insertions(+), 4 deletions(-)
Quoted 13 lines
Quoted 11 lines

drivers/usb/gadget/function/u_audio.c static void u_audio_iso_fback_complete(struct · line 233

Quoted 11 lines
drivers/usb/gadget/function/u_audio.c +1 -1
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct
 usb_ep *ep,
       int status = req->status;
         /* i/f shutting down */
-    if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+    if (!prm->fb_ep_enabled) {

prm->fb_ep_enabled is not protected. Potential race problem here?

Quoted 3 lines

Given how the variable is used, I don't think so.
Could you please detail ?

I'm thinking of this scenario:

Since free_ep_fback() is called in a separate thread, u_audio_iso_fback_complete() can come in the middle after prm->fb_ep_enabled is cleared but before the usb_ep_dequeue(). So the request may be freed before being accessed again in usb_ep_dequeue().

Quoted 1 line

(I don't think this is really related to the current problem though)

It's related to how we go about to solve the problem right?

Can we just check for the req->status for -ECONNRESET and -ESHUTDOWN and free the request? Those 2 statuses should be for when the request is cancelled and when the endpoint is disabled.

Quoted 29 lines
Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/u_audio.c +6 -2
+        kfree(req->buf);
+        usb_ep_free_request(ep, req);
+        return;
+    }
+
+    if (req->status == -ESHUTDOWN)
           return;
         /*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct
 uac_rtd_params *prm, struct usb_ep *ep)
       prm->fb_ep_enabled = false;
         if (prm->req_fback) {
-        usb_ep_dequeue(ep, prm->req_fback);
-        kfree(prm->req_fback->buf);
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/u_audio.c +4 -1
-        usb_ep_free_request(ep, prm->req_fback);
+        if (usb_ep_dequeue(ep, prm->req_fback)) {
+            kfree(prm->req_fback->buf);
+            usb_ep_free_request(ep, prm->req_fback);
+        }
           prm->req_fback = NULL;
       }

On a separate note, I notice that f_uac2 only queues a single feedback request at a time for isoc endpoint? Even though the interval is 1ms, this will easily cause data drop.

Quoted 4 lines
Quoted 2 lines

Also, you're ignoring other request error status and still processing bogus data on request completion? That doesn't seem right.

Gadget is sendind the feedback data, not processing it. Every data sent

Ah ok.. I missed that it's IN request.

Quoted 3 lines

is OK. Yes, packets can be missed with the current implementation, meaning the feedback value is not reported as often as initially intended. On slower HW, packets are also missed with 2 requests queued,

2 is still too low. From our HW testing, minimum should be 4 to minimize data loss at 1ms interval.

Quoted 5 lines

not only on the feedback endpoint but also on the playback endpoint. Picking the approriate value is not straight forward. For the feedback endpoint it isn't big deal because, according to the spec, if the feedback is not sent, the host shall assume the value hasn't changed. This why the whole thing works as it is.

Why do this when we can avoid it. We know that there will be data drop with the way it's implemented now. There's no option to adjust the number of feedback requests either.

Quoted 3 lines

I admit things still aren't perfect, but there is progress ...

Thanks for the updates to audio/f_uac2. It's been a long time since anyone working on it.

BR,
Thinh

#12 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Quoted 18 lines · Thinh Nguyen
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/usb/gadget/function/u_audio.c +0 -0
index 018dd0978995..63d9340f008e 100644
Quoted 8 lines
Quoted 6 lines

drivers/usb/gadget/function/u_audio.c static void u_audio_iso_fback_complete(struct · line 233

Quoted 6 lines
drivers/usb/gadget/function/u_audio.c +1 -1
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct
 usb_ep *ep,
       int status = req->status;
         /* i/f shutting down */
-    if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+    if (!prm->fb_ep_enabled) {

prm->fb_ep_enabled is not protected. Potential race problem here?

Quoted 3 lines

Given how the variable is used, I don't think so.
Could you please detail ?

Quoted 7 lines

I'm thinking of this scenario:

Since free_ep_fback() is called in a separate thread, u_audio_iso_fback_complete() can come in the middle after prm->fb_ep_enabled is cleared but before the usb_ep_dequeue(). So the request may be freed before being accessed again in usb_ep_dequeue().

You are right - there is short window of opportunity.
Looks like a 2nd separate issue affecting all u_audio endpoints. (Would be solved by reverting the feedback series)

Fix seems fairly simple.

Quoted 8 lines
Quoted 1 line

(I don't think this is really related to the current problem though)

It's related to how we go about to solve the problem right?

Can we just check for the req->status for -ECONNRESET and -ESHUTDOWN and free the request? Those 2 statuses should be for when the request is cancelled and when the endpoint is disabled.

The same problem happened on the data endpoint while feedback was being reviewed. This is why data got fixed, and the feedback endpoint did not.

As explained in the description, the fix is merely aligning what is done for the different endpoints here.

Some of the people who have worked on this are no USB expert (myself included) so I'm sure things can be improved. I'd suggest to take the rework u_audio as a 2nd step.

Quoted 34 lines
Quoted 29 lines
Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/u_audio.c +6 -2
+        kfree(req->buf);
+        usb_ep_free_request(ep, req);
+        return;
+    }
+
+    if (req->status == -ESHUTDOWN)
           return;
         /*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct
 uac_rtd_params *prm, struct usb_ep *ep)
       prm->fb_ep_enabled = false;
         if (prm->req_fback) {
-        usb_ep_dequeue(ep, prm->req_fback);
-        kfree(prm->req_fback->buf);
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/u_audio.c +4 -1
-        usb_ep_free_request(ep, prm->req_fback);
+        if (usb_ep_dequeue(ep, prm->req_fback)) {
+            kfree(prm->req_fback->buf);
+            usb_ep_free_request(ep, prm->req_fback);
+        }
           prm->req_fback = NULL;
       }

On a separate note, I notice that f_uac2 only queues a single feedback request at a time for isoc endpoint? Even though the interval is 1ms, this will easily cause data drop.

Quoted 4 lines
Quoted 2 lines

Also, you're ignoring other request error status and still processing bogus data on request completion? That doesn't seem right.

Gadget is sendind the feedback data, not processing it. Every data sent

Quoted 8 lines

Ah ok.. I missed that it's IN request.

Quoted 3 lines

is OK. Yes, packets can be missed with the current implementation, meaning the feedback value is not reported as often as initially intended. On slower HW, packets are also missed with 2 requests queued,

2 is still too low. From our HW testing, minimum should be 4 to minimize data loss at 1ms interval.

Maybe 4 should be the default then ?

Quoted 8 lines
Quoted 6 lines

not only on the feedback endpoint but also on the playback endpoint. Picking the approriate value is not straight forward. For the feedback endpoint it isn't big deal because, according to the spec, if the feedback is not sent, the host shall assume the value hasn't changed. This why the whole thing works as it is.

Why do this when we can avoid it.

Because it was not known at the time

Quoted 12 lines

We know that there will be data drop
with the way it's implemented now. There's no option to adjust the number of feedback requests either.

Quoted 3 lines

I admit things still aren't perfect, but there is progress ...

Thanks for the updates to audio/f_uac2. It's been a long time since anyone working on it.

BR,
Thinh

#13 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

Quoted 44 lines · Jerome Brunet
Quoted 18 lines · Thinh Nguyen
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/usb/gadget/function/u_audio.c +0 -0
index 018dd0978995..63d9340f008e 100644
Quoted 8 lines
Quoted 6 lines

drivers/usb/gadget/function/u_audio.c static void u_audio_iso_fback_complete(struct · line 233

Quoted 6 lines
drivers/usb/gadget/function/u_audio.c +1 -1
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct
 usb_ep *ep,
       int status = req->status;
         /* i/f shutting down */
-    if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+    if (!prm->fb_ep_enabled) {

prm->fb_ep_enabled is not protected. Potential race problem here?

Quoted 3 lines

Given how the variable is used, I don't think so.
Could you please detail ?

Quoted 7 lines

I'm thinking of this scenario:

Since free_ep_fback() is called in a separate thread, u_audio_iso_fback_complete() can come in the middle after prm->fb_ep_enabled is cleared but before the usb_ep_dequeue(). So the request may be freed before being accessed again in usb_ep_dequeue().

You are right - there is short window of opportunity.
Looks like a 2nd separate issue affecting all u_audio endpoints. (Would be solved by reverting the feedback series)

Fix seems fairly simple.

Quoted 8 lines
Quoted 1 line

(I don't think this is really related to the current problem though)

It's related to how we go about to solve the problem right?

Can we just check for the req->status for -ECONNRESET and -ESHUTDOWN and free the request? Those 2 statuses should be for when the request is cancelled and when the endpoint is disabled.

Quoted 5 lines

The same problem happened on the data endpoint while feedback was being reviewed. This is why data got fixed, and the feedback endpoint did not.

As explained in the description, the fix is merely aligning what is done for the different endpoints here.

IMHO, it'd be better if we can have a proper fix to the newly introduced feedback endpoint so that doesn't leave problems to be patched up later. This makes it easier to backport fix patches that don't touch too many places.

Quoted 4 lines

Some of the people who have worked on this are no USB expert (myself included) so I'm sure things can be improved. I'd suggest to take the rework u_audio as a 2nd step.

Sure. I just wanted to bring them up as I notice them while looking through the reported patch.

Quoted 37 lines
Quoted 37 lines
Quoted 30 lines
Quoted 30 lines
Quoted 17 lines
Quoted 17 lines
drivers/usb/gadget/function/u_audio.c +6 -2
+        kfree(req->buf);
+        usb_ep_free_request(ep, req);
+        return;
+    }
+
+    if (req->status == -ESHUTDOWN)
           return;
         /*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct
 uac_rtd_params *prm, struct usb_ep *ep)
       prm->fb_ep_enabled = false;
         if (prm->req_fback) {
-        usb_ep_dequeue(ep, prm->req_fback);
-        kfree(prm->req_fback->buf);
Quoted 8 lines
Quoted 8 lines
drivers/usb/gadget/function/u_audio.c +4 -1
-        usb_ep_free_request(ep, prm->req_fback);
+        if (usb_ep_dequeue(ep, prm->req_fback)) {
+            kfree(prm->req_fback->buf);
+            usb_ep_free_request(ep, prm->req_fback);
+        }
           prm->req_fback = NULL;
       }

On a separate note, I notice that f_uac2 only queues a single feedback request at a time for isoc endpoint? Even though the interval is 1ms, this will easily cause data drop.

Quoted 4 lines
Quoted 2 lines

Also, you're ignoring other request error status and still processing bogus data on request completion? That doesn't seem right.

Gadget is sendind the feedback data, not processing it. Every data sent

Ah ok.. I missed that it's IN request.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

is OK. Yes, packets can be missed with the current implementation, meaning the feedback value is not reported as often as initially intended. On slower HW, packets are also missed with 2 requests queued,

2 is still too low. From our HW testing, minimum should be 4 to minimize data loss at 1ms interval.

Maybe 4 should be the default then ?

Yes. Would it be a problem?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

not only on the feedback endpoint but also on the playback endpoint. Picking the approriate value is not straight forward. For the feedback endpoint it isn't big deal because, according to the spec, if the feedback is not sent, the host shall assume the value hasn't changed. This why the whole thing works as it is.

Quoted 14 lines
Quoted 1 line

Why do this when we can avoid it.

Because it was not known at the time

Quoted 10 lines

We know that there will be data drop
with the way it's implemented now. There's no option to adjust the number of feedback requests either.

Quoted 3 lines

I admit things still aren't perfect, but there is progress ...

Thanks for the updates to audio/f_uac2. It's been a long time since anyone working on it.

Thanks,
Thinh

#14 lore raw

Re: [PATCH] usb: gadget: f_uac2: fixup feedback endpoint stop

In reply to Ferry Toth
Quoted 35 lines · Ferry Toth

Hi,

Op 25-08-2021 om 11:21 schreef Jerome Brunet:

Quoted 23 lines

When the uac2 function is stopped, there seems to be an issue with some platforms (Intel Merrifield at least)

BUG: kernel NULL pointer dereference, address: 0000000000000008
...
RIP: 0010:dwc3_gadget_del_and_unmap_request+0x19/0xe0
...
Call Trace:
  dwc3_remove_requests.constprop.0+0x12f/0x170
  __dwc3_gadget_ep_disable+0x7a/0x160
  dwc3_gadget_ep_disable+0x3d/0xd0
  usb_ep_disable+0x1c/0x70
  u_audio_stop_capture+0x79/0x120 [u_audio]
  afunc_set_alt+0x73/0x80 [usb_f_uac2]
  composite_setup+0x224/0x1b90 [libcomposite]

The issue happens only when the gadget is using the sync type "async", not "adaptive". This indicates that problem is likely coming from the feedback endpoint, which is only used with async synchronization mode.

Update the feedback endpoint free function to release the endpoint the same way it is done for the data endpoint.

Quoted 8 lines

---

Hi Ferry,

Would you mind trying this before reverting the whole thing ? The HW I have did not show the issue so far so I can't really check if it helps. Hopefully, it does ...

Quoted 7 lines

Tested this evening and confirming that this resolves my issue. I can't say much about the code itself, maybe Thinh?

Would be great if we could get this in instead of reverting the series.

Thanks for reporting and testing this.
I guess we should also add

Quoted 33 lines
Quoted 33 lines
  drivers/usb/gadget/function/u_audio.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
drivers/usb/gadget/function/u_audio.c +11 -4
index 018dd0978995..63d9340f008e 100644
@@ -230,7 +230,13 @@ static void u_audio_iso_fback_complete(struct usb_ep *ep,
  	int status = req->status;
    	/* i/f shutting down */
-	if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN)
+	if (!prm->fb_ep_enabled) {
+		kfree(req->buf);
+		usb_ep_free_request(ep, req);
+		return;
+	}
+
+	if (req->status == -ESHUTDOWN)
  		return;
    	/*
@@ -421,9 +427,10 @@ static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
  	prm->fb_ep_enabled = false;
    	if (prm->req_fback) {
-		usb_ep_dequeue(ep, prm->req_fback);
-		kfree(prm->req_fback->buf);
-		usb_ep_free_request(ep, prm->req_fback);
+		if (usb_ep_dequeue(ep, prm->req_fback)) {
+			kfree(prm->req_fback->buf);
+			usb_ep_free_request(ep, prm->req_fback);
+		}
  		prm->req_fback = NULL;
  	}