net: use reciprocal_scale() helper
authorDaniel Borkmann <dborkman@redhat.com>
Sat, 23 Aug 2014 18:58:54 +0000 (20:58 +0200)
committerDavid S. Miller <davem@davemloft.net>
Sat, 23 Aug 2014 19:21:21 +0000 (12:21 -0700)
Replace open codings of (((u64) <x> * <y>) >> 32) with reciprocal_scale().

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
14 files changed:
net/core/dev.c
net/core/flow_dissector.c
net/ipv4/inet_hashtables.c
net/ipv4/netfilter/ipt_CLUSTERIP.c
net/ipv4/udp.c
net/ipv6/inet6_hashtables.c
net/ipv6/udp.c
net/netfilter/nf_conntrack_core.c
net/netfilter/nf_conntrack_expect.c
net/netfilter/nf_nat_core.c
net/netfilter/xt_HMARK.c
net/netfilter/xt_cluster.c
net/netfilter/xt_hashlimit.c
net/sched/sch_fq_codel.c

index b65a5051361f2dea31a0fac078b3dd656e126cc8..1421dad4cb29cf6dc08f20021e15faef5244ba62 100644 (file)
@@ -3124,8 +3124,7 @@ static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
        }
 
        if (map) {
-               tcpu = map->cpus[((u64) hash * map->len) >> 32];
-
+               tcpu = map->cpus[reciprocal_scale(hash, map->len)];
                if (cpu_online(tcpu)) {
                        cpu = tcpu;
                        goto done;
index 660c6492fb78302ba978e2a280fdcb5248d64189..8ffcc97871c8374acb6fd9088e197cb5c7340e37 100644 (file)
@@ -298,7 +298,7 @@ u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
                qcount = dev->tc_to_txq[tc].count;
        }
 
-       return (u16) (((u64)skb_get_hash(skb) * qcount) >> 32) + qoffset;
+       return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
 }
 EXPORT_SYMBOL(__skb_tx_hash);
 
@@ -371,9 +371,8 @@ static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
                        if (map->len == 1)
                                queue_index = map->queues[0];
                        else
-                               queue_index = map->queues[
-                                   ((u64)skb_get_hash(skb) * map->len) >> 32];
-
+                               queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
+                                                                          map->len)];
                        if (unlikely(queue_index >= dev->real_num_tx_queues))
                                queue_index = -1;
                }
index 43116e8c8e1323cdd8d902c5b88e42187c8df991..9111a4e221557173df0ce08e95632ee059d00b61 100644 (file)
@@ -229,7 +229,7 @@ begin:
                        }
                } else if (score == hiscore && reuseport) {
                        matches++;
-                       if (((u64)phash * matches) >> 32 == 0)
+                       if (reciprocal_scale(phash, matches) == 0)
                                result = sk;
                        phash = next_pseudo_random32(phash);
                }
index 2510c02c2d2168ffcd27f286c7eab3ca75165f26..e90f83a3415b464014cecf0f072b2188f6889b7a 100644 (file)
@@ -285,7 +285,7 @@ clusterip_hashfn(const struct sk_buff *skb,
        }
 
        /* node numbers are 1..n, not 0..n */
-       return (((u64)hashval * config->num_total_nodes) >> 32) + 1;
+       return reciprocal_scale(hashval, config->num_total_nodes) + 1;
 }
 
 static inline int
index f57c0e4c2326fd1a7d4b85cbdb4304657e5660f8..32f9571e776b8237ef612cc9b183eb655191a2b2 100644 (file)
@@ -224,7 +224,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
                remaining = (high - low) + 1;
 
                rand = prandom_u32();
-               first = (((u64)rand * remaining) >> 32) + low;
+               first = reciprocal_scale(rand, remaining) + low;
                /*
                 * force rand to be an odd multiple of UDP_HTABLE_SIZE
                 */
@@ -448,7 +448,7 @@ begin:
                        }
                } else if (score == badness && reuseport) {
                        matches++;
-                       if (((u64)hash * matches) >> 32 == 0)
+                       if (reciprocal_scale(hash, matches) == 0)
                                result = sk;
                        hash = next_pseudo_random32(hash);
                }
@@ -529,7 +529,7 @@ begin:
                        }
                } else if (score == badness && reuseport) {
                        matches++;
-                       if (((u64)hash * matches) >> 32 == 0)
+                       if (reciprocal_scale(hash, matches) == 0)
                                result = sk;
                        hash = next_pseudo_random32(hash);
                }
index 262e13c02ec27dea15154d9b4af5fd413dd1c504..8260190089588d040e46052f33055e5220cbd882 100644 (file)
@@ -198,7 +198,7 @@ begin:
                        }
                } else if (score == hiscore && reuseport) {
                        matches++;
-                       if (((u64)phash * matches) >> 32 == 0)
+                       if (reciprocal_scale(phash, matches) == 0)
                                result = sk;
                        phash = next_pseudo_random32(phash);
                }
index 4836af8f582d32d881f8697c1c92b6909e37ab27..25ffe737b975aecc60ebc741e0ab7d38c10a2832 100644 (file)
@@ -243,7 +243,7 @@ begin:
                                goto exact_match;
                } else if (score == badness && reuseport) {
                        matches++;
-                       if (((u64)hash * matches) >> 32 == 0)
+                       if (reciprocal_scale(hash, matches) == 0)
                                result = sk;
                        hash = next_pseudo_random32(hash);
                }
@@ -323,7 +323,7 @@ begin:
                        }
                } else if (score == badness && reuseport) {
                        matches++;
-                       if (((u64)hash * matches) >> 32 == 0)
+                       if (reciprocal_scale(hash, matches) == 0)
                                result = sk;
                        hash = next_pseudo_random32(hash);
                }
index 0b634e7f165208cf17aec37b0d141c51b7d69d7b..5016a6929085ebdbf151a1f47582d36195885540 100644 (file)
@@ -142,7 +142,7 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple, u16 zone)
 
 static u32 __hash_bucket(u32 hash, unsigned int size)
 {
-       return ((u64)hash * size) >> 32;
+       return reciprocal_scale(hash, size);
 }
 
 static u32 hash_bucket(u32 hash, const struct net *net)
index f87e8f68ad453e9baeec017cc74534e8ce85dfab..91a1837acd0e8fb981ccea73ae262197afecfb33 100644 (file)
@@ -83,7 +83,8 @@ static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple
        hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
                      (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
                       (__force __u16)tuple->dst.u.all) ^ nf_conntrack_hash_rnd);
-       return ((u64)hash * nf_ct_expect_hsize) >> 32;
+
+       return reciprocal_scale(hash, nf_ct_expect_hsize);
 }
 
 struct nf_conntrack_expect *
index 552f97cd9fde5c510ac055642affb66adbda181d..4e0b47831d43a25f021a1eeb2c62c2307b8630e1 100644 (file)
@@ -126,7 +126,8 @@ hash_by_src(const struct net *net, u16 zone,
        /* Original src, to ensure we map it consistently if poss. */
        hash = jhash2((u32 *)&tuple->src, sizeof(tuple->src) / sizeof(u32),
                      tuple->dst.protonum ^ zone ^ nf_conntrack_hash_rnd);
-       return ((u64)hash * net->ct.nat_htable_size) >> 32;
+
+       return reciprocal_scale(hash, net->ct.nat_htable_size);
 }
 
 /* Is this tuple already taken? (not by us) */
@@ -274,7 +275,7 @@ find_best_ips_proto(u16 zone, struct nf_conntrack_tuple *tuple,
                }
 
                var_ipp->all[i] = (__force __u32)
-                       htonl(minip + (((u64)j * dist) >> 32));
+                       htonl(minip + reciprocal_scale(j, dist));
                if (var_ipp->all[i] != range->max_addr.all[i])
                        full_range = true;
 
index 73b73f687c580ccfe66648234caab5f0bdc543b9..02afaf48a7290b15e6f2d56b2ca2c5cb07455b4d 100644 (file)
@@ -126,7 +126,7 @@ hmark_hash(struct hmark_tuple *t, const struct xt_hmark_info *info)
        hash = jhash_3words(src, dst, t->uports.v32, info->hashrnd);
        hash = hash ^ (t->proto & info->proto_mask);
 
-       return (((u64)hash * info->hmodulus) >> 32) + info->hoffset;
+       return reciprocal_scale(hash, info->hmodulus) + info->hoffset;
 }
 
 static void
index f4af1bfafb1c61642ddb56ac490f7e161df5d39d..96fa26b20b67dce9ed2d2abaaf6ce71dda363faf 100644 (file)
@@ -55,7 +55,8 @@ xt_cluster_hash(const struct nf_conn *ct,
                WARN_ON(1);
                break;
        }
-       return (((u64)hash * info->total_nodes) >> 32);
+
+       return reciprocal_scale(hash, info->total_nodes);
 }
 
 static inline bool
index 47dc6836830a9cfe1b329fbdddabfd1d80c4d100..52eb3e03458d95a3f65b8e8cfd39f4b847cc39c0 100644 (file)
@@ -135,7 +135,7 @@ hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
         * give results between [0 and cfg.size-1] and same hash distribution,
         * but using a multiply, less expensive than a divide
         */
-       return ((u64)hash * ht->cfg.size) >> 32;
+       return reciprocal_scale(hash, ht->cfg.size);
 }
 
 static struct dsthash_ent *
index 063b726bf1f8636e3529a2e61d1b43fced918d9c..cc56c8bb9bed7c3487ffd39c7bbed5c445ef144b 100644 (file)
@@ -77,7 +77,8 @@ static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
        hash = jhash_3words((__force u32)keys.dst,
                            (__force u32)keys.src ^ keys.ip_proto,
                            (__force u32)keys.ports, q->perturbation);
-       return ((u64)hash * q->flows_cnt) >> 32;
+
+       return reciprocal_scale(hash, q->flows_cnt);
 }
 
 static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,