Skip to contentSkip to conversation

syscalls/fanotify: Kill the child process before exit

PATCH zhaogongyi 4 participants 5 messages Nov 5, 2021

lore
Share & tools
lore

zhaogongyi

author
#0 lore raw

Hi,

Quoted 12 lines · Petr Vorel
Quoted 6 lines

Hi all,

Quoted 2 lines

Before the main process exit abnormally, we need to kill the child process.

Amir, Matthew, could you please have a look?

If anything, I feel as though stop_children() should probably be called from cleanup() as that callback will be invoked if any of the SAFE_FANOTIFY_* macros fail anyway, right?

It seems that calling before run_children, we need not cleanup though the SAFE_FANOTIFY_* macros fail.

Quoted 3 lines

I don't feel like there's a need to introduce another helper here to explicitly handle the cleanup case, but I could also be wrong.

When we run the testcase simultaneously, and the ulimit of open files is small, the testcase will fail and remain many while(1) tasks in system, it makes the system very stuck.

Shall we need to cleanup it?

Quoted 60 lines
Quoted 4 lines
Quoted 4 lines
---
 .../kernel/syscalls/fanotify/fanotify07.c     | 35

++++++++++++++++++-

Quoted 52 lines
Quoted 51 lines
 1 file changed, 34 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/fanotify/fanotify07.c +33 -1
index cc56d9019..0a0b4f1e4 100644
@@ -108,6 +108,39 @@ static int setup_instance(void)
 	return fd;
 }
 
+static int setup_another_instance(void) {
+	int rval;
+	int fd = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY);
+
+	if (fd == -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO, "fanotify_init() failed");
+	}
+
+	if (fd < -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO,
+			"invalid fanotify_init() return %d", fd);
+	}
+
+	rval = fanotify_mark(fd,
+		FAN_MARK_ADD, FAN_ACCESS_PERM, AT_FDCWD, fname);
+
+	if (rval == -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO, "fanotify_mark() failed");
+	}
+
+	if (rval < -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO,
+			"invalid fanotify_mark() return %d", rval);
+	}
+
+	return fd;
+}
+
 static void loose_fanotify_events(void)  {
 	int not_responded = 0;
@@ -160,7 +193,7 @@ static void test_fanotify(void)
 	 * Create and destroy another instance. This may hang if
 	 * unanswered fanotify events block notification subsystem.
 	 */
-	newfd = setup_instance();
+	newfd = setup_another_instance();
Quoted 1 line

testcases/kernel/syscalls/fanotify/fanotify07.c static void test_fanotify(void) · line 197

Quoted 1 line
testcases/kernel/syscalls/fanotify/fanotify07.c +0 -0
 	SAFE_CLOSE(newfd);

/M

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw
Quoted 9 lines · zhaogongyi

Hi,

Quoted 7 lines · Petr Vorel
Quoted 5 lines

Hi all,

Quoted 2 lines

Before the main process exit abnormally, we need to kill the child process.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Amir, Matthew, could you please have a look?

If anything, I feel as though stop_children() should probably be called from cleanup() as that callback will be invoked if any of the SAFE_FANOTIFY_* macros fail anyway, right?

It seems that calling before run_children, we need not cleanup though the SAFE_FANOTIFY_* macros fail.

Even if stop_children() is called prior to run_children(), maybe it would be OK given the fact that child_pid[] is a global and pre-intialized with zeros. Meaning, that if SAFE_KILL() was called passing a PID value of 0, then all processes part of the calling process' process group would be sent a SIGKILL signal, which is kind of what we want anyway when taking a teardown path. This is just a quick thought.

Nonetheless, you could also introduce barriers into stop_children() i.e. checking whether PID values in child_pid[] are all zero and if so return early. That'd prevent you from taking any unnecessary cleanup path in the event that something fails before any child processes are spawned?

Amir likely has an alternate suggestion, or he may be happy with your initial proposal.

Quoted 7 lines
Quoted 2 lines

I don't feel like there's a need to introduce another helper here to explicitly handle the cleanup case, but I could also be wrong.

When we run the testcase simultaneously, and the ulimit of open files is small, the testcase will fail and remain many while(1) tasks in system, it makes the system very stuck.

Shall we need to cleanup it?

I'm not saying that this cleanup isn't necessary, just that it could be placed in a more appropriate location.

Quoted 56 lines
Quoted 56 lines
Quoted 3 lines
Quoted 3 lines
---
 .../kernel/syscalls/fanotify/fanotify07.c     | 35

++++++++++++++++++-

Quoted 52 lines
Quoted 51 lines
 1 file changed, 34 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/fanotify/fanotify07.c +33 -1
index cc56d9019..0a0b4f1e4 100644
@@ -108,6 +108,39 @@ static int setup_instance(void)
 	return fd;
 }
 
