cgroup: avoid flushing global workqueue in cgroup1_pidlist_destroy_all
Hi Michal,
Thanks for the review.
Quoted 7 lines
a) a single reader taking more than
hung_task_timeout_secs(that'd be a softlockup earlier),
b) starvation ofcgroup1_pidlist_destroy_all()by many (queued)cgroup_pidlist_start()callers which goes against the second-long caching of pidlists,
c) there is large number ofnr_cgroups*nr_pidnseswhich makes the caching ineffective
An honest caveat first: this was reported by a customer on a production Kubernetes node. A guest memory dump was taken at the time, but it couldn't be analyzed with crash, so I can't give you the exact nr_cgroups/nr_pidnses.
That said, I believe c) alone is sufficient, and neither a) nor b) is needed to explain the 120s stall, because the flush latency is backlog x per-work latency rather than the latency of any single work:
- backlog: flush_workqueue() waits for all works already queued on the shared wq, which drains serially (WQ_PERCPU, max_active=1). Container churn constantly runs cgroup destruction, and each destroyed cgroup queues one work per cached (type, ns) pidlist, so the queue ahead of the flusher grows with churn rate x nr_cgroups.
- per-work latency: each destroy work must acquire the owner's pidlist_mutex, contending with readers (kubelet/cadvisor/runtime scraping cgroup.procs). pidlist_array_load() runs entirely under that mutex (css_set walk, kvmalloc, sort), so on a node with frequent scraping each work can sit tens of ms behind a reader.
A few thousand queued works each delayed by tens of ms already exceed hung_task_timeout_secs -- no single reader needs to hold the mutex for that long (so a) doesn't apply), and each individual work does get the mutex eventually, so it's not sustained starvation either (so b) doesn't apply).
More on b): the second-long caching only helps readers that re-read within that 1s window (e.g. a single cat doing several seq_file iterations). Periodic scrapers like kubelet/cadvisor, with typical intervals of 10-30s, never hit the cache at all: every scrape round rebuilds the pidlist under the mutex and queues an expiry work 1s later. So heavy reader traffic and the existence of the cache are not in conflict -- the cache is simply ineffective for this access pattern. It also means the wq steadily carries ~nr_cgroups expiry works per scrape round, on top of the works queued by churn, which is what the flusher ends up waiting behind.
On nr_pidnses specifically: containers typically share the pod/host pid namespace, so in the common case the multiplier is really nr_cgroups x scraping frequency rather than nr_pidnses. I can't confirm the customer's pidns usage for the same reason as above.
Quoted 1 line
OTOH, I'm surprised this v1-issue popped up only now
cgroup v1 is legacy but still the default on widely deployed enterprise distros, and per-node cgroup density plus metrics scraping frequency have grown a lot in recent years, so the backlog needed to trip this only became common recently. Triggering it also requires all three conditions at once -- many cgroups, frequent full-sweep scraping, and sustained create/destroy churn -- which is presumably why it isn't seen more often: missing any one of them, the queue never builds up. That's my best explanation -- admittedly not provable without an analyzable dump.
Quoted 2 lines
What's the point of the workqueue after this change? (Mainly the expiration + having process context for the handler.)
It still serves the normal path: the deferred expiry that makes the pidlist cache work, and process context for freeing. The patch only removes the flush from the cgroup-removal path where, as you noted, the orphan list guarantees the pidlists head won't be used after cgrp removal.
If the lack of exact field numbers is a blocker, I can put together a synthetic reproducer (N cgroups with concurrent cgroup.procs readers plus destroy churn) and report measured flush_workqueue() latency with and without the patch. Would that address your concern?
Thanks,
Junnan