Skip to contentSkip to conversation

syscalls/pipe13: Add regression test for pipe to wake up all readers

PATCH New Yang Xu 2 participants 8 messages Feb 24, 2020 – Feb 26, 2020

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/pipe/.gitignore |  1 +
 testcases/kernel/syscalls/pipe/pipe13.c   | 64 +++++++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe/pipe13.c
runtest/syscalls +1 -0
index e42db9910..39d04a3a8 100644
@@ -874,6 +874,7 @@ pipe09 pipe09
 pipe10 pipe10
 pipe11 pipe11
 pipe12 pipe12
+pipe13 pipe13
 
 pipe2_01 pipe2_01
 pipe2_02 pipe2_02
testcases/kernel/syscalls/pipe/.gitignore +1 -0
index 90b502547..23e7186a6 100644
@@ -10,3 +10,4 @@
 /pipe10
 /pipe11
 /pipe12
+/pipe13
testcases/kernel/syscalls/pipe/pipe13.c new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

Hi!

testcases/kernel/syscalls/pipe/pipe13.c line 33

Quoted 13 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +13 -0
+static void verify_pipe(void)
+{
+	int fds[2];
+	int status1, status2;
+	pid_t p1, p2;
+	int ret;
+
+	SAFE_PIPE(fds);
+
+	p1 = SAFE_FORK();
+	if (p1 == 0) {
+		SAFE_CLOSE(fds[1]);
+		SAFE_READ(0, fds[0], &status1, sizeof(status1));

^
Why status1 here?

can't we just pass a dummy char buf; and size 1 here?

It's not being written to anyways.

testcases/kernel/syscalls/pipe/pipe13.c line 39

Quoted 6 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +6 -0
+		exit(0);
+	}
+	p2 = SAFE_FORK();
+	if (p2 == 0) {
+		SAFE_CLOSE(fds[1]);
+		SAFE_READ(0, fds[0], &status2, sizeof(status2));

^ Here as well.

testcases/kernel/syscalls/pipe/pipe13.c line 43

Quoted 4 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +4 -0
+		exit(0);
+	}
+
+	sleep(1);

This sleep here has to be replaced by a proper synchronization given that it's here to make sure both of the readers sleep on the pipe we should:

* Use checkpoints to make sure both of the children have managed to get before the SAFE_READ().

* The the parent should use the TST_PROCESS_STATE_WAIT() to make sure both of the chidlren are sleeping

testcases/kernel/syscalls/pipe/pipe13.c line 47

Quoted 4 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +4 -0
+	SAFE_CLOSE(fds[1]);
+
+	SAFE_WAITPID(p1, &status1, 0);
+	ret = waitpid(p2, &status2, WNOHANG);

We should just use waitpid with -1 as a pid here and WNOHANG twice, because if one of the children hangs it's not guaranteed in any way which one would that be.

Quoted 25 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +17 -0
+	if (ret == p2) {
+		tst_res(TPASS, "pipe wakes up everybody when last write closes");
+	} else {
+		tst_res(TFAIL, "pipe doesn't wake up every body when last write closes");
+		SAFE_KILL(p2, SIGKILL);
+		SAFE_WAIT(&status2);
+	}
+}
+
+static struct tst_test test = {
+	.test_all = verify_pipe,
+	.forks_child = 1,
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "6551d5c56eb"},
+		{}
+	}
+};
Signature · 7 lines
2.18.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

Hi!

Quoted 39 lines

Hi!

testcases/kernel/syscalls/pipe/pipe13.c line 33

Quoted 13 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +13 -0
+static void verify_pipe(void)
+{
+	int fds[2];
+	int status1, status2;
+	pid_t p1, p2;
+	int ret;
+
+	SAFE_PIPE(fds);
+
+	p1 = SAFE_FORK();
+	if (p1 == 0) {
+		SAFE_CLOSE(fds[1]);
+		SAFE_READ(0, fds[0], &status1, sizeof(status1));

^
Why status1 here?

can't we just pass a dummy char buf; and size 1 here?

It's not being written to anyways.

testcases/kernel/syscalls/pipe/pipe13.c line 39

Quoted 6 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +6 -0
+		exit(0);
+	}
+	p2 = SAFE_FORK();
+	if (p2 == 0) {
+		SAFE_CLOSE(fds[1]);
+		SAFE_READ(0, fds[0], &status2, sizeof(status2));

^
Here as well.

testcases/kernel/syscalls/pipe/pipe13.c line 43

Quoted 4 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +4 -0
+		exit(0);
+	}
+
+	sleep(1);

This sleep here has to be replaced by a proper synchronization given that it's here to make sure both of the readers sleep on the pipe we should:

Quoted 5 lines

* Use checkpoints to make sure both of the children have managed to get before the SAFE_READ().

* The the parent should use the TST_PROCESS_STATE_WAIT() to make sure both of the chidlren are sleeping

OK, I will use wait and wakeup api as below:

static void do_child(int i)

{
         char buf;
         SAFE_CLOSE(fds[1]);
         TST_CHECKPOINT_WAKE(i);
         SAFE_READ(0, fds[0], &buf, 1);
         exit(0);
}

staic void verify_pipe(void)
....

for (i = 0; i < 2; i++) {
                 pid[i] = SAFE_FORK();
                 if (pid[i] == 0)
                         do_child(i);
                 TST_CHECKPOINT_WAIT(i);
                 TST_PROCESS_STATE_WAIT(pid[i], 'S', 0);
         }

....

Quoted 10 lines

testcases/kernel/syscalls/pipe/pipe13.c line 47

Quoted 5 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +4 -0
+	SAFE_CLOSE(fds[1]);
+
+	SAFE_WAITPID(p1, &status1, 0);
+	ret = waitpid(p2, &status2, WNOHANG);

We should just use waitpid with -1 as a pid here and WNOHANG twice, because if one of the children hangs it's not guaranteed in any way which one would that be.

On my environment, kernel wakes up the first read and the remaining read doesn't be waked up. (I add three childs, 2,3 doesn't wake up)

Quoted 26 lines
Quoted 25 lines
testcases/kernel/syscalls/pipe/pipe13.c new file +17 -0
+	if (ret == p2) {
+		tst_res(TPASS, "pipe wakes up everybody when last write closes");
+	} else {
+		tst_res(TFAIL, "pipe doesn't wake up every body when last write closes");
+		SAFE_KILL(p2, SIGKILL);
+		SAFE_WAIT(&status2);
+	}
+}
+
+static struct tst_test test = {
+	.test_all = verify_pipe,
+	.forks_child = 1,
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "6551d5c56eb"},
+		{}
+	}
+};
Signature · 7 lines
2.18.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

Hi!

Quoted 6 lines
Quoted 4 lines

We should just use waitpid with -1 as a pid here and WNOHANG twice, because if one of the children hangs it's not guaranteed in any way which one would that be.

On my environment, kernel wakes up the first read and the remaining read doesn't be waked up. (I add three childs, 2,3 doesn't wake up)

But that behavior is not written down in any standard, that's just how the kernel internals are working at the moment, we should not assume it will work like this in the future.

What I would do here would be:

	int ret, cnt = 0, sleep_us = 1, fail = 0;

	while (cnt < 2 && sleep_us < 100000) {
		ret = waitpid(-1, NULL, WNOHANG);

		if (ret < 0)
			tst_brk(TBROK | TERRNO, "waitpid()");

		if (ret > 0) {
			cnt++;
			for (i = 0; i < 2; i++) {
				if (pid[i] == ret)
					pid[i] = 0;
			}
			continue;
		}

		usleep(sleep_time);
		sleep_time *= 2;
	}

	for (i = 0; i < 2; i++) {
		if (pid[i]) {
			tst_res(TINFO, "pid %i still sleeps", pid[i]);
			fail = 1;
			SAFE_KILL(pid[i], SIGKILL);
			SAFE_WAIT(NULL);
		}
	}

	if (fail)
		tst_res(TFAIL, "Closed pipe didn't wake everyone");

This has also advantage that we can easily run the test even for 100 children as well as two if we change the upper bound of the for loops to a variable.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

