public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: xiaoshoukui <xiaoshoukui@gmail.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 1/1 1/1] Add ioctl_loop08 test for LOOP_GET_STATUS lock imbalance
Date: Fri, 10 Feb 2023 03:44:08 -0500	[thread overview]
Message-ID: <20230210084408.8360-1-xiaoshoukui@gmail.com> (raw)

From: xiaoshoukui <xiaoshoukui@ruijie.com.cn>

Signed-off-by: xiaoshoukui <xiaoshoukui@ruijie.com.cn>
---
 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

diff --git a/testcases/kernel/syscalls/ioctl/Makefile b/testcases/kernel/syscalls/ioctl/Makefile
index c2ff6c8e7..b61c61189 100644
--- a/testcases/kernel/syscalls/ioctl/Makefile
+++ b/testcases/kernel/syscalls/ioctl/Makefile
@@ -12,3 +12,4 @@ FILTER_OUT_MAKE_TARGETS	+= ioctl02
 endif
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+CFLAGS+=-pthread
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop08.c b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
new file mode 100644
index 000000000..a6cd31805
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 xiaoshoukui <xiaoshoukui@ruijie.com.cn>
+ */
+
+/*\
+ * [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 <osandov@fb.com>
+ *  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"
+#include <pthread.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 *ioctl_thread(void *arg)
+{
+	char dev_path[1024];
+	int dev_num, dev_fd;
+	struct tcase *tc = (struct tcase *)arg;
+
+	tst_res(TINFO, "%s", tc->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 (tc->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);
+}
+
+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);
+	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,
+	.needs_kconfigs = (const char *[]) {
+					    "CONFIG_LOCKDEP=y",
+					    NULL },
+	.tags = (const struct tst_tag[]) {
+					  { "linux-git", "bdac616db9bb "},
+					  {}
+					   },
+	.needs_drivers = (const char *const[]) {
+						"loop",
+						NULL }
+};
-- 
2.20.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2023-02-10  8:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10  8:44 xiaoshoukui [this message]
2023-02-10 10:46 ` [LTP] [PATCH 1/1 1/1] Add ioctl_loop08 test for LOOP_GET_STATUS lock imbalance Cyril Hrubis
2023-02-10 11:42   ` xiao shoukui
2023-02-10 12:13     ` Cyril Hrubis
2023-02-13  9:35       ` xiao shoukui
2023-02-14  9:55         ` Richard Palethorpe
  -- strict thread matches above, loose matches on Subject: below --
2023-02-10  8:05 xiaoshoukui

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=20230210084408.8360-1-xiaoshoukui@gmail.com \
    --to=xiaoshoukui@gmail.com \
    --cc=ltp@lists.linux.it \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox