Skip to contentSkip to conversation

rtpipe consistency problems

Xenomai-help Ulf Karlsson 2 participants 7 messages Dec 10, 2007 – Dec 24, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

I encounter a problem with rtpipes where the packets are being being "locked up" inside the pipe, that is the read(2) call waiting for the data does not return, Furthermore, it appears like I lose packets in the pipe.

I have attached a two simple C-programs that demonstrate this behavior.

The program linux-pipe.c writes increasing integer values to the pipe and expects to be able to read the same value back from the pipe, while the program xeno-pipe.c reads an integer from the pipe and instantly writes the very same value back.

I am using a 2.6.20.21 kernel together with xenomai 2.4.0.

Here is an example run after having started xeno-pipe in another shell:

$ ./linux-pipe 0
0
1
2
...
3499
3500
<process hangs in read(2)>
$ ./linux-pipe 10
mismatch: in: 10 out: 3501 <now when read(2) is restarted, the old
value is delivered>
$ ./linux-pipe 20
mismatch: in: 20 out: 10 <the pipe is still out of sync however>
$ ./linux-pipe 30
30
31
32
<the value 20 that was previously written has somehow been lost in the
pipe and it is back in sync!>

The behavior is somewhat different between individual runs, but roughly the same.

I'd be grateful for any help or suggestions.

Regards,
Ulf

/* gcc $(xeno-config --xeno-cflags --xeno-ldflags) -Wall -lnative \
* xeno-pipe.c -o xeno-pipe
*/
#include <native/task.h>
#include <native/pipe.h>
#include <stdio.h>
#include <error.h>
#include <sys/mman.h>

#define PIPE_NAME "extcmd1"

RT_TASK main_task;
RT_PIPE extcmd_pipe;

int main()

{
	int ret;
	unsigned long val;
	mlockall(MCL_CURRENT|MCL_FUTURE);
	rt_task_shadow(&main_task, "main task", 5, T_CPU(0));

	ret = rt_pipe_create(&extcmd_pipe, PIPE_NAME, P_MINOR_AUTO,0);
	if (ret < 0) {
		error(0,-ret,"rt_pipe_create");
		return;
	}
	for (;;) {
		ret = rt_pipe_read(&extcmd_pipe, &val, sizeof (val), TM_INFINITE);
		if (ret < 0)
			error(1,-ret,"rt_pipe_read");
		ret = rt_pipe_write(&extcmd_pipe, &val, sizeof (val), P_NORMAL);
		if (ret < 0)
			error(1,-ret,"rt_pipe_write");
	}
}

