net: use get_random_u{16,32,64}() where appropriate
Subsystems (8)
PATCH v4 1/1 net: use get_random_u{16,32,64}() where appropriate
Use the typed random integer helpers instead of get_random_bytes() when filling a single integer variable. The helpers return the value directly, require no pointer or size argument, and better express intent.
Skipped sites writing into __be16 (netdevsim) and __le64 (ceph) fields where a direct assignment would trigger sparse endianness warnings.
---
Notes:
Changes v3 -> v4:
- Dropped net/ceph/auth_x.c site: client_challenge is __le64, direct assignment triggers sparse endianness warning (Matthieu Baerts)
- Added Reviewed-by from Matthieu Baerts for net/mptcp changes
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/Z/BfE0zn+DJxhBH7@debian/
drivers/net/netdevsim/psample.c | 4 ++-- net/core/net_namespace.c | 2 +- net/mac80211/mesh_plink.c | 2 +- net/mptcp/subflow.c | 4 ++-- net/openvswitch/flow_table.c | 2 +- net/sctp/sm_make_chunk.c | 4 ++-- net/tipc/node.c | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-)
drivers/net/netdevsim/psample.c +2 -2
@@ -94,7 +94,7 @@ static void nsim_dev_psample_md_prepare(const struct nsim_dev_psample *psample,if (psample->out_tc_occ_max) {u64 out_tc_occ; -get_random_bytes (&out_tc_occ,sizeof (u64)); + out_tc_occ =get_random_u64 (); md->out_tc_occ = out_tc_occ & (psample->out_tc_occ_max -1 ); md->out_tc_occ_valid =1 ; }
@@ -102,7 +102,7 @@ static void nsim_dev_psample_md_prepare(const struct nsim_dev_psample *psample,if (psample->latency_max) {u64 latency; -get_random_bytes (&latency,sizeof (u64)); + latency =get_random_u64 (); md->latency = latency & (psample->latency_max -1 ); md->latency_valid =1 ; }
net/core/net_namespace.c +1 -1
@@ -411,7 +411,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_nref_tracker_dir_init (&net->refcnt_tracker,128 ,"net_refcnt" );ref_tracker_dir_init (&net->notrefcnt_tracker,128 ,"net_notrefcnt" ); -get_random_bytes (&net->hash_mix,sizeof (u32)); + net->hash_mix =get_random_u32 (); net->dev_base_seq =1 ; net->user_ns = user_ns;
net/mac80211/mesh_plink.c +1 -1
@@ -712,7 +712,7 @@ void mesh_plink_timer(struct timer_list *t)"Mesh plink for %pM (retry, timeout): %d %d\n" , sta->sta.addr, sta->mesh->plink_retries, sta->mesh->plink_timeout); -get_random_bytes (&rand,sizeof (u32)); + rand =get_random_u32 (); sta->mesh->plink_timeout = sta->mesh->plink_timeout + rand % sta->mesh->plink_timeout; ++sta->mesh->plink_retries;
net/mptcp/subflow.c +2 -2
@@ -72,7 +72,7 @@ static void subflow_req_create_thmac(struct mptcp_subflow_request_sock *subflow_struct mptcp_sock *msk = subflow_req->msk;u8 hmac[SHA256_DIGEST_SIZE ]; -get_random_bytes (&subflow_req->local_nonce,sizeof (u32)); + subflow_req->local_nonce =get_random_u32 (); subflow_generate_hmac(READ_ONCE(msk->local_key), READ_ONCE(msk->remote_key),
@@ -1639,7 +1639,7 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_pm_local *local, ssk = sf->sk; subflow =mptcp_subflow_ctx (ssk);do { -get_random_bytes (&subflow->local_nonce,sizeof (u32)); + subflow->local_nonce =get_random_u32 (); }while (!subflow->local_nonce);/* if 'IPADDRANY', the ID will be set later, after the routing */
net/openvswitch/flow_table.c +1 -1
@@ -167,7 +167,7 @@ static struct table_instance *table_instance_alloc(int new_size) ti->n_buckets = new_size; ti->node_ver =0 ; -get_random_bytes (&ti->hash_seed,sizeof (u32)); + ti->hash_seed =get_random_u32 ();return ti; }
net/sctp/sm_make_chunk.c +2 -2
@@ -2727,7 +2727,7 @@ __u32 sctp_generate_tag(const struct sctp_endpoint *ep)__u32 x;do { -get_random_bytes (&x,sizeof (__u32)); + x =get_random_u32 (); }while (x ==0 );return x;
@@ -2738,7 +2738,7 @@ __u32 sctp_generate_tsn(const struct sctp_endpoint *ep) {__u32 retval; -get_random_bytes (&retval,sizeof (__u32)); + retval =get_random_u32 ();return retval; }
net/tipc/node.c +1 -1
@@ -1275,7 +1275,7 @@ void tipc_node_check_dest(struct net *net, u32 addr, goto exit; if_name =strchr (b->name,':' ) +1 ; -get_random_bytes (&session,sizeof (u16)); + session =get_random_u16 (); if (!tipc_link_create(net, if_name, b->identity, b->tolerance, b->net_plane, b->mtu, b->priority, b->min_win, b->max_win, session,
Signature · 1 line
2.53.0
Quoted 6 lines · David Carlier
Use the typed random integer helpers instead of
get_random_bytes()when filling a single integer variable. The helpers return the value directly, require no pointer or size argument, and better express intent.
Quoted 6 lines
Skipped sites writing into
__be16(netdevsim) and__le64(ceph) fields where a direct assignment would trigger sparse endianness warnings.Signed-off-by David Carlier <[email protected]>Reviewed-by Matthieu Baerts (NGI0) <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Tue, 7 Apr 2026 16:07:58 +0100 you wrote:
Use the typed random integer helpers instead of
get_random_bytes()when filling a single integer variable. The helpers return the value directly, require no pointer or size argument, and better express intent.Skipped sites writing into
__be16(netdevsim) and__le64(ceph) fields where a direct assignment would trigger sparse endianness warnings.[...]
Here is the summary with links:
- [v4,net-next] net: use get_random_u{16,32,64}() where appropriate
https://git.kernel.org/netdev/net-next/c/9addea5d44b6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html