mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Thomas Gleixner' <tglx@linutronix.de>,
	Yejune Deng <yejune.deng@gmail.com>,
	"john.stultz@linaro.org" <john.stultz@linaro.org>,
	"sboyd@kernel.org" <sboyd@kernel.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"yejune.deng@gmail.com" <yejune.deng@gmail.com>
Subject: RE: [PATCH] ntp: use memset and offsetof init
Date: Mon, 8 Feb 2021 09:28:25 +0000	[thread overview]
Message-ID: <0e0242149d664d76a663fcad853904bf@AcuMS.aculab.com> (raw)
In-Reply-To: <87k0rm8lmh.fsf@nanos.tec.linutronix.de>

From: Thomas Gleixner
> Sent: 05 February 2021 17:34
> 
> On Wed, Jan 20 2021 at 10:51, Yejune Deng wrote:
> > In pps_fill_timex(), use memset and offsetof instead of '= 0'.
> >
> > Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
> > ---
> >  kernel/time/ntp.c | 13 +++++--------
> >  1 file changed, 5 insertions(+), 8 deletions(-)
> >
> > diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
> > index 87389b9e21ab..3416c0381104 100644
> > --- a/kernel/time/ntp.c
> > +++ b/kernel/time/ntp.c
> > @@ -225,14 +225,11 @@ static inline int is_error_status(int status)
> >  static inline void pps_fill_timex(struct __kernel_timex *txc)
> >  {
> >  	/* PPS is not implemented, so these are zero */
> > -	txc->ppsfreq	   = 0;
> > -	txc->jitter	   = 0;
> > -	txc->shift	   = 0;
> > -	txc->stabil	   = 0;
> > -	txc->jitcnt	   = 0;
> > -	txc->calcnt	   = 0;
> > -	txc->errcnt	   = 0;
> > -	txc->stbcnt	   = 0;
> > +	int offset, len;
> > +
> > +	offset = offsetof(struct __kernel_timex, ppsfreq);
> > +	len    = offsetof(struct __kernel_timex, tai) - offset;
> > +	memset(txc + offset, 0, len);
> 
> That zeros bytes at a memory location which is
> 
>      (offset) * sizeof(struct __kernel_timex)
> 
> bytes away from txc. How did this every boot?
> 
> And no, even if you fix that pointer math problem then this kind of
> calculation from the middle of a struct is error prone.

It is also, at best, a code size optimisation.
If memset() is actually called (not inlined) then you get a whole
lot of tests against the size and alignment before any writes
of zero happen - which will be the same ones as in the inline code.

It can be worth using memcpy to copy part of a structure
but usually for one with lots of small fields (especially bitfields).

	David

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


      reply	other threads:[~2021-02-08  9:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-20  2:51 Yejune Deng
2021-01-20  9:32 ` [kbuild] " Dan Carpenter
2021-01-26  7:44 ` [ntp] a29bace5d3: BUG:Bad_rss-counter_state_mm:#type:MM_FILEPAGES_val kernel test robot
2021-02-05 17:33 ` [PATCH] ntp: use memset and offsetof init Thomas Gleixner
2021-02-08  9:28   ` David Laight [this message]

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=0e0242149d664d76a663fcad853904bf@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=yejune.deng@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

all inboxes | Powered by JetHome®