Skip to contentSkip to conversation

lib: Some #include cleanups

PATCH New Rasmus Villemoes 1 participant 20 messages Dec 31, 2014

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

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
#2 lore raw

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
index 926d00429ed2..14fc1dfadb3f 100644
@@ -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
#5 lore raw

PATCH 5/19 lib/idr.c: Remove redundant include

In reply to Rasmus Villemoes

idr.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
index e654aebd5f80..5335c43adf46 100644
@@ -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
#6 lore raw

PATCH 6/19 lib/genalloc.c: Remove redundant include

In reply to Rasmus Villemoes

Removing 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
index 2e65d206b01c..17d8f58f6716 100644
@@ -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
#10 lore raw

PATCH 10/19 lib/kobject_uevent.c: Remove redundant include

In reply to Rasmus Villemoes

The 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
index 9ebf9e20de53..f6c2c1e7779c 100644
@@ -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
#16 lore raw

PATCH 16/19 lib/stmp_device.c: Replace module.h include

In reply to Rasmus Villemoes

stmp_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
index 8ac9bcc4289a..a904656f4fd7 100644
@@ -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
#15 lore raw

PATCH 15/19 lib/sort.c: Move include inside #if 0

In reply to Rasmus Villemoes

The 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
index 14fc1dfadb3f..43c9fe73ae2e 100644
@@ -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
#19 lore raw

PATCH 19/19 lib/lcm.c: Replace include

In reply to Rasmus Villemoes

We 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
index 51cc6b13cd52..e97dbd51e756 100644
@@ -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
#18 lore raw

PATCH 18/19 lib/percpu_ida.c: Remove redundant includes

In reply to Rasmus Villemoes

These 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
index 93d145e5539c..f75715131f20 100644
@@ -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
#17 lore raw

PATCH 17/19 lib/strncpy_from_user.c: Replace module.h include

In reply to Rasmus Villemoes

strncpy_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
index bb2b201d6ad0..e0af6ff73d14 100644
@@ -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
#14 lore raw

PATCH 14/19 lib/show_mem.c: Remove redundant include

In reply to Rasmus Villemoes

show_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
index 7de89f4a36cf..adc98e1825ba 100644
@@ -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
#13 lore raw

PATCH 13/19 lib/radix-tree.c: Change to simpler include

In reply to Rasmus Villemoes

The 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
index 3291a8e37490..3d2aa27b845b 100644
@@ -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
#12 lore raw

PATCH 12/19 lib/plist.c: Remove redundant include

In reply to Rasmus Villemoes

Removing 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
index d408e774b746..3a30c53db061 100644
@@ -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
#11 lore raw

PATCH 11/19 lib/nlattr.c: Remove redundant include

In reply to Rasmus Villemoes

nlattr.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
index 9c3e85ff0a6c..76a1b59523ab 100644
@@ -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
#9 lore raw

PATCH 9/19 lib/llist.c: Remove redundant include

In reply to Rasmus Villemoes

This 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
index f76196d07409..0b0e9779d675 100644
@@ -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
#8 lore raw

PATCH 8/19 lib/md5.c: Simplify include

In reply to Rasmus Villemoes

md5.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
index 958a3c15923c..bb0cd01d356d 100644
@@ -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
#7 lore raw

PATCH 7/19 lib/list_sort.c: Rearrange includes

In reply to Rasmus Villemoes

Memory 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
index 12bcba1c8612..b29015102698 100644
@@ -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
#4 lore raw

PATCH 4/19 lib/halfmd4.c: Simplify includes

In reply to Rasmus Villemoes

We 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
index 2cd9f1cf9fa3..f4754282c9c2 100644
@@ -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
index 66d0ee8b7776..a8fe6274a13c 100644
@@ -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
#3 lore raw

PATCH 3/19 lib/dynamic_queue_limits.c: Simplify includes

In reply to Rasmus Villemoes

The 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
index 0777c5a45fa0..f346715e2255 100644
@@ -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
#1 lore raw

PATCH 1/19 lib/interval_tree.c: Simplify includes

In reply to Rasmus Villemoes

The 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
index f367f9ad544c..c85f6600a5f8 100644
@@ -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