Export posix mqueues and named semaphores to procfs
v2 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
Changes in v2:
- Implement review comments from Jan:
- Split test adjustments into two separate patches
- Move the posix xpntree to process.c/internal.h
- Remove posix xpntree external declaration from sem.c and mqueue.c
Florian Bezdeka (7):
cobalt/registry: Share xnregistry_vfreg_ops with other compile units cobalt/registry: Initialize refcnt of exported virtual proc files cobalt/posix/process: Adding a xnptree for exporting registry entries cobalt/posix/mqueue: Export created mqueues to procfs cobalt/posix/semaphore: Export named semaphores to procfs testsuite/smokey/leaks: Fix error reporting from child to parent
process
testsuite/smokey/leaks: Adding checks for procfs exports
include/cobalt/kernel/registry.h | 2 + kernel/cobalt/posix/internal.h | 2 + kernel/cobalt/posix/mqueue.c | 36 ++++++++++++++++- kernel/cobalt/posix/process.c | 2 + kernel/cobalt/posix/sem.c | 35 +++++++++++++++- kernel/cobalt/registry.c | 1 + testsuite/smokey/leaks/leaks.c | 68 +++++++++++++++++++++----------- 7 files changed, 121 insertions(+), 25 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/7 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
@@ -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
PATCH v2 2/7 cobalt/registry: Initialize refcnt of exported virtual proc files
In reply to Florian BezdekaThe 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
@@ -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
PATCH v2 3/7 cobalt/posix/process: Adding a xnptree for exporting registry entries
In reply to Florian BezdekaWill be used later for exporting registry entries, namely named semaphores as well as mqueues to /proc/xenomai/registry/posix/{mqueue,sem}
--- kernel/cobalt/posix/internal.h | 2 ++ kernel/cobalt/posix/process.c | 2 ++ 2 files changed, 4 insertions(+)
kernel/cobalt/posix/internal.h +2 -0
@@ -47,6 +47,8 @@#define cobalt_mark_deleted (t) ((t)->magic = ~(t)->magic) +extern struct xnptree posix_ptree; +static inline xnhandle_t cobalt_get_handle_from_user (xnhandle_t *u_h) {xnhandle_t handle;
kernel/cobalt/posix/process.c +2 -0
@@ -83,6 +83,8 @@ static struct xnsynch yield_sync;LIST_HEAD (cobalt_global_thread_list); +DEFINE_XNPTREE (posix_ptree,"posix" ); +struct cobalt_resources cobalt_global_resources = { .condq =LIST_HEAD_INIT (cobalt_global_resources.condq), .mutexq =LIST_HEAD_INIT (cobalt_global_resources.mutexq),
Signature · 1 line
2.30.2
PATCH v2 4/7 cobalt/posix/mqueue: Export created mqueues to procfs
In reply to Florian BezdekaExporting 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 | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)
kernel/cobalt/posix/mqueue.c +35 -1
@@ -87,6 +87,39 @@ static struct mq_attr default_attr = {static LIST_HEAD (cobalt_mqq); +#ifdef CONFIG_XENO_OPT_VFILE + +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 +385,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
PATCH v2 5/7 cobalt/posix/semaphore: Export named semaphores to procfs
In reply to Florian BezdekaExporting 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 | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
kernel/cobalt/posix/sem.c +34 -1
@@ -26,6 +26,38 @@#include "sem.h" #include <trace/events/cobalt-posix.h> +#ifdef CONFIG_XENO_OPT_VFILE + +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 +166,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
PATCH v2 6/7 testsuite/smokey/leaks: Fix error reporting from child to parent process
In reply to Florian BezdekaThe 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 | 41 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 23 deletions(-)
testsuite/smokey/leaks/leaks.c +18 -23
@@ -91,9 +91,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 +103,36 @@ 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; 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; + +return 0 ; }static int run_leaks (struct smokey_test *t,int argc,char *const argv[])
@@ -143,7 +141,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 +233,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 +244,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
PATCH v2 7/7 testsuite/smokey/leaks: Adding checks for procfs exports
In reply to Florian BezdekaMake sure that named semaphores as well as mqueues are exported to the procfs.
--- testsuite/smokey/leaks/leaks.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
testsuite/smokey/leaks/leaks.c +27 -0
@@ -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;
@@ -121,6 +140,10 @@ static inline int subprocess_leak(void)if (ret)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);
@@ -132,6 +155,10 @@ static inline int subprocess_leak(void)if (ret <0 )return ret; + ret =procfs_exists ("mqueue" ,MQ_NAME ); +if (ret) +return ret; +return 0 ; }
Signature · 1 line
2.30.2
Re: [PATCH v2 7/7] testsuite/smokey/leaks: Adding checks for procfs exports
Quoted 46 lines · Florian Bezdeka
Make sure that named semaphores as well as mqueues are exported to the procfs.
Signed-off-by Florian Bezdeka <[email protected]>--- testsuite/smokey/leaks/leaks.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)testsuite/smokey/leaks/leaks.c +19
-0@@ -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;
testsuite/smokey/leaks/leaks.c static inline int subprocess_leak(void) · line 146
Quoted 7 lines
testsuite/smokey/leaks/leaks.c +4
-0@@ -121,6 +140,10 @@ static inline int subprocess_leak(void)if (ret)return ret; + ret =procfs_exists ("sem" ,SEM_NAME ); +if (ret) +return ret; +
This comes too fast for the registry, see failing tests in CI, e.g.
https://source.denx.de/Xenomai/xenomai-images/-/jobs/313503. The registration with /proc seems to happen asynchronously. If I add a usleep(100000) to procfs_exists(), the tests seem to pass.
Jan
Quoted 15 lines
testsuite/smokey/leaks/leaks.c +4
-0sevt.sigev_notify =SIGEV_THREAD_ID ; sevt.sigev_signo =SIGALRM ; sevt.sigev_notify_thread_id =syscall (__NR_gettid);@@ -132,6 +155,10 @@ static inline int subprocess_leak(void)if (ret <0 )return ret; + ret =procfs_exists ("mqueue" ,MQ_NAME ); +if (ret) +return ret; +return 0 ; }
Signature · 2 lines
Siemens AG, T RDA IOT Corporate Competence Center Embedded Linux
Re: [PATCH v2 7/7] testsuite/smokey/leaks: Adding checks for procfs exports
Quoted 50 lines · Jan Kiszka
Quoted 49 lines · Florian Bezdeka
Make sure that named semaphores as well as mqueues are exported to the procfs.
Signed-off-by Florian Bezdeka <[email protected]>--- testsuite/smokey/leaks/leaks.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)testsuite/smokey/leaks/leaks.c +19
-0@@ -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;@@ -121,6 +140,10 @@ static inline int subprocess_leak(void)if (ret)return ret;
Quoted 9 lines
testsuite/smokey/leaks/leaks.c static inline int subprocess_leak(void) · line 146
Quoted 4 lines
testsuite/smokey/leaks/leaks.c +4
-0+ ret =procfs_exists ("sem" ,SEM_NAME ); +if (ret) +return ret; +This comes too fast for the registry, see failing tests in CI, e.g.
https://source.denx.de/Xenomai/xenomai-images/-/jobs/313503. The registration with /proc seems to happen asynchronously. If I add a usleep(100000) toprocfs_exists(), the tests seem to pass.
ACK that's a race. The export itself is delegated to Linux, that's the root cause. Let's try the usleep(). I don't have a better idea right now.
Quoted 19 lines
Jan
Quoted 15 lines
testsuite/smokey/leaks/leaks.c +4
-0sevt.sigev_notify =SIGEV_THREAD_ID ; sevt.sigev_signo =SIGALRM ; sevt.sigev_notify_thread_id =syscall (__NR_gettid);@@ -132,6 +155,10 @@ static inline int subprocess_leak(void)if (ret <0 )return ret; + ret =procfs_exists ("mqueue" ,MQ_NAME ); +if (ret) +return ret; +return 0 ; }
Jan Kiszka
AppliedQuoted 40 lines · Florian Bezdeka
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,
FlorianChanges in v2:
- Implement review comments from Jan:
- Split test adjustments into two separate patches
- Move the posix xpntree toprocess.c/internal.h
- Remove posix xpntree external declaration fromsem.candmqueue.cFlorian Bezdeka (7):
cobalt/registry: Sharexnregistry_vfreg_opswith other compile units cobalt/registry: Initialize refcnt of exported virtual proc files cobalt/posix/process: Adding a xnptree for exporting registry entries cobalt/posix/mqueue: Export created mqueues to procfs cobalt/posix/semaphore: Export named semaphores to procfs testsuite/smokey/leaks: Fix error reporting from child to parent
process
testsuite/smokey/leaks: Adding checks for procfs exports
Quoted 9 lines
include/cobalt/kernel/registry.h | 2 + kernel/cobalt/posix/internal.h | 2 + kernel/cobalt/posix/mqueue.c | 36 ++++++++++++++++- kernel/cobalt/posix/process.c | 2 + kernel/cobalt/posix/sem.c | 35 +++++++++++++++- kernel/cobalt/registry.c | 1 + testsuite/smokey/leaks/leaks.c | 68 +++++++++++++++++++++----------- 7 files changed, 121 insertions(+), 25 deletions(-)
Thanks, applied to next.
Jan
Signature · 2 lines
Siemens AG, T RDA IOT Corporate Competence Center Embedded Linux