Skip to contentSkip to conversation

RFC: Solved unnecessary schedule latency in the TTY layer (1/3)

PATCH In review Ivo Sieben 4 participants 11 messages May 3, 2012 – May 15, 2012 TTY layer and serial drivers

lore
Share & tools
lore

Ivo Sieben

author
#0 lore raw

PATCH 1/3 RFC: Solved unnecessary schedule latency in the TTY layer (1/3)

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
In case of PREEMPT_RT or when low_latency flag is set by the serial driver the TTY receive flip buffer is copied to the line discipline directly instead of using a workqueue in the background. Therefor only in case a workqueue is actually used for copying data to the line discipline we'll have to wait for the workqueue to finish. For a PREEMPT system this prevents us from unnecessary blocking by the workqueue spin lock.

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

---
 drivers/tty/tty_buffer.c |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)
drivers/tty/tty_buffer.c +11 -7
index 6c9b7cd..ed86359 100644
@@ -317,12 +317,7 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
 
 void tty_schedule_flip(struct tty_struct *tty)
 {
-	unsigned long flags;
-	spin_lock_irqsave(&tty->buf.lock, flags);
-	if (tty->buf.tail != NULL)
-		tty->buf.tail->commit = tty->buf.tail->used;
-	spin_unlock_irqrestore(&tty->buf.lock, flags);
-	schedule_work(&tty->buf.work);
+	tty_flip_buffer_push(tty);
 }
 EXPORT_SYMBOL(tty_schedule_flip);
 
@@ -469,7 +464,16 @@ static void flush_to_ldisc(struct work_struct *work)
  */
 void tty_flush_to_ldisc(struct tty_struct *tty)
 {
-	flush_work(&tty->buf.work);
+	/*
+	 * Only in case a workqueue is actually used for copying data to the
+	 * line discipline, we'll have to wait for the workqueue to finish. In
+	 * other cases this prevents us from unnecessary blocking by the workqueue
+	 * spin lock.
+	 */
+#ifndef CONFIG_PREEMPT_RT_FULL
+	if (!tty->low_latency)
+		flush_work(&tty->buf.work);
+#endif
 }
 
 /**
Signature · 1 line
1.7.0.4
author
#1 lore raw

PATCH 2/3 RFC: Solved unnecessary schedule latency in the TTY layer (2/3)

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
The global "normal" spin lock that guards the line discipline administration is replaced by a raw spin lock.

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

Conflicts:

drivers/tty/tty_ldisc.c

---
 drivers/tty/tty_ldisc.c |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)
drivers/tty/tty_ldisc.c +15 -15
index 24b95db..30836cc 100644
@@ -26,7 +26,7 @@
  *	callers who will do ldisc lookups and cannot sleep.
  */
 
-static DEFINE_SPINLOCK(tty_ldisc_lock);
+static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
 /* Line disc dispatch table */
@@ -53,18 +53,18 @@ static void put_ldisc(struct tty_ldisc *ld)
 	 * We really want an "atomic_dec_and_lock_irqsave()",
 	 * but we don't have it, so this does it by hand.
 	 */
-	local_irq_save(flags);
-	if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
+	if (atomic_dec_and_test(&ld->users)) {
 		struct tty_ldisc_ops *ldo = ld->ops;
 
 		ldo->refcount--;
 		module_put(ldo->owner);
-		spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+		raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 
 		kfree(ld);
 		return;
 	}
-	local_irq_restore(flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 	wake_up(&tty_ldisc_idle);
 }
 
@@ -89,11 +89,11 @@ int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
 	if (disc < N_TTY || disc >= NR_LDISCS)
 		return -EINVAL;
 
-	spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
 	tty_ldiscs[disc] = new_ldisc;
 	new_ldisc->num = disc;
 	new_ldisc->refcount = 0;
-	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 
 	return ret;
 }
@@ -119,12 +119,12 @@ int tty_unregister_ldisc(int disc)
 	if (disc < N_TTY || disc >= NR_LDISCS)
 		return -EINVAL;
 
-	spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
 	if (tty_ldiscs[disc]->refcount)
 		ret = -EBUSY;
 	else
 		tty_ldiscs[disc] = NULL;
-	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 
 	return ret;
 }
@@ -135,7 +135,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
 	unsigned long flags;
 	struct tty_ldisc_ops *ldops, *ret;
 
-	spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
 	ret = ERR_PTR(-EINVAL);
 	ldops = tty_ldiscs[disc];
 	if (ldops) {
@@ -145,7 +145,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
 			ret = ldops;
 		}
 	}
-	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 	return ret;
 }
 
