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=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 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 47582C433ED for ; Fri, 9 Apr 2021 07:22:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0923E61177 for ; Fri, 9 Apr 2021 07:22:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229621AbhDIHWP (ORCPT ); Fri, 9 Apr 2021 03:22:15 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:15994 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229703AbhDIHWO (ORCPT ); Fri, 9 Apr 2021 03:22:14 -0400 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4FGqJh3jbvzvS38; Fri, 9 Apr 2021 15:19:48 +0800 (CST) Received: from [127.0.0.1] (10.69.38.196) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.498.0; Fri, 9 Apr 2021 15:21:53 +0800 Subject: Re: [PATCH 5/7] iio: core: simplify some devm functions To: Alexandru Ardelean CC: Jonathan Cameron , linux-iio , Lars-Peter Clausen , "Hennerich, Michael" , Peter Meerwald-Stadler , , Tian Tao References: <1617881896-3164-1-git-send-email-yangyicong@hisilicon.com> <1617881896-3164-6-git-send-email-yangyicong@hisilicon.com> From: Yicong Yang Message-ID: <9f1257a2-a69e-acbc-8c0a-2d2157274225@hisilicon.com> Date: Fri, 9 Apr 2021 15:21:53 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.69.38.196] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org On 2021/4/8 21:09, Alexandru Ardelean wrote: > On Thu, Apr 8, 2021 at 2:41 PM Yicong Yang wrote: >> >> Use devm_add_action_or_reset() instead of devres_alloc() and >> devres_add(), which works the same. This will simplify the >> code. There is no functional changes. >> >> Signed-off-by: Yicong Yang >> --- >> drivers/iio/industrialio-core.c | 43 +++++++++++++++-------------------------- >> 1 file changed, 16 insertions(+), 27 deletions(-) >> >> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c >> index 7db761a..2dfbed3 100644 >> --- a/drivers/iio/industrialio-core.c >> +++ b/drivers/iio/industrialio-core.c >> @@ -1627,9 +1627,9 @@ void iio_device_free(struct iio_dev *dev) >> } >> EXPORT_SYMBOL(iio_device_free); >> >> -static void devm_iio_device_release(struct device *dev, void *res) >> +static void devm_iio_device_release(void *iio_dev) >> { >> - iio_device_free(*(struct iio_dev **)res); >> + iio_device_free(iio_dev); >> } >> >> /** >> @@ -1645,20 +1645,17 @@ static void devm_iio_device_release(struct device *dev, void *res) >> */ >> struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) >> { >> - struct iio_dev **ptr, *iio_dev; >> - >> - ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), >> - GFP_KERNEL); >> - if (!ptr) >> - return NULL; >> + struct iio_dev *iio_dev; >> + int ret; >> >> iio_dev = iio_device_alloc(parent, sizeof_priv); >> - if (iio_dev) { >> - *ptr = iio_dev; >> - devres_add(parent, ptr); >> - } else { >> - devres_free(ptr); >> - } >> + if (!iio_dev) >> + return iio_dev; > > This is correct. > But the preference is usually: > if (!iio_dev) > return NULL; > since it returned iio_dev when failure before, so i just keep as is >> + >> + ret = devm_add_action_or_reset(parent, devm_iio_device_release, >> + iio_dev); >> + if (ret) >> + return ERR_PTR(ret); >> >> return iio_dev; >> } >> @@ -1889,29 +1886,21 @@ void iio_device_unregister(struct iio_dev *indio_dev) >> } >> EXPORT_SYMBOL(iio_device_unregister); >> >> -static void devm_iio_device_unreg(struct device *dev, void *res) >> +static void devm_iio_device_unreg(void *indio_dev) >> { >> - iio_device_unregister(*(struct iio_dev **)res); >> + iio_device_unregister(indio_dev); >> } >> >> int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, >> struct module *this_mod) >> { >> - struct iio_dev **ptr; >> int ret; >> >> - ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); >> - if (!ptr) >> - return -ENOMEM; >> - >> - *ptr = indio_dev; >> ret = __iio_device_register(indio_dev, this_mod); >> - if (!ret) >> - devres_add(dev, ptr); >> - else >> - devres_free(ptr); >> + if (ret) >> + return ret; >> >> - return ret; >> + return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev); >> } >> EXPORT_SYMBOL_GPL(__devm_iio_device_register); >> >> -- >> 2.8.1 >> > > . >