diff mbox series

IB/core: Use sizeof_field() helper

Message ID 20200527144152.GA22605@embeddedor (mailing list archive)
State Accepted
Delegated to: Jason Gunthorpe
Headers show
Series IB/core: Use sizeof_field() helper | expand

Commit Message

Gustavo A. R. Silva May 27, 2020, 2:41 p.m. UTC
Make use of the sizeof_field() helper instead of an open-coded version.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/infiniband/core/sa_query.c     | 8 ++++----
 drivers/infiniband/core/uverbs_cmd.c   | 2 +-
 drivers/infiniband/core/uverbs_ioctl.c | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

Comments

Jason Gunthorpe May 27, 2020, 4:44 p.m. UTC | #1
On Wed, May 27, 2020 at 09:41:52AM -0500, Gustavo A. R. Silva wrote:
> Make use of the sizeof_field() helper instead of an open-coded version.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/infiniband/core/sa_query.c     | 8 ++++----
>  drivers/infiniband/core/uverbs_cmd.c   | 2 +-
>  drivers/infiniband/core/uverbs_ioctl.c | 2 +-
>  3 files changed, 6 insertions(+), 6 deletions(-)

What kind of tool are you using for this? It seems to miss a lot, I
added in a few others to this patch and applied it, thanks:

diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
index 8f70c5c38ab7c3..a2ed09a3c714a9 100644
--- a/drivers/infiniband/core/sa_query.c
+++ b/drivers/infiniband/core/sa_query.c
@@ -420,7 +420,7 @@ static const struct ib_field opa_path_rec_table[] = {
 
 #define MCMEMBER_REC_FIELD(field) \
 	.struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),	\
