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=-6.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,T_DKIMWL_WL_HIGH 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 1AEABC31E40 for ; Tue, 11 Jun 2019 20:46:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CB75C204FD for ; Tue, 11 Jun 2019 20:46:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="VLItlOM/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406684AbfFKUqh (ORCPT ); Tue, 11 Jun 2019 16:46:37 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:41727 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406248AbfFKUqg (ORCPT ); Tue, 11 Jun 2019 16:46:36 -0400 Received: by mail-pf1-f193.google.com with SMTP id m30so7652532pff.8 for ; Tue, 11 Jun 2019 13:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=8BCrEWxENFI652njZuyMg7oU6st6d90fR1m0hvbHelQ=; b=VLItlOM/U0/csHcUVmJtFGAA/3ks5F+OEZrSeoslGcAAJmtSlj+iB0/ek7AEsZLbtT DpfHa5NTqWHmT15WxBDcExNmkFz9PBSNokh4XujZvPfdj5vxckL3Qf2i4YdJAdYbwQLI Lm9eq3+Z4l4jXm61ETvS4xKC3CSnt7fx8hw+c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=8BCrEWxENFI652njZuyMg7oU6st6d90fR1m0hvbHelQ=; b=ZFZtm7PfWgsYjuuS6eMKWHEYW+f/B5E0BaLGD424WLIpiDKEZ7amAVKl/fKC2vy4le iUUdUskCsNcR/RvggZRyiSDX0G664mDNoEOOer8sOWnCZPV9m1xbsQqBYQf8NZZhgo8J mcqpw7cXlwMe4hl8a7VG9N9qvmt2PLB6gPDx3VNoM1+A89zgfWWF6rcE63xhXAxbMiEX aoNv9k0kollwuunBrQHcxWOYooq0l0oxP+SxPd8MUVzaLYgEqGUO3Sd3mQ7u9cBfm8ke sSLPFjuKSoC++CeI90oDmJj0y4M7MOKsjUQ+tOpAKG81ysxwUDZcHnBHY3QjDaDNHYC+ 5Ajg== X-Gm-Message-State: APjAAAVoIhzeTHvo2TrqNTvjomh17qANvbifaiXOMQAiQPZDjZfp/qsV mBJCbiXhKvlz8XA7CFk7hRq9IA== X-Google-Smtp-Source: APXvYqxVmscktUxOjSNmw0CUlKoqQbpfH4xGysEDq5ymBOLXo2+kCSblhcFdRPP/wDhMPjZXRG2wfQ== X-Received: by 2002:a62:ae01:: with SMTP id q1mr27321750pff.219.1560285995689; Tue, 11 Jun 2019 13:46:35 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id d3sm17055548pfa.176.2019.06.11.13.46.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 11 Jun 2019 13:46:34 -0700 (PDT) Date: Tue, 11 Jun 2019 13:46:34 -0700 From: Kees Cook To: Shyam Saini Cc: kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, linux-ext4@vger.kernel.org, devel@lists.orangefs.org, linux-mm@kvack.org, linux-sctp@vger.kernel.org, bpf@vger.kernel.org, kvm@vger.kernel.org, mayhs11saini@gmail.com Subject: Re: [PATCH V2] include: linux: Regularise the use of FIELD_SIZEOF macro Message-ID: <201906111345.74AA9311F5@keescook> References: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 12, 2019 at 01:08:36AM +0530, Shyam Saini wrote: > In favour of FIELD_SIZEOF, this patch also deprecates other two similar > macros sizeof_field and SIZEOF_FIELD. > > For code compatibility reason, retain sizeof_field macro as a wrapper macro > to FIELD_SIZEOF Can you explain this part? First sentence says you want to remove sizeof_field, and the second says you're keeping it? I thought the point was to switch all of these to FIELD_SIZEOF()? -Kees > > Signed-off-by: Shyam Saini > --- > Changelog: > > V1->V2 > - Consolidate previous patch 1 and 2 into single patch > - For code compatibility reason, retain sizeof_field macro as a > wrapper macro to FIELD_SIZEOF > > arch/arm64/include/asm/processor.h | 10 +++++----- > arch/mips/cavium-octeon/executive/cvmx-bootmem.c | 9 +-------- > drivers/gpu/drm/i915/gvt/scheduler.c | 2 +- > drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c | 4 ++-- > fs/befs/linuxvfs.c | 2 +- > fs/ext2/super.c | 2 +- > fs/ext4/super.c | 2 +- > fs/freevxfs/vxfs_super.c | 2 +- > fs/orangefs/super.c | 2 +- > fs/ufs/super.c | 2 +- > include/linux/kernel.h | 9 --------- > include/linux/slab.h | 2 +- > include/linux/stddef.h | 17 ++++++++++++++--- > kernel/fork.c | 2 +- > kernel/utsname.c | 2 +- > net/caif/caif_socket.c | 2 +- > net/core/skbuff.c | 2 +- > net/ipv4/raw.c | 2 +- > net/ipv6/raw.c | 2 +- > net/sctp/socket.c | 4 ++-- > tools/testing/selftests/bpf/bpf_util.h | 22 +++++++++++++++++++--- > virt/kvm/kvm_main.c | 2 +- > 22 files changed, 58 insertions(+), 47 deletions(-) > > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index fcd0e691b1ea..ace906d887cc 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -164,13 +164,13 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset, > unsigned long *size) > { > /* Verify that there is no padding among the whitelisted fields: */ > - BUILD_BUG_ON(sizeof_field(struct thread_struct, uw) != > - sizeof_field(struct thread_struct, uw.tp_value) + > - sizeof_field(struct thread_struct, uw.tp2_value) + > - sizeof_field(struct thread_struct, uw.fpsimd_state)); > + BUILD_BUG_ON(FIELD_SIZEOF(struct thread_struct, uw) != > + FIELD_SIZEOF(struct thread_struct, uw.tp_value) + > + FIELD_SIZEOF(struct thread_struct, uw.tp2_value) + > + FIELD_SIZEOF(struct thread_struct, uw.fpsimd_state)); > > *offset = offsetof(struct thread_struct, uw); > - *size = sizeof_field(struct thread_struct, uw); > + *size = FIELD_SIZEOF(struct thread_struct, uw); > } > > #ifdef CONFIG_COMPAT > diff --git a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > index ba8f82a29a81..44b506a14666 100644 > --- a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > +++ b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > @@ -45,13 +45,6 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > /* See header file for descriptions of functions */ > > /** > - * This macro returns the size of a member of a structure. > - * Logically it is the same as "sizeof(s::field)" in C++, but > - * C lacks the "::" operator. > - */ > -#define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field) > - > -/** > * This macro returns a member of the > * cvmx_bootmem_named_block_desc_t structure. These members can't > * be directly addressed as they might be in memory not directly > @@ -65,7 +58,7 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \ > __cvmx_bootmem_desc_get(addr, \ > offsetof(struct cvmx_bootmem_named_block_desc, field), \ > - SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc, field)) > + FIELD_SIZEOF(struct cvmx_bootmem_named_block_desc, field)) > > /** > * This function is the implementation of the get macros defined > diff --git a/drivers/gpu/drm/i915/gvt/scheduler.c b/drivers/gpu/drm/i915/gvt/scheduler.c > index 0f919f0a43d4..820f95a52542 100644 > --- a/drivers/gpu/drm/i915/gvt/scheduler.c > +++ b/drivers/gpu/drm/i915/gvt/scheduler.c > @@ -1243,7 +1243,7 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) > sizeof(struct intel_vgpu_workload), 0, > SLAB_HWCACHE_ALIGN, > offsetof(struct intel_vgpu_workload, rb_tail), > - sizeof_field(struct intel_vgpu_workload, rb_tail), > + FIELD_SIZEOF(struct intel_vgpu_workload, rb_tail), > NULL); > > if (!s->workloads) { > diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > index 46baf3b44309..c0447bf07fbb 100644 > --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > @@ -49,13 +49,13 @@ struct mlxsw_sp_fid_8021d { > }; > > static const struct rhashtable_params mlxsw_sp_fid_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, fid_index), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, fid_index), > .key_offset = offsetof(struct mlxsw_sp_fid, fid_index), > .head_offset = offsetof(struct mlxsw_sp_fid, ht_node), > }; > > static const struct rhashtable_params mlxsw_sp_fid_vni_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, vni), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, vni), > .key_offset = offsetof(struct mlxsw_sp_fid, vni), > .head_offset = offsetof(struct mlxsw_sp_fid, vni_ht_node), > }; > diff --git a/fs/befs/linuxvfs.c b/fs/befs/linuxvfs.c > index 462d096ff3e9..06ffd4829e2e 100644 > --- a/fs/befs/linuxvfs.c > +++ b/fs/befs/linuxvfs.c > @@ -438,7 +438,7 @@ befs_init_inodecache(void) > SLAB_ACCOUNT), > offsetof(struct befs_inode_info, > i_data.symlink), > - sizeof_field(struct befs_inode_info, > + FIELD_SIZEOF(struct befs_inode_info, > i_data.symlink), > init_once); > if (befs_inode_cachep == NULL) > diff --git a/fs/ext2/super.c b/fs/ext2/super.c > index 1d7ab73b1014..d9a6c81f4a47 100644 > --- a/fs/ext2/super.c > +++ b/fs/ext2/super.c > @@ -220,7 +220,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext2_inode_info, i_data), > - sizeof_field(struct ext2_inode_info, i_data), > + FIELD_SIZEOF(struct ext2_inode_info, i_data), > init_once); > if (ext2_inode_cachep == NULL) > return -ENOMEM; > diff --git a/fs/ext4/super.c b/fs/ext4/super.c > index 4079605d437a..b1b5856248bd 100644 > --- a/fs/ext4/super.c > +++ b/fs/ext4/super.c > @@ -1148,7 +1148,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext4_inode_info, i_data), > - sizeof_field(struct ext4_inode_info, i_data), > + FIELD_SIZEOF(struct ext4_inode_info, i_data), > init_once); > if (ext4_inode_cachep == NULL) > return -ENOMEM; > diff --git a/fs/freevxfs/vxfs_super.c b/fs/freevxfs/vxfs_super.c > index a89f68c3cbed..ffd22f85bbe0 100644 > --- a/fs/freevxfs/vxfs_super.c > +++ b/fs/freevxfs/vxfs_super.c > @@ -329,7 +329,7 @@ vxfs_init(void) > sizeof(struct vxfs_inode_info), 0, > SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, > offsetof(struct vxfs_inode_info, vii_immed.vi_immed), > - sizeof_field(struct vxfs_inode_info, > + FIELD_SIZEOF(struct vxfs_inode_info, > vii_immed.vi_immed), > NULL); > if (!vxfs_inode_cachep) > diff --git a/fs/orangefs/super.c b/fs/orangefs/super.c > index ee5efdc35cc1..30f625059ad9 100644 > --- a/fs/orangefs/super.c > +++ b/fs/orangefs/super.c > @@ -646,7 +646,7 @@ int orangefs_inode_cache_initialize(void) > ORANGEFS_CACHE_CREATE_FLAGS, > offsetof(struct orangefs_inode_s, > link_target), > - sizeof_field(struct orangefs_inode_s, > + FIELD_SIZEOF(struct orangefs_inode_s, > link_target), > orangefs_inode_cache_ctor); > > diff --git a/fs/ufs/super.c b/fs/ufs/super.c > index 3d247c0d92aa..1e8bcd950f6d 100644 > --- a/fs/ufs/super.c > +++ b/fs/ufs/super.c > @@ -1469,7 +1469,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ufs_inode_info, i_u1.i_symlink), > - sizeof_field(struct ufs_inode_info, > + FIELD_SIZEOF(struct ufs_inode_info, > i_u1.i_symlink), > init_once); > if (ufs_inode_cachep == NULL) > diff --git a/include/linux/kernel.h b/include/linux/kernel.h > index 74b1ee9027f5..4672391cdb5b 100644 > --- a/include/linux/kernel.h > +++ b/include/linux/kernel.h > @@ -79,15 +79,6 @@ > */ > #define round_down(x, y) ((x) & ~__round_mask(x, y)) > > -/** > - * FIELD_SIZEOF - get the size of a struct's field > - * @t: the target struct > - * @f: the target struct's field > - * Return: the size of @f in the struct definition without having a > - * declared instance of @t. > - */ > -#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) > - > #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP > > #define DIV_ROUND_DOWN_ULL(ll, d) \ > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 9449b19c5f10..8bdfdd389b37 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -175,7 +175,7 @@ void memcg_destroy_kmem_caches(struct mem_cgroup *); > sizeof(struct __struct), \ > __alignof__(struct __struct), (__flags), \ > offsetof(struct __struct, __field), \ > - sizeof_field(struct __struct, __field), NULL) > + FIELD_SIZEOF(struct __struct, __field), NULL) > > /* > * Common kmalloc functions provided by all allocators > diff --git a/include/linux/stddef.h b/include/linux/stddef.h > index 998a4ba28eba..a5960e2b4a8b 100644 > --- a/include/linux/stddef.h > +++ b/include/linux/stddef.h > @@ -20,12 +20,23 @@ enum { > #endif > > /** > - * sizeof_field(TYPE, MEMBER) > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > + > +/* > + * For code compatibility > * > + * sizeof_field(TYPE, MEMBER) > * @TYPE: The structure containing the field of interest > * @MEMBER: The field to return the size of > */ > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > + > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > > /** > * offsetofend(TYPE, MEMBER) > @@ -34,6 +45,6 @@ enum { > * @MEMBER: The member within the structure to get the end offset of > */ > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > > #endif > diff --git a/kernel/fork.c b/kernel/fork.c > index 75675b9bf6df..ef40b95bf82c 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -2553,7 +2553,7 @@ void __init proc_caches_init(void) > mm_size, ARCH_MIN_MMSTRUCT_ALIGN, > SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct mm_struct, saved_auxv), > - sizeof_field(struct mm_struct, saved_auxv), > + FIELD_SIZEOF(struct mm_struct, saved_auxv), > NULL); > vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); > mmap_init(); > diff --git a/kernel/utsname.c b/kernel/utsname.c > index f0e491193009..28257c571553 100644 > --- a/kernel/utsname.c > +++ b/kernel/utsname.c > @@ -174,6 +174,6 @@ void __init uts_ns_init(void) > "uts_namespace", sizeof(struct uts_namespace), 0, > SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct uts_namespace, name), > - sizeof_field(struct uts_namespace, name), > + FIELD_SIZEOF(struct uts_namespace, name), > NULL); > } > diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c > index 13ea920600ae..3306bbed77eb 100644 > --- a/net/caif/caif_socket.c > +++ b/net/caif/caif_socket.c > @@ -1033,7 +1033,7 @@ static int caif_create(struct net *net, struct socket *sock, int protocol, > .owner = THIS_MODULE, > .obj_size = sizeof(struct caifsock), > .useroffset = offsetof(struct caifsock, conn_req.param), > - .usersize = sizeof_field(struct caifsock, conn_req.param) > + .usersize = FIELD_SIZEOF(struct caifsock, conn_req.param) > }; > > if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN)) > diff --git a/net/core/skbuff.c b/net/core/skbuff.c > index 47c1aa9ee045..816bea0c4a8e 100644 > --- a/net/core/skbuff.c > +++ b/net/core/skbuff.c > @@ -3983,7 +3983,7 @@ void __init skb_init(void) > 0, > SLAB_HWCACHE_ALIGN|SLAB_PANIC, > offsetof(struct sk_buff, cb), > - sizeof_field(struct sk_buff, cb), > + FIELD_SIZEOF(struct sk_buff, cb), > NULL); > skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", > sizeof(struct sk_buff_fclones), > diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c > index 0b8e06ca75d6..efa4c745f7b9 100644 > --- a/net/ipv4/raw.c > +++ b/net/ipv4/raw.c > @@ -977,7 +977,7 @@ struct proto raw_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw_sock), > .useroffset = offsetof(struct raw_sock, filter), > - .usersize = sizeof_field(struct raw_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw_sock, filter), > .h.raw_hash = &raw_v4_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_raw_setsockopt, > diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c > index 70693bc7ad9d..257c71e22d74 100644 > --- a/net/ipv6/raw.c > +++ b/net/ipv6/raw.c > @@ -1292,7 +1292,7 @@ struct proto rawv6_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw6_sock), > .useroffset = offsetof(struct raw6_sock, filter), > - .usersize = sizeof_field(struct raw6_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw6_sock, filter), > .h.raw_hash = &raw_v6_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_rawv6_setsockopt, > diff --git a/net/sctp/socket.c b/net/sctp/socket.c > index 39ea0a37af09..6b648a6033b9 100644 > --- a/net/sctp/socket.c > +++ b/net/sctp/socket.c > @@ -9377,7 +9377,7 @@ struct proto sctp_prot = { > .useroffset = offsetof(struct sctp_sock, subscribe), > .usersize = offsetof(struct sctp_sock, initmsg) - > offsetof(struct sctp_sock, subscribe) + > - sizeof_field(struct sctp_sock, initmsg), > + FIELD_SIZEOF(struct sctp_sock, initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > @@ -9419,7 +9419,7 @@ struct proto sctpv6_prot = { > .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), > .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - > offsetof(struct sctp6_sock, sctp.subscribe) + > - sizeof_field(struct sctp6_sock, sctp.initmsg), > + FIELD_SIZEOF(struct sctp6_sock, sctp.initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h > index a29206ebbd13..571c35aac90f 100644 > --- a/tools/testing/selftests/bpf/bpf_util.h > +++ b/tools/testing/selftests/bpf/bpf_util.h > @@ -58,13 +58,29 @@ static inline unsigned int bpf_num_possible_cpus(void) > # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) > #endif > > -#ifndef sizeof_field > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > +/* > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#ifndef FIELD_SIZEOF > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > #endif > > +/* > + * For code compatibility > + * > + * sizeof_field(TYPE, MEMBER) > + * @TYPE: The structure containing the field of interest > + * @MEMBER: The field to return the size of > + */ > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > + > #ifndef offsetofend > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > #endif > > #endif /* __BPF_UTIL__ */ > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index ca54b09adf5b..e43e3a26f6ab 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -4275,7 +4275,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, > kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, > SLAB_ACCOUNT, > offsetof(struct kvm_vcpu, arch), > - sizeof_field(struct kvm_vcpu, arch), > + FIELD_SIZEOF(struct kvm_vcpu, arch), > NULL); > if (!kvm_vcpu_cache) { > r = -ENOMEM; > -- > 2.11.0 > -- Kees Cook From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kees Cook Date: Tue, 11 Jun 2019 20:46:34 +0000 Subject: Re: [PATCH V2] include: linux: Regularise the use of FIELD_SIZEOF macro Message-Id: <201906111345.74AA9311F5@keescook> List-Id: References: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> In-Reply-To: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Shyam Saini Cc: kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, linux-ext4@vger.kernel.org, devel@lists.orangefs.org, linux-mm@kvack.org, linux-sctp@vger.kernel.org, bpf@vger.kernel.org, kvm@vger.kernel.org, mayhs11saini@gmail.com On Wed, Jun 12, 2019 at 01:08:36AM +0530, Shyam Saini wrote: > In favour of FIELD_SIZEOF, this patch also deprecates other two similar > macros sizeof_field and SIZEOF_FIELD. > > For code compatibility reason, retain sizeof_field macro as a wrapper macro > to FIELD_SIZEOF Can you explain this part? First sentence says you want to remove sizeof_field, and the second says you're keeping it? I thought the point was to switch all of these to FIELD_SIZEOF()? -Kees > > Signed-off-by: Shyam Saini > --- > Changelog: > > V1->V2 > - Consolidate previous patch 1 and 2 into single patch > - For code compatibility reason, retain sizeof_field macro as a > wrapper macro to FIELD_SIZEOF > > arch/arm64/include/asm/processor.h | 10 +++++----- > arch/mips/cavium-octeon/executive/cvmx-bootmem.c | 9 +-------- > drivers/gpu/drm/i915/gvt/scheduler.c | 2 +- > drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c | 4 ++-- > fs/befs/linuxvfs.c | 2 +- > fs/ext2/super.c | 2 +- > fs/ext4/super.c | 2 +- > fs/freevxfs/vxfs_super.c | 2 +- > fs/orangefs/super.c | 2 +- > fs/ufs/super.c | 2 +- > include/linux/kernel.h | 9 --------- > include/linux/slab.h | 2 +- > include/linux/stddef.h | 17 ++++++++++++++--- > kernel/fork.c | 2 +- > kernel/utsname.c | 2 +- > net/caif/caif_socket.c | 2 +- > net/core/skbuff.c | 2 +- > net/ipv4/raw.c | 2 +- > net/ipv6/raw.c | 2 +- > net/sctp/socket.c | 4 ++-- > tools/testing/selftests/bpf/bpf_util.h | 22 +++++++++++++++++++--- > virt/kvm/kvm_main.c | 2 +- > 22 files changed, 58 insertions(+), 47 deletions(-) > > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index fcd0e691b1ea..ace906d887cc 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -164,13 +164,13 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset, > unsigned long *size) > { > /* Verify that there is no padding among the whitelisted fields: */ > - BUILD_BUG_ON(sizeof_field(struct thread_struct, uw) !> - sizeof_field(struct thread_struct, uw.tp_value) + > - sizeof_field(struct thread_struct, uw.tp2_value) + > - sizeof_field(struct thread_struct, uw.fpsimd_state)); > + BUILD_BUG_ON(FIELD_SIZEOF(struct thread_struct, uw) !> + FIELD_SIZEOF(struct thread_struct, uw.tp_value) + > + FIELD_SIZEOF(struct thread_struct, uw.tp2_value) + > + FIELD_SIZEOF(struct thread_struct, uw.fpsimd_state)); > > *offset = offsetof(struct thread_struct, uw); > - *size = sizeof_field(struct thread_struct, uw); > + *size = FIELD_SIZEOF(struct thread_struct, uw); > } > > #ifdef CONFIG_COMPAT > diff --git a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > index ba8f82a29a81..44b506a14666 100644 > --- a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > +++ b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > @@ -45,13 +45,6 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > /* See header file for descriptions of functions */ > > /** > - * This macro returns the size of a member of a structure. > - * Logically it is the same as "sizeof(s::field)" in C++, but > - * C lacks the "::" operator. > - */ > -#define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field) > - > -/** > * This macro returns a member of the > * cvmx_bootmem_named_block_desc_t structure. These members can't > * be directly addressed as they might be in memory not directly > @@ -65,7 +58,7 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \ > __cvmx_bootmem_desc_get(addr, \ > offsetof(struct cvmx_bootmem_named_block_desc, field), \ > - SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc, field)) > + FIELD_SIZEOF(struct cvmx_bootmem_named_block_desc, field)) > > /** > * This function is the implementation of the get macros defined > diff --git a/drivers/gpu/drm/i915/gvt/scheduler.c b/drivers/gpu/drm/i915/gvt/scheduler.c > index 0f919f0a43d4..820f95a52542 100644 > --- a/drivers/gpu/drm/i915/gvt/scheduler.c > +++ b/drivers/gpu/drm/i915/gvt/scheduler.c > @@ -1243,7 +1243,7 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) > sizeof(struct intel_vgpu_workload), 0, > SLAB_HWCACHE_ALIGN, > offsetof(struct intel_vgpu_workload, rb_tail), > - sizeof_field(struct intel_vgpu_workload, rb_tail), > + FIELD_SIZEOF(struct intel_vgpu_workload, rb_tail), > NULL); > > if (!s->workloads) { > diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > index 46baf3b44309..c0447bf07fbb 100644 > --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > @@ -49,13 +49,13 @@ struct mlxsw_sp_fid_8021d { > }; > > static const struct rhashtable_params mlxsw_sp_fid_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, fid_index), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, fid_index), > .key_offset = offsetof(struct mlxsw_sp_fid, fid_index), > .head_offset = offsetof(struct mlxsw_sp_fid, ht_node), > }; > > static const struct rhashtable_params mlxsw_sp_fid_vni_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, vni), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, vni), > .key_offset = offsetof(struct mlxsw_sp_fid, vni), > .head_offset = offsetof(struct mlxsw_sp_fid, vni_ht_node), > }; > diff --git a/fs/befs/linuxvfs.c b/fs/befs/linuxvfs.c > index 462d096ff3e9..06ffd4829e2e 100644 > --- a/fs/befs/linuxvfs.c > +++ b/fs/befs/linuxvfs.c > @@ -438,7 +438,7 @@ befs_init_inodecache(void) > SLAB_ACCOUNT), > offsetof(struct befs_inode_info, > i_data.symlink), > - sizeof_field(struct befs_inode_info, > + FIELD_SIZEOF(struct befs_inode_info, > i_data.symlink), > init_once); > if (befs_inode_cachep = NULL) > diff --git a/fs/ext2/super.c b/fs/ext2/super.c > index 1d7ab73b1014..d9a6c81f4a47 100644 > --- a/fs/ext2/super.c > +++ b/fs/ext2/super.c > @@ -220,7 +220,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext2_inode_info, i_data), > - sizeof_field(struct ext2_inode_info, i_data), > + FIELD_SIZEOF(struct ext2_inode_info, i_data), > init_once); > if (ext2_inode_cachep = NULL) > return -ENOMEM; > diff --git a/fs/ext4/super.c b/fs/ext4/super.c > index 4079605d437a..b1b5856248bd 100644 > --- a/fs/ext4/super.c > +++ b/fs/ext4/super.c > @@ -1148,7 +1148,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext4_inode_info, i_data), > - sizeof_field(struct ext4_inode_info, i_data), > + FIELD_SIZEOF(struct ext4_inode_info, i_data), > init_once); > if (ext4_inode_cachep = NULL) > return -ENOMEM; > diff --git a/fs/freevxfs/vxfs_super.c b/fs/freevxfs/vxfs_super.c > index a89f68c3cbed..ffd22f85bbe0 100644 > --- a/fs/freevxfs/vxfs_super.c > +++ b/fs/freevxfs/vxfs_super.c > @@ -329,7 +329,7 @@ vxfs_init(void) > sizeof(struct vxfs_inode_info), 0, > SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, > offsetof(struct vxfs_inode_info, vii_immed.vi_immed), > - sizeof_field(struct vxfs_inode_info, > + FIELD_SIZEOF(struct vxfs_inode_info, > vii_immed.vi_immed), > NULL); > if (!vxfs_inode_cachep) > diff --git a/fs/orangefs/super.c b/fs/orangefs/super.c > index ee5efdc35cc1..30f625059ad9 100644 > --- a/fs/orangefs/super.c > +++ b/fs/orangefs/super.c > @@ -646,7 +646,7 @@ int orangefs_inode_cache_initialize(void) > ORANGEFS_CACHE_CREATE_FLAGS, > offsetof(struct orangefs_inode_s, > link_target), > - sizeof_field(struct orangefs_inode_s, > + FIELD_SIZEOF(struct orangefs_inode_s, > link_target), > orangefs_inode_cache_ctor); > > diff --git a/fs/ufs/super.c b/fs/ufs/super.c > index 3d247c0d92aa..1e8bcd950f6d 100644 > --- a/fs/ufs/super.c > +++ b/fs/ufs/super.c > @@ -1469,7 +1469,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ufs_inode_info, i_u1.i_symlink), > - sizeof_field(struct ufs_inode_info, > + FIELD_SIZEOF(struct ufs_inode_info, > i_u1.i_symlink), > init_once); > if (ufs_inode_cachep = NULL) > diff --git a/include/linux/kernel.h b/include/linux/kernel.h > index 74b1ee9027f5..4672391cdb5b 100644 > --- a/include/linux/kernel.h > +++ b/include/linux/kernel.h > @@ -79,15 +79,6 @@ > */ > #define round_down(x, y) ((x) & ~__round_mask(x, y)) > > -/** > - * FIELD_SIZEOF - get the size of a struct's field > - * @t: the target struct > - * @f: the target struct's field > - * Return: the size of @f in the struct definition without having a > - * declared instance of @t. > - */ > -#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) > - > #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP > > #define DIV_ROUND_DOWN_ULL(ll, d) \ > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 9449b19c5f10..8bdfdd389b37 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -175,7 +175,7 @@ void memcg_destroy_kmem_caches(struct mem_cgroup *); > sizeof(struct __struct), \ > __alignof__(struct __struct), (__flags), \ > offsetof(struct __struct, __field), \ > - sizeof_field(struct __struct, __field), NULL) > + FIELD_SIZEOF(struct __struct, __field), NULL) > > /* > * Common kmalloc functions provided by all allocators > diff --git a/include/linux/stddef.h b/include/linux/stddef.h > index 998a4ba28eba..a5960e2b4a8b 100644 > --- a/include/linux/stddef.h > +++ b/include/linux/stddef.h > @@ -20,12 +20,23 @@ enum { > #endif > > /** > - * sizeof_field(TYPE, MEMBER) > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > + > +/* > + * For code compatibility > * > + * sizeof_field(TYPE, MEMBER) > * @TYPE: The structure containing the field of interest > * @MEMBER: The field to return the size of > */ > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > + > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > > /** > * offsetofend(TYPE, MEMBER) > @@ -34,6 +45,6 @@ enum { > * @MEMBER: The member within the structure to get the end offset of > */ > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > > #endif > diff --git a/kernel/fork.c b/kernel/fork.c > index 75675b9bf6df..ef40b95bf82c 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -2553,7 +2553,7 @@ void __init proc_caches_init(void) > mm_size, ARCH_MIN_MMSTRUCT_ALIGN, > SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct mm_struct, saved_auxv), > - sizeof_field(struct mm_struct, saved_auxv), > + FIELD_SIZEOF(struct mm_struct, saved_auxv), > NULL); > vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); > mmap_init(); > diff --git a/kernel/utsname.c b/kernel/utsname.c > index f0e491193009..28257c571553 100644 > --- a/kernel/utsname.c > +++ b/kernel/utsname.c > @@ -174,6 +174,6 @@ void __init uts_ns_init(void) > "uts_namespace", sizeof(struct uts_namespace), 0, > SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct uts_namespace, name), > - sizeof_field(struct uts_namespace, name), > + FIELD_SIZEOF(struct uts_namespace, name), > NULL); > } > diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c > index 13ea920600ae..3306bbed77eb 100644 > --- a/net/caif/caif_socket.c > +++ b/net/caif/caif_socket.c > @@ -1033,7 +1033,7 @@ static int caif_create(struct net *net, struct socket *sock, int protocol, > .owner = THIS_MODULE, > .obj_size = sizeof(struct caifsock), > .useroffset = offsetof(struct caifsock, conn_req.param), > - .usersize = sizeof_field(struct caifsock, conn_req.param) > + .usersize = FIELD_SIZEOF(struct caifsock, conn_req.param) > }; > > if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN)) > diff --git a/net/core/skbuff.c b/net/core/skbuff.c > index 47c1aa9ee045..816bea0c4a8e 100644 > --- a/net/core/skbuff.c > +++ b/net/core/skbuff.c > @@ -3983,7 +3983,7 @@ void __init skb_init(void) > 0, > SLAB_HWCACHE_ALIGN|SLAB_PANIC, > offsetof(struct sk_buff, cb), > - sizeof_field(struct sk_buff, cb), > + FIELD_SIZEOF(struct sk_buff, cb), > NULL); > skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", > sizeof(struct sk_buff_fclones), > diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c > index 0b8e06ca75d6..efa4c745f7b9 100644 > --- a/net/ipv4/raw.c > +++ b/net/ipv4/raw.c > @@ -977,7 +977,7 @@ struct proto raw_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw_sock), > .useroffset = offsetof(struct raw_sock, filter), > - .usersize = sizeof_field(struct raw_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw_sock, filter), > .h.raw_hash = &raw_v4_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_raw_setsockopt, > diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c > index 70693bc7ad9d..257c71e22d74 100644 > --- a/net/ipv6/raw.c > +++ b/net/ipv6/raw.c > @@ -1292,7 +1292,7 @@ struct proto rawv6_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw6_sock), > .useroffset = offsetof(struct raw6_sock, filter), > - .usersize = sizeof_field(struct raw6_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw6_sock, filter), > .h.raw_hash = &raw_v6_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_rawv6_setsockopt, > diff --git a/net/sctp/socket.c b/net/sctp/socket.c > index 39ea0a37af09..6b648a6033b9 100644 > --- a/net/sctp/socket.c > +++ b/net/sctp/socket.c > @@ -9377,7 +9377,7 @@ struct proto sctp_prot = { > .useroffset = offsetof(struct sctp_sock, subscribe), > .usersize = offsetof(struct sctp_sock, initmsg) - > offsetof(struct sctp_sock, subscribe) + > - sizeof_field(struct sctp_sock, initmsg), > + FIELD_SIZEOF(struct sctp_sock, initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > @@ -9419,7 +9419,7 @@ struct proto sctpv6_prot = { > .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), > .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - > offsetof(struct sctp6_sock, sctp.subscribe) + > - sizeof_field(struct sctp6_sock, sctp.initmsg), > + FIELD_SIZEOF(struct sctp6_sock, sctp.initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h > index a29206ebbd13..571c35aac90f 100644 > --- a/tools/testing/selftests/bpf/bpf_util.h > +++ b/tools/testing/selftests/bpf/bpf_util.h > @@ -58,13 +58,29 @@ static inline unsigned int bpf_num_possible_cpus(void) > # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) > #endif > > -#ifndef sizeof_field > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > +/* > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#ifndef FIELD_SIZEOF > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > #endif > > +/* > + * For code compatibility > + * > + * sizeof_field(TYPE, MEMBER) > + * @TYPE: The structure containing the field of interest > + * @MEMBER: The field to return the size of > + */ > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > + > #ifndef offsetofend > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > #endif > > #endif /* __BPF_UTIL__ */ > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index ca54b09adf5b..e43e3a26f6ab 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -4275,7 +4275,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, > kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, > SLAB_ACCOUNT, > offsetof(struct kvm_vcpu, arch), > - sizeof_field(struct kvm_vcpu, arch), > + FIELD_SIZEOF(struct kvm_vcpu, arch), > NULL); > if (!kvm_vcpu_cache) { > r = -ENOMEM; > -- > 2.11.0 > -- Kees Cook 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=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,T_DKIMWL_WL_HIGH autolearn=unavailable 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 8C7F7C31E40 for ; Tue, 11 Jun 2019 20:46:49 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5D6C5204FD for ; Tue, 11 Jun 2019 20:46:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="pSYqls8d"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="VLItlOM/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D6C5204FD Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=isuAeQGQPVd4jO3weD3Pjps4uRAx+5Yx7fPauNviIDw=; b=pSYqls8dn9WrMV 7eTAT2SknO/xqTuV0BkQbdnnP4tzwVUeTaexmLAF1jwyn+yChzJlJJgiw+SCQ7+HSqnxEiUtfGaZs FFO3IQsqJOf/uLTGRXA9NtqydLQ47jff08XEBneXsuJwfd5KCW6bZAvjNiBfZ2n+kuMi5kWdxoLfU 2FqaOhYadz8Oc/HvMh8ykCm0j0hVsdpn/Kopt8xYuVTSAHaWKGLpfg1i+LBdOtGrjcEjf+VphIXl+ /R+HOdati2aENc7Mles0o2RqPqv8B5euadGVu21WOvSCq9a3sGlkDtDf1EbH+PMXNZhc7Ebx8dAG1 Gz+MYfxWpFZJkq48lByg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hanfF-0003cb-Us; Tue, 11 Jun 2019 20:46:42 +0000 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hanfA-0003br-8x for linux-arm-kernel@lists.infradead.org; Tue, 11 Jun 2019 20:46:38 +0000 Received: by mail-pf1-x443.google.com with SMTP id p184so4875747pfp.7 for ; Tue, 11 Jun 2019 13:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=8BCrEWxENFI652njZuyMg7oU6st6d90fR1m0hvbHelQ=; b=VLItlOM/U0/csHcUVmJtFGAA/3ks5F+OEZrSeoslGcAAJmtSlj+iB0/ek7AEsZLbtT DpfHa5NTqWHmT15WxBDcExNmkFz9PBSNokh4XujZvPfdj5vxckL3Qf2i4YdJAdYbwQLI Lm9eq3+Z4l4jXm61ETvS4xKC3CSnt7fx8hw+c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=8BCrEWxENFI652njZuyMg7oU6st6d90fR1m0hvbHelQ=; b=FMvchAuZ8pixDiPgQCRN6KOiV8YYhNNFd5xhvV5Yhv0F0bO3ksWHPCwdrSwsO9+TeX Ts8hWWOSnx4Dl7wKj5oXwtlljyggwBunCI6Mx6de71W7Iosfw2z++jO1m7obDc+SNb19 v4vPNgiEAcjh/YanQbIXkKTmkef01B2hi184OQFzgfhGuyqnVHrdOdj8Bg3kxJiOcN+H madNe32MEL/fww67x7HYNSsPfBY79n85p7vwKvUPVGkujxxJYJMabE/48odIXZmk23hE rBNIpSP4OZGoqccaN28GHoypBGTisg6Dx2RdtcFqCBv53mS/vGUUpe8QHuFIN10J49Iv ecUw== X-Gm-Message-State: APjAAAWeWBISryqa12sHxylWc68uHHmiyBZ9Y6NT96UYTBV4R50Bhkjq 9CxjvNy70ecGUDK9qplsNlt0NyKY7IA= X-Google-Smtp-Source: APXvYqxVmscktUxOjSNmw0CUlKoqQbpfH4xGysEDq5ymBOLXo2+kCSblhcFdRPP/wDhMPjZXRG2wfQ== X-Received: by 2002:a62:ae01:: with SMTP id q1mr27321750pff.219.1560285995689; Tue, 11 Jun 2019 13:46:35 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id d3sm17055548pfa.176.2019.06.11.13.46.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 11 Jun 2019 13:46:34 -0700 (PDT) Date: Tue, 11 Jun 2019 13:46:34 -0700 From: Kees Cook To: Shyam Saini Subject: Re: [PATCH V2] include: linux: Regularise the use of FIELD_SIZEOF macro Message-ID: <201906111345.74AA9311F5@keescook> References: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20190611193836.2772-1-shyam.saini@amarulasolutions.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190611_134636_319809_B39AA270 X-CRM114-Status: GOOD ( 26.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org, kernel-hardening@lists.openwall.com, netdev@vger.kernel.org, intel-gfx@lists.freedesktop.org, linux-mips@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-sctp@vger.kernel.org, devel@lists.orangefs.org, akpm@linux-foundation.org, linux-ext4@vger.kernel.org, intel-gvt-dev@lists.freedesktop.org, bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, mayhs11saini@gmail.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Jun 12, 2019 at 01:08:36AM +0530, Shyam Saini wrote: > In favour of FIELD_SIZEOF, this patch also deprecates other two similar > macros sizeof_field and SIZEOF_FIELD. > > For code compatibility reason, retain sizeof_field macro as a wrapper macro > to FIELD_SIZEOF Can you explain this part? First sentence says you want to remove sizeof_field, and the second says you're keeping it? I thought the point was to switch all of these to FIELD_SIZEOF()? -Kees > > Signed-off-by: Shyam Saini > --- > Changelog: > > V1->V2 > - Consolidate previous patch 1 and 2 into single patch > - For code compatibility reason, retain sizeof_field macro as a > wrapper macro to FIELD_SIZEOF > > arch/arm64/include/asm/processor.h | 10 +++++----- > arch/mips/cavium-octeon/executive/cvmx-bootmem.c | 9 +-------- > drivers/gpu/drm/i915/gvt/scheduler.c | 2 +- > drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c | 4 ++-- > fs/befs/linuxvfs.c | 2 +- > fs/ext2/super.c | 2 +- > fs/ext4/super.c | 2 +- > fs/freevxfs/vxfs_super.c | 2 +- > fs/orangefs/super.c | 2 +- > fs/ufs/super.c | 2 +- > include/linux/kernel.h | 9 --------- > include/linux/slab.h | 2 +- > include/linux/stddef.h | 17 ++++++++++++++--- > kernel/fork.c | 2 +- > kernel/utsname.c | 2 +- > net/caif/caif_socket.c | 2 +- > net/core/skbuff.c | 2 +- > net/ipv4/raw.c | 2 +- > net/ipv6/raw.c | 2 +- > net/sctp/socket.c | 4 ++-- > tools/testing/selftests/bpf/bpf_util.h | 22 +++++++++++++++++++--- > virt/kvm/kvm_main.c | 2 +- > 22 files changed, 58 insertions(+), 47 deletions(-) > > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index fcd0e691b1ea..ace906d887cc 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -164,13 +164,13 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset, > unsigned long *size) > { > /* Verify that there is no padding among the whitelisted fields: */ > - BUILD_BUG_ON(sizeof_field(struct thread_struct, uw) != > - sizeof_field(struct thread_struct, uw.tp_value) + > - sizeof_field(struct thread_struct, uw.tp2_value) + > - sizeof_field(struct thread_struct, uw.fpsimd_state)); > + BUILD_BUG_ON(FIELD_SIZEOF(struct thread_struct, uw) != > + FIELD_SIZEOF(struct thread_struct, uw.tp_value) + > + FIELD_SIZEOF(struct thread_struct, uw.tp2_value) + > + FIELD_SIZEOF(struct thread_struct, uw.fpsimd_state)); > > *offset = offsetof(struct thread_struct, uw); > - *size = sizeof_field(struct thread_struct, uw); > + *size = FIELD_SIZEOF(struct thread_struct, uw); > } > > #ifdef CONFIG_COMPAT > diff --git a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > index ba8f82a29a81..44b506a14666 100644 > --- a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > +++ b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c > @@ -45,13 +45,6 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > /* See header file for descriptions of functions */ > > /** > - * This macro returns the size of a member of a structure. > - * Logically it is the same as "sizeof(s::field)" in C++, but > - * C lacks the "::" operator. > - */ > -#define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field) > - > -/** > * This macro returns a member of the > * cvmx_bootmem_named_block_desc_t structure. These members can't > * be directly addressed as they might be in memory not directly > @@ -65,7 +58,7 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; > #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \ > __cvmx_bootmem_desc_get(addr, \ > offsetof(struct cvmx_bootmem_named_block_desc, field), \ > - SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc, field)) > + FIELD_SIZEOF(struct cvmx_bootmem_named_block_desc, field)) > > /** > * This function is the implementation of the get macros defined > diff --git a/drivers/gpu/drm/i915/gvt/scheduler.c b/drivers/gpu/drm/i915/gvt/scheduler.c > index 0f919f0a43d4..820f95a52542 100644 > --- a/drivers/gpu/drm/i915/gvt/scheduler.c > +++ b/drivers/gpu/drm/i915/gvt/scheduler.c > @@ -1243,7 +1243,7 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) > sizeof(struct intel_vgpu_workload), 0, > SLAB_HWCACHE_ALIGN, > offsetof(struct intel_vgpu_workload, rb_tail), > - sizeof_field(struct intel_vgpu_workload, rb_tail), > + FIELD_SIZEOF(struct intel_vgpu_workload, rb_tail), > NULL); > > if (!s->workloads) { > diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > index 46baf3b44309..c0447bf07fbb 100644 > --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_fid.c > @@ -49,13 +49,13 @@ struct mlxsw_sp_fid_8021d { > }; > > static const struct rhashtable_params mlxsw_sp_fid_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, fid_index), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, fid_index), > .key_offset = offsetof(struct mlxsw_sp_fid, fid_index), > .head_offset = offsetof(struct mlxsw_sp_fid, ht_node), > }; > > static const struct rhashtable_params mlxsw_sp_fid_vni_ht_params = { > - .key_len = sizeof_field(struct mlxsw_sp_fid, vni), > + .key_len = FIELD_SIZEOF(struct mlxsw_sp_fid, vni), > .key_offset = offsetof(struct mlxsw_sp_fid, vni), > .head_offset = offsetof(struct mlxsw_sp_fid, vni_ht_node), > }; > diff --git a/fs/befs/linuxvfs.c b/fs/befs/linuxvfs.c > index 462d096ff3e9..06ffd4829e2e 100644 > --- a/fs/befs/linuxvfs.c > +++ b/fs/befs/linuxvfs.c > @@ -438,7 +438,7 @@ befs_init_inodecache(void) > SLAB_ACCOUNT), > offsetof(struct befs_inode_info, > i_data.symlink), > - sizeof_field(struct befs_inode_info, > + FIELD_SIZEOF(struct befs_inode_info, > i_data.symlink), > init_once); > if (befs_inode_cachep == NULL) > diff --git a/fs/ext2/super.c b/fs/ext2/super.c > index 1d7ab73b1014..d9a6c81f4a47 100644 > --- a/fs/ext2/super.c > +++ b/fs/ext2/super.c > @@ -220,7 +220,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext2_inode_info, i_data), > - sizeof_field(struct ext2_inode_info, i_data), > + FIELD_SIZEOF(struct ext2_inode_info, i_data), > init_once); > if (ext2_inode_cachep == NULL) > return -ENOMEM; > diff --git a/fs/ext4/super.c b/fs/ext4/super.c > index 4079605d437a..b1b5856248bd 100644 > --- a/fs/ext4/super.c > +++ b/fs/ext4/super.c > @@ -1148,7 +1148,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ext4_inode_info, i_data), > - sizeof_field(struct ext4_inode_info, i_data), > + FIELD_SIZEOF(struct ext4_inode_info, i_data), > init_once); > if (ext4_inode_cachep == NULL) > return -ENOMEM; > diff --git a/fs/freevxfs/vxfs_super.c b/fs/freevxfs/vxfs_super.c > index a89f68c3cbed..ffd22f85bbe0 100644 > --- a/fs/freevxfs/vxfs_super.c > +++ b/fs/freevxfs/vxfs_super.c > @@ -329,7 +329,7 @@ vxfs_init(void) > sizeof(struct vxfs_inode_info), 0, > SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, > offsetof(struct vxfs_inode_info, vii_immed.vi_immed), > - sizeof_field(struct vxfs_inode_info, > + FIELD_SIZEOF(struct vxfs_inode_info, > vii_immed.vi_immed), > NULL); > if (!vxfs_inode_cachep) > diff --git a/fs/orangefs/super.c b/fs/orangefs/super.c > index ee5efdc35cc1..30f625059ad9 100644 > --- a/fs/orangefs/super.c > +++ b/fs/orangefs/super.c > @@ -646,7 +646,7 @@ int orangefs_inode_cache_initialize(void) > ORANGEFS_CACHE_CREATE_FLAGS, > offsetof(struct orangefs_inode_s, > link_target), > - sizeof_field(struct orangefs_inode_s, > + FIELD_SIZEOF(struct orangefs_inode_s, > link_target), > orangefs_inode_cache_ctor); > > diff --git a/fs/ufs/super.c b/fs/ufs/super.c > index 3d247c0d92aa..1e8bcd950f6d 100644 > --- a/fs/ufs/super.c > +++ b/fs/ufs/super.c > @@ -1469,7 +1469,7 @@ static int __init init_inodecache(void) > (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| > SLAB_ACCOUNT), > offsetof(struct ufs_inode_info, i_u1.i_symlink), > - sizeof_field(struct ufs_inode_info, > + FIELD_SIZEOF(struct ufs_inode_info, > i_u1.i_symlink), > init_once); > if (ufs_inode_cachep == NULL) > diff --git a/include/linux/kernel.h b/include/linux/kernel.h > index 74b1ee9027f5..4672391cdb5b 100644 > --- a/include/linux/kernel.h > +++ b/include/linux/kernel.h > @@ -79,15 +79,6 @@ > */ > #define round_down(x, y) ((x) & ~__round_mask(x, y)) > > -/** > - * FIELD_SIZEOF - get the size of a struct's field > - * @t: the target struct > - * @f: the target struct's field > - * Return: the size of @f in the struct definition without having a > - * declared instance of @t. > - */ > -#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) > - > #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP > > #define DIV_ROUND_DOWN_ULL(ll, d) \ > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 9449b19c5f10..8bdfdd389b37 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -175,7 +175,7 @@ void memcg_destroy_kmem_caches(struct mem_cgroup *); > sizeof(struct __struct), \ > __alignof__(struct __struct), (__flags), \ > offsetof(struct __struct, __field), \ > - sizeof_field(struct __struct, __field), NULL) > + FIELD_SIZEOF(struct __struct, __field), NULL) > > /* > * Common kmalloc functions provided by all allocators > diff --git a/include/linux/stddef.h b/include/linux/stddef.h > index 998a4ba28eba..a5960e2b4a8b 100644 > --- a/include/linux/stddef.h > +++ b/include/linux/stddef.h > @@ -20,12 +20,23 @@ enum { > #endif > > /** > - * sizeof_field(TYPE, MEMBER) > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > + > +/* > + * For code compatibility > * > + * sizeof_field(TYPE, MEMBER) > * @TYPE: The structure containing the field of interest > * @MEMBER: The field to return the size of > */ > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > + > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > > /** > * offsetofend(TYPE, MEMBER) > @@ -34,6 +45,6 @@ enum { > * @MEMBER: The member within the structure to get the end offset of > */ > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > > #endif > diff --git a/kernel/fork.c b/kernel/fork.c > index 75675b9bf6df..ef40b95bf82c 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -2553,7 +2553,7 @@ void __init proc_caches_init(void) > mm_size, ARCH_MIN_MMSTRUCT_ALIGN, > SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct mm_struct, saved_auxv), > - sizeof_field(struct mm_struct, saved_auxv), > + FIELD_SIZEOF(struct mm_struct, saved_auxv), > NULL); > vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); > mmap_init(); > diff --git a/kernel/utsname.c b/kernel/utsname.c > index f0e491193009..28257c571553 100644 > --- a/kernel/utsname.c > +++ b/kernel/utsname.c > @@ -174,6 +174,6 @@ void __init uts_ns_init(void) > "uts_namespace", sizeof(struct uts_namespace), 0, > SLAB_PANIC|SLAB_ACCOUNT, > offsetof(struct uts_namespace, name), > - sizeof_field(struct uts_namespace, name), > + FIELD_SIZEOF(struct uts_namespace, name), > NULL); > } > diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c > index 13ea920600ae..3306bbed77eb 100644 > --- a/net/caif/caif_socket.c > +++ b/net/caif/caif_socket.c > @@ -1033,7 +1033,7 @@ static int caif_create(struct net *net, struct socket *sock, int protocol, > .owner = THIS_MODULE, > .obj_size = sizeof(struct caifsock), > .useroffset = offsetof(struct caifsock, conn_req.param), > - .usersize = sizeof_field(struct caifsock, conn_req.param) > + .usersize = FIELD_SIZEOF(struct caifsock, conn_req.param) > }; > > if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN)) > diff --git a/net/core/skbuff.c b/net/core/skbuff.c > index 47c1aa9ee045..816bea0c4a8e 100644 > --- a/net/core/skbuff.c > +++ b/net/core/skbuff.c > @@ -3983,7 +3983,7 @@ void __init skb_init(void) > 0, > SLAB_HWCACHE_ALIGN|SLAB_PANIC, > offsetof(struct sk_buff, cb), > - sizeof_field(struct sk_buff, cb), > + FIELD_SIZEOF(struct sk_buff, cb), > NULL); > skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", > sizeof(struct sk_buff_fclones), > diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c > index 0b8e06ca75d6..efa4c745f7b9 100644 > --- a/net/ipv4/raw.c > +++ b/net/ipv4/raw.c > @@ -977,7 +977,7 @@ struct proto raw_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw_sock), > .useroffset = offsetof(struct raw_sock, filter), > - .usersize = sizeof_field(struct raw_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw_sock, filter), > .h.raw_hash = &raw_v4_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_raw_setsockopt, > diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c > index 70693bc7ad9d..257c71e22d74 100644 > --- a/net/ipv6/raw.c > +++ b/net/ipv6/raw.c > @@ -1292,7 +1292,7 @@ struct proto rawv6_prot = { > .unhash = raw_unhash_sk, > .obj_size = sizeof(struct raw6_sock), > .useroffset = offsetof(struct raw6_sock, filter), > - .usersize = sizeof_field(struct raw6_sock, filter), > + .usersize = FIELD_SIZEOF(struct raw6_sock, filter), > .h.raw_hash = &raw_v6_hashinfo, > #ifdef CONFIG_COMPAT > .compat_setsockopt = compat_rawv6_setsockopt, > diff --git a/net/sctp/socket.c b/net/sctp/socket.c > index 39ea0a37af09..6b648a6033b9 100644 > --- a/net/sctp/socket.c > +++ b/net/sctp/socket.c > @@ -9377,7 +9377,7 @@ struct proto sctp_prot = { > .useroffset = offsetof(struct sctp_sock, subscribe), > .usersize = offsetof(struct sctp_sock, initmsg) - > offsetof(struct sctp_sock, subscribe) + > - sizeof_field(struct sctp_sock, initmsg), > + FIELD_SIZEOF(struct sctp_sock, initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > @@ -9419,7 +9419,7 @@ struct proto sctpv6_prot = { > .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), > .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - > offsetof(struct sctp6_sock, sctp.subscribe) + > - sizeof_field(struct sctp6_sock, sctp.initmsg), > + FIELD_SIZEOF(struct sctp6_sock, sctp.initmsg), > .sysctl_mem = sysctl_sctp_mem, > .sysctl_rmem = sysctl_sctp_rmem, > .sysctl_wmem = sysctl_sctp_wmem, > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h > index a29206ebbd13..571c35aac90f 100644 > --- a/tools/testing/selftests/bpf/bpf_util.h > +++ b/tools/testing/selftests/bpf/bpf_util.h > @@ -58,13 +58,29 @@ static inline unsigned int bpf_num_possible_cpus(void) > # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) > #endif > > -#ifndef sizeof_field > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > +/* > + * FIELD_SIZEOF - get the size of a struct's field > + * @t: the target struct > + * @f: the target struct's field > + * Return: the size of @f in the struct definition without having a > + * declared instance of @t. > + */ > +#ifndef FIELD_SIZEOF > +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) > #endif > > +/* > + * For code compatibility > + * > + * sizeof_field(TYPE, MEMBER) > + * @TYPE: The structure containing the field of interest > + * @MEMBER: The field to return the size of > + */ > +#define sizeof_field(TYPE, MEMBER) FIELD_SIZEOF(TYPE, MEMBER) > + > #ifndef offsetofend > #define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > + (offsetof(TYPE, MEMBER) + FIELD_SIZEOF(TYPE, MEMBER)) > #endif > > #endif /* __BPF_UTIL__ */ > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index ca54b09adf5b..e43e3a26f6ab 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -4275,7 +4275,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, > kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, > SLAB_ACCOUNT, > offsetof(struct kvm_vcpu, arch), > - sizeof_field(struct kvm_vcpu, arch), > + FIELD_SIZEOF(struct kvm_vcpu, arch), > NULL); > if (!kvm_vcpu_cache) { > r = -ENOMEM; > -- > 2.11.0 > -- Kees Cook _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel