mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] greybus: svc_watchdog: initialize reset_work only once
@ 2026-11-27  7:52 Jiayi Zhang
  2026-11-27  8:01 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Jiayi Zhang @ 2026-11-27  7:52 UTC (permalink / raw)
  To: Johan Hovold, Alex Elder, Greg Kroah-Hartman
  Cc: Rui Miguel Silva, Farhad Alemi, greybus-dev, linux-kernel,
	syzbot+e6bea924b5ee24f7cb98, syzkaller-bugs, Jiayi Zhang

reset_work is a file-scope delayed work shared by all SVC watchdogs, but
do_work() calls INIT_DELAYED_WORK() on it every time a ping fails, right
before scheduling it to run half a second later. With two host devices,
the watchdog of the second SVC can see its ping fail while the work
scheduled by the first one is still pending. INIT_DELAYED_WORK() then
reinitializes a timer that is still enqueued, and debugobjects reports:

  greybus 1-svc: Resetting the greybus network, watch out!!!
  greybus 2-svc: Resetting the greybus network, watch out!!!
  ODEBUG: init active (active state 0) object: ffffffff9bda1f68 object type: timer_list hint: greybus_reset+0x0/0x50
  WARNING: lib/debugobjects.c:629 at debug_print_object+0x198/0x2b0
  Workqueue: events do_work
  Call Trace:
   __debug_object_init
   timer_init_key
   do_work drivers/greybus/svc_watchdog.c:92
   process_one_work

Without CONFIG_DEBUG_OBJECTS_TIMERS nothing catches this and the timer
is added to the timer wheel a second time while it is still linked there.

Initialize reset_work once with DECLARE_DELAYED_WORK() and only schedule
it from do_work(). schedule_delayed_work() does nothing when the work is
already pending, which is what is wanted here: greybus_reset() resets the
whole greybus network, so one run serves both watchdogs.

Fixes: ed7279ae31b0 ("greybus: svc: add a "watchdog" to check the network health")
Reported-by: syzbot+e6bea924b5ee24f7cb98@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e6bea924b5ee24f7cb98
Link: https://lore.kernel.org/r/CA+0ovCiyBFo_gx4UaRN095=MdWaBgKm=TqA1uYdOu-hb_BPt6A@mail.gmail.com/
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
Compile tested only (allmodconfig, W=1); syzbot has no reproducer for
this report.

AI tools assisted with the analysis, the code and the changelog.

 drivers/greybus/svc_watchdog.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/greybus/svc_watchdog.c b/drivers/greybus/svc_watchdog.c
index b318eb34bcca..11eb7c961b06 100644
--- a/drivers/greybus/svc_watchdog.c
+++ b/drivers/greybus/svc_watchdog.c
@@ -20,8 +20,6 @@ struct gb_svc_watchdog {
 	struct notifier_block pm_notifier;
 };
 
-static struct delayed_work reset_work;
-
 static int svc_watchdog_pm_notifier(struct notifier_block *notifier,
 				    unsigned long pm_event, void *unused)
 {
@@ -61,6 +59,8 @@ static void greybus_reset(struct work_struct *work)
 	call_usermodehelper(start_path, argv, envp, UMH_WAIT_EXEC);
 }
 
