auto, kbuild: flatten KBUILD_CFLAGS
Subsystems (4)
PATCH 1/8 auto, kbuild: flatten KBUILD_CFLAGS
Make it slightly easier to see what compiler options are added and removed (and not worry about column limit too!)
--- Makefile | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
Makefile +17 -5
@@ -554,11 +554,23 @@ LINUXINCLUDE := \ $(USERINCLUDE) KBUILD_AFLAGS := -D__ASSEMBLY__ -fno-PIE -KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \ - -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \ - -Werror=implicit-function-declaration -Werror=implicit-int \ - -Werror=return-type -Wno-format-security -funsigned-char \ - -std=gnu11 + +KBUILD_CFLAGS := +KBUILD_CFLAGS += -std=gnu11 +KBUILD_CFLAGS += -fshort-wchar +KBUILD_CFLAGS += -funsigned-char +KBUILD_CFLAGS += -fno-common +KBUILD_CFLAGS += -fno-PIE +KBUILD_CFLAGS += -fno-strict-aliasing +KBUILD_CFLAGS += -Wall +KBUILD_CFLAGS += -Wundef +KBUILD_CFLAGS += -Werror=implicit-function-declaration +KBUILD_CFLAGS += -Werror=implicit-int +KBUILD_CFLAGS += -Werror=return-type +KBUILD_CFLAGS += -Werror=strict-prototypes +KBUILD_CFLAGS += -Wno-format-security +KBUILD_CFLAGS += -Wno-trigraphs + KBUILD_CPPFLAGS := -D__KERNEL__ KBUILD_RUSTFLAGS := $(rust_common_flags) \ --target=$(objtree)/scripts/target.json \
Signature · 1 line
2.40.1
PATCH 2/8 auto: add "auto" keyword as alias for __auto_type
It has similar semantics to "auto" keyword from a language which can not be named on this mailing list, in particular:
{
int a;
const auto b = a; // const int b = a;
b = 1; // compile error
}
{
char a;
auto b = a; // char b = a;
// no integer promotions
static_assert(sizeof(b) == 1);
}
{
int a;
const auto p = &a; // int *const p = &a;
*p = 1; // works because const is applied only to top-level
}It can be used to save on macroexpansion inside macro forests which use typeof() somewhere deep enough. It is cool regardless.
gcc 5.1 supports __auto_type.
--- Documentation/process/coding-style.rst | 31 +++++++++++++++++++++----- include/linux/compiler_types.h | 2 ++ 2 files changed, 27 insertions(+), 6 deletions(-)
Documentation/process/coding-style.rst +25 -6
@@ -1018,7 +1018,26 @@ result. Typical examples would be functions that return pointers; they use NULL or the ERR_PTR mechanism to report failure. -17) Using bool +17) Using auto +-------------- + +Use ``auto`` macro-keyword (alias for ``__auto_type`` extension) in macros +with "evaluate argument once" idiom: + +.. code-block:: c + + #define min2(a, b) \ + ({ \ + auto a_ = (a); \ + auto b_ = (b); \ + a_ < b_ ? a_ : b_; \ + }) + +Read https://gcc.gnu.org/onlinedocs/gcc/Typeof.html before using ``auto`` or +changing anything with ``auto`` in it. + + +18) Using bool -------------- The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
@@ -1048,7 +1067,7 @@ readable alternative if the call-sites have naked true/false constants. Otherwise limited use of bool in structures and arguments can improve readability. -18) Don't re-invent the kernel macros +19) Don't re-invent the kernel macros ------------------------------------- The header file include/linux/kernel.h contains a number of macros that
@@ -1071,7 +1090,7 @@ need them. Feel free to peruse that header file to see what else is already defined that you shouldn't reproduce in your code. -19) Editor modelines and other cruft +20) Editor modelines and other cruft ------------------------------------ Some editors can interpret configuration information embedded in source files,
@@ -1105,7 +1124,7 @@ own custom mode, or may have some other magic method for making indentation work correctly. -20) Inline assembly +21) Inline assembly ------------------- In architecture-specific code, you may need to use inline assembly to interface
@@ -1137,7 +1156,7 @@ the next instruction in the assembly output: : /* outputs */ : /* inputs */ : /* clobbers */); -21) Conditional Compilation +22) Conditional Compilation --------------------------- Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
@@ -1186,7 +1205,7 @@ expression used. For instance: #endif /* CONFIG_SOMETHING */ -22) Do not crash the kernel +23) Do not crash the kernel --------------------------- In general, the decision to crash the kernel belongs to the user, rather
include/linux/compiler_types.h +2 -0
@@ -4,6 +4,8 @@#ifndef __ASSEMBLY__ +#define auto __auto_type +/* * Skipped when running bindgen due to a libclang issue; * see https://github.com/rust-lang/rust-bindgen/issues/2244.
Signature · 1 line
2.40.1
PATCH 3/8 auto, proc: use "auto" instead of quite chatty macros
In reply to Alexey Dobriyan--- fs/proc/inode.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-)
fs/proc/inode.c +8 -24
@@ -309,9 +309,7 @@ static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter)static ssize_t pde_read (struct proc_dir_entry *pde,struct file *file,char __user *buf,size_t count,loff_t *ppos) { - typeof_member(struct proc_ops, proc_read) read; - - read = pde->proc_ops->proc_read; + auto read = pde->proc_ops->proc_read;if (read)return read (file, buf, count, ppos);return -EIO ;
@@ -333,9 +331,7 @@ static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count,static ssize_t pde_write (struct proc_dir_entry *pde,struct file *file,const char __user *buf,size_t count,loff_t *ppos) { - typeof_member(struct proc_ops, proc_write) write; - - write = pde->proc_ops->proc_write; + auto write = pde->proc_ops->proc_write;if (write)return write (file, buf, count, ppos);return -EIO ;
@@ -357,9 +353,7 @@ static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_tstatic __poll_t pde_poll (struct proc_dir_entry *pde,struct file *file,struct poll_table_struct *pts) { - typeof_member(struct proc_ops, proc_poll) poll; - - poll = pde->proc_ops->proc_poll; + auto poll = pde->proc_ops->proc_poll;if (poll)return poll (file, pts);return DEFAULT_POLLMASK ;
@@ -381,9 +375,7 @@ static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)static long pde_ioctl (struct proc_dir_entry *pde,struct file *file,unsigned int cmd,unsigned long arg) { - typeof_member(struct proc_ops, proc_ioctl) ioctl; - - ioctl = pde->proc_ops->proc_ioctl; + auto ioctl = pde->proc_ops->proc_ioctl;if (ioctl)return ioctl (file, cmd, arg);return -ENOTTY ;
@@ -406,9 +398,7 @@ static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigne#ifdef CONFIG_COMPAT static long pde_compat_ioctl (struct proc_dir_entry *pde,struct file *file,unsigned int cmd,unsigned long arg) { - typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl; - - compat_ioctl = pde->proc_ops->proc_compat_ioctl; + auto compat_ioctl = pde->proc_ops->proc_compat_ioctl;if (compat_ioctl)return compat_ioctl (file, cmd, arg);return -ENOTTY ;
@@ -430,9 +420,7 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsignedstatic int pde_mmap (struct proc_dir_entry *pde,struct file *file,struct vm_area_struct *vma) { - typeof_member(struct proc_ops, proc_mmap) mmap; - - mmap = pde->proc_ops->proc_mmap; + auto mmap = pde->proc_ops->proc_mmap;if (mmap)return mmap (file, vma);return -EIO ;
@@ -457,9 +445,7 @@ pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned lo unsigned long len, unsigned long pgoff, unsigned long flags) { - typeof_member(struct proc_ops, proc_get_unmapped_area) get_area; - - get_area = pde->proc_ops->proc_get_unmapped_area; + auto get_area = pde->proc_ops->proc_get_unmapped_area;#ifdef CONFIG_MMU if (!get_area) get_area = current->mm->get_unmapped_area;
@@ -555,9 +541,7 @@ static int proc_reg_release(struct inode *inode, struct file *file)struct pde_opener *pdeo;if (pde_is_permanent (pde)) { - typeof_member(struct proc_ops, proc_release) release; - - release = pde->proc_ops->proc_release; + auto release = pde->proc_ops->proc_release;if (release) {return release (inode, file); }
Signature · 1 line
2.40.1
PATCH 4/8 auto: promote DIV64_U64_ROUND_UP macro to function
In reply to Alexey Dobriyanso that people don't convert it to "auto".
Second argument is cast to u64:
u64 _tmp = (d);
so it's safe to make it function argument of type u64.
First argument is used like this:
(ll) + _tmp - 1
which is
typeof(ll) + u64 - int
If u64 is "unsigned long long" then everything will be promoted to it, so making promotion earlier in function argument is OK.
If u64 is "unsigned long" and typeof(ll) is ranked lower than "unsigned long" then it will be promoted to "unsigned long", then it is OK to promote to u64 earlier as function argument.
If u64 is "unsigned long" and typeof(ll) is "long long" or "unsigned long long" then expression will be promoted to typeof(ll) but then everything will be truncated to "u64" when passed to div64_u64(). Thus having type wider that "u64" doesn't really matter.
--- include/linux/math64.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/math64.h +4 -2
@@ -298,8 +298,10 @@ u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div);* * Return: dividend / divisor rounded up */ -#define DIV64_U64_ROUND_UP (ll, d) \ - ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); }) +static inline u64 DIV64_U64_ROUND_UP (u64 ll,u64 d) +{ +return div64_u64 (ll + d -1 , d); +}/** * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
Signature · 1 line
2.40.1
PATCH 5/8 auto: promote DIV64_U64_ROUND_CLOSEST macro to function
In reply to Alexey DobriyanSame explanation as for DIV64_U64_ROUND_UP: div64_u64() wants "u64" in the first argument so using anything wider in calculations before that doesn't matter.
--- include/linux/math64.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/math64.h +4 -2
@@ -313,8 +313,10 @@ static inline u64 DIV64_U64_ROUND_UP(u64 ll, u64 d)* * Return: dividend / divisor rounded to nearest integer */ -#define DIV64_U64_ROUND_CLOSEST (dividend, divisor) \ - ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); }) +static inline u64 DIV64_U64_ROUND_CLOSEST (u64 dividend,u64 divisor) +{ +return div64_u64 (dividend + divisor /2 , divisor); +}/** * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer
Signature · 1 line
2.40.1
PATCH 6/8 auto: promote DIV_U64_ROUND_CLOSEST macro to function
In reply to Alexey DobriyanBoth arguments are cast to specific types, so moving cast earlier is OK.
--- include/linux/math64.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/math64.h +4 -2
@@ -328,8 +328,10 @@ static inline u64 DIV64_U64_ROUND_CLOSEST(u64 dividend, u64 divisor)* * Return: dividend / divisor rounded to nearest integer */ -#define DIV_U64_ROUND_CLOSEST (dividend, divisor) \ - ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) +static inline u64 DIV_U64_ROUND_CLOSEST (u64 dividend,u32 divisor) +{ +return div_u64 (dividend + divisor /2 , divisor); +}/** * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
Signature · 1 line
2.40.1
PATCH 7/8 auto: promote DIV_S64_ROUND_CLOSEST macro to function
In reply to Alexey Dobriyanso that people don't use "auto" in it.
Both arguments are cast to specific types so it's OK to move them to function arguments:
s64 __x = (dividend); s32 __d = (divisor);
Delete useless () while I'm at it.
--- include/linux/math64.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
include/linux/math64.h +10 -11
@@ -335,21 +335,20 @@ static inline u64 DIV_U64_ROUND_CLOSEST(u64 dividend, u32 divisor)/** * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer -* @dividend: signed 64bit dividend -* @divisor: signed 32bit divisor +* @x: signed 64bit dividend +* @d: signed 32bit divisor * * Divide signed 64bit dividend by signed 32bit divisor * and round to closest integer. * * Return: dividend / divisor rounded to nearest integer */ -#define DIV_S64_ROUND_CLOSEST (dividend, divisor)( \ -{ \ - s64 __x = (dividend); \ - s32 __d = (divisor); \ - ((__x > 0) == (__d > 0)) ? \ - div_s64((__x + (__d / 2)), __d) : \ - div_s64((__x - (__d / 2)), __d); \ -} \ -) +static inline s64 DIV_S64_ROUND_CLOSEST (s64 x,s32 d) +{ +if ((x >0 ) == (d >0 )) { +return div_s64 (x + d /2 , d); + }else { +return div_s64 (x - d /2 , d); + } +}#endif /* _LINUX_MATH64_H */
Signature · 1 line
2.40.1
Quoted 2 lines · On Thu, 18 May 2023 18:46:42 +0300 Alexey Dobriyan <[email protected]> wrote:
Make it slightly easier to see what compiler options are added and removed (and not worry about column limit too!)
I wish you'd cc'ed Linus.
Turning those four upper-cased macros into upper-cased inline functions is just sad. If we're going to do this we surely should go around and make them lower-case.
Quoted 10 lines · Andrew Morton
Quoted 2 lines · On Thu, 18 May 2023 18:46:42 +0300 Alexey Dobriyan <[email protected]> wrote:
Make it slightly easier to see what compiler options are added and removed (and not worry about column limit too!)
I wish you'd cc'ed Linus.
Turning those four upper-cased macros into upper-cased inline functions is just sad. If we're going to do this we surely should go around and make them lower-case.
I always wanted to rewrite division functions and get rid of our countless variants:
q = kdiv(n, d); q = kdiv3(n, d, &r);
if it gets in they will be renamed again :-)