Skip to contentSkip to conversation

Export posix mqueues and named semaphores to procfs

PATCH Superseded Florian Bezdeka 2 participants 10 messages Aug 23, 2021 – Aug 24, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Export posix mqueues and named semaphores to procfs

Hi all,

with this series I'm trying to care about all the feedback I got for RFCs I sent out some days ago. Exporting mqueues and named semaphores to the procfs helps to debug and identify resource leaks.

mqueues will be exported to
/proc/xenomai/registry/posix/mqueue/<name>

named semaphores will be exported to
/proc/xenomai/registry/posix/sem/<name>

While adding the tests I noticed that the exit code of the subprocess started in the smokey leak test was not correctly reported to the parent process. That is now fixed as well. (See last patch for details)

All patches compile time tested for all targets by the gitlab hackerspace CI pipeline. In addition manually tested on ARM64. My internal CI is currently out of order, so tests on real hardware were skipped this time.

Best regards,
Florian

Florian Bezdeka (6):
cobalt/registry: Share xnregistry_vfreg_ops with other compile units cobalt/registry: Initialize refcnt of exported virtual proc files cobalt/registry: Adding a new xnptree for exporting posix entries cobalt/posix/mqueue: Export created mqueues to procfs cobalt/posix/semaphore: Export named semaphores to procfs testsuite/smokey/leaks: Add export checks for mqueues and named
semaphores

 include/cobalt/kernel/registry.h |  2 +
 kernel/cobalt/posix/mqueue.c     | 38 +++++++++++++++++-
 kernel/cobalt/posix/sem.c        | 37 ++++++++++++++++-
 kernel/cobalt/registry.c         |  3 ++
 testsuite/smokey/leaks/leaks.c   | 68 +++++++++++++++++++++-----------
 5 files changed, 123 insertions(+), 25 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/6 cobalt/registry: Share xnregistry_vfreg_ops with other compile units

Allows re-using xnregistry_vfreg_ops. All other file options were already available.

---
 include/cobalt/kernel/registry.h | 2 ++
 1 file changed, 2 insertions(+)
include/cobalt/kernel/registry.h +2 -0
index 624db5a78..a459da503 100644
@@ -195,6 +195,8 @@ extern struct xnpnode_ops xnregistry_vfsnap_ops;
 
 extern struct xnpnode_ops xnregistry_vlink_ops;
 
+extern struct xnpnode_ops xnregistry_vfreg_ops;
+
 /** @} */
 
 #endif /* !_COBALT_KERNEL_REGISTRY_H */
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/6 cobalt/registry: Initialize refcnt of exported virtual proc files

In reply to Florian Bezdeka

The refcnt field was never initialized and triggered a BUG() in vfile_regular_release (vfile.c) when closing / releasing such a virtual file.

---
 kernel/cobalt/registry.c | 1 +
 1 file changed, 1 insertion(+)
kernel/cobalt/registry.c +1 -0
index fa1de66c3..211e0f76f 100644
@@ -403,6 +403,7 @@ static int registry_export_vfreg(struct xnobject *object,
 	object->vfile_u.vfreg.privsz = p->vfile.privsz;
 	object->vfile_u.vfreg.ops = p->vfile.ops;
 	object->vfile_u.vfreg.entry.lockops = p->vfile.lockops;
+	object->vfile_u.vfreg.entry.refcnt = 0;
 
 	ret = xnvfile_init_regular(object->key, &object->vfile_u.vfreg,
 				   &pnode->vdir);
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/6 cobalt/registry: Adding a new xnptree for exporting posix entries

In reply to Florian Bezdeka

Will be used later for exporting named semaphores as well as mqueues to /proc/xenomai/registry/posix/{mqueue,sem}

---
 kernel/cobalt/registry.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/cobalt/registry.c +2 -0
index 211e0f76f..3c67b64e6 100644
@@ -73,6 +73,8 @@ static int proc_virq;
 
 static struct xnvfile_directory registry_vfroot;
 
+DEFINE_XNPTREE(posix_ptree, "posix");
+
 static int usage_vfile_show(struct xnvfile_regular_iterator *it, void *data)
 {
 	xnvfile_printf(it, "%u/%u\n",
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH 3/6] cobalt/registry: Adding a new xnptree for exporting posix entries

Quoted 14 lines · Florian Bezdeka

Will be used later for exporting named semaphores as well as mqueues to /proc/xenomai/registry/posix/{mqueue,sem}

---
 kernel/cobalt/registry.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/cobalt/registry.c +0 -0
index 211e0f76f..3c67b64e6 100644

kernel/cobalt/registry.c static int proc_virq; · line 80

Quoted 8 lines
kernel/cobalt/registry.c +2 -0
@@ -73,6 +73,8 @@ static int proc_virq;
 
 static struct xnvfile_directory registry_vfroot;
 
+DEFINE_XNPTREE(posix_ptree, "posix");
+
 static int usage_vfile_show(struct xnvfile_regular_iterator *it, void *data)
 {
 	xnvfile_printf(it, "%u/%u\n",

Better let kernel/cobalt/posix/process.c carry this.

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#5 lore raw

PATCH 4/6 cobalt/posix/mqueue: Export created mqueues to procfs

In reply to Florian Bezdeka

Exporting created mqueues to /proc/xenomai/registry/posix/mqueue helps to identify resource leaks by making mqueues visible. The content of all created virtual files is empty. Removal of such files is not possible and reports "operation not supported".

---
 kernel/cobalt/posix/mqueue.c | 38 +++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
kernel/cobalt/posix/mqueue.c +37 -1
index ebe7cf7b0..72759e484 100644
@@ -87,6 +87,41 @@ static struct mq_attr default_attr = {
 
 static LIST_HEAD(cobalt_mqq);
 
+#ifdef CONFIG_XENO_OPT_VFILE
+
+extern struct xnptree posix_ptree;
+
+static int mq_vfile_show(struct xnvfile_regular_iterator *it, void *data)
+{
+	return 0;
+}
+
+static struct xnvfile_regular_ops mq_vfile_ops = {
+	.show = mq_vfile_show,
+};
+
+static struct xnpnode_regular __mq_pnode = {
+	.node = {
+		.dirname = "mqueue",
+		.root = &posix_ptree,
+		.ops = &xnregistry_vfreg_ops,
+	},
+	.vfile = {
+		.ops = &mq_vfile_ops,
+	},
+};
+
+#else /* !CONFIG_XENO_OPT_VFILE */
+
+static struct xnpnode_link __mq_pnode = {
+	.node = {
+		.dirname = "mqueue",
+	}
+};
+
+#endif /* !CONFIG_XENO_OPT_VFILE */
+
+
 static inline struct cobalt_msg *mq_msg_alloc(struct cobalt_mq *mq)
 {
 	if (list_empty(&mq->avail))
@@ -352,7 +387,8 @@ static int mq_open(int uqd, const char *name, int oflags,
 		}
 
 		xnlock_get_irqsave(&nklock, s);
-		err = xnregistry_enter(mq->name, mq, &mq->handle, NULL);
+		err = xnregistry_enter(mq->name, mq, &mq->handle,
+				       &__mq_pnode.node);
 		if (err < 0)
 			--mq->refs;
 		else
Signature · 1 line
2.30.2
#6 lore raw

Re: [PATCH 4/6] cobalt/posix/mqueue: Export created mqueues to procfs

Quoted 15 lines · Florian Bezdeka

Exporting created mqueues to /proc/xenomai/registry/posix/mqueue helps to identify resource leaks by making mqueues visible. The content of all created virtual files is empty. Removal of such files is not possible and reports "operation not supported".

---
 kernel/cobalt/posix/mqueue.c | 38 +++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
kernel/cobalt/posix/mqueue.c +0 -0
index ebe7cf7b0..72759e484 100644

kernel/cobalt/posix/mqueue.c static struct mq_attr default_attr = { · line 92

Quoted 6 lines
kernel/cobalt/posix/mqueue.c +3 -0
@@ -87,6 +87,41 @@ static struct mq_attr default_attr = {
 
 static LIST_HEAD(cobalt_mqq);
 
+#ifdef CONFIG_XENO_OPT_VFILE
+
+extern struct xnptree posix_ptree;

kernel/cobalt/posix/internal.h

Jan

Quoted 46 lines
kernel/cobalt/posix/mqueue.c +34 -1
+
+static int mq_vfile_show(struct xnvfile_regular_iterator *it, void *data)
+{
+	return 0;
+}
+
+static struct xnvfile_regular_ops mq_vfile_ops = {
+	.show = mq_vfile_show,
+};
+
+static struct xnpnode_regular __mq_pnode = {
+	.node = {
+		.dirname = "mqueue",
+		.root = &posix_ptree,
+		.ops = &xnregistry_vfreg_ops,
+	},
+	.vfile = {
+		.ops = &mq_vfile_ops,
+	},
+};
+
+#else /* !CONFIG_XENO_OPT_VFILE */
+
+static struct xnpnode_link __mq_pnode = {
+	.node = {
+		.dirname = "mqueue",
+	}
+};
+
+#endif /* !CONFIG_XENO_OPT_VFILE */
+
+
 static inline struct cobalt_msg *mq_msg_alloc(struct cobalt_mq *mq)
 {
 	if (list_empty(&mq->avail))
@@ -352,7 +387,8 @@ static int mq_open(int uqd, const char *name, int oflags,
 		}
 
 		xnlock_get_irqsave(&nklock, s);
-		err = xnregistry_enter(mq->name, mq, &mq->handle, NULL);
+		err = xnregistry_enter(mq->name, mq, &mq->handle,
+				       &__mq_pnode.node);
 		if (err < 0)
 			--mq->refs;
 		else
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#7 lore raw

PATCH 5/6 cobalt/posix/semaphore: Export named semaphores to procfs

In reply to Florian Bezdeka

Exporting named semaphores to /proc/xenomai/registry/posix/sem helps to identify resource leaks by making registered semaphores visible. The content of all created virtual files is empty. Removal of such files is not possible and reports "operation not supported".

---
 kernel/cobalt/posix/sem.c | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
kernel/cobalt/posix/sem.c +36 -1
index 72b20c78d..88d7b363a 100644
@@ -26,6 +26,40 @@
 #include "sem.h"
 #include <trace/events/cobalt-posix.h>
 
+#ifdef CONFIG_XENO_OPT_VFILE
+
+extern struct xnptree posix_ptree;
+
+static int sem_vfile_show(struct xnvfile_regular_iterator *it, void *data)
+{
+	return 0;
+}
+
+static struct xnvfile_regular_ops sem_vfile_ops = {
+	.show = sem_vfile_show,
+};
+
+static struct xnpnode_regular __sem_pnode = {
+	.node = {
+		.dirname = "sem",
+		.root = &posix_ptree,
+		.ops = &xnregistry_vfreg_ops,
+	},
+	.vfile = {
+		.ops = &sem_vfile_ops,
+	},
+};
+
+#else /* !CONFIG_XENO_OPT_VFILE */
+
+static struct xnpnode_link __sem_pnode = {
+	.node = {
+		.dirname = "sem",
+	}
+};
+
+#endif /* !CONFIG_XENO_OPT_VFILE */
+
 static inline struct cobalt_resources *sem_kqueue(struct cobalt_sem *sem)
 {
 	int pshared = !!(sem->flags & SEM_PSHARED);
@@ -134,7 +168,8 @@ __cobalt_sem_init(const char *name, struct cobalt_sem_shadow *sm,
 		goto err_lock_put;
 	}
 
-	ret = xnregistry_enter(name ?: "", sem, &sem->resnode.handle, NULL);
+	ret = xnregistry_enter(name ?: "", sem, &sem->resnode.handle,
+			       name ? &__sem_pnode.node : NULL);
 	if (ret < 0)
 		goto err_lock_put;
 
Signature · 1 line
2.30.2
#8 lore raw

PATCH 6/6 testsuite/smokey/leaks: Add export checks for mqueues and named semaphores

In reply to Florian Bezdeka

Make sure mqueues and named semaphores are exported to the procfs if the kernel has procfs support activated.

While at it: The exit code of the child process was never correctly reported to the parent process. Hence, the status code check was wrong at all. The variable previously named subprocess_status was not shared between both processes.

---
 testsuite/smokey/leaks/leaks.c | 68 ++++++++++++++++++++++------------
 1 file changed, 45 insertions(+), 23 deletions(-)
testsuite/smokey/leaks/leaks.c +45 -23
index d5577d85e..433dfcc17 100644
@@ -28,6 +28,7 @@
 #include <pthread.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
 #include <semaphore.h>
 #include <mqueue.h>
 #include <signal.h>
@@ -71,6 +72,24 @@ const char *memdev[] = {
 
 static int memdevfd[3];
 
+static int procfs_exists(const char *type, const char *name)
+{
+	struct stat s;
+	char path[128];
+	int ret;
+
+	/* Ignore if the kernel seems to be compiled without procfs support */
+	if (stat("/proc/xenomai", &s) || !S_ISDIR(s.st_mode))
+		return 0;
+
+	ret = snprintf(path, 128, "%s/%s/%s", "/proc/xenomai/registry/posix",
+		       type, &name[1]);
+	if (ret < 0)
+		return -EINVAL;
+
+	return smokey_check_errno(stat(path, &s));
+}
+
 static unsigned long long get_used(void)
 {
 	struct cobalt_memdev_stat statbuf;
@@ -91,9 +110,7 @@ static void *empty(void *cookie)
 	return cookie;
 }
 
-static int subprocess_status;
-
-static inline void subprocess_leak(void)
+static inline int subprocess_leak(void)
 {
 	struct sigevent sevt;
 	pthread_mutex_t mutex;
@@ -105,36 +122,44 @@ static inline void subprocess_leak(void)
 
 	ret = smokey_check_status(pthread_create(&thread, NULL, empty, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 	
 	ret = smokey_check_status(pthread_mutex_init(&mutex, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 	
 	ret = smokey_check_status(pthread_cond_init(&cond, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_errno(sem_init(&sem, 0, 0));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_errno(-!(sem_open(SEM_NAME, O_CREAT, 0644, 1)));
 	if (ret)
-		goto fail;
+		return ret;
+
+	ret = procfs_exists("sem", SEM_NAME);
+	if (ret)
+		return ret;
 
 	sevt.sigev_notify = SIGEV_THREAD_ID;
 	sevt.sigev_signo = SIGALRM;
 	sevt.sigev_notify_thread_id = syscall(__NR_gettid);
 	ret = smokey_check_errno(timer_create(CLOCK_MONOTONIC, &sevt, &tm));
 	if (ret)
-		goto fail;
-	
+		return ret;
+
 	ret = smokey_check_errno(mq_open(MQ_NAME, O_RDWR | O_CREAT, 0644, NULL));
-	if (ret >= 0)
-		return;
-fail:
-	subprocess_status = ret;
+	if (ret < 0)
+		return ret;
+
+	ret = procfs_exists("mqueue", MQ_NAME);
+	if (ret)
+		return ret;
+
+	return 0;
 }
 
 static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
@@ -143,7 +168,7 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	struct sigevent sevt;
 	pthread_mutex_t mutex;
 	pthread_cond_t cond;
-	int fd, failed = 0, i, ret;
+	int fd, failed = 0, i, ret, child_ret;
 	pthread_t thread;
 	sem_t sem, *psem;
 	timer_t tm;
@@ -235,11 +260,9 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	child = smokey_check_errno(fork());
 	if (child < 0)
 		return child;
-	if (!child) {
-		subprocess_leak();
-		exit(EXIT_SUCCESS);
-	}
-	while (waitpid(child, NULL, 0) == -1 && errno == EINTR);
+	if (!child)
+		exit(subprocess_leak());
+	while (waitpid(child, &child_ret, 0) == -1 && errno == EINTR);
 	sleep(1);
 
 	ret = smokey_check_errno(sem_unlink(SEM_NAME));
@@ -248,9 +271,8 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	ret = smokey_check_errno(mq_unlink(MQ_NAME));
 	if (ret)
 		return ret;
-	if (subprocess_status)
-		return subprocess_status;
-		
+	if (WIFEXITED(child_ret) && WEXITSTATUS(child_ret))
+		return -WEXITSTATUS(child_ret);
 	check_used("fork", before, failed);
 #endif
 
Signature · 1 line
2.30.2
#9 lore raw

Re: [PATCH 6/6] testsuite/smokey/leaks: Add export checks for mqueues and named semaphores

Quoted 7 lines · Florian Bezdeka

Make sure mqueues and named semaphores are exported to the procfs if the kernel has procfs support activated.

While at it: The exit code of the child process was never correctly reported to the parent process. Hence, the status code check was wrong at all. The variable previously named subprocess_status was not shared between both processes.

The second part should better be separate.

Jan

Quoted 147 lines
---
 testsuite/smokey/leaks/leaks.c | 68 ++++++++++++++++++++++------------
 1 file changed, 45 insertions(+), 23 deletions(-)
testsuite/smokey/leaks/leaks.c +45 -23
index d5577d85e..433dfcc17 100644
@@ -28,6 +28,7 @@
 #include <pthread.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
 #include <semaphore.h>
 #include <mqueue.h>
 #include <signal.h>
@@ -71,6 +72,24 @@ const char *memdev[] = {
 
 static int memdevfd[3];
 
+static int procfs_exists(const char *type, const char *name)
+{
+	struct stat s;
+	char path[128];
+	int ret;
+
+	/* Ignore if the kernel seems to be compiled without procfs support */
+	if (stat("/proc/xenomai", &s) || !S_ISDIR(s.st_mode))
+		return 0;
+
+	ret = snprintf(path, 128, "%s/%s/%s", "/proc/xenomai/registry/posix",
+		       type, &name[1]);
+	if (ret < 0)
+		return -EINVAL;
+
+	return smokey_check_errno(stat(path, &s));
+}
+
 static unsigned long long get_used(void)
 {
 	struct cobalt_memdev_stat statbuf;
@@ -91,9 +110,7 @@ static void *empty(void *cookie)
 	return cookie;
 }
 
-static int subprocess_status;
-
-static inline void subprocess_leak(void)
+static inline int subprocess_leak(void)
 {
 	struct sigevent sevt;
 	pthread_mutex_t mutex;
@@ -105,36 +122,44 @@ static inline void subprocess_leak(void)
 
 	ret = smokey_check_status(pthread_create(&thread, NULL, empty, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_status(pthread_mutex_init(&mutex, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_status(pthread_cond_init(&cond, NULL));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_errno(sem_init(&sem, 0, 0));
 	if (ret)
-		goto fail;
+		return ret;
 
 	ret = smokey_check_errno(-!(sem_open(SEM_NAME, O_CREAT, 0644, 1)));
 	if (ret)
-		goto fail;
+		return ret;
+
+	ret = procfs_exists("sem", SEM_NAME);
+	if (ret)
+		return ret;
 
 	sevt.sigev_notify = SIGEV_THREAD_ID;
 	sevt.sigev_signo = SIGALRM;
 	sevt.sigev_notify_thread_id = syscall(__NR_gettid);
 	ret = smokey_check_errno(timer_create(CLOCK_MONOTONIC, &sevt, &tm));
 	if (ret)
-		goto fail;
-	
+		return ret;
+
 	ret = smokey_check_errno(mq_open(MQ_NAME, O_RDWR | O_CREAT, 0644, NULL));
-	if (ret >= 0)
-		return;
-fail:
-	subprocess_status = ret;
+	if (ret < 0)
+		return ret;
+
+	ret = procfs_exists("mqueue", MQ_NAME);
+	if (ret)
+		return ret;
+
+	return 0;
 }
 
 static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
@@ -143,7 +168,7 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	struct sigevent sevt;
 	pthread_mutex_t mutex;
 	pthread_cond_t cond;
-	int fd, failed = 0, i, ret;
+	int fd, failed = 0, i, ret, child_ret;
 	pthread_t thread;
 	sem_t sem, *psem;
 	timer_t tm;
@@ -235,11 +260,9 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	child = smokey_check_errno(fork());
 	if (child < 0)
 		return child;
-	if (!child) {
-		subprocess_leak();
-		exit(EXIT_SUCCESS);
-	}
-	while (waitpid(child, NULL, 0) == -1 && errno == EINTR);
+	if (!child)
+		exit(subprocess_leak());
+	while (waitpid(child, &child_ret, 0) == -1 && errno == EINTR);
 	sleep(1);
 
 	ret = smokey_check_errno(sem_unlink(SEM_NAME));
@@ -248,9 +271,8 @@ static int run_leaks(struct smokey_test *t, int argc, char *const argv[])
 	ret = smokey_check_errno(mq_unlink(MQ_NAME));
 	if (ret)
 		return ret;
-	if (subprocess_status)
-		return subprocess_status;
-		
+	if (WIFEXITED(child_ret) && WEXITSTATUS(child_ret))
+		return -WEXITSTATUS(child_ret);
 	check_used("fork", before, failed);
 #endif
 
Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux