2023-05-18 16:05:17

by Alexey Dobriyan

[permalink] [raw]
Subject: [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!)

Signed-off-by: Alexey Dobriyan <[email protected]>
---
Makefile | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index f836936fb4d8..10fcc64fcd1f 100644
--- a/Makefile
+++ b/Makefile
@@ -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 \
--
2.40.1



2023-05-18 16:08:34

by Alexey Dobriyan

[permalink] [raw]
Subject: [PATCH 7/8] auto: promote DIV_S64_ROUND_CLOSEST macro to function

so 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.

Signed-off-by: Alexey Dobriyan <[email protected]>
---
include/linux/math64.h | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/include/linux/math64.h b/include/linux/math64.h
index 3ab235c8a94c..7ecae251b0f4 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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 */
--
2.40.1


2023-05-18 16:09:48

by Alexey Dobriyan

[permalink] [raw]
Subject: [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.

Signed-off-by: Alexey Dobriyan <[email protected]>
---
Documentation/process/coding-style.rst | 31 +++++++++++++++++++++-----
include/linux/compiler_types.h | 2 ++
2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 6db37a46d305..5bfa605c8bac 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -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
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 547ea1ff806e..bf0ac2a04154 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -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.
--
2.40.1


2023-05-18 20:57:08

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS

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.


2023-05-19 11:42:18

by Alexey Dobriyan

[permalink] [raw]
Subject: Re: [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS

On Thu, May 18, 2023 at 01:37:32PM -0700, Andrew Morton wrote:
> 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 :-)