Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/ttm: race in ttm_pool_type_give() and spin in ttm_pool_shrink_idle() - [High] drm/ttm: unbatched set_pages_wb() IPI storm in ttm_pool_shrink_idle() --
commit be4ff4c980b2546ac4ff032bfa520f015175f024 Author: Eduardo P. Gomez <[email protected]> drm/ttm: reclaim pooled pages after a period of inactivity This commit adds a delayed work item to drain pooled TTM pages through ttm_pool_shrink() after five seconds without page returns, and moves page accounting in ttm_pool_type_give() ahead of list_lru_add(). > diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c > index 1bf37023fed66..6af8a9c894b4e 100644 > --- a/drivers/gpu/drm/ttm/ttm_pool.c > +++ b/drivers/gpu/drm/ttm/ttm_pool.c [ ... ] > @@ -335,14 +365,17 @@ static void ttm_pool_type_give(struct ttm_pool_type > *pt, struct page *p) > clear_page(page_address(p + i)); > } > > + /* Account for the pages before a concurrent shrinker can take them. */ > + atomic_long_add(num_pages, &allocated_pages[nid]); > + mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages); > + mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages); > + > INIT_LIST_HEAD(&p->lru); > rcu_read_lock(); > list_lru_add(&pt->pages, &p->lru, nid, NULL); > rcu_read_unlock(); > > - atomic_long_add(num_pages, &allocated_pages[nid]); > - mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages); > - mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages); > + ttm_pool_note_return(); > } [Severity: High] Can ttm_pool_shrink_idle() immediately reclaim a page that is being returned here, or spin when allocated_pages[nid] is ahead of the LRU? Because allocated_pages[nid] and pt->pages are updated before ttm_pool_note_return() refreshes pool_last_return, an already-running idle worker sees the new page while pool_last_return still holds the old expired timestamp: CPU0 CPU1 (kworker) ttm_pool_type_give() ttm_pool_shrink_idle() atomic_long_add(..., &allocated_pages[nid]) list_lru_add(&pt->pages, ...) atomic_long_read(&allocated_pages[nid]) expires = READ_ONCE(pool_last_return) + pool_idle_delay time_before(now, expires) == false ttm_pool_shrink(nid, batch) <-- reclaims just-returned page ttm_pool_note_return() WRITE_ONCE(pool_last_return, jiffies) In addition, ttm_pool_shrink_idle() loops for as long as atomic_long_read(&allocated_pages[nid]) > 0 without checking whether ttm_pool_shrink() freed any pages. allocated_pages[nid] is positive while no pages remain on any pt->pages LRU both between atomic_long_add() and list_lru_add() above (with preemption enabled) and while concurrent ttm_pool_dispose_list() calls are freeing isolated pages before decrementing allocated_pages[nid]. During those windows, ttm_pool_shrink() returns 0 on every call and ttm_pool_shrink_idle() spins acquiring pool_shrink_rwsem and shrinker_lock; if direct reclaim preempts ttm_pool_type_give() between atomic_long_add() and list_lru_add(), ttm_pool_shrinker_scan() can also spin without cond_resched(). Updating pool_last_return before publishing the page and breaking out of the shrink loop when a full pass over shrinker_list frees no pages would avoid both issues. [ ... ] > @@ -473,6 +506,30 @@ static unsigned int ttm_pool_shrink(int nid, unsigned > long num_to_free) > return num_pages; > } > > +/* Return unused pool pages after the last return has aged out. */ > +static void ttm_pool_shrink_idle(struct work_struct *work) > +{ > + const unsigned long batch = 1UL << (MAX_PAGE_ORDER / 2); > + int nid; > + > + for_each_node_state(nid, N_MEMORY) { > + while (atomic_long_read(&allocated_pages[nid]) > 0) { > + unsigned long now = jiffies; > + unsigned long expires = READ_ONCE(pool_last_return) + > + pool_idle_delay; > + > + if (time_before(now, expires)) { > + queue_delayed_work(system_dfl_long_wq, > &pool_idle_work, > + expires - now); > + return; > + } > + > + ttm_pool_shrink(nid, batch); > + cond_resched(); > + } > + } > +} [Severity: High] Does draining the pool through ttm_pool_shrink() trigger an IPI storm of unbatched set_pages_wb() calls on x86? ttm_pool_shrink() frees pages one entry at a time via ttm_pool_dispose_list() -> ttm_pool_free_page(): drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_free_page() { ... #ifdef CONFIG_X86 /* We don't care that set_pages_wb is inefficient here. This is only * used when we have to shrink and CPU overhead is irrelevant then. */ if (caching != ttm_cached && !PageHighMem(p)) set_pages_wb(p, 1 << order); #endif ... } Unlike ttm_pool_backup(), which batches page-attribute changes with set_pages_array_wb(), each set_pages_wb() call here issues a synchronous cross-CPU TLB shootdown IPI to every online CPU for a single page block. Because ttm_pool_shrink() also inspects and rotates only one ttm_pool_type per call across the global and per-device entries on shrinker_list, draining a single populated pool type walks dozens of empty LRUs under pool_shrink_rwsem and shrinker_lock for every 32-entry batch. Later commit "drm/ttm: retain a reserve during idle pool reclaim" in this series keeps a 128 MiB per-node reserve, but any pooled WC/UC pages above that threshold still hit this path five seconds after the last return. Also, pool_last_return is only updated in ttm_pool_type_give() and not in ttm_pool_type_take(), so ttm_pool_shrink_idle() will drain the pool even while a workload is actively taking pages via ttm_pool_alloc(). -- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
