Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH v4 00/11] watchqueue testing suite
@ 2022-01-07 21:20 Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 01/11] Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback Andrea Cervesato
                   ` (10 more replies)
  0 siblings, 11 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This patch-set add a new testing suite for the watch queue kernel feature
described in https://www.kernel.org/doc/html/latest/watch_queue.html

Andrea Cervesato (11):
  Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback
  Add lapi/watch_queue.h header fallback
  Add wqueue01 test
  Add wqueue02 test
  Add wqueue03 test
  Add wqueue04 test
  Add wqueue05 test
  Add wqueue06 test
  Add wqueue07 test
  Add wqueue08 test
  Add wqueue09 test

 include/lapi/keyctl.h                  |   4 +
 include/lapi/watch_queue.h             | 112 +++++++++++++++++++
 runtest/watchqueue                     |   9 ++
 scenario_groups/default                |   1 +
 testcases/kernel/Makefile              |   1 +
 testcases/kernel/watchqueue/.gitignore |   9 ++
 testcases/kernel/watchqueue/Makefile   |   8 ++
 testcases/kernel/watchqueue/common.h   | 149 +++++++++++++++++++++++++
 testcases/kernel/watchqueue/wqueue01.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue02.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue03.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue04.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue05.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue06.c |  39 +++++++
 testcases/kernel/watchqueue/wqueue07.c |  40 +++++++
 testcases/kernel/watchqueue/wqueue08.c |  44 ++++++++
 testcases/kernel/watchqueue/wqueue09.c |  52 +++++++++
 17 files changed, 668 insertions(+)
 create mode 100644 include/lapi/watch_queue.h
 create mode 100644 runtest/watchqueue
 create mode 100644 testcases/kernel/watchqueue/.gitignore
 create mode 100644 testcases/kernel/watchqueue/Makefile
 create mode 100644 testcases/kernel/watchqueue/common.h
 create mode 100644 testcases/kernel/watchqueue/wqueue01.c
 create mode 100644 testcases/kernel/watchqueue/wqueue02.c
 create mode 100644 testcases/kernel/watchqueue/wqueue03.c
 create mode 100644 testcases/kernel/watchqueue/wqueue04.c
 create mode 100644 testcases/kernel/watchqueue/wqueue05.c
 create mode 100644 testcases/kernel/watchqueue/wqueue06.c
 create mode 100644 testcases/kernel/watchqueue/wqueue07.c
 create mode 100644 testcases/kernel/watchqueue/wqueue08.c
 create mode 100644 testcases/kernel/watchqueue/wqueue09.c

-- 
2.34.1


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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 01/11] Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 02/11] Add lapi/watch_queue.h header fallback Andrea Cervesato
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 include/lapi/keyctl.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/include/lapi/keyctl.h b/include/lapi/keyctl.h
index d899345c9..3be782494 100644
--- a/include/lapi/keyctl.h
+++ b/include/lapi/keyctl.h
@@ -140,6 +140,10 @@ static inline key_serial_t keyctl_join_session_keyring(const char *name) {
 # define KEYCTL_INVALIDATE 21
 #endif
 
+#ifndef KEYCTL_WATCH_KEY
+# define KEYCTL_WATCH_KEY 32
+#endif
+
 /* key permissions */
 #ifndef KEY_POS_VIEW
 # define KEY_POS_VIEW    0x01000000
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 02/11] Add lapi/watch_queue.h header fallback
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 01/11] Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 03/11] Add wqueue01 test Andrea Cervesato
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 include/lapi/watch_queue.h | 112 +++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 include/lapi/watch_queue.h

diff --git a/include/lapi/watch_queue.h b/include/lapi/watch_queue.h
new file mode 100644
index 000000000..438e62239
--- /dev/null
+++ b/include/lapi/watch_queue.h
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ *
+ * This file is created to resolve conflicts between user space and kernel
+ * space fctnl.h declaration. linux/watch_queue.h is not handled, since
+ * user space fcntl.h redefines kernel space structures.
+ */
+
+#ifndef LAPI_WATCH_QUEUE_H__
+#define LAPI_WATCH_QUEUE_H__
+
+#include <stdint.h>
+#include "lapi/ioctl.h"
+#include "lapi/fcntl.h"
+
+#define O_NOTIFICATION_PIPE	O_EXCL	/* Parameter to pipe2() selecting notification pipe */
+
+#define IOC_WATCH_QUEUE_SET_SIZE	_IO('W', 0x60)	/* Set the size in pages */
+#define IOC_WATCH_QUEUE_SET_FILTER	_IO('W', 0x61)	/* Set the filter */
+
+enum watch_notification_type {
+	WATCH_TYPE_META		= 0,	/* Special record */
+	WATCH_TYPE_KEY_NOTIFY	= 1,	/* Key change event notification */
+	WATCH_TYPE__NR		= 2
+};
+
+enum watch_meta_notification_subtype {
+	WATCH_META_REMOVAL_NOTIFICATION	= 0,	/* Watched object was removed */
+	WATCH_META_LOSS_NOTIFICATION	= 1,	/* Data loss occurred */
+};
+
+/*
+ * Notification record header.  This is aligned to 64-bits so that subclasses
+ * can contain __u64 fields.
+ */
+struct watch_notification {
+	uint32_t			type:24;	/* enum watch_notification_type */
+	uint32_t			subtype:8;	/* Type-specific subtype (filterable) */
+	uint32_t			info;
+#define WATCH_INFO_LENGTH	0x0000007f	/* Length of record */
+#define WATCH_INFO_LENGTH__SHIFT 0
+#define WATCH_INFO_ID		0x0000ff00	/* ID of watchpoint */
+#define WATCH_INFO_ID__SHIFT	8
+#define WATCH_INFO_TYPE_INFO	0xffff0000	/* Type-specific info */
+#define WATCH_INFO_TYPE_INFO__SHIFT 16
+#define WATCH_INFO_FLAG_0	0x00010000	/* Type-specific info, flag bit 0 */
+#define WATCH_INFO_FLAG_1	0x00020000	/* ... */
+#define WATCH_INFO_FLAG_2	0x00040000
+#define WATCH_INFO_FLAG_3	0x00080000
+#define WATCH_INFO_FLAG_4	0x00100000
+#define WATCH_INFO_FLAG_5	0x00200000
+#define WATCH_INFO_FLAG_6	0x00400000
+#define WATCH_INFO_FLAG_7	0x00800000
+};
+
+/*
+ * Notification filtering rules (IOC_WATCH_QUEUE_SET_FILTER).
+ */
+struct watch_notification_type_filter {
+	uint32_t	type;			/* Type to apply filter to */
+	uint32_t	info_filter;		/* Filter on watch_notification::info */
+	uint32_t	info_mask;		/* Mask of relevant bits in info_filter */
+	uint32_t	subtype_filter[8];	/* Bitmask of subtypes to filter on */
+};
+
+struct watch_notification_filter {
+	uint32_t	nr_filters;		/* Number of filters */
+	uint32_t	__reserved;		/* Must be 0 */
+	struct watch_notification_type_filter filters[];
+};
+
+
+/*
+ * Extended watch removal notification.  This is used optionally if the type
+ * wants to indicate an identifier for the object being watched, if there is
+ * such.  This can be distinguished by the length.
+ *
+ * type -> WATCH_TYPE_META
+ * subtype -> WATCH_META_REMOVAL_NOTIFICATION
+ */
+struct watch_notification_removal {
+	struct watch_notification watch;
+	uint64_t	id;		/* Type-dependent identifier */
+};
+
+/*
+ * Type of key/keyring change notification.
+ */
+enum key_notification_subtype {
+	NOTIFY_KEY_INSTANTIATED	= 0, /* Key was instantiated (aux is error code) */
+	NOTIFY_KEY_UPDATED	= 1, /* Key was updated */
+	NOTIFY_KEY_LINKED	= 2, /* Key (aux) was added to watched keyring */
+	NOTIFY_KEY_UNLINKED	= 3, /* Key (aux) was removed from watched keyring */
+	NOTIFY_KEY_CLEARED	= 4, /* Keyring was cleared */
+	NOTIFY_KEY_REVOKED	= 5, /* Key was revoked */
+	NOTIFY_KEY_INVALIDATED	= 6, /* Key was invalidated */
+	NOTIFY_KEY_SETATTR	= 7, /* Key's attributes got changed */
+};
+
+/*
+ * Key/keyring notification record.
+ * - watch.type = WATCH_TYPE_KEY_NOTIFY
+ * - watch.subtype = enum key_notification_type
+ */
+struct key_notification {
+	struct watch_notification watch;
+	uint32_t	key_id;		/* The key/keyring affected */
+	uint32_t	aux;		/* Per-type auxiliary data */
+};
+
+#endif /* LAPI_WATCH_QUEUE_H__ */
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 03/11] Add wqueue01 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 01/11] Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 02/11] Add lapi/watch_queue.h header fallback Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-26 11:58   ` Cyril Hrubis
  2022-01-07 21:20 ` [LTP] [PATCH v4 04/11] Add wqueue02 test Andrea Cervesato
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_UPDATED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 runtest/watchqueue                     |   9 ++
 scenario_groups/default                |   1 +
 testcases/kernel/Makefile              |   1 +
 testcases/kernel/watchqueue/.gitignore |   9 ++
 testcases/kernel/watchqueue/Makefile   |   8 ++
 testcases/kernel/watchqueue/common.h   | 149 +++++++++++++++++++++++++
 testcases/kernel/watchqueue/wqueue01.c |  40 +++++++
 7 files changed, 217 insertions(+)
 create mode 100644 runtest/watchqueue
 create mode 100644 testcases/kernel/watchqueue/.gitignore
 create mode 100644 testcases/kernel/watchqueue/Makefile
 create mode 100644 testcases/kernel/watchqueue/common.h
 create mode 100644 testcases/kernel/watchqueue/wqueue01.c

diff --git a/runtest/watchqueue b/runtest/watchqueue
new file mode 100644
index 000000000..bd6b0a423
--- /dev/null
+++ b/runtest/watchqueue
@@ -0,0 +1,9 @@
+wqueue01 wqueue01
+wqueue02 wqueue02
+wqueue03 wqueue03
+wqueue04 wqueue04
+wqueue05 wqueue05
+wqueue06 wqueue06
+wqueue07 wqueue07
+wqueue08 wqueue08
+wqueue09 wqueue09
diff --git a/scenario_groups/default b/scenario_groups/default
index 1dc2987d5..68bd5300d 100644
--- a/scenario_groups/default
+++ b/scenario_groups/default
@@ -30,3 +30,4 @@ cve
 crypto
 kernel_misc
 uevent
+watchqueue
diff --git a/testcases/kernel/Makefile b/testcases/kernel/Makefile
index 4604f1f38..d44856c57 100644
--- a/testcases/kernel/Makefile
+++ b/testcases/kernel/Makefile
@@ -38,6 +38,7 @@ SUBDIRS			+= connectors \
 			   sound \
 			   tracing \
 			   uevents \
+			   watchqueue \
 
 ifeq ($(WITH_POWER_MANAGEMENT_TESTSUITE),yes)
 SUBDIRS			+= power_management
diff --git a/testcases/kernel/watchqueue/.gitignore b/testcases/kernel/watchqueue/.gitignore
new file mode 100644
index 000000000..dcfcd8272
--- /dev/null
+++ b/testcases/kernel/watchqueue/.gitignore
@@ -0,0 +1,9 @@
+wqueue01
+wqueue02
+wqueue03
+wqueue04
+wqueue05
+wqueue06
+wqueue07
+wqueue08
+wqueue09
diff --git a/testcases/kernel/watchqueue/Makefile b/testcases/kernel/watchqueue/Makefile
new file mode 100644
index 000000000..896d66d42
--- /dev/null
+++ b/testcases/kernel/watchqueue/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir			?= ../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+LDLIBS += $(KEYUTILS_LIBS)
diff --git a/testcases/kernel/watchqueue/common.h b/testcases/kernel/watchqueue/common.h
new file mode 100644
index 000000000..00d19b5bc
--- /dev/null
+++ b/testcases/kernel/watchqueue/common.h
@@ -0,0 +1,149 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef WQUEUE_COMMON_H__
+#define WQUEUE_COMMON_H__
+
+#include "tst_test.h"
+#include "lapi/watch_queue.h"
+#include "lapi/keyctl.h"
+
+static struct watch_notification_filter wqueue_filter = {
+	.nr_filters	= 2,
+	.filters = {
+		[0]	= {
+			.type			= WATCH_TYPE_META,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+		[1]	= {
+			.type			= WATCH_TYPE_KEY_NOTIFY,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+	},
+};
+
+static inline int wqueue_key_event(struct watch_notification *n, size_t len, unsigned int wtype, int type)
+{
+	struct key_notification *k;
+	const char* msg;
+
+	if (wtype != WATCH_TYPE_KEY_NOTIFY)
+		return 0;
+
+	if (len != sizeof(struct key_notification))
+		tst_brk(TBROK, "Incorrect key message length");
+
+	switch (n->subtype) {
+		case NOTIFY_KEY_INSTANTIATED:
+			msg = "instantiated";
+			break;
+		case NOTIFY_KEY_UPDATED:
+			msg = "updated";
+			break;
+		case NOTIFY_KEY_LINKED:
+			msg = "linked";
+			break;
+		case NOTIFY_KEY_UNLINKED:
+			msg = "unlinked";
+			break;
+		case NOTIFY_KEY_CLEARED:
+			msg = "cleared";
+			break;
+		case NOTIFY_KEY_REVOKED:
+			msg = "revoked";
+			break;
+		case NOTIFY_KEY_INVALIDATED:
+			msg = "invalidated";
+			break;
+		case NOTIFY_KEY_SETATTR:
+			msg = "setattr";
+			break;
+		default:
+			msg = "Invalid notification";
+			break;
+	};
+
+	k = (struct key_notification *)n;
+	tst_res(TINFO, "KEY %08x change=%u[%s] aux=%u",
+		k->key_id, n->subtype, msg, k->aux);
+
+	if (n->subtype == type)
+		return 1;
+
+	return 0;
+}
+
+static inline key_serial_t wqueue_add_key(int fd)
+{
+	key_serial_t key;
+
+	key = add_key("user", "ltptestkey", "a", 1, KEY_SPEC_SESSION_KEYRING);
+	if (key == -1)
+		tst_brk(TBROK, "add_key error: %s", tst_strerrno(errno));
+
+	keyctl(KEYCTL_WATCH_KEY, key, fd, 0x01);
+	keyctl(KEYCTL_WATCH_KEY, KEY_SPEC_SESSION_KEYRING, fd, 0x02);
+
+	return key;
+}
+
+static inline int wqueue_watch(int buf_size, struct watch_notification_filter *filter)
+{
+	int pipefd[2];
+	int fd;
+
+	SAFE_PIPE2(pipefd, O_NOTIFICATION_PIPE);
+
+	fd = pipefd[0];
+
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_SIZE, buf_size);
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_FILTER, filter);
+
+	return fd;
+}
+
+typedef void (*wqueue_callback) (struct watch_notification *n, size_t len, unsigned int wtype);
+
+static void wqueue_consumer(int fd, wqueue_callback cb)
+{
+	unsigned char buffer[433], *p, *end;
+	union {
+		struct watch_notification n;
+		unsigned char buf1[128];
+	} n;
+	ssize_t buf_len;
+
+	tst_res(TINFO, "Reading watch queue events");
+
+	buf_len = SAFE_READ(0, fd, buffer, sizeof(buffer));
+
+	p = buffer;
+	end = buffer + buf_len;
+	while (p < end) {
+		size_t largest, len;
+
+		largest = end - p;
+		if (largest > 128)
+			largest = 128;
+
+		if (largest < sizeof(struct watch_notification))
+			tst_brk(TBROK, "Short message header: %zu", largest);
+
+		memcpy(&n, p, largest);
+
+		tst_res(TINFO, "NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x",
+				p - buffer, n.n.type, n.n.subtype, n.n.info);
+
+		len = n.n.info & WATCH_INFO_LENGTH;
+		if (len < sizeof(n.n) || len > largest)
+			tst_brk(TBROK, "Bad message length: %zu/%zu", len, largest);
+
+		cb(&n.n, len, n.n.type);
+
+		p += len;
+	}
+}
+
+#endif
diff --git a/testcases/kernel/watchqueue/wqueue01.c b/testcases/kernel/watchqueue/wqueue01.c
new file mode 100644
index 000000000..24c3908ce
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue01.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl update is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_updated(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UPDATED))
+		tst_res(TPASS, "keyctl update has been recognized");
+	else
+		tst_res(TFAIL, "keyctl update has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_UPDATE, key, "b", 1);
+	wqueue_consumer(fd, saw_key_updated);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 04/11] Add wqueue02 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (2 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 03/11] Add wqueue01 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 05/11] Add wqueue03 test Andrea Cervesato
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_UNLINKED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue02.c | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue02.c

diff --git a/testcases/kernel/watchqueue/wqueue02.c b/testcases/kernel/watchqueue/wqueue02.c
new file mode 100644
index 000000000..cf012fd15
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue02.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl unlink is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_unlinked(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UNLINKED))
+		tst_res(TPASS, "keyctl unlink has been recognized");
+	else
+		tst_res(TFAIL, "keyctl unlink has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_UNLINK, key, KEY_SPEC_SESSION_KEYRING);
+	wqueue_consumer(fd, saw_key_unlinked);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 05/11] Add wqueue03 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (3 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 04/11] Add wqueue02 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 06/11] Add wqueue04 test Andrea Cervesato
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_REVOKED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue03.c | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue03.c

diff --git a/testcases/kernel/watchqueue/wqueue03.c b/testcases/kernel/watchqueue/wqueue03.c
new file mode 100644
index 000000000..50beaefdc
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue03.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl revoke is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_revoked(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_REVOKED))
+		tst_res(TPASS, "keyctl revoke has been recognized");
+	else
+		tst_res(TFAIL, "keyctl revoke has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_REVOKE, key);
+	wqueue_consumer(fd, saw_key_revoked);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 06/11] Add wqueue04 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (4 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 05/11] Add wqueue03 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 07/11] Add wqueue05 test Andrea Cervesato
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_LINKED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue04.c | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue04.c

diff --git a/testcases/kernel/watchqueue/wqueue04.c b/testcases/kernel/watchqueue/wqueue04.c
new file mode 100644
index 000000000..04e99e891
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue04.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl link is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_linked(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_LINKED))
+		tst_res(TPASS, "keyctl link has been recognized");
+	else
+		tst_res(TFAIL, "keyctl link has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_LINK, key, KEY_SPEC_SESSION_KEYRING);
+	wqueue_consumer(fd, saw_key_linked);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 07/11] Add wqueue05 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (5 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 06/11] Add wqueue04 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 08/11] Add wqueue06 test Andrea Cervesato
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_INVALIDATED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue05.c | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue05.c

diff --git a/testcases/kernel/watchqueue/wqueue05.c b/testcases/kernel/watchqueue/wqueue05.c
new file mode 100644
index 000000000..43e401512
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue05.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl invalidate is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_invalidated(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_INVALIDATED))
+		tst_res(TPASS, "keyctl invalidate has been recognized");
+	else
+		tst_res(TFAIL, "keyctl invalidate has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_INVALIDATE, key);
+	wqueue_consumer(fd, saw_key_invalidated);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 08/11] Add wqueue06 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (6 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 07/11] Add wqueue05 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 09/11] Add wqueue07 test Andrea Cervesato
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_CLEARED event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue06.c | 39 ++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue06.c

diff --git a/testcases/kernel/watchqueue/wqueue06.c b/testcases/kernel/watchqueue/wqueue06.c
new file mode 100644
index 000000000..eb8fab82f
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue06.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl clear is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_cleared(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_CLEARED))
+		tst_res(TPASS, "keyctl clear has been recognized");
+	else
+		tst_res(TFAIL, "keyctl clear has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	wqueue_add_key(fd);
+
+	keyctl(KEYCTL_CLEAR, KEY_SPEC_SESSION_KEYRING);
+	wqueue_consumer(fd, saw_key_cleared);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 09/11] Add wqueue07 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (7 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 08/11] Add wqueue06 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 10/11] Add wqueue08 test Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 11/11] Add wqueue09 test Andrea Cervesato
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing NOTIFY_KEY_SETATTR event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue07.c | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue07.c

diff --git a/testcases/kernel/watchqueue/wqueue07.c b/testcases/kernel/watchqueue/wqueue07.c
new file mode 100644
index 000000000..1dbaee3e9
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue07.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl setperm is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_setattr(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_SETATTR))
+		tst_res(TPASS, "keyctl setattr has been recognized");
+	else
+		tst_res(TFAIL, "keyctl setattr has not been recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_SETPERM, key, KEY_POS_ALL | KEY_USR_ALL);
+	wqueue_consumer(fd, saw_key_setattr);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 10/11] Add wqueue08 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (8 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 09/11] Add wqueue07 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  2022-01-07 21:20 ` [LTP] [PATCH v4 11/11] Add wqueue09 test Andrea Cervesato
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing WATCH_META_REMOVAL_NOTIFICATION event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue08.c | 44 ++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue08.c

