Documentation: syscalls: add a note about ABI-agnostic types
PATCH 1/1 Documentation: syscalls: add a note about ABI-agnostic types
Recently added memfd_secret() syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.
https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rst doesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.
--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)
Documentation/process/adding-syscalls.rst +7 -0
@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs; types like ``char`` and ``int`` +have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. + Proposing the API -----------------
Signature · 1 line
2.25.1
Ping?
Quoted 35 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +7
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs; types like ``char`` and ``int`` +have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. + Proposing the API -----------------Signature · 1 line
2.25.1
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:
Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;
In the case of pointers, the best is to use __u64. The pointer can then be read on Kernelspace with something like this:
static inline void __user *media_get_uptr(__u64 arg)
{
return (void __user *)(uintptr_t)arg;
}Quoted 4 lines
Documentation/process/adding-syscalls.rst arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit · line 181
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +
I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
For instance, I remember we had to replace "int" and "enum" by "__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeing x86_64 Kernels with 32-bits userspace and when cameras started being supported on arm32.
We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
Thanks,
Mauro
Quoted 54 lines · Mauro Carvalho Chehab
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;In the case of pointers, the best is to use
__u64. The pointer can then be read on Kernelspace with something like this:static inline void __user *media_get_uptr(__u64 arg) { return (void __user *)(uintptr_t)arg; }Quoted 4 lines
Documentation/process/adding-syscalls.rst
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
Quoted 8 lines
For instance, I remember we had to replace "int" and "enum" by "
__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeingx86_64Kernels with 32-bits userspace and when cameras started being supported on arm32.We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
Fwiw, Aleksa and I have written extended syscall documentation documenting the agreement that we came to in a dedicated session with a wide range of kernel folks during Linux Plumbers last year. We simply never had time to actually send this series but fwiw here it is. It also mentions the use of correct types. If people feel it's worth it I can send as a proper series:
From 9035258aaa23c5e1bb5bc2242f97221a3e5b9a87 Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Fri, 4 Sep 2020 14:27:47 +0200
Subject: [PATCH 1/6] docs: split extensibility section into two subsections
The section already explains two different formats that are available to extend a syscall. Move each into its own subsection. This clarifies the structure and will be useful when we extend each section in follow-up patches.
--- Documentation/process/adding-syscalls.rst | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/process/adding-syscalls.rst +6 -0
@@ -65,6 +65,9 @@ together with the corresponding follow-up system calls -- ``pipe``/``pipe2``, ``renameat``/``renameat2`` -- so learn from the history of the kernel and plan for extensions from the start.) +Baseline extensibility: adding a flag argument +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + For simpler system calls that only take a couple of arguments, the preferred way to allow for future extensibility is to include a flags argument to the system call. To make sure that userspace programs can safely use flags
@@ -76,6 +79,9 @@ flags, and reject the system call (with ``EINVAL``) if it does:: (If no flags values are used yet, check that the flags argument is zero.) +Advanced extensibility: extensible structs +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + For more sophisticated system calls that involve a larger number of arguments, it's preferred to encapsulate the majority of the arguments into a structure that is passed in by pointer. Such a structure can cope with future extension
base-commit: e49d033bddf5b565044e2abe4241353959bc9120
Signature · 18 lines
2.27.0 From de1b27a0d29ac8edfb19226e3ad0547831f12a97 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Fri, 4 Sep 2020 14:37:53 +0200 Subject: [PATCH 2/6] docs: require unsigned int as type for flag arguments This has been an undocumented requirement for quite a long time but people still get confused and add system calls that use e.g. unsigned long as flag argument. Document the unsigned int requirement for flag arguments. Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Co-developed-by: Aleksa Sarai <[email protected]> Signed-off-by: Christian Brauner <[email protected]> --- Documentation/process/adding-syscalls.rst | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
Documentation/process/adding-syscalls.rst +10 -3
@@ -70,9 +70,16 @@ Baseline extensibility: adding a flag argument For simpler system calls that only take a couple of arguments, the preferred way to allow for future extensibility is to include a flags argument to the -system call. To make sure that userspace programs can safely use flags -between kernel versions, check whether the flags value holds any unknown -flags, and reject the system call (with ``EINVAL``) if it does:: +system call. As such, flag arguments function as a baseline for extensibility. + +Different types such as ``int`` or ``unsigned long`` have been used for flag +arguments. Since this is not just inconsistent but can also lead to issues +with sign- and zero extension all new system calls are expected to use +``unsigned int`` as type for flag arguments. + +To make sure that userspace programs can safely use flags between kernel +versions, check whether the flags value holds any unknown flags, and reject the +system call (with ``EINVAL``) if it does:: if (flags & ~(THING_FLAG1 | THING_FLAG2 | THING_FLAG3)) return -EINVAL;
Signature · 14 lines
2.27.0 From 08bc20493242d93c187b8cdcf9c2353e385a2565 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Fri, 4 Sep 2020 14:52:56 +0200 Subject: [PATCH 3/6] docs: clarify flag value checking a little Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Co-developed-by: Aleksa Sarai <[email protected]> Signed-off-by: Christian Brauner <[email protected]> --- Documentation/process/adding-syscalls.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
Documentation/process/adding-syscalls.rst +9 -5
@@ -77,14 +77,18 @@ arguments. Since this is not just inconsistent but can also lead to issues with sign- and zero extension all new system calls are expected to use ``unsigned int`` as type for flag arguments. -To make sure that userspace programs can safely use flags between kernel -versions, check whether the flags value holds any unknown flags, and reject the -system call (with ``EINVAL``) if it does:: +A system call doesn't need to support any flags right away to justify adding +a flag argument. If no flags are supported yet, the new system call needs +to check that the flag argument is zero and to return ``EINVAL`` if it is not:: - if (flags & ~(THING_FLAG1 | THING_FLAG2 | THING_FLAG3)) + if (flags) return -EINVAL; -(If no flags values are used yet, check that the flags argument is zero.) +Similarly, if flags are supported the system call needs to check that no +unknown flag values are present and return ``EINVAL`` if there are:: + + if (flags & ~(THING_FLAG1 | THING_FLAG2 | THING_FLAG3)) + return -EINVAL; Advanced extensibility: extensible structs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signature · 14 lines
2.27.0 From 35745a05b7d7350f2039b7d868e52f6c306f7c8e Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Fri, 4 Sep 2020 15:39:38 +0200 Subject: [PATCH 4/6] docs: extend section about extensible structs Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Co-developed-by: Aleksa Sarai <[email protected]> Signed-off-by: Christian Brauner <[email protected]> --- Documentation/process/adding-syscalls.rst | 88 +++++++++++++++++------ 1 file changed, 68 insertions(+), 20 deletions(-)
Documentation/process/adding-syscalls.rst +68 -20
108 lines not on this page · raw message
Signature · 14 lines
2.27.0 From 92d48ab239e28f56d1d685ca1f1618aef08e7fe6 Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Fri, 4 Sep 2020 15:57:36 +0200 Subject: [PATCH 5/6] docs: document how to name revised syscalls Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Co-developed-by: Aleksa Sarai <[email protected]> Signed-off-by: Christian Brauner <[email protected]> --- Documentation/process/adding-syscalls.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
Documentation/process/adding-syscalls.rst +22 -0
@@ -168,6 +168,28 @@ flag-only system calls, the system call needs to verify any unknown values for flag-like fields in the passed struct are zeroed. +Designing the API: Revisions of syscalls +----------------------------------------------- + +System calls that were not designed to be extensible or system calls that use +a flag argument for extensions running out of bits (e.g. :manpage:`clone(2)`) +sometimes need to be replaced. + +If the revised system call provides a superset (or a reasonably large subset, +such as when a feature that turned out to be a design mistake is dropped) of +the features of the old system call, it is common practice to give it the same +name with a number appended. Examples for this include ``dup2``/``dup3``, +``epoll_create``/``epoll_create1`` and others. + +For some syscalls the appended number indicates the number of arguments +(``accept``/``accept4``) for others the number of the revision +(``clone``/``clone3``, ``epoll_create``/``epoll_create1``). New system calls +that are a revision of an earlier system call should treat the appended number +as the number of the revision. For example, if you were to add a revised +version of ``readlinkat`` with an additional flag argument it should be named +``readlinkat2``. + + Designing the API: Other Considerations ---------------------------------------
Signature · 14 lines
2.27.0 From 0f61fcb2949170532283048e8ac0d8e3c16305db Mon Sep 17 00:00:00 2001 From: Christian Brauner <[email protected]> Date: Fri, 4 Sep 2020 17:06:05 +0200 Subject: [PATCH 6/6] docs: add section about multiplexers Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Co-developed-by: Aleksa Sarai <[email protected]> Signed-off-by: Christian Brauner <[email protected]> --- Documentation/process/adding-syscalls.rst | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+)
Documentation/process/adding-syscalls.rst +39 -0
@@ -51,6 +51,45 @@ interface. getting/setting a simple flag related to a process. +Designing the API: No more multiplexers +--------------------------------------- + +New system calls are not allowed to be multiplexers. The kernel strongly +encourages simple system call designs. + +The kernel has historically supported multiplexing system calls but both the +kernel and userspace have learned the hard way that this is usually +a significant design mistake. The list of actively supported multiplexing +system calls ranges from domain specific multiplexers such as +:manpage:`seccomp(2)` or :manpage:`keyctl(2)` to more generic multiplexers such +as :manpage:`prctl(2)` and :manpage:`ptrace(2)`, up to ``ioctl``, a generic +multiplexer not bound to any specific API or functionality. In essence, +a multiplexing system call often implements the functionality of multiple +simple system calls. + +Multiplexers are especially problematic when they are type-polymorph. This +includes all multiplexers that pass different types through a void pointer, +a ``long``, or a union using a specific argument to differentiate the type or +the member of the union to look at. The ``ioctl`` multiplexer is a prime +example of a type-polymorph multiplexer. Depending on the second argument +passed to ``ioctl`` different types can be passed as the third argument. Some +multiplexers, including ``ioctl``, :manpage:`prctl(2)`, or :manpage:`fcntl(2)` +even pass a different number of arguments depending on the command that is +executed. + +Multiplexers also pose significant problems for userspace libraries: + + - Type safety is lost for the most part. + - There are no real advantages in terms of useability. In fact, userspace + libraries do consider exposing the commands implemented by a system call as + separate library calls. + - Multiplexers provide problems for 32 bit systems on 64 bit kernels. They + can for example cause breakage with ILP32 (i.e. I-ntegers, L-ongs, and + P-ointers are 32 bit wide) targets when types are not promoted correctly for + use with the kernel/userspace ABI. Getting this wrong is easier than + getting it right. + + Designing the API: Planning for Extension -----------------------------------------
Signature · 1 line
2.27.0
Quoted 64 lines · Christian Brauner
Quoted 54 lines · Mauro Carvalho Chehab
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;In the case of pointers, the best is to use
__u64. The pointer can then be read on Kernelspace with something like this:static inline void __user *media_get_uptr(__u64 arg) { return (void __user *)(uintptr_t)arg; }Quoted 4 lines
Documentation/process/adding-syscalls.rst
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
Quoted 8 lines
For instance, I remember we had to replace "int" and "enum" by "
__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeingx86_64Kernels with 32-bits userspace and when cameras started being supported on arm32.We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
Quoted 6 lines
Fwiw, Aleksa and I have written extended syscall documentation documenting the agreement that we came to in a dedicated session with a wide range of kernel folks during Linux Plumbers last year. We simply never had time to actually send this series but fwiw here it is. It also mentions the use of correct types. If people feel it's worth it I can send as a proper series:
Yes, please.
Quoted 4 lines
From
9035258aaa23c5e1bb5bc2242f97221a3e5b9a87Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Fri, 4 Sep 2020 14:27:47 +0200
Subject: [PATCH 1/6] docs: split extensibility section into two subsections
...
Signature · 2 lines
Sincerely yours, Mike.
Quoted 65 lines · Mike Rapoport
Quoted 64 lines · Christian Brauner
Quoted 62 lines · Mauro Carvalho Chehab
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;In the case of pointers, the best is to use
__u64. The pointer can then be read on Kernelspace with something like this:static inline void __user *media_get_uptr(__u64 arg) { return (void __user *)(uintptr_t)arg; }Quoted 4 lines
Documentation/process/adding-syscalls.rst
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
For instance, I remember we had to replace "int" and "enum" by "
__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeingx86_64Kernels with 32-bits userspace and when cameras started being supported on arm32.We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
Quoted 8 lines
Quoted 6 lines
Fwiw, Aleksa and I have written extended syscall documentation documenting the agreement that we came to in a dedicated session with a wide range of kernel folks during Linux Plumbers last year. We simply never had time to actually send this series but fwiw here it is. It also mentions the use of correct types. If people feel it's worth it I can send as a proper series:
Yes, please.
Ok, I'll try to fix the commit messages and send it out.
Christian
Quoted 73 lines · Christian Brauner
Quoted 72 lines · Mike Rapoport
Quoted 70 lines · Christian Brauner
Quoted 54 lines · Mauro Carvalho Chehab
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;In the case of pointers, the best is to use
__u64. The pointer can then be read on Kernelspace with something like this:static inline void __user *media_get_uptr(__u64 arg) { return (void __user *)(uintptr_t)arg; }Quoted 4 lines
Documentation/process/adding-syscalls.rst
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
Quoted 8 lines
For instance, I remember we had to replace "int" and "enum" by "
__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeingx86_64Kernels with 32-bits userspace and when cameras started being supported on arm32.We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
Fwiw, Aleksa and I have written extended syscall documentation documenting the agreement that we came to in a dedicated session with a wide range of kernel folks during Linux Plumbers last year. We simply never had time to actually send this series but fwiw here it is. It also mentions the use of correct types. If people feel it's worth it I can send as a proper series:
Quoted 5 lines
Quoted 1 line
Yes, please.
Ok, I'll try to fix the commit messages and send it out.
Christian
Could you pelease keep me in CC in your submission?
Quoted 36 lines · Mauro Carvalho Chehab
Em Tue, 13 Apr 2021 21:40:20 -0700
Yury Norov <[email protected]> escreveu:Quoted 32 lines
Ping?
Quoted 29 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +5
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. + The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs;
Quoted 7 lines
In the case of pointers, the best is to use
__u64. The pointer can then be read on Kernelspace with something like this:static inline void __user *media_get_uptr(__u64 arg) { return (void __user *)(uintptr_t)arg; }
For 32-bit userspace reserving 64-bit type for pointers looks excessive, isn't? And anyways, how could this help to prevent malicious/broken compat userspace from passing pointers with dirty top 32 bits?
From what I can see, in case of compat ABI, the 'void *' args are cast to compat_uptr_t in the compat layer, and then passed to native handlers. Bypassing compat layer in the example above would break consistency for a syscall.
Quoted 8 lines
Quoted 4 lines
Documentation/process/adding-syscalls.rst arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit · line 181
Quoted 4 lines
types like ``char`` and ``int``
Documentation/process/adding-syscalls.rst +3
-0+have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. +I don't think this is true for all compilers on userspace, as the C standard doesn't define how many bits an int/unsigned int has. So, even if this is today's reality, things may change in the future.
Agree, it's not a standard in C, but this is pretty much a standard in Linux. Introducing a new ABI nor ILP32, neither LP64 would require huge amount of work, especially on a maintenance level, and I bet it will be blocked by Arnd. :) In practice it's correct to recommend using unsigned int for flags now, and if in future someone will introduce new ABI, it will be his responsibility to explain us how to design syscalls in a compatible and unified way.
Quoted 8 lines
For instance, I remember we had to replace "int" and "enum" by "
__u32" and "long" by "__u64" at the media uAPI in the past, when we start seeingx86_64Kernels with 32-bits userspace and when cameras started being supported on arm32.We did have some real bugs with "enum", as, on that time, some compilers (gcc, I guess) were optimizing them to have less than 32 bits on certain architectures, when it fits.
I think this example agrees with what I said - if userspace has nonstandard ABI, it has to use kernel types to communicate with kernel, which are exposed as __u32-style typedefs. For me, it's a compatibility layer implemented in userspace.
This patch is about good practices for standard 32, 64 and compat ABIs supported by kernel.
(Or if I missed you point, can you please explain in more details?)
Thanks,
Yury
Quoted 35 lines · Yury Norov
Recently added
memfd_secret()syscall had a flags parameter passed as unsigned long, which requires creation of compat entry for it. It was possible to change the type of flags to unsigned int and so avoid bothering with compat layer.https://www.spinics.net/lists/linux-mm/msg251550.html
Documentation/process/adding-syscalls.rstdoesn't point clearly about preference of ABI-agnostic types. This patch adds such notification.Signed-off-by Yury Norov <[email protected]>--- Documentation/process/adding-syscalls.rst | 7 +++++++ 1 file changed, 7 insertions(+)Documentation/process/adding-syscalls.rst +7
-0@@ -172,6 +172,13 @@ arguments (i.e. parameter 1, 3, 5), to allow use of contiguous pairs of 32-bit registers. (This concern does not apply if the arguments are part of a structure that's passed in by pointer.) +Whenever possible, try to use ABI-agnostic types for passing parameters to +a syscall in order to avoid creating compat entry for it. Linux supports two +ABI models - ILP32 and LP64. The types like ``void *``, ``long``, ``size_t``, +``off_t`` have different size in those ABIs; types like ``char`` and ``int`` +have the same size and don't require a compat layer support. For flags, it's +always better to use ``unsigned int``. + Proposing the API -----------------Signature · 1 line
2.25.1
So, what about this patch? Mauro, are you OK with this? If no objections, I'd like to have it merged in this window.