Skip to content

Commit 4f51fb0

Browse files
tohojogregkh
authored andcommitted
page_pool: Track DMA-mapped pages and unmap them when destroying the pool
[ Upstream commit ee62ce7 ] 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/[email protected] [2] https://lore.kernel.org/r/[email protected] Reported-by: Yonglong Liu <[email protected]> Closes: https://lore.kernel.org/r/[email protected] Fixes: ff7d6b2 ("page_pool: refurbish version of page_pool code") Suggested-by: Mina Almasry <[email protected]> Reviewed-by: Mina Almasry <[email protected]> Reviewed-by: Jesper Dangaard Brouer <[email protected]> Tested-by: Jesper Dangaard Brouer <[email protected]> Tested-by: Qiuling Ren <[email protected]> Tested-by: Yuying Ma <[email protected]> Tested-by: Yonglong Liu <[email protected]> Acked-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 25a912b commit 4f51fb0

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
@@ -4243,13 +4243,51 @@ static inline void pgalloc_tag_copy(struct folio *new, struct folio *old)
42434243
}
42444244
#endif /* CONFIG_MEM_ALLOC_PROFILING */
42454245

4246+
/*
4247+
* DMA mapping IDs for page_pool
4248+
*
4249+
* When DMA-mapping a page, page_pool allocates an ID (from an xarray) and
4250+
* stashes it in the upper bits of page->pp_magic. We always want to be able to
4251+
* unambiguously identify page pool pages (using page_pool_page_is_pp()). Non-PP
4252+
* pages can have arbitrary kernel pointers stored in the same field as pp_magic
4253+
* (since it overlaps with page->lru.next), so we must ensure that we cannot
4254+
* mistake a valid kernel pointer with any of the values we write into this
4255+
* field.
4256+
*
4257+
* On architectures that set POISON_POINTER_DELTA, this is already ensured,
4258+
* since this value becomes part of PP_SIGNATURE; meaning we can just use the
4259+
* space between the PP_SIGNATURE value (without POISON_POINTER_DELTA), and the
4260+
* lowest bits of POISON_POINTER_DELTA. On arches where POISON_POINTER_DELTA is
4261+
* 0, we make sure that we leave the two topmost bits empty, as that guarantees
4262+
* we won't mistake a valid kernel pointer for a value we set, regardless of the
4263+
* VMSPLIT setting.
4264+
*
4265+
* Altogether, this means that the number of bits available is constrained by
4266+
* the size of an unsigned long (at the upper end, subtracting two bits per the
4267+
* above), and the definition of PP_SIGNATURE (with or without
4268+
* POISON_POINTER_DELTA).
4269+
*/
4270+
#define PP_DMA_INDEX_SHIFT (1 + __fls(PP_SIGNATURE - POISON_POINTER_DELTA))
4271+
#if POISON_POINTER_DELTA > 0
4272+
/* PP_SIGNATURE includes POISON_POINTER_DELTA, so limit the size of the DMA
4273+
* index to not overlap with that if set
4274+
*/
4275+
#define PP_DMA_INDEX_BITS MIN(32, __ffs(POISON_POINTER_DELTA) - PP_DMA_INDEX_SHIFT)
4276+
#else
4277+
/* Always leave out the topmost two; see above. */
4278+
#define PP_DMA_INDEX_BITS MIN(32, BITS_PER_LONG - PP_DMA_INDEX_SHIFT - 2)
4279+
#endif
4280+
4281+
#define PP_DMA_INDEX_MASK GENMASK(PP_DMA_INDEX_BITS + PP_DMA_INDEX_SHIFT - 1, \
4282+
PP_DMA_INDEX_SHIFT)
4283+
42464284
/* Mask used for checking in page_pool_page_is_pp() below. page->pp_magic is
42474285
* OR'ed with PP_SIGNATURE after the allocation in order to preserve bit 0 for
4248-
* the head page of compound page and bit 1 for pfmemalloc page.
4249-
* page_is_pfmemalloc() is checked in __page_pool_put_page() to avoid recycling
4250-
* the pfmemalloc page.
4286+
* the head page of compound page and bit 1 for pfmemalloc page, as well as the
4287+
* bits used for the DMA index. page_is_pfmemalloc() is checked in
4288+
* __page_pool_put_page() to avoid recycling the pfmemalloc page.
42514289
*/
4252-
#define PP_MAGIC_MASK ~0x3UL
4290+
#define PP_MAGIC_MASK ~(PP_DMA_INDEX_MASK | 0x3UL)
42534291

