Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lockdep: fix static memory detection even more

On the parisc architecture, lockdep reports for all static objects which
are in the __initdata section (e.g. "setup_done" in devtmpfs,
"kthreadd_done" in init/main.c) this warning:

INFO: trying to register non-static key.

The warning itself is wrong, because those objects are in the __initdata
section, but the section itself is on parisc outside of range from
_stext to _end, which is why the static_obj() functions returns a wrong
answer.

While fixing this issue, I noticed that the whole existing check can
be simplified a lot.
Instead of checking against the _stext and _end symbols (which include
code areas too) just check for the .data and .bss segments (since we check a
data object). This can be done with the existing is_kernel_core_data()
macro.

In addition objects in the __initdata section can be checked with
init_section_contains(), and is_kernel_rodata() allows keys to be in the
_ro_after_init section.

This partly reverts and simplifies commit bac59d18c701 ("x86/setup: Fix static
memory detection").

Link: https://lkml.kernel.org/r/ZNqrLRaOi/3wPAdp@p100
Fixes: bac59d18c701 ("x86/setup: Fix static memory detection")
Signed-off-by: Helge Deller <deller@gmx.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Helge Deller and committed by
Andrew Morton
0a6b58c5 66553609

+14 -40
-18
arch/x86/include/asm/sections.h
··· 2 2 #ifndef _ASM_X86_SECTIONS_H 3 3 #define _ASM_X86_SECTIONS_H 4 4 5 - #define arch_is_kernel_initmem_freed arch_is_kernel_initmem_freed 6 - 7 5 #include <asm-generic/sections.h> 8 6 #include <asm/extable.h> 9 7 ··· 15 17 extern char __end_of_kernel_reserve[]; 16 18 17 19 extern unsigned long _brk_start, _brk_end; 18 - 19 - static inline bool arch_is_kernel_initmem_freed(unsigned long addr) 20 - { 21 - /* 22 - * If _brk_start has not been cleared, brk allocation is incomplete, 23 - * and we can not make assumptions about its use. 24 - */ 25 - if (_brk_start) 26 - return 0; 27 - 28 - /* 29 - * After brk allocation is complete, space between _brk_end and _end 30 - * is available for allocation. 31 - */ 32 - return addr >= _brk_end && addr < (unsigned long)&_end; 33 - } 34 20 35 21 #endif /* _ASM_X86_SECTIONS_H */
+14 -22
kernel/locking/lockdep.c
··· 819 819 * Is this the address of a static object: 820 820 */ 821 821 #ifdef __KERNEL__ 822 - /* 823 - * Check if an address is part of freed initmem. After initmem is freed, 824 - * memory can be allocated from it, and such allocations would then have 825 - * addresses within the range [_stext, _end]. 826 - */ 827 - #ifndef arch_is_kernel_initmem_freed 828 - static int arch_is_kernel_initmem_freed(unsigned long addr) 829 - { 830 - if (system_state < SYSTEM_FREEING_INITMEM) 831 - return 0; 832 - 833 - return init_section_contains((void *)addr, 1); 834 - } 835 - #endif 836 - 837 822 static int static_obj(const void *obj) 838 823 { 839 - unsigned long start = (unsigned long) &_stext, 840 - end = (unsigned long) &_end, 841 - addr = (unsigned long) obj; 824 + unsigned long addr = (unsigned long) obj; 842 825 843 - if (arch_is_kernel_initmem_freed(addr)) 844 - return 0; 826 + if (is_kernel_core_data(addr)) 827 + return 1; 845 828 846 829 /* 847 - * static variable? 830 + * keys are allowed in the __ro_after_init section. 848 831 */ 849 - if ((addr >= start) && (addr < end)) 832 + if (is_kernel_rodata(addr)) 833 + return 1; 834 + 835 + /* 836 + * in initdata section and used during bootup only? 837 + * NOTE: On some platforms the initdata section is 838 + * outside of the _stext ... _end range. 839 + */ 840 + if (system_state < SYSTEM_FREEING_INITMEM && 841 + init_section_contains((void *)addr, 1)) 850 842 return 1; 851 843 852 844 /*