Skip to contentSkip to conversation

char: xillybus: Fix issues reported by Sashiko

PATCH New Eli Billauer 1 participant 9 messages Nov 20

Subsystems (2)
lore
Share & tools
lore

Eli Billauer

author maintainer
#0 lore raw

v3 cover char: xillybus: Fix issues reported by Sashiko

This series is a follow-up to the previously submitted patchset [1]. It addresses Sashiko review comments on surrounding code. This v3 series is the result of Sashiko's comments on v2 [3], which in turn was the result of comments on v1 [2]. Another reason for this v3 is to rebase after the insertion of commit 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family").

One Sashiko comment from [2] is not addressed in this patch set:

"Can unbinding the device while a file descriptor is open cause a use-after-free in xillybus_write()?" This remark is correct, but will be fixed in a future set of patches, which makes the PCIe driver hot-pluggable.

The comment about autoflush_skipped in [2] was repeated with a better motivation in Sashiko's comment [3], so it's followed in this patch set.

Regards,
Eli

[1] https://lore.kernel.org/all/[email protected]/
[2] https://sashiko.dev/#/patchset/[email protected]
[3] https://sashiko.dev/#/patchset/[email protected]

Eli Billauer (8):
char: xillybus: Improve sanity check
char: xillybus: Safer access of unaligned pointers
char: xillybus: Return error in fops read method handler
char: xillybus: Rely on FMODE flags in poll method
char: xillybus: Refactor flush function
char: xillybus: Correct handling of wait queue
char: xillybus: Avoid indefinite sleep of work item
char: xillybus: Cancel delayed work items before teardown

 drivers/char/xillybus/xillybus.h      |   3 +
 drivers/char/xillybus/xillybus_core.c | 109 ++++++++++++-----
 drivers/char/xillybus/xillyusb.c      | 166 +++++++++++---------------
 3 files changed, 153 insertions(+), 125 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v3 1/8 char: xillybus: Improve sanity check