@@ -153,10 +153,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
 {
 	unsigned long flags;
 
-	spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
 	ldops->refcount--;
 	module_put(ldops->owner);
-	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 }
 
 /**
@@ -286,11 +286,11 @@ static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
 	unsigned long flags;
 	struct tty_ldisc *ld;
 
-	spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
 	ld = NULL;
 	if (test_bit(TTY_LDISC, &tty->flags))
 		ld = get_ldisc(tty->ldisc);
-	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 	return ld;
 }
 
Signature · 1 line
1.7.0.4
maintainer
#2 lore raw

Re: [PATCH 2/3] RFC: Solved unnecessary schedule latency in the TTY layer (2/3)

Quoted 4 lines · Ivo Sieben

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
The global "normal" spin lock that guards the line discipline administration is replaced by a raw spin lock.

Why, what does this cause to have happen? What's the difference here that causes any speedups or latency fixes?

thanks,

greg k-h

author
#3 lore raw

Re: [PATCH 2/3] RFC: Solved unnecessary schedule latency in the TTY layer (2/3)

Hi,

2012/5/3 Greg KH <[email protected]>:

Quoted 4 lines

Why, what does this cause to have happen?  What's the difference here that causes any speedups or latency fixes?

This patch solves the following scenario:
- A low priority process starts to send data to serial port A
- In order to do this, the low priority process calls the tty_ldisc_ref_wait() function to retrieve a reference to its line discipline.
- In the tty_ldisc_ref_wait() function, the global ldisc spinlock is locked.
- While the spin lock is still locked, a context switch takes place and a second process on high real-time priority starts reading data from another serial port B (this is a possible scenario on a PREEMPT_RT system where a "normal" spin lock behaves like a mutex and no interrupts are actually disabled, although the "irqsave" postfix in the spinlock function name suggests otherwise)
- The second project also calls the tty_ldisc_ref_wait() function, and therefor gets blocked on the global ldisc spin lock.
- As a result priority inversion takes place, and the second process is scheduled out, the first process is scheduled in on high priority. After the first process has released the spin lock, the second process is scheduled again.

The above scenario is not wrong, it works fine... but the scheduling & mutex handling requires a lot of extra processing time what makes that a normal read operation of about 50us sometimes can last up to 230us. By using raw spin locks this situation is prevented. I think it is legitimate to use raw spin locks because the critical sections in ldisc are small.

Regards,
Ivo Sieben
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#4 lore raw

PATCH 3/3 RFC: Solved unnecessary schedule latency in the TTY layer (3/3)

In reply to Ivo Sieben

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
The global wait_queue that is used for line discipline idle handling is moved to a separate wait_queue for each line instance. This prevents unnecessary blocking on one line, because of idle handling on another line.

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

---
 drivers/tty/tty_ldisc.c   |    7 ++++---
 include/linux/tty_ldisc.h |    2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)
drivers/tty/tty_ldisc.c +4 -3
index 30836cc..77eb299 100644
@@ -28,7 +28,6 @@
 
 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
-static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
 /* Line disc dispatch table */
 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
 
@@ -65,7 +64,7 @@ static void put_ldisc(struct tty_ldisc *ld)
 		return;
 	}
 	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
-	wake_up(&tty_ldisc_idle);
+	wake_up(&ld->wq_idle);
 }
 
 /**
@@ -200,6 +199,8 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
 
 	ld->ops = ldops;
 	atomic_set(&ld->users, 1);
+	init_waitqueue_head(&ld->wq_idle);
+
 	return ld;
 }
 
@@ -538,7 +539,7 @@ static void tty_ldisc_flush_works(struct tty_struct *tty)
 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
 {
 	long ret;
-	ret = wait_event_timeout(tty_ldisc_idle,
+	ret = wait_event_timeout(tty->ldisc->wq_idle,
 			atomic_read(&tty->ldisc->users) == 1, timeout);
 	return ret > 0 ? 0 : -EBUSY;
 }
include/linux/tty_ldisc.h +2 -0
index ff7dc08..fb79dd8 100644
@@ -110,6 +110,7 @@
 #include <linux/fs.h>
 #include <linux/wait.h>
 #include <linux/pps_kernel.h>
+#include <linux/wait.h>
 
 struct tty_ldisc_ops {
 	int	magic;
@@ -154,6 +155,7 @@ struct tty_ldisc_ops {
 struct tty_ldisc {
 	struct tty_ldisc_ops *ops;
 	atomic_t users;
+	wait_queue_head_t wq_idle;
 };
 
 #define TTY_LDISC_MAGIC	0x5403
Signature · 1 line
1.7.0.4
maintainer
#5 lore raw

Re: [PATCH 3/3] RFC: Solved unnecessary schedule latency in the TTY layer (3/3)

Quoted 6 lines · Ivo Sieben

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
The global wait_queue that is used for line discipline idle handling is moved to a separate wait_queue for each line instance. This prevents unnecessary blocking on one line, because of idle handling on another line.

This patch makes sense even in a non-RT case, as it localizes the lock to the specific ldisc.

Alan, any objection to me applying this one to the tree now?

thanks,

greg k-h

Quoted 76 lines

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

---
 drivers/tty/tty_ldisc.c   |    7 ++++---
 include/linux/tty_ldisc.h |    2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)
drivers/tty/tty_ldisc.c +4 -3
index 30836cc..77eb299 100644
@@ -28,7 +28,6 @@
 
 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
-static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
 /* Line disc dispatch table */
 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
 
