linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* New syscall: leftpad()
@ 2016-03-31 22:33 Richard Weinberger
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Richard Weinberger @ 2016-03-31 22:33 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-api

Recent happenings in the node.js community showed how fragile software is when
it comes to dependencies of fundamental algorithms like leftpad[1].
A node.js package which provided ledpad vanished and broke a lot of software.
This raised our attention and we came to the conclusion that it is the kernel's
job to provide such functionality such that node.js based applications can in future
rely in Linux's "don't break userspace" rule.
We hope that glibc and Andoid's bionic will soon offer wrapper functions for this
new leftpad system call.
We put leftpad into the kernel not only because of Linux's stable ABI,
also for performance reasons.
As everyone knows, within the kernel everything is faster and better.
Leftpad has millions of users, so it has to be as fast as possible.
This new system call will also help making services like left-pad.io[2]
faster and more reliable. If the leftpad() system call gets adopted by a wider user base
it might also make sense to add a generic npm() system call which acts like ioctl()
where kernel modules can register new functions that are often used by node.js.
Such functions might be, is_array(), is_int(), etc.

Enjoy,
//richard

[1] http://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/
[2] http://left-pad.io/

[PATCH] Implement leftpad syscall
[PATCH] leftpad.2: Document new syscall

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] Implement leftpad syscall
  2016-03-31 22:33 New syscall: leftpad() Richard Weinberger
@ 2016-03-31 22:33 ` Richard Weinberger
  2016-03-31 22:46   ` Michael Kerrisk (man-pages)
                     ` (5 more replies)
  2016-03-31 22:33 ` [PATCH] leftpad.2: Document new syscall Richard Weinberger
  2016-03-31 23:36 ` New syscall: leftpad() Randy Dunlap
  2 siblings, 6 replies; 13+ messages in thread
From: Richard Weinberger @ 2016-03-31 22:33 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-api, David Gstir, Richard Weinberger

From: David Gstir <david@sigma-star.at>

Implement the leftpad() system call such that userspace,
especially node.js applications, can in the near future directly
use it and no longer depend on fragile npm packages.

Signed-off-by: David Gstir <david@sigma-star.at>
Signed-off-by: Richard Weinberger <richard@nod.at>
---
 arch/x86/entry/syscalls/syscall_64.tbl |  1 +
 include/linux/syscalls.h               |  1 +
 kernel/sys.c                           | 35 ++++++++++++++++++++++++++++++++++
 kernel/sys_ni.c                        |  1 +
 4 files changed, 38 insertions(+)

diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index cac6d17..f287712 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -335,6 +335,7 @@
 326	common	copy_file_range		sys_copy_file_range
 327	64	preadv2			sys_preadv2
 328	64	pwritev2		sys_pwritev2
+329	common	leftpad			sys_leftpad
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index d795472..a0850bb 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -898,4 +898,5 @@ asmlinkage long sys_copy_file_range(int fd_in, loff_t __user *off_in,
 
 asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
 
+asmlinkage long sys_leftpad(char *str, char pad, char *dst, size_t dst_len);
 #endif
diff --git a/kernel/sys.c b/kernel/sys.c
index cf8ba54..e42d972 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2432,3 +2432,38 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
 	return 0;
 }
 #endif /* CONFIG_COMPAT */
+
+
+SYSCALL_DEFINE4(leftpad, char *, src, char, pad, char *, dst, size_t, dst_len)
+{
+	char *buf;
+	long ret;
+	size_t len = strlen_user(src);
+	size_t pad_len = dst_len - len;
+
+	if (dst_len <= len || dst_len > 4096) {
+		return -EINVAL;
+	}
+
+	buf = kmalloc(dst_len, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, pad, pad_len);
+	ret = copy_from_user(buf + pad_len, src, len);
+	if (ret) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	ret = copy_to_user(dst, buf, dst_len);
+	if (ret) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	ret = pad_len;
+out:
+	kfree(buf);
+	return ret;
+}
diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
index 2c5e3a8..262608d 100644
--- a/kernel/sys_ni.c
+++ b/kernel/sys_ni.c
@@ -175,6 +175,7 @@ cond_syscall(sys_setfsgid);
 cond_syscall(sys_capget);
 cond_syscall(sys_capset);
 cond_syscall(sys_copy_file_range);
+cond_syscall(sys_leftpad);
 
 /* arch-specific weak syscall entries */
 cond_syscall(sys_pciconfig_read);
-- 
1.8.4.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH] leftpad.2: Document new syscall
  2016-03-31 22:33 New syscall: leftpad() Richard Weinberger
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
@ 2016-03-31 22:33 ` Richard Weinberger
  2016-04-08 18:10   ` Heinrich Schuchardt
  2016-03-31 23:36 ` New syscall: leftpad() Randy Dunlap
  2 siblings, 1 reply; 13+ messages in thread
