* [PATCH v5] docs: Explain the desired position of function attributes
@ 2021-10-05 15:26 Kees Cook
2021-10-05 15:39 ` Joe Perches
2021-10-12 19:35 ` Jonathan Corbet
0 siblings, 2 replies; 6+ messages in thread
From: Kees Cook @ 2021-10-05 15:26 UTC (permalink / raw)
To: Jonathan Corbet
Cc: Kees Cook, Randy Dunlap, Joe Perches, Alexey Dobriyan,
Nick Desaulniers, Linus Torvalds, Rasmus Villemoes,
Andrew Morton, linux-kernel, linux-doc, linux-hardening,
Miguel Ojeda
While discussing how to format the addition of various function
attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
close as possible to Linus's preferences for future reference.
(Though I note the dissent voiced by Joe Perches, Alexey Dobriyan, and
others that would prefer all attributes live on a separate leading line.)
[1] https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/
Signed-off-by: Kees Cook <keescook@chromium.org>
---
v5:
- drop extern (joe)
- fix __malloc position (miguel)
v4: https://lore.kernel.org/lkml/20210930235754.2635912-1-keescook@chromium.org
---
Documentation/process/coding-style.rst | 37 +++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 42969ab37b34..5756ff775233 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -480,13 +480,48 @@ closing function brace line. E.g.:
}
EXPORT_SYMBOL(system_is_up);
+6.1) Function prototypes
+************************
+
In function prototypes, include parameter names with their data types.
Although this is not required by the C language, it is preferred in Linux
because it is a simple way to add valuable information for the reader.
-Do not use the ``extern`` keyword with function prototypes as this makes
+Do not use the ``extern`` keyword with function declarations as this makes
lines longer and isn't strictly necessary.
+When writing function prototypes, please keep the `order of elements regular
+<https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/>`_.
+For example, using this function declaration example::
+
+ __init void * __must_check action(enum magic value, size_t size, u8 count,
+ char *fmt, ...) __printf(4, 5) __malloc;
+
+The preferred order of elements for a function prototype is:
+
+- storage class (below, ``static __always_inline``, noting that ``__always_inline``
+ is technically an attribute but is treated like ``inline``)
+- storage class attributes (here, ``__init`` -- i.e. section declarations, but also
+ things like ``__cold``)
+- return type (here, ``void *``)
+- return type attributes (here, ``__must_check``)
+- function name (here, ``action``)
+- function parameters (here, ``(enum magic value, size_t size, u8 count, char *fmt, ...)``,
+ noting that parameter names should always be included)
+- function parameter attributes (here, ``__printf(4, 5)``)
+- function behavior attributes (here, ``__malloc``)
+
+Note that for a function **definition** (i.e. the actual function body),
+the compiler does not allow function parameter attributes after the
+function parameters. In these cases, they should go after the storage
+class attributes (e.g. note the changed position of ``__printf(4, 5)``
+below, compared to the **declaration** example above)::
+
+ static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
+ size_t size, u8 count, char *fmt, ...) __malloc
+ {
+ ...
+ }
7) Centralized exiting of functions
-----------------------------------
--
2.30.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v5] docs: Explain the desired position of function attributes
2021-10-05 15:26 [PATCH v5] docs: Explain the desired position of function attributes Kees Cook
@ 2021-10-05 15:39 ` Joe Perches
2021-10-05 17:04 ` Kees Cook
2021-10-12 19:35 ` Jonathan Corbet
1 sibling, 1 reply; 6+ messages in thread
From: Joe Perches @ 2021-10-05 15:39 UTC (permalink / raw)
To: Kees Cook, Jonathan Corbet
Cc: Randy Dunlap, Alexey Dobriyan, Nick Desaulniers, Linus Torvalds,
Rasmus Villemoes, Andrew Morton, linux-kernel, linux-doc,
linux-hardening, Miguel Ojeda
On Tue, 2021-10-05 at 08:26 -0700, Kees Cook wrote:
> While discussing how to format the addition of various function
> attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
> close as possible to Linus's preferences for future reference.
> +For example, using this function declaration example::
> +
> + __init void * __must_check action(enum magic value, size_t size, u8 count,
> + char *fmt, ...) __printf(4, 5) __malloc;
trivia: almost all fmt declarations should be const char *
> +Note that for a function **definition** (i.e. the actual function body),
> +the compiler does not allow function parameter attributes after the
> +function parameters. In these cases, they should go after the storage
> +class attributes (e.g. note the changed position of ``__printf(4, 5)``
> +below, compared to the **declaration** example above)::
> +
> + static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
> + size_t size, u8 count, char *fmt, ...) __malloc
here too, and 80 columns?
> + {
> + ...
> + }
Or just put all the attributes before the storage class... <grumble/chuckle>
cheers, Joe
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v5] docs: Explain the desired position of function attributes
2021-10-05 15:39 ` Joe Perches
@ 2021-10-05 17:04 ` Kees Cook
2021-10-05 19:15 ` Randy Dunlap
0 siblings, 1 reply; 6+ messages in thread
From: Kees Cook @ 2021-10-05 17:04 UTC (permalink / raw)
To: Joe Perches
Cc: Jonathan Corbet, Randy Dunlap, Alexey Dobriyan, Nick Desaulniers,
Linus Torvalds, Rasmus Villemoes, Andrew Morton, linux-kernel,
linux-doc, linux-hardening, Miguel Ojeda
On Tue, Oct 05, 2021 at 08:39:14AM -0700, Joe Perches wrote:
> On Tue, 2021-10-05 at 08:26 -0700, Kees Cook wrote:
> > While discussing how to format the addition of various function
> > attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
> > close as possible to Linus's preferences for future reference.
> > +For example, using this function declaration example::
> > +
> > + __init void * __must_check action(enum magic value, size_t size, u8 count,
> > + char *fmt, ...) __printf(4, 5) __malloc;
>
> trivia: almost all fmt declarations should be const char *
Heh, good point!
> > +Note that for a function **definition** (i.e. the actual function body),
> > +the compiler does not allow function parameter attributes after the
> > +function parameters. In these cases, they should go after the storage
> > +class attributes (e.g. note the changed position of ``__printf(4, 5)``
> > +below, compared to the **declaration** example above)::
> > +
> > + static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
> > + size_t size, u8 count, char *fmt, ...) __malloc
>
> here too, and 80 columns?
Kernel standard is now 100. *shrug*
> > + {
> > + ...
> > + }
>
> Or just put all the attributes before the storage class... <grumble/chuckle>
I hear ya...
--
Kees Cook
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v5] docs: Explain the desired position of function attributes
2021-10-05 17:04 ` Kees Cook
@ 2021-10-05 19:15 ` Randy Dunlap
2021-10-06 0:51 ` Joe Perches
0 siblings, 1 reply; 6+ messages in thread
From: Randy Dunlap @ 2021-10-05 19:15 UTC (permalink / raw)
To: Kees Cook, Joe Perches
Cc: Jonathan Corbet, Alexey Dobriyan, Nick Desaulniers,
Linus Torvalds, Rasmus Villemoes, Andrew Morton, linux-kernel,
linux-doc, linux-hardening, Miguel Ojeda
On 10/5/21 10:04 AM, Kees Cook wrote:
> On Tue, Oct 05, 2021 at 08:39:14AM -0700, Joe Perches wrote:
>> On Tue, 2021-10-05 at 08:26 -0700, Kees Cook wrote:
>>> While discussing how to format the addition of various function
>>> attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
>>> close as possible to Linus's preferences for future reference.
>>> +For example, using this function declaration example::
>>> +
>>> + __init void * __must_check action(enum magic value, size_t size, u8 count,
>>> + char *fmt, ...) __printf(4, 5) __malloc;
>>
>> trivia: almost all fmt declarations should be const char *
>
> Heh, good point!
>
>>> +Note that for a function **definition** (i.e. the actual function body),
>>> +the compiler does not allow function parameter attributes after the
>>> +function parameters. In these cases, they should go after the storage
>>> +class attributes (e.g. note the changed position of ``__printf(4, 5)``
>>> +below, compared to the **declaration** example above)::
>>> +
>>> + static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
>>> + size_t size, u8 count, char *fmt, ...) __malloc
>>
>> here too, and 80 columns?
>
> Kernel standard is now 100. *shrug*
That's more for exceptions, not the common rule.
AFAIUI.
>
>>> + {
>>> + ...
>>> + }
>>
>> Or just put all the attributes before the storage class... <grumble/chuckle>
>
> I hear ya...
>
--
~Randy
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v5] docs: Explain the desired position of function attributes
2021-10-05 19:15 ` Randy Dunlap
@ 2021-10-06 0:51 ` Joe Perches
0 siblings, 0 replies; 6+ messages in thread
From: Joe Perches @ 2021-10-06 0:51 UTC (permalink / raw)
To: Randy Dunlap, Kees Cook
Cc: Jonathan Corbet, Alexey Dobriyan, Nick Desaulniers,
Linus Torvalds, Rasmus Villemoes, Andrew Morton, linux-kernel,
linux-doc, linux-hardening, Miguel Ojeda
On Tue, 2021-10-05 at 12:15 -0700, Randy Dunlap wrote:
> On 10/5/21 10:04 AM, Kees Cook wrote:
> > On Tue, Oct 05, 2021 at 08:39:14AM -0700, Joe Perches wrote:
> > > On Tue, 2021-10-05 at 08:26 -0700, Kees Cook wrote:
> > > > While discussing how to format the addition of various function
> > > > attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
> > > > close as possible to Linus's preferences for future reference.
> > > > +For example, using this function declaration example::
> > > > +
> > > > + __init void * __must_check action(enum magic value, size_t size, u8 count,
> > > > + char *fmt, ...) __printf(4, 5) __malloc;
> > >
> > > trivia: almost all fmt declarations should be const char *
> >
> > Heh, good point!
> >
> > > > +Note that for a function **definition** (i.e. the actual function body),
> > > > +the compiler does not allow function parameter attributes after the
> > > > +function parameters. In these cases, they should go after the storage
> > > > +class attributes (e.g. note the changed position of ``__printf(4, 5)``
> > > > +below, compared to the **declaration** example above)::
> > > > +
> > > > + static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
> > > > + size_t size, u8 count, char *fmt, ...) __malloc
> > >
> > > here too, and 80 columns?
> >
> > Kernel standard is now 100. *shrug*
>
> That's more for exceptions, not the common rule.
> AFAIUI.
And for function definitions that are not static inline, when
separate function declarations exist, the function definition
does not need any attribute marking at all.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v5] docs: Explain the desired position of function attributes
2021-10-05 15:26 [PATCH v5] docs: Explain the desired position of function attributes Kees Cook
2021-10-05 15:39 ` Joe Perches
@ 2021-10-12 19:35 ` Jonathan Corbet
1 sibling, 0 replies; 6+ messages in thread
From: Jonathan Corbet @ 2021-10-12 19:35 UTC (permalink / raw)
To: Kees Cook
Cc: Kees Cook, Randy Dunlap, Joe Perches, Alexey Dobriyan,
Nick Desaulniers, Linus Torvalds, Rasmus Villemoes,
Andrew Morton, linux-kernel, linux-doc, linux-hardening,
Miguel Ojeda
Kees Cook <keescook@chromium.org> writes:
> While discussing how to format the addition of various function
> attributes, some "unwritten rules" of ordering surfaced[1]. Capture as
> close as possible to Linus's preferences for future reference.
>
> (Though I note the dissent voiced by Joe Perches, Alexey Dobriyan, and
> others that would prefer all attributes live on a separate leading line.)
>
> [1] https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/
>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> v5:
> - drop extern (joe)
> - fix __malloc position (miguel)
> v4: https://lore.kernel.org/lkml/20210930235754.2635912-1-keescook@chromium.org
> ---
> Documentation/process/coding-style.rst | 37 +++++++++++++++++++++++++-
> 1 file changed, 36 insertions(+), 1 deletion(-)
I've applied this, thanks.
jon
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-10-12 19:35 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-05 15:26 [PATCH v5] docs: Explain the desired position of function attributes Kees Cook
2021-10-05 15:39 ` Joe Perches
2021-10-05 17:04 ` Kees Cook
2021-10-05 19:15 ` Randy Dunlap
2021-10-06 0:51 ` Joe Perches
2021-10-12 19:35 ` Jonathan Corbet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).