Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp213333imm; Wed, 18 Jul 2018 00:28:00 -0700 (PDT) X-Google-Smtp-Source: AAOMgpceAcn+HPWjSzogdXweXme/nnmjIxFHchXbh3DdncybG+1k4HeFhjnD3fvc+XKKJ6n3S4sJ X-Received: by 2002:a17:902:b48f:: with SMTP id y15-v6mr4876782plr.44.1531898880665; Wed, 18 Jul 2018 00:28:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531898880; cv=none; d=google.com; s=arc-20160816; b=fV69vTFyhWWZCvxOPuMWLIyV73/qKvZZnMBN3HISQTTO1c8Ug0zKrkI4nk8wEMCEO+ T6vkHMtuu7Gs6+6Tf8gsl6k6441uGsDOPh804TxO+Rx+N9QJabHb9+PKPDE/V0Eeod3O FJ2dz2zNFFEX35DeQaUmm8C+peIFrpzLESSa23W2GlzCjw2UiLn5rO0E74/K7t4qkIj/ UKhOnqRC60H1ucxdbKROTy/M75IFSOtN936voU7Y4FHy1rjtSzfqHFBCMDMXF9zRKXsP DZXuTuiZJrE6cmpWFV+vFCgNVuAq9ceR8e8yEQ50dhdr7nLWZpVeUREzZvXn2Pf3UyWk gksw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=zE4epP90XnU3nX0Iwt2mqi0Hzxp+D4gcRwpICVpSp2I=; b=lLIMVLhdnjYrhOH6zdR1Qdk3a17xVy84uFKZdTWJfqMaqcvCki0cLMxiG/xtDwRFsH 7EL6B1rtQqr7xmLK5Bxk+6NozQeL6I0P45YIehGSt6i+OfJovmDoHSU04pN6V2NLLFsr UKO4FSz2BDHFXoWMWeP5b41Hr7mmZoCPmjA/ceyVChAWX510zpHdRbsc6garq9a4EwI+ eA6dczfy6Xz2PMb5hUAkQXZCbsDKCa/M+zcanE1znmgjlJ78bY2/MEJS44g7JVb/q+D8 fJHSbtidOyAIFGrpIZuKxWnYVMC9STAsQ5oMIuve9U4CPqgjyLDGjhWOsK4lRLHghp3V UVsA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z13-v6si2885685pfc.118.2018.07.18.00.27.45; Wed, 18 Jul 2018 00:28:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730278AbeGRIC2 (ORCPT + 99 others); Wed, 18 Jul 2018 04:02:28 -0400 Received: from mail.bootlin.com ([62.4.15.54]:38430 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729095AbeGRIB4 (ORCPT ); Wed, 18 Jul 2018 04:01:56 -0400 Received: by mail.bootlin.com (Postfix, from userid 110) id 52395207AC; Wed, 18 Jul 2018 09:25:28 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost (unknown [88.191.26.124]) by mail.bootlin.com (Postfix) with ESMTPSA id 4B56D208C4; Wed, 18 Jul 2018 09:25:08 +0200 (CEST) Date: Wed, 18 Jul 2018 09:25:08 +0200 From: Alexandre Belloni To: Denis OSTERLAND Cc: "a.zummo@towertech.it" , "linux-kernel@vger.kernel.org" , "robh+dt@kernel.org" , "m.grzeschik@pengutronix.de" , "devicetree@vger.kernel.org" , "mark.rutland@arm.com" , "linux-rtc@vger.kernel.org" , "kernel@pengutronix.de" Subject: Re: [PATCH v4 1/5] rtc: sysfs: facilitate attribute add to rtc device Message-ID: <20180718072508.GG3211@piout.net> References: <20180710090710.9066-1-Denis.Osterland@diehl.com> <20180710090710.9066-2-Denis.Osterland@diehl.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180710090710.9066-2-Denis.Osterland@diehl.com> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On 10/07/2018 09:44:15+0000, Denis OSTERLAND wrote: > From: Denis Osterland > > This patches addresses following problem: > rtc_allocate_device > devm_device_add_group <-- kernel oops / null pointer, because > sysfs entry does not yet exist > rtc_register_device > rc = devm_device_add_group > if (rc) > return rc; <-- forbidden to return error code > after device register > > In case groups were not yet registered (kobj.state_in_sysfs == 0) > rtc_add_group just addes given group to dev.groups, > otherwise it uses devm_device_add_group. > > Signed-off-by: Denis Osterland > --- > drivers/rtc/rtc-core.h | 6 ++++++ > drivers/rtc/rtc-sysfs.c | 39 +++++++++++++++++++++++++++++++++++++++ > 2 files changed, 45 insertions(+) > > diff --git a/drivers/rtc/rtc-core.h b/drivers/rtc/rtc-core.h > index 0abf98983e13..81d1c3ce7a96 100644 > --- a/drivers/rtc/rtc-core.h > +++ b/drivers/rtc/rtc-core.h > @@ -40,9 +40,15 @@ static inline void rtc_proc_del_device(struct rtc_device *rtc) > > #ifdef CONFIG_RTC_INTF_SYSFS > const struct attribute_group **rtc_get_dev_attribute_groups(void); > +int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp); > #else > static inline const struct attribute_group **rtc_get_dev_attribute_groups(void) > { > return NULL; > } > +static inline > +int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) > +{ > + return 0; > +} > #endif > diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c > index 454da38c6012..9a177b8f761b 100644 > --- a/drivers/rtc/rtc-sysfs.c > +++ b/drivers/rtc/rtc-sysfs.c > @@ -317,3 +317,42 @@ const struct attribute_group **rtc_get_dev_attribute_groups(void) > { > return rtc_attr_groups; > } > + > +static size_t rtc_group_count(struct rtc_device *rtc) > +{ I don't feel this function is necessary, you can include it in __rtc_add_group > + const struct attribute_group **groups = rtc->dev.groups; > + size_t cnt = 0; > + > + if (groups) > + for (; *groups; groups++) > + cnt++; > + > + return cnt; > +} > + > +static inline int > +__rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) > +{ > + size_t cnt = rtc_group_count(rtc); > + const struct attribute_group **groups, **old; > + > + groups = devm_kzalloc(&rtc->dev, (cnt+2)*sizeof(*groups), GFP_KERNEL); Please use devm_kcalloc > + if (IS_ERR_OR_NULL(groups)) > + return PTR_ERR(groups); > + memcpy(groups, rtc->dev.groups, cnt*sizeof(*groups)); > + groups[cnt] = grp; > + > + old = rtc->dev.groups; > + rtc->dev.groups = groups; > + if (old != rtc_attr_groups) > + devm_kfree(&rtc->dev, old); > + > + return 0; > +} > + > +int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp) > +{ > + return rtc->dev.kobj.state_in_sysfs ? > + devm_device_add_group(&rtc->dev, grp) : > + __rtc_add_group(rtc, grp); Using devm_device_add_group after RTC registration is racy and should not be allowed. I would merge __rtc_add_group in rtc_add_group and return an error if rtc->registered is true. -- Alexandre Belloni, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com