/* gcc -Wall linux-pipe.c -o linux-pipe
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <stdlib.h>
int main(int argc, char *argv[])

{
	unsigned long in = 0, out;
	int fd;
	int ret;

	if (argc > 1)
		in = atoi(argv[1]);
	
	fd = open("/proc/xenomai/registry/native/pipes/extcmd1", O_RDWR);
	if (fd < 0)
		error(0, -fd, "open");

	for (;;) {
		ret = write(fd, &in, sizeof (in));
		if (ret < 0)
			error(1, -ret, "write");
		ret = read(fd, &out, sizeof (out));
		if (ret < 0)
			error(1, -ret, "read");
		if (in != out) {
			printf("mismatch: in: %lu out: %lu\n", in, out);
			return 1;
		}
		printf("%lu\n", out);
		in++;
	}

	return 0;
}
#1 lore raw
Quoted 7 lines · Ulf Karlsson

Hi,

I encounter a problem with rtpipes where the packets are being being "locked up" inside the pipe, that is the read(2) call waiting for the data does not return, Furthermore, it appears like I lose packets in the pipe.

Quoted 9 lines

I have attached a two simple C-programs that demonstrate this behavior.

The program linux-pipe.c writes increasing integer values to the pipe and expects to be able to read the same value back from the pipe, while the program xeno-pipe.c reads an integer from the pipe and instantly writes the very same value back.

I am using a 2.6.20.21 kernel together with xenomai 2.4.0.

I can't reproduce this issue here, on the very same kernel/xenomai combo. Looking at the pipe code, a closing regular Linux side (linux-pipe) explicitly flushes the real-time output queue, so I just don't get how any lingering data could be delivered across a pipe close. Weird.

Could you send your .config? TIA,

Signature · 1 line
Philippe.
#2 lore raw

Hi,

Quoted 7 lines

I can't reproduce this issue here, on the very same kernel/xenomai combo. Looking at the pipe code, a closing regular Linux side (linux-pipe) explicitly flushes the real-time output queue, so I just don't get how any lingering data could be delivered across a pipe close. Weird.

Could you send your .config? TIA,

I have attached my .config-file.

My config is based on an ubuntu configuration (although I'm using a vanilla kernel), and provided that my notes are correct it should contain the following explicit changes:

Set Processor family to AMD K7
Disable: Processor type and features -> HPET Timer
Disable: Processor type and features -> SMP
Disable: Power management options -> CPUFREQ
Disable: Power management options -> APM
Disable: Power management options -> ACPI -> ACPI_PROCESSOR
Disable: Device drivers -> Input -> Misc -> PCSPKR
Disable: PARAVIRT
Enable: Real-time sub-system -> Interrupt Shield
Enable: Real-time sub-system -> Interfaces -> RTAI and VxWorks
Enable: Real-time sub-system -> Drivers -> 16550 UART
Enable: Real-time sub-system -> Drivers -> Testing drivers
Disable: Kernel hacking -> Debug info

Ulf

#3 lore raw
Quoted 8 lines · Ulf Karlsson

Hi,

Quoted 6 lines

I can't reproduce this issue here, on the very same kernel/xenomai combo. Looking at the pipe code, a closing regular Linux side (linux-pipe) explicitly flushes the real-time output queue, so I just don't get how any lingering data could be delivered across a pipe close. Weird.

Quoted 4 lines
Quoted 1 line

Could you send your .config? TIA,

I have attached my .config-file.

I need to run this testcase on a SMP then. Ok, I'll have a look at this. Thanks for reporting.

Signature · 1 line
Philippe.
#4 lore raw
In reply to Ulf Karlsson
Quoted 8 lines · Ulf Karlsson

Hi,

Quoted 6 lines

I can't reproduce this issue here, on the very same kernel/xenomai combo. Looking at the pipe code, a closing regular Linux side (linux-pipe) explicitly flushes the real-time output queue, so I just don't get how any lingering data could be delivered across a pipe close. Weird.

Quoted 4 lines
Quoted 1 line

Could you send your .config? TIA,

I have attached my .config-file.

Bug confirmed. Some SMP-race seems to be at work here. Will fix, thanks.

Signature · 1 line
Philippe.
#5 lore raw
Quoted 9 lines · Philippe Gerum
Quoted 8 lines · Ulf Karlsson

Hi,

Quoted 6 lines

I can't reproduce this issue here, on the very same kernel/xenomai combo. Looking at the pipe code, a closing regular Linux side (linux-pipe) explicitly flushes the real-time output queue, so I just don't get how any lingering data could be delivered across a pipe close. Weird.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Could you send your .config? TIA,

I have attached my .config-file.

Bug confirmed. Some SMP-race seems to be at work here. Will fix, thanks.

The attached patch against 2.4.0 fixes the issue. It has been merged into all open maintenance and development trees, and will be part of the upcoming v2.3.6 and v2.4.1 releases.

Thanks,

Signature · 1 line
Philippe.
nucleus/pipe.c +43 -39
Index: ksrc/nucleus/pipe.c
===================================================================
@@ -101,32 +101,36 @@
 }
 
 /* Must be entered with nklock held. */
+#define xnpipe_wait(__state, __mask, __s, __cond)	\
+({							\
+	wait_queue_head_t *__waitq;			\
+	DEFINE_WAIT(__wait);				\
+	int __sigpending;				\
+							\
+	if ((__mask) & XNPIPE_USER_WREAD)		\
+		__waitq = &(__state)->readq;		\
+	else						\
+		__waitq = &(__state)->syncq;		\
+							\
+	xnpipe_enqueue_wait(__state, __mask);		\
+	xnlock_put_irqrestore(&nklock, __s);		\
+									\
+	prepare_to_wait_exclusive(__waitq, &__wait, TASK_INTERRUPTIBLE);\
+									\
+	if (__cond)							\
+		xnpipe_dequeue_wait(__state, __mask);			\
+	else								\
+		schedule();						\
+									\
+	finish_wait(__waitq, &__wait);					\
+	__sigpending = signal_pending(current);				\
+									\
+	/* Restore the interrupt state initially set by the caller. */	\
+	xnlock_get_irqsave(&nklock, __s);				\
+									\
+	__sigpending;							\
+})
 