+static DECLARE_DELAYED_WORK(reset_work, greybus_reset);
+
 static void do_work(struct work_struct *work)
 {
 	struct gb_svc_watchdog *watchdog;
@@ -89,7 +89,6 @@ static void do_work(struct work_struct *work)
 		} else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO) {
 			dev_err(&svc->dev, "Resetting the greybus network, watch out!!!\n");
 
-			INIT_DELAYED_WORK(&reset_work, greybus_reset);
 			schedule_delayed_work(&reset_work, HZ / 2);
 
 			/*

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] greybus: svc_watchdog: initialize reset_work only once
  2026-11-27  7:52 [PATCH] greybus: svc_watchdog: initialize reset_work only once Jiayi Zhang
@ 2026-11-27  8:01 ` Greg Kroah-Hartman
  2026-11-27 14:14   ` Jiayi Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-27  8:01 UTC (permalink / raw)
  To: Jiayi Zhang
  Cc: Johan Hovold, Alex Elder, Rui Miguel Silva, Farhad Alemi,
	greybus-dev, linux-kernel, syzbot+e6bea924b5ee24f7cb98,
	syzkaller-bugs

On Fri, Nov 27, 2026 at 07:52:43AM +0000, Jiayi Zhang wrote:
> reset_work is a file-scope delayed work shared by all SVC watchdogs, but
> do_work() calls INIT_DELAYED_WORK() on it every time a ping fails, right
> before scheduling it to run half a second later. With two host devices,
> the watchdog of the second SVC can see its ping fail while the work
> scheduled by the first one is still pending. INIT_DELAYED_WORK() then
> reinitializes a timer that is still enqueued, and debugobjects reports:
> 
>   greybus 1-svc: Resetting the greybus network, watch out!!!
>   greybus 2-svc: Resetting the greybus network, watch out!!!
>   ODEBUG: init active (active state 0) object: ffffffff9bda1f68 object type: timer_list hint: greybus_reset+0x0/0x50
>   WARNING: lib/debugobjects.c:629 at debug_print_object+0x198/0x2b0
>   Workqueue: events do_work
>   Call Trace:
>    __debug_object_init
>    timer_init_key
>    do_work drivers/greybus/svc_watchdog.c:92
>    process_one_work

Cool, how are you running this?  What fake devices do you have where you
have two svc hosts?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] greybus: svc_watchdog: initialize reset_work only once
  2026-11-27  8:01 ` Greg Kroah-Hartman
@ 2026-11-27 14:14   ` Jiayi Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Jiayi Zhang @ 2026-11-27 14:14 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Johan Hovold, Alex Elder, Rui Miguel Silva, Farhad Alemi,
	greybus-dev, linux-kernel, syzbot+e6bea924b5ee24f7cb98,
	syzkaller-bugs

On Fri, Nov 27, 2026 at 09:01:56AM +0100, Greg Kroah-Hartman wrote:
> Cool, how are you running this?  What fake devices do you have where you
> have two svc hosts?

When I sent the patch I was not running it: syzbot has no reproducer for
this report, and the analysis was from the source and the syzbot console
logs. The two hosts in those logs are emulated USB devices. syzkaller
connects them through raw-gadget and dummy_hcd with the ID of the ES2
bridge, and es2_ap_driver binds to each of them:

  usb 5-1: New USB device found, idVendor=18d1, idProduct=1eaf, bcdDevice= 0.00
  usb 9-1: New USB device found, idVendor=18d1, idProduct=1eaf, bcdDevice= 0.00

Since then I wrote a small program that does the same on purpose, and
tested the patch with it. It is below. It emulates two ES2 bridges with
raw-gadget on two dummy_hcd instances (dummy_hcd.num=2). Each device
answers the enumeration, the CPort count request and the ARPC requests,
sends the SVC protocol version and hello requests, and then never answers
the watchdog ping. Both pings time out and both watchdogs schedule
reset_work.

Tested in QEMU on mainline (9437ee8b6cf6), x86_64 defconfig and
kvm_guest.config plus GREYBUS_ES2, USB_DUMMY_HCD, USB_RAW_GADGET and
DEBUG_OBJECTS_TIMERS, with the two hello requests 100 ms apart
("es2_two_hosts 2 100"). With one device there is no warning.

- without the patch, 3 of 3 runs:

  greybus 2-svc: Resetting the greybus network, watch out!!!
  greybus 1-svc: Resetting the greybus network, watch out!!!
  ODEBUG: init active (active state 0) object: 00000000a2d4af25 object type: timer_list hint: greybus_reset+0x0/0x40
  WARNING: lib/debugobjects.c:629 at debug_print_object+0x60/0xa0, CPU#0: kworker/0:1/12
  Workqueue: events do_work

- with the patch, 3 of 3 runs: no warning, and greybus_reset() runs once,
  half a second after the first ping failure:

  greybus 1-svc: Resetting the greybus network, watch out!!!
  greybus 2-svc: Resetting the greybus network, watch out!!!
  svc_watchdog: calling "/system/bin/start unipro_reset" to reset greybus network!

Thanks,
Jiayi

---
es2_two_hosts.c, build with "gcc -static -O2 -o es2_two_hosts es2_two_hosts.c -lpthread":

// SPDX-License-Identifier: GPL-2.0
/*
 * Emulate several Greybus ES2 bridges (USB 18d1:1eaf) with raw-gadget on
 * dummy_hcd, so that es2_ap_driver registers one greybus host device and one
 * SVC per emulated device.
 *
 * Each emulated device answers the USB enumeration, the CPort count request
 * and the ARPC requests of the driver, then sends the two SVC requests that
 * bring up the SVC connection (protocol version and hello). It never answers
 * the SVC ping of the watchdog, so the ping of every SVC times out, and all
 * watchdogs schedule the file-scope reset_work within a short time of each
 * other.
 *
 * Needs CONFIG_GREYBUS_ES2, CONFIG_USB_DUMMY_HCD, CONFIG_USB_RAW_GADGET and
 * dummy_hcd.num=<devices> on the kernel command line.
 *
 *   es2_two_hosts [devices (2)] [delay between the hellos in ms (0)] [seconds to run (12)]
 */
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/usb/ch9.h>
#include <linux/usb/raw_gadget.h>

#define GB_APB_REQUEST_CPORT_COUNT	0x04
#define GB_APB_REQUEST_ARPC_RUN		0x12
#define GB_SVC_TYPE_PROTOCOL_VERSION	0x01
#define GB_SVC_TYPE_SVC_HELLO		0x02

#define EP_CPORT_IN	0x81	/* first bulk IN: CPort messages to the host */
#define EP_CPORT_OUT	0x02	/* bulk OUT: CPort messages from the host */
#define EP_ARPC_IN	0x86	/* second bulk IN: ARPC responses */

struct io {
	struct usb_raw_ep_io inner;
	uint8_t data[4096];
};

struct event {
	struct usb_raw_event inner;
	struct usb_ctrlrequest ctrl;
};

struct dev {
	int id, fd;
	int ep_in, ep_out, ep_arpc;
	pthread_t ep0, svc, out;
};

static int ndev = 2, delay_ms, run_s = 12;
static pthread_barrier_t hello_barrier;
static struct timespec t0;

static void say(struct dev *d, const char *fmt, ...)
{
	struct timespec t;
	char buf[256];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
	clock_gettime(CLOCK_MONOTONIC, &t);
	printf("[%7.3f] dev%d: %s\n", (t.tv_sec - t0.tv_sec) + (t.tv_nsec - t0.tv_nsec) / 1e9, d->id, buf);
	fflush(stdout);
}

static void die(struct dev *d, const char *what)
{
	say(d, "%s: %s", what, strerror(errno));
	exit(1);
}

static const struct usb_device_descriptor dev_desc = {
	.bLength = USB_DT_DEVICE_SIZE,
	.bDescriptorType = USB_DT_DEVICE,
	.bcdUSB = 0x0200,
	.bMaxPacketSize0 = 64,
	.idVendor = 0x18d1,
	.idProduct = 0x1eaf,
	.bNumConfigurations = 1,
};

#define EP_DESC(addr) { \
	.bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, \
	.bEndpointAddress = (addr), .bmAttributes = USB_ENDPOINT_XFER_BULK, .wMaxPacketSize = 512 }

