From patchwork Tue Jul 10 09:44:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Osterland-Heim X-Patchwork-Id: 941892 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-rtc-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=diehl.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41PyHS5b0Dz9rvt for ; Tue, 10 Jul 2018 19:55:16 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933115AbeGJJyL convert rfc822-to-8bit (ORCPT ); Tue, 10 Jul 2018 05:54:11 -0400 Received: from enterprise02.smtp.diehl.com ([193.201.238.220]:64394 "EHLO enterprise02.smtp.diehl.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933112AbeGJJyI (ORCPT ); Tue, 10 Jul 2018 05:54:08 -0400 X-$ESA-Groupmapping: true X-IronPort-AV: E=Sophos;i="5.51,334,1526335200"; d="scan'208";a="34013273" From: Denis OSTERLAND To: "a.zummo@towertech.it" , "alexandre.belloni@bootlin.com" CC: "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" , Denis OSTERLAND Subject: [PATCH v4 1/5] rtc: sysfs: facilitate attribute add to rtc device Thread-Topic: [PATCH v4 1/5] rtc: sysfs: facilitate attribute add to rtc device Thread-Index: AQHUGDKQ7gjvnwME1UyQbN8b3dTaxg== Date: Tue, 10 Jul 2018 09:44:15 +0000 Message-ID: <20180710090710.9066-2-Denis.Osterland@diehl.com> References: <20180710090710.9066-1-Denis.Osterland@diehl.com> In-Reply-To: <20180710090710.9066-1-Denis.Osterland@diehl.com> Accept-Language: de-DE, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Evolution 3.18.5.2-0ubuntu3.2 MIME-Version: 1.0 X-TrailerSkip: 1 X-GBS-PROC: PkB65aL1SqtESF35r/jQn2OESzdj0IA8yu1PnMgu0XgzlQ5mQv/arVdZ+xMfDDY0 X-TNEFEvaluated: 1 Content-Language: en-US Sender: linux-rtc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rtc@vger.kernel.org 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) +{ + 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); + 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); +}