Skip to content

Commit 4608fdf

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: conntrack: collect all entries in one cycle
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>
1 parent 38ea9de commit 4608fdf

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

@@ -1363,49 +1358,43 @@ static bool gc_worker_can_early_drop(const struct nf_conn *ct)
13631358

13641359
static void gc_worker(struct work_struct *work)
13651360
{
1366-
unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
1367-
unsigned int i, goal, buckets = 0, expired_count = 0;
1368-
unsigned int nf_conntrack_max95 = 0;
1361+
unsigned long end_time = jiffies + GC_SCAN_MAX_DURATION;
1362+
unsigned int i, hashsz, nf_conntrack_max95 = 0;
1363+
unsigned long next_run = GC_SCAN_INTERVAL;
13691364
struct conntrack_gc_work *gc_work;
1370-
unsigned int ratio, scanned = 0;
1371-
unsigned long next_run;
1372-
13731365
gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
13741366

1375-
goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
1376-
i = gc_work->last_bucket;
1367+
i = gc_work->next_bucket;
13771368
if (gc_work->early_drop)
13781369
nf_conntrack_max95 = nf_conntrack_max / 100u * 95u;
13791370

13801371
do {
13811372
struct nf_conntrack_tuple_hash *h;
13821373
struct hlist_nulls_head *ct_hash;
13831374
struct hlist_nulls_node *n;
1384-
unsigned int hashsz;
13851375
struct nf_conn *tmp;
13861376

1387-
i++;
13881377
rcu_read_lock();
13891378

13901379
nf_conntrack_get_ht(&ct_hash, &hashsz);
1391-
if (i >= hashsz)
1392-
i = 0;
1380+
if (i >= hashsz) {
1381+
rcu_read_unlock();
1382+
break;
1383+
}
13931384

13941385
hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
13951386
struct nf_conntrack_net *cnet;
13961387
struct net *net;
13971388

13981389
tmp = nf_ct_tuplehash_to_ctrack(h);
13991390

1400-
scanned++;
14011391
if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
14021392
nf_ct_offload_timeout(tmp);
14031393
continue;
14041394
}
14051395

14061396
if (nf_ct_is_expired(tmp)) {
14071397
nf_ct_gc_expired(tmp);
1408-
expired_count++;
14091398
continue;
14101399
}
14111400

@@ -1438,7 +1427,14 @@ static void gc_worker(struct work_struct *work)
14381427
*/
14391428
rcu_read_unlock();
14401429
cond_resched();
1441-
} while (++buckets < goal);
1430+
i++;
1431+
1432+
if (time_after(jiffies, end_time) && i < hashsz) {
1433+
gc_work->next_bucket = i;
1434+
next_run = 0;
1435+
break;
1436+
}
1437+
} while (i < hashsz);
14421438

14431439
if (gc_work->exiting)
14441440
return;
@@ -1449,40 +1445,17 @@ static void gc_worker(struct work_struct *work)
14491445
*
14501446
* This worker is only here to reap expired entries when system went
14511447
* idle after a busy period.
1452-
*
1453-
* The heuristics below are supposed to balance conflicting goals:
1454-
*
1455-
* 1. Minimize time until we notice a stale entry
1456-
* 2. Maximize scan intervals to not waste cycles
1457-
*
1458-
* Normally, expire ratio will be close to 0.
1459-
*
1460-
* As soon as a sizeable fraction of the entries have expired
1461-
* increase scan frequency.
14621448
*/
1463-
ratio = scanned ? expired_count * 100 / scanned : 0;
1464-
if (ratio > GC_EVICT_RATIO) {
1465-
gc_work->next_gc_run = min_interval;
1466-
} else {
1467-
unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
1468-
1469-
BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1470-
1471-
gc_work->next_gc_run += min_interval;
1472-
if (gc_work->next_gc_run > max)
1473-
gc_work->next_gc_run = max;
1449+
if (next_run) {
1450+
gc_work->early_drop = false;
1451+
gc_work->next_bucket = 0;
14741452
}
1475-
1476-
next_run = gc_work->next_gc_run;
1477-
gc_work->last_bucket = i;
1478-
gc_work->early_drop = false;
14791453
queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
14801454
}
14811455

14821456
static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
14831457
{
14841458
INIT_DEFERRABLE_WORK(&gc_work->dwork, gc_worker);
1485-
gc_work->next_gc_run = HZ;
14861459
gc_work->exiting = false;
14871460
}
14881461

0 commit comments

Comments
 (0)