diff mbox series

[net,v3] net: Refine key_len calculations in rhashtable_params

Message ID 20241220082436.1195276-1-buaajxlj@163.com (mailing list archive)
State New
Delegated to: Netdev Maintainers
Headers show
Series [net,v3] net: Refine key_len calculations in rhashtable_params | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present fail Series targets non-next tree, but doesn't contain any Fixes tags
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 49 maintainers not CCed: jiri@resnulli.us nbd@nbd.name xiyou.wangcong@gmail.com sbhatta@marvell.com oss-drivers@corigine.com kuniyu@amazon.com jhs@mojatatu.com michael.chan@broadcom.com petrm@nvidia.com lorenzo@kernel.org johannes@sipsolutions.net sean.wang@mediatek.com jmaloy@redhat.com intel-wired-lan@lists.osuosl.org linux-mediatek@lists.infradead.org linux-net-drivers@amd.com cmi@nvidia.com roopa@nvidia.com saeedm@nvidia.com Mark-MC.Lee@mediatek.com dsahern@kernel.org hui.zhou@corigine.com louis.peens@corigine.com taras.chornyi@plvision.eu linux-wireless@vger.kernel.org razor@blackwall.org ecree.xilinx@gmail.com matthias.bgg@gmail.com ying.xue@windriver.com linux-arm-kernel@lists.infradead.org bridge@lists.linux.dev leon@kernel.org bharat@chelsio.com idosch@nvidia.com linux-rdma@vger.kernel.org moshe@nvidia.com gal@nvidia.com cratiu@nvidia.com przemyslaw.kitszel@intel.com tipc-discussion@lists.sourceforge.net arnd@arndb.de daniel.devilliers@corigine.com allison.henderson@oracle.com rds-devel@oss.oracle.com habetsm.xilinx@gmail.com tariqt@nvidia.com angelogioacchino.delregno@collabora.com bpf@vger.kernel.org kvalo@kernel.org
netdev/build_clang success Errors and warnings before: 50 this patch: 50
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 651 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 12 this patch: 12
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-12-21--00-00 (tests: 879)

Commit Message

Liang Jie Dec. 20, 2024, 8:24 a.m. UTC
From: Liang Jie <liangjie@lixiang.com>

This patch improves the calculation of key_len in the rhashtable_params
structures across the net driver modules by replacing hardcoded sizes
and previous calculations with appropriate macros like sizeof_field()
and offsetofend().

Previously, key_len was set using hardcoded sizes like sizeof(u32) or
sizeof(unsigned long), or using offsetof() calculations. This patch
replaces these with sizeof_field() and correct use of offsetofend(),
making the code more robust, maintainable, and improving readability.

Using sizeof_field() and offsetofend() provides several advantages:
- They explicitly specify the size of the field or the end offset of a
  member being used as a key.
- They ensure that the key_len is accurate even if the structs change in
  the future.
- They improve code readability by clearly indicating which fields are used
  and how their sizes are determined, making the code easier to understand
  and maintain.

For example, instead of:
    .key_len    = sizeof(u32),
we now use:
    .key_len    = sizeof_field(struct mae_mport_desc, mport_id),

And instead of:
    .key_len    = offsetof(struct efx_tc_encap_match, linkage),
we now use:
    .key_len    = offsetofend(struct efx_tc_encap_match, ip_tos_mask),

These changes eliminate the risk in certain scenarios of including
unintended padding or extra data in the key, ensuring the rhashtable
functions correctly.

Signed-off-by: Liang Jie <liangjie@lixiang.com>

---

Changes in v3:
- Slove checkpatch warrnings.
- Link to v2: https://lore.kernel.org/all/20241217094608.4149466-1-buaajxlj@163.com/

Changes in v2:
- Apply similar modifications to the 'net' directory.
- Fix compilation errors.
- Link to v1: https://lore.kernel.org/all/20241217071411.3863379-1-buaajxlj@163.com/

 drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c        |  8 ++++----
 drivers/net/ethernet/broadcom/bnxt/bnxt_tc.h        |  1 -
 .../net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c    |  2 +-
 drivers/net/ethernet/intel/ice/ice_eswitch_br.c     |  2 +-
 .../net/ethernet/marvell/prestera/prestera_acl.c    |  6 +++---
 .../net/ethernet/marvell/prestera/prestera_router.c |  4 ++--
 .../ethernet/marvell/prestera/prestera_router_hw.c  |  6 +++---
 drivers/net/ethernet/mediatek/mtk_ppe_offload.c     |  2 +-
 .../net/ethernet/mellanox/mlx5/core/en/rep/neigh.c  |  2 +-
 drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c  |  8 ++++----
 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c     |  2 +-
 .../net/ethernet/mellanox/mlx5/core/esw/bridge.c    |  2 +-
 .../ethernet/mellanox/mlx5/core/esw/bridge_mcast.c  |  2 +-
 .../ethernet/mellanox/mlxsw/core_acl_flex_actions.c |  6 +++---
 drivers/net/ethernet/mellanox/mlxsw/spectrum.c      |  4 ++--
 drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c  |  4 ++--
 .../ethernet/mellanox/mlxsw/spectrum_acl_atcam.c    |  4 ++--
 .../net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c |  2 +-
 drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c   |  2 +-
 drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c  |  4 ++--
 .../net/ethernet/mellanox/mlxsw/spectrum_router.c   |  8 ++++----
 .../ethernet/mellanox/mlxsw/spectrum_switchdev.c    |  2 +-
 drivers/net/ethernet/netronome/nfp/bpf/main.c       |  3 ++-
 .../net/ethernet/netronome/nfp/flower/conntrack.c   |  6 ++++--
 .../net/ethernet/netronome/nfp/flower/metadata.c    | 13 ++++++++-----
 .../net/ethernet/netronome/nfp/flower/qos_conf.c    |  2 +-
 .../net/ethernet/netronome/nfp/flower/tunnel_conf.c |  2 +-
 drivers/net/ethernet/sfc/mae.c                      |  2 +-
 drivers/net/ethernet/sfc/tc.c                       | 12 ++++++------
 drivers/net/ethernet/sfc/tc_conntrack.c             |  8 ++++----
 drivers/net/ethernet/sfc/tc_counters.c              |  8 ++++----
 drivers/net/ethernet/sfc/tc_encap_actions.c         |  2 +-
 drivers/net/netdevsim/fib.c                         |  4 ++--
 drivers/net/vxlan/vxlan_mdb.c                       |  2 +-
 drivers/net/vxlan/vxlan_vnifilter.c                 |  2 +-
 drivers/net/wireless/virtual/mac80211_hwsim.c       |  2 +-
 net/bridge/br_multicast.c                           |  4 ++--
 net/bridge/br_vlan.c                                |  2 +-
 net/ipv4/ip_fragment.c                              |  4 ++--
 net/ipv4/ipmr.c                                     |  2 +-
 net/ipv6/ila/ila_xlat.c                             |  2 +-
 net/ipv6/ioam6.c                                    |  4 ++--
 net/ipv6/ip6mr.c                                    |  2 +-
 net/mac80211/mesh_pathtbl.c                         |  2 +-
 net/rds/bind.c                                      |  2 +-
 net/sched/cls_flower.c                              |  2 +-
 net/tipc/socket.c                                   |  2 +-
 47 files changed, 92 insertions(+), 87 deletions(-)
