Skip to contentSkip to conversation

mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

PATCH New Mathura_Kumar 2 participants 8 messages Feb 26 – Feb 27

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

POSIX message queues currently lack a mechanism to read a message without removing it from the queue. This is a long-standing limitation,when we require inspection of queue state without altering it.

Modifying existing mq_receive() semantics via additional flags was considered. However, altering behavior of an existing syscall risks breaking backward compatibility for applications relying on current semantics. Since mq_receive() guarantees message removal, changing this contract is not safe.

To preserve ABI stability, this patch introduces a new system call that performs a non-destructive receive operation (peek). The existing behavior remains unchanged.

Design considerations:

Two approaches for copying message data to userspace were evaluated:

1) Refcount-based message lifecycle handling
- This can help us Avoids intermediate temp kernel copy
- Extends message lifetime
-But this may increase writer starvation under heavy load and
add unneassery complication on priority management and delay more time to free space in inode due refcount may prevent

2) Temporary kernel buffer copy
- Copies message into a bounded kernel buffer
- Reduces time message remains locked
- Improves fairness under write-heavy workloads
- Simpler lifetime management

My implementation adopts the temporary buffer approach to minimize starvation and reduce locking complexity. The design allows future transition if refcounting is deemed preferable.

Architecture support: Entry was made in relevant system call table
- x86
- ARM

Testing:
- 15+ functional test cases
- Multi-threaded producer/consumer scenarios
- concurrent pop and peek
- Edge cases: empty queue, FIFO invalid flags, signal interruption etc.

---
 arch/x86/entry/syscalls/syscall_32.tbl |   2 +
 arch/x86/entry/syscalls/syscall_64.tbl |   2 +
 include/linux/syscalls.h               |   9 +
 include/uapi/asm-generic/unistd.h      |   5 +-
 ipc/mqueue.c                           | 180 ++++++
 ipc/msg.c                              |   2 +-
 ipc/msgutil.c                          |  51 +-
 ipc/util.h                             |   3 +-
 tools/testing/selftests/ipc/.gitignore |   1 +
 tools/testing/selftests/ipc/Makefile   |   5 +-
 tools/testing/selftests/ipc/mq_peek.c  | 794 +++++++++++++++++++++++++
 11 files changed, 1022 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/ipc/mq_peek.c
arch/x86/entry/syscalls/syscall_32.tbl +2 -0
index f832ebd2d79b..d6701f763203 100644
@@ -477,3 +477,5 @@
 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_time32
+473	i386	mq_timedreceive2_time64	sys_mq_timedreceive2
arch/x86/entry/syscalls/syscall_64.tbl +2 -0
index 524155d655da..539654953280 100644
@@ -441,3 +441,5 @@
 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.
+# Adding new syscalls from here to keep marginal gap intact in 472 to 511 for future expansion
+548	common	mq_timedreceive2		sys_mq_timedreceive2
include/linux/syscalls.h +9 -0
index 02bd6ddb6278..4db405159a8b 100644
@@ -746,6 +746,15 @@ 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_mq_timedreceive2_time32(mqd_t mqdes,
+			    struct mq_timedreceive2_args __user *uargs,
+			    unsigned int flags, unsigned long index,
+			    struct old_timespec32 __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 +4 -1
index a627acc8fb5f..1d27fd3e118e 100644
@@ -865,7 +865,10 @@ __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield)
 
 #undef __NR_syscalls
 #define __NR_syscalls 472