From: Richard Weinberger @ 2016-03-31 22:33 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-api, Daniel Walter, Richard Weinberger

From: Daniel Walter <dwalter@sigma-star.at>

Signed-off-by: Daniel Walter <dwalter@sigma-star.at>
Signed-off-by: Richard Weinberger <richard@nod.at>
---
 man2/leftpad.2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 man2/leftpad.2

diff --git a/man2/leftpad.2 b/man2/leftpad.2
new file mode 100644
index 0000000..ff5f401
--- /dev/null
+++ b/man2/leftpad.2
@@ -0,0 +1,55 @@
+.\" Copyright (c) 2016 sigma-star gmbh
+.\" (office@sigma-star.at)
+.\"
+.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
+.\" This is free documentation; you can redistribute it and/or
+.\" modify it under the terms of the GNU General Public License as
+.\" published by the Free Software Foundation; either version 2 of
+.\" the License, or (at your option) any later version.
+.\"
+.\" The GNU General Public License's references to "object code"
+.\" and "executables" are to be interpreted as the output of any
+.\" document formatting or typesetting system, including
+.\" intermediate and printed output.
+.\"
+.\" This manual is distributed in the hope that it will be useful,
+.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+.\" GNU General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public
+.\" License along with this manual; if not, see
+.\" <http://www.gnu.org/licenses/>.
+.\" %%%LICENSE_END
+.\"
+.TH LEFTPAD 2 2016-04-01 "Linux" "Linux Programmer's Manual"
+.SH NAME
+leftpad
+.SH SYNOPSIS
+.nf
+.B #include <sys/types.h>
+
+.BI "int leftpad(char *src, char pad, char *dst, size_t dst_len);
+.fi
+.SH DESCRIPTION
+This function provides left padding for strings.
+.LP
+The string in
+.I src
+will be left padded with the chosen padding character
+.I pad
+and stored in
+.I dst
+ .
+.SH RETURN VALUE
+On success, returns the number of padding characters added.
+.SH ERRORS
+.TP
+.B EINVAL
+The size of the destination buffer
+.I dst
+is shorter than the source string
+.I src
+ . Or the destination string is longer than 4096 bytes.
+.SH SEE ALSO
+.BR snprintf (3)
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
@ 2016-03-31 22:46   ` Michael Kerrisk (man-pages)
  2016-03-31 23:09   ` Greg KH
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Michael Kerrisk (man-pages) @ 2016-03-31 22:46 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel; +Cc: mtk.manpages, linux-api, David Gstir

On 04/01/2016 11:33 AM, Richard Weinberger wrote:
> From: David Gstir <david@sigma-star.at>
> 
> Implement the leftpad() system call such that userspace,
> especially node.js applications, can in the near future directly
> use it and no longer depend on fragile npm packages.

Works can't express the importance of adding this system call!
Thanks so much for proposing and implementing it!

Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>

Cheers,

Michael

