Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4098866yba; Wed, 17 Apr 2019 04:42:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqwuSdcVn0gu2wjKJyS7UlkK4Ml6VN+8+IHrlzAz0lBwClIFIn8wo0y2E8rqX0xqa11WHHzE X-Received: by 2002:a63:8142:: with SMTP id t63mr78175732pgd.63.1555501363672; Wed, 17 Apr 2019 04:42:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555501363; cv=none; d=google.com; s=arc-20160816; b=IbxAS8aKfWoPcQgke6GehZGfNG2HulkwjU/BAf5gz5627zB+bKOmci1YM2ke+3DQ7W ZpnCgg/gC/SllNDsdrVHLG5wVsvW6hZq0GVJgoZL1/d01/HC66LHoq9HYhgejak0cran RYFzuu0W7RY+PlG811Xl9w+jk+0A9xWbTxKkbAeJnIibHEowNF9/nxonnxsfmvL3YMKC Wcpd58Qk5+wLYdyS8IkB1MCKIXupidyEigOZDPwwfzZxxTewALojGNWk24jqKJS8jDYr gknjseSSJtT4ueXOFS5O9RlzDqwfWpIBrduGl3IX7RlZPZ6ThrWK3acTmz3RAcvW+eVO ddLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:dkim-signature; bh=EDbRpCydC3S6vLeF7++IiUU+jHXgnsSgM309DC1HEbY=; b=pAz9iFZm//uoi2dIecgdK9/4TTuaKNQKNJJF0hVJmUqRe/Hzc0W+OHvpZmoa8x9tt7 LpfK1vTbCPK684Zo9DjM/Nn8HCZy7NrMfXzi1b70Ukh6mZ5IW7JnPmu8J85l1xFDeePX MjP7WK9+THjsSaiInaYL4w1zwpwJNddXLWSo+OHTp8qWEDjasv6SkjbxfXj+zYjN52Fa m5Coi2E2hTu5LH72xwIekebuLqMS1xO2vW8sXu0hQ0prtAVuEaHwoxr8WOUbeL4l4KM4 tUHR62F+7EmguitCJaFnNLQw0XPluoU/7hNk55Sn3k8S+3K1u4cOo++E5BQn4K9jBwCm Z/WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ogVeuicY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o35si50423962pgb.551.2019.04.17.04.42.28; Wed, 17 Apr 2019 04:42:43 -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; dkim=pass header.i=@linaro.org header.s=google header.b=ogVeuicY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731743AbfDQLlg (ORCPT + 99 others); Wed, 17 Apr 2019 07:41:36 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:41713 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725792AbfDQLlf (ORCPT ); Wed, 17 Apr 2019 07:41:35 -0400 Received: by mail-io1-f67.google.com with SMTP id v10so20272250iom.8 for ; Wed, 17 Apr 2019 04:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=EDbRpCydC3S6vLeF7++IiUU+jHXgnsSgM309DC1HEbY=; b=ogVeuicYeqvOQG1GVM4a47BREt035jGHJSznG2rAcem3fpMrUwO+oVlKTc7UXrV25v ZlGtNouiPwMoRjROZq+oKdhUfaBzQ4aT5RZ8GHstd8qQQTpUsGXI/lawcazeBPC7SMq7 vqW1u9XOSiY9ZESLBiIeR+8hzBs3qmwjnCysihWiWFIzOgzVf+vwTce7dNA74u3rXoyX 81oRLh1/oWCqNnHFx6469NPyAj6bEQvsRNC10U7qGVrtImtEZE26n/dVoMcGshGty5tK tMhq9RjnYl7emFeqXIKygKXvQypx4WDZ/qhEM/zkvt8GJSgGJsyJupu0QjwhdmSrqcNG P/8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=EDbRpCydC3S6vLeF7++IiUU+jHXgnsSgM309DC1HEbY=; b=bLJumNEJ74xEnm+RbzAq7C5052m7WiPGBU30rWYhLxrWxUAYQkVvVHh3UBIRaNJhop O3b1lXtjmhYJlIa6B5qubco2nQ29npjtEdp+8JF/uteCPN4jmP58q9KUz/hns5KVZMkP ceWulQ49qxYRuQdjn/1XdfrXOm1BAG0WorqyIJ0X/ZYbk9+gNIVtsncLKThwCaOGZIfU I8s3zjf114SwOTimZpXq4qNDmV/YLPU8OSCFxX7tuKBh9ti7v7iCbFhcRKVoiYdCsLqz 0NZxf8nR/9tt5XZ6i2FeQ3v08yBW5QZYmR3cMY6CRV2oQJI/vHvTdaJk5C2Hsm0C0PHG AAXA== X-Gm-Message-State: APjAAAXUM/SpC6oG7aYMfBH7yJQeYOaHrN4XW3cdwHbFXhWpJyWmNyFw BelONhvBAjHvjzBAEb8YFruvTMSyjhc= X-Received: by 2002:a5d:9947:: with SMTP id v7mr4236930ios.25.1555501294316; Wed, 17 Apr 2019 04:41:34 -0700 (PDT) Received: from [172.22.22.26] (c-71-195-29-92.hsd1.mn.comcast.net. [71.195.29.92]) by smtp.googlemail.com with ESMTPSA id v10sm19620620iob.30.2019.04.17.04.41.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 04:41:33 -0700 (PDT) Subject: Re: [greybus-dev] [PATCH v2] Staging: greybus: Cleanup in greybus driver To: Madhumitha Prabakaran , johan@kernel.org, elder@kernel.org, gregkh@linuxfoundation.org, greybus-dev@lists.linaro.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org References: <20190416221318.21161-1-madhumithabiw@gmail.com> From: Alex Elder Message-ID: Date: Wed, 17 Apr 2019 06:41:32 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190416221318.21161-1-madhumithabiw@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/16/19 5:13 PM, Madhumitha Prabakaran wrote: > Fix a blank line after structure declarations. Also, convert > macros into inline functions in order to maintain Linux kernel > coding style based on which the inline function is > preferable over the macro. Madhumitha, here is my explanation for why *not* to convert these container_of() macros to inline functions. It's not just because "we do this all over the kernel." The reason is that it actually does not improve the code. Inline functions are often better than macros because they are explicit about types, allowing the compiler to tell you if you are passing parameters of the right type, and possibly assigning results to objects of the right type. Here is the definition of the container_of() macro in : #define container_of(ptr, type, member) ({ \ const typeof(((type *)0)->member) * __mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); }) You see that ptr is explicitly assigned to a local variable of the type of the member field, so the compiler is able to check that assignment. And the return value is similarly cast to the type of the containing structure, so the type compatibility of the assignment can also be checked. It is assumed that where this macro is used, the caller knows it is passing an appropriate address. There is another thing about this particular definition make it just as good as an inline function. A macro expansion can result in one of its parameters being used more than once, and that allows those parameters to be *evaluated* more than once in a single statement, which can produce incorrect code. This macro is defined using the "statement expression" extension to C--where a compound statement is enclosed in parentheses--({ ... }). This allows a local variable to be used in the macro expansion, which avoids any reuse of the macro parameters which might cause side-effects. So anyway, I don't think there is any benefit to replacing macros like this that do container_of() with inline functions. -Alex > Blank line fixes are suggested by checkpatch.pl > > Signed-off-by: Madhumitha Prabakaran > > Changes in v2: > - To maintain consistency in driver greybus, all the instances of macro > with container_of are fixed in a single patch. > --- > drivers/staging/greybus/bundle.h | 6 +++++- > drivers/staging/greybus/control.h | 6 +++++- > drivers/staging/greybus/gbphy.h | 12 ++++++++++-- > drivers/staging/greybus/greybus.h | 6 +++++- > drivers/staging/greybus/hd.h | 6 +++++- > drivers/staging/greybus/interface.h | 6 +++++- > drivers/staging/greybus/module.h | 6 +++++- > drivers/staging/greybus/svc.h | 6 +++++- > 8 files changed, 45 insertions(+), 9 deletions(-) > > diff --git a/drivers/staging/greybus/bundle.h b/drivers/staging/greybus/bundle.h > index 8734d2055657..84956eedb1c4 100644 > --- a/drivers/staging/greybus/bundle.h > +++ b/drivers/staging/greybus/bundle.h > @@ -31,7 +31,11 @@ struct gb_bundle { > > struct list_head links; /* interface->bundles */ > }; > -#define to_gb_bundle(d) container_of(d, struct gb_bundle, dev) > + > +static inline struct gb_bundle *to_gb_bundle(struct device *d) > +{ > + return container_of(d, struct gb_bundle, dev); > +} > > /* Greybus "private" definitions" */ > struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id, > diff --git a/drivers/staging/greybus/control.h b/drivers/staging/greybus/control.h > index 3a29ec05f631..a681ef74e7fe 100644 > --- a/drivers/staging/greybus/control.h > +++ b/drivers/staging/greybus/control.h > @@ -24,7 +24,11 @@ struct gb_control { > char *vendor_string; > char *product_string; > }; > -#define to_gb_control(d) container_of(d, struct gb_control, dev) > + > +static inline struct gb_control *to_gb_control(struct device *d) > +{ > + return container_of(d, struct gb_control, dev); > +} > > struct gb_control *gb_control_create(struct gb_interface *intf); > int gb_control_enable(struct gb_control *control); > diff --git a/drivers/staging/greybus/gbphy.h b/drivers/staging/greybus/gbphy.h > index 99463489d7d6..20307f6dfcb9 100644 > --- a/drivers/staging/greybus/gbphy.h > +++ b/drivers/staging/greybus/gbphy.h > @@ -15,7 +15,11 @@ 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(struct device *d) > +{ > + return container_of(d, struct gbphy_device, dev); > +} > > static inline void *gb_gbphy_get_data(struct gbphy_device *gdev) > { > @@ -43,7 +47,11 @@ 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 *d) > +{ > + return container_of(d, struct gbphy_driver, driver); > +} > > int gb_gbphy_register_driver(struct gbphy_driver *driver, > struct module *owner, const char *mod_name); > diff --git a/drivers/staging/greybus/greybus.h b/drivers/staging/greybus/greybus.h > index d03ddb7c9df0..a82d5002b4d5 100644 > --- a/drivers/staging/greybus/greybus.h > +++ b/drivers/staging/greybus/greybus.h > @@ -64,7 +64,11 @@ struct greybus_driver { > > struct device_driver driver; > }; > -#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver) > + > +static inline struct greybus_driver *to_greybus_driver(struct device_driver *d) > +{ > + return container_of(d, struct greybus_driver, driver); > +} > > static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data) > { > diff --git a/drivers/staging/greybus/hd.h b/drivers/staging/greybus/hd.h > index 6cf024a20a58..de7c49d05266 100644 > --- a/drivers/staging/greybus/hd.h > +++ b/drivers/staging/greybus/hd.h > @@ -57,7 +57,11 @@ struct gb_host_device { > /* Private data for the host driver */ > unsigned long hd_priv[0] __aligned(sizeof(s64)); > }; > -#define to_gb_host_device(d) container_of(d, struct gb_host_device, dev) > + > +static inline struct gb_host_device *to_gb_host_device(struct device *d) > +{ > + return container_of(d, struct gb_host_device, dev); > +} > > int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id); > void gb_hd_cport_release_reserved(struct gb_host_device *hd, u16 cport_id); > diff --git a/drivers/staging/greybus/interface.h b/drivers/staging/greybus/interface.h > index 1c00c5bb3ec9..f86c0d596dbe 100644 > --- a/drivers/staging/greybus/interface.h > +++ b/drivers/staging/greybus/interface.h > @@ -63,7 +63,11 @@ struct gb_interface { > struct work_struct mode_switch_work; > struct completion mode_switch_completion; > }; > -#define to_gb_interface(d) container_of(d, struct gb_interface, dev) > + > +static inline struct gb_interface *to_gb_interface(struct device *d) > +{ > + return container_of(d, struct gb_interface, dev); > +} > > struct gb_interface *gb_interface_create(struct gb_module *module, > u8 interface_id); > diff --git a/drivers/staging/greybus/module.h b/drivers/staging/greybus/module.h > index b1ebcc6636db..c427b788b677 100644 > --- a/drivers/staging/greybus/module.h > +++ b/drivers/staging/greybus/module.h > @@ -22,7 +22,11 @@ struct gb_module { > > struct gb_interface *interfaces[0]; > }; > -#define to_gb_module(d) container_of(d, struct gb_module, dev) > + > +static inline struct gb_module *to_gb_module(struct device *d) > +{ > + return container_of(d, struct gb_module, dev); > +} > > struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id, > size_t num_interfaces); > diff --git a/drivers/staging/greybus/svc.h b/drivers/staging/greybus/svc.h > index ad01783bac9c..4e35ac9ed0ff 100644 > --- a/drivers/staging/greybus/svc.h > +++ b/drivers/staging/greybus/svc.h > @@ -52,7 +52,11 @@ struct gb_svc { > struct dentry *debugfs_dentry; > struct svc_debugfs_pwrmon_rail *pwrmon_rails; > }; > -#define to_gb_svc(d) container_of(d, struct gb_svc, dev) > + > +static inline struct gb_svc *to_gb_svc(struct device *d) > +{ > + return container_of(d, struct gb_svc, dev); > +} > > struct gb_svc *gb_svc_create(struct gb_host_device *hd); > int gb_svc_add(struct gb_svc *svc); >