All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: Dmitry Adamushko <dmitry.adamushko@domain.hid>
Cc: xenomai@xenomai.org, xenomai@xenomai.org
Subject: [Xenomai-core] Re: [Xenomai-help] printk
Date: Tue, 15 Nov 2005 10:38:03 +0100	[thread overview]
Message-ID: <4379AC7B.8040500@domain.hid> (raw)
In-Reply-To: <OFBDFF2ABC.D4775297-ONC12570B9.004E7A08-C12570B9.00517F6A@datacon.at>

Dmitry Adamushko wrote:
>  > > ...
>  > >
>  > > This cannot happen in async mode, since the output would be 
> buffered and
>  > > printk() never called on behalf of the preempted handler.
>  > >
>  > >>
>  > >> let's say at the (*) point
>  > >>
>  > >> void __ipipe_flush_printk (unsigned virq)
>  > >> {
>  > >>       char *p = __ipipe_printk_buf;
>  > >>       int out = 0, len;
>  > >>
>  > >>       clear_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags);
>  > >>
>  > >>       while (out < __ipipe_printk_fill) {
>  > >>               len = strlen(p) + 1;
>  > >>               printk("%s",p);
>  > >>               p += len;
>  > >>               out += len;
>  > >>       }
>  > >> (*) <---------------------------- preempted
>  > >>       __ipipe_printk_fill = 0;
>  > >> }
>  > >>
>  > >> When linux gets controll back the virq continues its execution and
>  > >> sets __ipipe_printk_fill up to 0.
>  > >>
>  > >> This cannot happen only if virqs are manipulated with the primary
>  > >> domain being stalled as well. But you told "and under __Linux
>  > >> domain___ stalling protection in __ipipe_flush_printk since it's a
>  > >> virq handler".
>  > >>
>  > >>
>  > >>  > and finally, printk() cannot preempt
>  > >>  > __ipipe_flush_printk under normal operation mode (i.e. async 
> mode).
>  > >> AFAICS,
>  > >>  > there's no race here.
>  >
>  > Mea culpa, Dmitry is right, in the above situation he depicted, we
>  > could drop a
>  > portion of the output buffer. A way to fix this would be to hw lock
>  > a test and
>  > decrement section of __ipipe_printk_fill in the flush handler.
>  >
> 
> Something like that (just a draft) would work probably but I suppose 
> something a bit more graceful would be implemented. Actually, with a 
> small optimization of IPIPE_PRINTK_FLAG.
> 
> void __ipipe_flush_printk (unsigned virq)
> {
>       char *p = __ipipe_printk_buf;
> -       int out = 0, len;
> +       int out = 0, len, used;
> 
> ...
> -       clear_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags);
> ...
> 
> + spin_lock_irqsave_hw(&__ipipe_printk_lock,flags);
> + used = __ipipe_printk_fill;
> + spin_unlock_irqrestore_hw(&__ipipe_printk_lock,flags);
> 
> + oncemore:
> 
>       while (out < used) {
>               len = strlen(p) + 1;
>               printk("%s",p);
>               p += len;
>               out += len;
>       }
> 
> + spin_lock_irqsave_hw(&__ipipe_printk_lock,flags);
> + if (__ipipe_printk_fill == used)
> + {
> -       __ipipe_printk_fill = 0;
> 
> +     used = __ipipe_printk_fill = 0;
> 
> + // when everything is done
> +      clear_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags);
> 
> + }
> + else
> + used = __ipipe_printk_fill;
> + spin_unlock_irqrestore_hw(&__ipipe_printk_lock,flags);
> 
> + if (used)
> + goto oncemore;
> 
> ...
> }
> 
> and
> 
> asmlinkage int printk(const char *fmt, ...)
> {
> + int virq_is_active;
> ...
> 
>       spin_lock_irqsave_hw(&__ipipe_printk_lock,flags);
> 
>       fbytes = __LOG_BUF_LEN - __ipipe_printk_fill;
> 
>       if (fbytes > 1) {
>               r = vscnprintf(__ipipe_printk_buf + __ipipe_printk_fill,
>                              fbytes, fmt, args) + 1; /* account for the 
> null byte */
>               __ipipe_printk_fill += r;
>       } else
>               r = 0;
> 
> + virq_is_active = 
> test_and_set_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags);
> 
>       spin_unlock_irqrestore_hw(&__ipipe_printk_lock,flags);
> 
> - if (!test_and_set_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags))
> +      if (!virq_is_active)
>               ipipe_trigger_irq(__ipipe_printk_virq);
> out:
>       va_end(args);
> 
>       return r;
> }
> 

Another approach is about dropping the non-atomic update sequence that hurts, tolerating 
null runs of the virq when the seldom preemption case is seen, but without requiring hw 
interrupt masking to protect the shared stuff. Livelocking Linux inside the virq handler 
would still be possible whenever the RT side spams the kernel log, but this would not be 
an issue for us, since there is no such thing as a fair real-time system anyway.

--- kernel/printk.c	2 Nov 2005 16:29:34 -0000	1.2
+++ kernel/printk.c	15 Nov 2005 09:11:33 -0000
@@ -511,24 +511,23 @@

  static ipipe_spinlock_t __ipipe_printk_lock = IPIPE_SPIN_LOCK_UNLOCKED;

-static int __ipipe_printk_fill;
+static atomic_t __ipipe_printk_fill;

  static char __ipipe_printk_buf[__LOG_BUF_LEN];

  void __ipipe_flush_printk (unsigned virq)
  {
  	char *p = __ipipe_printk_buf;
-	int out = 0, len;
+	int len;

-	clear_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags);
-
-	while (out < __ipipe_printk_fill) {
-		len = strlen(p) + 1;
-		printk("%s",p);
-		p += len;
-		out += len;
+	while (test_and_clear_bit(IPIPE_PPRINTK_FLAG,&ipipe_root_domain->flags)) {
+		while (atomic_read(&__ipipe_printk_fill) > 0) {
+			len = strlen(p) + 1;
+			printk("%s",p);
+			p += len;
+			atomic_sub(len,&__ipipe_printk_fill);
+		}
  	}
-	__ipipe_printk_fill = 0;
  }

  asmlinkage int printk(const char *fmt, ...)
@@ -548,12 +547,12 @@

  	spin_lock_irqsave_hw(&__ipipe_printk_lock,flags);

-	fbytes = __LOG_BUF_LEN - __ipipe_printk_fill;
+	fbytes = __LOG_BUF_LEN - atomic_read(&__ipipe_printk_fill);

  	if (fbytes > 1)	{
-		r = vscnprintf(__ipipe_printk_buf + __ipipe_printk_fill,
+		r = vscnprintf(__ipipe_printk_buf + atomic_read(&__ipipe_printk_fill),
  			       fbytes, fmt, args) + 1; /* account for the null byte */
-		__ipipe_printk_fill += r;
+		atomic_add(r,&__ipipe_printk_fill);
  	} else
  		r = 0;

-- 

Philippe.


  parent reply	other threads:[~2005-11-15  9:38 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-10 21:05 [Xenomai-help] Creation of a rt-queue from the user space ROSSIER Daniel
2005-11-11  9:30 ` [Xenomai-help] printk Ignacio García Pérez
2005-11-11 11:07   ` Dmitry Adamushko
2005-11-14 10:52     ` Ignacio García Pérez
2005-11-14 11:26       ` Philippe Gerum
2005-11-14 12:03         ` Dmitry Adamushko
2005-11-14 13:22           ` Philippe Gerum
2005-11-14 13:47             ` Philippe Gerum
2005-11-14 14:50               ` [Xenomai-core] " Dmitry Adamushko
2005-11-14 15:56                 ` [Xenomai-core] rt_pipe_* usage Ignacio García Pérez
2005-11-14 16:15                   ` [Xenomai-core] More on rt pipes usage Ignacio García Pérez
2005-11-15 13:24                     ` Philippe Gerum
2005-11-15 16:41                       ` [Xenomai-help] " Ignacio García Pérez
2005-11-14 16:23                   ` [Xenomai-core] rt_pipe_* usage Dmitry Adamushko
2005-11-14 16:36                     ` Ignacio García Pérez
2005-11-15 12:41                       ` [Xenomai-core] Web site error (API doc search) Ignacio García Pérez
2005-11-15 13:16                       ` [Xenomai-core] rt_pipe_* usage Philippe Gerum
2005-11-15 16:22                         ` Ignacio García Pérez
2005-11-16  5:38                           ` Philippe Gerum
2005-11-15  9:38                 ` Philippe Gerum [this message]
2005-11-15 10:00                   ` [Xenomai-core] Re: [Xenomai-help] printk Dmitry Adamushko
2005-11-16 12:58                     ` Philippe Gerum
2005-11-16 14:44                       ` Dmitry Adamushko
2005-11-17  9:44                       ` [Xenomai-help] Blocking reads from pipes Ignacio García Pérez
2005-11-17 10:21                         ` Romain Lenglet
2005-11-17 13:16                           ` Ignacio García Pérez
2005-11-17 15:11                             ` Dmitry Adamushko
2005-11-17 17:24                               ` Gilles Chanteperdrix
2005-11-17 17:55                                 ` [Xenomai-core] " Dmitry Adamushko
2005-11-17 19:17                                   ` Gilles Chanteperdrix
2005-11-17 19:45                                     ` Dmitry Adamushko
2005-11-18  8:57                                       ` Ignacio García Pérez
2005-11-18  9:10                                         ` Dmitry Adamushko
2005-11-17 19:40                                 ` Dmitry Adamushko
2005-11-17 10:46                         ` Dmitry Adamushko
2005-11-14 12:15       ` [Xenomai-help] xn_pipe_create [minor] Ignacio García Pérez
2005-11-14 13:53         ` Dmitry Adamushko
2005-11-14 16:28           ` Ignacio García Pérez
2005-11-14 16:29             ` Philippe Gerum
2005-11-14 16:41               ` Ignacio García Pérez
2005-11-14 16:52                 ` Dmitry Adamushko
2005-11-14 17:38                   ` Philippe Gerum
2005-11-14 18:33                     ` Dmitry Adamushko
2005-11-15  8:04                       ` Philippe Gerum
2005-11-14 18:03                   ` [Xenomai-help] Strange pipe behaviour Ignacio García Pérez
2005-11-16  5:45                     ` Philippe Gerum
2005-11-16  7:45                       ` Ignacio García Pérez
2005-11-16 11:45                         ` Philippe Gerum
2005-11-14 16:40             ` [Xenomai-help] xn_pipe_create [minor] Dmitry Adamushko
2005-11-12 19:45   ` [Xenomai-help] printk Philippe Gerum
2005-11-14 10:47   ` [Xenomai-help] Invalid characters in task's names Ignacio García Pérez
2005-11-14 11:39     ` Philippe Gerum
2005-11-11 14:08 ` [Xenomai-help] Creation of a rt-queue from the user space Jan Kiszka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4379AC7B.8040500@domain.hid \
    --to=rpm@xenomai.org \
    --cc=dmitry.adamushko@domain.hid \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.