> Signed-off-by: David Gstir <david@sigma-star.at>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  arch/x86/entry/syscalls/syscall_64.tbl |  1 +
>  include/linux/syscalls.h               |  1 +
>  kernel/sys.c                           | 35 ++++++++++++++++++++++++++++++++++
>  kernel/sys_ni.c                        |  1 +
>  4 files changed, 38 insertions(+)
> 
> diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
> index cac6d17..f287712 100644
> --- a/arch/x86/entry/syscalls/syscall_64.tbl
> +++ b/arch/x86/entry/syscalls/syscall_64.tbl
> @@ -335,6 +335,7 @@
>  326	common	copy_file_range		sys_copy_file_range
>  327	64	preadv2			sys_preadv2
>  328	64	pwritev2		sys_pwritev2
> +329	common	leftpad			sys_leftpad
>  
>  #
>  # x32-specific system call numbers start at 512 to avoid cache impact
> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
> index d795472..a0850bb 100644
> --- a/include/linux/syscalls.h
> +++ b/include/linux/syscalls.h
> @@ -898,4 +898,5 @@ asmlinkage long sys_copy_file_range(int fd_in, loff_t __user *off_in,
>  
>  asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
>  
> +asmlinkage long sys_leftpad(char *str, char pad, char *dst, size_t dst_len);
>  #endif
> diff --git a/kernel/sys.c b/kernel/sys.c
> index cf8ba54..e42d972 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -2432,3 +2432,38 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
>  	return 0;
>  }
>  #endif /* CONFIG_COMPAT */
> +
> +
> +SYSCALL_DEFINE4(leftpad, char *, src, char, pad, char *, dst, size_t, dst_len)
> +{
> +	char *buf;
> +	long ret;
> +	size_t len = strlen_user(src);
> +	size_t pad_len = dst_len - len;
> +
> +	if (dst_len <= len || dst_len > 4096) {
> +		return -EINVAL;
> +	}
> +
> +	buf = kmalloc(dst_len, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	memset(buf, pad, pad_len);
> +	ret = copy_from_user(buf + pad_len, src, len);
> +	if (ret) {
> +		ret = -EFAULT;
> +		goto out;
> +	}
> +
> +	ret = copy_to_user(dst, buf, dst_len);
> +	if (ret) {
> +		ret = -EFAULT;
> +		goto out;
> +	}
> +
> +	ret = pad_len;
> +out:
> +	kfree(buf);
> +	return ret;
> +}
> diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
> index 2c5e3a8..262608d 100644
> --- a/kernel/sys_ni.c
> +++ b/kernel/sys_ni.c
> @@ -175,6 +175,7 @@ cond_syscall(sys_setfsgid);
>  cond_syscall(sys_capget);
>  cond_syscall(sys_capset);
>  cond_syscall(sys_copy_file_range);
> +cond_syscall(sys_leftpad);
>  
>  /* arch-specific weak syscall entries */
>  cond_syscall(sys_pciconfig_read);
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
  2016-03-31 22:46   ` Michael Kerrisk (man-pages)
@ 2016-03-31 23:09   ` Greg KH
  2016-04-01  1:33   ` Frederic Weisbecker
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Greg KH @ 2016-03-31 23:09 UTC (permalink / raw)
  To: Richard Weinberger; +Cc: linux-kernel, linux-api, David Gstir

On Fri, Apr 01, 2016 at 12:33:32AM +0200, Richard Weinberger wrote:
> +	if (dst_len <= len || dst_len > 4096) {
> +		return -EINVAL;
> +	}

Please always use checkpatch.pl when submitting patches.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: New syscall: leftpad()
  2016-03-31 22:33 New syscall: leftpad() Richard Weinberger
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
  2016-03-31 22:33 ` [PATCH] leftpad.2: Document new syscall Richard Weinberger
@ 2016-03-31 23:36 ` Randy Dunlap
  2016-04-01  8:06   ` Richard Weinberger
  2 siblings, 1 reply; 13+ messages in thread
From: Randy Dunlap @ 2016-03-31 23:36 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel; +Cc: linux-api

Please be more careful in your description...

On 03/31/16 15:33, Richard Weinberger wrote:
> Recent happenings in the node.js community showed how fragile software is when
> it comes to dependencies of fundamental algorithms like leftpad[1].
> A node.js package which provided ledpad vanished and broke a lot of software.

                                   leftpad

> This raised our attention and we came to the conclusion that it is the kernel's
> job to provide such functionality such that node.js based applications can in future
> rely in Linux's "don't break userspace" rule.
> We hope that glibc and Andoid's bionic will soon offer wrapper functions for this

                         Android's

> new leftpad system call.
> We put leftpad into the kernel not only because of Linux's stable ABI,
> also for performance reasons.
> As everyone knows, within the kernel everything is faster and better.
> Leftpad has millions of users, so it has to be as fast as possible.
> This new system call will also help making services like left-pad.io[2]
> faster and more reliable. If the leftpad() system call gets adopted by a wider user base
> it might also make sense to add a generic npm() system call which acts like ioctl()
> where kernel modules can register new functions that are often used by node.js.
> Such functions might be, is_array(), is_int(), etc.


-- 
~Randy

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
  2016-03-31 22:46   ` Michael Kerrisk (man-pages)
  2016-03-31 23:09   ` Greg KH
@ 2016-04-01  1:33   ` Frederic Weisbecker
  2016-04-01  3:22   ` kbuild test robot
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Frederic Weisbecker @ 2016-04-01  1:33 UTC (permalink / raw)
  To: Richard Weinberger; +Cc: linux-kernel, linux-api, David Gstir

On Fri, Apr 01, 2016 at 12:33:32AM +0200, Richard Weinberger wrote:
> From: David Gstir <david@sigma-star.at>
> 
> Implement the leftpad() system call such that userspace,
> especially node.js applications, can in the near future directly
> use it and no longer depend on fragile npm packages.
> 
> Signed-off-by: David Gstir <david@sigma-star.at>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  arch/x86/entry/syscalls/syscall_64.tbl |  1 +
>  include/linux/syscalls.h               |  1 +
>  kernel/sys.c                           | 35 ++++++++++++++++++++++++++++++++++
>  kernel/sys_ni.c                        |  1 +
>  4 files changed, 38 insertions(+)
> 
> diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
> index cac6d17..f287712 100644
> --- a/arch/x86/entry/syscalls/syscall_64.tbl
> +++ b/arch/x86/entry/syscalls/syscall_64.tbl
> @@ -335,6 +335,7 @@
>  326	common	copy_file_range		sys_copy_file_range
>  327	64	preadv2			sys_preadv2
>  328	64	pwritev2		sys_pwritev2
> +329	common	leftpad			sys_leftpad
>  
>  #
>  # x32-specific system call numbers start at 512 to avoid cache impact
> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
> index d795472..a0850bb 100644
> --- a/include/linux/syscalls.h
> +++ b/include/linux/syscalls.h
> @@ -898,4 +898,5 @@ asmlinkage long sys_copy_file_range(int fd_in, loff_t __user *off_in,
>  
>  asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
>  
> +asmlinkage long sys_leftpad(char *str, char pad, char *dst, size_t dst_len);
>  #endif
> diff --git a/kernel/sys.c b/kernel/sys.c
> index cf8ba54..e42d972 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -2432,3 +2432,38 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
>  	return 0;
>  }
>  #endif /* CONFIG_COMPAT */
> +
> +
> +SYSCALL_DEFINE4(leftpad, char *, src, char, pad, char *, dst, size_t, dst_len)

Have you considered /dev/leftpad instead?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
                     ` (2 preceding siblings ...)
  2016-04-01  1:33   ` Frederic Weisbecker
@ 2016-04-01  3:22   ` kbuild test robot
  2016-04-01  6:56   ` Richard Cochran
  2016-04-01  7:14   ` Scotty Bauer
  5 siblings, 0 replies; 13+ messages in thread
From: kbuild test robot @ 2016-04-01  3:22 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: kbuild-all, linux-kernel, linux-api, David Gstir, Richard Weinberger

[-- Attachment #1: Type: text/plain, Size: 1416 bytes --]

Hi David,

[auto build test ERROR on v4.6-rc1]
[also build test ERROR on next-20160331]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Richard-Weinberger/Implement-leftpad-syscall/20160401-063620
config: microblaze-mmu_defconfig (attached as .config)
reproduce:
        wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=microblaze 

All errors (new ones prefixed by >>):

   kernel/sys.c: In function 'SYSC_leftpad':
>> kernel/sys.c:2441:2: error: implicit declaration of function 'strlen_user' [-Werror=implicit-function-declaration]
     size_t len = strlen_user(src);
     ^
   cc1: some warnings being treated as errors

vim +/strlen_user +2441 kernel/sys.c

  2435	
  2436	
  2437	SYSCALL_DEFINE4(leftpad, char *, src, char, pad, char *, dst, size_t, dst_len)
  2438	{
  2439		char *buf;
  2440		long ret;
> 2441		size_t len = strlen_user(src);
  2442		size_t pad_len = dst_len - len;
  2443	
  2444		if (dst_len <= len || dst_len > 4096) {

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 12144 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
                     ` (3 preceding siblings ...)
  2016-04-01  3:22   ` kbuild test robot
@ 2016-04-01  6:56   ` Richard Cochran
  2016-04-01  7:14   ` Scotty Bauer
  5 siblings, 0 replies; 13+ messages in thread
From: Richard Cochran @ 2016-04-01  6:56 UTC (permalink / raw)
  To: Richard Weinberger; +Cc: linux-kernel, linux-api, David Gstir

On Fri, Apr 01, 2016 at 12:33:32AM +0200, Richard Weinberger wrote:
> From: David Gstir <david@sigma-star.at>
> 
> Implement the leftpad() system call such that userspace,
> especially node.js applications, can in the near future directly
> use it and no longer depend on fragile npm packages.
> 
> Signed-off-by: David Gstir <david@sigma-star.at>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  arch/x86/entry/syscalls/syscall_64.tbl |  1 +
>  include/linux/syscalls.h               |  1 +
>  kernel/sys.c                           | 35 ++++++++++++++++++++++++++++++++++
>  kernel/sys_ni.c                        |  1 +
>  4 files changed, 38 insertions(+)

This is okay as far as it goes, but you need to add the other archs
and put the relevant maintainers onto CC.

Thanks,
Richard

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Implement leftpad syscall
  2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
                     ` (4 preceding siblings ...)
  2016-04-01  6:56   ` Richard Cochran
@ 2016-04-01  7:14   ` Scotty Bauer
  5 siblings, 0 replies; 13+ messages in thread
From: Scotty Bauer @ 2016-04-01  7:14 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel; +Cc: linux-api, David Gstir



On 03/31/2016 04:33 PM, Richard Weinberger wrote:
> From: David Gstir <david@sigma-star.at>
> 
> Implement the leftpad() system call such that userspace,
> especially node.js applications, can in the near future directly
> use it and no longer depend on fragile npm packages.
> 
> Signed-off-by: David Gstir <david@sigma-star.at>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  arch/x86/entry/syscalls/syscall_64.tbl |  1 +
>  include/linux/syscalls.h               |  1 +
>  kernel/sys.c                           | 35 ++++++++++++++++++++++++++++++++++
>  kernel/sys_ni.c                        |  1 +
>  4 files changed, 38 insertions(+)
> 
> diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
> index cac6d17..f287712 100644
> --- a/arch/x86/entry/syscalls/syscall_64.tbl
> +++ b/arch/x86/entry/syscalls/syscall_64.tbl
> @@ -335,6 +335,7 @@
>  326	common	copy_file_range		sys_copy_file_range
>  327	64	preadv2			sys_preadv2
>  328	64	pwritev2		sys_pwritev2
> +329	common	leftpad			sys_leftpad
>  
>  #
>  # x32-specific system call numbers start at 512 to avoid cache impact
> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
> index d795472..a0850bb 100644
> --- a/include/linux/syscalls.h
> +++ b/include/linux/syscalls.h
> @@ -898,4 +898,5 @@ asmlinkage long sys_copy_file_range(int fd_in, loff_t __user *off_in,
>  
>  asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
>  
> +asmlinkage long sys_leftpad(char *str, char pad, char *dst, size_t dst_len);
>  #endif
> diff --git a/kernel/sys.c b/kernel/sys.c
> index cf8ba54..e42d972 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -2432,3 +2432,38 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
>  	return 0;
>  }
>  #endif /* CONFIG_COMPAT */
> +
> +
> +SYSCALL_DEFINE4(leftpad, char *, src, char, pad, char *, dst, size_t, dst_len)
> +{
> +	char *buf;
> +	long ret;
> +	size_t len = strlen_user(src);
> +	size_t pad_len = dst_len - len; 
> +
> +	if (dst_len <= len || dst_len > 4096) {
> +		return -EINVAL;
> +	}
> +
> +	buf = kmalloc(dst_len, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	memset(buf, pad, pad_len);
> +	ret = copy_from_user(buf + pad_len, src, len);
> +	if (ret) {
> +		ret = -EFAULT;
> +		goto out;
> +	}
> +
> +	ret = copy_to_user(dst, buf, dst_len);
> +	if (ret) {
> +		ret = -EFAULT;
> +		goto out;
> +	}
> +
> +	ret = pad_len;
> +out:
> +	kfree(buf);
> +	return ret;
> +}

This looks good, but since we want this to be as fast as possible we might just want to eliminate all
branches (Pesky bounds checks), and write directly into user memory to eliminate the pesky copy_from/copy_to. The second
idea would eliminate that slow kmalloc as well.

What do you think?

> diff --git a/kernel/sys_ni.c b/kernel/sys_ni.
> index 2c5e3a8..262608d 100644
> --- a/kernel/sys_ni.c
> +++ b/kernel/sys_ni.c
> @@ -175,6 +175,7 @@ cond_syscall(sys_setfsgid);
>  cond_syscall(sys_capget);
>  cond_syscall(sys_capset);
>  cond_syscall(sys_copy_file_range);
> +cond_syscall(sys_leftpad);
>  
>  /* arch-specific weak syscall entries */
>  cond_syscall(sys_pciconfig_read);
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: New syscall: leftpad()
  2016-03-31 23:36 ` New syscall: leftpad() Randy Dunlap
@ 2016-04-01  8:06   ` Richard Weinberger
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger @ 2016-04-01  8:06 UTC (permalink / raw)
  To: Randy Dunlap, linux-kernel; +Cc: linux-api

Am 01.04.2016 um 01:36 schrieb Randy Dunlap:
> Please be more careful in your description...

I'm very sorry. Will do a v2 soon. ;-)

> On 03/31/16 15:33, Richard Weinberger wrote:
>> Recent happenings in the node.js community showed how fragile software is when
>> it comes to dependencies of fundamental algorithms like leftpad[1].
>> A node.js package which provided ledpad vanished and broke a lot of software.
> 
>                                    leftpad
> 
>> This raised our attention and we came to the conclusion that it is the kernel's
>> job to provide such functionality such that node.js based applications can in future
>> rely in Linux's "don't break userspace" rule.
>> We hope that glibc and Andoid's bionic will soon offer wrapper functions for this
> 
>                          Android's
> 
>> new leftpad system call.
>> We put leftpad into the kernel not only because of Linux's stable ABI,
>> also for performance reasons.
>> As everyone knows, within the kernel everything is faster and better.
>> Leftpad has millions of users, so it has to be as fast as possible.
>> This new system call will also help making services like left-pad.io[2]
>> faster and more reliable. If the leftpad() system call gets adopted by a wider user base
>> it might also make sense to add a generic npm() system call which acts like ioctl()
>> where kernel modules can register new functions that are often used by node.js.
>> Such functions might be, is_array(), is_int(), etc.
> 
> 

Thanks,
//richard

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] leftpad.2: Document new syscall
  2016-03-31 22:33 ` [PATCH] leftpad.2: Document new syscall Richard Weinberger
@ 2016-04-08 18:10   ` Heinrich Schuchardt
  2016-04-09 14:12     ` Richard Weinberger
  0 siblings, 1 reply; 13+ messages in thread
From: Heinrich Schuchardt @ 2016-04-08 18:10 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel
  Cc: linux-api, Daniel Walter, Michael Kerrisk (man-pages),
	Greg Kroah-Hartman

On 04/01/2016 12:33 AM, Richard Weinberger wrote:
> From: Daniel Walter <dwalter@sigma-star.at>
> 
> Signed-off-by: Daniel Walter <dwalter@sigma-star.at>
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  man2/leftpad.2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 55 insertions(+)
>  create mode 100644 man2/leftpad.2
> 
> diff --git a/man2/leftpad.2 b/man2/leftpad.2
> new file mode 100644
> index 0000000..ff5f401
> --- /dev/null
> +++ b/man2/leftpad.2
> @@ -0,0 +1,55 @@
> +.\" Copyright (c) 2016 sigma-star gmbh
> +.\" (office@sigma-star.at)
> +.\"
> +.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
> +.\" This is free documentation; you can redistribute it and/or
> +.\" modify it under the terms of the GNU General Public License as
> +.\" published by the Free Software Foundation; either version 2 of
> +.\" the License, or (at your option) any later version.
> +.\"
> +.\" The GNU General Public License's references to "object code"
> +.\" and "executables" are to be interpreted as the output of any
> +.\" document formatting or typesetting system, including
> +.\" intermediate and printed output.
> +.\"
> +.\" This manual is distributed in the hope that it will be useful,
> +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
> +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +.\" GNU General Public License for more details.
> +.\"
> +.\" You should have received a copy of the GNU General Public
> +.\" License along with this manual; if not, see
> +.\" <http://www.gnu.org/licenses/>.
> +.\" %%%LICENSE_END
> +.\"
> +.TH LEFTPAD 2 2016-04-01 "Linux" "Linux Programmer's Manual"
> +.SH NAME
> +leftpad
> +.SH SYNOPSIS
> +.nf
> +.B #include <sys/types.h>
> +
> +.BI "int leftpad(char *src, char pad, char *dst, size_t dst_len);
> +.fi
> +.SH DESCRIPTION
> +This function provides left padding for strings.
> +.LP
> +The string in
> +.I src
> +will be left padded with the chosen padding character
> +.I pad
> +and stored in
> +.I dst
> + .
> +.SH RETURN VALUE
> +On success, returns the number of padding characters added.
> +.SH ERRORS
> +.TP
> +.B EINVAL
> +The size of the destination buffer
> +.I dst
> +is shorter than the source string
> +.I src
> + . Or the destination string is longer than 4096 bytes.

Why should the call be limited to an arbitrary number of 4096 bytes?

There is no such limit in malloc so why should we need one here?

NAK

Best regards

Heinrich Schuchardt

> +.SH SEE ALSO
> +.BR snprintf (3)
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] leftpad.2: Document new syscall
  2016-04-08 18:10   ` Heinrich Schuchardt
@ 2016-04-09 14:12     ` Richard Weinberger
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger @ 2016-04-09 14:12 UTC (permalink / raw)
  To: Heinrich Schuchardt, linux-kernel
  Cc: linux-api, Daniel Walter, Michael Kerrisk (man-pages),
	Greg Kroah-Hartman

Am 08.04.2016 um 20:10 schrieb Heinrich Schuchardt:
> On 04/01/2016 12:33 AM, Richard Weinberger wrote:
>> From: Daniel Walter <dwalter@sigma-star.at>
>>
>> Signed-off-by: Daniel Walter <dwalter@sigma-star.at>
>> Signed-off-by: Richard Weinberger <richard@nod.at>
>> ---
>>  man2/leftpad.2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 55 insertions(+)
>>  create mode 100644 man2/leftpad.2
>>
>> diff --git a/man2/leftpad.2 b/man2/leftpad.2
>> new file mode 100644
>> index 0000000..ff5f401
>> --- /dev/null
>> +++ b/man2/leftpad.2
>> @@ -0,0 +1,55 @@
>> +.\" Copyright (c) 2016 sigma-star gmbh
>> +.\" (office@sigma-star.at)
>> +.\"
>> +.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
>> +.\" This is free documentation; you can redistribute it and/or
>> +.\" modify it under the terms of the GNU General Public License as
>> +.\" published by the Free Software Foundation; either version 2 of
>> +.\" the License, or (at your option) any later version.
>> +.\"
>> +.\" The GNU General Public License's references to "object code"
>> +.\" and "executables" are to be interpreted as the output of any
>> +.\" document formatting or typesetting system, including
>> +.\" intermediate and printed output.
>> +.\"
>> +.\" This manual is distributed in the hope that it will be useful,
>> +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> +.\" GNU General Public License for more details.
>> +.\"
>> +.\" You should have received a copy of the GNU General Public
>> +.\" License along with this manual; if not, see
>> +.\" <http://www.gnu.org/licenses/>.
>> +.\" %%%LICENSE_END
>> +.\"
>> +.TH LEFTPAD 2 2016-04-01 "Linux" "Linux Programmer's Manual"
>> +.SH NAME
>> +leftpad
>> +.SH SYNOPSIS
>> +.nf
>> +.B #include <sys/types.h>
>> +
>> +.BI "int leftpad(char *src, char pad, char *dst, size_t dst_len);
>> +.fi
>> +.SH DESCRIPTION
>> +This function provides left padding for strings.
>> +.LP
>> +The string in
>> +.I src
>> +will be left padded with the chosen padding character
>> +.I pad
>> +and stored in
>> +.I dst
>> + .
>> +.SH RETURN VALUE
>> +On success, returns the number of padding characters added.
>> +.SH ERRORS
>> +.TP
>> +.B EINVAL
>> +The size of the destination buffer
>> +.I dst
>> +is shorter than the source string
>> +.I src
>> + . Or the destination string is longer than 4096 bytes.
> 
> Why should the call be limited to an arbitrary number of 4096 bytes?
> 
> There is no such limit in malloc so why should we need one here?
> 
> NAK

Sorry, the review window for this patch is now closed.
Please resend your comments on April 1st 2017.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-04-09 14:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-31 22:33 New syscall: leftpad() Richard Weinberger
2016-03-31 22:33 ` [PATCH] Implement leftpad syscall Richard Weinberger
2016-03-31 22:46   ` Michael Kerrisk (man-pages)
2016-03-31 23:09   ` Greg KH
2016-04-01  1:33   ` Frederic Weisbecker
2016-04-01  3:22   ` kbuild test robot
2016-04-01  6:56   ` Richard Cochran
2016-04-01  7:14   ` Scotty Bauer
2016-03-31 22:33 ` [PATCH] leftpad.2: Document new syscall Richard Weinberger
2016-04-08 18:10   ` Heinrich Schuchardt
2016-04-09 14:12     ` Richard Weinberger
2016-03-31 23:36 ` New syscall: leftpad() Randy Dunlap
2016-04-01  8:06   ` Richard Weinberger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).