All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] mm: Uninline copy_overflow()
@ 2022-02-12  7:50 Christophe Leroy
  2022-02-12 18:17 ` kernel test robot
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe Leroy @ 2022-02-12  7:50 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Christophe Leroy, linux-kernel, linux-mm

While building a small config with CONFIG_CC_OPTIMISE_FOR_SIZE,
I ended up with more than 50 times the following function in vmlinux
because GCC doesn't honor the 'inline' keyword:

	c00243bc <copy_overflow>:
	c00243bc:	94 21 ff f0 	stwu    r1,-16(r1)
	c00243c0:	7c 85 23 78 	mr      r5,r4
	c00243c4:	7c 64 1b 78 	mr      r4,r3
	c00243c8:	3c 60 c0 62 	lis     r3,-16286
	c00243cc:	7c 08 02 a6 	mflr    r0
	c00243d0:	38 63 5e e5 	addi    r3,r3,24293
	c00243d4:	90 01 00 14 	stw     r0,20(r1)
	c00243d8:	4b ff 82 45 	bl      c001c61c <__warn_printk>
	c00243dc:	0f e0 00 00 	twui    r0,0
	c00243e0:	80 01 00 14 	lwz     r0,20(r1)
	c00243e4:	38 21 00 10 	addi    r1,r1,16
	c00243e8:	7c 08 03 a6 	mtlr    r0
	c00243ec:	4e 80 00 20 	blr

With -Winline, GCC tells:

	/include/linux/thread_info.h:212:20: warning: inlining failed in call to 'copy_overflow': call is unlikely and code size would grow [-Winline]

copy_overflow() is a non conditional warning called by
check_copy_size() on an error path.

check_copy_size() have to remain inlined in order to benefit
from constant folding, but copy_overflow() is not worth inlining.

Uninline it when CONFIG_BUG is selected.

When CONFIG_BUG is not selected, WARN() does nothing so skip it.

This reduces the size of vmlinux by almost 4kbytes.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v4: Make copy_overflow() a no-op when CONFIG_BUG is not selected

v3: Added missing ; after EXPORT_SYMBOL()

v2: Added missing EXPORT_SYMBOL() and enhanced commit message
---
 include/linux/thread_info.h | 5 ++++-
 mm/maccess.c                | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 73a6f34b3847..9f392ec76f2b 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -209,9 +209,12 @@ __bad_copy_from(void);
 extern void __compiletime_error("copy destination size is too small")
 __bad_copy_to(void);
 
+void __copy_overflow(int size, unsigned long count);
+
 static inline void copy_overflow(int size, unsigned long count)
 {
-	WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
+	if (IS_ENABLED(CONFIG_BUG))
+		__copy_overflow(size, count);
 }
 
 static __always_inline __must_check bool
diff --git a/mm/maccess.c b/mm/maccess.c
index d3f1a1f0b1c1..4d46708c9a0d 100644
--- a/mm/maccess.c
+++ b/mm/maccess.c
@@ -335,3 +335,9 @@ long strnlen_user_nofault(const void __user *unsafe_addr, long count)
 
 	return ret;
 }
+
+void __copy_overflow(int size, unsigned long count)
+{
+	WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
+}
+EXPORT_SYMBOL_GPL(__copy_overflow);
-- 
2.34.1


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

* Re: [PATCH v4] mm: Uninline copy_overflow()
  2022-02-12  7:50 [PATCH v4] mm: Uninline copy_overflow() Christophe Leroy
@ 2022-02-12 18:17 ` kernel test robot
  2022-02-13 18:58   ` Christophe Leroy
  0 siblings, 1 reply; 4+ messages in thread
From: kernel test robot @ 2022-02-12 18:17 UTC (permalink / raw)
  To: kbuild-all

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

Hi Christophe,

I love your patch! Yet something to improve:

[auto build test ERROR on linux/master]
[also build test ERROR on linus/master v5.17-rc3]
[cannot apply to hnaz-mm/master next-20220211]
[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]

url:    https://github.com/0day-ci/linux/commits/Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2c271fe77d52a0555161926c232cd5bc07178b39
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20220213/202202130121.dZLDqfpe-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
        git checkout cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from arch/x86/include/asm/preempt.h:7,
                    from include/linux/preempt.h:78,
                    from include/linux/percpu.h:6,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/hardirq.h:5,
                    from drivers/net/ethernet/mellanox/mlx4/cq.c:37:
   In function 'check_copy_size',
       inlined from 'copy_to_user' at include/linux/uaccess.h:199:6,
       inlined from 'mlx4_init_user_cqes' at drivers/net/ethernet/mellanox/mlx4/cq.c:317:9,
       inlined from 'mlx4_cq_alloc' at drivers/net/ethernet/mellanox/mlx4/cq.c:394:10:
>> include/linux/thread_info.h:228:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small
     228 |    __bad_copy_from();
         |    ^~~~~~~~~~~~~~~~~


vim +/__bad_copy_from +228 include/linux/thread_info.h

b0377fedb65280 Al Viro   2017-06-29  219  
9dd819a15162f8 Kees Cook 2019-09-25  220  static __always_inline __must_check bool
b0377fedb65280 Al Viro   2017-06-29  221  check_copy_size(const void *addr, size_t bytes, bool is_source)
b0377fedb65280 Al Viro   2017-06-29  222  {
c80d92fbb67b2c Kees Cook 2021-06-17  223  	int sz = __builtin_object_size(addr, 0);
b0377fedb65280 Al Viro   2017-06-29  224  	if (unlikely(sz >= 0 && sz < bytes)) {
b0377fedb65280 Al Viro   2017-06-29  225  		if (!__builtin_constant_p(bytes))
b0377fedb65280 Al Viro   2017-06-29  226  			copy_overflow(sz, bytes);
b0377fedb65280 Al Viro   2017-06-29  227  		else if (is_source)
b0377fedb65280 Al Viro   2017-06-29 @228  			__bad_copy_from();
b0377fedb65280 Al Viro   2017-06-29  229  		else
b0377fedb65280 Al Viro   2017-06-29  230  			__bad_copy_to();
b0377fedb65280 Al Viro   2017-06-29  231  		return false;
b0377fedb65280 Al Viro   2017-06-29  232  	}
6d13de1489b6bf Kees Cook 2019-12-04  233  	if (WARN_ON_ONCE(bytes > INT_MAX))
6d13de1489b6bf Kees Cook 2019-12-04  234  		return false;
b0377fedb65280 Al Viro   2017-06-29  235  	check_object_size(addr, bytes, is_source);
b0377fedb65280 Al Viro   2017-06-29  236  	return true;
b0377fedb65280 Al Viro   2017-06-29  237  }
b0377fedb65280 Al Viro   2017-06-29  238  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

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

* Re: [PATCH v4] mm: Uninline copy_overflow()
  2022-02-12 18:17 ` kernel test robot
@ 2022-02-13 18:58   ` Christophe Leroy
  2022-02-16  8:55     ` Chen, Rong A
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe Leroy @ 2022-02-13 18:58 UTC (permalink / raw)
  To: kbuild-all

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



Le 12/02/2022 à 19:17, kernel test robot a écrit :
> Hi Christophe,
> 
> I love your patch! Yet something to improve:
> 
> [auto build test ERROR on linux/master]
> [also build test ERROR on linus/master v5.17-rc3]
> [cannot apply to hnaz-mm/master next-20220211]
> [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]
> 
> url:    https://github.com/0day-ci/linux/commits/Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2c271fe77d52a0555161926c232cd5bc07178b39
> config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20220213/202202130121.dZLDqfpe-lkp(a)intel.com/config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
> reproduce (this is a W=1 build):
>          # https://github.com/0day-ci/linux/commit/cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
>          git remote add linux-review https://github.com/0day-ci/linux
>          git fetch --no-tags linux-review Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
>          git checkout cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
>          # save the config file to linux build tree
>          mkdir build_dir
>          make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
> 
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@intel.com>
> 
> All errors (new ones prefixed by >>):
> 
>     In file included from arch/x86/include/asm/preempt.h:7,
>                      from include/linux/preempt.h:78,
>                      from include/linux/percpu.h:6,
>                      from include/linux/context_tracking_state.h:5,
>                      from include/linux/hardirq.h:5,
>                      from drivers/net/ethernet/mellanox/mlx4/cq.c:37:
>     In function 'check_copy_size',
>         inlined from 'copy_to_user' at include/linux/uaccess.h:199:6,
>         inlined from 'mlx4_init_user_cqes' at drivers/net/ethernet/mellanox/mlx4/cq.c:317:9,
>         inlined from 'mlx4_cq_alloc' at drivers/net/ethernet/mellanox/mlx4/cq.c:394:10:
>>> include/linux/thread_info.h:228:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small
>       228 |    __bad_copy_from();
>           |    ^~~~~~~~~~~~~~~~~
> 