The sanity check in fifo_init() added in commit 1ce74f6f2216 ("char:
xillybus: Add defensive sanity checks") is unnecessarily complicated and may not work as expected due to the incorrect assumption that 1 << n == 0 when n>31 due to overflow. Replace it with a single check at the function's invocation. This check also eliminates any possibility for overflow in this function's left-shift operations.

Fixes 1ce74f6f2216 ("char: xillybus: Add defensive sanity checks")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- New patch, replacing v1's "char: xillybus: Use unsigned int literal". Following Sashiko's review, reject log2_size > ilog2(SZ_1G) before the left-shift operations, instead of relying on the overflow check, which a shift by 32 bits or more could bypass.

 drivers/char/xillybus/xillyusb.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/char/xillybus/xillyusb.c +3 -6
index bd3babd39f35..c4ddde221f0b 100644
@@ -374,6 +374,9 @@ static int fifo_init(struct xillyfifo *fifo,
 
 	unsigned int log2_fifo_buf_size;
 
+	if (log2_size > ilog2(SZ_1G))
+		return -ENOMEM; /* Reported as greed for memory */
+
 	mutex_lock(&fifo_buf_order_mutex);
 
 retry:
@@ -390,12 +393,6 @@ static int fifo_init(struct xillyfifo *fifo,
 	fifo->bufnum = 1 << log2_bufnum;
 	fifo->size = fifo->bufnum * fifo->bufsize;
 
-	if (!fifo->size || /* Unsigned integer overflow */
-	    fifo->size > SZ_1G) { /* Avoid signed int issues */
-		mutex_unlock(&fifo_buf_order_mutex);
-		return -ENOMEM; /* Reported as greed for memory */
-	}
-
 	fifo->mem = kmalloc_array(fifo->bufnum, sizeof(void *), GFP_KERNEL);
 
 	if (!fifo->mem) {
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v3 2/8 char: xillybus: Safer access of unaligned pointers

In reply to Eli Billauer

Use get_unaligned_le16() rather than dereferencing unaligned pointers directly for portability.

Fixes a53d1202aef1 ("char: xillybus: Add driver for XillyUSB (Xillybus variant for USB)")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillyusb.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/char/xillybus/xillyusb.c +4 -3
index c4ddde221f0b..16d6101d9269 100644
@@ -17,6 +17,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <asm/byteorder.h>
+#include <linux/unaligned.h>
 #include <linux/io.h>
 #include <linux/interrupt.h>
 #include <linux/sched.h>
@@ -1957,8 +1958,8 @@ static int setup_channels(struct xillyusb_dev *xdev,
 	new_channels = chan;
 
 	for (i = 0; i < num_channels; i++, chan++) {
-		unsigned int in_desc = le16_to_cpu(*chandesc++);
-		unsigned int out_desc = le16_to_cpu(*chandesc++);
+		unsigned int in_desc = get_unaligned_le16(chandesc++);
+		unsigned int out_desc = get_unaligned_le16(chandesc++);
 
 		chan->xdev = xdev;
 		mutex_init(&chan->in_mutex);
@@ -2105,7 +2106,7 @@ static int xillyusb_discovery(struct usb_interface *interface)
 
 	/* Phase II: Set up the streams as defined in IDT */
 
-	num_channels = le16_to_cpu(*((__le16 *)(idt + 1)));
+	num_channels = get_unaligned_le16(idt + 1);
 	names_offset = 3 + num_channels * 4;
 	idt_len -= 4; /* Exclude CRC */
 
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v3 3/8 char: xillybus: Return error in fops read method handler

In reply to Eli Billauer

Be sure to return an error status from the driver's read fops method when the USB device is unplugged or a hardware error occurs, and all available data has been delivered.

Before this patch, the return value was zero in this case, meaning EOF, which didn't expose the reason for ending the data stream.

The existing memory barrier smp_rmb(), together with the assignment of xdev->error in report_io_error() with a spinlock taken, ensure that xdev->error has its updated value when the hereby corrected assignment takes place.

Fixes a53d1202aef1 ("char: xillybus: Add driver for XillyUSB (Xillybus variant for USB)")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillyusb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/xillybus/xillyusb.c +1 -1
index 16d6101d9269..1aa8e7303ad1 100644
@@ -1541,7 +1541,7 @@ static ssize_t xillyusb_read(struct file *filp, char __user *userbuf,
 			if (READ_ONCE(fifo->fill))
 				continue;
 
-			rc = 0;
+			rc = xdev->error; /* Usually zero, meaning EOF */
 			break;
 		}
 
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

PATCH v3 4/8 char: xillybus: Rely on FMODE flags in poll method

In reply to Eli Billauer

The poll method of xillyusb.c previously tested chan->in_fifo and chan->out_ep for being NULL in order to determine if the file is opened for read and/or write, respectively. This works incorrectly if two files are opened on the same device file, one for read and one for write.

In particular, if the release method is called for the file opened for read while a poll is called for the file opened for write (and vice versa), a race condition could lead to access of freed memory.

Relying on the FMODE flags as a guarantee for the validity of chan->in_fifo and chan->out_ep as pointers to allocated memory is safe, as the allocation and deallocation occurs with the open and release methods for each file.

Fixes a53d1202aef1 ("char: xillybus: Add driver for XillyUSB (Xillybus variant for USB)")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillyusb.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/char/xillybus/xillyusb.c +8 -5
index 1aa8e7303ad1..f9c8591a679f 100644
@@ -1853,10 +1853,13 @@ static __poll_t xillyusb_poll(struct file *filp, poll_table *wait)
 	struct xillyusb_channel *chan = filp->private_data;
 	__poll_t mask = 0;
 
-	if (chan->in_fifo)
+	bool open_for_read = !!(filp->f_mode & FMODE_READ);
+	bool open_for_write = !!(filp->f_mode & FMODE_WRITE);
+
+	if (open_for_read)
 		poll_wait(filp, &chan->in_fifo->waitq, wait);
 
-	if (chan->out_ep)
+	if (open_for_write)
 		poll_wait(filp, &chan->out_ep->fifo.waitq, wait);
 
 	/*
@@ -1869,7 +1872,7 @@ static __poll_t xillyusb_poll(struct file *filp, poll_table *wait)
 	 * wait queue.
 	 */
 
-	if (!chan->poll_used && chan->in_fifo) {
+	if (!chan->poll_used && open_for_read) {
 		chan->poll_used = 1;
 		request_read_anything(chan, OPCODE_SET_PUSH);
 	}
@@ -1881,11 +1884,11 @@ static __poll_t xillyusb_poll(struct file *filp, poll_table *wait)
 	 * which it may not. So make it never work.
 	 */
 
-	if (chan->in_fifo && !chan->in_synchronous &&
+	if (open_for_read && !chan->in_synchronous &&
 	    (READ_ONCE(chan->in_fifo->fill) || !chan->read_data_ok))
 		mask |= EPOLLIN | EPOLLRDNORM;
 
-	if (chan->out_ep &&
+	if (open_for_write &&
 	    (READ_ONCE(chan->out_ep->fifo.fill) != chan->out_ep->fifo.size))
 		mask |= EPOLLOUT | EPOLLWRNORM;
 
Signature · 1 line
2.17.1
author maintainer
#5 lore raw

PATCH v3 5/8 char: xillybus: Refactor flush function

In reply to Eli Billauer

Previous flush_downstream() looped on wait_event_interruptible() and wait_event_interruptible_timeout() without returning if an interrupt occurred. Replace these with non-interruptible counterparts and use wake_up() instead of wake_up_interruptible() on the relevant wait queue.

In addition, flush_downstream()'s @interruptible argument is removed. Instead, this function either waits with a timeout only, or with an interruptible wait. This is what the callers of this function requested anyhow.

Fixes a53d1202aef1 ("char: xillybus: Add driver for XillyUSB (Xillybus variant for USB)")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- Always call wait_event_timeout(), with a timeout of max(1L, left_to_sleep), so that the wait condition is checked even when the deadline has already passed. This addresses Sashiko's comment about a missing condition check in flush_downstream().
- Added include of <linux/minmax.h> for max().
- Added Fixes: tag.
- Added Link to the v1 review.

 drivers/char/xillybus/xillyusb.c | 86 ++++++++++++++------------------
 1 file changed, 37 insertions(+), 49 deletions(-)
drivers/char/xillybus/xillyusb.c +37 -49
index f9c8591a679f..decc87f8e059 100644
@@ -12,6 +12,7 @@
  */
 
 #include <linux/types.h>
+#include <linux/minmax.h>
 #include <linux/slab.h>
 #include <linux/list.h>
 #include <linux/device.h>
@@ -601,7 +602,7 @@ static void wakeup_all(struct work_struct *work)
 
 		mutex_unlock(&chan->lock);
 
-		wake_up_interruptible(&chan->flushq);
+		wake_up(&chan->flushq);
 	}
 
 	mutex_unlock(&xdev->process_in_mutex);
@@ -920,12 +921,12 @@ static int process_in_opcode(struct xillyusb_dev *xdev,
 
 	case OPCODE_REACHED_CHECKPOINT:
 		chan->flushing = 0;
-		wake_up_interruptible(&chan->flushq);
+		wake_up(&chan->flushq);
 		break;
 
 	case OPCODE_CANCELED_CHECKPOINT:
 		chan->canceled = 1;
-		wake_up_interruptible(&chan->flushq);
+		wake_up(&chan->flushq);
 		break;
 
 	default:
@@ -1127,11 +1128,13 @@ static int xillyusb_send_opcode(struct xillyusb_dev *xdev,
  * chan->flushed is there to avoid multiple flushes at the same position,
  * in particular as a result of programs that close the file descriptor
  * e.g. after a dup2() for redirection.
+ *
+ * If timeout == 0, no timeout is applied and the wait is interruptible,
+ * otherwise a timeout is applied but the wait is non-interruptible.
  */
 
 static int flush_downstream(struct xillyusb_channel *chan,
-			    unsigned long timeout,
-			    bool interruptible)
+			    unsigned long timeout)
 {
 	struct xillyusb_dev *xdev = chan->xdev;
 	int chan_num = chan->chan_idx << 1;
@@ -1156,21 +1159,19 @@ static int flush_downstream(struct xillyusb_channel *chan,
 			return rc; /* Only real error, never -EINTR */
 
 		/* Ignoring interrupts. Cancellation must be handled */
-		while (!chan->canceled) {
-			left_to_sleep = cancel_deadline - jiffies;
+		left_to_sleep = cancel_deadline - jiffies;
 
-			if (left_to_sleep <= 0) {
-				report_io_error(xdev, -EIO);
-				return -EIO;
-			}
+		rc = wait_event_timeout(chan->flushq,
+					chan->canceled ||
+					xdev->error,
+					max(1L, left_to_sleep));
 
-			rc = wait_event_interruptible_timeout(chan->flushq,
-							      chan->canceled ||
-							      xdev->error,
-							      left_to_sleep);
+		if (xdev->error)
+			return xdev->error;
 
-			if (xdev->error)
-				return xdev->error;
+		if (!rc) {
+			report_io_error(xdev, -EIO);
+			return -EIO;
 		}
 	}
 
@@ -1191,40 +1192,27 @@ static int flush_downstream(struct xillyusb_channel *chan,
 	if (rc)
 		return rc; /* Only real error, never -EINTR */
 
-	if (!timeout) {
-		while (chan->flushing) {
-			rc = wait_event_interruptible(chan->flushq,
-						      !chan->flushing ||
-						      xdev->error);
-			if (xdev->error)
-				return xdev->error;
-
-			if (interruptible && rc)
-				return -EINTR;
-		}
-
-		goto done;
-	}
-
-	while (chan->flushing) {
+	if (timeout) {
 		left_to_sleep = deadline - jiffies;
 
-		if (left_to_sleep <= 0)
-			return -ETIMEDOUT;
+		rc = wait_event_timeout(chan->flushq,
+					!chan->flushing ||
+					xdev->error,
+					max(1L, left_to_sleep));
 
-		rc = wait_event_interruptible_timeout(chan->flushq,
-						      !chan->flushing ||
-						      xdev->error,
-						      left_to_sleep);
+		if (!rc)
+			return -ETIMEDOUT;
+	} else {
+		rc = wait_event_interruptible(chan->flushq,
+					      !chan->flushing || xdev->error);
+	}
 
-		if (xdev->error)
-			return xdev->error;
+	if (xdev->error)
+		return xdev->error;
 
-		if (interruptible && rc < 0)
-			return -EINTR;
-	}
+	if (rc < 0)
+		return -EINTR;
 
-done:
 	chan->flushed = 1;
 	return 0;
 }
@@ -1332,7 +1320,7 @@ static int xillyusb_open(struct inode *inode, struct file *filp)
 		 * This can't go wrong unless there's a serious hardware error
 		 * (or the computer is stuck for 500 ms?)
 		 */
-		rc = flush_downstream(chan, XILLY_RESPONSE_TIMEOUT, false);
+		rc = flush_downstream(chan, XILLY_RESPONSE_TIMEOUT);
 
 		if (rc == -ETIMEDOUT) {
 			rc = -EIO;
@@ -1626,7 +1614,7 @@ static int xillyusb_flush(struct file *filp, fl_owner_t id)
 	 * the user pressed CTRL-C, that interrupt will still be in flight by
 	 * the time we reach here, and the opportunity to flush is lost.
 	 */
-	rc = flush_downstream(chan, HZ, false);
+	rc = flush_downstream(chan, HZ);
 
 	mutex_unlock(&chan->out_mutex);
 
@@ -1696,7 +1684,7 @@ static ssize_t xillyusb_write(struct file *filp, const char __user *userbuf,
 	}
 
 	if (chan->out_synchronous) {
-		int flush_rc = flush_downstream(chan, 0, true);
+		int flush_rc = flush_downstream(chan, 0);
 
 		if (flush_rc && !rc)
 			rc = flush_rc;
@@ -1833,7 +1821,7 @@ static loff_t xillyusb_llseek(struct file *filp, loff_t offset, int whence)
 
 	if (chan->writable) {
 		chan->flushed = 0;
-		rc = flush_downstream(chan, HZ, false);
+		rc = flush_downstream(chan, HZ);
 	}
 
 end:
Signature · 1 line
2.17.1
author maintainer
#6 lore raw

PATCH v3 6/8 char: xillybus: Correct handling of wait queue

In reply to Eli Billauer

Don't use wait_event_interruptible() in an infinite while loop, and replace with a non-interruptible timed-out wait instead. Use wake_up() instead of wake_up_interruptible() to match that change.

Also, use the correct form of wait function in the disconnect method.

Fixes a53d1202aef1 ("char: xillybus: Add driver for XillyUSB (Xillybus variant for USB)")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- No changes.

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillyusb.c | 49 ++++++++++++--------------------
 1 file changed, 18 insertions(+), 31 deletions(-)
drivers/char/xillybus/xillyusb.c +18 -31
index decc87f8e059..fcac39bcab4d 100644
@@ -41,6 +41,7 @@ MODULE_LICENSE("GPL v2");
 
 #define XILLY_RX_TIMEOUT		(10 * HZ / 1000)
 #define XILLY_RESPONSE_TIMEOUT		(500 * HZ / 1000)
+#define XILLY_ETERNITY_TIMEOUT		(10 * HZ)
 
 #define BUF_SIZE_ORDER			4
 #define BUFNUM				8
@@ -594,11 +595,11 @@ static void wakeup_all(struct work_struct *work)
 			 * processed.
 			 */
 			chan->read_data_ok = 0;
-			wake_up_interruptible(&chan->in_fifo->waitq);
+			wake_up(&chan->in_fifo->waitq);
 		}
 
 		if (chan->out_ep)
-			wake_up_interruptible(&chan->out_ep->fifo.waitq);
+			wake_up(&chan->out_ep->fifo.waitq);
 
 		mutex_unlock(&chan->lock);
 
@@ -607,7 +608,7 @@ static void wakeup_all(struct work_struct *work)
 
 	mutex_unlock(&xdev->process_in_mutex);
 
-	wake_up_interruptible(&xdev->msg_ep->fifo.waitq);
+	wake_up(&xdev->msg_ep->fifo.waitq);
 
 	kref_put(&xdev->kref, cleanup_dev);
 }
@@ -870,7 +871,7 @@ static void try_queue_bulk_out(struct xillyusb_endpoint *ep)
 	mutex_unlock(&ep->ep_mutex);
 
 	if (do_wake)
-		wake_up_interruptible(&fifo->waitq);
+		wake_up(&fifo->waitq);
 }
 
 static void bulk_out_work(struct work_struct *work)
@@ -916,7 +917,7 @@ static int process_in_opcode(struct xillyusb_dev *xdev,
 
 		in_fifo = READ_ONCE(chan->in_fifo);
 		if (in_fifo)
-			wake_up_interruptible(&in_fifo->waitq);
+			wake_up(&in_fifo->waitq);
 		break;
 
 	case OPCODE_REACHED_CHECKPOINT:
@@ -1016,7 +1017,7 @@ static int process_bulk_in(struct xillybuffer *xb)
 			return -EIO;
 		}
 
-		wake_up_interruptible(&fifo->waitq);
+		wake_up(&fifo->waitq);
 
 		dwconsume = (bytes + 3) >> 2;
 		dws -= dwconsume;
@@ -1087,18 +1088,10 @@ static int xillyusb_send_opcode(struct xillyusb_dev *xdev,
 
 	mutex_lock(&xdev->msg_mutex);
 
-	/*
-	 * The wait queue is woken with the interruptible variant, so the
-	 * wait function matches, however returning because of an interrupt
-	 * will mess things up considerably, in particular when the caller is
-	 * the release method. And the xdev->error part prevents being stuck
-	 * forever in the event of a bizarre hardware bug: Pull the USB plug.
-	 */
-
-	while (wait_event_interruptible(fifo->waitq,
-					fifo->fill <= (fifo->size - 8) ||
-					xdev->error))
-		; /* Empty loop */
+	if (!wait_event_timeout(fifo->waitq,
+				fifo->fill <= (fifo->size - 8) ||
+				xdev->error, XILLY_ETERNITY_TIMEOUT))
+		report_io_error(xdev, -EIO);
 
 	if (xdev->error) {
 		rc = xdev->error;
@@ -1711,18 +1704,12 @@ static int xillyusb_release(struct inode *inode, struct file *filp)
 		 * If rc_read is nonzero, xdev->error indicates a global
 		 * device error. The error is reported later, so that
 		 * resources are freed.
-		 *
-		 * Looping on wait_event_interruptible() kinda breaks the idea
-		 * of being interruptible, and this should have been
-		 * wait_event(). Only it's being waken with
-		 * wake_up_interruptible() for the sake of other uses. If
-		 * there's a global device error, chan->read_data_ok is
-		 * deasserted and the wait queue is awaken, so this is covered.
 		 */
 
-		while (wait_event_interruptible(in_fifo->waitq,
-						!chan->read_data_ok))
-			; /* Empty loop */
+		if (!wait_event_timeout(in_fifo->waitq,
+					!chan->read_data_ok,
+					XILLY_ETERNITY_TIMEOUT))
+			report_io_error(xdev, -EIO);
 
 		safely_assign_in_fifo(chan, NULL);
 		fifo_mem_release(in_fifo);
@@ -2232,9 +2219,9 @@ static void xillyusb_disconnect(struct usb_interface *interface)
 	 * but somehow the message wasn't sent. Should never happen.
 	 */
 
-	rc = wait_event_interruptible_timeout(fifo->waitq,
-					      msg_ep->drained || xdev->error,
-					      XILLY_RESPONSE_TIMEOUT);
+	rc = wait_event_timeout(fifo->waitq,
+				msg_ep->drained || xdev->error,
+				XILLY_RESPONSE_TIMEOUT);
 
 	if (!rc)
 		dev_err(&interface->dev,
Signature · 1 line
2.17.1
author maintainer
#7 lore raw

PATCH v3 7/8 char: xillybus: Avoid indefinite sleep of work item

In reply to Eli Billauer

xillybus_write() may sleep indefinitely with rd_mutex taken while waiting for a DMA buffer to become available for writing. As the work item implementing automatic flush also takes this mutex, it may sleep indefinitely as well.

Thanks to the Concurrency Managed Workqueue (cmwq), there has been no visible problem with work items sleeping forever, but this shouldn't be allowed by design.

This commit makes the autoflush work item return immediately if xillybus_write() is invoked in parallel, and record that fact with the help of a new struct member, autoflush_skipped. This way, the work item doesn't attempt to take rd_mutex, avoiding sleeping indefinitely. When an autoflush has been skipped this way, xillybus_write() compensates for this with a direct call to xillybus_myflush().

Fixes 48bae0507410 ("staging: New driver: Xillybus generic interface for FPGA")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- Consume autoflush_skipped only when dont_autoflush reaches zero, so only the last active writer performs the compensating flush, as suggested in the v2 review.
- Added Link to the v2 review.
- Rebased onto the updated kernel tree (devm_kmalloc_obj() conversion).

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillybus.h      |  3 +
 drivers/char/xillybus/xillybus_core.c | 88 +++++++++++++++++++++------
 2 files changed, 72 insertions(+), 19 deletions(-)
drivers/char/xillybus/xillybus.h +3 -0
index 98c7ac4dd1f9..cd3cde833ce3 100644
@@ -77,6 +77,9 @@ struct xilly_channel {
 	int rd_full;
 	spinlock_t rd_spinlock;
 	struct mutex rd_mutex;
+	struct mutex autoflush_mutex;
+	int dont_autoflush;
+	bool autoflush_skipped;
 	wait_queue_head_t rd_wait;
 	int rd_ref_count;
 	int rd_allow_partial;
drivers/char/xillybus/xillybus_core.c +69 -19

144 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#8 lore raw

PATCH v3 8/8 char: xillybus: Cancel delayed work items before teardown

In reply to Eli Billauer

xillybus_core queues only delayed work items in its workqueue. Use cancel_delayed_work_sync() instead of flush_workqueue() to clean up queued work items, as the latter does not wait for or cancel delayed work items whose timers have not yet expired.

Fixes 48bae0507410 ("staging: New driver: Xillybus generic interface for FPGA")
Assisted-by Sashiko-0.2.5:gemini-3.1-pro-preview

---

Notes:
Changes in v3:
- Rebased onto the updated kernel tree (devm_kmalloc_obj() conversion).

Changes in v2:
- Added Fixes: tag.

 drivers/char/xillybus/xillybus_core.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/char/xillybus/xillybus_core.c +10 -11
index 3433f07e4286..879e26aa6a83 100644
@@ -1922,9 +1922,10 @@ struct xilly_endpoint *xillybus_init_endpoint(struct device *dev)
 }
 EXPORT_SYMBOL(xillybus_init_endpoint);
 
-static int xilly_quiesce(struct xilly_endpoint *endpoint)
+static int xilly_quiesce(struct xilly_endpoint *endpoint, int to_cancel)
 {
 	long t;
+	int i;
 
 	endpoint->idtlen = -1;
 	endpoint->num_channels = 0;
@@ -1940,6 +1941,9 @@ static int xilly_quiesce(struct xilly_endpoint *endpoint)
 
 	xilly_allow_isr(endpoint, false);
 
+	for (i = 1; i <= to_cancel; i++)
+		cancel_delayed_work_sync(&endpoint->channels[i]->rd_workitem);
+
 	if (t <= 0) {
 		dev_err(endpoint->dev,
 			"Failed to quiesce the device on exit.\n");
@@ -1952,6 +1956,7 @@ int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
 {
 	int rc;
 	long t;
+	int quiesce_channels = 0;
 
 	void *bootstrap_resources;
 	int idtbuffersize = (1 << PAGE_SHIFT);
@@ -2054,6 +2059,7 @@ int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
 		goto failed_idt;
 
 	xilly_allow_isr(endpoint, true);
+	quiesce_channels = endpoint->num_channels;
 
 	rc = xillybus_init_chrdev(dev, &xillybus_fops,
 				  endpoint->owner, endpoint,
@@ -2070,8 +2076,7 @@ int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
 	return 0;
 
 failed_idt:
-	xilly_quiesce(endpoint);
-	flush_workqueue(xillybus_wq);
+	xilly_quiesce(endpoint, quiesce_channels);
 
 	return rc;
 }
@@ -2081,13 +2086,7 @@ void xillybus_endpoint_remove(struct xilly_endpoint *endpoint)
 {
 	xillybus_cleanup_chrdev(endpoint, endpoint->dev);
 
-	xilly_quiesce(endpoint);
-
-	/*
-	 * Flushing is done upon endpoint release to prevent access to memory
-	 * just about to be released. This makes the quiesce complete.
-	 */
-	flush_workqueue(xillybus_wq);
+	xilly_quiesce(endpoint, endpoint->num_channels);
 }
 EXPORT_SYMBOL(xillybus_endpoint_remove);
 
@@ -2102,7 +2101,7 @@ static int __init xillybus_init(void)
 
 static void __exit xillybus_exit(void)
 {
-	/* flush_workqueue() was called for each endpoint released */
+	/* Work items have been canceled for each endpoint separately */
 	destroy_workqueue(xillybus_wq);
 }
 
Signature · 1 line
2.17.1