* [PATCH 2/8] auto: add "auto" keyword as alias for __auto_type
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 3/8] auto, proc: use "auto" instead of quite chatty macros Alexey Dobriyan
` (5 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
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 <adobriyan@gmail.com>
---
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
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 3/8] auto, proc: use "auto" instead of quite chatty macros
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 2/8] auto: add "auto" keyword as alias for __auto_type Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 4/8] auto: promote DIV64_U64_ROUND_UP macro to function Alexey Dobriyan
` (4 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
fs/proc/inode.c | 32 ++++++++------------------------
1 file changed, 8 insertions(+), 24 deletions(-)
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index f495fdb39151..88644891b876 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -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_t
static __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, unsigned
static 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);
}
--
2.40.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 4/8] auto: promote DIV64_U64_ROUND_UP macro to function
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 2/8] auto: add "auto" keyword as alias for __auto_type Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 3/8] auto, proc: use "auto" instead of quite chatty macros Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 5/8] auto: promote DIV64_U64_ROUND_CLOSEST " Alexey Dobriyan
` (3 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
so 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.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
include/linux/math64.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/include/linux/math64.h b/include/linux/math64.h
index 8b9191a2849e..7e0e2625dc35 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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
--
2.40.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 5/8] auto: promote DIV64_U64_ROUND_CLOSEST macro to function
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
` (2 preceding siblings ...)
2023-05-18 15:46 ` [PATCH 4/8] auto: promote DIV64_U64_ROUND_UP macro to function Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 6/8] auto: promote DIV_U64_ROUND_CLOSEST " Alexey Dobriyan
` (2 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
Same 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.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
include/linux/math64.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/include/linux/math64.h b/include/linux/math64.h
index 7e0e2625dc35..d8770c8a9cff 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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
--
2.40.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 6/8] auto: promote DIV_U64_ROUND_CLOSEST macro to function
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
` (3 preceding siblings ...)
2023-05-18 15:46 ` [PATCH 5/8] auto: promote DIV64_U64_ROUND_CLOSEST " Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 15:46 ` [PATCH 7/8] auto: promote DIV_S64_ROUND_CLOSEST " Alexey Dobriyan
2023-05-18 20:37 ` [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Andrew Morton
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
Both arguments are cast to specific types, so moving cast earlier is OK.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
include/linux/math64.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/include/linux/math64.h b/include/linux/math64.h
index d8770c8a9cff..3ab235c8a94c 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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
--
2.40.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 7/8] auto: promote DIV_S64_ROUND_CLOSEST macro to function
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
` (4 preceding siblings ...)
2023-05-18 15:46 ` [PATCH 6/8] auto: promote DIV_U64_ROUND_CLOSEST " Alexey Dobriyan
@ 2023-05-18 15:46 ` Alexey Dobriyan
2023-05-18 20:37 ` [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Andrew Morton
6 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-18 15:46 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, adobriyan
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 <adobriyan@gmail.com>
---
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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS
2023-05-18 15:46 [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Alexey Dobriyan
` (5 preceding siblings ...)
2023-05-18 15:46 ` [PATCH 7/8] auto: promote DIV_S64_ROUND_CLOSEST " Alexey Dobriyan
@ 2023-05-18 20:37 ` Andrew Morton
2023-05-19 11:15 ` Alexey Dobriyan
6 siblings, 1 reply; 9+ messages in thread
From: Andrew Morton @ 2023-05-18 20:37 UTC (permalink / raw)
To: Alexey Dobriyan; +Cc: linux-kernel
On Thu, 18 May 2023 18:46:42 +0300 Alexey Dobriyan <adobriyan@gmail.com> 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.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS
2023-05-18 20:37 ` [PATCH 1/8] auto, kbuild: flatten KBUILD_CFLAGS Andrew Morton
@ 2023-05-19 11:15 ` Alexey Dobriyan
0 siblings, 0 replies; 9+ messages in thread
From: Alexey Dobriyan @ 2023-05-19 11:15 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel
On Thu, May 18, 2023 at 01:37:32PM -0700, Andrew Morton wrote:
> On Thu, 18 May 2023 18:46:42 +0300 Alexey Dobriyan <adobriyan@gmail.com> 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 :-)
^ permalink raw reply [flat|nested] 9+ messages in thread