From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C049CC433DB for ; Mon, 8 Feb 2021 09:44:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6FEF364E8B for ; Mon, 8 Feb 2021 09:44:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231905AbhBHJnG convert rfc822-to-8bit (ORCPT ); Mon, 8 Feb 2021 04:43:06 -0500 Received: from eu-smtp-delivery-151.mimecast.com ([207.82.80.151]:55507 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231613AbhBHJab (ORCPT ); Mon, 8 Feb 2021 04:30:31 -0500 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-186-5nZVffcuP9aUc65kc1ph8w-1; Mon, 08 Feb 2021 09:28:26 +0000 X-MC-Unique: 5nZVffcuP9aUc65kc1ph8w-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) by AcuMS.aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Mon, 8 Feb 2021 09:28:25 +0000 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Mon, 8 Feb 2021 09:28:25 +0000 From: David Laight To: 'Thomas Gleixner' , Yejune Deng , "john.stultz@linaro.org" , "sboyd@kernel.org" CC: "linux-kernel@vger.kernel.org" , "yejune.deng@gmail.com" Subject: RE: [PATCH] ntp: use memset and offsetof init Thread-Topic: [PATCH] ntp: use memset and offsetof init Thread-Index: AQHW+/XmZ/hgh4r3dkyEzm1InFXTWKpN/+Qg Date: Mon, 8 Feb 2021 09:28:25 +0000 Message-ID: <0e0242149d664d76a663fcad853904bf@AcuMS.aculab.com> References: <20210120025114.16294-1-yejune.deng@gmail.com> <87k0rm8lmh.fsf@nanos.tec.linutronix.de> In-Reply-To: <87k0rm8lmh.fsf@nanos.tec.linutronix.de> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > > --- > > 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)