diff --git a/testcases/kernel/watchqueue/wqueue08.c b/testcases/kernel/watchqueue/wqueue08.c
new file mode 100644
index 000000000..a8e01fbd2
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue08.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if key watch removal is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_watch_removal(struct watch_notification *n, LTP_ATTRIBUTE_UNUSED size_t len, unsigned int wtype)
+{
+	if (wtype != WATCH_TYPE_META)
+		return;
+
+	if (n->subtype == WATCH_META_REMOVAL_NOTIFICATION)
+		tst_res(TPASS, "Meta removal notification received");
+	else
+		tst_res(TFAIL, "Event not recognized");
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	/* if watch_id = -1 key is removed from the watch queue */
+	keyctl(KEYCTL_WATCH_KEY, key, fd, -1);
+	wqueue_consumer(fd, saw_watch_removal);
+
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [LTP] [PATCH v4 11/11] Add wqueue09 test
  2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
                   ` (9 preceding siblings ...)
  2022-01-07 21:20 ` [LTP] [PATCH v4 10/11] Add wqueue08 test Andrea Cervesato
@ 2022-01-07 21:20 ` Andrea Cervesato
  10 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-01-07 21:20 UTC (permalink / raw)
  To: ltp

This test is testing WATCH_META_LOSS_NOTIFICATION event

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/watchqueue/wqueue09.c | 52 ++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/wqueue09.c

diff --git a/testcases/kernel/watchqueue/wqueue09.c b/testcases/kernel/watchqueue/wqueue09.c
new file mode 100644
index 000000000..ac49bb8a0
--- /dev/null
+++ b/testcases/kernel/watchqueue/wqueue09.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Fill the watch queue and wait for a notification loss.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static int data_lost;
+
+static void saw_data_loss(struct watch_notification *n, LTP_ATTRIBUTE_UNUSED size_t len, unsigned int wtype)
+{
+	if (wtype != WATCH_TYPE_META)
+		return;
+
+	if (n->subtype == WATCH_META_LOSS_NOTIFICATION)
+		data_lost = 1;
+}
+
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(1, &wqueue_filter);
+
+	key = wqueue_add_key(fd);
+	keyctl(KEYCTL_UPDATE, key, "b", 1);
+	keyctl(KEYCTL_REVOKE, key);
+
+	data_lost = 0;
+	while (!data_lost)
+		wqueue_consumer(fd, saw_data_loss);
+
+	SAFE_CLOSE(fd);
+
+	if (data_lost)
+		tst_res(TPASS, "Meta loss notification received");
+	else
+		tst_res(TFAIL, "Event not recognized");
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.34.1


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [LTP] [PATCH v4 03/11] Add wqueue01 test
  2022-01-07 21:20 ` [LTP] [PATCH v4 03/11] Add wqueue01 test Andrea Cervesato
@ 2022-01-26 11:58   ` Cyril Hrubis
  0 siblings, 0 replies; 13+ messages in thread
From: Cyril Hrubis @ 2022-01-26 11:58 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi!
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
> ---
>  runtest/watchqueue                     |   9 ++
>  scenario_groups/default                |   1 +
>  testcases/kernel/Makefile              |   1 +
>  testcases/kernel/watchqueue/.gitignore |   9 ++
>  testcases/kernel/watchqueue/Makefile   |   8 ++
>  testcases/kernel/watchqueue/common.h   | 149 +++++++++++++++++++++++++
>  testcases/kernel/watchqueue/wqueue01.c |  40 +++++++
>  7 files changed, 217 insertions(+)
>  create mode 100644 runtest/watchqueue
>  create mode 100644 testcases/kernel/watchqueue/.gitignore
>  create mode 100644 testcases/kernel/watchqueue/Makefile
>  create mode 100644 testcases/kernel/watchqueue/common.h
>  create mode 100644 testcases/kernel/watchqueue/wqueue01.c
> 
> diff --git a/runtest/watchqueue b/runtest/watchqueue
> new file mode 100644
> index 000000000..bd6b0a423
> --- /dev/null
> +++ b/runtest/watchqueue
> @@ -0,0 +1,9 @@
> +wqueue01 wqueue01
> +wqueue02 wqueue02
> +wqueue03 wqueue03
> +wqueue04 wqueue04
> +wqueue05 wqueue05
> +wqueue06 wqueue06
> +wqueue07 wqueue07
> +wqueue08 wqueue08
> +wqueue09 wqueue09

While this is minor, such entries should be added with each respective
test.

> diff --git a/scenario_groups/default b/scenario_groups/default
> index 1dc2987d5..68bd5300d 100644
> --- a/scenario_groups/default
> +++ b/scenario_groups/default
> @@ -30,3 +30,4 @@ cve
>  crypto
>  kernel_misc
>  uevent
> +watchqueue
> diff --git a/testcases/kernel/Makefile b/testcases/kernel/Makefile
> index 4604f1f38..d44856c57 100644
> --- a/testcases/kernel/Makefile
> +++ b/testcases/kernel/Makefile
> @@ -38,6 +38,7 @@ SUBDIRS			+= connectors \
>  			   sound \
>  			   tracing \
>  			   uevents \
> +			   watchqueue \
>  
>  ifeq ($(WITH_POWER_MANAGEMENT_TESTSUITE),yes)
>  SUBDIRS			+= power_management
> diff --git a/testcases/kernel/watchqueue/.gitignore b/testcases/kernel/watchqueue/.gitignore
> new file mode 100644
> index 000000000..dcfcd8272
> --- /dev/null
> +++ b/testcases/kernel/watchqueue/.gitignore
> @@ -0,0 +1,9 @@
> +wqueue01
> +wqueue02
> +wqueue03
> +wqueue04
> +wqueue05
> +wqueue06
> +wqueue07
> +wqueue08
> +wqueue09

Same here.

> diff --git a/testcases/kernel/watchqueue/Makefile b/testcases/kernel/watchqueue/Makefile
> new file mode 100644
> index 000000000..896d66d42
> --- /dev/null
> +++ b/testcases/kernel/watchqueue/Makefile
> @@ -0,0 +1,8 @@
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +top_srcdir			?= ../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> +
> +LDLIBS += $(KEYUTILS_LIBS)
> diff --git a/testcases/kernel/watchqueue/common.h b/testcases/kernel/watchqueue/common.h
> new file mode 100644
> index 000000000..00d19b5bc
> --- /dev/null
> +++ b/testcases/kernel/watchqueue/common.h
> @@ -0,0 +1,149 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +#ifndef WQUEUE_COMMON_H__
> +#define WQUEUE_COMMON_H__
> +
> +#include "tst_test.h"
> +#include "lapi/watch_queue.h"
> +#include "lapi/keyctl.h"
> +
> +static struct watch_notification_filter wqueue_filter = {
> +	.nr_filters	= 2,
> +	.filters = {
> +		[0]	= {
> +			.type			= WATCH_TYPE_META,
> +			.subtype_filter[0]	= UINT_MAX,
> +		},
> +		[1]	= {
> +			.type			= WATCH_TYPE_KEY_NOTIFY,
> +			.subtype_filter[0]	= UINT_MAX,
> +		},
> +	},
> +};
> +
> +static inline int wqueue_key_event(struct watch_notification *n, size_t len, unsigned int wtype, int type)
> +{
> +	struct key_notification *k;
> +	const char* msg;
> +
> +	if (wtype != WATCH_TYPE_KEY_NOTIFY)
> +		return 0;
> +
> +	if (len != sizeof(struct key_notification))
> +		tst_brk(TBROK, "Incorrect key message length");
> +
> +	switch (n->subtype) {
> +		case NOTIFY_KEY_INSTANTIATED:
> +			msg = "instantiated";
> +			break;
> +		case NOTIFY_KEY_UPDATED:
> +			msg = "updated";
> +			break;
> +		case NOTIFY_KEY_LINKED:
> +			msg = "linked";
> +			break;
> +		case NOTIFY_KEY_UNLINKED:
> +			msg = "unlinked";
> +			break;
> +		case NOTIFY_KEY_CLEARED:
> +			msg = "cleared";
> +			break;
> +		case NOTIFY_KEY_REVOKED:
> +			msg = "revoked";
> +			break;
> +		case NOTIFY_KEY_INVALIDATED:
> +			msg = "invalidated";
> +			break;
> +		case NOTIFY_KEY_SETATTR:
> +			msg = "setattr";
> +			break;
> +		default:
> +			msg = "Invalid notification";
> +			break;
> +	};
> +
> +	k = (struct key_notification *)n;
> +	tst_res(TINFO, "KEY %08x change=%u[%s] aux=%u",
> +		k->key_id, n->subtype, msg, k->aux);
> +
> +	if (n->subtype == type)
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static inline key_serial_t wqueue_add_key(int fd)
> +{
> +	key_serial_t key;
> +
> +	key = add_key("user", "ltptestkey", "a", 1, KEY_SPEC_SESSION_KEYRING);
> +	if (key == -1)
> +		tst_brk(TBROK, "add_key error: %s", tst_strerrno(errno));
> +
> +	keyctl(KEYCTL_WATCH_KEY, key, fd, 0x01);
> +	keyctl(KEYCTL_WATCH_KEY, KEY_SPEC_SESSION_KEYRING, fd, 0x02);
> +
> +	return key;
> +}
> +
> +static inline int wqueue_watch(int buf_size, struct watch_notification_filter *filter)
> +{
> +	int pipefd[2];
> +	int fd;
> +
> +	SAFE_PIPE2(pipefd, O_NOTIFICATION_PIPE);

We can get two different errors here that should be translated to TCONF.

If you look at watch_queue.h in the linux kernel sources it looks like:

#ifdef CONFIG_WATCH_QUEUE
...
#else
static inline int watch_queue_init(struct pipe_inode_info *pipe)
{
        return -ENOPKG;
}
#endif

So in the case that CONFIG_WATCH_QUEUE is not set pipe2() with
O_NOTIFICATION_PIPE will fail with errno set to ENOPKG.


And on kernels that are older than the watch queue implementation we will
trip over:

	if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
                return -EINVAL;

In the __do_pipe_flags() function and the call will fail with EINVAL
instead.

So this code should look like:

	TEST(pipe2(pipefd, O_NOTIFICATION_PIPE));
	if (TST_RET) {
		switch (TST_ERR) {
		case ENOPKG:
			tst_brk(TCONF | TTERRNO, "CONFIG_WATCH_QUEUE is not set");
		break;
		case EINVAL:
			tst_brk(TCONF | TTERRNO, "O_NOTIFICATION_PIPE is not supported");
		break;
		default:
			tst_brk(TBROK | TTERRNO, "pipe2() returned %ld", TST_RET);
		}
	}

> +	fd = pipefd[0];
> +
> +	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_SIZE, buf_size);
> +	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_FILTER, filter);
> +
> +	return fd;
> +}
> +
> +typedef void (*wqueue_callback) (struct watch_notification *n, size_t len, unsigned int wtype);
> +
> +static void wqueue_consumer(int fd, wqueue_callback cb)
> +{
> +	unsigned char buffer[433], *p, *end;
> +	union {
> +		struct watch_notification n;
> +		unsigned char buf1[128];
> +	} n;
> +	ssize_t buf_len;
> +
> +	tst_res(TINFO, "Reading watch queue events");
> +
> +	buf_len = SAFE_READ(0, fd, buffer, sizeof(buffer));
> +
> +	p = buffer;
> +	end = buffer + buf_len;
> +	while (p < end) {
> +		size_t largest, len;
> +
> +		largest = end - p;
> +		if (largest > 128)
> +			largest = 128;
> +
> +		if (largest < sizeof(struct watch_notification))
> +			tst_brk(TBROK, "Short message header: %zu", largest);
> +
> +		memcpy(&n, p, largest);
> +
> +		tst_res(TINFO, "NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x",
> +				p - buffer, n.n.type, n.n.subtype, n.n.info);
> +
> +		len = n.n.info & WATCH_INFO_LENGTH;
> +		if (len < sizeof(n.n) || len > largest)
> +			tst_brk(TBROK, "Bad message length: %zu/%zu", len, largest);
> +
> +		cb(&n.n, len, n.n.type);
> +
> +		p += len;
> +	}
> +}
> +
> +#endif
> diff --git a/testcases/kernel/watchqueue/wqueue01.c b/testcases/kernel/watchqueue/wqueue01.c
> new file mode 100644
> index 000000000..24c3908ce
> --- /dev/null
> +++ b/testcases/kernel/watchqueue/wqueue01.c
> @@ -0,0 +1,40 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test if keyctl update is correctly recognized by watch queue.
> + */
> +
> +#include "tst_test.h"
> +#include "lapi/keyctl.h"
> +#include "common.h"
> +
> +static void saw_key_updated(struct watch_notification *n, size_t len, unsigned int wtype)
> +{
> +	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UPDATED))
> +		tst_res(TPASS, "keyctl update has been recognized");
> +	else
> +		tst_res(TFAIL, "keyctl update has not been recognized");
> +}
> +
> +static void run(void)
> +{
> +	int fd;
> +	key_serial_t key;
> +
> +	fd = wqueue_watch(256, &wqueue_filter);
> +	key = wqueue_add_key(fd);
> +
> +	keyctl(KEYCTL_UPDATE, key, "b", 1);
> +	wqueue_consumer(fd, saw_key_updated);
> +
> +	SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +};
> -- 
> 2.34.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-01-26 11:56 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-07 21:20 [LTP] [PATCH v4 00/11] watchqueue testing suite Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 01/11] Add KEYCTL_WATCH_KEY to lapi/keyctl.h fallback Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 02/11] Add lapi/watch_queue.h header fallback Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 03/11] Add wqueue01 test Andrea Cervesato
2022-01-26 11:58   ` Cyril Hrubis
2022-01-07 21:20 ` [LTP] [PATCH v4 04/11] Add wqueue02 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 05/11] Add wqueue03 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 06/11] Add wqueue04 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 07/11] Add wqueue05 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 08/11] Add wqueue06 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 09/11] Add wqueue07 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 10/11] Add wqueue08 test Andrea Cervesato
2022-01-07 21:20 ` [LTP] [PATCH v4 11/11] Add wqueue09 test Andrea Cervesato

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox