Skip to content

Commit f68ad16

Browse files
Florian Westphalgregkh
authored andcommitted
netfilter: conntrack: collect all entries in one cycle
[ Upstream commit 4608fdf ] Michal Kubecek reports that conntrack gc is responsible for frequent wakeups (every 125ms) on idle systems. On busy systems, timed out entries are evicted during lookup. The gc worker is only needed to remove entries after system becomes idle after a busy period. To resolve this, always scan the entire table. If the scan is taking too long, reschedule so other work_structs can run and resume from next bucket. After a completed scan, wait for 2 minutes before the next cycle. Heuristics for faster re-schedule are removed. GC_SCAN_INTERVAL could be exposed as a sysctl in the future to allow tuning this as-needed or even turn the gc worker off. Reported-by: Michal Kubecek <mkubecek@suse.cz> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a13a2df commit f68ad16

1 file changed

Lines changed: 22 additions & 49 deletions

File tree

‎net/netfilter/nf_conntrack_core.c‎

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,17 @@ EXPORT_SYMBOL_GPL(nf_conntrack_hash);
6666

6767
struct conntrack_gc_work {
6868
struct delayed_work dwork;
69-
u32 last_bucket;
69+
u32 next_bucket;
7070
bool exiting;
7171
bool early_drop;
72-
long next_gc_run;
7372
};
7473

7574
static __read_mostly struct kmem_cache *nf_conntrack_cachep;
7675
static DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
7776
static __read_mostly bool nf_conntrack_locks_all;
7877

79-
/* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */
80-
#define GC_MAX_BUCKETS_DIV 128u
81-
/* upper bound of full table scan */
82-
#define GC_MAX_SCAN_JIFFIES (16u * HZ)
83-
/* desired ratio of entries found to be expired */
84-
#define GC_EVICT_RATIO 50u
78+
#define GC_SCAN_INTERVAL (120u * HZ)
79+
#define GC_SCAN_MAX_DURATION msecs_to_jiffies(10)
8580

8681
static struct conntrack_gc_work conntrack_gc_work;
8782

@@ -1352,48 +1347,42 @@ static bool gc_worker_can_early_drop(const struct nf_conn *ct)
13521347

13531348
static void gc_worker(struct work_struct *work)
13541349
{
1355-
unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
1356-
unsigned int i, goal, buckets = 0, expired_count = 0;
1357-
unsigned int nf_conntrack_max95 = 0;
1350+
unsigned long end_time = jiffies + GC_SCAN_MAX_DURATION;
1351+
unsigned int i, hashsz, nf_conntrack_max95 = 0;
1352+
unsigned long next_run = GC_SCAN_INTERVAL;
13581353
struct conntrack_gc_work *gc_work;
1359-
unsigned int ratio, scanned = 0;
1360-
unsigned long next_run;
1361-
13621354
gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
13631355

1364-
goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
1365-
i = gc_work->last_bucket;
1356+
i = gc_work->next_bucket;
13661357
if (gc_work->early_drop)
13671358
nf_conntrack_max95 = nf_conntrack_max / 100u * 95u;
13681359

13691360
do {
13701361
struct nf_conntrack_tuple_hash *h;
13711362
struct hlist_nulls_head *ct_hash;
13721363
struct hlist_nulls_node *n;
1373-
unsigned int hashsz;
13741364
struct nf_conn *tmp;
13751365

1376-
i++;
13771366
rcu_read_lock();
13781367

13791368
nf_conntrack_get_ht(&ct_hash, &hashsz);
1380-
if (i >= hashsz)
1381-
i = 0;
1369+
if (i >= hashsz) {
1370+
rcu_read_unlock();
1371+
break;
1372+
}
13821373

13831374
hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
13841375
struct net *net;
13851376

13861377
tmp = nf_ct_tuplehash_to_ctrack(h);
13871378

1388-
scanned++;
13891379
if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
13901380
nf_ct_offload_timeout(tmp);
13911381
continue;
13921382
}
13931383

13941384
if (nf_ct_is_expired(tmp)) {
13951385
nf_ct_gc_expired(tmp);
1396-
expired_count++;
13971386
continue;
13981387
}
13991388

@@ -1425,7 +1414,14 @@ static void gc_worker(struct work_struct *work)
14251414
*/
14261415
rcu_read_unlock();
14271416
cond_resched();
1428-
} while (++buckets < goal);
1417+
i++;
1418+
1419+
if (time_after(jiffies, end_time) && i < hashsz) {
1420+
gc_work->next_bucket = i;
1421+
next_run = 0;
1422+
break;
1423+
}
1424+
} while (i < hashsz);
14291425

14301426
if (gc_work->exiting)
14311427
return;
@@ -1436,40 +1432,17 @@ static void gc_worker(struct work_struct *work)
14361432
*
14371433
* This worker is only here to reap expired entries when system went
14381434
* idle after a busy period.
1439-
*
1440-
* The heuristics below are supposed to balance conflicting goals:
1441-
*
1442-
* 1. Minimize time until we notice a stale entry
1443-
* 2. Maximize scan intervals to not waste cycles
1444-
*
1445-
* Normally, expire ratio will be close to 0.
1446-
*
1447-
* As soon as a sizeable fraction of the entries have expired
1448-
* increase scan frequency.
14491435
*/
1450-
ratio = scanned ? expired_count * 100 / scanned : 0;
1451-
if (ratio > GC_EVICT_RATIO) {
1452-
gc_work->next_gc_run = min_interval;
1453-
} else {
1454-
unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
1455-
1456-
BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1457-
1458-
gc_work->next_gc_run += min_interval;
1459-
if (gc_work->next_gc_run > max)
1460-
gc_work->next_gc_run = max;
1436+
if (next_run) {
1437+
gc_work->early_drop = false;
1438+
gc_work->next_bucket = 0;
14611439
}
1462-
1463-
next_run = gc_work->next_gc_run;
1464-
gc_work->last_bucket = i;
1465-
gc_work->early_drop = false;
14661440
queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
14671441
}
14681442

14691443
static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
14701444
{
14711445
INIT_DEFERRABLE_WORK(&gc_work->dwork, gc_worker);
1472-
gc_work->next_gc_run = HZ;
14731446
gc_work->exiting = false;
14741447
}
14751448

0 commit comments

Comments
 (0)