static const struct usb_endpoint_descriptor ep_cport_in = EP_DESC(EP_CPORT_IN);
static const struct usb_endpoint_descriptor ep_cport_out = EP_DESC(EP_CPORT_OUT);
static const struct usb_endpoint_descriptor ep_arpc_in = EP_DESC(EP_ARPC_IN);

static int config_desc(uint8_t *p)
{
	struct usb_config_descriptor c = {
		.bLength = USB_DT_CONFIG_SIZE,
		.bDescriptorType = USB_DT_CONFIG,
		.wTotalLength = USB_DT_CONFIG_SIZE + USB_DT_INTERFACE_SIZE + 3 * USB_DT_ENDPOINT_SIZE,
		.bNumInterfaces = 1,
		.bConfigurationValue = 1,
		.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
		.bMaxPower = 50,
	};
	struct usb_interface_descriptor i = {
		.bLength = USB_DT_INTERFACE_SIZE,
		.bDescriptorType = USB_DT_INTERFACE,
		.bNumEndpoints = 3,
		.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
	};
	uint8_t *q = p;

	memcpy(q, &c, c.bLength); q += c.bLength;
	memcpy(q, &i, i.bLength); q += i.bLength;
	memcpy(q, &ep_cport_in, USB_DT_ENDPOINT_SIZE); q += USB_DT_ENDPOINT_SIZE;
	memcpy(q, &ep_arpc_in, USB_DT_ENDPOINT_SIZE); q += USB_DT_ENDPOINT_SIZE;
	memcpy(q, &ep_cport_out, USB_DT_ENDPOINT_SIZE); q += USB_DT_ENDPOINT_SIZE;
	return q - p;
}

/* A greybus message for CPort 0 (the SVC CPort): 8 byte header and payload. */
static void cport_write(struct dev *d, uint16_t id, uint8_t type, const void *payload, int len)
{
	struct io io = { .inner = { .ep = d->ep_in, .length = 8 + len } };

	io.data[0] = 8 + len;
	io.data[2] = id & 0xff;
	io.data[3] = id >> 8;
	io.data[4] = type;
	memcpy(io.data + 8, payload, len);
	if (ioctl(d->fd, USB_RAW_IOCTL_EP_WRITE, &io) < 0)
		die(d, "write to the CPort IN endpoint");
}

/* Bring up the SVC connection, then stay silent. */
static void *svc_thread(void *arg)
{
	struct dev *d = arg;
	static const uint8_t version[2] = { 0, 1 };
	static const uint8_t hello[3] = { 1, 0, 1 };	/* endo id 1, AP interface 1 */

	cport_write(d, 1, GB_SVC_TYPE_PROTOCOL_VERSION, version, sizeof(version));
	say(d, "sent SVC protocol version request");
	pthread_barrier_wait(&hello_barrier);
	if (d->id && delay_ms)
		usleep(d->id * delay_ms * 1000);
	cport_write(d, 2, GB_SVC_TYPE_SVC_HELLO, hello, sizeof(hello));
	say(d, "sent SVC hello request");
	return NULL;
}

/* Take what the host sends on the CPort OUT endpoint, print it, answer nothing. */
static void *out_thread(void *arg)
{
	struct dev *d = arg;

	for (;;) {
		struct io io = { .inner = { .ep = d->ep_out, .length = sizeof(io.data) } };
		int n = ioctl(d->fd, USB_RAW_IOCTL_EP_READ, &io);

		if (n < 0) {
			if (errno == EINTR)
				continue;
			die(d, "read from the CPort OUT endpoint");
		}
		if (n >= 8)
			say(d, "host sent: operation id %u, type 0x%02x, %d bytes%s",
			    io.data[2] | io.data[3] << 8, io.data[4], n,
			    io.data[4] == 0x13 ? " (SVC ping, not answered)" : "");
	}
	return NULL;
}

static int ep_enable(struct dev *d, const struct usb_endpoint_descriptor *desc)
{
	int h = ioctl(d->fd, USB_RAW_IOCTL_EP_ENABLE, desc);

	if (h < 0)
		die(d, "enable endpoint");
	return h;
}

static void ep0_write(struct dev *d, const void *data, int len, int wlength)
{
	struct io io = { .inner = { .length = len < wlength ? len : wlength } };

	memcpy(io.data, data, io.inner.length);
	if (ioctl(d->fd, USB_RAW_IOCTL_EP0_WRITE, &io) < 0)
		say(d, "ep0 write: %s", strerror(errno));
}

static int ep0_read(struct dev *d, struct io *io, int wlength)
{
	int n;

	memset(&io->inner, 0, sizeof(io->inner));
	io->inner.length = wlength;
	n = ioctl(d->fd, USB_RAW_IOCTL_EP0_READ, io);
	if (n < 0)
		say(d, "ep0 read: %s", strerror(errno));
	return n;
}

static void control(struct dev *d, const struct usb_ctrlrequest *c)
{
	uint8_t buf[256];
	struct io io;
	int n;

	if ((c->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
		switch (c->bRequest) {
		case USB_REQ_GET_DESCRIPTOR:
			switch (c->wValue >> 8) {
			case USB_DT_DEVICE:
				ep0_write(d, &dev_desc, sizeof(dev_desc), c->wLength);
				return;
			case USB_DT_CONFIG:
				n = config_desc(buf);
				ep0_write(d, buf, n, c->wLength);
				return;
			}
			break;
		case USB_REQ_SET_CONFIGURATION:
			d->ep_in = ep_enable(d, &ep_cport_in);
			d->ep_arpc = ep_enable(d, &ep_arpc_in);
			d->ep_out = ep_enable(d, &ep_cport_out);
			ioctl(d->fd, USB_RAW_IOCTL_VBUS_DRAW, 50);
			ioctl(d->fd, USB_RAW_IOCTL_CONFIGURE, 0);
			ep0_read(d, &io, 0);
			say(d, "configured");
			pthread_create(&d->out, NULL, out_thread, d);
			pthread_create(&d->svc, NULL, svc_thread, d);
			return;
		case USB_REQ_GET_STATUS:
			memset(buf, 0, 2);
			ep0_write(d, buf, 2, c->wLength);
			return;
		}
		ioctl(d->fd, USB_RAW_IOCTL_EP0_STALL, 0);
		return;
	}

	if (c->bRequestType & USB_DIR_IN) {
		memset(buf, 0, sizeof(buf));
		if (c->bRequest == GB_APB_REQUEST_CPORT_COUNT)
			buf[0] = 32;
		ep0_write(d, buf, c->wLength < sizeof(buf) ? c->wLength : sizeof(buf), c->wLength);
		return;
	}

	n = ep0_read(d, &io, c->wLength);
	if (c->bRequest == GB_APB_REQUEST_ARPC_RUN && n >= 5) {
		/* struct arpc_response_message: id of the request, result 0 */
		struct io rsp = { .inner = { .ep = d->ep_arpc, .length = 3 } };

		rsp.data[0] = io.data[0];
		rsp.data[1] = io.data[1];
		if (ioctl(d->fd, USB_RAW_IOCTL_EP_WRITE, &rsp) < 0)
			say(d, "ARPC response: %s", strerror(errno));
		else
			say(d, "answered ARPC request type 0x%02x", io.data[4]);
	}
}

static void *ep0_thread(void *arg)
{
	struct dev *d = arg;
	struct usb_raw_init init = { .speed = USB_SPEED_HIGH };

	d->fd = open("/dev/raw-gadget", O_RDWR);
	if (d->fd < 0)
		die(d, "open /dev/raw-gadget");
	strcpy((char *)init.driver_name, "dummy_udc");
	snprintf((char *)init.device_name, sizeof(init.device_name), "dummy_udc.%d", d->id);
	if (ioctl(d->fd, USB_RAW_IOCTL_INIT, &init) < 0)
		die(d, "USB_RAW_IOCTL_INIT");
	if (ioctl(d->fd, USB_RAW_IOCTL_RUN, 0) < 0)
		die(d, "USB_RAW_IOCTL_RUN");
	for (;;) {
		struct event e = { .inner = { .length = sizeof(e.ctrl) } };

		if (ioctl(d->fd, USB_RAW_IOCTL_EVENT_FETCH, &e) < 0) {
			if (errno == EINTR)
				continue;
			die(d, "USB_RAW_IOCTL_EVENT_FETCH");
		}
		if (e.inner.type == USB_RAW_EVENT_CONTROL)
			control(d, &e.ctrl);
	}
	return NULL;
}

int main(int argc, char **argv)
{
	struct dev *devs;
	int i;

	if (argc > 1)
		ndev = atoi(argv[1]);
	if (argc > 2)
		delay_ms = atoi(argv[2]);
	if (argc > 3)
		run_s = atoi(argv[3]);
	clock_gettime(CLOCK_MONOTONIC, &t0);
	pthread_barrier_init(&hello_barrier, NULL, ndev);
	devs = calloc(ndev, sizeof(*devs));
	for (i = 0; i < ndev; i++) {
		devs[i].id = i;
		pthread_create(&devs[i].ep0, NULL, ep0_thread, &devs[i]);
	}
	sleep(run_s);
	printf("done after %d s, disconnecting\n", run_s);
	return 0;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27 14:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  7:52 [PATCH] greybus: svc_watchdog: initialize reset_work only once Jiayi Zhang
2026-11-27  8:01 ` Greg Kroah-Hartman
2026-11-27 14:14   ` Jiayi Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®