diff mbox series

Patch

diff --git a/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c b/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c
index d2ca90407cce..7aff67fad4a9 100644
--- a/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c
+++ b/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c
@@ -1982,28 +1982,28 @@  static int bnxt_tc_setup_indr_cb(struct net_device *netdev, struct Qdisc *sch, v
 static const struct rhashtable_params bnxt_tc_flow_ht_params = {
 	.head_offset = offsetof(struct bnxt_tc_flow_node, node),
 	.key_offset = offsetof(struct bnxt_tc_flow_node, cookie),
-	.key_len = sizeof(((struct bnxt_tc_flow_node *)0)->cookie),
+	.key_len = sizeof_field(struct bnxt_tc_flow_node, cookie),
 	.automatic_shrinking = true
 };
 
 static const struct rhashtable_params bnxt_tc_l2_ht_params = {
 	.head_offset = offsetof(struct bnxt_tc_l2_node, node),
 	.key_offset = offsetof(struct bnxt_tc_l2_node, key),
-	.key_len = BNXT_TC_L2_KEY_LEN,
+	.key_len = offsetofend(struct bnxt_tc_l2_key, inner_vlan_tci),
 	.automatic_shrinking = true
 };
 
 static const struct rhashtable_params bnxt_tc_decap_l2_ht_params = {
 	.head_offset = offsetof(struct bnxt_tc_l2_node, node),
 	.key_offset = offsetof(struct bnxt_tc_l2_node, key),
-	.key_len = BNXT_TC_L2_KEY_LEN,
+	.key_len = offsetofend(struct bnxt_tc_l2_key, inner_vlan_tci),
 	.automatic_shrinking = true
 };
 
 static const struct rhashtable_params bnxt_tc_tunnel_ht_params = {
 	.head_offset = offsetof(struct bnxt_tc_tunnel_node, node),
 	.key_offset = offsetof(struct bnxt_tc_tunnel_node, key),
-	.key_len = sizeof(struct ip_tunnel_key),
+	.key_len = sizeof_field(struct bnxt_tc_tunnel_node, key),
 	.automatic_shrinking = true
 };
 
diff --git a/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.h b/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.h
index 10c62b094914..9a48f6bc25ed 100644
--- a/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.h
+++ b/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.h
@@ -173,7 +173,6 @@  struct bnxt_tc_tunnel_node {
  */
 struct bnxt_tc_l2_node {
 	/* hash key: first 16b of key */
-#define BNXT_TC_L2_KEY_LEN			16
 	struct bnxt_tc_l2_key	key;
 	struct rhash_head	node;
 
diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c
index 69d045d769c4..ac5b209e46e7 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c
@@ -1103,7 +1103,7 @@  static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
 	.nelem_hint = 384,
 	.head_offset = offsetof(struct ch_tc_flower_entry, node),
 	.key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
-	.key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
+	.key_len = sizeof_field(struct ch_tc_flower_entry, tc_flower_cookie),
 	.max_size = 524288,
 	.min_size = 512,
 	.automatic_shrinking = true
diff --git a/drivers/net/ethernet/intel/ice/ice_eswitch_br.c b/drivers/net/ethernet/intel/ice/ice_eswitch_br.c
index cccb7ddf61c9..b06e188985ee 100644
--- a/drivers/net/ethernet/intel/ice/ice_eswitch_br.c
+++ b/drivers/net/ethernet/intel/ice/ice_eswitch_br.c
@@ -13,7 +13,7 @@ 
 
 static const struct rhashtable_params ice_fdb_ht_params = {
 	.key_offset = offsetof(struct ice_esw_br_fdb_entry, data),
-	.key_len = sizeof(struct ice_esw_br_fdb_data),
+	.key_len = sizeof_field(struct ice_esw_br_fdb_entry, data),
 	.head_offset = offsetof(struct ice_esw_br_fdb_entry, ht_node),
 	.automatic_shrinking = true,
 };
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_acl.c b/drivers/net/ethernet/marvell/prestera/prestera_acl.c
index cba89fda504b..312a725c10f7 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_acl.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_acl.c
@@ -79,14 +79,14 @@  struct prestera_acl_vtcam {
 };
 
 static const struct rhashtable_params prestera_acl_ruleset_ht_params = {
-	.key_len = sizeof(struct prestera_acl_ruleset_ht_key),
+	.key_len = sizeof_field(struct prestera_acl_ruleset, ht_key),
 	.key_offset = offsetof(struct prestera_acl_ruleset, ht_key),
 	.head_offset = offsetof(struct prestera_acl_ruleset, ht_node),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params prestera_acl_rule_ht_params = {
-	.key_len = sizeof(unsigned long),
+	.key_len = sizeof_field(struct prestera_acl_rule, cookie),
 	.key_offset = offsetof(struct prestera_acl_rule, cookie),
 	.head_offset = offsetof(struct prestera_acl_rule, ht_node),
 	.automatic_shrinking = true,
@@ -95,7 +95,7 @@  static const struct rhashtable_params prestera_acl_rule_ht_params = {
 static const struct rhashtable_params __prestera_acl_rule_entry_ht_params = {
 	.key_offset  = offsetof(struct prestera_acl_rule_entry, key),
 	.head_offset = offsetof(struct prestera_acl_rule_entry, ht_node),
-	.key_len     = sizeof(struct prestera_acl_rule_entry_key),
+	.key_len     = sizeof_field(struct prestera_acl_rule_entry, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_router.c b/drivers/net/ethernet/marvell/prestera/prestera_router.c
index de317179a7dc..dcde1fbd6da1 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_router.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_router.c
@@ -67,14 +67,14 @@  struct prestera_kern_fib_cache {
 static const struct rhashtable_params __prestera_kern_neigh_cache_ht_params = {
 	.key_offset  = offsetof(struct prestera_kern_neigh_cache, key),
 	.head_offset = offsetof(struct prestera_kern_neigh_cache, ht_node),
-	.key_len     = sizeof(struct prestera_kern_neigh_cache_key),
+	.key_len     = sizeof_field(struct prestera_kern_neigh_cache, key),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params __prestera_kern_fib_cache_ht_params = {
 	.key_offset  = offsetof(struct prestera_kern_fib_cache, key),
 	.head_offset = offsetof(struct prestera_kern_fib_cache, ht_node),
-	.key_len     = sizeof(struct prestera_kern_fib_cache_key),
+	.key_len     = sizeof_field(struct prestera_kern_fib_cache, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/marvell/prestera/prestera_router_hw.c b/drivers/net/ethernet/marvell/prestera/prestera_router_hw.c
index 02faaea2aefa..9830b5512056 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_router_hw.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_router_hw.c
@@ -35,19 +35,19 @@ 
 static const struct rhashtable_params __prestera_fib_ht_params = {
 	.key_offset  = offsetof(struct prestera_fib_node, key),
 	.head_offset = offsetof(struct prestera_fib_node, ht_node),
-	.key_len     = sizeof(struct prestera_fib_key),
+	.key_len     = sizeof_field(struct prestera_fib_node, key),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params __prestera_nh_neigh_ht_params = {
 	.key_offset  = offsetof(struct prestera_nh_neigh, key),
-	.key_len     = sizeof(struct prestera_nh_neigh_key),
+	.key_len     = sizeof_field(struct prestera_nh_neigh, key),
 	.head_offset = offsetof(struct prestera_nh_neigh, ht_node),
 };
 
 static const struct rhashtable_params __prestera_nexthop_group_ht_params = {
 	.key_offset  = offsetof(struct prestera_nexthop_group, key),
-	.key_len     = sizeof(struct prestera_nexthop_group_key),
+	.key_len     = sizeof_field(struct prestera_nexthop_group, key),
 	.head_offset = offsetof(struct prestera_nexthop_group, ht_node),
 };
 
diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
index f20bb390df3a..0145c06ebec9 100644
--- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
+++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
@@ -47,7 +47,7 @@  struct mtk_flow_data {
 static const struct rhashtable_params mtk_flow_ht_params = {
 	.head_offset = offsetof(struct mtk_flow_entry, node),
 	.key_offset = offsetof(struct mtk_flow_entry, cookie),
-	.key_len = sizeof(unsigned long),
+	.key_len = sizeof_field(struct mtk_flow_entry, cookie),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c b/drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c
index 2e9bee4e5209..dfd798c59051 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c
@@ -269,7 +269,7 @@  static int mlx5e_rep_netevent_event(struct notifier_block *nb,
 static const struct rhashtable_params mlx5e_neigh_ht_params = {
 	.head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
 	.key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
-	.key_len = sizeof(struct mlx5e_neigh),
+	.key_len = sizeof_field(struct mlx5e_neigh_hash_entry, m_neigh),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c
index a84ebac2f011..5a3f370ab136 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c
@@ -173,7 +173,7 @@  mlx5_tc_ct_entry_destroy_mod_hdr(struct mlx5_tc_ct_priv *ct_priv,
 static const struct rhashtable_params cts_ht_params = {
 	.head_offset = offsetof(struct mlx5_ct_entry, node),
 	.key_offset = offsetof(struct mlx5_ct_entry, cookie),
-	.key_len = sizeof(((struct mlx5_ct_entry *)0)->cookie),
+	.key_len = sizeof_field(struct mlx5_ct_entry, cookie),
 	.automatic_shrinking = true,
 	.min_size = 16 * 1024,
 };
@@ -181,14 +181,14 @@  static const struct rhashtable_params cts_ht_params = {
 static const struct rhashtable_params zone_params = {
 	.head_offset = offsetof(struct mlx5_ct_ft, node),
 	.key_offset = offsetof(struct mlx5_ct_ft, zone),
-	.key_len = sizeof(((struct mlx5_ct_ft *)0)->zone),
+	.key_len = sizeof_field(struct mlx5_ct_ft, zone),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params tuples_ht_params = {
 	.head_offset = offsetof(struct mlx5_ct_entry, tuple_node),
 	.key_offset = offsetof(struct mlx5_ct_entry, tuple),
-	.key_len = sizeof(((struct mlx5_ct_entry *)0)->tuple),
+	.key_len = sizeof_field(struct mlx5_ct_entry, tuple),
 	.automatic_shrinking = true,
 	.min_size = 16 * 1024,
 };
@@ -196,7 +196,7 @@  static const struct rhashtable_params tuples_ht_params = {
 static const struct rhashtable_params tuples_nat_ht_params = {
 	.head_offset = offsetof(struct mlx5_ct_entry, tuple_nat_node),
 	.key_offset = offsetof(struct mlx5_ct_entry, tuple_nat),
-	.key_len = sizeof(((struct mlx5_ct_entry *)0)->tuple_nat),
+	.key_len = sizeof_field(struct mlx5_ct_entry, tuple_nat),
 	.automatic_shrinking = true,
 	.min_size = 16 * 1024,
 };
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
index 6b3b1afe8312..ee40c89b0f3f 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
@@ -4339,7 +4339,7 @@  static void get_flags(int flags, unsigned long *flow_flags)
 static const struct rhashtable_params tc_ht_params = {
 	.head_offset = offsetof(struct mlx5e_tc_flow, node),
 	.key_offset = offsetof(struct mlx5e_tc_flow, cookie),
-	.key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
+	.key_len = sizeof_field(struct mlx5e_tc_flow, cookie),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge.c b/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge.c
index c5ea1d1d2b03..9b9877bc7eb0 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge.c
@@ -15,7 +15,7 @@ 
 
 static const struct rhashtable_params fdb_ht_params = {
 	.key_offset = offsetof(struct mlx5_esw_bridge_fdb_entry, key),
-	.key_len = sizeof(struct mlx5_esw_bridge_fdb_key),
+	.key_len = sizeof_field(struct mlx5_esw_bridge_fdb_entry, key),
 	.head_offset = offsetof(struct mlx5_esw_bridge_fdb_entry, ht_node),
 	.automatic_shrinking = true,
 };
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge_mcast.c b/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge_mcast.c
index 22dd30cf8033..7a39446c23f7 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge_mcast.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/esw/bridge_mcast.c
@@ -9,7 +9,7 @@ 
 
 static const struct rhashtable_params mdb_ht_params = {
 	.key_offset = offsetof(struct mlx5_esw_bridge_mdb_entry, key),
-	.key_len = sizeof(struct mlx5_esw_bridge_mdb_key),
+	.key_len = sizeof_field(struct mlx5_esw_bridge_mdb_entry, key),
 	.head_offset = offsetof(struct mlx5_esw_bridge_mdb_entry, ht_node),
 	.automatic_shrinking = true,
 };
diff --git a/drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_actions.c b/drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_actions.c
index 1915fa41c622..1dc77bf66c17 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_actions.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_actions.c
@@ -106,7 +106,7 @@  struct mlxsw_afa_set {
 };
 
 static const struct rhashtable_params mlxsw_afa_set_ht_params = {
-	.key_len = sizeof(struct mlxsw_afa_set_ht_key),
+	.key_len = sizeof_field(struct mlxsw_afa_set, ht_key),
 	.key_offset = offsetof(struct mlxsw_afa_set, ht_key),
 	.head_offset = offsetof(struct mlxsw_afa_set, ht_node),
 	.automatic_shrinking = true,
@@ -124,7 +124,7 @@  struct mlxsw_afa_fwd_entry {
 };
 
 static const struct rhashtable_params mlxsw_afa_fwd_entry_ht_params = {
-	.key_len = sizeof(struct mlxsw_afa_fwd_entry_ht_key),
+	.key_len = sizeof_field(struct mlxsw_afa_fwd_entry, ht_key),
 	.key_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_key),
 	.head_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_node),
 	.automatic_shrinking = true,
@@ -188,7 +188,7 @@  struct mlxsw_afa_policer {
 };
 
 static const struct rhashtable_params mlxsw_afa_policer_ht_params = {
-	.key_len = sizeof(u32),
+	.key_len = sizeof_field(struct mlxsw_afa_policer, fa_index),
 	.key_offset = offsetof(struct mlxsw_afa_policer, fa_index),
 	.head_offset = offsetof(struct mlxsw_afa_policer, ht_node),
 	.automatic_shrinking = true,
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum.c
index 3f5e5d99251b..744084836fe7 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum.c
@@ -2844,7 +2844,7 @@  struct mlxsw_sp_sample_trigger_node {
 static const struct rhashtable_params mlxsw_sp_sample_trigger_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_sample_trigger_node, trigger),
 	.head_offset = offsetof(struct mlxsw_sp_sample_trigger_node, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_sample_trigger),
+	.key_len = sizeof_field(struct mlxsw_sp_sample_trigger_node, trigger),
 	.automatic_shrinking = true,
 };
 
@@ -3005,7 +3005,7 @@  struct mlxsw_sp_ipv6_addr_node {
 static const struct rhashtable_params mlxsw_sp_ipv6_addr_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_ipv6_addr_node, key),
 	.head_offset = offsetof(struct mlxsw_sp_ipv6_addr_node, ht_node),
-	.key_len = sizeof(struct in6_addr),
+	.key_len = sizeof_field(struct mlxsw_sp_ipv6_addr_node, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
index 3e70cee4d2f3..3701911fa5d5 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
@@ -78,14 +78,14 @@  struct mlxsw_sp_acl_rule {
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
+	.key_len = sizeof_field(struct mlxsw_sp_acl_ruleset, ht_key),
 	.key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
 	.head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
-	.key_len = sizeof(unsigned long),
+	.key_len = sizeof_field(struct mlxsw_sp_acl_rule, cookie),
 	.key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
 	.head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
 	.automatic_shrinking = true,
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c
index 07cb1e26ca3e..49201c4324ac 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c
@@ -52,13 +52,13 @@  struct mlxsw_sp_acl_atcam_region_12kb {
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_atcam_lkey_id_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_acl_atcam_lkey_id_ht_key),
+	.key_len = sizeof_field(struct mlxsw_sp_acl_atcam_lkey_id, ht_key),
 	.key_offset = offsetof(struct mlxsw_sp_acl_atcam_lkey_id, ht_key),
 	.head_offset = offsetof(struct mlxsw_sp_acl_atcam_lkey_id, ht_node),
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_atcam_entries_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_acl_atcam_entry_ht_key),
+	.key_len = sizeof_field(struct mlxsw_sp_acl_atcam_entry, ht_key),
 	.key_offset = offsetof(struct mlxsw_sp_acl_atcam_entry, ht_key),
 	.head_offset = offsetof(struct mlxsw_sp_acl_atcam_entry, ht_node),
 };
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
index b1d08e958bf9..adf293dccda3 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_tcam.c
@@ -198,7 +198,7 @@  struct mlxsw_sp_acl_tcam_ventry {
 };
 
 static const struct rhashtable_params mlxsw_sp_acl_tcam_vchunk_ht_params = {
-	.key_len = sizeof(unsigned int),
+	.key_len = sizeof_field(struct mlxsw_sp_acl_tcam_vchunk, priority),
 	.key_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, priority),
 	.head_offset = offsetof(struct mlxsw_sp_acl_tcam_vchunk, ht_node),
 	.automatic_shrinking = true,
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c
index 69cd689dbc83..5805520c5bbc 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c
@@ -91,7 +91,7 @@  struct mlxsw_sp_mr_route {
 };
 
 static const struct rhashtable_params mlxsw_sp_mr_route_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_mr_route_key),
+	.key_len = sizeof_field(struct mlxsw_sp_mr_route, key),
 	.key_offset = offsetof(struct mlxsw_sp_mr_route, key),
 	.head_offset = offsetof(struct mlxsw_sp_mr_route, ht_node),
 	.automatic_shrinking = true,
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c
index 5479a1c19d2e..11e1ae8decce 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_nve.c
@@ -77,7 +77,7 @@  struct mlxsw_sp_nve_mc_list {
 };
 
 static const struct rhashtable_params mlxsw_sp_nve_mc_list_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_nve_mc_list_key),
+	.key_len = sizeof_field(struct mlxsw_sp_nve_mc_list, key),
 	.key_offset = offsetof(struct mlxsw_sp_nve_mc_list, key),
 	.head_offset = offsetof(struct mlxsw_sp_nve_mc_list, ht_node),
 };
@@ -810,7 +810,7 @@  struct mlxsw_sp_nve_ipv6_ht_node {
 };
 
 static const struct rhashtable_params mlxsw_sp_nve_ipv6_ht_params = {
-	.key_len = sizeof(struct mlxsw_sp_nve_ipv6_ht_key),
+	.key_len = sizeof_field(struct mlxsw_sp_nve_ipv6_ht_node, key),
 	.key_offset = offsetof(struct mlxsw_sp_nve_ipv6_ht_node, key),
 	.head_offset = offsetof(struct mlxsw_sp_nve_ipv6_ht_node, ht_node),
 };
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
index 7d6d859cef3f..f114b52a0ab0 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
@@ -2196,7 +2196,7 @@  struct mlxsw_sp_neigh_entry {
 static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
 	.head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_neigh_key),
+	.key_len = sizeof_field(struct mlxsw_sp_neigh_entry, key),
 };
 
 struct mlxsw_sp_neigh_entry *
@@ -3375,7 +3375,7 @@  bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh)
 static const struct rhashtable_params mlxsw_sp_nexthop_group_vr_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_nexthop_group_vr_entry, key),
 	.head_offset = offsetof(struct mlxsw_sp_nexthop_group_vr_entry, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_nexthop_group_vr_key),
+	.key_len = sizeof_field(struct mlxsw_sp_nexthop_group_vr_entry, key),
 	.automatic_shrinking = true,
 };
 
@@ -3662,7 +3662,7 @@  mlxsw_sp_nexthop6_group_lookup(struct mlxsw_sp *mlxsw_sp,
 static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_nexthop, key),
 	.head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_nexthop_key),
+	.key_len = sizeof_field(struct mlxsw_sp_nexthop, key),
 };
 
 static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
@@ -6561,7 +6561,7 @@  mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
 static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_fib_node, key),
 	.head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_fib_key),
+	.key_len = sizeof_field(struct mlxsw_sp_fib_node, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
index 6397ff0dc951..cee484cbe31f 100644
--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
@@ -127,7 +127,7 @@  struct mlxsw_sp_mdb_entry_port {
 static const struct rhashtable_params mlxsw_sp_mdb_ht_params = {
 	.key_offset = offsetof(struct mlxsw_sp_mdb_entry, key),
 	.head_offset = offsetof(struct mlxsw_sp_mdb_entry, ht_node),
-	.key_len = sizeof(struct mlxsw_sp_mdb_entry_key),
+	.key_len = sizeof_field(struct mlxsw_sp_mdb_entry, key),
 };
 
 static int
diff --git a/drivers/net/ethernet/netronome/nfp/bpf/main.c b/drivers/net/ethernet/netronome/nfp/bpf/main.c
index f469950c7265..e63f36d3a34f 100644
--- a/drivers/net/ethernet/netronome/nfp/bpf/main.c
+++ b/drivers/net/ethernet/netronome/nfp/bpf/main.c
@@ -15,7 +15,8 @@ 
 
 const struct rhashtable_params nfp_bpf_maps_neutral_params = {
 	.nelem_hint		= 4,
-	.key_len		= sizeof_field(struct bpf_map, id),
+	.key_len		= sizeof_field(struct nfp_bpf_neutral_map,
+						map_id),
 	.key_offset		= offsetof(struct nfp_bpf_neutral_map, map_id),
 	.head_offset		= offsetof(struct nfp_bpf_neutral_map, l),
 	.automatic_shrinking	= true,
diff --git a/drivers/net/ethernet/netronome/nfp/flower/conntrack.c b/drivers/net/ethernet/netronome/nfp/flower/conntrack.c
index 15180538b80a..ea0ce4e9a798 100644
--- a/drivers/net/ethernet/netronome/nfp/flower/conntrack.c
+++ b/drivers/net/ethernet/netronome/nfp/flower/conntrack.c
@@ -10,7 +10,8 @@ 
 const struct rhashtable_params nfp_tc_ct_merge_params = {
 	.head_offset		= offsetof(struct nfp_fl_ct_tc_merge,
 					   hash_node),
-	.key_len		= sizeof(unsigned long) * 2,
+	.key_len		= sizeof_field(struct nfp_fl_ct_tc_merge,
+						cookie),
 	.key_offset		= offsetof(struct nfp_fl_ct_tc_merge, cookie),
 	.automatic_shrinking	= true,
 };
@@ -18,7 +19,8 @@  const struct rhashtable_params nfp_tc_ct_merge_params = {
 const struct rhashtable_params nfp_nft_ct_merge_params = {
 	.head_offset		= offsetof(struct nfp_fl_nft_tc_merge,
 					   hash_node),
-	.key_len		= sizeof(unsigned long) * 3,
+	.key_len		= sizeof_field(struct nfp_fl_nft_tc_merge,
+						cookie),
 	.key_offset		= offsetof(struct nfp_fl_nft_tc_merge, cookie),
 	.automatic_shrinking	= true,
 };
diff --git a/drivers/net/ethernet/netronome/nfp/flower/metadata.c b/drivers/net/ethernet/netronome/nfp/flower/metadata.c
index 80e4675582bf..aece799a5ed7 100644
--- a/drivers/net/ethernet/netronome/nfp/flower/metadata.c
+++ b/drivers/net/ethernet/netronome/nfp/flower/metadata.c
@@ -34,7 +34,8 @@  struct nfp_fl_stats_ctx_to_flow {
 static const struct rhashtable_params stats_ctx_table_params = {
 	.key_offset	= offsetof(struct nfp_fl_stats_ctx_to_flow, stats_cxt),
 	.head_offset	= offsetof(struct nfp_fl_stats_ctx_to_flow, ht_node),
-	.key_len	= sizeof(u32),
+	.key_len	= sizeof_field(struct nfp_fl_stats_ctx_to_flow,
+					stats_cxt),
 };
 
 static int nfp_release_stats_entry(struct nfp_app *app, u32 stats_context_id)
@@ -485,19 +486,21 @@  const struct rhashtable_params nfp_flower_table_params = {
 const struct rhashtable_params merge_table_params = {
 	.key_offset	= offsetof(struct nfp_merge_info, parent_ctx),
 	.head_offset	= offsetof(struct nfp_merge_info, ht_node),
-	.key_len	= sizeof(u64),
+	.key_len	= sizeof_field(struct nfp_merge_info, parent_ctx),
 };
 
 const struct rhashtable_params nfp_zone_table_params = {
 	.head_offset		= offsetof(struct nfp_fl_ct_zone_entry, hash_node),
-	.key_len		= sizeof(u16),
+	.key_len		= sizeof_field(struct nfp_fl_ct_zone_entry,
+						zone),
 	.key_offset		= offsetof(struct nfp_fl_ct_zone_entry, zone),
 	.automatic_shrinking	= false,
 };
 
 const struct rhashtable_params nfp_ct_map_params = {
 	.head_offset		= offsetof(struct nfp_fl_ct_map_entry, hash_node),
-	.key_len		= sizeof(unsigned long),
+	.key_len		= sizeof_field(struct nfp_fl_ct_map_entry,
+						cookie),
 	.key_offset		= offsetof(struct nfp_fl_ct_map_entry, cookie),
 	.automatic_shrinking	= true,
 };
@@ -505,7 +508,7 @@  const struct rhashtable_params nfp_ct_map_params = {
 const struct rhashtable_params neigh_table_params = {
 	.key_offset	= offsetof(struct nfp_neigh_entry, neigh_cookie),
 	.head_offset	= offsetof(struct nfp_neigh_entry, ht_node),
-	.key_len	= sizeof(unsigned long),
+	.key_len	= sizeof_field(struct nfp_neigh_entry, neigh_cookie),
 };
 
 int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count,
diff --git a/drivers/net/ethernet/netronome/nfp/flower/qos_conf.c b/drivers/net/ethernet/netronome/nfp/flower/qos_conf.c
index e7180b4793c7..1785ad13e1a8 100644
--- a/drivers/net/ethernet/netronome/nfp/flower/qos_conf.c
+++ b/drivers/net/ethernet/netronome/nfp/flower/qos_conf.c
@@ -555,7 +555,7 @@  int nfp_flower_setup_qos_offload(struct nfp_app *app, struct net_device *netdev,
 static const struct rhashtable_params stats_meter_table_params = {
 	.key_offset	= offsetof(struct nfp_meter_entry, meter_id),
 	.head_offset	= offsetof(struct nfp_meter_entry, ht_node),
-	.key_len	= sizeof(u32),
+	.key_len	= sizeof_field(struct nfp_meter_entry, meter_id),
 };
 
 struct nfp_meter_entry *
diff --git a/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c b/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
index 0d7d138d6e0d..51ae5e2244aa 100644
--- a/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
+++ b/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
@@ -201,7 +201,7 @@  struct nfp_tun_offloaded_mac {
 static const struct rhashtable_params offloaded_macs_params = {
 	.key_offset	= offsetof(struct nfp_tun_offloaded_mac, addr),
 	.head_offset	= offsetof(struct nfp_tun_offloaded_mac, ht_node),
-	.key_len	= ETH_ALEN,
+	.key_len	= sizeof_field(struct nfp_tun_offloaded_mac, addr),
 	.automatic_shrinking	= true,
 };
 
diff --git a/drivers/net/ethernet/sfc/mae.c b/drivers/net/ethernet/sfc/mae.c
index 50f097487b14..72907685cbec 100644
--- a/drivers/net/ethernet/sfc/mae.c
+++ b/drivers/net/ethernet/sfc/mae.c
@@ -1053,7 +1053,7 @@  static bool efx_mae_asl_id(u32 id)
 
 /* mport handling */
 static const struct rhashtable_params efx_mae_mports_ht_params = {
-	.key_len	= sizeof(u32),
+	.key_len	= sizeof_field(struct mae_mport_desc, mport_id),
 	.key_offset	= offsetof(struct mae_mport_desc, mport_id),
 	.head_offset	= offsetof(struct mae_mport_desc, linkage),
 };
diff --git a/drivers/net/ethernet/sfc/tc.c b/drivers/net/ethernet/sfc/tc.c
index 0d93164988fc..d62e3e815acf 100644
--- a/drivers/net/ethernet/sfc/tc.c
+++ b/drivers/net/ethernet/sfc/tc.c
@@ -90,31 +90,31 @@  s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv)
 }
 
 static const struct rhashtable_params efx_tc_mac_ht_params = {
-	.key_len	= offsetofend(struct efx_tc_mac_pedit_action, h_addr),
-	.key_offset	= 0,
+	.key_len	= sizeof_field(struct efx_tc_mac_pedit_action, h_addr),
+	.key_offset	= offsetof(struct efx_tc_mac_pedit_action, h_addr),
 	.head_offset	= offsetof(struct efx_tc_mac_pedit_action, linkage),
 };
 
 static const struct rhashtable_params efx_tc_encap_match_ht_params = {
-	.key_len	= offsetof(struct efx_tc_encap_match, linkage),
+	.key_len	= offsetofend(struct efx_tc_encap_match, ip_tos_mask),
 	.key_offset	= 0,
 	.head_offset	= offsetof(struct efx_tc_encap_match, linkage),
 };
 
 static const struct rhashtable_params efx_tc_match_action_ht_params = {
-	.key_len	= sizeof(unsigned long),
+	.key_len	= sizeof_field(struct efx_tc_flow_rule, cookie),
 	.key_offset	= offsetof(struct efx_tc_flow_rule, cookie),
 	.head_offset	= offsetof(struct efx_tc_flow_rule, linkage),
 };
 
 static const struct rhashtable_params efx_tc_lhs_rule_ht_params = {
-	.key_len	= sizeof(unsigned long),
+	.key_len	= sizeof_field(struct efx_tc_lhs_rule, cookie),
 	.key_offset	= offsetof(struct efx_tc_lhs_rule, cookie),
 	.head_offset	= offsetof(struct efx_tc_lhs_rule, linkage),
 };
 
 static const struct rhashtable_params efx_tc_recirc_ht_params = {
-	.key_len	= offsetof(struct efx_tc_recirc_id, linkage),
+	.key_len	= offsetofend(struct efx_tc_recirc_id, net_dev),
 	.key_offset	= 0,
 	.head_offset	= offsetof(struct efx_tc_recirc_id, linkage),
 };
diff --git a/drivers/net/ethernet/sfc/tc_conntrack.c b/drivers/net/ethernet/sfc/tc_conntrack.c
index d90206f27161..36bb7c78f2b9 100644
--- a/drivers/net/ethernet/sfc/tc_conntrack.c
+++ b/drivers/net/ethernet/sfc/tc_conntrack.c
@@ -16,14 +16,14 @@  static int efx_tc_flow_block(enum tc_setup_type type, void *type_data,
 			     void *cb_priv);
 
 static const struct rhashtable_params efx_tc_ct_zone_ht_params = {
-	.key_len	= offsetof(struct efx_tc_ct_zone, linkage),
-	.key_offset	= 0,
+	.key_len	= sizeof_field(struct efx_tc_ct_zone, zone),
+	.key_offset	= offsetof(struct efx_tc_ct_zone, zone),
 	.head_offset	= offsetof(struct efx_tc_ct_zone, linkage),
 };
 
 static const struct rhashtable_params efx_tc_ct_ht_params = {
-	.key_len	= offsetof(struct efx_tc_ct_entry, linkage),
-	.key_offset	= 0,
+	.key_len	= sizeof_field(struct efx_tc_ct_entry, cookie),
+	.key_offset	= offsetof(struct efx_tc_ct_entry, cookie),
 	.head_offset	= offsetof(struct efx_tc_ct_entry, linkage),
 };
 
diff --git a/drivers/net/ethernet/sfc/tc_counters.c b/drivers/net/ethernet/sfc/tc_counters.c
index a421b0123506..067d75973f0a 100644
--- a/drivers/net/ethernet/sfc/tc_counters.c
+++ b/drivers/net/ethernet/sfc/tc_counters.c
@@ -17,14 +17,14 @@ 
 /* Counter-management hashtables */
 
 static const struct rhashtable_params efx_tc_counter_id_ht_params = {
-	.key_len	= offsetof(struct efx_tc_counter_index, linkage),
-	.key_offset	= 0,
+	.key_len	= sizeof_field(struct efx_tc_counter_index, cookie),
+	.key_offset	= offsetof(struct efx_tc_counter_index, cookie),
 	.head_offset	= offsetof(struct efx_tc_counter_index, linkage),
 };
 
 static const struct rhashtable_params efx_tc_counter_ht_params = {
-	.key_len	= offsetof(struct efx_tc_counter, linkage),
-	.key_offset	= 0,
+	.key_len	= sizeof_field(struct efx_tc_counter, fw_id),
+	.key_offset	= offsetof(struct efx_tc_counter, fw_id),
 	.head_offset	= offsetof(struct efx_tc_counter, linkage),
 };
 
diff --git a/drivers/net/ethernet/sfc/tc_encap_actions.c b/drivers/net/ethernet/sfc/tc_encap_actions.c
index 87443f9dfd22..fa081bbcdef6 100644
--- a/drivers/net/ethernet/sfc/tc_encap_actions.c
+++ b/drivers/net/ethernet/sfc/tc_encap_actions.c
@@ -17,7 +17,7 @@ 
 #include <net/arp.h>
 
 static const struct rhashtable_params efx_neigh_ht_params = {
-	.key_len	= offsetof(struct efx_neigh_binder, ha),
+	.key_len	= offsetofend(struct efx_neigh_binder, dst_ip6),
 	.key_offset	= 0,
 	.head_offset	= offsetof(struct efx_neigh_binder, linkage),
 };
diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
index 16c382c42227..4184b0f0a196 100644
--- a/drivers/net/netdevsim/fib.c
+++ b/drivers/net/netdevsim/fib.c
@@ -115,7 +115,7 @@  struct nsim_fib_event {
 static const struct rhashtable_params nsim_fib_rt_ht_params = {
 	.key_offset = offsetof(struct nsim_fib_rt, key),
 	.head_offset = offsetof(struct nsim_fib_rt, ht_node),
-	.key_len = sizeof(struct nsim_fib_rt_key),
+	.key_len = sizeof_field(struct nsim_fib_rt, key),
 	.automatic_shrinking = true,
 };
 
@@ -129,7 +129,7 @@  struct nsim_nexthop {
 static const struct rhashtable_params nsim_nexthop_ht_params = {
 	.key_offset = offsetof(struct nsim_nexthop, id),
 	.head_offset = offsetof(struct nsim_nexthop, ht_node),
-	.key_len = sizeof(u32),
+	.key_len = sizeof_field(struct nsim_nexthop, id),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/vxlan/vxlan_mdb.c b/drivers/net/vxlan/vxlan_mdb.c
index 8735891ee128..29e7c91ffd2b 100644
--- a/drivers/net/vxlan/vxlan_mdb.c
+++ b/drivers/net/vxlan/vxlan_mdb.c
@@ -85,7 +85,7 @@  struct vxlan_mdb_flush_desc {
 static const struct rhashtable_params vxlan_mdb_rht_params = {
 	.head_offset = offsetof(struct vxlan_mdb_entry, rhnode),
 	.key_offset = offsetof(struct vxlan_mdb_entry, key),
-	.key_len = sizeof(struct vxlan_mdb_entry_key),
+	.key_len = sizeof_field(struct vxlan_mdb_entry, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/drivers/net/vxlan/vxlan_vnifilter.c b/drivers/net/vxlan/vxlan_vnifilter.c
index d2023e7131bd..c70954c54e92 100644
--- a/drivers/net/vxlan/vxlan_vnifilter.c
+++ b/drivers/net/vxlan/vxlan_vnifilter.c
@@ -29,7 +29,7 @@  static inline int vxlan_vni_cmp(struct rhashtable_compare_arg *arg,
 const struct rhashtable_params vxlan_vni_rht_params = {
 	.head_offset = offsetof(struct vxlan_vni_node, vnode),
 	.key_offset = offsetof(struct vxlan_vni_node, vni),
-	.key_len = sizeof(__be32),
+	.key_len = sizeof_field(struct vxlan_vni_node, vni),
 	.nelem_hint = 3,
 	.max_size = VXLAN_N_VID,
 	.obj_cmpfn = vxlan_vni_cmp,
diff --git a/drivers/net/wireless/virtual/mac80211_hwsim.c b/drivers/net/wireless/virtual/mac80211_hwsim.c
index 347a15544afe..8d405d15a09b 100644
--- a/drivers/net/wireless/virtual/mac80211_hwsim.c
+++ b/drivers/net/wireless/virtual/mac80211_hwsim.c
@@ -757,7 +757,7 @@  struct mac80211_hwsim_data {
 static const struct rhashtable_params hwsim_rht_params = {
 	.nelem_hint = 2,
 	.automatic_shrinking = true,
-	.key_len = ETH_ALEN,
+	.key_len = sizeof_field(struct mac80211_hwsim_data, addresses[1]),
 	.key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
 	.head_offset = offsetof(struct mac80211_hwsim_data, rht),
 };
diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index b2ae0d2434d2..cb21142f4686 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -39,14 +39,14 @@ 
 static const struct rhashtable_params br_mdb_rht_params = {
 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
-	.key_len = sizeof(struct br_ip),
+	.key_len = sizeof_field(struct net_bridge_mdb_entry, addr),
 	.automatic_shrinking = true,
 };
 
 static const struct rhashtable_params br_sg_port_rht_params = {
 	.head_offset = offsetof(struct net_bridge_port_group, rhnode),
 	.key_offset = offsetof(struct net_bridge_port_group, key),
-	.key_len = sizeof(struct net_bridge_port_group_sg_key),
+	.key_len = sizeof_field(struct net_bridge_port_group, key),
 	.automatic_shrinking = true,
 };
 
diff --git a/net/bridge/br_vlan.c b/net/bridge/br_vlan.c
index 89f51ea4cabe..95674019106d 100644
--- a/net/bridge/br_vlan.c
+++ b/net/bridge/br_vlan.c
@@ -22,7 +22,7 @@  static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
 static const struct rhashtable_params br_vlan_rht_params = {
 	.head_offset = offsetof(struct net_bridge_vlan, vnode),
 	.key_offset = offsetof(struct net_bridge_vlan, vid),
-	.key_len = sizeof(u16),
+	.key_len = sizeof_field(struct net_bridge_vlan, vid),
 	.nelem_hint = 3,
 	.max_size = VLAN_N_VID,
 	.obj_cmpfn = br_vlan_cmp,
diff --git a/net/ipv4/ip_fragment.c b/net/ipv4/ip_fragment.c
index 07036a2943c1..0d8234460a13 100644
--- a/net/ipv4/ip_fragment.c
+++ b/net/ipv4/ip_fragment.c
@@ -737,8 +737,8 @@  static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
 
 static const struct rhashtable_params ip4_rhash_params = {
 	.head_offset		= offsetof(struct inet_frag_queue, node),
-	.key_offset		= offsetof(struct inet_frag_queue, key),
-	.key_len		= sizeof(struct frag_v4_compare_key),
+	.key_offset		= offsetof(struct inet_frag_queue, key.v4),
+	.key_len		= sizeof_field(struct inet_frag_queue, key.v4),
 	.hashfn			= ip4_key_hashfn,
 	.obj_hashfn		= ip4_obj_hashfn,
 	.obj_cmpfn		= ip4_obj_cmpfn,
diff --git a/net/ipv4/ipmr.c b/net/ipv4/ipmr.c
index 99d8faa508e5..217b2dd200f1 100644
--- a/net/ipv4/ipmr.c
+++ b/net/ipv4/ipmr.c
@@ -393,7 +393,7 @@  static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
 static const struct rhashtable_params ipmr_rht_params = {
 	.head_offset = offsetof(struct mr_mfc, mnode),
 	.key_offset = offsetof(struct mfc_cache, cmparg),
-	.key_len = sizeof(struct mfc_cache_cmp_arg),
+	.key_len = sizeof_field(struct mfc_cache, cmparg),
 	.nelem_hint = 3,
 	.obj_cmpfn = ipmr_hash_cmp,
 	.automatic_shrinking = true,
diff --git a/net/ipv6/ila/ila_xlat.c b/net/ipv6/ila/ila_xlat.c
index 7646e401c630..8f0550fc3673 100644
--- a/net/ipv6/ila/ila_xlat.c
+++ b/net/ipv6/ila/ila_xlat.c
@@ -85,7 +85,7 @@  static const struct rhashtable_params rht_params = {
 	.nelem_hint = 1024,
 	.head_offset = offsetof(struct ila_map, node),
 	.key_offset = offsetof(struct ila_map, xp.ip.locator_match),
-	.key_len = sizeof(u64), /* identifier */
+	.key_len = sizeof_field(struct ila_map, xp.ip.locator_match),
 	.max_size = 1048576,
 	.min_size = 256,
 	.automatic_shrinking = true,
diff --git a/net/ipv6/ioam6.c b/net/ipv6/ioam6.c
index a84d332f952f..a4cd627b4531 100644
--- a/net/ipv6/ioam6.c
+++ b/net/ipv6/ioam6.c
@@ -61,7 +61,7 @@  static int ioam6_sc_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
 }
 
 static const struct rhashtable_params rht_ns_params = {
-	.key_len		= sizeof(__be16),
+	.key_len		= sizeof_field(struct ioam6_namespace, id),
 	.key_offset		= offsetof(struct ioam6_namespace, id),
 	.head_offset		= offsetof(struct ioam6_namespace, head),
 	.automatic_shrinking	= true,
@@ -69,7 +69,7 @@  static const struct rhashtable_params rht_ns_params = {
 };
 
 static const struct rhashtable_params rht_sc_params = {
-	.key_len		= sizeof(u32),
+	.key_len		= sizeof_field(struct ioam6_schema, id),
 	.key_offset		= offsetof(struct ioam6_schema, id),
 	.head_offset		= offsetof(struct ioam6_schema, head),
 	.automatic_shrinking	= true,
diff --git a/net/ipv6/ip6mr.c b/net/ipv6/ip6mr.c
index 578ff1336afe..d63fbe7f51f6 100644
--- a/net/ipv6/ip6mr.c
+++ b/net/ipv6/ip6mr.c
@@ -376,7 +376,7 @@  static int ip6mr_hash_cmp(struct rhashtable_compare_arg *arg,
 static const struct rhashtable_params ip6mr_rht_params = {
 	.head_offset = offsetof(struct mr_mfc, mnode),
 	.key_offset = offsetof(struct mfc6_cache, cmparg),
-	.key_len = sizeof(struct mfc6_cache_cmp_arg),
+	.key_len = sizeof_field(struct mfc6_cache, cmparg),
 	.nelem_hint = 3,
 	.obj_cmpfn = ip6mr_hash_cmp,
 	.automatic_shrinking = true,
diff --git a/net/mac80211/mesh_pathtbl.c b/net/mac80211/mesh_pathtbl.c
index 9f9cb5af0a97..672036d1cf34 100644
--- a/net/mac80211/mesh_pathtbl.c
+++ b/net/mac80211/mesh_pathtbl.c
@@ -28,7 +28,7 @@  static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
 static const struct rhashtable_params mesh_rht_params = {
 	.nelem_hint = 2,
 	.automatic_shrinking = true,
-	.key_len = ETH_ALEN,
+	.key_len = sizeof_field(struct mesh_path, dst),
 	.key_offset = offsetof(struct mesh_path, dst),
 	.head_offset = offsetof(struct mesh_path, rhash),
 	.hashfn = mesh_table_hash,
diff --git a/net/rds/bind.c b/net/rds/bind.c
index 97a29172a8ee..67ff5444d4a1 100644
--- a/net/rds/bind.c
+++ b/net/rds/bind.c
@@ -43,7 +43,7 @@  static struct rhashtable bind_hash_table;
 
 static const struct rhashtable_params ht_parms = {
 	.nelem_hint = 768,
-	.key_len = RDS_BOUND_KEY_LEN,
+	.key_len = sizeof_field(struct rds_sock, rs_bound_key),
 	.key_offset = offsetof(struct rds_sock, rs_bound_key),
 	.head_offset = offsetof(struct rds_sock, rs_bound_node),
 	.max_size = 16384,
diff --git a/net/sched/cls_flower.c b/net/sched/cls_flower.c
index 1008ec8a464c..ec127f9ff56e 100644
--- a/net/sched/cls_flower.c
+++ b/net/sched/cls_flower.c
@@ -147,7 +147,7 @@  struct cls_fl_filter {
 
 static const struct rhashtable_params mask_ht_params = {
 	.key_offset = offsetof(struct fl_flow_mask, key),
-	.key_len = sizeof(struct fl_flow_key),
+	.key_len = sizeof_field(struct fl_flow_mask, key),
 	.head_offset = offsetof(struct fl_flow_mask, ht_node),
 	.automatic_shrinking = true,
 };
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 65dcbb54f55d..ab76c8c1580a 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -3041,7 +3041,7 @@  static const struct rhashtable_params tsk_rht_params = {
 	.nelem_hint = 192,
 	.head_offset = offsetof(struct tipc_sock, node),
 	.key_offset = offsetof(struct tipc_sock, portid),
-	.key_len = sizeof(u32), /* portid */
+	.key_len = sizeof_field(struct tipc_sock, portid),
 	.max_size = 1048576,
 	.min_size = 256,
 	.automatic_shrinking = true,