linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Lorenzo Stoakes' <lstoakes@gmail.com>, Lu Hongfei <luhongfei@vivo.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Christoph Hellwig <hch@infradead.org>,
	"open list:VMALLOC" <linux-mm@kvack.org>,
	open list <linux-kernel@vger.kernel.org>,
	"opensource.kernel@vivo.com" <opensource.kernel@vivo.com>
Subject: RE: [PATCH] mm/vmalloc: Replace the ternary conditional operator with min()
Date: Sat, 10 Jun 2023 20:09:28 +0000	[thread overview]
Message-ID: <f53f28de489f4c209776e404323ef5a1@AcuMS.aculab.com> (raw)
In-Reply-To: <3fc87d60-4e81-4f49-95f0-0503ad5cdf35@lucifer.local>

From: Lorenzo Stoakes
> Sent: 09 June 2023 09:49
> On Fri, Jun 09, 2023 at 08:09:45AM +0100, Lorenzo Stoakes wrote:
> > On Fri, Jun 09, 2023 at 02:13:09PM +0800, Lu Hongfei wrote:
> > > It would be better to replace the traditional ternary conditional
> > > operator with min() in zero_iter
> > >
> > > Signed-off-by: Lu Hongfei <luhongfei@vivo.com>
> > > ---
> > >  mm/vmalloc.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> > > index 29077d61ff81..42df032e6c27
> > > --- a/mm/vmalloc.c
> > > +++ b/mm/vmalloc.c
> > > @@ -3571,7 +3571,7 @@ static size_t zero_iter(struct iov_iter *iter, size_t count)
> > >  	while (remains > 0) {
> > >  		size_t num, copied;
> > >
> > > -		num = remains < PAGE_SIZE ? remains : PAGE_SIZE;
> > > +		num = min(remains, PAGE_SIZE);
> 
> OK, as per the pedantic test bot, you'll need to change this to:-
> 
> num = min_t(size_t, remains, PAGE_SIZE);

There has to be a valid reason why min/max have strong type checks.
Using min_t() all the time is just subverting them and means that
bugs are more likely than if the extra tests in min() were absent.

The problem here is that size_t is 'unsigned int' but PAGE_SIZE
'unsigned long'.
A 'safe' change is min(remains + 0ULL, PAGE_SIZE).

But, in reality, min/max should always be valid when one
value is a constant between 0 and MAX_INT.
The constant just needs forcing to 'signed int' (eg assigning
to a temporary on that type) before the comparison (etc).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)



  reply	other threads:[~2023-06-10 20:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09  6:13 Lu Hongfei
2023-06-09  7:09 ` Lorenzo Stoakes
2023-06-09  8:48   ` Lorenzo Stoakes
2023-06-10 20:09     ` David Laight [this message]
2023-06-10 21:06       ` Lorenzo Stoakes
2023-06-10 22:08         ` Andrew Morton
2023-06-10 22:23           ` Lorenzo Stoakes
2023-06-10 22:29           ` David Laight
2023-06-10 22:18         ` David Laight
2023-06-10 22:35           ` Lorenzo Stoakes
2023-06-09  8:28 ` kernel test robot
2023-06-09  9:12 ` kernel test robot
2023-06-09  9:35 ` kernel test robot

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=f53f28de489f4c209776e404323ef5a1@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lstoakes@gmail.com \
    --cc=luhongfei@vivo.com \
    --cc=opensource.kernel@vivo.com \
    --cc=urezki@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