-
+#define __NR_mq_timedreceive2 473
+__SC_3264(__NR_mq_timedreceive2, sys_mq_timedreceive2_time32, sys_mq_timedreceive2)
+#define __NR_mq_timedreceive2_time64 474
+__SYSCALL(__NR_mq_timedreceive2_time64, sys_mq_timedreceive2)
 /*
  * 32 bit systems traditionally used different
  * syscalls for off_t and loff_t arguments, while
ipc/mqueue.c +180 -0

228 lines not on this page · raw message

ipc/msg.c +1 -1
index 62996b97f0ac..6392b11dd7f7 100644
@@ -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 +23 -28

69 lines not on this page · raw message

ipc/util.h +2 -1
index a55d6cebe6d3..374abeee79b3 100644
@@ -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)
 {
tools/testing/selftests/ipc/.gitignore +1 -0
index 9ed280e4c704..fe609d98ecb2 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 msgque_test
 msgque
+mq_peek
tools/testing/selftests/ipc/Makefile +4 -1
index 50e9c299fc4a..fa9d4f500783 100644
@@ -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__
@@ -12,7 +14,8 @@ 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 +794 -0

794 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Mathura_Kumar,

kernel test robot noticed the following build warnings:

[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on linus/master v7.0-rc1 next-20260226] [cannot apply to shuah-kselftest/next shuah-kselftest/fixes tip/x86/asm] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathura_Kumar/mqueue-introduce-new-do_mq_timedreceive2-mq_peek-syscall-for-non-destructive-receive-and-inspection/20260227-021340
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20260226174010.118080-1-academic1mathura%40gmail.com
patch subject: [PATCH] mqueue: introduce new  do_mq_timedreceive2() [ mq_peek  syscall] for non-destructive receive             and inspection
config: s390-allnoconfig (https://download.01.org/0day-ci/archive/20260227/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 9a109fbb6e184ec9bcce10615949f598f4c974a9)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260227/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> <stdin>:1630:2: warning: syscall mq_timedreceive2 not implemented [-W#warnings]

1630 | #warning syscall mq_timedreceive2 not implemented
| ^
<stdin>:1633:2: warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]
1633 | #warning syscall mq_timedreceive2_time64 not implemented
| ^
2 warnings generated.
--

>> <stdin>:1630:2: warning: syscall mq_timedreceive2 not implemented [-W#warnings]

1630 | #warning syscall mq_timedreceive2 not implemented
| ^
<stdin>:1633:2: warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]
1633 | #warning syscall mq_timedreceive2_time64 not implemented
| ^
2 warnings generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Mathura_Kumar
kernel test robot noticed the following build errors:

Hi Mathura_Kumar,

kernel test robot noticed the following build errors:

[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on linus/master v7.0-rc1 next-20260226] [cannot apply to shuah-kselftest/next shuah-kselftest/fixes tip/x86/asm] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathura_Kumar/mqueue-introduce-new-do_mq_timedreceive2-mq_peek-syscall-for-non-destructive-receive-and-inspection/20260227-021340
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20260226174010.118080-1-academic1mathura%40gmail.com
patch subject: [PATCH] mqueue: introduce new  do_mq_timedreceive2() [ mq_peek  syscall] for non-destructive receive             and inspection
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20260227/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260227/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

>> <stdin>:1633:2: warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]

1633 | #warning syscall mq_timedreceive2_time64 not implemented
| ^
1 warning generated.
--

>> ld.lld: error: undefined symbol: __x64_sys_mq_timedreceive2
   >>> referenced by syscall_64.c
   >>>               arch/x86/entry/syscall_64.o:(x64_sys_call) in archive vmlinux.a
   >>> referenced by syscall_64.c
   >>>               arch/x86/entry/syscall_64.o:(sys_call_table) in archive vmlinux.a
   >>> did you mean: __x64_sys_mq_timedreceive
   >>> defined in: vmlinux.a(kernel/sys_ni.o)
--
>> <stdin>:1633:2: warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]

1633 | #warning syscall mq_timedreceive2_time64 not implemented
| ^
1 warning generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Mathura_Kumar
kernel test robot noticed the following build errors:

Hi Mathura_Kumar,

kernel test robot noticed the following build errors:

[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on linus/master v7.0-rc1 next-20260226] [cannot apply to shuah-kselftest/next shuah-kselftest/fixes tip/x86/asm] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathura_Kumar/mqueue-introduce-new-do_mq_timedreceive2-mq_peek-syscall-for-non-destructive-receive-and-inspection/20260227-021340
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20260226174010.118080-1-academic1mathura%40gmail.com
patch subject: [PATCH] mqueue: introduce new  do_mq_timedreceive2() [ mq_peek  syscall] for non-destructive receive             and inspection
config: um-allnoconfig (https://download.01.org/0day-ci/archive/20260227/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 9a109fbb6e184ec9bcce10615949f598f4c974a9)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260227/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

In file included from kernel/sched/rq-offsets.c:5:
In file included from kernel/sched/sched.h:64:
In file included from include/linux/syscalls_api.h:1:

>> include/linux/syscalls.h:750:42: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]

750 | sys_mq_timedreceive2(mqd_t mqdes, struct mq_timedreceive2_args __user *uargs,
| ^
include/linux/syscalls.h:755:15: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]
755 | struct mq_timedreceive2_args __user *uargs,
| ^
2 warnings generated.
--
/usr/bin/ld: warning: .tmp_vmlinux1 has a LOAD segment with RWX permissions

>> /usr/bin/ld: arch/x86/um/sys_call_table_32.o:(.rodata+0x760): undefined reference to `sys_mq_timedreceive2_time32'
>> /usr/bin/ld: arch/x86/um/sys_call_table_32.o:(.rodata+0x764): undefined reference to `sys_mq_timedreceive2'

clang: error: linker command failed with exit code 1 (use -v to see invocation)
--
In file included from kernel/sched/rq-offsets.c:5:
In file included from kernel/sched/sched.h:64:
In file included from include/linux/syscalls_api.h:1:

>> include/linux/syscalls.h:750:42: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]

750 | sys_mq_timedreceive2(mqd_t mqdes, struct mq_timedreceive2_args __user *uargs,
| ^
include/linux/syscalls.h:755:15: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]
755 | struct mq_timedreceive2_args __user *uargs,
| ^
2 warnings generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

PATCH 1/2 mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

POSIX message queues currently lack a mechanism to read a message without removing it from the queue. This is a long-standing limitation,when we require inspection of queue state without altering it.

Modifying existing mq_receive() semantics via additional flags was considered. However, altering behavior of an existing syscall risks breaking backward compatibility for applications relying on current semantics. Since mq_receive() guarantees message removal, changing this contract is not safe.

To preserve ABI stability, this patch introduces a new system call that performs a non-destructive receive operation (peek). The existing behavior remains unchanged.

Design considerations:

Two approaches for copying message data to userspace were evaluated:

1) Refcount-based message lifecycle handling
- This can help us Avoids intermediate temp kernel copy
- Extends message lifetime
-But this may increase writer starvation under heavy load and
add unneassery complication on priority management and delay more time to free space in inode due refcount may prevent

2) Temporary kernel buffer copy
- Copies message into a bounded kernel buffer
- Reduces time message remains locked
- Improves fairness under write-heavy workloads
- Simpler lifetime management

My implementation adopts the temporary buffer approach to minimize starvation and reduce locking complexity. The design allows future transition if refcounting is deemed preferable.

Architecture support: Entry was made in relevant system call table
- x86
- ARM

Testing:
- 15+ functional test cases
- Multi-threaded producer/consumer scenarios
- concurrent pop and peek
- Edge cases: empty queue, FIFO invalid flags, signal interruption etc.

---
 arch/x86/entry/syscalls/syscall_32.tbl |   2 +
 arch/x86/entry/syscalls/syscall_64.tbl |   2 +
 include/linux/syscalls.h               |   9 +
 include/uapi/asm-generic/unistd.h      |   5 +-
 ipc/mqueue.c                           | 180 ++++++
 ipc/msg.c                              |   2 +-
 ipc/msgutil.c                          |  51 +-
 ipc/util.h                             |   3 +-
 tools/testing/selftests/ipc/.gitignore |   1 +
 tools/testing/selftests/ipc/Makefile   |   5 +-
 tools/testing/selftests/ipc/mq_peek.c  | 794 +++++++++++++++++++++++++
 11 files changed, 1022 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/ipc/mq_peek.c
arch/x86/entry/syscalls/syscall_32.tbl +2 -0
index f832ebd2d79b..d6701f763203 100644
@@ -477,3 +477,5 @@
 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_time32
+473	i386	mq_timedreceive2_time64	sys_mq_timedreceive2
arch/x86/entry/syscalls/syscall_64.tbl +2 -0
index 524155d655da..539654953280 100644
@@ -441,3 +441,5 @@
 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.
+# Adding new syscalls from here to keep marginal gap intact in 472 to 511 for future expansion
+548	common	mq_timedreceive2		sys_mq_timedreceive2
include/linux/syscalls.h +9 -0
index 02bd6ddb6278..4db405159a8b 100644
@@ -746,6 +746,15 @@ 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_mq_timedreceive2_time32(mqd_t mqdes,
+			    struct mq_timedreceive2_args __user *uargs,
+			    unsigned int flags, unsigned long index,
+			    struct old_timespec32 __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 +4 -1
index a627acc8fb5f..1d27fd3e118e 100644
@@ -865,7 +865,10 @@ __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield)
 
 #undef __NR_syscalls
 #define __NR_syscalls 472
-
+#define __NR_mq_timedreceive2 473
+__SC_3264(__NR_mq_timedreceive2, sys_mq_timedreceive2_time32, sys_mq_timedreceive2)
+#define __NR_mq_timedreceive2_time64 474
+__SYSCALL(__NR_mq_timedreceive2_time64, sys_mq_timedreceive2)
 /*
  * 32 bit systems traditionally used different
  * syscalls for off_t and loff_t arguments, while
ipc/mqueue.c +180 -0

228 lines not on this page · raw message

ipc/msg.c +1 -1
index 62996b97f0ac..6392b11dd7f7 100644
@@ -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 +23 -28

69 lines not on this page · raw message

ipc/util.h +2 -1
index a55d6cebe6d3..374abeee79b3 100644
@@ -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)
 {
tools/testing/selftests/ipc/.gitignore +1 -0
index 9ed280e4c704..fe609d98ecb2 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 msgque_test
 msgque
+mq_peek
tools/testing/selftests/ipc/Makefile +4 -1
index 50e9c299fc4a..fa9d4f500783 100644
@@ -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__
@@ -12,7 +14,8 @@ 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 +794 -0

794 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 2/2 mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

Following Two warning issue by build and test on V1:

1) include/linux/syscalls.h:750:42: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]
fix-included the missing header for global visibility

2) warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]

fix- This seem false postive warning, script not able to correctly map syscall table
with handler
in file - ~/linux/arch/x86/entry/syscalls/syscall_32.tbl
473 causing warning due to now explicit handler for name "mq_timedrecive2_time64",because it also handle by same normal SYS_DEFINE for 64 bit arch because logically both are same if 32bit machine call 64 bit, if we add named defined then it create unnessary
redudancy and duplicacy, so even at this time i am not changing this, if required kindlly
once test in runtime and guide me.

472 i386 mq_timedreceive2 sys_mq_timedreceive2_time32
473 i386 mq_timedreceive2_time64 sys_mq_timedreceive2

---
 include/linux/syscalls.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/syscalls.h +1 -0
index 4db405159a8b..307cfd6f154c 100644
@@ -93,6 +93,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
 /*
Signature · 1 line
2.43.0
#6 lore raw

PATCH 1/2 mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

In reply to kernel test robot

POSIX message queues currently lack a mechanism to read a message without removing it from the queue. This is a long-standing limitation,when we require inspection of queue state without altering it.

Modifying existing mq_receive() semantics via additional flags was considered. However, altering behavior of an existing syscall risks breaking backward compatibility for applications relying on current semantics. Since mq_receive() guarantees message removal, changing this contract is not safe.

To preserve ABI stability, this patch introduces a new system call that performs a non-destructive receive operation (peek). The existing behavior remains unchanged.

Design considerations:

Two approaches for copying message data to userspace were evaluated:

1) Refcount-based message lifecycle handling
- This can help us Avoids intermediate temp kernel copy
- Extends message lifetime
-But this may increase writer starvation under heavy load and
add unneassery complication on priority management and delay more time to free space in inode due refcount may prevent

2) Temporary kernel buffer copy
- Copies message into a bounded kernel buffer
- Reduces time message remains locked
- Improves fairness under write-heavy workloads
- Simpler lifetime management

My implementation adopts the temporary buffer approach to minimize starvation and reduce locking complexity. The design allows future transition if refcounting is deemed preferable.

Architecture support: Entry was made in relevant system call table
- x86
- ARM

Testing:
- 15+ functional test cases
- Multi-threaded producer/consumer scenarios
- concurrent pop and peek
- Edge cases: empty queue, FIFO invalid flags, signal interruption etc.

---
 arch/x86/entry/syscalls/syscall_32.tbl |   2 +
 arch/x86/entry/syscalls/syscall_64.tbl |   2 +
 include/linux/syscalls.h               |   9 +
 include/uapi/asm-generic/unistd.h      |   5 +-
 ipc/mqueue.c                           | 180 ++++++
 ipc/msg.c                              |   2 +-
 ipc/msgutil.c                          |  51 +-
 ipc/util.h                             |   3 +-
 tools/testing/selftests/ipc/.gitignore |   1 +
 tools/testing/selftests/ipc/Makefile   |   5 +-
 tools/testing/selftests/ipc/mq_peek.c  | 794 +++++++++++++++++++++++++
 11 files changed, 1022 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/ipc/mq_peek.c
arch/x86/entry/syscalls/syscall_32.tbl +2 -0
index f832ebd2d79b..d6701f763203 100644
@@ -477,3 +477,5 @@
 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_time32
+473	i386	mq_timedreceive2_time64	sys_mq_timedreceive2
arch/x86/entry/syscalls/syscall_64.tbl +2 -0
index 524155d655da..539654953280 100644
@@ -441,3 +441,5 @@
 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.
+# Adding new syscalls from here to keep marginal gap intact in 472 to 511 for future expansion
+548	common	mq_timedreceive2		sys_mq_timedreceive2
include/linux/syscalls.h +9 -0
index 02bd6ddb6278..4db405159a8b 100644
@@ -746,6 +746,15 @@ 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_mq_timedreceive2_time32(mqd_t mqdes,
+			    struct mq_timedreceive2_args __user *uargs,
+			    unsigned int flags, unsigned long index,
+			    struct old_timespec32 __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 +4 -1
index a627acc8fb5f..1d27fd3e118e 100644
@@ -865,7 +865,10 @@ __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield)
 
 #undef __NR_syscalls
 #define __NR_syscalls 472
-
+#define __NR_mq_timedreceive2 473
+__SC_3264(__NR_mq_timedreceive2, sys_mq_timedreceive2_time32, sys_mq_timedreceive2)
+#define __NR_mq_timedreceive2_time64 474
+__SYSCALL(__NR_mq_timedreceive2_time64, sys_mq_timedreceive2)
 /*
  * 32 bit systems traditionally used different
  * syscalls for off_t and loff_t arguments, while
ipc/mqueue.c +180 -0

228 lines not on this page · raw message

ipc/msg.c +1 -1
index 62996b97f0ac..6392b11dd7f7 100644
@@ -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 +23 -28

69 lines not on this page · raw message

ipc/util.h +2 -1
index a55d6cebe6d3..374abeee79b3 100644
@@ -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)
 {
tools/testing/selftests/ipc/.gitignore +1 -0
index 9ed280e4c704..fe609d98ecb2 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 msgque_test
 msgque
+mq_peek
tools/testing/selftests/ipc/Makefile +4 -1
index 50e9c299fc4a..fa9d4f500783 100644
@@ -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__
@@ -12,7 +14,8 @@ 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 +794 -0

794 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 2/2 mqueue: introduce new do_mq_timedreceive2() [ mq_peek syscall] for non-destructive receive and inspection

Following Two warning issue by build and test on V1: Ignore Patch V2 that has small typo
in line 18 (corrected is warning due to it not has explicit handler)

1) include/linux/syscalls.h:750:42: warning: declaration of 'struct mq_timedreceive2_args' will not be visible outside of this function [-Wvisibility]
fix-included the missing header for global visibility

2) warning: syscall mq_timedreceive2_time64 not implemented [-W#warnings]

fix- This seem false postive warning, script not able to correctly map syscall table
with handler
in file - ~/linux/arch/x86/entry/syscalls/syscall_32.tbl
473 causing warning due to it not has explicit handler for name "mq_timedrecive2_time64",because it also handle by same normal SYS_DEFINE for 64 bit arch because logically both are same if 32bit machine call 64 bit, if we add named defined then it create unnessary
redudancy and duplicacy, so even at this time i am not changing this, if required kindlly
once test in runtime and guide me.

472 i386 mq_timedreceive2 sys_mq_timedreceive2_time32
473 i386 mq_timedreceive2_time64 sys_mq_timedreceive2

---
 include/linux/syscalls.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/syscalls.h +1 -0
index 4db405159a8b..307cfd6f154c 100644
@@ -93,6 +93,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
 /*
Signature · 1 line
2.43.0