Add new system call for non-destructive peek and inspection to posix ipc mqueue
Subsystems (20)
v3 cover Add new system call for non-destructive peek and inspection to posix ipc mqueue
Patch series overview:
1. Add New system call do_mq_timedreceive2() and handler implementation
2. Add system call number in all most common arch.
3. Prepared Documentation and test
4. Add entry in performance tools all most common file
5. Add bitmask flag and user struct size as input for extensibility
change since v2:
- bitmask flag is added and check for valid operation
- userspace struct argument size is added for extensibility
- new test is added
- documentation is updated approximately
- increased the total system call count in performance tool as well
- v2 Link: https://lore.kernel.org/linux-arch/[email protected]/T/#t
Thanks for reviewing.
Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/ipc.rst | 228 +++++ arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/tools/syscall_32.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 +- ipc/mqueue.c | 211 ++++- ipc/msg.c | 2 +- ipc/msgutil.c | 48 +- ipc/util.h | 3 +- kernel/sys_ni.c | 3 + scripts/syscall.tbl | 1 + tools/include/uapi/asm-generic/unistd.h | 6 +- .../arch/alpha/entry/syscalls/syscall.tbl | 1 + .../perf/arch/arm/entry/syscalls/syscall.tbl | 1 + .../arch/arm64/entry/syscalls/syscall_32.tbl | 1 + .../arch/mips/entry/syscalls/syscall_n64.tbl | 1 + .../arch/parisc/entry/syscalls/syscall.tbl | 1 + .../arch/powerpc/entry/syscalls/syscall.tbl | 1 + .../perf/arch/s390/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/sh/entry/syscalls/syscall.tbl | 1 + .../arch/sparc/entry/syscalls/syscall.tbl | 1 + .../arch/x86/entry/syscalls/syscall_32.tbl | 1 + .../arch/x86/entry/syscalls/syscall_64.tbl | 1 + .../arch/xtensa/entry/syscalls/syscall.tbl | 1 + tools/scripts/syscall.tbl | 1 + tools/testing/selftests/ipc/.gitignore | 1 + tools/testing/selftests/ipc/Makefile | 9 +- tools/testing/selftests/ipc/mq_peek.c | 813 ++++++++++++++++++ 45 files changed, 1343 insertions(+), 45 deletions(-) create mode 100644 Documentation/userspace-api/ipc.rst create mode 100644 tools/testing/selftests/ipc/mq_peek.c
Signature · 1 line
2.43.0
PATCH v3 1/5 IPC: Added New system call do_mq_timedreceive2() for non-destructive peek on posix mqueue
--- include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 ++- ipc/mqueue.c | 186 ++++++++++++++++++++++++++++-- ipc/msg.c | 2 +- ipc/msgutil.c | 48 ++++---- ipc/util.h | 3 +- kernel/sys_ni.c | 1 + 9 files changed, 231 insertions(+), 42 deletions(-)
include/linux/compat.h +4 -2
@@ -22,6 +22,7 @@#include <asm/compat.h> #include <asm/siginfo.h> #include <asm/signal.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /*
@@ -801,8 +802,9 @@ asmlinkage long compat_sys_pwritev64v2(unsigned long fd,const struct iovec __user *vec, unsigned long vlen, loff_t pos, rwf_t flags);#endif - - +asmlinkage long compat_sys_mq_timedreceive2 (mqd_t mqdes,struct compat_mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct old_timespec32 __user *abs_timeout);/* * Deprecated system calls which are still defined in * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch
include/linux/syscalls.h +6 -0
@@ -79,6 +79,7 @@ struct mnt_id_req;struct ns_id_req ;struct xattr_args ;struct file_attr ; +struct mq_timedreceive2_args ;#include <linux/types.h> #include <linux/aio_abi.h>
@@ -93,6 +94,7 @@ struct file_attr;#include <linux/key.h> #include <linux/personality.h> #include <trace/syscall.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /*
@@ -746,6 +748,10 @@ asmlinkage long sys_mq_timedsend_time32(mqd_t mqdes,const char __user *u_msg_ptr, unsigned int msg_len, unsigned int msg_prio, const struct old_timespec32 __user *u_abs_timeout); +asmlinkage long +sys_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct __kernel_timespec __user *abs_timeout);asmlinkage long sys_msgget (key_t key,int msgflg);asmlinkage long sys_old_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);
include/uapi/asm-generic/unistd.h +5 -2
@@ -863,9 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) -#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_mq_timedreceive2 472 +__SC_COMP (__NR_mq_timedreceive2, sys_mq_timedreceive2, + compat_sys_mq_timedreceive2) +#undef __NR_syscalls +#define __NR_syscalls 473/* * 32 bit systems traditionally used different * syscalls for off_t and loff_t arguments, while
include/uapi/linux/mqueue.h +13 -1
@@ -18,8 +18,8 @@#ifndef _LINUX_MQUEUE_H#define _LINUX_MQUEUE_H -#include <linux/types.h> +#include <asm/compat.h> #define MQ_PRIO_MAX 32768/* per-uid limit of kernel memory used by mqueue, in bytes */
@@ -33,6 +33,18 @@ struct mq_attr {__kernel_long_t __reserved[4 ];/* ignored for input, zeroed for output */ }; +struct mq_timedreceive2_args { +size_t msg_len; +unsigned int *msg_prio; +char *msg_ptr; +}; + +struct compat_mq_timedreceive2_args { +compat_size_t msg_len; +compat_uptr_t msg_prio; +compat_uptr_t msg_ptr; +}; +/* * SIGEV_THREAD implementation: * SIGEV_THREAD must be implemented in user space. If SIGEV_THREAD is passed
ipc/mqueue.c +178 -8
233 lines not on this page · raw message
ipc/msg.c +1 -1
@@ -1156,7 +1156,7 @@ static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, in* not update queue parameters. */ if (msgflg &MSG_COPY ) { - msg =copy_msg (msg, copy); + msg =copy_msg (msg, copy, msg->m_ts); goto out_unlock0; }
ipc/msgutil.c +21 -27
67 lines not on this page · raw message
ipc/util.h +2 -1
@@ -197,8 +197,9 @@ int ipc_parse_version(int *cmd);extern void free_msg (struct msg_msg *msg);extern struct msg_msg *load_msg (const void __user *src,size_t len); -extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst); +extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len);extern int store_msg (void __user *dest,struct msg_msg *msg,size_t len); +extern struct msg_msg *alloc_msg (size_t len);static inline int ipc_checkid (struct kern_ipc_perm *ipcp,int id) {
kernel/sys_ni.c +1 -0
@@ -391,6 +391,7 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq);COND_SYSCALL (rseq_slice_yield); +COND_SYSCALL (mq_timedreceive2);COND_SYSCALL (uretprobe);COND_SYSCALL (uprobe);
Signature · 1 line
2.43.0
Re: [PATCH v3 1/5]IPC: Added New system call do_mq_timedreceive2() for non-destructive peek on posix mqueue
Hi Mathura_Kumar,
First of all, please read
https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html.
You need to write a detailed commit message for each patch.
Before sending a patch, it is recommended to run checkpatch:
$ ./scripts/checkpatch.pl `git format-patch HEAD~1` WARNING: Missing commit description - Add an appropriate one
WARNING: line length of 107 exceeds 100 columns
#38: FILE: include/linux/compat.h:805:
+asmlinkage long compat_sys_mq_timedreceive2(mqd_t mqdes, struct compat_mq_timedreceive2_args __user *uargs,
WARNING: line length of 128 exceeds 100 columns
#39: FILE: include/linux/compat.h:806:
+ unsigned int flags, unsigned long index,
WARNING: line length of 131 exceeds 100 columns
#40: FILE: include/linux/compat.h:807:
+ struct old_timespec32 __user *abs_timeout);
WARNING: Use #include <linux/compat.h> instead of <asm/compat.h> #104: FILE: include/uapi/linux/mqueue.h:22:
+#include <asm/compat.h>
WARNING: line length of 122 exceeds 100 columns #306: FILE: ipc/mqueue.c:1636:
+ struct compat_mq_timedreceive2_args __user *uargs)
total: 0 errors, 6 warnings, 408 lines checked
Quoted 189 lines · Mathura_Kumar
Signed-off-by Mathura_Kumar <[email protected]>--- include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 ++- ipc/mqueue.c | 186 ++++++++++++++++++++++++++++-- ipc/msg.c | 2 +- ipc/msgutil.c | 48 ++++---- ipc/util.h | 3 +- kernel/sys_ni.c | 1 + 9 files changed, 231 insertions(+), 42 deletions(-)include/linux/compat.h +4
-2@@ -22,6 +22,7 @@#include <asm/compat.h> #include <asm/siginfo.h> #include <asm/signal.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -801,8 +802,9 @@ asmlinkage long compat_sys_pwritev64v2(unsigned long fd,const struct iovec __user *vec, unsigned long vlen, loff_t pos, rwf_t flags);#endif - - +asmlinkage long compat_sys_mq_timedreceive2 (mqd_t mqdes,struct compat_mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct old_timespec32 __user *abs_timeout);/* * Deprecated system calls which are still defined in * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch include/linux/syscalls.h +6
-0@@ -79,6 +79,7 @@ struct mnt_id_req;struct ns_id_req ;struct xattr_args ;struct file_attr ; +struct mq_timedreceive2_args ;#include <linux/types.h> #include <linux/aio_abi.h> @@ -93,6 +94,7 @@ struct file_attr;#include <linux/key.h> #include <linux/personality.h> #include <trace/syscall.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -746,6 +748,10 @@ asmlinkage long sys_mq_timedsend_time32(mqd_t mqdes,const char __user *u_msg_ptr, unsigned int msg_len, unsigned int msg_prio, const struct old_timespec32 __user *u_abs_timeout); +asmlinkage long +sys_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct __kernel_timespec __user *abs_timeout);asmlinkage long sys_msgget (key_t key,int msgflg);asmlinkage long sys_old_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);include/uapi/asm-generic/unistd.h +5
-2@@ -863,9 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) -#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_mq_timedreceive2 472 +__SC_COMP (__NR_mq_timedreceive2, sys_mq_timedreceive2, + compat_sys_mq_timedreceive2) +#undef __NR_syscalls +#define __NR_syscalls 473/* * 32 bit systems traditionally used different * syscalls for off_t and loff_t arguments, while include/uapi/linux/mqueue.h +13
-1@@ -18,8 +18,8 @@#ifndef _LINUX_MQUEUE_H#define _LINUX_MQUEUE_H -#include <linux/types.h> +#include <asm/compat.h> #define MQ_PRIO_MAX 32768/* per-uid limit of kernel memory used by mqueue, in bytes */ @@ -33,6 +33,18 @@ struct mq_attr {__kernel_long_t __reserved[4 ];/* ignored for input, zeroed for output */ }; +struct mq_timedreceive2_args { +size_t msg_len; +unsigned int *msg_prio; +char *msg_ptr; +}; + +struct compat_mq_timedreceive2_args { +compat_size_t msg_len; +compat_uptr_t msg_prio; +compat_uptr_t msg_ptr; +}; +/* * SIGEV_THREAD implementation: * SIGEV_THREAD must be implemented in user space. If SIGEV_THREAD is passed ipc/mqueue.c +53
-0@@ -53,6 +53,7 @@ struct mqueue_fs_context {#define SEND 0#define RECV 1 +#define MQ_PEEK 2#define STATE_NONE 0#define STATE_READY 1@@ -1230,6 +1231,115 @@ static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,return ret; } +static struct msg_msg *mq_peek_index (struct mqueue_inode_info *info,int index) +{ +struct rb_node *node; +struct posix_msg_tree_node *leaf; +struct msg_msg *msg; + +int count =0 ; + +/* Start from highest priority */ + node =rb_last (&info->msg_tree); +while (node) { + leaf = rb_entry(node, struct posix_msg_tree_node, rb_node); +list_for_each_entry (msg, &leaf->msg_list, m_list) { +if (count == index) +return msg; + count++; + } + + node =rb_prev (node); + } + +return NULL ; +} + +static int do_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args *args, +unsigned int flags,unsigned long index, +struct timespec64 *ts) +{ +ssize_t ret; +struct msg_msg *msg_ptr, *k_msg_buffer; +long k_m_type; +size_t k_m_ts; +struct inode *inode; +struct mqueue_inode_info *info; + +if (!(flags &MQ_PEEK )) { +return do_mq_timedreceive (mqdes, args->msg_ptr, args->msg_len, + args->msg_prio, ts); + } +audit_mq_sendrecv (mqdes, args->msg_len,0 , ts); +CLASS (fd, f)(mqdes); +if (fd_empty (f)) +return -EBADF ; + + inode =file_inode (fd_file (f)); +if (unlikely (fd_file (f)->f_op != &mqueue_file_operations)) +return -EBADF ; + info =MQUEUE_I (inode); +audit_file (fd_file (f)); + +if (unlikely (!(fd_file (f)->f_mode &FMODE_READ ))) +return -EBADF ;
ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1295
Quoted 10 lines
ipc/mqueue.c +10
-0+ +if (unlikely (args->msg_len < info->attr.mq_msgsize)) +return -EMSGSIZE ; +if (index >= (unsigned long )info->attr.mq_maxmsg) +return -ENOENT ; + +spin_lock (&info->lock); +if (info->attr.mq_curmsgs ==0 ) { +spin_unlock (&info->lock); +return -EAGAIN ;
should it be ENOENT?
Quoted 9 lines
ipc/mqueue.c +9
-0+ } + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr) { +spin_unlock (&info->lock); +return -ENOENT ; + } + k_m_type = msg_ptr->m_type; + k_m_ts = msg_ptr->m_ts; +spin_unlock (&info->lock);
ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1314
Quoted 10 lines
ipc/mqueue.c +10
-0+ + k_msg_buffer =alloc_msg (k_m_ts); +if (!k_msg_buffer) +return -ENOMEM ; + +/* +* Two spin locks are necessary here. We are avoiding atomic memory +* allocation and premature allocation before confirming that +* a message actually exists to peek. +*/
MSG_COPY doesn't require to lock the spinlock twice, so why can't we do the same thing here?
ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1326
Quoted 12 lines
ipc/mqueue.c +12
-0+spin_lock (&info->lock); + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr || msg_ptr->m_type != k_m_type || + msg_ptr->m_ts != k_m_ts) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EAGAIN ; + } +if (IS_ERR (copy_msg (msg_ptr, k_msg_buffer, k_m_ts))) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EINVAL ;
you probably need to return the code returned by copy_msg...
Quoted 251 lines
ipc/mqueue.c +84
-8+ } +spin_unlock (&info->lock); + + ret = k_msg_buffer->m_ts; +if (args->msg_prio &&put_user (k_m_type, args->msg_prio)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +if (store_msg (args->msg_ptr, k_msg_buffer, k_m_ts)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +free_msg (k_msg_buffer); +return ret; +} + SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr, size_t, msg_len, unsigned int, msg_prio, const struct __kernel_timespec __user *, u_abs_timeout)@@ -1258,6 +1368,27 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct mq_timedreceive2_args __user *, uargs, unsigned int, + flags, const unsigned long, index, + const struct __kernel_timespec __user *, u_abs_timeout) +{ +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (copy_from_user (&args, uargs,sizeof (args))) +return -EFAULT ; + +if (u_abs_timeout) { +int res =prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); +} +/* * Notes: the case when user wants us to deregister (with NULL as pointer) * and he isn't currently owner of notification, will be silently discarded. @@ -1449,6 +1580,17 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return 0 ; } +#ifdef CONFIG_COMPAT_32BIT_TIME +static int compat_prepare_timeout (const struct old_timespec32 __user *p, +struct timespec64 *ts) +{ +if (get_old_timespec32 (ts, p)) +return -EFAULT ; +if (!timespec64_valid (ts)) +return -EINVAL ; +return 0 ; +} +#ifdef CONFIG_COMPAT struct compat_mq_attr {@@ -1490,6 +1632,22 @@ static inline int put_compat_mq_attr(const struct mq_attr *attr,return 0 ; } +static inline int get_compat_mq_args (struct mq_timedreceive2_args *args, +struct compat_mq_timedreceive2_args __user *uargs) +{ +struct compat_mq_timedreceive2_args v; + +if (copy_from_user (&v, uargs,sizeof (v))) +return -EFAULT ; + +memset (args,0 ,sizeof (*args)); + args->msg_len = (size_t )v.msg_len; + args->msg_prio = (unsigned int *)compat_ptr (v.msg_prio); + args->msg_ptr = (char *)compat_ptr (v.msg_ptr); + +return 0 ; +} + COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, compat_mode_t, mode, struct compat_mq_attr __user *, u_attr)@@ -1541,19 +1699,30 @@ COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return -EFAULT ;return 0 ; } -#endif -#ifdef CONFIG_COMPAT_32BIT_TIME -static int compat_prepare_timeout (const struct old_timespec32 __user *p, -struct timespec64 *ts) +COMPAT_SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct compat_mq_timedreceive2_args __user *, uargs, + unsigned int, flags, const unsigned long, index, + const struct old_timespec32 __user *, u_abs_timeout) { -if (get_old_timespec32 (ts, p)) +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (get_compat_mq_args (&args, uargs))return -EFAULT ; -if (!timespec64_valid (ts)) -return -EINVAL ; -return 0 ; + +if (u_abs_timeout) { +int res =compat_prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); } +#endif + SYSCALL_DEFINE5(mq_timedsend_time32, mqd_t, mqdes, const char __user *, u_msg_ptr, unsigned int, msg_len, unsigned int, msg_prio,@@ -1583,6 +1752,7 @@ SYSCALL_DEFINE5(mq_timedreceive_time32, mqd_t, mqdes, }return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +#endif static const struct inode_operations mqueue_dir_inode_operations = {ipc/msg.c +1
-1@@ -1156,7 +1156,7 @@ static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, in* not update queue parameters. */ if (msgflg &MSG_COPY ) { - msg =copy_msg (msg, copy); + msg =copy_msg (msg, copy, msg->m_ts); goto out_unlock0; }ipc/msgutil.c +21
-27@@ -51,7 +51,7 @@ static int __init init_msg_buckets(void) }subsys_initcall (init_msg_buckets); -static struct msg_msg *alloc_msg (size_t len) +struct msg_msg *alloc_msg (size_t len) {struct msg_msg *msg;struct msg_msgseg **pseg;@@ -122,39 +122,33 @@ struct msg_msg *load_msg(const void __user *src, size_t len)free_msg (msg);return ERR_PTR (err); } -#ifdef CONFIG_CHECKPOINT_RESTORE -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) + +struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len) { -struct msg_msgseg *dst_pseg, *src_pseg; -size_t len = src->m_ts; -size_t alen; +struct msg_msgseg *src_seg, *dst_seg; +size_t remaining, chunk; -if (src->m_ts > dst->m_ts) +if (len > src->m_ts)return ERR_PTR (-EINVAL ); - - alen =min (len,DATALEN_MSG ); -memcpy (dst +1 , src +1 , alen); - -for (dst_pseg = dst->next, src_pseg = src->next; - src_pseg !=NULL ; - dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) { - - len -= alen; - alen =min (len,DATALEN_SEG ); -memcpy (dst_pseg +1 , src_pseg +1 , alen); + chunk =min (len,DATALEN_MSG ); +memcpy (dst +1 , src +1 , chunk); + remaining = len - chunk; + src_seg = src->next; + dst_seg = dst->next; +while (remaining >0 && src_seg && dst_seg) { + chunk =min (remaining,DATALEN_SEG ); +memcpy (dst_seg +1 , src_seg +1 , chunk); + remaining -= chunk; + src_seg = src_seg->next; + dst_seg = dst_seg->next; } - +if (remaining !=0 ) +return ERR_PTR (-EINVAL ); dst->m_type = src->m_type; - dst->m_ts = src->m_ts; - + dst->m_ts = src->m_ts;return dst; } -#else -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) -{ -return ERR_PTR (-ENOSYS ); -} -#endif +int store_msg (void __user *dest,struct msg_msg *msg,size_t len) {size_t alen;ipc/util.h +2
-1@@ -197,8 +197,9 @@ int ipc_parse_version(int *cmd);extern void free_msg (struct msg_msg *msg);extern struct msg_msg *load_msg (const void __user *src,size_t len); -extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst); +extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len);extern int store_msg (void __user *dest,struct msg_msg *msg,size_t len); +extern struct msg_msg *alloc_msg (size_t len);static inline int ipc_checkid (struct kern_ipc_perm *ipcp,int id) {kernel/sys_ni.c +1
-1@@ -391,6 +391,7 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq);COND_SYSCALL (rseq_slice_yield); +COND_SYSCALL (mq_timedreceive2);COND_SYSCALL (uretprobe);COND_SYSCALL (uprobe); --2.43.0
Re: [PATCH v3 1/5]IPC: Added New system call do_mq_timedreceive2() for non-destructive peek on posix mqueue
Hi, Andrei
Thanks for review,
I have taken two lock, This is simple thought I had-
In first attempt, I want to have a look into queue to see, is even data exist to peek (because I preferred to allocate memory after confirmation) then I taken second lock after allocating temporary buffer to ensure, does even at this point already seen msg exist, intact and till we finish to copy to user_space, we are not going to allow other system call to consume same msg or alter anything. If I do not take a second lock, What if someone else in between tries to consume the same msg ?
One more approach- pre-allocate memory before confirming data, then take one lock during copy to temporary buffer only.
How ensure atomicity and existence of data during the period of copy if we do not take lock ?
Because copy_msg(msg_ptr, k_msg_buffer, k_m_ts) is doing a temporary copy to the kernel buffer only while holding a second lock, the final copy to use_space is lock free.
Please leave more insight on this.
Mathura,
Quoted 528 lines · Andrei Vagin
Hi
Mathura_Kumar,First of all, please read
https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html.You need to write a detailed commit message for each patch.
Before sending a patch, it is recommended to run checkpatch:
$ ./scripts/checkpatch.pl `git format-patch HEAD~1` WARNING: Missing commit description - Add an appropriate oneWARNING: line length of 107 exceeds 100 columns
#38: FILE:include/linux/compat.h:805:
+asmlinkage long compat_sys_mq_timedreceive2(mqd_t mqdes, structcompat_mq_timedreceive2_args__user*uargs,WARNING: line length of 128 exceeds 100 columns
#39: FILE:include/linux/compat.h:806:
+ unsigned int flags, unsigned long index,WARNING: line length of 131 exceeds 100 columns
#40: FILE:include/linux/compat.h:807:
+ structold_timespec32__user*abs_timeout);WARNING: Use #include <
linux/compat.h> instead of <asm/compat.h> #104: FILE:include/uapi/linux/mqueue.h:22:
+#include <asm/compat.h>WARNING: line length of 122 exceeds 100 columns #306: FILE:
ipc/mqueue.c:1636:
+ structcompat_mq_timedreceive2_args__user*uargs)total: 0 errors, 6 warnings, 408 lines checked
Quoted 189 lines · Mathura_Kumar
Signed-off-by Mathura_Kumar <[email protected]>--- include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 ++- ipc/mqueue.c | 186 ++++++++++++++++++++++++++++-- ipc/msg.c | 2 +- ipc/msgutil.c | 48 ++++---- ipc/util.h | 3 +- kernel/sys_ni.c | 1 + 9 files changed, 231 insertions(+), 42 deletions(-)include/linux/compat.h +4
-2@@ -22,6 +22,7 @@#include <asm/compat.h> #include <asm/siginfo.h> #include <asm/signal.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -801,8 +802,9 @@ asmlinkage long compat_sys_pwritev64v2(unsigned long fd,const struct iovec __user *vec, unsigned long vlen, loff_t pos, rwf_t flags);#endif - - +asmlinkage long compat_sys_mq_timedreceive2 (mqd_t mqdes,struct compat_mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct old_timespec32 __user *abs_timeout);/* * Deprecated system calls which are still defined in * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch include/linux/syscalls.h +6
-0@@ -79,6 +79,7 @@ struct mnt_id_req;struct ns_id_req ;struct xattr_args ;struct file_attr ; +struct mq_timedreceive2_args ;#include <linux/types.h> #include <linux/aio_abi.h> @@ -93,6 +94,7 @@ struct file_attr;#include <linux/key.h> #include <linux/personality.h> #include <trace/syscall.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -746,6 +748,10 @@ asmlinkage long sys_mq_timedsend_time32(mqd_t mqdes,const char __user *u_msg_ptr, unsigned int msg_len, unsigned int msg_prio, const struct old_timespec32 __user *u_abs_timeout); +asmlinkage long +sys_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct __kernel_timespec __user *abs_timeout);asmlinkage long sys_msgget (key_t key,int msgflg);asmlinkage long sys_old_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);include/uapi/asm-generic/unistd.h +5
-2@@ -863,9 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) -#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_mq_timedreceive2 472 +__SC_COMP (__NR_mq_timedreceive2, sys_mq_timedreceive2, + compat_sys_mq_timedreceive2) +#undef __NR_syscalls +#define __NR_syscalls 473/* * 32 bit systems traditionally used different * syscalls for off_t and loff_t arguments, while include/uapi/linux/mqueue.h +13
-1@@ -18,8 +18,8 @@#ifndef _LINUX_MQUEUE_H#define _LINUX_MQUEUE_H -#include <linux/types.h> +#include <asm/compat.h> #define MQ_PRIO_MAX 32768/* per-uid limit of kernel memory used by mqueue, in bytes */ @@ -33,6 +33,18 @@ struct mq_attr {__kernel_long_t __reserved[4 ];/* ignored for input, zeroed for output */ }; +struct mq_timedreceive2_args { +size_t msg_len; +unsigned int *msg_prio; +char *msg_ptr; +}; + +struct compat_mq_timedreceive2_args { +compat_size_t msg_len; +compat_uptr_t msg_prio; +compat_uptr_t msg_ptr; +}; +/* * SIGEV_THREAD implementation: * SIGEV_THREAD must be implemented in user space. If SIGEV_THREAD is passed ipc/mqueue.c +53
-0@@ -53,6 +53,7 @@ struct mqueue_fs_context {#define SEND 0#define RECV 1 +#define MQ_PEEK 2#define STATE_NONE 0#define STATE_READY 1@@ -1230,6 +1231,115 @@ static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,return ret; } +static struct msg_msg *mq_peek_index (struct mqueue_inode_info *info,int index) +{ +struct rb_node *node; +struct posix_msg_tree_node *leaf; +struct msg_msg *msg; + +int count =0 ; + +/* Start from highest priority */ + node =rb_last (&info->msg_tree); +while (node) { + leaf = rb_entry(node, struct posix_msg_tree_node, rb_node); +list_for_each_entry (msg, &leaf->msg_list, m_list) { +if (count == index) +return msg; + count++; + } + + node =rb_prev (node); + } + +return NULL ; +} + +static int do_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args *args, +unsigned int flags,unsigned long index, +struct timespec64 *ts) +{ +ssize_t ret; +struct msg_msg *msg_ptr, *k_msg_buffer; +long k_m_type; +size_t k_m_ts; +struct inode *inode; +struct mqueue_inode_info *info; + +if (!(flags &MQ_PEEK )) { +return do_mq_timedreceive (mqdes, args->msg_ptr, args->msg_len, + args->msg_prio, ts); + } +audit_mq_sendrecv (mqdes, args->msg_len,0 , ts); +CLASS (fd, f)(mqdes); +if (fd_empty (f)) +return -EBADF ; + + inode =file_inode (fd_file (f)); +if (unlikely (fd_file (f)->f_op != &mqueue_file_operations)) +return -EBADF ; + info =MQUEUE_I (inode); +audit_file (fd_file (f)); + +if (unlikely (!(fd_file (f)->f_mode &FMODE_READ ))) +return -EBADF ;ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1295
Quoted 10 lines
ipc/mqueue.c +10
-0+ +if (unlikely (args->msg_len < info->attr.mq_msgsize)) +return -EMSGSIZE ; +if (index >= (unsigned long )info->attr.mq_maxmsg) +return -ENOENT ; + +spin_lock (&info->lock); +if (info->attr.mq_curmsgs ==0 ) { +spin_unlock (&info->lock); +return -EAGAIN ;should it be ENOENT?
Quoted 9 lines
ipc/mqueue.c +9
-0+ } + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr) { +spin_unlock (&info->lock); +return -ENOENT ; + } + k_m_type = msg_ptr->m_type; + k_m_ts = msg_ptr->m_ts; +spin_unlock (&info->lock);ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1314
Quoted 10 lines
ipc/mqueue.c +10
-0+ + k_msg_buffer =alloc_msg (k_m_ts); +if (!k_msg_buffer) +return -ENOMEM ; + +/* +* Two spin locks are necessary here. We are avoiding atomic memory +* allocation and premature allocation before confirming that +* a message actually exists to peek. +*/
MSG_COPYdoesn't require to lock the spinlock twice, so why can't we do the same thing here?ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1326
Quoted 12 lines
ipc/mqueue.c +12
-0+spin_lock (&info->lock); + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr || msg_ptr->m_type != k_m_type || + msg_ptr->m_ts != k_m_ts) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EAGAIN ; + } +if (IS_ERR (copy_msg (msg_ptr, k_msg_buffer, k_m_ts))) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EINVAL ;you probably need to return the code returned by
copy_msg...Quoted 251 lines
ipc/mqueue.c +84
-8+ } +spin_unlock (&info->lock); + + ret = k_msg_buffer->m_ts; +if (args->msg_prio &&put_user (k_m_type, args->msg_prio)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +if (store_msg (args->msg_ptr, k_msg_buffer, k_m_ts)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +free_msg (k_msg_buffer); +return ret; +} + SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr, size_t, msg_len, unsigned int, msg_prio, const struct __kernel_timespec __user *, u_abs_timeout)@@ -1258,6 +1368,27 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct mq_timedreceive2_args __user *, uargs, unsigned int, + flags, const unsigned long, index, + const struct __kernel_timespec __user *, u_abs_timeout) +{ +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (copy_from_user (&args, uargs,sizeof (args))) +return -EFAULT ; + +if (u_abs_timeout) { +int res =prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); +} +/* * Notes: the case when user wants us to deregister (with NULL as pointer) * and he isn't currently owner of notification, will be silently discarded. @@ -1449,6 +1580,17 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return 0 ; } +#ifdef CONFIG_COMPAT_32BIT_TIME +static int compat_prepare_timeout (const struct old_timespec32 __user *p, +struct timespec64 *ts) +{ +if (get_old_timespec32 (ts, p)) +return -EFAULT ; +if (!timespec64_valid (ts)) +return -EINVAL ; +return 0 ; +} +#ifdef CONFIG_COMPAT struct compat_mq_attr {@@ -1490,6 +1632,22 @@ static inline int put_compat_mq_attr(const struct mq_attr *attr,return 0 ; } +static inline int get_compat_mq_args (struct mq_timedreceive2_args *args, +struct compat_mq_timedreceive2_args __user *uargs) +{ +struct compat_mq_timedreceive2_args v; + +if (copy_from_user (&v, uargs,sizeof (v))) +return -EFAULT ; + +memset (args,0 ,sizeof (*args)); + args->msg_len = (size_t )v.msg_len; + args->msg_prio = (unsigned int *)compat_ptr (v.msg_prio); + args->msg_ptr = (char *)compat_ptr (v.msg_ptr); + +return 0 ; +} + COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, compat_mode_t, mode, struct compat_mq_attr __user *, u_attr)@@ -1541,19 +1699,30 @@ COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return -EFAULT ;return 0 ; } -#endif -#ifdef CONFIG_COMPAT_32BIT_TIME -static int compat_prepare_timeout (const struct old_timespec32 __user *p, -struct timespec64 *ts) +COMPAT_SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct compat_mq_timedreceive2_args __user *, uargs, + unsigned int, flags, const unsigned long, index, + const struct old_timespec32 __user *, u_abs_timeout) { -if (get_old_timespec32 (ts, p)) +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (get_compat_mq_args (&args, uargs))return -EFAULT ; -if (!timespec64_valid (ts)) -return -EINVAL ; -return 0 ; + +if (u_abs_timeout) { +int res =compat_prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); } +#endif + SYSCALL_DEFINE5(mq_timedsend_time32, mqd_t, mqdes, const char __user *, u_msg_ptr, unsigned int, msg_len, unsigned int, msg_prio,@@ -1583,6 +1752,7 @@ SYSCALL_DEFINE5(mq_timedreceive_time32, mqd_t, mqdes, }return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +#endif static const struct inode_operations mqueue_dir_inode_operations = {ipc/msg.c +1
-1@@ -1156,7 +1156,7 @@ static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, in* not update queue parameters. */ if (msgflg &MSG_COPY ) { - msg =copy_msg (msg, copy); + msg =copy_msg (msg, copy, msg->m_ts); goto out_unlock0; }ipc/msgutil.c +21
-27@@ -51,7 +51,7 @@ static int __init init_msg_buckets(void) }subsys_initcall (init_msg_buckets); -static struct msg_msg *alloc_msg (size_t len) +struct msg_msg *alloc_msg (size_t len) {struct msg_msg *msg;struct msg_msgseg **pseg;@@ -122,39 +122,33 @@ struct msg_msg *load_msg(const void __user *src, size_t len)free_msg (msg);return ERR_PTR (err); } -#ifdef CONFIG_CHECKPOINT_RESTORE -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) + +struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len) { -struct msg_msgseg *dst_pseg, *src_pseg; -size_t len = src->m_ts; -size_t alen; +struct msg_msgseg *src_seg, *dst_seg; +size_t remaining, chunk; -if (src->m_ts > dst->m_ts) +if (len > src->m_ts)return ERR_PTR (-EINVAL ); - - alen =min (len,DATALEN_MSG ); -memcpy (dst +1 , src +1 , alen); - -for (dst_pseg = dst->next, src_pseg = src->next; - src_pseg !=NULL ; - dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) { - - len -= alen; - alen =min (len,DATALEN_SEG ); -memcpy (dst_pseg +1 , src_pseg +1 , alen); + chunk =min (len,DATALEN_MSG ); +memcpy (dst +1 , src +1 , chunk); + remaining = len - chunk; + src_seg = src->next; + dst_seg = dst->next; +while (remaining >0 && src_seg && dst_seg) { + chunk =min (remaining,DATALEN_SEG ); +memcpy (dst_seg +1 , src_seg +1 , chunk); + remaining -= chunk; + src_seg = src_seg->next; + dst_seg = dst_seg->next; } - +if (remaining !=0 ) +return ERR_PTR (-EINVAL ); dst->m_type = src->m_type; - dst->m_ts = src->m_ts; - + dst->m_ts = src->m_ts;return dst; } -#else -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) -{ -return ERR_PTR (-ENOSYS ); -} -#endif +int store_msg (void __user *dest,struct msg_msg *msg,size_t len) {size_t alen;ipc/util.h +2
-1@@ -197,8 +197,9 @@ int ipc_parse_version(int *cmd);extern void free_msg (struct msg_msg *msg);extern struct msg_msg *load_msg (const void __user *src,size_t len); -extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst); +extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len);extern int store_msg (void __user *dest,struct msg_msg *msg,size_t len); +extern struct msg_msg *alloc_msg (size_t len);static inline int ipc_checkid (struct kern_ipc_perm *ipcp,int id) {kernel/sys_ni.c +1
-1@@ -391,6 +391,7 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq);COND_SYSCALL (rseq_slice_yield); +COND_SYSCALL (mq_timedreceive2);COND_SYSCALL (uretprobe);COND_SYSCALL (uprobe); --2.43.0
Re: [PATCH v3 1/5]IPC: Added New system call do_mq_timedreceive2() for non-destructive peek on posix mqueue
Quoted 15 lines · Mathura
Hi, Andrei
Thanks for review,I have taken two lock, This is simple thought I had-
In first attempt, I want to have a look into queue to see, is even data exist to peek (because I preferred to allocate memory after confirmation) then I taken second lock after allocating temporary buffer to ensure, does even at this point already seen msg exist, intact and till we finish to copy to
user_space, we are not going to allow other system call to consume same msg or alter anything. If I do not take a second lock, What if someone else in between tries to consume the same msg ?
Quoted 2 lines
One more approach- pre-allocate memory before confirming data, then take one lock during copy to temporary buffer only.
I think this is what you need to do.
Quoted 3 lines
How ensure atomicity and existence of data during the period of copy if we do not take lock ?
I don't suggest copying data without holding a lock...
Quoted 538 lines
Because
copy_msg(msg_ptr, k_msg_buffer, k_m_ts)is doing a temporary copy to the kernel buffer only while holding a second lock, the final copy touse_spaceis lock free.Please leave more insight on this.
Mathura,
Quoted 528 lines · Andrei Vagin
Hi
Mathura_Kumar,First of all, please read
https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html.You need to write a detailed commit message for each patch.
Before sending a patch, it is recommended to run checkpatch:
$ ./scripts/checkpatch.pl `git format-patch HEAD~1` WARNING: Missing commit description - Add an appropriate oneWARNING: line length of 107 exceeds 100 columns
#38: FILE:include/linux/compat.h:805:
+asmlinkage long compat_sys_mq_timedreceive2(mqd_t mqdes, structcompat_mq_timedreceive2_args__user*uargs,WARNING: line length of 128 exceeds 100 columns
#39: FILE:include/linux/compat.h:806:
+ unsigned int flags, unsigned long index,WARNING: line length of 131 exceeds 100 columns
#40: FILE:include/linux/compat.h:807:
+ structold_timespec32__user*abs_timeout);WARNING: Use #include <
linux/compat.h> instead of <asm/compat.h> #104: FILE:include/uapi/linux/mqueue.h:22:
+#include <asm/compat.h>WARNING: line length of 122 exceeds 100 columns #306: FILE:
ipc/mqueue.c:1636:
+ structcompat_mq_timedreceive2_args__user*uargs)total: 0 errors, 6 warnings, 408 lines checked
Quoted 189 lines · Mathura_Kumar
Signed-off-by Mathura_Kumar <[email protected]>--- include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 ++- ipc/mqueue.c | 186 ++++++++++++++++++++++++++++-- ipc/msg.c | 2 +- ipc/msgutil.c | 48 ++++---- ipc/util.h | 3 +- kernel/sys_ni.c | 1 + 9 files changed, 231 insertions(+), 42 deletions(-)include/linux/compat.h +4
-2@@ -22,6 +22,7 @@#include <asm/compat.h> #include <asm/siginfo.h> #include <asm/signal.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -801,8 +802,9 @@ asmlinkage long compat_sys_pwritev64v2(unsigned long fd,const struct iovec __user *vec, unsigned long vlen, loff_t pos, rwf_t flags);#endif - - +asmlinkage long compat_sys_mq_timedreceive2 (mqd_t mqdes,struct compat_mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct old_timespec32 __user *abs_timeout);/* * Deprecated system calls which are still defined in * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch include/linux/syscalls.h +6
-0@@ -79,6 +79,7 @@ struct mnt_id_req;struct ns_id_req ;struct xattr_args ;struct file_attr ; +struct mq_timedreceive2_args ;#include <linux/types.h> #include <linux/aio_abi.h> @@ -93,6 +94,7 @@ struct file_attr;#include <linux/key.h> #include <linux/personality.h> #include <trace/syscall.h> +#include <linux/mqueue.h> #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER /* @@ -746,6 +748,10 @@ asmlinkage long sys_mq_timedsend_time32(mqd_t mqdes,const char __user *u_msg_ptr, unsigned int msg_len, unsigned int msg_prio, const struct old_timespec32 __user *u_abs_timeout); +asmlinkage long +sys_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args __user *uargs, +unsigned int flags,unsigned long index, +struct __kernel_timespec __user *abs_timeout);asmlinkage long sys_msgget (key_t key,int msgflg);asmlinkage long sys_old_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);include/uapi/asm-generic/unistd.h +5
-2@@ -863,9 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) -#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_mq_timedreceive2 472 +__SC_COMP (__NR_mq_timedreceive2, sys_mq_timedreceive2, + compat_sys_mq_timedreceive2) +#undef __NR_syscalls +#define __NR_syscalls 473/* * 32 bit systems traditionally used different * syscalls for off_t and loff_t arguments, while include/uapi/linux/mqueue.h +13
-1@@ -18,8 +18,8 @@#ifndef _LINUX_MQUEUE_H#define _LINUX_MQUEUE_H -#include <linux/types.h> +#include <asm/compat.h> #define MQ_PRIO_MAX 32768/* per-uid limit of kernel memory used by mqueue, in bytes */ @@ -33,6 +33,18 @@ struct mq_attr {__kernel_long_t __reserved[4 ];/* ignored for input, zeroed for output */ }; +struct mq_timedreceive2_args { +size_t msg_len; +unsigned int *msg_prio; +char *msg_ptr; +}; + +struct compat_mq_timedreceive2_args { +compat_size_t msg_len; +compat_uptr_t msg_prio; +compat_uptr_t msg_ptr; +}; +/* * SIGEV_THREAD implementation: * SIGEV_THREAD must be implemented in user space. If SIGEV_THREAD is passed ipc/mqueue.c +53
-0@@ -53,6 +53,7 @@ struct mqueue_fs_context {#define SEND 0#define RECV 1 +#define MQ_PEEK 2#define STATE_NONE 0#define STATE_READY 1@@ -1230,6 +1231,115 @@ static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,return ret; } +static struct msg_msg *mq_peek_index (struct mqueue_inode_info *info,int index) +{ +struct rb_node *node; +struct posix_msg_tree_node *leaf; +struct msg_msg *msg; + +int count =0 ; + +/* Start from highest priority */ + node =rb_last (&info->msg_tree); +while (node) { + leaf = rb_entry(node, struct posix_msg_tree_node, rb_node); +list_for_each_entry (msg, &leaf->msg_list, m_list) { +if (count == index) +return msg; + count++; + } + + node =rb_prev (node); + } + +return NULL ; +} + +static int do_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args *args, +unsigned int flags,unsigned long index, +struct timespec64 *ts) +{ +ssize_t ret; +struct msg_msg *msg_ptr, *k_msg_buffer; +long k_m_type; +size_t k_m_ts; +struct inode *inode; +struct mqueue_inode_info *info; + +if (!(flags &MQ_PEEK )) { +return do_mq_timedreceive (mqdes, args->msg_ptr, args->msg_len, + args->msg_prio, ts); + } +audit_mq_sendrecv (mqdes, args->msg_len,0 , ts); +CLASS (fd, f)(mqdes); +if (fd_empty (f)) +return -EBADF ; + + inode =file_inode (fd_file (f)); +if (unlikely (fd_file (f)->f_op != &mqueue_file_operations)) +return -EBADF ; + info =MQUEUE_I (inode); +audit_file (fd_file (f)); + +if (unlikely (!(fd_file (f)->f_mode &FMODE_READ ))) +return -EBADF ;ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1295
Quoted 10 lines
ipc/mqueue.c +10
-0+ +if (unlikely (args->msg_len < info->attr.mq_msgsize)) +return -EMSGSIZE ; +if (index >= (unsigned long )info->attr.mq_maxmsg) +return -ENOENT ; + +spin_lock (&info->lock); +if (info->attr.mq_curmsgs ==0 ) { +spin_unlock (&info->lock); +return -EAGAIN ;should it be ENOENT?
Quoted 9 lines
ipc/mqueue.c +9
-0+ } + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr) { +spin_unlock (&info->lock); +return -ENOENT ; + } + k_m_type = msg_ptr->m_type; + k_m_ts = msg_ptr->m_ts; +spin_unlock (&info->lock);ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1314
Quoted 10 lines
ipc/mqueue.c +10
-0+ + k_msg_buffer =alloc_msg (k_m_ts); +if (!k_msg_buffer) +return -ENOMEM ; + +/* +* Two spin locks are necessary here. We are avoiding atomic memory +* allocation and premature allocation before confirming that +* a message actually exists to peek. +*/
MSG_COPYdoesn't require to lock the spinlock twice, so why can't we do the same thing here?ipc/mqueue.c static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr, · line 1326
Quoted 12 lines
ipc/mqueue.c +12
-0+spin_lock (&info->lock); + msg_ptr =mq_peek_index (info, index); +if (!msg_ptr || msg_ptr->m_type != k_m_type || + msg_ptr->m_ts != k_m_ts) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EAGAIN ; + } +if (IS_ERR (copy_msg (msg_ptr, k_msg_buffer, k_m_ts))) { +spin_unlock (&info->lock); +free_msg (k_msg_buffer); +return -EINVAL ;you probably need to return the code returned by
copy_msg...Quoted 251 lines
ipc/mqueue.c +84
-8+ } +spin_unlock (&info->lock); + + ret = k_msg_buffer->m_ts; +if (args->msg_prio &&put_user (k_m_type, args->msg_prio)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +if (store_msg (args->msg_ptr, k_msg_buffer, k_m_ts)) { +free_msg (k_msg_buffer); +return -EFAULT ; + } +free_msg (k_msg_buffer); +return ret; +} + SYSCALL_DEFINE5(mq_timedsend, mqd_t, mqdes, const char __user *, u_msg_ptr, size_t, msg_len, unsigned int, msg_prio, const struct __kernel_timespec __user *, u_abs_timeout)@@ -1258,6 +1368,27 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct mq_timedreceive2_args __user *, uargs, unsigned int, + flags, const unsigned long, index, + const struct __kernel_timespec __user *, u_abs_timeout) +{ +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (copy_from_user (&args, uargs,sizeof (args))) +return -EFAULT ; + +if (u_abs_timeout) { +int res =prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); +} +/* * Notes: the case when user wants us to deregister (with NULL as pointer) * and he isn't currently owner of notification, will be silently discarded. @@ -1449,6 +1580,17 @@ SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return 0 ; } +#ifdef CONFIG_COMPAT_32BIT_TIME +static int compat_prepare_timeout (const struct old_timespec32 __user *p, +struct timespec64 *ts) +{ +if (get_old_timespec32 (ts, p)) +return -EFAULT ; +if (!timespec64_valid (ts)) +return -EINVAL ; +return 0 ; +} +#ifdef CONFIG_COMPAT struct compat_mq_attr {@@ -1490,6 +1632,22 @@ static inline int put_compat_mq_attr(const struct mq_attr *attr,return 0 ; } +static inline int get_compat_mq_args (struct mq_timedreceive2_args *args, +struct compat_mq_timedreceive2_args __user *uargs) +{ +struct compat_mq_timedreceive2_args v; + +if (copy_from_user (&v, uargs,sizeof (v))) +return -EFAULT ; + +memset (args,0 ,sizeof (*args)); + args->msg_len = (size_t )v.msg_len; + args->msg_prio = (unsigned int *)compat_ptr (v.msg_prio); + args->msg_ptr = (char *)compat_ptr (v.msg_ptr); + +return 0 ; +} + COMPAT_SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, compat_mode_t, mode, struct compat_mq_attr __user *, u_attr)@@ -1541,19 +1699,30 @@ COMPAT_SYSCALL_DEFINE3(mq_getsetattr, mqd_t, mqdes,return -EFAULT ;return 0 ; } -#endif -#ifdef CONFIG_COMPAT_32BIT_TIME -static int compat_prepare_timeout (const struct old_timespec32 __user *p, -struct timespec64 *ts) +COMPAT_SYSCALL_DEFINE5 (mq_timedreceive2, mqd_t, mqdes, + struct compat_mq_timedreceive2_args __user *, uargs, + unsigned int, flags, const unsigned long, index, + const struct old_timespec32 __user *, u_abs_timeout) { -if (get_old_timespec32 (ts, p)) +struct mq_timedreceive2_args args; +struct timespec64 ts, *p =NULL ; + +if (get_compat_mq_args (&args, uargs))return -EFAULT ; -if (!timespec64_valid (ts)) -return -EINVAL ; -return 0 ; + +if (u_abs_timeout) { +int res =compat_prepare_timeout (u_abs_timeout, &ts); + +if (res) +return res; + p = &ts; + } +return do_mq_timedreceive2 (mqdes, &args, flags, index, p); } +#endif + SYSCALL_DEFINE5(mq_timedsend_time32, mqd_t, mqdes, const char __user *, u_msg_ptr, unsigned int, msg_len, unsigned int, msg_prio,@@ -1583,6 +1752,7 @@ SYSCALL_DEFINE5(mq_timedreceive_time32, mqd_t, mqdes, }return do_mq_timedreceive (mqdes, u_msg_ptr, msg_len, u_msg_prio, p); } +#endif static const struct inode_operations mqueue_dir_inode_operations = {ipc/msg.c +1
-1@@ -1156,7 +1156,7 @@ static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, in* not update queue parameters. */ if (msgflg &MSG_COPY ) { - msg =copy_msg (msg, copy); + msg =copy_msg (msg, copy, msg->m_ts); goto out_unlock0; }ipc/msgutil.c +21
-27@@ -51,7 +51,7 @@ static int __init init_msg_buckets(void) }subsys_initcall (init_msg_buckets); -static struct msg_msg *alloc_msg (size_t len) +struct msg_msg *alloc_msg (size_t len) {struct msg_msg *msg;struct msg_msgseg **pseg;@@ -122,39 +122,33 @@ struct msg_msg *load_msg(const void __user *src, size_t len)free_msg (msg);return ERR_PTR (err); } -#ifdef CONFIG_CHECKPOINT_RESTORE -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) + +struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len) { -struct msg_msgseg *dst_pseg, *src_pseg; -size_t len = src->m_ts; -size_t alen; +struct msg_msgseg *src_seg, *dst_seg; +size_t remaining, chunk; -if (src->m_ts > dst->m_ts) +if (len > src->m_ts)return ERR_PTR (-EINVAL ); - - alen =min (len,DATALEN_MSG ); -memcpy (dst +1 , src +1 , alen); - -for (dst_pseg = dst->next, src_pseg = src->next; - src_pseg !=NULL ; - dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) { - - len -= alen; - alen =min (len,DATALEN_SEG ); -memcpy (dst_pseg +1 , src_pseg +1 , alen); + chunk =min (len,DATALEN_MSG ); +memcpy (dst +1 , src +1 , chunk); + remaining = len - chunk; + src_seg = src->next; + dst_seg = dst->next; +while (remaining >0 && src_seg && dst_seg) { + chunk =min (remaining,DATALEN_SEG ); +memcpy (dst_seg +1 , src_seg +1 , chunk); + remaining -= chunk; + src_seg = src_seg->next; + dst_seg = dst_seg->next; } - +if (remaining !=0 ) +return ERR_PTR (-EINVAL ); dst->m_type = src->m_type; - dst->m_ts = src->m_ts; - + dst->m_ts = src->m_ts;return dst; } -#else -struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst) -{ -return ERR_PTR (-ENOSYS ); -} -#endif +int store_msg (void __user *dest,struct msg_msg *msg,size_t len) {size_t alen;ipc/util.h +2
-1@@ -197,8 +197,9 @@ int ipc_parse_version(int *cmd);extern void free_msg (struct msg_msg *msg);extern struct msg_msg *load_msg (const void __user *src,size_t len); -extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst); +extern struct msg_msg *copy_msg (struct msg_msg *src,struct msg_msg *dst,size_t len);extern int store_msg (void __user *dest,struct msg_msg *msg,size_t len); +extern struct msg_msg *alloc_msg (size_t len);static inline int ipc_checkid (struct kern_ipc_perm *ipcp,int id) {kernel/sys_ni.c +1
-1@@ -391,6 +391,7 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq);COND_SYSCALL (rseq_slice_yield); +COND_SYSCALL (mq_timedreceive2);COND_SYSCALL (uretprobe);COND_SYSCALL (uprobe); --2.43.0
PATCH v3 2/5 IPC: Added system call number in all most common arch.
In reply to Mathura_Kumar--- arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/tools/syscall_32.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + scripts/syscall.tbl | 1 + 17 files changed, 17 insertions(+)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -511,3 +511,4 @@ 579 common file_setattr sys_file_setattr 580 common listns sys_listns 581 common rseq_slice_yield sys_rseq_slice_yield +582 common mq_timedreceive2 sys_mq_timedreceive2
arch/arm/tools/syscall.tbl +1 -0
@@ -486,3 +486,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/arm64/tools/syscall_32.tbl +1 -0
@@ -483,3 +483,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -471,3 +471,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -477,3 +477,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -410,3 +410,4 @@ 469 n32 file_setattr sys_file_setattr 470 n32 listns sys_listns 471 n32 rseq_slice_yield sys_rseq_slice_yield +472 n32 mq_timedreceive2 compat_sys_mq_timedreceive2
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -386,3 +386,4 @@ 469 n64 file_setattr sys_file_setattr 470 n64 listns sys_listns 471 n64 rseq_slice_yield sys_rseq_slice_yield +472 n64 mq_timedreceive2 sys_mq_timedreceive2
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
@@ -459,3 +459,4 @@ 469 o32 file_setattr sys_file_setattr 470 o32 listns sys_listns 471 o32 rseq_slice_yield sys_rseq_slice_yield +472 o32 mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -470,3 +470,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -562,3 +562,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 nospu rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -398,3 +398,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -475,3 +475,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -517,3 +517,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -477,3 +477,4 @@ 469 i386 file_setattr sys_file_setattr 470 i386 listns sys_listns 471 i386 rseq_slice_yield sys_rseq_slice_yield +472 i386 mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -441,3 +441,4 @@ 547 x32 pwritev2 compat_sys_pwritev64v2 # This is the end of the legacy x32 range. Numbers 548 and above are # not special and are not to be used for x32-specific syscalls. +548 common mq_timedreceive2 sys_mq_timedreceive2
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -442,3 +442,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
scripts/syscall.tbl +1 -0
@@ -412,3 +412,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
Signature · 1 line
2.43.0
PATCH v3 3/5 IPC: Prepared Documentation and test
In reply to Mathura_Kumar--- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/ipc.rst | 222 +++++++ tools/testing/selftests/ipc/.gitignore | 1 + tools/testing/selftests/ipc/Makefile | 9 +- tools/testing/selftests/ipc/mq_peek.c | 785 +++++++++++++++++++++++++ 5 files changed, 1017 insertions(+), 1 deletion(-) create mode 100644 Documentation/userspace-api/ipc.rst create mode 100644 tools/testing/selftests/ipc/mq_peek.c
Documentation/userspace-api/index.rst +1 -0
@@ -22,6 +22,7 @@ System calls ioctl/index mseal rseq + ipc Security-related interfaces ===========================
Documentation/userspace-api/ipc.rst new file +222 -0
222 lines not on this page · raw message
tools/testing/selftests/ipc/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only msgque_test msgque +mq_peek
tools/testing/selftests/ipc/Makefile +8 -1
@@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 uname_M := $(shell uname -m 2>/dev/null || echo not) ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/) +CC = gcc +CFLAGS = -static -O2 ifeq ($(ARCH),i386) ARCH := x86 CFLAGS := -DCONFIG_X86_32 -D__i386__
@@ -9,10 +11,15 @@ ifeq ($(ARCH),x86_64) ARCH := x86 CFLAGS := -DCONFIG_X86_64 -D__x86_64__ endif +ifeq ($(ARCH),aarch64) + ARCH := arm64 + CFLAGS := -DCONFIG_ARM64 +endif CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := msgque +TEST_GEN_PROGS := msgque mq_peek +LDLIBS += -lrt -lpthread include ../lib.mk
tools/testing/selftests/ipc/mq_peek.c new file +785 -0
785 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 4/5 IPC:Added entry in performance tools for new system call
In reply to Mathura_Kumar--- tools/include/uapi/asm-generic/unistd.h | 6 +++++- tools/perf/arch/alpha/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/arm/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl | 1 + tools/perf/arch/mips/entry/syscalls/syscall_n64.tbl | 1 + tools/perf/arch/parisc/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/powerpc/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/s390/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/sh/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/sparc/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/x86/entry/syscalls/syscall_32.tbl | 1 + tools/perf/arch/x86/entry/syscalls/syscall_64.tbl | 1 + tools/perf/arch/xtensa/entry/syscalls/syscall.tbl | 1 + tools/scripts/syscall.tbl | 1 + 14 files changed, 18 insertions(+), 1 deletion(-)
tools/include/uapi/asm-generic/unistd.h +5 -1
@@ -863,8 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#define __NR_mq_timedreceive2 472 +__SC_COMP (__NR_mq_timedreceive2, sys_mq_timedreceive2, + compat_sys_mq_timedreceive2) +#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 473/* * 32 bit systems traditionally used different
tools/perf/arch/alpha/entry/syscalls/syscall.tbl +1 -0
@@ -502,3 +502,4 @@ 570 common lsm_set_self_attr sys_lsm_set_self_attr 571 common lsm_list_modules sys_lsm_list_modules 572 common mseal sys_mseal +582 common mq_timedreceive2 sys_mq_timedreceive2
tools/perf/arch/arm/entry/syscalls/syscall.tbl +1 -0
@@ -486,3 +486,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
tools/perf/arch/arm64/entry/syscalls/syscall_32.tbl +1 -0
@@ -474,3 +474,4 @@ 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules 462 common mseal sys_mseal +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/mips/entry/syscalls/syscall_n64.tbl +1 -0
@@ -386,3 +386,4 @@ 469 n64 file_setattr sys_file_setattr 470 n64 listns sys_listns 471 n64 rseq_slice_yield sys_rseq_slice_yield +472 n64 mq_timedreceive2 sys_mq_timedreceive2
tools/perf/arch/parisc/entry/syscalls/syscall.tbl +1 -0
@@ -461,3 +461,4 @@ 460 common lsm_set_self_attr sys_lsm_set_self_attr 461 common lsm_list_modules sys_lsm_list_modules 462 common mseal sys_mseal +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/powerpc/entry/syscalls/syscall.tbl +1 -0
@@ -562,3 +562,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 nospu rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/s390/entry/syscalls/syscall.tbl +1 -0
@@ -398,3 +398,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/sh/entry/syscalls/syscall.tbl +1 -0
@@ -475,3 +475,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
tools/perf/arch/sparc/entry/syscalls/syscall.tbl +1 -0
@@ -517,3 +517,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -477,3 +477,4 @@ 469 i386 file_setattr sys_file_setattr 470 i386 listns sys_listns 471 i386 rseq_slice_yield sys_rseq_slice_yield +472 i386 mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -441,3 +441,4 @@ 547 x32 pwritev2 compat_sys_pwritev64v2 # This is the end of the legacy x32 range. Numbers 548 and above are # not special and are not to be used for x32-specific syscalls. +548 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
tools/perf/arch/xtensa/entry/syscalls/syscall.tbl +1 -0
@@ -442,3 +442,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2
tools/scripts/syscall.tbl +1 -0
@@ -412,3 +412,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mq_timedreceive2 sys_mq_timedreceive2 compat_sys_mq_timedreceive2
Signature · 1 line
2.43.0
PATCH v3 5/5 Added bitmask flag and user struct argument size as input for extensibility
In reply to Mathura_Kumar--- Documentation/userspace-api/ipc.rst | 24 ++-- include/linux/compat.h | 4 +- include/linux/syscalls.h | 4 +- ipc/mqueue.c | 179 +++++++++++++++----------- kernel/sys_ni.c | 2 + tools/testing/selftests/ipc/mq_peek.c | 54 ++++++-- 6 files changed, 163 insertions(+), 104 deletions(-)
Documentation/userspace-api/ipc.rst +15 -9
@@ -54,13 +54,10 @@ SYNOPSIS ssize_t mq_timedreceive2(mqd_t mqdes, struct mq_timedreceive2_args *uargs, - unsigned int flags, + size_t usize, unsigned int flags, unsigned long index, const struct timespec *abs_timeout); -Note: No glibc wrapper exists for this syscall. Callers must invoke it -directly using syscall(2). - DESCRIPTION mq_timedreceive2() receives or peeks at a message from the message queue referred to by the descriptor mqdes.
@@ -78,14 +75,19 @@ DESCRIPTION If not NULL, the priority of the received message is stored here. + The usize argument take size of struct to make mq_timedreceive2_args + extensible in future if required. + The flags argument controls receive behavior. The following flag is defined: ``MQ_PEEK`` Copy the message into msg_ptr without removing it from - the queue. The queue is not modified. If this flag is - not set, behavior is identical to mq_timedreceive() and - the message is consumed. + the queue. The queue is not modified. + + ``MQ_RECV`` + Copy the message into msg_ptr and consume a single message by + invoking the existing do_mq_timedreceive() handler. The index argument selects which message to operate on within the priority-ordered queue. index 0 refers to the highest
@@ -116,7 +118,7 @@ ERRORS ``EINVAL`` flags contains an unknown value, or index is nonzero - and MQ_PEEK is not set. + and contradictory flag was passed. ``EMSGSIZE`` msg_len is less than the mq_msgsize attribute of
@@ -137,7 +139,7 @@ consolidated into struct mq_timedreceive2_args rather than passed as individual syscall arguments. Due to limited six arguments, The original mq_timedreceive() consumes all six slots, leaving no room for extension. Consolidating the buffer parameters -into a struct recovers two argument slots for flags and index while +into a struct recovers three argument slots for flags, size and index while keeping the interface clean and forward-compatible. Future extensions can be made by adding new flag bits without
@@ -220,3 +222,7 @@ Tests for mq_timedreceive2() should cover the following: threads returns consistent results and does not corrupt queue state. Verify that a concurrent mq_receive() and mq_peek() do not race. + +9) Contradictory Operation: verify that simultaneous inconsistent flag work as + expected. +
include/linux/compat.h +2 -2
@@ -803,8 +803,8 @@ asmlinkage long compat_sys_pwritev64v2(unsigned long fd, unsigned long vlen, loff_t pos, rwf_t flags);#endif asmlinkage long compat_sys_mq_timedreceive2 (mqd_t mqdes,struct compat_mq_timedreceive2_args __user *uargs, -unsigned int flags,unsigned long index, -struct old_timespec32 __user *abs_timeout); +size_t usize,unsigned int flags,const unsigned long index, +const struct old_timespec32 __user *abs_timeout);/* * Deprecated system calls which are still defined in * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch
include/linux/syscalls.h +2 -2
@@ -750,8 +750,8 @@ asmlinkage long sys_mq_timedsend_time32(mqd_t mqdes, const struct old_timespec32 __user *u_abs_timeout);asmlinkage long sys_mq_timedreceive2 (mqd_t mqdes,struct mq_timedreceive2_args __user *uargs, -unsigned int flags,unsigned long index, -struct __kernel_timespec __user *abs_timeout); +size_t usize,unsigned int flags,const unsigned long index, +const struct __kernel_timespec __user *abs_timeout);asmlinkage long sys_msgget (key_t key,int msgflg);asmlinkage long sys_old_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf);
ipc/mqueue.c +101 -78
252 lines not on this page · raw message
kernel/sys_ni.c +2 -0
@@ -391,6 +391,8 @@ COND_SYSCALL(setuid16);/* restartable sequence */ COND_SYSCALL (rseq);COND_SYSCALL (rseq_slice_yield); + +/* ipc */ COND_SYSCALL (mq_timedreceive2);COND_SYSCALL (uretprobe);
tools/testing/selftests/ipc/mq_peek.c +41 -13
125 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 9 lines · Mathura_Kumar
Patch series overview:
1. Add New system call
do_mq_timedreceive2()and handler implementation
2. Add system call number in all most common arch.
3. Prepared Documentation and test
4. Add entry in performance tools all most common file
5. Add bitmask flag and user struct size as input for extensibility
Could you please explain why MSG_COPY can't be used to implement C/R? It was introduced by 4a674f34ba04a ("ipc: introduce message queue copy feature")
Quoted 65 lines
change since v2:
- bitmask flag is added and check for valid operation
- userspace struct argument size is added for extensibility
- new test is added
- documentation is updated approximately
- increased the total system call count in performance tool as well
- v2 Link: https://lore.kernel.org/linux-arch/[email protected]/T/#tThanks for reviewing.
Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/ipc.rst | 228 +++++ arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/tools/syscall_32.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 +- ipc/mqueue.c | 211 ++++- ipc/msg.c | 2 +- ipc/msgutil.c | 48 +- ipc/util.h | 3 +- kernel/sys_ni.c | 3 + scripts/syscall.tbl | 1 + tools/include/uapi/asm-generic/unistd.h | 6 +- .../arch/alpha/entry/syscalls/syscall.tbl | 1 + .../perf/arch/arm/entry/syscalls/syscall.tbl | 1 + .../arch/arm64/entry/syscalls/syscall_32.tbl | 1 + .../arch/mips/entry/syscalls/syscall_n64.tbl | 1 + .../arch/parisc/entry/syscalls/syscall.tbl | 1 + .../arch/powerpc/entry/syscalls/syscall.tbl | 1 + .../perf/arch/s390/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/sh/entry/syscalls/syscall.tbl | 1 + .../arch/sparc/entry/syscalls/syscall.tbl | 1 + .../arch/x86/entry/syscalls/syscall_32.tbl | 1 + .../arch/x86/entry/syscalls/syscall_64.tbl | 1 + .../arch/xtensa/entry/syscalls/syscall.tbl | 1 + tools/scripts/syscall.tbl | 1 + tools/testing/selftests/ipc/.gitignore | 1 + tools/testing/selftests/ipc/Makefile | 9 +- tools/testing/selftests/ipc/mq_peek.c | 813 ++++++++++++++++++ 45 files changed, 1343 insertions(+), 45 deletions(-) create mode 100644 Documentation/userspace-api/ipc.rst create mode 100644 tools/testing/selftests/ipc/mq_peek.c--
2.43.0
A message that is not in the archive (sent off-list or not yet received): CA+QNo220nhDmyazh=JQi0OEfhsGAc7az+GRkM=G5KdZj4+SyNg@mail.gmail.com
Hi, Andrei
Thanks for reviewing and time,
I saw the code what you pointing to, here is current constraint what I see-
1) "Kernel will figure out dilemma"- current implementation is seems
to be highly tied with System V MSG semantic, If we try to add logic to handle Posix queue as well, then I think we need to mix posix queue and current system V path, its create cross subsystem coupling,we need to detect is msqid input passed in arg of msgrcv() is file descriptor for posix queue or integer id of kernel object for system V (As we all know posix queue is implemented on virtual file system), then route to SysV path or POSIX path.We require extra lookup attempts and ambiguous failure modes.
Both are integers so in extremely rare cases ,it may overlap then, We may end-up copying unrelated data to user_space
2) Readability issue- we need to think about many things before merging both, like Wakeup semantic,data structure, priority and FIFO handling for posix msg.
3) Backward compatibility risk- changing behavior of msgrcv() to accept FDs behave differently based on detection, Could break existing programs and introduce silent misbehavior.
4) Non-Trivial to extend existing one- from given below snippet, it seem pretty much clear that no input to size of user_space struct taken, "msgsz" used for buffer or how many byte actually going to copy, if we just add new member in struct as input in case we required ,it may cause compatibility issue with different arch and 32 bit or 64 bit struct layout handling be tricky without the size of struct.
SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, long, msgtyp, int, msgflg)
{
return ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
}
and static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
long (*msg_handler)(void __user *, struct msg_msg *, size_t)){}Thanks,
Mathura
Quoted 108 lines · Mathura
Hi, Andrei
Thanks for reviewing and time,
I saw the code what you pointing to, here is current constraint what I see-1) "Kernel will figure out dilemma"- current implementation is seems to be highly tied with System V MSG semantic, If we try to add logic to handle Posix queue as well, then I think we need to mix posix queue and current system V path, its create cross subsystem coupling,we need to detect is msqid input passed in arg of
msgrcv()is file descriptor for posix queue or integer id of kernel object for system V (As we all know posix queue is implemented on virtual file system), then route to SysV path or POSIX path.We require extra lookup attempts and ambiguous failure modes.
Both are integers so in extremely rare cases ,it may overlap then, We may end-up copying unrelated data touser_space2) Readability issue- we need to think about many things before merging both, like Wakeup semantic,data structure, priority and FIFO handling for posix msg.
3) Backward compatibility risk- changing behavior of
msgrcv()to accept FDs behave differently based on detection, Could break existing programs and introduce silent misbehavior.4) Non-Trivial to extend existing one- from given below snippet, it seem pretty much clear that no input to size of
user_spacestruct taken, "msgsz" used for buffer or how many byte actually going to copy, if we just add new member in struct as input in case we required ,it may cause compatibility issue with different arch and 32 bit or 64 bit struct layout handling be tricky without the size of struct.
SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, long, msgtyp, int, msgflg)
{
returnksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
}and static long do_msgrcv(int msqid, void
__user*buf,size_tbufsz, long msgtyp, int msgflg,long (*msg_handler)(void __user *, struct msg_msg *, size_t)){}Thanks,
MathuraQuoted 80 lines · Andrei Vagin
Quoted 9 lines · Mathura_Kumar
Patch series overview:
1. Add New system call
do_mq_timedreceive2()and handler implementation
2. Add system call number in all most common arch.
3. Prepared Documentation and test
4. Add entry in performance tools all most common file
5. Add bitmask flag and user struct size as input for extensibilityCould you please explain why
MSG_COPYcan't be used to implement C/R? It was introduced by4a674f34ba04a("ipc: introduce message queue copy feature")Quoted 65 lines
change since v2:
- bitmask flag is added and check for valid operation
- userspace struct argument size is added for extensibility
- new test is added
- documentation is updated approximately
- increased the total system call count in performance tool as well
- v2 Link: https://lore.kernel.org/linux-arch/[email protected]/T/#tThanks for reviewing.
Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/ipc.rst | 228 +++++ arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/tools/syscall_32.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/compat.h | 6 +- include/linux/syscalls.h | 6 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/mqueue.h | 14 +- ipc/mqueue.c | 211 ++++- ipc/msg.c | 2 +- ipc/msgutil.c | 48 +- ipc/util.h | 3 +- kernel/sys_ni.c | 3 + scripts/syscall.tbl | 1 + tools/include/uapi/asm-generic/unistd.h | 6 +- .../arch/alpha/entry/syscalls/syscall.tbl | 1 + .../perf/arch/arm/entry/syscalls/syscall.tbl | 1 + .../arch/arm64/entry/syscalls/syscall_32.tbl | 1 + .../arch/mips/entry/syscalls/syscall_n64.tbl | 1 + .../arch/parisc/entry/syscalls/syscall.tbl | 1 + .../arch/powerpc/entry/syscalls/syscall.tbl | 1 + .../perf/arch/s390/entry/syscalls/syscall.tbl | 1 + tools/perf/arch/sh/entry/syscalls/syscall.tbl | 1 + .../arch/sparc/entry/syscalls/syscall.tbl | 1 + .../arch/x86/entry/syscalls/syscall_32.tbl | 1 + .../arch/x86/entry/syscalls/syscall_64.tbl | 1 + .../arch/xtensa/entry/syscalls/syscall.tbl | 1 + tools/scripts/syscall.tbl | 1 + tools/testing/selftests/ipc/.gitignore | 1 + tools/testing/selftests/ipc/Makefile | 9 +- tools/testing/selftests/ipc/mq_peek.c | 813 ++++++++++++++++++ 45 files changed, 1343 insertions(+), 45 deletions(-) create mode 100644 Documentation/userspace-api/ipc.rst create mode 100644 tools/testing/selftests/ipc/mq_peek.c--
2.43.0
Quoted 5 lines · Mathura
Hi, Andrei
Thanks for reviewing and time,
I saw the code what you pointing to, here is current constraint what I see-
That was my mistake. It happens when I am rushing through some unfamiliar code. I wasn't aware that IPC queues and POSIX mqueues shared some kernel logic. However, I still think it’s worth looking at the MSG_COPY implementation for IPC queues as a reference.
Thanks,
Andrei
Well, I will see that for reference
Before that here is quick question from my side-
Which better and clean approach
1) Current implementation follows one system call to one msg copy to user space as per priority and FIFO with a given index value from posix IPC RB Tree, this allows us to go beyond Just from peek to early inspection in some other use case by passing valid index.
2) Just dump everything and all data in one call to user space.
Thanks,
Mathura
Quoted 15 lines · Andrei Vagin
Quoted 5 lines · Mathura
Hi, Andrei
Thanks for reviewing and time,
I saw the code what you pointing to, here is current constraint what I see-That was my mistake. It happens when I am rushing through some unfamiliar code. I wasn't aware that IPC queues and POSIX mqueues shared some kernel logic. However, I still think it’s worth looking at the
MSG_COPYimplementation for IPC queues as a reference.Thanks,
Andrei
Quoted 11 lines · Mathura
Well, I will see that for reference
Before that here is quick question from my side-
Which better and clean approach1) Current implementation follows one system call to one msg copy to user space as per priority and FIFO with a given index value from posix IPC RB Tree, this allows us to go beyond Just from peek to early inspection in some other use case by passing valid index.
2) Just dump everything and all data in one call to user space.
It probably will not work reliably. I think we can do something inspired by recvmmsg:
mq_recvmmsg(mqdes, iovec, iovec_len, attrs, attrs_len, flags)
where attrs is an extandable structure:
struct mq_recvmmsg_attrs {
uint64_t peek_offset; // MSG_PEEK
/* new fields can be added in the future */
}
It is just an idea...
Thanks,
Andrei