Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_kernel_cleanup [2025/07/01 13:05] – [The (working) cleanup scripts] rpjday | linux_kernel_cleanup [2025/07/02 13:03] (current) – [5. Finding allegedly "unused" header files] rpjday | ||
---|---|---|---|
Line 10: | Line 10: | ||
* identifying " | * identifying " | ||
* identifying "bad #if" preprocessor checks for non-existent Kconfig variables | * identifying "bad #if" preprocessor checks for non-existent Kconfig variables | ||
+ | * identifying allegedly " | ||
NOTE: Don't try to submit a mega-patch with as many similar patches as you possibly can; rather, submit patches on a subsystem by subsystem basis, to make the patches manageable so that they can be reviewed and approved by just the maintainers of that subsystem. | NOTE: Don't try to submit a mega-patch with as many similar patches as you possibly can; rather, submit patches on a subsystem by subsystem basis, to make the patches manageable so that they can be reviewed and approved by just the maintainers of that subsystem. | ||
Line 15: | Line 16: | ||
**// | **// | ||
- | ==== The (working) cleanup scripts ==== | + | ==== 1. Calculating the length of an array ==== |
- | + | ||
- | === 1. Calculating the length of an array === | + | |
A lot of kernel code needs to calculate the length of an array, frequently to iterate through all of its elements. There are two standard ways to do this in C language: | A lot of kernel code needs to calculate the length of an array, frequently to iterate through all of its elements. There are two standard ways to do this in C language: | ||
Line 71: | Line 70: | ||
Note that there some obvious examples of what the script is looking for, as well as some false positives. Note also how various files insist on re-inventing the test in defining a macro that does what is already in that Linux header file. | Note that there some obvious examples of what the script is looking for, as well as some false positives. Note also how various files insist on re-inventing the test in defining a macro that does what is already in that Linux header file. | ||
- | === 2. Check if something is a power of two === | + | ==== 2. Check if something is a power of two ==== |
Quite a lot of kernel code needs to check if an integer value is an exact power of two -- the general test for that is already defined in the header file '' | Quite a lot of kernel code needs to check if an integer value is an exact power of two -- the general test for that is already defined in the header file '' | ||
Line 136: | Line 135: | ||
so you need to be careful as to what you think any of that simplifies to. | so you need to be careful as to what you think any of that simplifies to. | ||
- | === 3. Finding " | + | ==== 3. Finding " |
Many kernel Kconfig files contain " | Many kernel Kconfig files contain " | ||
Line 204: | Line 203: | ||
In other words, the above " | In other words, the above " | ||
- | === 4. Find " | + | ==== 4. Find " |
This check refers to " | This check refers to " | ||
Line 217: | Line 216: | ||
</ | </ | ||
- | The standard approach here would be to check carefully that there are no references to that string anywhere in the source tree, and check the Git log to see if/when that symbol was removed, and why. | + | The standard approach here would be to check carefully that there are no references to that string anywhere in the source tree, and check the Git log to see if/when that symbol was removed, and why, and clean it up. |
Additional examples from the drivers/ directory: | Additional examples from the drivers/ directory: | ||
Line 282: | Line 281: | ||
done | done | ||
</ | </ | ||
+ | |||
+ | ==== 5. Finding allegedly " | ||
+ | |||
+ | " | ||
+ | |||
+ | One reason is that a source file was removed, but its associated supporting header file was overlooked and is still sitting there, now having no purpose in life. Another (quite common) reason is that many of those header files contain enums or macros for hex offsets for particular devices, so that even if nothing is including them at the moment, they still need to be preserved in case something needs all that content. | ||
+ | |||
+ | As a basic example of the current '' | ||
+ | |||
+ | < | ||
+ | ===== phy-mv-usb.h ===== | ||
+ | ./ | ||
+ | ===== sisusb_tables.h ===== | ||
+ | ./ | ||
+ | </ | ||
+ | |||
+ | The above tells us simply that there are two header files under that directory that appear to not be included from // | ||
+ | |||
+ | Here is the admittedly brute force script '' | ||
+ | |||
+ | < | ||
+ | #!/bin/sh | ||
+ | |||
+ | DIR=${1-*} | ||
+ | |||
+ | LONGHDRS=$(find ${DIR} -name " | ||
+ | |||
+ | HDRS="" | ||
+ | |||
+ | for h in ${LONGHDRS} ; do | ||
+ | HDRS=" | ||
+ | done | ||
+ | |||
+ | HDRS=$(for h in ${HDRS} ; do echo $h ; done | sort -u) | ||
+ | |||
+ | # Test that each header file is included from *somewhere*. | ||
+ | |||
+ | for h in ${HDRS} ; do | ||
+ | # echo " | ||
+ | egrep -rq " | ||
+ | echo "===== ${h} =====" | ||
+ | find . -name " | ||
+ | grep -rwH ${h} * | ||
+ | } | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | **EXERCISE**: |