PATCH 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

Hi!

Quoted 11 lines

Hi!

Quoted 6 lines
Quoted 4 lines

We should just use waitpid with -1 as a pid here and WNOHANG twice, because if one of the children hangs it's not guaranteed in any way which one would that be.

On my environment, kernel wakes up the first read and the remaining read doesn't be waked up. (I add three childs, 2,3 doesn't wake up)

But that behavior is not written down in any standard, that's just how the kernel internals are working at the moment, we should not assume it will work like this in the future.

Sound reasonable to me, I will follow your advise.

Quoted 38 lines

What I would do here would be:

	int ret, cnt = 0, sleep_us = 1, fail = 0;

	while (cnt < 2 && sleep_us < 100000) {
		ret = waitpid(-1, NULL, WNOHANG);

		if (ret < 0)
			tst_brk(TBROK | TERRNO, "waitpid()");

		if (ret > 0) {
			cnt++;
			for (i = 0; i < 2; i++) {
				if (pid[i] == ret)
					pid[i] = 0;
			}
			continue;
		}

		usleep(sleep_time);
		sleep_time *= 2;
	}

	for (i = 0; i < 2; i++) {
		if (pid[i]) {
			tst_res(TINFO, "pid %i still sleeps", pid[i]);
			fail = 1;
			SAFE_KILL(pid[i], SIGKILL);
			SAFE_WAIT(NULL);
		}
	}

	if (fail)
		tst_res(TFAIL, "Closed pipe didn't wake everyone");
Quoted 3 lines

This has also advantage that we can easily run the test even for 100 children as well as two if we change the upper bound of the for loops to a variable.

Yes. this way is more wise.

Best Regards
Yang Xu

Quoted 1 line
author
#5 lore raw

PATCH v3 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

In reply to Cyril Hrubis

---
v2->v3:
1.use Cyril's waitpid way and add his signed-off-by
2. change child num to 10, IMO, it is more meaningful than 2

 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/pipe/.gitignore |  1 +
 testcases/kernel/syscalls/pipe/pipe13.c   | 87 +++++++++++++++++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe/pipe13.c
runtest/syscalls +1 -0
index d2551b2ec..f51456b8f 100644
@@ -874,6 +874,7 @@ pipe09 pipe09
 pipe10 pipe10
 pipe11 pipe11
 pipe12 pipe12
+pipe13 pipe13
 
 pipe2_01 pipe2_01
 pipe2_02 pipe2_02
testcases/kernel/syscalls/pipe/.gitignore +1 -0
index 90b502547..23e7186a6 100644
@@ -10,3 +10,4 @@
 /pipe10
 /pipe11
 /pipe12
+/pipe13
testcases/kernel/syscalls/pipe/pipe13.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.18.0
#6 lore raw

PATCH v3 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

Hi!
I've added a few more fixes/assertions and pushed, thanks.

* Changed the code to run the test for a few different number of children

* Added check for return value from the read() in the child

* Added SAFE_CLOSE(fds[0]) into the parent
- this fixes failures with large enough -i parameter where the test would run out of file descriptors

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#7 lore raw

PATCH v2 1/1 syscalls/pipe13: Add regression test for pipe to wake up all readers

In reply to Cyril Hrubis

---
v1->v2:
1.use ltp library wake and wait api
2.remove useless var

 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/pipe/.gitignore |  1 +
 testcases/kernel/syscalls/pipe/pipe13.c   | 73 +++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pipe/pipe13.c
runtest/syscalls +1 -0
index d2551b2ec..f51456b8f 100644
@@ -874,6 +874,7 @@ pipe09 pipe09
 pipe10 pipe10
 pipe11 pipe11
 pipe12 pipe12
+pipe13 pipe13
 
 pipe2_01 pipe2_01
 pipe2_02 pipe2_02
testcases/kernel/syscalls/pipe/.gitignore +1 -0
index 90b502547..23e7186a6 100644
@@ -10,3 +10,4 @@
 /pipe10
 /pipe11
 /pipe12
+/pipe13
testcases/kernel/syscalls/pipe/pipe13.c new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.18.0