Skip to content

Commit 4bcfdd6

Browse files
tohojosmb49
authored andcommitted
page_pool: Track DMA-mapped pages and unmap them when destroying the pool
BugLink: https://bugs.launchpad.net/bugs/2119603 [ Upstream commit ee62ce7a1d909ccba0399680a03c2dee83bcae95 ] When enabling DMA mapping in page_pool, pages are kept DMA mapped until they are released from the pool, to avoid the overhead of re-mapping the pages every time they are used. This causes resource leaks and/or crashes when there are pages still outstanding while the device is torn down, because page_pool will attempt an unmap through a non-existent DMA device on the subsequent page return. To fix this, implement a simple tracking of outstanding DMA-mapped pages in page pool using an xarray. This was first suggested by Mina[0], and turns out to be fairly straight forward: We simply store pointers to pages directly in the xarray with xa_alloc() when they are first DMA mapped, and remove them from the array on unmap. Then, when a page pool is torn down, it can simply walk the xarray and unmap all pages still present there before returning, which also allows us to get rid of the get/put_device() calls in page_pool. Using xa_cmpxchg(), no additional synchronisation is needed, as a page will only ever be unmapped once. To avoid having to walk the entire xarray on unmap to find the page reference, we stash the ID assigned by xa_alloc() into the page structure itself, using the upper bits of the pp_magic field. This requires a couple of defines to avoid conflicting with the POINTER_POISON_DELTA define, but this is all evaluated at compile-time, so does not affect run-time performance. The bitmap calculations in this patch gives the following number of bits for different architectures: - 23 bits on 32-bit architectures - 21 bits on PPC64 (because of the definition of ILLEGAL_POINTER_VALUE) - 32 bits on other 64-bit architectures Stashing a value into the unused bits of pp_magic does have the effect that it can make the value stored there lie outside the unmappable range (as governed by the mmap_min_addr sysctl), for architectures that don't define ILLEGAL_POINTER_VALUE. This means that if one of the pointers that is aliased to the pp_magic field (such as page->lru.next) is dereferenced while the page is owned by page_pool, that could lead to a dereference into userspace, which is a security concern. The risk of this is mitigated by the fact that (a) we always clear pp_magic before releasing a page from page_pool, and (b) this would need a use-after-free bug for struct page, which can have many other risks since page->lru.next is used as a generic list pointer in multiple places in the kernel. As such, with this patch we take the position that this risk is negligible in practice. For more discussion, see[1]. Since all the tracking added in this patch is performed on DMA map/unmap, no additional code is needed in the fast path, meaning the performance overhead of this tracking is negligible there. A micro-benchmark shows that the total overhead of the tracking itself is about 400 ns (39 cycles(tsc) 395.218 ns; sum for both map and unmap[2]). Since this cost is only paid on DMA map and unmap, it seems like an acceptable cost to fix the late unmap issue. Further optimisation can narrow the cases where this cost is paid (for instance by eliding the tracking when DMA map/unmap is a no-op). The extra memory needed to track the pages is neatly encapsulated inside xarray, which uses the 'struct xa_node' structure to track items. This structure is 576 bytes long, with slots for 64 items, meaning that a full node occurs only 9 bytes of overhead per slot it tracks (in practice, it probably won't be this efficient, but in any case it should be an acceptable overhead). [0] https://lore.kernel.org/all/CAHS8izPg7B5DwKfSuzz-iOop_YRbk3Sd6Y4rX7KBG9DcVJcyWg@mail.gmail.com/ [1] https://lore.kernel.org/r/20250320023202.GA25514@openwall.com [2] https://lore.kernel.org/r/ae07144c-9295-4c9d-a400-153bb689fe9e@huawei.com Reported-by: Yonglong Liu <liuyonglong@huawei.com> Closes: https://lore.kernel.org/r/8743264a-9700-4227-a556-5f931c720211@huawei.com Fixes: ff7d6b2 ("page_pool: refurbish version of page_pool code") Suggested-by: Mina Almasry <almasrymina@google.com> Reviewed-by: Mina Almasry <almasrymina@google.com> Reviewed-by: Jesper Dangaard Brouer <hawk@kernel.org> Tested-by: Jesper Dangaard Brouer <hawk@kernel.org> Tested-by: Qiuling Ren <qren@redhat.com> Tested-by: Yuying Ma <yuma@redhat.com> Tested-by: Yonglong Liu <liuyonglong@huawei.com> Acked-by: Jesper Dangaard Brouer <hawk@kernel.org> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Link: https://patch.msgid.link/20250409-page-pool-track-dma-v9-2-6a9ef2e0cba8@redhat.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent 2f75943 commit 4bcfdd6

File tree

5 files changed

+147
-18
lines changed

5 files changed

+147
-18
lines changed

include/linux/mm.h

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,13 +4162,51 @@ int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *st
41624162
int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status);
41634163
int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);
41644164

4165+
/*
4166+
* DMA mapping IDs for page_pool
4167+
*
4168+
* When DMA-mapping a page, page_pool allocates an ID (from an xarray) and
4169+
* stashes it in the upper bits of page->pp_magic. We always want to be able to
4170+
* unambiguously identify page pool pages (using page_pool_page_is_pp()). Non-PP
4171+
* pages can have arbitrary kernel pointers stored in the same field as pp_magic
4172+
* (since it overlaps with page->lru.next), so we must ensure that we cannot
4173+
* mistake a valid kernel pointer with any of the values we write into this
4174+
* field.
4175+
*
4176+
* On architectures that set POISON_POINTER_DELTA, this is already ensured,
4177+
* since this value becomes part of PP_SIGNATURE; meaning we can just use the
4178+
* space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the
4179+
* lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is
4180+
* 0, we make sure that we leave the two topmost bits empty, as that guarantees
4181+
* we won't mistake a valid kernel pointer for a value we set, regardless of the
4182+
* VMSPLIT setting.
4183+
*
4184+
* Altogether, this means that the number of bits available is constrained by
4185+
* the size of an unsigned long (at the upper end, subtracting two bits per the
4186+
* above), and the definition of PP_SIGNATURE (with or without
4187+
* POISON_POINTER_DELTA).
4188+
*/
4189+
#define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA))
4190+
#if POISON_POINTER_DELTA > 0
4191+
/* PP_SIGNATURE includes POISON_POINTER_DELTA, so limit the size of the DMA
4192+
* index to not overlap with that if set
4193+
*/
4194+
#define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT)
4195+
#else
4196+
/* Always leave out the topmost two; see above. */
4197+
#define PP_DMA_INDEX_BITS MIN(32, BITS_PER_LONG - PP_DMA_INDEX_SHIFT - 2)
4198+
#endif
4199+
4200+
#define PP_DMA_INDEX_MASK GENMASK(PP_DMA_INDEX_BITS + PP_DMA_INDEX_SHIFT - 1, \
4201+
PP_DMA_INDEX_SHIFT)
4202+
41654203
/* Mask used for checking in page_pool_page_is_pp() below. page->pp_magic is
41664204
* OR'ed with PP_SIGNATURE after the allocation in order to preserve bit 0 for
4167-
* the head page of compound page and bit 1 for pfmemalloc page.
4168-
* page_is_pfmemalloc() is checked in __page_pool_put_page() to avoid recycling
4169-
* the pfmemalloc page.
4205+
* the head page of compound page and bit 1 for pfmemalloc page, as well as the
4206+
* bits used for the DMA index. page_is_pfmemalloc() is checked in
4207+
* __page_pool_put_page() to avoid recycling the pfmemalloc page.
41704208
*/
4171-
#define PP_MAGIC_MASK ~0x3UL
4209+
#define PP_MAGIC_MASK ~(PP_DMA_INDEX_MASK | 0x3UL)
41724210

41734211
#ifdef CONFIG_PAGE_POOL
41744212
static inline bool page_pool_page_is_pp(struct page *page)

include/linux/poison.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
#define KEY_DESTROY 0xbd
7171

7272
/********** net/core/page_pool.c **********/
73+
/*
74+
* page_pool uses additional free bits within this value to store data, see the
75+
* definition of PP_DMA_INDEX_MASK in mm.h
76+
*/
7377
#define PP_SIGNATURE (0x40 + POISON_POINTER_DELTA)
7478

7579
/********** net/core/skbuff.c **********/

include/net/page_pool/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/dma-direction.h>
77
#include <linux/ptr_ring.h>
88
#include <linux/types.h>
9+
#include <linux/xarray.h>
910
#include <net/netmem.h>
1011

1112
#define PP_FLAG_DMA_MAP BIT(0) /* Should page_pool do the DMA
@@ -33,6 +34,9 @@
3334
#define PP_FLAG_ALL (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | \
3435
PP_FLAG_SYSTEM_POOL | PP_FLAG_ALLOW_UNREADABLE_NETMEM)
3536

37+
/* Index limit to stay within PP_DMA_INDEX_BITS for DMA indices */
38+
#define PP_DMA_INDEX_LIMIT XA_LIMIT(1, BIT(PP_DMA_INDEX_BITS) - 1)
39+
3640
/*
3741
* Fast allocation side cache array/stack
3842
*
@@ -217,6 +221,8 @@ struct page_pool {
217221

218222
void *mp_priv;
219223

224+
struct xarray dma_mapped;
225+
220226
#ifdef CONFIG_PAGE_POOL_STATS
221227
/* recycle stats are per-cpu to avoid locking */
222228
struct page_pool_recycle_stats __percpu *recycle_stats;

net/core/netmem_priv.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
static inline unsigned long netmem_get_pp_magic(netmem_ref netmem)
77
{
8-
return __netmem_clear_lsb(netmem)->pp_magic;
8+
return __netmem_clear_lsb(netmem)->pp_magic & ~PP_DMA_INDEX_MASK;
99
}
1010

1111
static inline void netmem_or_pp_magic(netmem_ref netmem, unsigned long pp_magic)
@@ -15,6 +15,8 @@ static inline void netmem_or_pp_magic(netmem_ref netmem, unsigned long pp_magic)
1515

1616
static inline void netmem_clear_pp_magic(netmem_ref netmem)
1717
{
18+
WARN_ON_ONCE(__netmem_clear_lsb(netmem)->pp_magic & PP_DMA_INDEX_MASK);
19+
1820
__netmem_clear_lsb(netmem)->pp_magic = 0;
1921
}
2022

@@ -33,4 +35,28 @@ static inline void netmem_set_dma_addr(netmem_ref netmem,
3335
{
3436
__netmem_clear_lsb(netmem)->dma_addr = dma_addr;
3537
}
38+
39+
static inline unsigned long netmem_get_dma_index(netmem_ref netmem)
40+
{
41+
unsigned long magic;
42+
43+
if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
44+
return 0;
45+
46+
magic = __netmem_clear_lsb(netmem)->pp_magic;
47+
48+
return (magic & PP_DMA_INDEX_MASK) >> PP_DMA_INDEX_SHIFT;
49+
}
50+
51+
static inline void netmem_set_dma_index(netmem_ref netmem,
52+
unsigned long id)
53+
{
54+
unsigned long magic;
55+
56+
if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
57+
return;
58+
59+
magic = netmem_get_pp_magic(netmem) | (id << PP_DMA_INDEX_SHIFT);
60+
__netmem_clear_lsb(netmem)->pp_magic = magic;
61+
}
3662
#endif

net/core/page_pool.c

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ static int page_pool_init(struct page_pool *pool,
274274
/* Driver calling page_pool_create() also call page_pool_destroy() */
275275
refcount_set(&pool->user_cnt, 1);
276276

277-
if (pool->dma_map)
278-
get_device(pool->p.dev);
277+
xa_init_flags(&pool->dma_mapped, XA_FLAGS_ALLOC1);
279278

280279
if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) {
281280
/* We rely on rtnl_lock()ing to make sure netdev_rx_queue
@@ -316,9 +315,7 @@ static int page_pool_init(struct page_pool *pool,
316315
static void page_pool_uninit(struct page_pool *pool)
317316
{
318317
ptr_ring_cleanup(&pool->ring, NULL);
319-
320-
if (pool->dma_map)
321-
put_device(pool->p.dev);
318+
xa_destroy(&pool->dma_mapped);
322319

323320
#ifdef CONFIG_PAGE_POOL_STATS
324321
if (!pool->system)
@@ -459,13 +456,21 @@ page_pool_dma_sync_for_device(const struct page_pool *pool,
459456
netmem_ref netmem,
460457
u32 dma_sync_size)
461458
{
462-
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev))
463-
__page_pool_dma_sync_for_device(pool, netmem, dma_sync_size);
459+
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) {
460+
rcu_read_lock();
461+
/* re-check under rcu_read_lock() to sync with page_pool_scrub() */
462+
if (pool->dma_sync)
463+
__page_pool_dma_sync_for_device(pool, netmem,
464+
dma_sync_size);
465+
rcu_read_unlock();
466+
}
464467
}
465468

466-
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)
469+
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t gfp)
467470
{
468471
dma_addr_t dma;
472+
int err;
473+
u32 id;
469474

470475
/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
471476
* since dma_addr_t can be either 32 or 64 bits and does not always fit
@@ -479,15 +484,30 @@ static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)
479484
if (dma_mapping_error(pool->p.dev, dma))
480485
return false;
481486

482-
if (page_pool_set_dma_addr_netmem(netmem, dma))
487+
if (page_pool_set_dma_addr_netmem(netmem, dma)) {
488+
WARN_ONCE(1, "unexpected DMA address, please report to netdev@");
483489
goto unmap_failed;
490+
}
491+
492+
if (in_softirq())
493+
err = xa_alloc(&pool->dma_mapped, &id, netmem_to_page(netmem),
494+
PP_DMA_INDEX_LIMIT, gfp);
495+
else
496+
err = xa_alloc_bh(&pool->dma_mapped, &id, netmem_to_page(netmem),
497+
PP_DMA_INDEX_LIMIT, gfp);
498+
if (err) {
499+
WARN_ONCE(err != -ENOMEM, "couldn't track DMA mapping, please report to netdev@");
500+
goto unset_failed;
501+
}
484502

503+
netmem_set_dma_index(netmem, id);
485504
page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len);
486505

487506
return true;
488507

508+
unset_failed:
509+
page_pool_set_dma_addr_netmem(netmem, 0);
489510
unmap_failed:
490-
WARN_ONCE(1, "unexpected DMA address, please report to netdev@");
491511
dma_unmap_page_attrs(pool->p.dev, dma,
492512
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
493513
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
@@ -504,7 +524,7 @@ static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
504524
if (unlikely(!page))
505525
return NULL;
506526

507-
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page)))) {
527+
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page), gfp))) {
508528
put_page(page);
509529
return NULL;
510530
}
@@ -550,7 +570,7 @@ static noinline netmem_ref __page_pool_alloc_pages_slow(struct page_pool *pool,
550570
*/
551571
for (i = 0; i < nr_pages; i++) {
552572
netmem = pool->alloc.cache[i];
553-
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem))) {
573+
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem, gfp))) {
554574
put_page(netmem_to_page(netmem));
555575
continue;
556576
}
@@ -652,6 +672,8 @@ void page_pool_clear_pp_info(netmem_ref netmem)
652672
static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
653673
netmem_ref netmem)
654674
{
675+
struct page *old, *page = netmem_to_page(netmem);
676+
unsigned long id;
655677
dma_addr_t dma;
656678

657679
if (!pool->dma_map)
@@ -660,13 +682,25 @@ static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
660682
*/
661683
return;
662684

685+
id = netmem_get_dma_index(netmem);
686+
if (!id)
687+
return;
688+
689+
if (in_softirq())
690+
old = xa_cmpxchg(&pool->dma_mapped, id, page, NULL, 0);
691+
else
692+
old = xa_cmpxchg_bh(&pool->dma_mapped, id, page, NULL, 0);
693+
if (old != page)
694+
return;
695+
663696
dma = page_pool_get_dma_addr_netmem(netmem);
664697

665698
/* When page is unmapped, it cannot be returned to our pool */
666699
dma_unmap_page_attrs(pool->p.dev, dma,
667700
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
668701
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
669702
page_pool_set_dma_addr_netmem(netmem, 0);
703+
netmem_set_dma_index(netmem, 0);
670704
}
671705

672706
/* Disconnects a page (from a page_pool). API users can have a need
@@ -1076,8 +1110,29 @@ static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
10761110

10771111
static void page_pool_scrub(struct page_pool *pool)
10781112
{
1113+
unsigned long id;
1114+
void *ptr;
1115+
10791116
page_pool_empty_alloc_cache_once(pool);
1080-
pool->destroy_cnt++;
1117+
if (!pool->destroy_cnt++ && pool->dma_map) {
1118+
if (pool->dma_sync) {
1119+
/* Disable page_pool_dma_sync_for_device() */
1120+
pool->dma_sync = false;
1121+
1122+
/* Make sure all concurrent returns that may see the old
1123+
* value of dma_sync (and thus perform a sync) have
1124+
* finished before doing the unmapping below. Skip the
1125+
* wait if the device doesn't actually need syncing, or
1126+
* if there are no outstanding mapped pages.
1127+
*/
1128+
if (dma_dev_need_sync(pool->p.dev) &&
1129+
!xa_empty(&pool->dma_mapped))
1130+
synchronize_net();
1131+
}
1132+
1133+
xa_for_each(&pool->dma_mapped, id, ptr)
1134+
__page_pool_release_page_dma(pool, page_to_netmem(ptr));
1135+
}
10811136

10821137
/* No more consumers should exist, but producers could still
10831138
* be in-flight.

0 commit comments

Comments
 (0)