+static int setup_another_instance(void) {
+	int rval;
+	int fd = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY);
+
+	if (fd == -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO, "fanotify_init() failed");
+	}
+
+	if (fd < -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO,
+			"invalid fanotify_init() return %d", fd);
+	}
+
+	rval = fanotify_mark(fd,
+		FAN_MARK_ADD, FAN_ACCESS_PERM, AT_FDCWD, fname);
+
+	if (rval == -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO, "fanotify_mark() failed");
+	}
+
+	if (rval < -1) {
+		stop_children();
+		tst_brk(TBROK | TERRNO,
+			"invalid fanotify_mark() return %d", rval);
+	}
+
+	return fd;
+}
+
 static void loose_fanotify_events(void)  {
 	int not_responded = 0;
@@ -160,7 +193,7 @@ static void test_fanotify(void)
 	 * Create and destroy another instance. This may hang if
 	 * unanswered fanotify events block notification subsystem.
 	 */
-	newfd = setup_instance();
+	newfd = setup_another_instance();
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

testcases/kernel/syscalls/fanotify/fanotify07.c static void test_fanotify(void) · line 197

Quoted 1 line
testcases/kernel/syscalls/fanotify/fanotify07.c +0 -0
 	SAFE_CLOSE(newfd);

/M

/M

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 17 lines · Matthew Bobrowski
Quoted 15 lines · zhaogongyi

Hi,

Quoted 12 lines · Petr Vorel
Quoted 6 lines

Hi all,

Quoted 2 lines

Before the main process exit abnormally, we need to kill the child process.

Amir, Matthew, could you please have a look?

If anything, I feel as though stop_children() should probably be called from cleanup() as that callback will be invoked if any of the SAFE_FANOTIFY_* macros fail anyway, right?

Quoted 9 lines
Quoted 1 line

It seems that calling before run_children, we need not cleanup though the SAFE_FANOTIFY_* macros fail.

Even if stop_children() is called prior to run_children(), maybe it would be OK given the fact that child_pid[] is a global and pre-intialized with zeros. Meaning, that if SAFE_KILL() was called passing a PID value of 0, then all processes part of the calling process' process group would be sent a SIGKILL signal, which is kind of what we want anyway when taking a teardown path. This is just a quick thought.

This hack is not necessary.

Quoted 5 lines

Nonetheless, you could also introduce barriers into stop_children() i.e. checking whether PID values in child_pid[] are all zero and if so return early. That'd prevent you from taking any unnecessary cleanup path in the event that something fails before any child processes are spawned?

This would be the obvious solution.
But simpler to skip zero values then stop the iterator. This makes the cleanup helper reenetrant:

static int stop_children(void)

{
        int child_ret;
        int i, ret = 0;

        for (i = 0; i < MAX_CHILDREN; i++) {
                if (!child_pid[i]) continue;
                SAFE_KILL(child_pid[i], SIGKILL);
        }
        for (i = 0; i < MAX_CHILDREN; i++) {
                if (!child_pid[i]) continue;
                SAFE_WAITPID(child_pid[i], &child_ret, 0);
                if (!WIFSIGNALED(child_ret))
                        ret = 1;
                child_pid[i] = 0;
       }

        return ret;
}

Thanks,
Amir.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi all,

...

Quoted 2 lines

This would be the obvious solution.
But simpler to skip zero values then stop the iterator.

+1

Quoted 13 lines

This makes the cleanup helper reenetrant:

static int stop_children(void)

{
        int child_ret;
        int i, ret = 0;

        for (i = 0; i < MAX_CHILDREN; i++) {
                if (!child_pid[i]) continue;
                SAFE_KILL(child_pid[i], SIGKILL);
        }
        for (i = 0; i < MAX_CHILDREN; i++) {
                if (!child_pid[i]) continue;
Quoted 8 lines
                SAFE_WAITPID(child_pid[i], &child_ret, 0);
                if (!WIFSIGNALED(child_ret))
                        ret = 1;
                child_pid[i] = 0;
       }

        return ret;
}

Amir, thanks! Zhao, could you please test this in your setup?
(+ please be verbose about your setup, it helps to decide in the future that fix is still needed).

Kind regards,
Petr

Quoted 2 lines

Thanks,
Amir.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
In reply to zhaogongyi

Hi Zhao,

...

Quoted 5 lines
Quoted 2 lines

I don't feel like there's a need to introduce another helper here to explicitly handle the cleanup case, but I could also be wrong.

When we run the testcase simultaneously, and the ulimit of open files is small, the testcase will fail and remain many while(1) tasks in system, it makes the system very stuck.

This kind of info is worth of adding into git commit message.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp