lib: Some #include cleanups
Subsystems (7)
cover lib: Some #include cleanups
I'm contemplating doing some tree-wide spring cleaning, mostly consisting of removing redundant includes, but also refactoring some large and heterogeneous headers into smaller pieces.
This patch series consists of low-hanging fruit I picked from lib/. I've verified that "objdump -d lib/builtin.o" produces identical output for {allyes,allno,def}config, but this is of course only for x86_64. There's a small but measurable build time improvement: A defconfig build of lib/ from a clean tree is consistently .5 seconds (2 %) faster with these patches. There are lots of other apparently unused headers in lib/*.c, but I've only removed those where it actually ends up disappearing from the .cmd dependency file (since that's where the potential build gain lies, and helps prove that the header is indeed redundant).
I expect there is much larger speedups to gain from removing includes from .h files, but that is of course at the same time a huge can of worms, involving boatloads of tree-wide patches updating users to include what they use directly. So before I start doing that, I'd like to hear if people think it would be futile.
Rasmus Villemoes (19):
lib/interval_tree.c: Simplify includes lib/sort.c: Use simpler includes lib/dynamic_queue_limits.c: Simplify includes lib/halfmd4.c: Simplify includes lib/idr.c: Remove redundant include lib/genalloc.c: Remove redundant include lib/list_sort.c: Rearrange includes lib/md5.c: Simplify include
lib/llist.c: Remove redundant include lib/kobject_uevent.c: Remove redundant include lib/nlattr.c: Remove redundant include lib/plist.c: Remove redundant include lib/radix-tree.c: Change to simpler include lib/show_mem.c: Remove redundant include lib/sort.c: Move include inside #if 0 lib/stmp_device.c: Replace module.h include lib/strncpy_from_user.c: Replace module.h include lib/percpu_ida.c: Remove redundant includes lib/lcm.c: Replace include
include/linux/cryptohash.h | 2 ++ lib/dynamic_queue_limits.c | 4 ++-- lib/genalloc.c | 1 - lib/halfmd4.c | 2 +- lib/idr.c | 1 - lib/interval_tree.c | 4 ++-- lib/kobject_uevent.c | 1 - lib/lcm.c | 2 +- lib/list_sort.c | 7 +++++-- lib/llist.c | 1 - lib/md5.c | 2 +- lib/nlattr.c | 1 - lib/percpu_ida.c | 3 --- lib/plist.c | 1 - lib/radix-tree.c | 2 +- lib/show_mem.c | 1 - lib/sort.c | 6 +++--- lib/stmp_device.c | 3 ++- lib/strncpy_from_user.c | 3 ++- 19 files changed, 22 insertions(+), 25 deletions(-)
Signature · 1 line
2.1.3
PATCH 2/19 lib/sort.c: Use simpler includes
sort.c doesn't use facilities from kernel.h, but does use some types defined in linux/types.h. Include the latter directly instead of relying on some other header doing it. Similarly, include linux/export.h directly instead of through module.h. This removes 80 lines from the dependency file .sort.o.cmd.
--- lib/sort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/sort.c +2 -2
@@ -4,8 +4,8 @@* Jan 23 2005 Matt Mackall <[email protected]> */ -#include <linux/kernel.h> -#include <linux/module.h> +#include <linux/types.h> +#include <linux/export.h> #include <linux/sort.h> #include <linux/slab.h>
Signature · 1 line
2.1.3
PATCH 5/19 lib/idr.c: Remove redundant include
In reply to Rasmus Villemoesidr.c doesn't seem to use anything from hardirq.h (or anything included from that). Removing it produces identical objdump -d output, and gives 44 fewer lines in the .idr.o.cmd dependency file.
--- lib/idr.c | 1 - 1 file changed, 1 deletion(-)
lib/idr.c +0 -1
@@ -30,7 +30,6 @@#include <linux/idr.h> #include <linux/spinlock.h> #include <linux/percpu.h> -#include <linux/hardirq.h> #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)#define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
Signature · 1 line
2.1.3
PATCH 6/19 lib/genalloc.c: Remove redundant include
In reply to Rasmus VillemoesRemoving this include produces byte-identical output, and thus removes a false dependency.
--- lib/genalloc.c | 1 - 1 file changed, 1 deletion(-)
lib/genalloc.c +0 -1
@@ -34,7 +34,6 @@#include <linux/rculist.h> #include <linux/interrupt.h> #include <linux/genalloc.h> -#include <linux/of_address.h> #include <linux/of_device.h> static inline size_t chunk_size (const struct gen_pool_chunk *chunk)
Signature · 1 line
2.1.3
PATCH 10/19 lib/kobject_uevent.c: Remove redundant include
In reply to Rasmus VillemoesThe file doesn't seem to use anything from linux/user_namespace.h, and removing it yields byte-identical object code and strictly fewer dependencies in the .cmd file.
--- lib/kobject_uevent.c | 1 - 1 file changed, 1 deletion(-)
lib/kobject_uevent.c +0 -1
@@ -20,7 +20,6 @@#include <linux/export.h> #include <linux/kmod.h> #include <linux/slab.h> -#include <linux/user_namespace.h> #include <linux/socket.h> #include <linux/skbuff.h> #include <linux/netlink.h>
Signature · 1 line
2.1.3
PATCH 16/19 lib/stmp_device.c: Replace module.h include
In reply to Rasmus Villemoesstmp_device.c only needs EXPORT_SYMBOL, so just include compiler.h and export.h instead of the whole module.h machinery.
--- lib/stmp_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/stmp_device.c +2 -1
@@ -15,7 +15,8 @@#include <linux/io.h> #include <linux/errno.h> #include <linux/delay.h> -#include <linux/module.h> +#include <linux/compiler.h> +#include <linux/export.h> #include <linux/stmp_device.h> #define STMP_MODULE_CLKGATE (1 << 30)
Signature · 1 line
2.1.3
PATCH 15/19 lib/sort.c: Move include inside #if 0
In reply to Rasmus VillemoesThe sort function and its helpers don't do memory allocation, so the slab.h include is redundant. Move it inside the #if 0 protecting the self-test, similar to how it is done in lib/list_sort.c. This removes over 450 lines from the generated dependency file.
--- lib/sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sort.c +1 -1
@@ -7,7 +7,6 @@#include <linux/types.h> #include <linux/export.h> #include <linux/sort.h> -#include <linux/slab.h> static void u32_swap (void *a,void *b,int size) {
@@ -85,6 +84,7 @@ void sort(void *base, size_t num, size_t size,EXPORT_SYMBOL (sort);#if 0 +#include <linux/slab.h> /* a simple boot-time regression test */ int cmpint (const void *a,const void *b)
Signature · 1 line
2.1.3
PATCH 19/19 lib/lcm.c: Replace include
In reply to Rasmus VillemoesWe don't need all the stuff kernel.h pulls in; just compiler.h since export.h doesn't do necessary #includes. This removes more than 100 dependencies.
--- lib/lcm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lcm.c +1 -1
@@ -1,4 +1,4 @@ -#include <linux/kernel.h> +#include <linux/compiler.h> #include <linux/gcd.h> #include <linux/export.h> #include <linux/lcm.h>
Signature · 1 line
2.1.3
PATCH 18/19 lib/percpu_ida.c: Remove redundant includes
In reply to Rasmus VillemoesThese three #includes seem to be completely redundant: Removing them yields identical objdump -d output for each of {allyes,allno,def}config, and neither included file end up in the generated dependency file through some recursive include. In total, about 50 lines are eliminated from .percpu.o.cmd.
--- lib/percpu_ida.c | 3 --- 1 file changed, 3 deletions(-)
lib/percpu_ida.c +0 -3
@@ -19,13 +19,10 @@#include <linux/bug.h> #include <linux/err.h> #include <linux/export.h> -#include <linux/hardirq.h> -#include <linux/idr.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/percpu.h> #include <linux/sched.h> -#include <linux/slab.h> #include <linux/string.h> #include <linux/spinlock.h> #include <linux/percpu_ida.h>
Signature · 1 line
2.1.3
PATCH 17/19 lib/strncpy_from_user.c: Replace module.h include
In reply to Rasmus Villemoesstrncpy_from_user.c only needs EXPORT_SYMBOL, so just include compiler.h and export.h instead of the whole module.h machinery.
--- lib/strncpy_from_user.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/strncpy_from_user.c +2 -1
@@ -1,4 +1,5 @@ -#include <linux/module.h> +#include <linux/compiler.h> +#include <linux/export.h> #include <linux/uaccess.h> #include <linux/kernel.h> #include <linux/errno.h>
Signature · 1 line
2.1.3
PATCH 14/19 lib/show_mem.c: Remove redundant include
In reply to Rasmus Villemoesshow_mem.c doesn't use anything from nmi.h. Removing it yields identical objdump -d output for each of {allyes,allno,def}config and eliminates more than 100 lines in the dependency file.
--- lib/show_mem.c | 1 - 1 file changed, 1 deletion(-)
lib/show_mem.c +0 -1
@@ -6,7 +6,6 @@*/ #include <linux/mm.h> -#include <linux/nmi.h> #include <linux/quicklist.h> #include <linux/cma.h>
Signature · 1 line
2.1.3
PATCH 13/19 lib/radix-tree.c: Change to simpler include
In reply to Rasmus VillemoesThe comment helpfully explains why hardirq.h is included, but since 2d4b84739f0a ("hardirq: Split preempt count mask definitions") in_interrupt() has been provided by preempt_mask.h. Use that instead, saving around 40 lines in the generated dependency file.
--- lib/radix-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/radix-tree.c +1 -1
@@ -33,7 +33,7 @@#include <linux/string.h> #include <linux/bitops.h> #include <linux/rcupdate.h> -#include <linux/hardirq.h> /* in_interrupt() */ +#include <linux/preempt_mask.h> /* in_interrupt() */ /*
Signature · 1 line
2.1.3
PATCH 12/19 lib/plist.c: Remove redundant include
In reply to Rasmus VillemoesRemoving the include of linux/spinlock.h produces byte-identical output for {allno,def}config, and identical objdump -d output for allyesconfig. In the former two cases, more than a 100 lines are eliminated from the generated dependency file.
--- lib/plist.c | 1 - 1 file changed, 1 deletion(-)
lib/plist.c +0 -1
@@ -25,7 +25,6 @@#include <linux/bug.h> #include <linux/plist.h> -#include <linux/spinlock.h> #ifdef CONFIG_DEBUG_PI_LIST
Signature · 1 line
2.1.3
PATCH 11/19 lib/nlattr.c: Remove redundant include
In reply to Rasmus Villemoesnlattr.c doesn't seem to rely on anything from netdevice.h. Removing it yields identical objdump -d output for each of {allyes,allno,def}config, and eliminates more than 200 lines from the generated dependency file.
--- lib/nlattr.c | 1 - 1 file changed, 1 deletion(-)
lib/nlattr.c +0 -1
@@ -9,7 +9,6 @@#include <linux/kernel.h> #include <linux/errno.h> #include <linux/jiffies.h> -#include <linux/netdevice.h> #include <linux/skbuff.h> #include <linux/string.h> #include <linux/types.h>
Signature · 1 line
2.1.3
PATCH 9/19 lib/llist.c: Remove redundant include
In reply to Rasmus VillemoesThis file doesn't seem to use anything provided by linux/interrupt.h or anything recursively included through that. Removing it produces byte-identical output, while reducing .llist.o.cmd from 541 to 156 lines.
--- lib/llist.c | 1 - 1 file changed, 1 deletion(-)
lib/llist.c +0 -1
@@ -24,7 +24,6 @@*/ #include <linux/kernel.h> #include <linux/export.h> -#include <linux/interrupt.h> #include <linux/llist.h>
Signature · 1 line
2.1.3
PATCH 8/19 lib/md5.c: Simplify include
In reply to Rasmus Villemoesmd5.c doesn't use anything from kernel.h, except that that pulls in compiler.h, which is needed for the export.h to work.
--- lib/md5.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/md5.c +1 -1
@@ -1,4 +1,4 @@ -#include <linux/kernel.h> +#include <linux/compiler.h> #include <linux/export.h> #include <linux/cryptohash.h>
Signature · 1 line
2.1.3
PATCH 7/19 lib/list_sort.c: Rearrange includes
In reply to Rasmus VillemoesMemory allocation only happens in the self test, just as random numbers are only used there. So move the inclusion of slab.h inside the CONFIG_TEST_LIST_SORT.
We don't need module.h and all of the stuff it carries with it, so replace with export.h and compiler.h. Unfortunately, the ARRAY_SIZE macro from kernel.h requires the user to ensure bug.h is also included (for BUILD_BUG_ON_ZERO, used by __must_be_array). We used to get that through some maze of nested includes, but just include it explicitly.
linux/string.h is then only included implicitly through kernel.h->printk.h->dynamic_debug.h, but only if !CONFIG_DYNAMIC_DEBUG, so just include it explicitly (for memset).
objdump -d says the generated code is the same, and wc -l says that lib/.list_sort.o.cmd went from 579 to 165 lines.
--- lib/list_sort.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
lib/list_sort.c +5 -2
@@ -2,9 +2,11 @@#define pr_fmt (fmt) "list_sort_test: " fmt#include <linux/kernel.h> -#include <linux/module.h> +#include <linux/bug.h> +#include <linux/compiler.h> +#include <linux/export.h> +#include <linux/string.h> #include <linux/list_sort.h> -#include <linux/slab.h> #include <linux/list.h> #define MAX_LIST_LENGTH_BITS 20
@@ -146,6 +148,7 @@ EXPORT_SYMBOL(list_sort);#ifdef CONFIG_TEST_LIST_SORT +#include <linux/slab.h> #include <linux/random.h> /*
Signature · 1 line
2.1.3
PATCH 4/19 lib/halfmd4.c: Simplify includes
In reply to Rasmus VillemoesWe only need EXPORT_SYMBOL, so compiler.h and export.h suffice. This means linux/types.h is no longer implicitly included, so add an include of uapi/linux/types.h to linux/cryptohash.h for __u32. Other users of cryptohash.h cannot be affected, since they must already have been including uapi/linux/types.h in order for gcc not to complain about unknown types.
--- include/linux/cryptohash.h | 2 ++ lib/halfmd4.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/cryptohash.h +2 -0
@@ -1,6 +1,8 @@#ifndef __CRYPTOHASH_H#define __CRYPTOHASH_H +#include <uapi/linux/types.h> +#define SHA_DIGEST_WORDS 5 #define SHA_MESSAGE_BYTES (512 /*bits*/ / 8)#define SHA_WORKSPACE_WORDS 16
lib/halfmd4.c +1 -1
@@ -1,4 +1,4 @@ -#include <linux/kernel.h> +#include <linux/compiler.h> #include <linux/export.h> #include <linux/cryptohash.h>
Signature · 1 line
2.1.3
PATCH 3/19 lib/dynamic_queue_limits.c: Simplify includes
In reply to Rasmus VillemoesThe file doesn't use anything from ctype.h. Instead of module.h, just use export.h for EXPORT_SYMBOL. The latter requires the user to include compiler.h, so do that explicitly instead of relying on some other header pulling it in.
--- lib/dynamic_queue_limits.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/dynamic_queue_limits.c +2 -2
@@ -3,12 +3,12 @@* * Copyright (c) 2011, Tom Herbert <[email protected]> */ -#include <linux/module.h> #include <linux/types.h> -#include <linux/ctype.h> #include <linux/kernel.h> #include <linux/jiffies.h> #include <linux/dynamic_queue_limits.h> +#include <linux/compiler.h> +#include <linux/export.h> #define POSDIFF (A ,B ) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)#define AFTER_EQ (A ,B ) ((int)((A) - (B)) >= 0)
Signature · 1 line
2.1.3
PATCH 1/19 lib/interval_tree.c: Simplify includes
In reply to Rasmus VillemoesThe file uses nothing from init.h, and also doesn't need the full module.h machinery; export.h is sufficient. The latter requires the user to ensure compiler.h is included, so do that explicitly instead of relying on some other header pulling it in.
--- lib/interval_tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/interval_tree.c +2 -2
@@ -1,7 +1,7 @@ -#include <linux/init.h> #include <linux/interval_tree.h> #include <linux/interval_tree_generic.h> -#include <linux/module.h> +#include <linux/compiler.h> +#include <linux/export.h> #define START (node) ((node)->start)#define LAST (node) ((node)->last)
Signature · 1 line
2.1.3