@@ -65,7 +64,7 @@ static void put_ldisc(struct tty_ldisc *ld)
 		return;
 	}
 	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
-	wake_up(&tty_ldisc_idle);
+	wake_up(&ld->wq_idle);
 }
 
 /**
@@ -200,6 +199,8 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
 
 	ld->ops = ldops;
 	atomic_set(&ld->users, 1);
+	init_waitqueue_head(&ld->wq_idle);
+
 	return ld;
 }
 
@@ -538,7 +539,7 @@ static void tty_ldisc_flush_works(struct tty_struct *tty)
 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
 {
 	long ret;
-	ret = wait_event_timeout(tty_ldisc_idle,
+	ret = wait_event_timeout(tty->ldisc->wq_idle,
 			atomic_read(&tty->ldisc->users) == 1, timeout);
 	return ret > 0 ? 0 : -EBUSY;
 }
include/linux/tty_ldisc.h +2 -0
index ff7dc08..fb79dd8 100644
@@ -110,6 +110,7 @@
 #include <linux/fs.h>
 #include <linux/wait.h>
 #include <linux/pps_kernel.h>
+#include <linux/wait.h>
 
 struct tty_ldisc_ops {
 	int	magic;
@@ -154,6 +155,7 @@ struct tty_ldisc_ops {
 struct tty_ldisc {
 	struct tty_ldisc_ops *ops;
 	atomic_t users;
+	wait_queue_head_t wq_idle;
 };
 
 #define TTY_LDISC_MAGIC	0x5403
Signature · 7 lines
1.7.0.4


--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#6 lore raw

Re: [PATCH 3/3] RFC: Solved unnecessary schedule latency in the TTY layer (3/3)

In reply to Ivo Sieben
Quoted 11 lines · On Thu, 3 May 2012 14:37:43 +0200 Ivo Sieben <[email protected]> wrote:

Solved unnecessary schedule latency in the TTY layer when used in realtime context:
The global wait_queue that is used for line discipline idle handling is moved to a separate wait_queue for each line instance. This prevents unnecessary blocking on one line, because of idle handling on another line.

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

And this one as a standalone I think we should merge. I'm surprised it makes a difference but its small, clean and localises a lock which is a good thing.

Alan

author
#7 lore raw
In reply to Ivo Sieben

Hi,

2012/5/3 Ivo Sieben <[email protected]>:

Quoted 6 lines
drivers/tty/tty_buffer.c +0 -0
@@ -317,12 +317,7 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
 
  void tty_schedule_flip(struct tty_struct *tty)
  {

drivers/tty/tty_buffer.c EXPORT_SYMBOL(tty_insert_flip_string_flags); · line 323

Quoted 10 lines
drivers/tty/tty_buffer.c +1 -6
-       unsigned long flags;
-       spin_lock_irqsave(&tty->buf.lock, flags);
-       if (tty->buf.tail != NULL)
-               tty->buf.tail->commit = tty->buf.tail->used;
-       spin_unlock_irqrestore(&tty->buf.lock, flags);
-       schedule_work(&tty->buf.work);
+       tty_flip_buffer_push(tty);
  }
  EXPORT_SYMBOL(tty_schedule_flip);
 

I have an additional question on the above change in the patch that I've send:

I found that two functions in drivers/tty/tty_buffer.c implement almost the same functionality:
- tty_schedule_flip
- tty_flip_buffer_push

Only difference was that tty_schedule_flip() always uses the work queue, while the tty_flip_buffer_push only uses the work queue in case of a non prempt_rt system and low_latency flag unset.

But is my change correct? I see that most serial drivers use the tty_flip_buffer_push() function. But still a number of drivers use the tty_schedule_flip() function. I even found one driver that uses both (drivers/staging/serqt_usb2/serqt_usb2.c). Does my patch introduce bugs to these drivers? Or is the tty_schedule_flip() a legacy function, and would it be better to remove it completely?

Regards,
Ivo Sieben
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to Ivo Sieben
Quoted 4 lines

Note: In a PREEMPT_RT system "normal" spin locks behave like mutexes and no interrupts (and therefor no scheduling) is disabled.

(Coming back round to these patches now the urgent stuff is buried for a bit)

drivers/tty/tty_buffer.c EXPORT_SYMBOL(tty_insert_flip_string_flags); · line 321

Quoted 10 lines

void tty_schedule_flip(struct tty_struct *tty)
{

drivers/tty/tty_buffer.c +1 -6
-	unsigned long flags;
-	spin_lock_irqsave(&tty->buf.lock, flags);
-	if (tty->buf.tail != NULL)
-		tty->buf.tail->commit = tty->buf.tail->used;
-	spin_unlock_irqrestore(&tty->buf.lock, flags);
-	schedule_work(&tty->buf.work);
+	tty_flip_buffer_push(tty);
 }

You'd need to ifdef both of these for non RT cases. I think it may be right for RT although I'm not 100% sure on the locking.

At this point I think we'd be better off sorting out our tty locking in general before tackling RT optimisations. However as an RT patch it looks good and its interesting to see RT simplifying code not making it more complex.

author
#9 lore raw

Hi,

2012/5/10 Alan Cox <[email protected]>:

Quoted 3 lines

You'd need to ifdef both of these for non RT cases. I think it may be right for RT although I'm not 100% sure on the locking.

Alan, I don't completely understand what you mean with "ifdef both".

Regarding the following two functions defined in drivers/tty/tty_buffer.c
- tty_schedule_flip
- tty_flip_buffer_push

They both implement almost the same functionality:

void tty_schedule_flip(struct tty_struct *tty)

{
	unsigned long flags;
	spin_lock_irqsave(&tty->buf.lock, flags);
	if (tty->buf.tail != NULL)
		tty->buf.tail->commit = tty->buf.tail->used;
	spin_unlock_irqrestore(&tty->buf.lock, flags);
	schedule_work(&tty->buf.work);
}

void tty_flip_buffer_push(struct tty_struct *tty)

{
	unsigned long flags;
	spin_lock_irqsave(&tty->buf.lock, flags);
	if (tty->buf.tail != NULL)
		tty->buf.tail->commit = tty->buf.tail->used;
	spin_unlock_irqrestore(&tty->buf.lock, flags);
#ifndef CONFIG_PREEMPT_RT_FULL
	if (tty->low_latency)
		flush_to_ldisc(&tty->buf.work);
	else
		schedule_work(&tty->buf.work);
#else
	flush_to_ldisc(&tty->buf.work);

#endif
}

Only difference is that tty_schedule_flip() always uses the work queue, while the tty_flip_buffer_push only uses the work queue in case of a non prempt_rt system and low_latency flag unset.

I see that most serial drivers use the tty_flip_buffer_push() function. But still a number of drivers use the tty_schedule_flip() function. I even found one driver that uses both (drivers/staging/serqt_usb2/serqt_usb2.c). Is is there a reason for these two functions implementing slightly different behavior?

If not, I think it would be better to remove the tty_schedule_flip() function completely in a separate patch...

Regards,
Ivo Sieben

#10 lore raw
Quoted 15 lines · Ivo Sieben

void tty_flip_buffer_push(struct tty_struct *tty)

{
	unsigned long flags;
	spin_lock_irqsave(&tty->buf.lock, flags);
	if (tty->buf.tail != NULL)
		tty->buf.tail->commit = tty->buf.tail->used;
	spin_unlock_irqrestore(&tty->buf.lock, flags);
#ifndef CONFIG_PREEMPT_RT_FULL
	if (tty->low_latency)
		flush_to_ldisc(&tty->buf.work);
	else
		schedule_work(&tty->buf.work);
#else
	flush_to_ldisc(&tty->buf.work);
Quoted 3 lines

#endif
}

Note, just to keep ugly #ifdefs out of C code, or at least C functions, it is usually better to wrap the above in something like:

#ifdef CONFIG_PREEMPT_RT_FULL
static inline void tty_flush_work(struct tty_struct *tty)

{
	flush_to_ldisc(&tty->buf.work);
}
#else
static inline void tty_flush_work(struct tty_struct *tty)
{
	if (tty->low_latency)
		flush_to_ldisc(&tty->buf.work);
	else
		schedule_work(&tty->buf.work);
}

#endif

Then the C function would just look like:

void tty_flip_buffer_push(struct tty_struct *tty)

{
        unsigned long flags;
        spin_lock_irqsave(&tty->buf.lock, flags);
        if (tty->buf.tail != NULL)
                tty->buf.tail->commit = tty->buf.tail->used;
        spin_unlock_irqrestore(&tty->buf.lock, flags);

	tty_flush_work(tty);
}

-- Steve