-	.struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field,	\
+	.struct_size_bytes   = sizeof_field(struct ib_sa_mcmember_rec, field),	\
 	.field_name          = "sa_mcmember_rec:" #field
 
 static const struct ib_field mcmember_rec_table[] = {
@@ -504,7 +504,7 @@ static const struct ib_field mcmember_rec_table[] = {
 
 #define SERVICE_REC_FIELD(field) \
 	.struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),	\
-	.struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,	\
+	.struct_size_bytes   = sizeof_field(struct ib_sa_service_rec, field),	\
 	.field_name          = "sa_service_rec:" #field
 
 static const struct ib_field service_rec_table[] = {
@@ -710,7 +710,7 @@ static const struct ib_field opa_classport_info_rec_table[] = {
 
 #define GUIDINFO_REC_FIELD(field) \
 	.struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),	\
-	.struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,	\
+	.struct_size_bytes   = sizeof_field(struct ib_sa_guidinfo_rec, field),	\
 	.field_name          = "sa_guidinfo_rec:" #field
 
 static const struct ib_field guidinfo_rec_table[] = {
diff --git a/drivers/infiniband/core/ud_header.c b/drivers/infiniband/core/ud_header.c
index 29a45d2f8898e1..d65d541b9a2587 100644
--- a/drivers/infiniband/core/ud_header.c
+++ b/drivers/infiniband/core/ud_header.c
@@ -41,7 +41,7 @@
 
 #define STRUCT_FIELD(header, field) \
 	.struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field),      \
-	.struct_size_bytes   = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
+	.struct_size_bytes   = sizeof_field(struct ib_unpacked_ ## header, field), \
 	.field_name          = #header ":" #field
 
 static const struct ib_field lrh_table[]  = {
diff --git a/include/rdma/uverbs_ioctl.h b/include/rdma/uverbs_ioctl.h
index 5bd2b037e9147c..0418d7bddf3e0c 100644
--- a/include/rdma/uverbs_ioctl.h
+++ b/include/rdma/uverbs_ioctl.h
@@ -420,9 +420,9 @@ struct uapi_definition {
 		.scope = UAPI_SCOPE_OBJECT,                                    \
 		.needs_fn_offset =                                             \
 			offsetof(struct ib_device_ops, ibdev_fn) +             \
-			BUILD_BUG_ON_ZERO(                                     \
-			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
-			    sizeof(void *)),				       \
+			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
+						       ibdev_fn) !=            \
+					  sizeof(void *)),                     \
 	}
 
 /*
@@ -435,9 +435,9 @@ struct uapi_definition {
 		.scope = UAPI_SCOPE_METHOD,                                    \
 		.needs_fn_offset =                                             \
 			offsetof(struct ib_device_ops, ibdev_fn) +             \
-			BUILD_BUG_ON_ZERO(                                     \
-			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
-			    sizeof(void *)),                                   \
+			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
+						       ibdev_fn) !=            \
+					  sizeof(void *)),                     \
 	}
 
 /* Call a function to determine if the entire object is supported or not */
Gustavo A. R. Silva May 27, 2020, 5:04 p.m. UTC | #2
On Wed, May 27, 2020 at 01:44:52PM -0300, Jason Gunthorpe wrote:
> On Wed, May 27, 2020 at 09:41:52AM -0500, Gustavo A. R. Silva wrote:
> > Make use of the sizeof_field() helper instead of an open-coded version.
> > 
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> >  drivers/infiniband/core/sa_query.c     | 8 ++++----
> >  drivers/infiniband/core/uverbs_cmd.c   | 2 +-
> >  drivers/infiniband/core/uverbs_ioctl.c | 2 +-
> >  3 files changed, 6 insertions(+), 6 deletions(-)
> 
> What kind of tool are you using for this? It seems to miss a lot, I
> added in a few others to this patch and applied it, thanks:
> 

Hi Jason,

I was just doing a "terrain recognition" with grep. I certainly
wasn't expecting spaces between the sizeof operator and the opening
parenthesis: "sizeof ((struct ib_sa_mcmember_rec *) 0)->field".

Also, I was just running grep in drivers/infiniband/

I'll run a cocci script next time...

Thanks for your feedback. :)
--
Gustavo

> diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
> index 8f70c5c38ab7c3..a2ed09a3c714a9 100644
> --- a/drivers/infiniband/core/sa_query.c
> +++ b/drivers/infiniband/core/sa_query.c
> @@ -420,7 +420,7 @@ static const struct ib_field opa_path_rec_table[] = {
>  
>  #define MCMEMBER_REC_FIELD(field) \
>  	.struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),	\
> -	.struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field,	\
> +	.struct_size_bytes   = sizeof_field(struct ib_sa_mcmember_rec, field),	\
>  	.field_name          = "sa_mcmember_rec:" #field
>  
>  static const struct ib_field mcmember_rec_table[] = {
> @@ -504,7 +504,7 @@ static const struct ib_field mcmember_rec_table[] = {
>  
>  #define SERVICE_REC_FIELD(field) \
>  	.struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),	\
> -	.struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,	\
> +	.struct_size_bytes   = sizeof_field(struct ib_sa_service_rec, field),	\
>  	.field_name          = "sa_service_rec:" #field
>  
>  static const struct ib_field service_rec_table[] = {
> @@ -710,7 +710,7 @@ static const struct ib_field opa_classport_info_rec_table[] = {
>  
>  #define GUIDINFO_REC_FIELD(field) \
>  	.struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),	\
> -	.struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,	\
> +	.struct_size_bytes   = sizeof_field(struct ib_sa_guidinfo_rec, field),	\
>  	.field_name          = "sa_guidinfo_rec:" #field
>  
>  static const struct ib_field guidinfo_rec_table[] = {
> diff --git a/drivers/infiniband/core/ud_header.c b/drivers/infiniband/core/ud_header.c
> index 29a45d2f8898e1..d65d541b9a2587 100644
> --- a/drivers/infiniband/core/ud_header.c
> +++ b/drivers/infiniband/core/ud_header.c
> @@ -41,7 +41,7 @@
>  
>  #define STRUCT_FIELD(header, field) \
>  	.struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field),      \
> -	.struct_size_bytes   = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
> +	.struct_size_bytes   = sizeof_field(struct ib_unpacked_ ## header, field), \
>  	.field_name          = #header ":" #field
>  
>  static const struct ib_field lrh_table[]  = {
> diff --git a/include/rdma/uverbs_ioctl.h b/include/rdma/uverbs_ioctl.h
> index 5bd2b037e9147c..0418d7bddf3e0c 100644
> --- a/include/rdma/uverbs_ioctl.h
> +++ b/include/rdma/uverbs_ioctl.h
> @@ -420,9 +420,9 @@ struct uapi_definition {
>  		.scope = UAPI_SCOPE_OBJECT,                                    \
>  		.needs_fn_offset =                                             \
>  			offsetof(struct ib_device_ops, ibdev_fn) +             \
> -			BUILD_BUG_ON_ZERO(                                     \
> -			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
> -			    sizeof(void *)),				       \
> +			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
> +						       ibdev_fn) !=            \
> +					  sizeof(void *)),                     \
>  	}
>  
>  /*
> @@ -435,9 +435,9 @@ struct uapi_definition {
>  		.scope = UAPI_SCOPE_METHOD,                                    \
>  		.needs_fn_offset =                                             \
>  			offsetof(struct ib_device_ops, ibdev_fn) +             \
> -			BUILD_BUG_ON_ZERO(                                     \
> -			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
> -			    sizeof(void *)),                                   \
> +			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
> +						       ibdev_fn) !=            \
> +					  sizeof(void *)),                     \
>  	}
>  
>  /* Call a function to determine if the entire object is supported or not */
diff mbox series

Patch

diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
index 5c878646ff62a..8f70c5c38ab7c 100644
--- a/drivers/infiniband/core/sa_query.c
+++ b/drivers/infiniband/core/sa_query.c
@@ -190,7 +190,7 @@  static u32 tid;
 
 #define PATH_REC_FIELD(field) \
 	.struct_offset_bytes = offsetof(struct sa_path_rec, field),	\
-	.struct_size_bytes   = sizeof((struct sa_path_rec *)0)->field,	\
+	.struct_size_bytes   = sizeof_field(struct sa_path_rec, field),	\
 	.field_name          = "sa_path_rec:" #field
 
 static const struct ib_field path_rec_table[] = {
@@ -292,7 +292,7 @@  static const struct ib_field path_rec_table[] = {
 	.struct_offset_bytes = \
 		offsetof(struct sa_path_rec, field), \
 	.struct_size_bytes   = \
-		sizeof((struct sa_path_rec *)0)->field,	\
+		sizeof_field(struct sa_path_rec, field),	\
 	.field_name          = "sa_path_rec:" #field
 
 static const struct ib_field opa_path_rec_table[] = {
@@ -552,7 +552,7 @@  static const struct ib_field service_rec_table[] = {
 
 #define CLASSPORTINFO_REC_FIELD(field) \
 	.struct_offset_bytes = offsetof(struct ib_class_port_info, field),	\
-	.struct_size_bytes   = sizeof((struct ib_class_port_info *)0)->field,	\
+	.struct_size_bytes   = sizeof_field(struct ib_class_port_info, field),	\
 	.field_name          = "ib_class_port_info:" #field
 
 static const struct ib_field ib_classport_info_rec_table[] = {
@@ -630,7 +630,7 @@  static const struct ib_field ib_classport_info_rec_table[] = {
 	.struct_offset_bytes =\
 		offsetof(struct opa_class_port_info, field),	\
 	.struct_size_bytes   = \
-		sizeof((struct opa_class_port_info *)0)->field,	\
+		sizeof_field(struct opa_class_port_info, field),	\
 	.field_name          = "opa_class_port_info:" #field
 
 static const struct ib_field opa_classport_info_rec_table[] = {
diff --git a/drivers/infiniband/core/uverbs_cmd.c b/drivers/infiniband/core/uverbs_cmd.c
index 4859ac0df17c7..2067a939788bd 100644
--- a/drivers/infiniband/core/uverbs_cmd.c
+++ b/drivers/infiniband/core/uverbs_cmd.c
@@ -3741,7 +3741,7 @@  static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs)
 #define UAPI_DEF_WRITE_IO(req, resp)                                           \
 	.write.has_resp = 1 +                                                  \
 			  BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) +    \
-			  BUILD_BUG_ON_ZERO(sizeof(((req *)0)->response) !=    \
+			  BUILD_BUG_ON_ZERO(sizeof_field(req, response) !=    \
 					    sizeof(u64)),                      \
 	.write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
 
diff --git a/drivers/infiniband/core/uverbs_ioctl.c b/drivers/infiniband/core/uverbs_ioctl.c
index 42c5696f03bd7..2d882c02387c1 100644
--- a/drivers/infiniband/core/uverbs_ioctl.c
+++ b/drivers/infiniband/core/uverbs_ioctl.c
@@ -137,7 +137,7 @@  EXPORT_SYMBOL(_uverbs_alloc);
 static bool uverbs_is_attr_cleared(const struct ib_uverbs_attr *uattr,
 				   u16 len)
 {
-	if (uattr->len > sizeof(((struct ib_uverbs_attr *)0)->data))
+	if (uattr->len > sizeof_field(struct ib_uverbs_attr, data))
 		return ib_is_buffer_cleared(u64_to_user_ptr(uattr->data) + len,
 					    uattr->len - len);