-static inline int xnpipe_wait(xnpipe_state_t *state, int mask, spl_t s)
-{
-	wait_queue_head_t *waitq;
-	DEFINE_WAIT(wait);
-	int sigpending;
-
-	if (mask & XNPIPE_USER_WREAD)
-		waitq = &state->readq;
-	else
-		waitq = &state->syncq;
-
-	xnpipe_enqueue_wait(state, mask);
-	xnlock_put_irqrestore(&nklock, s);
-
-	prepare_to_wait_exclusive(waitq, &wait, TASK_INTERRUPTIBLE);
-	schedule();
-	finish_wait(waitq, &wait);
-	sigpending = signal_pending(current);
-
-	/* Restore the interrupt state initially set by the caller. */
-	xnlock_get_irqsave(&nklock, s);
-
-	return sigpending;
-}
-
 static inline void xnpipe_dequeue_wait(xnpipe_state_t *state, int mask)
 {
 	spl_t s;
@@ -641,11 +645,9 @@
 		if (xnpipe_open_handler) {
 			xnlock_put_irqrestore(&nklock, s);
 
-			err =
-			    xnpipe_open_handler(xnminor_from_state(state),
-						NULL);
-
-			if (err != 0) {
+			err = xnpipe_open_handler(xnminor_from_state(state),
+						  NULL);
+			if (err) {
 				xnpipe_cleanup_user_conn(state);
 				return err;
 			}
@@ -662,10 +664,11 @@
 			return -EWOULDBLOCK;
 		}
 
-		sigpending = xnpipe_wait(state, XNPIPE_USER_WREAD, s);
-
-		if (sigpending && !testbits(state->status, XNPIPE_KERN_CONN)) {
-			xnpipe_cleanup_user_conn(state);
+		sigpending = xnpipe_wait(state, XNPIPE_USER_WREAD, s,
+					 testbits(state->status, XNPIPE_KERN_CONN));
+		if (sigpending) {
+			if (!testbits(state->status, XNPIPE_KERN_CONN))
+				xnpipe_cleanup_user_conn(state);
 			xnlock_put_irqrestore(&nklock, s);
 			return -ERESTARTSYS;
 		}
@@ -675,9 +678,8 @@
 		xnlock_put_irqrestore(&nklock, s);
 
 		if (xnpipe_open_handler)
-			err =
-			    xnpipe_open_handler(xnminor_from_state(state),
-						state->cookie);
+			err = xnpipe_open_handler(xnminor_from_state(state),
+						  state->cookie);
 	}
 
 	if (err)
@@ -763,7 +765,8 @@
 			return -EAGAIN;
 		}
 
-		sigpending = xnpipe_wait(state, XNPIPE_USER_WREAD, s);
+		sigpending = xnpipe_wait(state, XNPIPE_USER_WREAD, s,
+					 !emptyq_p(&state->outq));
 		holder = getq(&state->outq);
 		mh = link2mh(holder);
 
@@ -897,7 +900,8 @@
 	if (file->f_flags & O_SYNC) {
 		xnlock_get_irqsave(&nklock, s);
 		if (!emptyq_p(&state->inq)) {
-			if (xnpipe_wait(state, XNPIPE_USER_WSYNC, s))
+			if (xnpipe_wait(state, XNPIPE_USER_WSYNC, s,
+					!emptyq_p(&state->inq)))
 				count = -ERESTARTSYS;
 		}
 		xnlock_put_irqrestore(&nklock, s);
#6 lore raw
Quoted 3 lines

The attached patch against 2.4.0 fixes the issue. It has been merged into all open maintenance and development trees, and will be part of the upcoming v2.3.6 and v2.4.1 releases.

Thanks for resolving this!

Ulf