42544292
#ifdef CONFIG_PAGE_POOL
42554293
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
*
@@ -216,6 +220,8 @@ struct page_pool {
216220

217221
void *mp_priv;
218222

223+
struct xarray dma_mapped;
224+
219225
#ifdef CONFIG_PAGE_POOL_STATS
220226
/* recycle stats are per-cpu to avoid locking */
221227
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
@@ -273,8 +273,7 @@ static int page_pool_init(struct page_pool *pool,
273273
/* Driver calling page_pool_create() also call page_pool_destroy() */
274274
refcount_set(&pool->user_cnt, 1);
275275

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

279278
if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) {
280279
/* We rely on rtnl_lock()ing to make sure netdev_rx_queue
@@ -312,9 +311,7 @@ static int page_pool_init(struct page_pool *pool,
312311
static void page_pool_uninit(struct page_pool *pool)
313312
{
314313
ptr_ring_cleanup(&pool->ring, NULL);
315-
316-
if (pool->dma_map)
317-
put_device(pool->p.dev);
314+
xa_destroy(&pool->dma_mapped);
318315

319316
#ifdef CONFIG_PAGE_POOL_STATS
320317
if (!pool->system)
@@ -455,13 +452,21 @@ page_pool_dma_sync_for_device(const struct page_pool *pool,
455452
netmem_ref netmem,
456453
u32 dma_sync_size)
457454
{
458-
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev))
459-
__page_pool_dma_sync_for_device(pool, netmem, dma_sync_size);
455+
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) {
456+
rcu_read_lock();
457+
/* re-check under rcu_read_lock() to sync with page_pool_scrub() */
458+
if (pool->dma_sync)
459+
__page_pool_dma_sync_for_device(pool, netmem,
460+
dma_sync_size);
461+
rcu_read_unlock();
462+
}
460463
}
461464

462-
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)
465+
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t gfp)
463466
{
464467
dma_addr_t dma;
468+
int err;
469+
u32 id;
465470

466471
/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
467472
* since dma_addr_t can be either 32 or 64 bits and does not always fit
@@ -475,15 +480,30 @@ static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)
475480
if (dma_mapping_error(pool->p.dev, dma))
476481
return false;
477482

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

499+
netmem_set_dma_index(netmem, id);
481500
page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len);
482501

483502
return true;
484503

504+
unset_failed:
505+
page_pool_set_dma_addr_netmem(netmem, 0);
485506
unmap_failed:
486-
WARN_ONCE(1, "unexpected DMA address, please report to netdev@");
487507
dma_unmap_page_attrs(pool->p.dev, dma,
488508
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
489509
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
@@ -500,7 +520,7 @@ static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
500520
if (unlikely(!page))
501521
return NULL;
502522

503-
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page)))) {
523+
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page), gfp))) {
504524
put_page(page);
505525
return NULL;
506526
}
@@ -547,7 +567,7 @@ static noinline netmem_ref __page_pool_alloc_pages_slow(struct page_pool *pool,
547567
*/
548568
for (i = 0; i < nr_pages; i++) {
549569
netmem = pool->alloc.cache[i];
550-
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem))) {
570+
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem, gfp))) {
551571
put_page(netmem_to_page(netmem));
552572
continue;
553573
}
@@ -649,6 +669,8 @@ void page_pool_clear_pp_info(netmem_ref netmem)
649669
static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
650670
netmem_ref netmem)
651671
{
672+
struct page *old, *page = netmem_to_page(netmem);
673+
unsigned long id;
652674
dma_addr_t dma;
653675

654676
if (!pool->dma_map)
@@ -657,13 +679,25 @@ static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
657679
*/
658680
return;
659681

682+
id = netmem_get_dma_index(netmem);
683+
if (!id)
684+
return;
685+
686+
if (in_softirq())
687+
old = xa_cmpxchg(&pool->dma_mapped, id, page, NULL, 0);
688+
else
689+
old = xa_cmpxchg_bh(&pool->dma_mapped, id, page, NULL, 0);
690+
if (old != page)
691+
return;
692+
660693
dma = page_pool_get_dma_addr_netmem(netmem);
661694

662695
/* When page is unmapped, it cannot be returned to our pool */
663696
dma_unmap_page_attrs(pool->p.dev, dma,
664697
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
665698
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
666699
page_pool_set_dma_addr_netmem(netmem, 0);
700+
netmem_set_dma_index(netmem, 0);
667701
}
668702

669703
/* Disconnects a page (from a page_pool). API users can have a need
@@ -1038,8 +1072,29 @@ static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
10381072

10391073
static void page_pool_scrub(struct page_pool *pool)
10401074
{
1075+
unsigned long id;
1076+
void *ptr;
1077+
10411078
page_pool_empty_alloc_cache_once(pool);
1042-
pool->destroy_cnt++;
1079+
if (!pool->destroy_cnt++ && pool->dma_map) {
1080+
if (pool->dma_sync) {
1081+
/* Disable page_pool_dma_sync_for_device() */
1082+
pool->dma_sync = false;
1083+
1084+
/* Make sure all concurrent returns that may see the old
1085+
* value of dma_sync (and thus perform a sync) have
1086+
* finished before doing the unmapping below. Skip the
1087+
* wait if the device doesn't actually need syncing, or
1088+
* if there are no outstanding mapped pages.
1089+
*/
1090+
if (dma_dev_need_sync(pool->p.dev) &&
1091+
!xa_empty(&pool->dma_mapped))
1092+
synchronize_net();
1093+
}
1094+
1095+
xa_for_each(&pool->dma_mapped, id, ptr)
1096+
__page_pool_release_page_dma(pool, page_to_netmem(ptr));
1097+
}
10431098

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

0 commit comments

Comments
 (0)