Skip to contentSkip to conversation

rt: {queue,schedule}_work_prio() allowing work priorities other than caller's priority

PATCH In review [email protected] 1 participant 2 messages Jul 5, 2008 – Jul 9, 2008 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rt: {queue,schedule}_work_prio() allowing work priorities other than caller's priority

Adds schedule_work_prio() and queue_work_prio() so that work can be scheduled with a different priority than the caller.

Currently under PREEMPT RT, queue_work() and therefore schedule_work() makes the work inherit the caller's priority.

However, if a real-time kernel interrupt handler thread defers work it may want the deferred work have lower priority so it does not contend for the CPU with succeeding interrupt handling. In the non real-time kernel, this was guaranteed because interrupts preempt workqueues.

The faulty case was a non-FIFO serial driver that deferred LED blinking to a workqueue using schedule_work(). However, that work used GPIO bitbanged I2C, which uses 50 usecs udelay()s. With the work inheriting the serial IRQ priority, it easily missed the ~60 usec deadline of 115200 bps communications.

This patch solves the issue by introducing and using schedule_work_prio().

kernel/workqueue.c +38 -4
Index: linux-2.6.24/kernel/workqueue.c
===================================================================
@@ -163,9 +163,10 @@
 }
 
 /**
- * queue_work - queue work on a workqueue
+ * queue_work_prio - queue work on a workqueue and set work priority
  * @wq: workqueue to use
  * @work: work to queue
+ * @prio: priority of work
  *
  * Returns 0 if @work was already on a queue, non-zero otherwise.
  *
@@ -174,17 +175,36 @@
  *
  * Especially no such guarantee on PREEMPT_RT.
  */
-int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
+int fastcall queue_work_prio(struct workqueue_struct *wq,
+  struct work_struct *work, int prio)
 {
 	int ret = 0, cpu = raw_smp_processor_id();
 
 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
 		BUG_ON(!plist_node_empty(&work->entry));
-		__queue_work(wq_per_cpu(wq, cpu), work, current->normal_prio);
+		__queue_work(wq_per_cpu(wq, cpu), work, prio);
 		ret = 1;
 	}
 	return ret;
 }
+EXPORT_SYMBOL_GPL(queue_work_prio);
+
+/**
+ * queue_work - queue work on a workqueue. work inherits caller's prio.
+ * @wq: workqueue to use
+ * @work: work to queue
+ *
+ * Returns 0 if @work was already on a queue, non-zero otherwise.
+ *
+ * We queue the work to the CPU it was submitted, but there is no
+ * guarantee that it will be processed by that CPU.
+ *
+ * Especially no such guarantee on PREEMPT_RT.
+ */
+int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
+{
+  return queue_work_prio(wq, work, current->normal_prio);
+}
 EXPORT_SYMBOL_GPL(queue_work);
 
 void delayed_work_timer_fn(unsigned long __data)
@@ -627,7 +647,8 @@
  * schedule_work - put work task in global workqueue
  * @work: job to be done
  *
- * This puts a job in the kernel-global workqueue.
+ * This puts a job in the kernel-global workqueue. The job inherits the
+ * caller's priority.
  */
 int fastcall schedule_work(struct work_struct *work)
 {
@@ -636,6 +657,19 @@
 EXPORT_SYMBOL(schedule_work);
 
 /**
+ * schedule_work_prio - put work task in global workqueue, set work prio
+ * @work: job to be done
+ *
+ * This puts a job in the kernel-global workqueue. The job gets the specified
+ * priority.
+ */
+int fastcall schedule_work_prio(struct work_struct *work, int prio)
+{
+	return queue_work_prio(keventd_wq, work, prio);
+}
+EXPORT_SYMBOL(schedule_work_prio);
+
+/**
  * schedule_delayed_work - put work task in global workqueue after delay
  * @dwork: job to be done
  * @delay: number of jiffies to wait or 0 for immediate execution
include/linux/workqueue.h +2 -0
Index: linux-2.6.24/include/linux/workqueue.h
===================================================================
@@ -185,6 +185,7 @@
 extern void destroy_workqueue(struct workqueue_struct *wq);
 
 extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work));
+extern int FASTCALL(queue_work_prio(struct workqueue_struct *wq, struct work_struct *work, int prio));
 extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq,
 			struct delayed_work *work, unsigned long delay));
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
@@ -194,6 +195,7 @@
 extern void flush_scheduled_work(void);
 
 extern int FASTCALL(schedule_work(struct work_struct *work));
+extern int FASTCALL(schedule_work_prio(struct work_struct *work, int prio));
 extern int FASTCALL(schedule_delayed_work(struct delayed_work *work,
 					unsigned long delay));
 extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
#1 lore raw

Hello,

Quoted 5 lines · On Sat, Jul 5, 2008 at 2:59 PM, <[email protected]> wrote:

The faulty case was a non-FIFO serial driver that deferred LED blinking to a workqueue using schedule_work(). However, that work used GPIO bitbanged I2C, which uses 50 usecs udelay()s. With the work inheriting the serial IRQ priority, it easily missed the ~60 usec deadline of 115200 bps communications.

John Kacur asked forthe (custom) code showing this case on IRC.

I suspect the current kernel also has drivers that *also* depend on workqueue's not pre-empting or delaying their calling interrupt handler.

See http://www.sidebranch.com/leon/ for the code. A short file roadmap and explanation of the analysis:

lowlevel_16550.c: non-FIFO UART driver axonbus.c: protocol handler
max7311.c: I2C -> GPIO extender, driving a few LEDs.

Queueing the "blink a LED" work happens in this code path:

lowlevel_16550.c:axonbus_isr() -> axonbus.c:ab_cmd_receive() -> boardsupport.c:ll_blink()

The queued work then performs this code path, running at the same real-time priority as the originating IRQ:
ll_blink_work() -> do_max7311_read() -> i2c-algo-bit.c:* -> udelay(50)

The udelay(50) thus keeps the 16550 interrupt from being serviced.

Scheduling the work at lower priority solved this.

Regards,

Leon.

p.s. we will submit the max7311 driver for upstream.