public inbox for linux-nvme@lists.infradead.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: <shao.mingyin@zte.com.cn>
Cc: <hch@lst.de>, <sagi@grimberg.me>, <kch@nvidia.com>,
	<linux-nvme@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
	<yang.tao172@zte.com.cn>, <yang.yang29@zte.com.cn>,
	<xu.xin16@zte.com.cn>
Subject: Re: [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c)
Date: Wed, 19 Mar 2025 21:33:27 +0000	[thread overview]
Message-ID: <20250319213327.2bf94bc1@pumpkin> (raw)
In-Reply-To: <20250319210032.5d442b0c@pumpkin>

On Wed, 19 Mar 2025 21:00:32 +0000
David Laight <david.laight.linux@gmail.com> wrote:

> On Mon, 17 Mar 2025 15:39:09 +0800 (CST)
> <shao.mingyin@zte.com.cn> wrote:
> 
> > From: LiHaoran <li.haoran7@zte.com.cn>
> > 
> > This patch replaces max(a, min(b, c)) by clamp(b, a, c) in the nvme
> > driver. This improves the readability.
> > 
> > Signed-off-by: LiHaoran <li.haoran7@zte.com.cn>
> > Cc: ShaoMingyin <shao.mingyin@zte.com.cn>
> > ---
> >  drivers/nvme/target/nvmet.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
> > index fcf4f460dc9a..30804b0ca66e 100644
> > --- a/drivers/nvme/target/nvmet.h
> > +++ b/drivers/nvme/target/nvmet.h
> > @@ -819,7 +819,7 @@ static inline u8 nvmet_cc_iocqes(u32 cc)
> >  /* Convert a 32-bit number to a 16-bit 0's based number */
> >  static inline __le16 to0based(u32 a)
> >  {
> > -	return cpu_to_le16(max(1U, min(1U << 16, a)) - 1);
> > +	return cpu_to_le16(clamp(1U << 16, 1U, a) - 1);  
> 
> Swap the arguments into a sane order - clamp(a, 1, 1 << 16)

To clarify further it is clamp(val, lo, hi) and is only well defined if 'lo <= hi'.
In particular the order of the comparisons is not defined.
It might be val > hi ? hi : val < lo ? lo : val.
Which won't give the value you expect when 'a' is zero.

> 
> 	David
> 
> >  }
> > 
> >  static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)  
> 



  reply	other threads:[~2025-03-19 21:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-17  7:39 [PATCH] nvme: replace max(a, min(b, c)) by clamp(b, a, c) shao.mingyin
2025-03-18  2:57 ` Chaitanya Kulkarni
2025-03-19 21:00 ` David Laight
2025-03-19 21:33   ` David Laight [this message]
     [not found] <20250317170938569jgM2gAWy39rgQQbnOh0Vu@zte.com.cn>
2025-03-17  9:31 ` Krzysztof Kozlowski

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=20250319213327.2bf94bc1@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=hch@lst.de \
    --cc=kch@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    --cc=shao.mingyin@zte.com.cn \
    --cc=xu.xin16@zte.com.cn \
    --cc=yang.tao172@zte.com.cn \
    --cc=yang.yang29@zte.com.cn \
    /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