Skip to content

Commit 8fc54f6

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: use reciprocal_scale() helper
Replace open codings of (((u64) <x> * <y>) >> 32) with reciprocal_scale(). Signed-off-by: Daniel Borkmann <[email protected]> Cc: Hannes Frederic Sowa <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 690e36e commit 8fc54f6

File tree

14 files changed

+24
-22
lines changed

14 files changed

+24
-22
lines changed

net/core/dev.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,8 +3124,7 @@ static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
31243124
}
31253125

31263126
if (map) {
3127-
tcpu = map->cpus[((u64) hash * map->len) >> 32];
3128-
3127+
tcpu = map->cpus[reciprocal_scale(hash, map->len)];
31293128
if (cpu_online(tcpu)) {
31303129
cpu = tcpu;
31313130
goto done;

net/core/flow_dissector.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
298298
qcount = dev->tc_to_txq[tc].count;
299299
}
300300

301-
return (u16) (((u64)skb_get_hash(skb) * qcount) >> 32) + qoffset;
301+
return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
302302
}
303303
EXPORT_SYMBOL(__skb_tx_hash);
304304

@@ -371,9 +371,8 @@ static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
371371
if (map->len == 1)
372372
queue_index = map->queues[0];
373373
else
374-
queue_index = map->queues[
375-
((u64)skb_get_hash(skb) * map->len) >> 32];
376-
374+
queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
375+
map->len)];
377376
if (unlikely(queue_index >= dev->real_num_tx_queues))
378377
queue_index = -1;
379378
}

net/ipv4/inet_hashtables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct sock *__inet_lookup_listener(struct net *net,
229229
}
230230
} else if (score == hiscore && reuseport) {
231231
matches++;
232-
if (((u64)phash * matches) >> 32 == 0)
232+
if (reciprocal_scale(phash, matches) == 0)
233233
result = sk;
234234
phash = next_pseudo_random32(phash);
235235
}

net/ipv4/netfilter/ipt_CLUSTERIP.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ clusterip_hashfn(const struct sk_buff *skb,
285285
}
286286

287287
/* node numbers are 1..n, not 0..n */
288-
return (((u64)hashval * config->num_total_nodes) >> 32) + 1;
288+
return reciprocal_scale(hashval, config->num_total_nodes) + 1;
289289
}
290290

291291
static inline int

net/ipv4/udp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
224224
remaining = (high - low) + 1;
225225

226226
rand = prandom_u32();
227-
first = (((u64)rand * remaining) >> 32) + low;
227+
first = reciprocal_scale(rand, remaining) + low;
228228
/*
229229
* force rand to be an odd multiple of UDP_HTABLE_SIZE
230230
*/
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
448448
}
449449
} else if (score == badness && reuseport) {
450450
matches++;
451-
if (((u64)hash * matches) >> 32 == 0)
451+
if (reciprocal_scale(hash, matches) == 0)
452452
result = sk;
453453
hash = next_pseudo_random32(hash);
454454
}
@@ -529,7 +529,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
529529
}
530530
} else if (score == badness && reuseport) {
531531
matches++;
532-
if (((u64)hash * matches) >> 32 == 0)
532+
if (reciprocal_scale(hash, matches) == 0)
533533
result = sk;
534534
hash = next_pseudo_random32(hash);
535535
}

net/ipv6/inet6_hashtables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct sock *inet6_lookup_listener(struct net *net,
198198
}
199199
} else if (score == hiscore && reuseport) {
200200
matches++;
201-
if (((u64)phash * matches) >> 32 == 0)
201+
if (reciprocal_scale(phash, matches) == 0)
202202
result = sk;
203203
phash = next_pseudo_random32(phash);
204204
}

net/ipv6/udp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
243243
goto exact_match;
244244
} else if (score == badness && reuseport) {
245245
matches++;
246-
if (((u64)hash * matches) >> 32 == 0)
246+
if (reciprocal_scale(hash, matches) == 0)
247247
result = sk;
248248
hash = next_pseudo_random32(hash);
249249
}
@@ -323,7 +323,7 @@ struct sock *__udp6_lib_lookup(struct net *net,
323323
}
324324
} else if (score == badness && reuseport) {
325325
matches++;
326-
if (((u64)hash * matches) >> 32 == 0)
326+
if (reciprocal_scale(hash, matches) == 0)
327327
result = sk;
328328
hash = next_pseudo_random32(hash);
329329
}

net/netfilter/nf_conntrack_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple, u16 zone)
142142

143143
static u32 __hash_bucket(u32 hash, unsigned int size)
144144
{
145-
return ((u64)hash * size) >> 32;
145+
return reciprocal_scale(hash, size);
146146
}
147147

148148
static u32 hash_bucket(u32 hash, const struct net *net)

net/netfilter/nf_conntrack_expect.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple
8383
hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
8484
(((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
8585
(__force __u16)tuple->dst.u.all) ^ nf_conntrack_hash_rnd);
86-
return ((u64)hash * nf_ct_expect_hsize) >> 32;
86+
87+
return reciprocal_scale(hash, nf_ct_expect_hsize);
8788
}
8889

8990
struct nf_conntrack_expect *

net/netfilter/nf_nat_core.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ hash_by_src(const struct net *net, u16 zone,
126126
/* Original src, to ensure we map it consistently if poss. */
127127
hash = jhash2((u32 *)&tuple->src, sizeof(tuple->src) / sizeof(u32),
128128
tuple->dst.protonum ^ zone ^ nf_conntrack_hash_rnd);
129-
return ((u64)hash * net->ct.nat_htable_size) >> 32;
129+
130+
return reciprocal_scale(hash, net->ct.nat_htable_size);
130131
}
131132

132133
/* Is this tuple already taken? (not by us) */
@@ -274,7 +275,7 @@ find_best_ips_proto(u16 zone, struct nf_conntrack_tuple *tuple,
274275
}
275276

276277
var_ipp->all[i] = (__force __u32)
277-
htonl(minip + (((u64)j * dist) >> 32));
278+
htonl(minip + reciprocal_scale(j, dist));
278279
if (var_ipp->all[i] != range->max_addr.all[i])
279280
full_range = true;
280281

net/netfilter/xt_HMARK.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ hmark_hash(struct hmark_tuple *t, const struct xt_hmark_info *info)
126126
hash = jhash_3words(src, dst, t->uports.v32, info->hashrnd);
127127
hash = hash ^ (t->proto & info->proto_mask);
128128

129-
return (((u64)hash * info->hmodulus) >> 32) + info->hoffset;
129+
return reciprocal_scale(hash, info->hmodulus) + info->hoffset;
130130
}
131131

132132
static void

net/netfilter/xt_cluster.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ xt_cluster_hash(const struct nf_conn *ct,
5555
WARN_ON(1);
5656
break;
5757
}
58-
return (((u64)hash * info->total_nodes) >> 32);
58+
59+
return reciprocal_scale(hash, info->total_nodes);
5960
}
6061

6162
static inline bool

net/netfilter/xt_hashlimit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
135135
* give results between [0 and cfg.size-1] and same hash distribution,
136136
* but using a multiply, less expensive than a divide
137137
*/
138-
return ((u64)hash * ht->cfg.size) >> 32;
138+
return reciprocal_scale(hash, ht->cfg.size);
139139
}
140140

141141
static struct dsthash_ent *

net/sched/sch_fq_codel.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
7777
hash = jhash_3words((__force u32)keys.dst,
7878
(__force u32)keys.src ^ keys.ip_proto,
7979
(__force u32)keys.ports, q->perturbation);
80-
return ((u64)hash * q->flows_cnt) >> 32;
80+
81+
return reciprocal_scale(hash, q->flows_cnt);
8182
}
8283

8384
static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,

0 commit comments

Comments
 (0)