Skip to content

Commit

Permalink
ipv4: speedup ip_idents_reserve()
Browse files Browse the repository at this point in the history
Under stress, ip_idents_reserve() is accessing a contended
cache line twice, with non optimal MESI transactions.

If we place timestamps in separate location, we reduce this
pressure by ~50% and allow atomic_add_return() to issue
a Request for Ownership.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Eric Dumazet authored and David S. Miller committed May 1, 2015
1 parent 9dd3c79 commit 355b590
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions net/ipv4/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,28 +457,26 @@ static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
}

#define IP_IDENTS_SZ 2048u
struct ip_ident_bucket {
atomic_t id;
u32 stamp32;
};

static struct ip_ident_bucket *ip_idents __read_mostly;
static atomic_t *ip_idents __read_mostly;
static u32 *ip_tstamps __read_mostly;

/* In order to protect privacy, we add a perturbation to identifiers
* if one generator is seldom used. This makes hard for an attacker
* to infer how many packets were sent between two points in time.
*/
u32 ip_idents_reserve(u32 hash, int segs)
{
struct ip_ident_bucket *bucket = ip_idents + hash % IP_IDENTS_SZ;
u32 old = ACCESS_ONCE(bucket->stamp32);
u32 *p_tstamp = ip_tstamps + hash % IP_IDENTS_SZ;
atomic_t *p_id = ip_idents + hash % IP_IDENTS_SZ;
u32 old = ACCESS_ONCE(*p_tstamp);
u32 now = (u32)jiffies;
u32 delta = 0;

if (old != now && cmpxchg(&bucket->stamp32, old, now) == old)
if (old != now && cmpxchg(p_tstamp, old, now) == old)
delta = prandom_u32_max(now - old);

return atomic_add_return(segs + delta, &bucket->id) - segs;
return atomic_add_return(segs + delta, p_id) - segs;
}
EXPORT_SYMBOL(ip_idents_reserve);

Expand Down Expand Up @@ -2741,6 +2739,10 @@ int __init ip_rt_init(void)

prandom_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents));

ip_tstamps = kcalloc(IP_IDENTS_SZ, sizeof(*ip_tstamps), GFP_KERNEL);
if (!ip_tstamps)
panic("IP: failed to allocate ip_tstamps\n");

for_each_possible_cpu(cpu) {
struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);

Expand Down

0 comments on commit 355b590

Please sign in to comment.