page_pool: do not release pool until inflight == 0.
Subsystems (8)
PATCH 1/1 page_pool: do not release pool until inflight == 0.
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple xdp_rxq_info.
--- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 4 +- include/net/page_pool.h | 54 +++----- include/net/xdp_priv.h | 4 - include/trace/events/xdp.h | 19 +-- net/core/page_pool.c | 115 ++++++++++------ net/core/xdp.c | 125 +++++++----------- 6 files changed, 137 insertions(+), 184 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -3
@@ -1502,10 +1502,8 @@ static void free_dma_rx_desc_resources(struct stmmac_priv *priv) rx_q->dma_erx, rx_q->dma_rx_phy);kfree (rx_q->buf_pool); -if (rx_q->page_pool) { -page_pool_request_shutdown (rx_q->page_pool); +if (rx_q->page_pool)page_pool_destroy (rx_q->page_pool); - } } }
include/net/page_pool.h +15 -39
83 lines not on this page · raw message
include/net/xdp_priv.h +0 -4
@@ -12,12 +12,8 @@ struct xdp_mem_allocator {struct page_pool *page_pool; struct zero_copy_allocator *zc_alloc; }; -int disconnect_cnt; -unsigned long defer_start;struct rhash_head node;struct rcu_head rcu; -struct delayed_work defer_wq; -unsigned long defer_warn; };#endif /* __LINUX_NET_XDP_PRIV_H__ */
include/trace/events/xdp.h +4 -15
@@ -317,19 +317,15 @@ __MEM_TYPE_MAP(__MEM_TYPE_TP_FN) TRACE_EVENT(mem_disconnect, - TP_PROTO(const struct xdp_mem_allocator *xa, - bool safe_to_remove, bool force), + TP_PROTO(const struct xdp_mem_allocator *xa), - TP_ARGS(xa, safe_to_remove, force), + TP_ARGS(xa), TP_STRUCT__entry( __field(const struct xdp_mem_allocator *, xa) __field(u32, mem_id) __field(u32, mem_type) __field(const void *, allocator) - __field(bool, safe_to_remove) - __field(bool, force) - __field(int, disconnect_cnt) ), TP_fast_assign(
@@ -337,19 +333,12 @@ TRACE_EVENT(mem_disconnect, __entry->mem_id = xa->mem.id; __entry->mem_type = xa->mem.type; __entry->allocator = xa->allocator; - __entry->safe_to_remove = safe_to_remove; - __entry->force = force; - __entry->disconnect_cnt = xa->disconnect_cnt; ), -TP_printk ("mem_id=%d mem_type=%s allocator=%p" -" safe_to_remove=%s force=%s disconnect_cnt=%d" , +TP_printk ("mem_id=%d mem_type=%s allocator=%p" , __entry->mem_id,__print_symbolic (__entry->mem_type, __MEM_TYPE_SYM_TAB), - __entry->allocator, - __entry->safe_to_remove ?"true" :"false" , - __entry->force ?"true" :"false" , - __entry->disconnect_cnt + __entry->allocator ) );
net/core/page_pool.c +71 -44
149 lines not on this page · raw message
net/core/xdp.c +46 -79
177 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 9 lines · On Mon, 11 Nov 2019 21:32:10 -0800 Jonathan Lemon <[email protected]>…
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
Can you please change this to:
[... can be used without] xdp memory model.
Quoted 8 lines
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple
xdp_rxq_info.Fixes d956a048cd3f ("xdp: force mem allocator removal and periodic warning")Signed-off-by Jonathan Lemon <[email protected]>---
[...]
net/core/page_pool.c
Quoted 4 lines
net/core/page_pool.c +0
-0
[...]
Found an issue, see below.
Quoted 35 lines
net/core/page_pool.c +3
-24@@ -338,31 +333,10 @@ static void __page_pool_empty_ring(struct page_pool *pool) } } -static void __warn_in_flight (struct page_pool *pool) +static void page_pool_free (struct page_pool *pool) { -u32 release_cnt =atomic_read (&pool->pages_state_release_cnt); -u32 hold_cnt =READ_ONCE (pool->pages_state_hold_cnt); -s32 distance; - - distance =_distance (hold_cnt, release_cnt); - -/* Drivers should fix this, but only problematic when DMA is used */ -WARN (1 ,"Still in-flight pages:%d hold:%u released:%u" , - distance, hold_cnt, release_cnt); -} - -void __page_pool_free (struct page_pool *pool) -{ -/* Only last user actually free/release resources */ -if (!page_pool_put (pool)) -return ; - -WARN (pool->alloc.count,"API usage violation" ); -WARN (!ptr_ring_empty (&pool->ring),"ptr_ring is not empty" ); - -/* Can happen due to forced shutdown */ -if (!__page_pool_safe_to_destroy (pool)) -__warn_in_flight (pool); +if (pool->disconnect) + pool->disconnect (pool);ptr_ring_cleanup (&pool->ring,NULL );
net/core/page_pool.c void __page_pool_free(struct page_pool *pool) · line 374
Quoted 4 lines
net/core/page_pool.c +0
-1@@ -371,12 +345,8 @@ void __page_pool_free(struct page_pool *pool)kfree (pool); } -EXPORT_SYMBOL (__page_pool_free);
I don't think this is correct according to RCU.
Let me reproduce the resulting version of page_pool_free():
static void page_pool_free(struct page_pool *pool)
{
if (pool->disconnect)
pool->disconnect(pool);
ptr_ring_cleanup(&pool->ring, NULL);
if (pool->p.flags & PP_FLAG_DMA_MAP)
put_device(pool->p.dev);
kfree(pool);}
The issue is that pool->disconnect() will call into mem_allocator_disconnect() -> mem_xa_remove(), and mem_xa_remove() does a RCU delayed free. And this function immediately does a kfree(pool).
I do know that we can ONLY reach this page_pool_free() function, when inflight == 0, but that can happen as soon as __page_pool_clean_page() does the decrement, and after this trace_page_pool_state_release() still have access the page_pool object (thus, hard to catch use-after-free).
Quoted 94 lines
net/core/page_pool.c +61
-7-/* Request to shutdown: release pages cached by page_pool, and check -* for in-flight pages -*/ -bool __page_pool_request_shutdown (struct page_pool *pool) +static void page_pool_scrub (struct page_pool *pool) {struct page *page;@@ -393,7 +363,64 @@ bool __page_pool_request_shutdown(struct page_pool *pool)* be in-flight. */ __page_pool_empty_ring (pool); - -return __page_pool_safe_to_destroy (pool); } -EXPORT_SYMBOL (__page_pool_request_shutdown); + +static int page_pool_release (struct page_pool *pool) +{ +int inflight; + +page_pool_scrub (pool); + inflight =page_pool_inflight (pool); +if (!inflight) +page_pool_free (pool); + +return inflight; +} + +static void page_pool_release_retry (struct work_struct *wq) +{ +struct delayed_work *dwq =to_delayed_work (wq); +struct page_pool *pool =container_of (dwq,typeof (*pool), release_dw); +int inflight; + + inflight =page_pool_release (pool); +if (!inflight) +return ; + +/* Periodic warning */ +if (time_after_eq (jiffies, pool->defer_warn)) { +int sec = (s32)((u32 )jiffies - (u32 )pool->defer_start) /HZ ; + +pr_warn ("%s() stalled pool shutdown %d inflight %d sec\n" , + __func__, inflight, sec); + pool->defer_warn = jiffies +DEFER_WARN_INTERVAL ; + } + +/* Still not ready to be disconnected, retry later */ +schedule_delayed_work (&pool->release_dw,DEFER_TIME ); +} + +void page_pool_use_xdp_mem (struct page_pool *pool,void (*disconnect)(void *)) +{ +refcount_inc (&pool->user_cnt); + pool->disconnect = disconnect; +} + +void page_pool_destroy (struct page_pool *pool) +{ +if (!pool) +return ; + +if (!page_pool_put (pool)) +return ; + +if (!page_pool_release (pool)) +return ; + + pool->defer_start = jiffies; + pool->defer_warn = jiffies +DEFER_WARN_INTERVAL ; + +INIT_DELAYED_WORK (&pool->release_dw, page_pool_release_retry); +schedule_delayed_work (&pool->release_dw,DEFER_TIME ); +} +EXPORT_SYMBOL (page_pool_destroy);net/core/xdp.c +0
-4@@ -70,10 +70,6 @@ static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu) xa = container_of(rcu, struct xdp_mem_allocator, rcu); -/* Allocator have indicated safe to remove before this is called */ -if (xa->mem.type ==MEM_TYPE_PAGE_POOL ) -page_pool_free (xa->page_pool); -/* Allow this ID to be reused */ ida_simple_remove (&mem_id_pool, xa->mem.id);@@ -85,10 +81,41 @@ static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)kfree (xa); }
net/core/xdp.c static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu) · line 91
Quoted 10 lines
net/core/xdp.c +8
-1-static bool __mem_id_disconnect (int id,bool force) +static void mem_xa_remove (struct xdp_mem_allocator *xa) +{ +trace_mem_disconnect (xa); + +mutex_lock (&mem_id_lock); + +if (!rhashtable_remove_fast (mem_id_ht, &xa->node, mem_id_rht_params)) +call_rcu (&xa->rcu, __xdp_mem_allocator_rcu_free);
RCU delayed free.
Quoted 55 lines
net/core/xdp.c +27
-12+ +mutex_unlock (&mem_id_lock); +} + +static void mem_allocator_disconnect (void *allocator) +{ +struct xdp_mem_allocator *xa; +struct rhashtable_iter iter; + +rhashtable_walk_enter (mem_id_ht, &iter); +do { +rhashtable_walk_start (&iter); + +while ((xa =rhashtable_walk_next (&iter)) && !IS_ERR (xa)) { +if (xa->allocator == allocator) +mem_xa_remove (xa); + } + +rhashtable_walk_stop (&iter); + + }while (xa ==ERR_PTR (-EAGAIN )); +rhashtable_walk_exit (&iter); +} +static void mem_id_disconnect (int id) {struct xdp_mem_allocator *xa; -bool safe_to_remove = true;mutex_lock (&mem_id_lock);@@ -96,51 +123,15 @@ static bool __mem_id_disconnect(int id, bool force)if (!xa) {mutex_unlock (&mem_id_lock);WARN (1 ,"Request remove non-existing id(%d), driver bug?" , id); -return true; +return ; } - xa->disconnect_cnt++; -/* Detects in-flight packet-pages for page_pool */ -if (xa->mem.type ==MEM_TYPE_PAGE_POOL ) - safe_to_remove =page_pool_request_shutdown (xa->page_pool); +trace_mem_disconnect (xa); -trace_mem_disconnect (xa, safe_to_remove, force); - -if ((safe_to_remove || force) && - !rhashtable_remove_fast (mem_id_ht, &xa->node, mem_id_rht_params)) +if (!rhashtable_remove_fast (mem_id_ht, &xa->node, mem_id_rht_params))call_rcu (&xa->rcu, __xdp_mem_allocator_rcu_free);mutex_unlock (&mem_id_lock); -return (safe_to_remove|force); -}
[...]
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Quoted 9 lines · Jesper Dangaard Brouer
Quoted 6 lines · On Mon, 11 Nov 2019 21:32:10 -0800 Jonathan Lemon <[email protected]>…
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Quoted 6 lines
Quoted 3 lines
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
Can you please change this to:
[... can be used without] xdp memory model.
Okay.
Quoted 76 lines
Quoted 8 lines
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple
xdp_rxq_info.Fixes d956a048cd3f ("xdp: force mem allocator removal and periodic warning")Signed-off-by Jonathan Lemon <[email protected]>---
[...]
net/core/page_pool.c
Quoted 4 lines
net/core/page_pool.c +0
-0[...]
Found an issue, see below.
Quoted 37 lines
net/core/page_pool.c +3
-24@@ -338,31 +333,10 @@ static void __page_pool_empty_ring(structpage_pool *pool) } } -static void __warn_in_flight (struct page_pool *pool) +static void page_pool_free(struct page_pool *pool) { -u32 release_cnt =atomic_read (&pool->pages_state_release_cnt); -u32 hold_cnt =READ_ONCE (pool->pages_state_hold_cnt); -s32 distance; - - distance =_distance (hold_cnt, release_cnt); - -/* Drivers should fix this, but only problematic when DMA is used */ -WARN (1 ,"Still in-flight pages:%d hold:%u released:%u" , - distance, hold_cnt, release_cnt); -} - -void __page_pool_free (struct page_pool *pool) -{ -/* Only last user actually free/release resources */ -if (!page_pool_put (pool)) -return ; - -WARN (pool->alloc.count,"API usage violation" ); -WARN (!ptr_ring_empty (&pool->ring),"ptr_ring is not empty" ); - -/* Can happen due to forced shutdown */ -if (!__page_pool_safe_to_destroy (pool)) -__warn_in_flight (pool); +if (pool->disconnect) + pool->disconnect (pool);ptr_ring_cleanup (&pool->ring,NULL );net/core/page_pool.c void __page_pool_free(struct page_pool *pool) · line 374
Quoted 4 lines
net/core/page_pool.c +0
-1@@ -371,12 +345,8 @@ void __page_pool_free(struct page_pool *pool)kfree (pool); } -EXPORT_SYMBOL (__page_pool_free);I don't think this is correct according to RCU.
Let me reproduce the resulting version of
page_pool_free():static void
page_pool_free(struct page_pool *pool){ if (pool->disconnect) pool->disconnect(pool); ptr_ring_cleanup(&pool->ring, NULL); if (pool->p.flags & PP_FLAG_DMA_MAP) put_device(pool->p.dev); kfree(pool);}
Quoted 8 lines
The issue is that
pool->disconnect()will call intomem_allocator_disconnect()->mem_xa_remove(), andmem_xa_remove()does a RCU delayed free. And this function immediately does a kfree(pool).I do know that we can ONLY reach this
page_pool_free()function, when inflight == 0, but that can happen as soon as__page_pool_clean_page()does the decrement, and after thistrace_page_pool_state_release()still have access thepage_poolobject (thus, hard to catch use-after-free).
Is this an issue? The RCU delayed free is for the xa object, it is held in an RCU-protected mem_id_ht, so it can't be freed until all the readers are complete.
The change of &pool->pages_state_release_cnt can decrement the inflight pages to 0, and another thread could see inflight == 0 and immediately the remove the pool. The atomic manipulation should be the last use of the pool - this should be documented, I'll add that as well:
skip_dma_unmap:
/* This may be the last page returned, releasing the pool, so
* it is not safe to reference pool afterwards.
*/
count = atomic_inc_return(&pool->pages_state_release_cnt);
trace_page_pool_state_release(pool, page, count);The trace_page_pool_state_release() does not dereference pool, it just reports the pointer value, so there shouldn't be any use-after-free.
Signature · 1 line
Jonathan
Quoted 120 lines · On Tue, 12 Nov 2019 08:33:58 -0800 "Jonathan Lemon" <[email protected]>…
Quoted 9 lines · Jesper Dangaard Brouer
Quoted 6 lines · On Mon, 11 Nov 2019 21:32:10 -0800 Jonathan Lemon <[email protected]>…
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Quoted 6 lines
Quoted 3 lines
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
Can you please change this to:
[... can be used without] xdp memory model.Okay.
Quoted 82 lines
Quoted 9 lines
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple
xdp_rxq_info.Fixes d956a048cd3f ("xdp: force mem allocator removal and periodicwarning")
Signed-off-by Jonathan Lemon <[email protected]>---
[...]
net/core/page_pool.c
Quoted 4 lines
net/core/page_pool.c +0
-0[...]
Found an issue, see below.
Quoted 37 lines
net/core/page_pool.c +3
-24@@ -338,31 +333,10 @@ static void __page_pool_empty_ring(structpage_pool *pool) } } -static void __warn_in_flight (struct page_pool *pool) +static void page_pool_free(struct page_pool *pool) { -u32 release_cnt =atomic_read (&pool->pages_state_release_cnt); -u32 hold_cnt =READ_ONCE (pool->pages_state_hold_cnt); -s32 distance; - - distance =_distance (hold_cnt, release_cnt); - -/* Drivers should fix this, but only problematic when DMA is used */ -WARN (1 ,"Still in-flight pages:%d hold:%u released:%u" , - distance, hold_cnt, release_cnt); -} - -void __page_pool_free (struct page_pool *pool) -{ -/* Only last user actually free/release resources */ -if (!page_pool_put (pool)) -return ; - -WARN (pool->alloc.count,"API usage violation" ); -WARN (!ptr_ring_empty (&pool->ring),"ptr_ring is not empty" ); - -/* Can happen due to forced shutdown */ -if (!__page_pool_safe_to_destroy (pool)) -__warn_in_flight (pool); +if (pool->disconnect) + pool->disconnect (pool);ptr_ring_cleanup (&pool->ring,NULL );net/core/page_pool.c void __page_pool_free(struct page_pool *pool) · line 374
Quoted 4 lines
net/core/page_pool.c +0
-1@@ -371,12 +345,8 @@ void __page_pool_free(struct page_pool *pool)kfree (pool); } -EXPORT_SYMBOL (__page_pool_free);I don't think this is correct according to RCU.
Let me reproduce the resulting version of
page_pool_free():static void
page_pool_free(struct page_pool *pool){ if (pool->disconnect) pool->disconnect(pool); ptr_ring_cleanup(&pool->ring, NULL); if (pool->p.flags & PP_FLAG_DMA_MAP) put_device(pool->p.dev); kfree(pool);}
The issue is that
pool->disconnect()will call intomem_allocator_disconnect()->mem_xa_remove(), andmem_xa_remove()does
a RCU delayed free. And this function immediately does a kfree(pool).Quoted 5 lines
I do know that we can ONLY reach this
page_pool_free()function, when inflight == 0, but that can happen as soon as__page_pool_clean_page()does the decrement, and after thistrace_page_pool_state_release()still have access thepage_poolobject (thus, hard to catch use-after-free).Is this an issue? The RCU delayed free is for the xa object, it is held in an RCU-protected
mem_id_ht, so it can't be freed until all the readers
are complete.The change of
&pool->pages_state_release_cntcan decrement the inflight pages to 0, and another thread could see inflight == 0 and immediately the remove the pool. The atomic manipulation should be the last use of the pool - this should be documented, I'll add that as well:
skip_dma_unmap:
Quoted 8 lines
/* This may be the last page returned, releasing the pool, so * it is not safe to reference pool afterwards. */ count = atomic_inc_return(&pool->pages_state_release_cnt); trace_page_pool_state_release(pool, page, count);The
trace_page_pool_state_release()does not dereference pool, it just reports the pointer value, so there shouldn't be any use-after-free.
In the tracepoint we can still dereference the pool object pointer. This is made easier via using bpftrace for example see[1] (and with BTF this will become more common to do so).
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Quoted 129 lines · Jesper Dangaard Brouer
Quoted 126 lines · On Tue, 12 Nov 2019 08:33:58 -0800 "Jonathan Lemon" <[email protected]>…
Quoted 9 lines · Jesper Dangaard Brouer
Quoted 6 lines · On Mon, 11 Nov 2019 21:32:10 -0800 Jonathan Lemon <[email protected]>…
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Quoted 6 lines
Quoted 3 lines
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
Can you please change this to:
[... can be used without] xdp memory model.Okay.
Quoted 82 lines
Quoted 9 lines
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple
xdp_rxq_info.Fixes d956a048cd3f ("xdp: force mem allocator removal and periodicwarning")
Signed-off-by Jonathan Lemon <[email protected]>---
[...]
net/core/page_pool.c
Quoted 4 lines
net/core/page_pool.c +0
-0[...]
Found an issue, see below.
Quoted 37 lines
net/core/page_pool.c +3
-24@@ -338,31 +333,10 @@ static void __page_pool_empty_ring(structpage_pool *pool) } } -static void __warn_in_flight (struct page_pool *pool) +static void page_pool_free(struct page_pool *pool) { -u32 release_cnt =atomic_read (&pool->pages_state_release_cnt); -u32 hold_cnt =READ_ONCE (pool->pages_state_hold_cnt); -s32 distance; - - distance =_distance (hold_cnt, release_cnt); - -/* Drivers should fix this, but only problematic when DMA is used */ -WARN (1 ,"Still in-flight pages:%d hold:%u released:%u" , - distance, hold_cnt, release_cnt); -} - -void __page_pool_free (struct page_pool *pool) -{ -/* Only last user actually free/release resources */ -if (!page_pool_put (pool)) -return ; - -WARN (pool->alloc.count,"API usage violation" ); -WARN (!ptr_ring_empty (&pool->ring),"ptr_ring is not empty" ); - -/* Can happen due to forced shutdown */ -if (!__page_pool_safe_to_destroy (pool)) -__warn_in_flight (pool); +if (pool->disconnect) + pool->disconnect (pool);ptr_ring_cleanup (&pool->ring,NULL );net/core/page_pool.c void __page_pool_free(struct page_pool *pool) · line 374
Quoted 4 lines
net/core/page_pool.c +0
-1@@ -371,12 +345,8 @@ void __page_pool_free(struct page_pool *pool)kfree (pool); } -EXPORT_SYMBOL (__page_pool_free);I don't think this is correct according to RCU.
Let me reproduce the resulting version of
page_pool_free():static void
page_pool_free(struct page_pool *pool){ if (pool->disconnect) pool->disconnect(pool); ptr_ring_cleanup(&pool->ring, NULL); if (pool->p.flags & PP_FLAG_DMA_MAP) put_device(pool->p.dev); kfree(pool);}
The issue is that
pool->disconnect()will call intomem_allocator_disconnect()->mem_xa_remove(), andmem_xa_remove()does
a RCU delayed free. And this function immediately does a kfree(pool).Quoted 5 lines
I do know that we can ONLY reach this
page_pool_free()function, when inflight == 0, but that can happen as soon as__page_pool_clean_page()does the decrement, and after thistrace_page_pool_state_release()still have access thepage_poolobject (thus, hard to catch use-after-free).Is this an issue? The RCU delayed free is for the xa object, it is held in an RCU-protected
mem_id_ht, so it can't be freed until all the readers
are complete.The change of
&pool->pages_state_release_cntcan decrement the inflight pages to 0, and another thread could see inflight == 0 and immediately the remove the pool. The atomic manipulation should be the last use of the pool - this should be documented, I'll add that as well:
skip_dma_unmap:/* This may be the last page returned, releasing the pool, so * it is not safe to reference pool afterwards. */ count = atomic_inc_return(&pool->pages_state_release_cnt); trace_page_pool_state_release(pool, page, count);
Quoted 6 lines
Quoted 2 lines
The
trace_page_pool_state_release()does not dereference pool, it just reports the pointer value, so there shouldn't be any use-after-free.In the tracepoint we can still dereference the pool object pointer. This is made easier via using bpftrace for example see[1] (and with BTF this will become more common to do so).
This seems problematic today - essentially the current code is only safe because it's borrowing the RCU protection from the xdp_mem info, right?
Signature · 1 line
Jonathan
Quoted 5 lines · Jesper Dangaard Brouer
Quoted 2 lines
The
trace_page_pool_state_release()does not dereference pool, it just reports the pointer value, so there shouldn't be any use-after-free.In the tracepoint we can still dereference the pool object pointer. This is made easier via using bpftrace for example see[1] (and with BTF this will become more common to do so).
bpf tracing progs cannot assume that the pointer is valid. The program can remember a kernel pointer in a map and then access it days later.
Like kretprobe on kfree_skb(). The skb is freed. 100% use-after-free. Such bpf program is broken and won't be reading meaningful values, but it won't crash the kernel.
On the other side we should not be passing pointers to freed objects into tracepoints. That just wrong.
May be simply move that questionable tracepoint?
Quoted 14 lines · Alexei Starovoitov
Quoted 5 lines · Jesper Dangaard Brouer
Quoted 2 lines
The
trace_page_pool_state_release()does not dereference pool, it just reports the pointer value, so there shouldn't be any use-after-free.In the tracepoint we can still dereference the pool object pointer. This is made easier via using bpftrace for example see[1] (and with BTF this will become more common to do so).
bpf tracing progs cannot assume that the pointer is valid. The program can remember a kernel pointer in a map and then access it days later.
Like kretprobe onkfree_skb(). The skb is freed. 100% use-after-free. Such bpf program is broken and won't be reading meaningful values, but it won't crash the kernel.
Quoted 3 lines
On the other side we should not be passing pointers to freed objects into tracepoints. That just wrong.
May be simply move that questionable tracepoint?
Yes, move and simplify it. I believe this patch should resolve the issue, it just reports pages entering/exiting the pool, without trying to access the counters - the counters are reported through the inflight tracepoint.
Signature · 1 line
Jonathan
include/trace/events/page_pool.h +10 -14
@@ -35,50 +35,46 @@ TRACE_EVENT(page_pool_inflight, __entry->pool, __entry->inflight, __entry->hold, __entry->release) ); -TRACE_EVENT (page_pool_state_release, +TRACE_EVENT (page_pool_page_release, TP_PROTO(const struct page_pool *pool, - const struct page *page, u32 release), + const struct page *page) -TP_ARGS (pool, page, release), +TP_ARGS (pool, page),TP_STRUCT__entry ( __field(const struct page_pool *, pool) __field(const struct page *, page) -__field (u32, release) ),TP_fast_assign ( __entry->pool = pool; __entry->page = page; - __entry->release = release; ), -TP_printk ("page_pool=%p page=%p release=%u" , - __entry->pool, __entry->page, __entry->release) +TP_printk ("page_pool=%p page=%p" , + __entry->pool, __entry->page) ); -TRACE_EVENT (page_pool_state_hold, +TRACE_EVENT (page_pool_page_hold, TP_PROTO(const struct page_pool *pool, - const struct page *page, u32 hold), + const struct page *page), -TP_ARGS (pool, page, hold), +TP_ARGS (pool, page),TP_STRUCT__entry ( __field(const struct page_pool *, pool) __field(const struct page *, page) -__field (u32, hold) ),TP_fast_assign ( __entry->pool = pool; __entry->page = page; - __entry->hold = hold; ), -TP_printk ("page_pool=%p page=%p hold=%u" , - __entry->pool, __entry->page, __entry->hold) +TP_printk ("page_pool=%p page=%p" , + __entry->pool, __entry->page) );#endif /* _TRACE_PAGE_POOL_H */
net/core/page_pool.c +5 -3
@@ -163,7 +163,7 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; -trace_page_pool_state_hold (pool, page, pool->pages_state_hold_cnt); +trace_page_pool_page_hold (pool, page);/* When page just alloc'ed is should/must have refcnt 1. */ return page;
@@ -222,9 +223,11 @@ static void __page_pool_clean_page(struct page_pool *pool, DMA_ATTR_SKIP_CPU_SYNC); page->dma_addr =0 ; skip_dma_unmap: +trace_page_pool_page_release (pool, page); +/* This may be the last page returned, releasing the pool, so +* it is not safe to reference pool afterwards. +*/ atomic_inc (&pool->pages_state_release_cnt); -trace_page_pool_state_release (pool, page, -atomic_read (&pool->pages_state_release_cnt)); }/* unmap the page and clean our state */
Quoted 18 lines · On Tue, 12 Nov 2019 09:32:10 -0800 "Jonathan Lemon" <[email protected]>…
Quoted 16 lines · Alexei Starovoitov
Quoted 7 lines · Jesper Dangaard Brouer
Quoted 3 lines
The
trace_page_pool_state_release()does not dereference pool, it just
reports the pointer value, so there shouldn't be any use-after-free.In the tracepoint we can still dereference the pool object pointer. This is made easier via using bpftrace for example see[1] (and with BTF
this will become more common to do so).bpf tracing progs cannot assume that the pointer is valid. The program can remember a kernel pointer in a map and then access it days later.
Like kretprobe onkfree_skb(). The skb is freed. 100% use-after-free. Such bpf program is broken and won't be reading meaningful values, but it won't crash the kernel.
Quoted 8 lines
Quoted 3 lines
On the other side we should not be passing pointers to freed objects into tracepoints. That just wrong.
May be simply move that questionable tracepoint?Yes, move and simplify it. I believe this patch should resolve the issue, it just reports pages entering/exiting the pool, without trying to access the counters - the counters are reported through the inflight tracepoint.
Sorry, I don't like loosing the counter. I have a plan for using these counters in a bpftrace script. (Worst case I might be able to live without the counters).
The basic idea is to use these tracepoints to detect if we leak DMA-mappings. I'll try write the bpftrace script today, and see it I can live without the counter.
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Quoted 3 lines · On Wed, 13 Nov 2019 11:08:23 +0100 Jesper Dangaard Brouer <[email protected]>…
The basic idea is to use these tracepoints to detect if we leak DMA-mappings. I'll try write the bpftrace script today, and see it I can live without the counter.
Didn't finish, here is how far I got:
https://github.com/xdp-project/xdp-project/blob/master/areas/mem/bpftrace/page_pool_track_leaks01.bt
In the end of the bpftrace script, I needed to iterate over a map, which I don't think bpftrace supports (Brendan) ?
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Quoted 10 lines · On Mon, 11 Nov 2019 21:32:10 -0800 Jonathan Lemon <[email protected]>…
The page pool keeps track of the number of pages in flight, and it isn't safe to remove the pool until all pages are returned.
Disallow removing the pool until all pages are back, so the pool is always available for page producers.
Make the page pool responsible for its own delayed destruction instead of relying on XDP, so the page pool can be used without xdp.
Quoted 7 lines
When all pages are returned, free the pool and notify xdp if the pool is registered with the xdp memory system. Have the callback perform a table walk since some drivers (cpsw) may share the pool among multiple
xdp_rxq_info.Fixes d956a048cd3f ("xdp: force mem allocator removal and periodic warning")Signed-off-by Jonathan Lemon <[email protected]>
FYI: I'm using this work-in-progress bpftrace script to test your patch:
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer