Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp1537267qtg; Tue, 21 Mar 2023 14:33:07 -0700 (PDT) X-Google-Smtp-Source: AK7set+9gavQp4zXbuo6kvly2gnJCJCES/h0/Cf7bzfaFhW522pPGGBdRSbkYkQMPtUmfyKy0bKf X-Received: by 2002:a05:6a20:7da6:b0:db:4a66:88b7 with SMTP id v38-20020a056a207da600b000db4a6688b7mr168945pzj.4.1679434387070; Tue, 21 Mar 2023 14:33:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679434387; cv=none; d=google.com; s=arc-20160816; b=0lBjb0x6tBQCyMM4erzTUj1oNShcPTS7tp4yVo0aNX9zu+8OBKdJVMET4FSJ5/1Frq fiSN/tRYBK1Aa0It1bLFiWv0M/5OhPsttPhGkkaIrcE9bKmxZX9roSuTyO/KH8k/6Hh9 z6xc6xZGHlsPAonzEAaKFiCKMCY141wqowBvunXPxVcvoO3wn//v6JBi9yQBfyBfB1me V8AXyg6CmwjTeEV+FNCq4AtV3+kTKnqU3cpqOnYKXh4vLbeUKw5p17LErZ6S0I33H+oF 9VGS5xvBdkt5Qb3LaQYnm1OlEaVbqicu3Wx4Zf5R30nWhCYabbDPyMNDVAm1PLACrnpo ki5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=6cymCq67neIvjclKAQVSM89ndPJUvh28PAk/YB/rfjw=; b=wGE9i3PXtF0CGk3ArSWiA7t2xT7lbQBuz7qaO2DqcjJNBDJd5xXJMsNmyppXOlt8ej vXp9nZz/mjmXmJEaGIpw9f7/Re8Lg7bTrspzL9uCfZ+JIUE+1b6To7LSWhtg7UD2U2P8 nrEDZ3Jy7AQib7iO6TWAr4eu2Sr37JE4M6USO1limlMNi1SWt2NKaCxVdiu4zW27AfJI 9k/JdwUOIIsf3Eh4LMy6/vEtDRzuJwwIzhbXGg+V50QTlJEZNSgaTzAIDxbFBmUFfQNt M4zyzzNNmcMNAARYmVd6dx7vnzEulUkdFJ3+HDA7/gtAm5u/IUJ+YUwMwhpL+G2Z9Dxn nSAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=NFw1sv41; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j25-20020a635519000000b0050303dea3f0si13684400pgb.572.2023.03.21.14.32.52; Tue, 21 Mar 2023 14:33:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=NFw1sv41; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230018AbjCUVaN (ORCPT + 99 others); Tue, 21 Mar 2023 17:30:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229671AbjCUVaI (ORCPT ); Tue, 21 Mar 2023 17:30:08 -0400 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 186C8F979 for ; Tue, 21 Mar 2023 14:29:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=date:from:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=6cymCq67neIvjclKAQVSM89ndPJUvh28PAk/YB/rfjw=; b=NFw1sv41L1xnnreazv7MPu80BpOVRHu9WK2Gyo2APJjw7PcQfshkXoAa iOzUsep2ULzZ+Y4qOox61+0luC8Wf3mSKgcYSnaAhd0OlbWjIxZGwAWKz m4myrltdbGVbC1vegXHFEMLZQtEMvnIGosBaIJqvqm9r+4ZW/zndeSOe3 o=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=julia.lawall@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="5.98,279,1673910000"; d="scan'208";a="50856107" Received: from 231.85.89.92.rev.sfr.net (HELO hadrien) ([92.89.85.231]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2023 22:29:25 +0100 Date: Tue, 21 Mar 2023 22:29:25 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Alex Elder cc: Menna Mahmoud , gregkh@linuxfoundation.org, outreachy@lists.linux.dev, johan@kernel.org, elder@kernel.org, linux-kernel@vger.kernel.org, linux-staging@lists.linux.dev Subject: Re: [PATCH v2] staging: greybus: use inline function for macros In-Reply-To: <5efa6e6d-8573-31de-639a-d15b2e9deca0@ieee.org> Message-ID: References: <20230321183456.10385-1-eng.mennamahmoud.mm@gmail.com> <2e869677-2693-6419-ea25-f0cc2efcf3dd@ieee.org> <5efa6e6d-8573-31de-639a-d15b2e9deca0@ieee.org> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 21 Mar 2023, Alex Elder wrote: > On 3/21/23 3:43 PM, Julia Lawall wrote: > > > > > > On Tue, 21 Mar 2023, Alex Elder wrote: > > > > > On 3/21/23 1:34 PM, Menna Mahmoud wrote: > > > > Convert `to_gbphy_dev` and `to_gbphy_driver` macros into a > > > > static inline function. > > > > > > > > It is not great to have macros that use the `container_of` macro, > > > > because from looking at the definition one cannot tell what type > > > > it applies to. > > > > > > > > One can get the same benefit from an efficiency point of view > > > > by making an inline function. > > > > > > > > Suggested-by: Julia Lawall > > > > Signed-off-by: Menna Mahmoud > > > > > > I'm sorry if this conflicts with what others have said. > > > > > > But the use of a macro (with a container_of() right-hand > > > side) to get at the structure containing a field pointer > > > is a widely-used idiom throughout the kernel. > > > > > > What you propose achieves the same result but I would > > > lean toward keeping it as a macro, mainly because it > > > is so common. > > > > Common is not necessarily good. Macros are less safe and less > > informative. > > I do agree that the inline function is better, and > is functionally equivalent (while being explicit > with types and avoiding any macro expansion funny > business). > > Do you think we should make changes like this throughout > the kernel (along the lines of the flexible array fixes, > to make things safer)? I don't think it's a terrible idea, > but it's likely a big undertaking and I predict push-back. I agree that it's not a terrible idea and that it's a big undertaking. The code would be a little safer if people had the habit of making functions, as it avoids the risk of parameterizing over the field name in the third argument. But the impact is probably lesser than for the flexible array fixes. But if we have the chance to clean up the staging code, at least, then why not. There do seem to be more than 100 definitions like: #define to_dove_clk(hw) container_of(hw, struct dove_clk, hw) where we have to hope that at all of the usage sites the argument is called hw. Probably anything else would cause a compiler error, but still it looks strange. julia > > Bottom line on this is that I don't think the proposed > change is wrong, but there is value in consistently > adhering to conventions. > > Others can comment; I have no real objection. > > -Alex > > > > > julia > > > > > > > > > > -Alex > > > > --- > > > > changes in v2: > > > > -send patch as a single patch. > > > > -edit the name of struct object. > > > > -edit commit message. > > > > --- > > > > drivers/staging/greybus/gbphy.h | 10 ++++++++-- > > > > 1 file changed, 8 insertions(+), 2 deletions(-) > > > > > > > > diff --git a/drivers/staging/greybus/gbphy.h > > > > b/drivers/staging/greybus/gbphy.h > > > > index d4a225b76338..e7ba232bada1 100644 > > > > --- a/drivers/staging/greybus/gbphy.h > > > > +++ b/drivers/staging/greybus/gbphy.h > > > > @@ -15,7 +15,10 @@ struct gbphy_device { > > > > struct list_head list; > > > > struct device dev; > > > > }; > > > > -#define to_gbphy_dev(d) container_of(d, struct gbphy_device, dev) > > > > +static inline struct gbphy_device *to_gbphy_dev(const struct device > > > > *_dev) > > > > +{ > > > > + return container_of(_dev, struct gbphy_device, dev); > > > > +} > > > > static inline void *gb_gbphy_get_data(struct gbphy_device *gdev) > > > > { > > > > @@ -43,7 +46,10 @@ struct gbphy_driver { > > > > struct device_driver driver; > > > > }; > > > > -#define to_gbphy_driver(d) container_of(d, struct gbphy_driver, driver) > > > > +static inline struct gbphy_driver *to_gbphy_driver(struct device_driver > > > > *drv) > > > > +{ > > > > + return container_of(drv, struct gbphy_driver, driver); > > > > +} > > > > int gb_gbphy_register_driver(struct gbphy_driver *driver, > > > > struct module *owner, const char > > > > *mod_name); > > > > > > > >