Skip to content

Commit 62c0798

Browse files
edumazetdavem330
authored andcommitted
once: add DO_ONCE_SLOW() for sleepable contexts
Christophe Leroy reported a ~80ms latency spike happening at first TCP connect() time. This is because __inet_hash_connect() uses get_random_once() to populate a perturbation table which became quite big after commit 4c2c8f0 ("tcp: increase source port perturb table to 2^16") get_random_once() uses DO_ONCE(), which block hard irqs for the duration of the operation. This patch adds DO_ONCE_SLOW() which uses a mutex instead of a spinlock for operations where we prefer to stay in process context. Then __inet_hash_connect() can use get_random_slow_once() to populate its perturbation table. Fixes: 4c2c8f0 ("tcp: increase source port perturb table to 2^16") Fixes: 190cc82 ("tcp: change source port randomizarion at connect() time") Reported-by: Christophe Leroy <[email protected]> Link: https://lore.kernel.org/netdev/CANn89iLAEYBaoYajy0Y9UmGFff5GPxDUoG-ErVB2jDdRNQ5Tug@mail.gmail.com/T/#t Signed-off-by: Eric Dumazet <[email protected]> Cc: Willy Tarreau <[email protected]> Tested-by: Christophe Leroy <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f75886a commit 62c0798

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

include/linux/once.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
#include <linux/types.h>
66
#include <linux/jump_label.h>
77

8+
/* Helpers used from arbitrary contexts.
9+
* Hard irqs are blocked, be cautious.
10+
*/
811
bool __do_once_start(bool *done, unsigned long *flags);
912
void __do_once_done(bool *done, struct static_key_true *once_key,
1013
unsigned long *flags, struct module *mod);
1114

15+
/* Variant for process contexts only. */
16+
bool __do_once_slow_start(bool *done);
17+
void __do_once_slow_done(bool *done, struct static_key_true *once_key,
18+
struct module *mod);
19+
1220
/* Call a function exactly once. The idea of DO_ONCE() is to perform
1321
* a function call such as initialization of random seeds, etc, only
1422
* once, where DO_ONCE() can live in the fast-path. After @func has
@@ -52,7 +60,27 @@ void __do_once_done(bool *done, struct static_key_true *once_key,
5260
___ret; \
5361
})
5462

63+
/* Variant of DO_ONCE() for process/sleepable contexts. */
64+
#define DO_ONCE_SLOW(func, ...) \
65+
({ \
66+
bool ___ret = false; \
67+
static bool __section(".data.once") ___done = false; \
68+
static DEFINE_STATIC_KEY_TRUE(___once_key); \
69+
if (static_branch_unlikely(&___once_key)) { \
70+
___ret = __do_once_slow_start(&___done); \
71+
if (unlikely(___ret)) { \
72+
func(__VA_ARGS__); \
73+
__do_once_slow_done(&___done, &___once_key, \
74+
THIS_MODULE); \
75+
} \
76+
} \
77+
___ret; \
78+
})
79+
5580
#define get_random_once(buf, nbytes) \
5681
DO_ONCE(get_random_bytes, (buf), (nbytes))
5782

83+
#define get_random_slow_once(buf, nbytes) \
84+
DO_ONCE_SLOW(get_random_bytes, (buf), (nbytes))
85+
5886
#endif /* _LINUX_ONCE_H */

lib/once.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,33 @@ void __do_once_done(bool *done, struct static_key_true *once_key,
6666
once_disable_jump(once_key, mod);
6767
}
6868
EXPORT_SYMBOL(__do_once_done);
69+
70+
static DEFINE_MUTEX(once_mutex);
71+
72+
bool __do_once_slow_start(bool *done)
73+
__acquires(once_mutex)
74+
{
75+
mutex_lock(&once_mutex);
76+
if (*done) {
77+
mutex_unlock(&once_mutex);
78+
/* Keep sparse happy by restoring an even lock count on
79+
* this mutex. In case we return here, we don't call into
80+
* __do_once_done but return early in the DO_ONCE_SLOW() macro.
81+
*/
82+
__acquire(once_mutex);
83+
return false;
84+
}
85+
86+
return true;
87+
}
88+
EXPORT_SYMBOL(__do_once_slow_start);
89+
90+
void __do_once_slow_done(bool *done, struct static_key_true *once_key,
91+
struct module *mod)
92+
__releases(once_mutex)
93+
{
94+
*done = true;
95+
mutex_unlock(&once_mutex);
96+
once_disable_jump(once_key, mod);
97+
}
98+
EXPORT_SYMBOL(__do_once_slow_done);

net/ipv4/inet_hashtables.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
958958
if (likely(remaining > 1))
959959
remaining &= ~1U;
960960

961-
net_get_random_once(table_perturb,
962-
INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
961+
get_random_slow_once(table_perturb,
962+
INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
963963
index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
964964

965965
offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);

0 commit comments

Comments
 (0)