This looks like a bug in GCC 9.x

You get the exact same result without my patch when unselecting CONFIG_BUG

You don't get the problem with gcc 8.5
You don't get the problem with gcc 10.3

My patch itself doesn't explain getting this error.

Christophe

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

* Re: [PATCH v4] mm: Uninline copy_overflow()
  2022-02-13 18:58   ` Christophe Leroy
@ 2022-02-16  8:55     ` Chen, Rong A
  0 siblings, 0 replies; 4+ messages in thread
From: Chen, Rong A @ 2022-02-16  8:55 UTC (permalink / raw)
  To: kbuild-all

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

Hi Christophe,

On 2/14/2022 2:58 AM, Christophe Leroy wrote:
> 
> 
> Le 12/02/2022 à 19:17, kernel test robot a écrit :
>> Hi Christophe,
>>
>> I love your patch! Yet something to improve:
>>
>> [auto build test ERROR on linux/master]
>> [also build test ERROR on linus/master v5.17-rc3]
>> [cannot apply to hnaz-mm/master next-20220211]
>> [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]
>>
>> url:    https://github.com/0day-ci/linux/commits/Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
>> base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2c271fe77d52a0555161926c232cd5bc07178b39
>> config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20220213/202202130121.dZLDqfpe-lkp(a)intel.com/config)
>> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
>> reproduce (this is a W=1 build):
>>           # https://github.com/0day-ci/linux/commit/cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
>>           git remote add linux-review https://github.com/0day-ci/linux
>>           git fetch --no-tags linux-review Christophe-Leroy/mm-Uninline-copy_overflow/20220212-155040
>>           git checkout cf4474ac62f0332bd9d39f9dd74dbb5afce9a7a4
>>           # save the config file to linux build tree
>>           mkdir build_dir
>>           make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
>>
>> If you fix the issue, kindly add following tag as appropriate
>> Reported-by: kernel test robot <lkp@intel.com>
>>
>> All errors (new ones prefixed by >>):
>>
>>      In file included from arch/x86/include/asm/preempt.h:7,
>>                       from include/linux/preempt.h:78,
>>                       from include/linux/percpu.h:6,
>>                       from include/linux/context_tracking_state.h:5,
>>                       from include/linux/hardirq.h:5,
>>                       from drivers/net/ethernet/mellanox/mlx4/cq.c:37:
>>      In function 'check_copy_size',
>>          inlined from 'copy_to_user' at include/linux/uaccess.h:199:6,
>>          inlined from 'mlx4_init_user_cqes' at drivers/net/ethernet/mellanox/mlx4/cq.c:317:9,
>>          inlined from 'mlx4_cq_alloc' at drivers/net/ethernet/mellanox/mlx4/cq.c:394:10:
>>>> include/linux/thread_info.h:228:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small
>>        228 |    __bad_copy_from();
>>            |    ^~~~~~~~~~~~~~~~~
>>
> 
> This looks like a bug in GCC 9.x
> 
> You get the exact same result without my patch when unselecting CONFIG_BUG
> 
> You don't get the problem with gcc 8.5
> You don't get the problem with gcc 10.3
> 
> My patch itself doesn't explain getting this error.

Thanks for the detailed explanation, we'll add the error to ignore list.

Best Regards,
Rong Chen

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

end of thread, other threads:[~2022-02-16  8:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-12  7:50 [PATCH v4] mm: Uninline copy_overflow() Christophe Leroy
2022-02-12 18:17 ` kernel test robot
2022-02-13 18:58   ` Christophe Leroy
2022-02-16  8:55     ` Chen, Rong A

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.