Add ioctl_loop08 test for LOOP_GET_STATUS lock imbalance
PATCH 1/1 Add ioctl_loop08 test for LOOP_GET_STATUS lock imbalance
From: xiaoshoukui <[email protected]>
--- testcases/kernel/syscalls/ioctl/Makefile | 1 + .../kernel/syscalls/ioctl/ioctl_loop08.c | 132 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c
testcases/kernel/syscalls/ioctl/Makefile +1 -0
@@ -12,3 +12,4 @@ FILTER_OUT_MAKE_TARGETS += ioctl02 endif include $(top_srcdir)/include/mk/generic_leaf_target.mk +CFLAGS+=-pthread
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +132 -0
132 lines not on this page · raw message
Signature · 5 lines
2.20.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
testcases/kernel/syscalls/ioctl/ioctl_loop08.c line 112
Quoted 9 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +9
-0+static void verify_ioctl_loop (void ) +{ +struct tcase *tc = &tcases[0 ]; +pthread_t th1, th2; + +pthread_create (&th1,NULL , ioctl_thread, tc); +usleep (500000 ); +pthread_create (&th2,NULL , ioctl_thread, tc +1 ); +usleep (500000 );
This part looks strange, why do we run the code that calls the ioctl in a separate thread if we are sleeping after the thread has been created and quite likely the thread will finish before the main thread returns from the sleep?
Why can we just call the ioctl_thread() sequentially here?
Quoted 9 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +9
-0+if (!find_kmsg (lock_imbalance)) +tst_res (TFAIL ,"Trigger lock imbalance" ); +else +tst_res (TPASS ,"Nothing bad happened, probably" ); +} + +static struct tst_test test = { + .test_all = verify_ioctl_loop, + .needs_root =1 ,
testcases/kernel/syscalls/ioctl/ioctl_loop08.c line 131
Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +10
-0+ .needs_kconfigs = (const char *[]) { +"CONFIG_LOCKDEP=y" , + NULL }, + .tags = (const struct tst_tag []) { + {"linux-git" ,"bdac616db9bb " }, + {} + }, + .needs_drivers = (const char *const[]) { +"loop" , +NULL }
The whitespaces here are all messed up.
Quoted 7 lines
+};
Signature · 5 lines
2.20.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Sincere thanks for your advice.
Based on my tests,the lockdep will block the ioctl request thread return to userspace when it detect a lock imbalance. Place ioctl request in the main thread, there is no chance to execute find_kmsg for determining what exactly a lock problem happaned and printing the test result.
Quoted 52 lines · Cyril Hrubis
Hi!
testcases/kernel/syscalls/ioctl/ioctl_loop08.c line 112
Quoted 9 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +9
-0+static void verify_ioctl_loop (void ) +{ +struct tcase *tc = &tcases[0 ]; +pthread_t th1, th2; + +pthread_create (&th1,NULL , ioctl_thread, tc); +usleep (500000 ); +pthread_create (&th2,NULL , ioctl_thread, tc +1 ); +usleep (500000 );This part looks strange, why do we run the code that calls the ioctl in a separate thread if we are sleeping after the thread has been created and quite likely the thread will finish before the main thread returns from the sleep?
Why can we just call the
ioctl_thread()sequentially here?Quoted 9 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +9
-0+if (!find_kmsg (lock_imbalance)) +tst_res (TFAIL ,"Trigger lock imbalance" ); +else +tst_res (TPASS ,"Nothing bad happened, probably" ); +} + +static struct tst_test test = { + .test_all = verify_ioctl_loop, + .needs_root =1 ,testcases/kernel/syscalls/ioctl/ioctl_loop08.c line 131
Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +10
-0+ .needs_kconfigs = (const char *[]) { +"CONFIG_LOCKDEP=y" , + NULL }, + .tags = (const struct tst_tag []) { + {"linux-git" ,"bdac616db9bb " }, + {} + }, + .needs_drivers = (const char *const[]) { +"loop" , +NULL }The whitespaces here are all messed up.
Quoted 7 lines
+};
--
2.20.1--
Mailing list info: https://lists.linux.it/listinfo/ltp--
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 5 lines
Sincere thanks for your advice.
Based on my tests,the lockdep will block the ioctl request thread return to userspace when it detect a lock imbalance. Place ioctl request in the main thread, there is no chance to executefind_kmsgfor determining what exactly a lock problem happaned and printing the test result.
Hmm, then maybe it would be easier and more reliable to run the ioctl() in a child processes and fail the test when the parent detects the child to lockup.
I suppose that the process that called the ioctl() ends up in the D state, right? In that case the parent read the /proc/pid/stat a few times with slight delays between them and if the process keeps hanging in D state we declare it blocked forever.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
good point. It's more user friendly. change to fork child
--- .../kernel/syscalls/ioctl/ioctl_loop08.c | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c
(quoted diff) +0 -0
b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
new file mode 100644
index 000000000..047273576
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +35 -0
@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2022 xiaoshoukui <[email protected]> +*/ + +/*\ +* [Description] +* +* This is a basic ioctl test about loopdevice LOOP_GET_STATUS +* and LOOP_GET_STATUS64. +* Commit 2d1d4c1e591f made loop_get_status() drop lo_ctx_mutex before +* returning, but the loop_get_status_old(), loop_get_status64(), and +* loop_get_status_compat() wrappers don't call loop_get_status() if the +* passed argument is NULL. The callers expect that the lock is dropped, so +* make sure we drop it in that case, too. +* +* Fixed by commit: +* +* commit bdac616db9bbadb90b7d6a406144571015e138f7 +* Author: Omar Sandoval <[email protected]> +* Date: Fri Apr 06 09:57:03 2018 -0700 +* +* loop: fix LOOP_GET_STATUS lock imbalance +*/ + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <stdlib.h> +#include "lapi/loop.h" +#include "tst_test.h" + +#define MAX_MSGSIZE 4096 + +static const char lock_imbalance[] = "lock held when returning to user
space";
+
+static struct tcase {
+ int ioctl_flag;
+ char *message;
+} tcases[] = {
+ { LOOP_GET_STATUS,
+ "Testing LOOP_GET_STATUS lock imbalance" },
+
+ { LOOP_GET_STATUS64,
+ "Testing LOOP_GET_STATUS64 lock imbalance" },
+};
+
+static int find_kmsg(const char *text_to_find)
+{
+ int f, msg_found = 0;
+ char msg[MAX_MSGSIZE + 1];
+
+ f = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
+
+ while (1) {
+ TEST(read(f, msg, MAX_MSGSIZE));
+ if (TST_RET < 0) {
+ if (TST_ERR == EAGAIN)
+ /* there are no more messages */
+ break;
+ else if (TST_ERR == EPIPE)
+ /* current message was overwritten */
+ continue;
+ else
+ tst_brk(TBROK | TTERRNO,
+ "err reading /dev/kmsg");
+ } else {
+ /* lines from kmsg are not NULL terminated */
+ msg[TST_RET] = '\0';
+ if (strstr(msg, text_to_find) != NULL) {+ msg_found = 1;
+ break;
+ }
+ }
+ }
+ SAFE_CLOSE(f);
+
+ if (msg_found)
+ return 0;
+ else
+ return -1;
+}
+
+static void do_child(void)
+{
+ char dev_path[1024]; + int dev_num, dev_fd; + unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+ tst_res(TINFO, "%s", tcases[i].message);
+ dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+
+ if (dev_num < 0)
+ tst_brk(TBROK, "Failed to find free loop device");
+
+ dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+
+ if (tcases[i].ioctl_flag == LOOP_GET_STATUS)
+ ioctl(dev_fd, LOOP_GET_STATUS, NULL);
+ else
+ ioctl(dev_fd, LOOP_GET_STATUS64, NULL);
+
+ if (dev_fd > 0)
+ SAFE_CLOSE(dev_fd);
+
+ }
+
+ exit(0);
+
+}
+
+static void verify_ioctl_loop(void)
+{
+ int ret, pid;
+
+ pid = SAFE_FORK();
+ if (!pid)
+ do_child();
+
+ ret = TST_PROCESS_STATE_WAIT(pid, 'D', 5000);
+
+ if (!ret && !find_kmsg(lock_imbalance))
+ tst_res(TFAIL, "Trigger ioctl loop lock imbalance");
+ else
+ tst_res(TPASS, "Nothing bad happened, probably");
+
+}
+
+static struct tst_test test = {
+ .test_all = verify_ioctl_loop,
+ .needs_root = 1,
+ .needs_kconfigs = (const char *[]) {+ "CONFIG_LOCKDEP=y",
+ NULL
+ },
+ .tags = (const struct tst_tag[]) {
+ {"linux-git", "bdac616db9bb "},
+ {}
+ },
+ .needs_drivers = (const char *const[]) {
+ "loop",
+ NULL
+ }, + .forks_child = 1, + .timeout = 60,
+};
Signature · 1 line
2.20.1
Quoted 22 lines · Cyril Hrubis
Hi!
Quoted 2 lines
Sincere thanks for your advice.
Based on my tests,the lockdep will block the ioctl request thread returnto
Quoted 1 line
userspace when it detect a lock imbalance. Place ioctl request in the
main
Quoted 2 lines
thread, there is no chance to execute
find_kmsgfor determining what exactly a lock problem happaned and printing the test result.Hmm, then maybe it would be easier and more reliable to run the
ioctl()in a child processes and fail the test when the parent detects the child to lockup.I suppose that the process that called the
ioctl()ends up in the D state, right? In that case the parent read the/proc/pid/stata few times with slight delays between them and if the process keeps hanging in D state we declare it blocked forever.--
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hello,
Quoted 1 line · xiao shoukui
good point. It's more user friendly. change to fork child
Please submit a V2 patch with these changes.
You can CC Cyril on the new patch and add a In-reply-to if you want it to be part of this thread. Most importantly though is that it is marked as V2.
Quoted 189 lines
--- .../kernel/syscalls/ioctl/ioctl_loop08.c | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.ctestcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +35
-0@@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2022 xiaoshoukui <[email protected]> +*/ + +/*\ +* [Description] +* +* This is a basic ioctl test about loopdevice LOOP_GET_STATUS +* and LOOP_GET_STATUS64. +* Commit 2d1d4c1e591f made loop_get_status() drop lo_ctx_mutex before +* returning, but the loop_get_status_old(), loop_get_status64(), and +* loop_get_status_compat() wrappers don't call loop_get_status() if the +* passed argument is NULL. The callers expect that the lock is dropped, so +* make sure we drop it in that case, too. +* +* Fixed by commit: +* +* commit bdac616db9bbadb90b7d6a406144571015e138f7 +* Author: Omar Sandoval <[email protected]> +* Date: Fri Apr 06 09:57:03 2018 -0700 +* +* loop: fix LOOP_GET_STATUS lock imbalance +*/ + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <stdlib.h> +#include "lapi/loop.h" +#include "tst_test.h" + +#define MAX_MSGSIZE 4096 + +static const char lock_imbalance[] = "lock held when returning to userspace";
testcases/kernel/syscalls/ioctl/ioctl_loop08.c new file +112
-0+ +static struct tcase { +int ioctl_flag; +char *message; +} tcases[] = { + {LOOP_GET_STATUS , +"Testing LOOP_GET_STATUS lock imbalance" }, + + {LOOP_GET_STATUS64 , +"Testing LOOP_GET_STATUS64 lock imbalance" }, +}; + +static int find_kmsg (const char *text_to_find) +{ +int f, msg_found =0 ; +char msg[MAX_MSGSIZE +1 ]; + + f =SAFE_OPEN ("/dev/kmsg" ,O_RDONLY |O_NONBLOCK ); + +while (1 ) { +TEST (read (f, msg,MAX_MSGSIZE )); +if (TST_RET <0 ) { +if (TST_ERR ==EAGAIN ) +/* there are no more messages */ +break ; +else if (TST_ERR ==EPIPE ) +/* current message was overwritten */ +continue ; +else +tst_brk (TBROK |TTERRNO , +"err reading /dev/kmsg" ); + }else { +/* lines from kmsg are not NULL terminated */ + msg[TST_RET ] ='\0' ; +if (strstr (msg, text_to_find) !=NULL ) { + msg_found =1 ; +break ; + } + } + } +SAFE_CLOSE (f); + +if (msg_found) +return 0 ; +else +return -1 ; +} + +static void do_child (void ) +{ +char dev_path[1024 ]; +int dev_num, dev_fd; +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (tcases); i++) { +tst_res (TINFO ,"%s" , tcases[i].message); + dev_num =tst_find_free_loopdev (dev_path,sizeof (dev_path)); + +if (dev_num <0 ) +tst_brk (TBROK ,"Failed to find free loop device" ); + + dev_fd =SAFE_OPEN (dev_path,O_RDWR ); + +if (tcases[i].ioctl_flag ==LOOP_GET_STATUS ) +ioctl (dev_fd,LOOP_GET_STATUS ,NULL ); +else +ioctl (dev_fd,LOOP_GET_STATUS64 ,NULL ); + +if (dev_fd >0 ) +SAFE_CLOSE (dev_fd); + + } + +exit (0 ); + +} + +static void verify_ioctl_loop (void ) +{ +int ret, pid; + + pid =SAFE_FORK (); +if (!pid) +do_child (); + + ret =TST_PROCESS_STATE_WAIT (pid,'D' ,5000 ); + +if (!ret && !find_kmsg (lock_imbalance)) +tst_res (TFAIL ,"Trigger ioctl loop lock imbalance" ); +else +tst_res (TPASS ,"Nothing bad happened, probably" ); + +} + +static struct tst_test test = { + .test_all = verify_ioctl_loop, + .needs_root =1 , + .needs_kconfigs = (const char *[]) { +"CONFIG_LOCKDEP=y" , +NULL + }, + .tags = (const struct tst_tag []) { + {"linux-git" ,"bdac616db9bb " }, + {} + }, + .needs_drivers = (const char *const []) { +"loop" , +NULL + }, + .forks_child =1 , + .timeout =60 , +};Signature · 1 line
2.20.1Quoted 22 lines · Cyril Hrubis
Hi!
Quoted 2 lines
Sincere thanks for your advice.
Based on my tests,the lockdep will block the ioctl request thread returnto
Quoted 1 line
userspace when it detect a lock imbalance. Place ioctl request in the
main
Quoted 2 lines
thread, there is no chance to execute
find_kmsgfor determining what exactly a lock problem happaned and printing the test result.Hmm, then maybe it would be easier and more reliable to run the
ioctl()in a child processes and fail the test when the parent detects the child to lockup.I suppose that the process that called the
ioctl()ends up in the D state, right? In that case the parent read the/proc/pid/stata few times with slight delays between them and if the process keeps hanging in D state we declare it blocked forever.--
Cyril Hrubis
[email protected]
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp