From: Andi Shyti <andi.shyti@kernel.org>
To: Jani Nikula <jani.nikula@intel.com>
Cc: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>,
Linus Walleij <linus.walleij@linaro.org>,
Lee Jones <lee@kernel.org>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Jonathan Corbet <corbet@lwn.net>,
workflows@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH 21/21] Docs: add Functions parameters order section
Date: Mon, 27 Oct 2025 19:22:31 +0100 [thread overview]
Message-ID: <xpo7v5l4nw5b3ijjgk4o63xukkeyv4bqp65gu47g4beszetczy@2xvdgy4u46tt> (raw)
In-Reply-To: <723c936f92352352c3b1a84b858d684f5b7a0834@intel.com>
Hi,
On Mon, Oct 27, 2025 at 11:02:48AM +0200, Jani Nikula wrote:
> On Sat, 25 Oct 2025, "Yury Norov (NVIDIA)" <yury.norov@gmail.com> wrote:
> > Standardize parameters ordering in some typical cases to minimize
> > confusion.
> >
> > Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> > ---
> > Documentation/process/coding-style.rst | 48 ++++++++++++++++++++++++++
> > 1 file changed, 48 insertions(+)
> >
> > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
> > index d1a8e5465ed9..dde24148305c 100644
> > --- a/Documentation/process/coding-style.rst
> > +++ b/Documentation/process/coding-style.rst
> > @@ -523,6 +523,54 @@ below, compared to the **declaration** example above)::
> > ...
> > }
> >
> > +6.2) Function parameters order
> > +------------------------------
> > +
> > +The order of parameters is important both for code generation and readability.
> > +Passing parameters in an unusual order is a common source of bugs. Listing
> > +them in standard widely adopted order helps to avoid confusion.
> > +
> > +Many ABIs put first function parameter and return value in R0. If your
> > +function returns one of its parameters, passing it at the very beginning
> > +would lead to a better code generation. For example::
> > +
> > + void *memset64(uint64_t *s, uint64_t v, size_t count);
> > + void *memcpy(void *dest, const void *src, size_t count);
> > +
> > +If your function doesn't propagate a parameter, but has a meaning of copying
> > +and/or processing data, the best practice is following the traditional order:
> > +destination, source, options, flags.
> > +
> > +for_each()-like iterators should take an enumerator the first. For example::
> > +
> > + for_each_set_bit(bit, mask, nbits);
> > + do_something(bit);
> > +
> > + list_for_each_entry(pos, head, member);
> > + do_something(pos);
> > +
> > +If function operates on a range or ranges of data, corresponding parameters
> > +may be described as ``start - end`` or ``start - size`` pairs. In both cases,
> > +the parameters should follow each other. For example::
> > +
> > + int
> > + check_range(unsigned long vstart, unsigned long vend,
> > + unsigned long kstart, unsigned long kend);
> > +
> > + static inline void flush_icache_range(unsigned long start, unsigned long end);
> > +
> > + static inline void flush_icache_user_page(struct vm_area_struct *vma,
> > + struct page *page,
> > + unsigned long addr, int len);
> > +
> > +Both ``start`` and ``end`` of the interval are inclusive.
> > +
> > +Describing intervals in order ``end - start`` is unfavorable. One notable
> > +example is the ``GENMASK(high, low)`` macro. While such a notation is popular
> > +in hardware context, particularly to describe registers structure, in context
> > +of software development it looks counter intuitive and confusing. Please switch
> > +to an equivalent ``BITS(low, high)`` version.
> > +
>
> GENMASK when used for defining hardware registers is completely fine,
> and *much* easier to deal with when you cross check against the specs
> that almost invariably define high:low.
I fully agree with Jani here! When coming into describing
registers my brain is hardwired to read values from left to
right, high-low.
Linus suggested also BITS(start_bit, n_bits) which, in my
opinion, complements what we already have.
We leave GENMASK to register mask descriptions and BITS to the
rest.
Andi
next prev parent reply other threads:[~2025-10-27 18:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20251025162858.305236-1-yury.norov@gmail.com>
2025-10-25 16:33 ` Yury Norov (NVIDIA)
2025-10-27 9:02 ` Jani Nikula
2025-10-27 16:08 ` Jeff Johnson
2025-10-27 18:22 ` Andi Shyti [this message]
2025-10-27 18:43 ` Randy Dunlap
[not found] <20251025164023.308884-1-yury.norov@gmail.com>
2025-10-25 16:40 ` Yury Norov (NVIDIA)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xpo7v5l4nw5b3ijjgk4o63xukkeyv4bqp65gu47g4beszetczy@2xvdgy4u46tt \
--to=andi.shyti@kernel.org \
--cc=corbet@lwn.net \
--cc=jani.nikula@intel.com \
--cc=lee@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=workflows@vger.kernel.org \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox