linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Joe Perches <joe@perches.com>
Cc: linux-kernel@vger.kernel.org,
	Pankaj Bharadiya <pankaj.laxminarayan.bharadiya@intel.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	"David S. Miller" <davem@davemloft.net>,
	Randy Dunlap <rdunlap@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	netdev@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v2 3/4] treewide: Use sizeof_member() macro
Date: Tue, 29 Oct 2019 15:30:18 -0700	[thread overview]
Message-ID: <201910291527.ED0E642@keescook> (raw)
In-Reply-To: <2231d5f0a82f880e6706e2d0f070328a029c9b21.camel@perches.com>

On Thu, Oct 10, 2019 at 04:50:27PM -0700, Joe Perches wrote:
> On Thu, 2019-10-10 at 16:23 -0700, Kees Cook wrote:
> > From: Pankaj Bharadiya <pankaj.laxminarayan.bharadiya@intel.com>
> > 
> > Replace all the occurrences of FIELD_SIZEOF() and sizeof_field() with
> > sizeof_member() except at places where these are defined. Later patches
> > will remove the unused definitions.
> > 
> > This patch is generated using following script:
> > 
> > EXCLUDE_FILES="include/linux/stddef.h|include/linux/kernel.h"
> > 
> > git grep -l -e "\bFIELD_SIZEOF\b" -e "\bsizeof_field\b" | while read file;
> > do
> > 
> > 	if [[ "$file" =~ $EXCLUDE_FILES ]]; then
> > 		continue
> > 	fi
> > 	sed -i  -e 's/\bFIELD_SIZEOF\b/sizeof_member/g' \
> > 		-e 's/\bsizeof_field\b/sizeof_member/g' \
> > 		$file;
> > done
> 
> While the sed works, a cocci script would perhaps
> be better as multi line argument realignment would
> also occur.
> 
> $ cat sizeof_member.cocci
> @@
> @@
> 
> -	FIELD_SIZEOF
> +	sizeof_member
> 
> @@
> @@
> 
> -	sizeof_field
> +	sizeof_member
> $
> 
> For instance, this sed produces:
> 
> diff --git a/crypto/adiantum.c b/crypto/adiantum.c
> @@ -435,10 +435,10 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm)
>  
>  	BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
>  		     sizeof(struct adiantum_request_ctx));
> -	subreq_size = max(FIELD_SIZEOF(struct adiantum_request_ctx,
> +	subreq_size = max(sizeof_member(struct adiantum_request_ctx,
>  				       u.hash_desc) +
>  			  crypto_shash_descsize(hash),
> -			  FIELD_SIZEOF(struct adiantum_request_ctx,
> +			  sizeof_member(struct adiantum_request_ctx,
>  				       u.streamcipher_req) +
>  			  crypto_skcipher_reqsize(streamcipher));
>  
> 
> where the cocci script produces:
> 
> --- crypto/adiantum.c
> +++ /tmp/cocci-output-22881-d8186c-adiantum.c
> @@ -435,11 +435,11 @@ static int adiantum_init_tfm(struct cryp
>  
>  	BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
>  		     sizeof(struct adiantum_request_ctx));
> -	subreq_size = max(FIELD_SIZEOF(struct adiantum_request_ctx,
> -				       u.hash_desc) +
> +	subreq_size = max(sizeof_member(struct adiantum_request_ctx,
> +					u.hash_desc) +
>  			  crypto_shash_descsize(hash),
> -			  FIELD_SIZEOF(struct adiantum_request_ctx,
> -				       u.streamcipher_req) +
> +			  sizeof_member(struct adiantum_request_ctx,
> +					u.streamcipher_req) +
>  			  crypto_skcipher_reqsize(streamcipher));
>  
>  	crypto_skcipher_set_reqsize(tfm,

I played with this a bit, and it seems Coccinelle can get this very very
wrong:

diff -u -p a/drivers/net/ethernet/mellanox/mlx5/core/fpga/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/fpga/ipsec.c
--- a/drivers/net/ethernet/mellanox/mlx5/core/fpga/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/fpga/ipsec.c
@@ -87,13 +87,13 @@ static const struct rhashtable_params rh
 	 * value is not constant during the lifetime
 	 * of the key object.
 	 */
-	.key_len = FIELD_SIZEOF(struct mlx5_fpga_ipsec_sa_ctx, hw_sa) -
-		   FIELD_SIZEOF(struct mlx5_ifc_fpga_ipsec_sa_v1, cmd),
+	.key_len = sizeof_member(struct mlx5_fpga_ipsec_sa_ctx, hw_sa) -
+	sizeof_member(struct mlx5_ifc_fpga_ipsec_sa_v1, cmd),
 	.key_offset = offsetof(struct mlx5_fpga_ipsec_sa_ctx, hw_sa) +
-		      FIELD_SIZEOF(struct mlx5_ifc_fpga_ipsec_sa_v1, cmd),
-	.head_offset = offsetof(struct mlx5_fpga_ipsec_sa_ctx, hash),
-	.automatic_shrinking = true,
-	.min_size = 1,
+		      sizeof_member(struct mlx5_ifc_fpga_ipsec_sa_v1, cmd),
+		      .head_offset = offsetof(struct mlx5_fpga_ipsec_sa_ctx, hash),
+		      .automatic_shrinking = true,
+		      .min_size = 1,
 };
 
 struct mlx5_fpga_ipsec {


So, since the sed is faster and causes fewer problems, I'll keep it
as-is.

-- 
Kees Cook

  parent reply	other threads:[~2019-10-29 22:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-10 23:23 [PATCH v2 0/4] treewide: Use sizeof_member() macro Kees Cook
2019-10-10 23:23 ` Kees Cook
2019-10-10 23:23 ` [PATCH v2 1/4] MIPS: OCTEON: Replace SIZEOF_FIELD() macro Kees Cook
2019-10-10 23:23   ` Kees Cook
2019-10-10 23:23 ` [PATCH v2 2/4] linux/stddef.h: Add sizeof_member() macro Kees Cook
2019-10-10 23:23   ` Kees Cook
2019-10-10 23:23 ` [PATCH v2 3/4] treewide: Use " Kees Cook
2019-10-10 23:50   ` Joe Perches
2019-10-10 23:50     ` Joe Perches
2019-10-29 22:30     ` Kees Cook [this message]
2019-10-29 22:30       ` Kees Cook
2019-10-10 23:23 ` [PATCH v2 4/4] include: Remove FIELD_SIZEOF() and sizeof_field() macros Kees Cook
2019-10-10 23:23   ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201910291527.ED0E642@keescook \
    --to=keescook@chromium.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=davem@davemloft.net \
    --cc=joe@perches.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pankaj.laxminarayan.bharadiya@intel.com \
    --cc=rdunlap@infradead.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).