Commit 861574d51bbd ("powerpc/uaccess: Implement masked user access")
provides optimised user access by avoiding the cost of access_ok().

Convert csum_and_copy_to_user() and csum_and_copy_from_user() to
scoped user access to benefit from masked user access.

csum_and_copy_to_user() and csum_and_copy_from_user() are only
called respectively by csum_and_copy_to_iter() and
csum_and_copy_from_iter_full() and they are only called twice.

Those functions used to be large but they were first reduced by
commit c693cc4676a0 ("saner calling conventions for
csum_and_copy_..._user()") then commit 70d65cd555c5 ("ppc: propagate
the calling conventions change down to csum_partial_copy_generic()").
With the additional size reduction provided by conversion to scoped
user access they are not worth being kept out of line.

  $ ./scripts/bloat-o-meter vmlinux.0 vmlinux.1
  add/remove: 0/2 grow/shrink: 2/0 up/down: 136/-176 (-40)
  Function                                     old     new   delta
  csum_and_copy_to_iter                       2416    2488     +72
  csum_and_copy_from_iter_full                2272    2336     +64
  csum_and_copy_to_user                         88       -     -88
  csum_and_copy_from_user                       88       -     -88
  Total: Before=11514471, After=11514431, chg -0.00%

Signed-off-by: Christophe Leroy (CS GROUP) <[email protected]>
---
This patch applies on top of v7.0-rc3, it requires commit af4e9ef3d784 
("uaccess: Fix scoped_user_read_access() for 'pointer to const'")
---
 arch/powerpc/include/asm/checksum.h  | 22 +++++++++++++---
 arch/powerpc/lib/Makefile            |  3 +--
 arch/powerpc/lib/checksum_wrappers.c | 39 ----------------------------
 3 files changed, 19 insertions(+), 45 deletions(-)
 delete mode 100644 arch/powerpc/lib/checksum_wrappers.c

diff --git a/arch/powerpc/include/asm/checksum.h 
b/arch/powerpc/include/asm/checksum.h
index 4b573a3b7e17..52921ea2494a 100644
--- a/arch/powerpc/include/asm/checksum.h
+++ b/arch/powerpc/include/asm/checksum.h
@@ -8,6 +8,7 @@
 
 #include <linux/bitops.h>
 #include <linux/in6.h>
+#include <linux/uaccess.h>
 /*
  * Computes the checksum of a memory block at src, length len,
  * and adds in "sum" (32-bit), while copying the block to dst.
@@ -21,11 +22,24 @@
 extern __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
 
 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
-extern __wsum csum_and_copy_from_user(const void __user *src, void *dst,
-                                     int len);
+static inline __wsum csum_and_copy_from_user(const void __user *src, void 
*dst, int len)
+{
+       scoped_user_read_access_size(src, len, efault)
+               return csum_partial_copy_generic((void __force *)src, dst, len);
+
+efault:
+       return 0;
+}
+
 #define HAVE_CSUM_COPY_USER
-extern __wsum csum_and_copy_to_user(const void *src, void __user *dst,
-                                   int len);
+static inline __wsum csum_and_copy_to_user(const void *src, void __user *dst, 
int len)
+{
+       scoped_user_write_access_size(dst, len, efault)
+               return csum_partial_copy_generic(src, (void __force *)dst, len);
+
+efault:
+       return 0;
+}
 
 #define _HAVE_ARCH_CSUM_AND_COPY
 #define csum_partial_copy_nocheck(src, dst, len)   \
diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
index f14ecab674a3..bcdf387f3998 100644
--- a/arch/powerpc/lib/Makefile
+++ b/arch/powerpc/lib/Makefile
@@ -62,8 +62,7 @@ obj64-$(CONFIG_ALTIVEC)       += vmx-helper.o
 obj64-$(CONFIG_KPROBES_SANITY_TEST)    += test_emulate_step.o \
                                           test_emulate_step_exec_instr.o
 
-obj-y                  += checksum_$(BITS).o checksum_wrappers.o \
-                          string_$(BITS).o
+obj-y                  += checksum_$(BITS).o string_$(BITS).o
 
 obj-y                  += sstep.o
 obj-$(CONFIG_PPC_FPU)  += ldstfp.o
diff --git a/arch/powerpc/lib/checksum_wrappers.c 
b/arch/powerpc/lib/checksum_wrappers.c
deleted file mode 100644
index 1a14c8780278..000000000000
--- a/arch/powerpc/lib/checksum_wrappers.c
+++ /dev/null
@@ -1,39 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- *
- * Copyright (C) IBM Corporation, 2010
- *
- * Author: Anton Blanchard <[email protected]>
- */
-#include <linux/export.h>
-#include <linux/compiler.h>
-#include <linux/types.h>
-#include <asm/checksum.h>
-#include <linux/uaccess.h>
-
-__wsum csum_and_copy_from_user(const void __user *src, void *dst,
-                              int len)
-{
-       __wsum csum;
-
-       if (unlikely(!user_read_access_begin(src, len)))
-               return 0;
-
-       csum = csum_partial_copy_generic((void __force *)src, dst, len);
-
-       user_read_access_end();
-       return csum;
-}
-
-__wsum csum_and_copy_to_user(const void *src, void __user *dst, int len)
-{
-       __wsum csum;
-
-       if (unlikely(!user_write_access_begin(dst, len)))
-               return 0;
-
-       csum = csum_partial_copy_generic(src, (void __force *)dst, len);
-
-       user_write_access_end();
-       return csum;
-}
-- 
2.49.0


Reply via email to