Skip to content

Commit ee62ce7

Browse files
tohojokuba-moo
authored andcommitted
page_pool: Track DMA-mapped pages and unmap them when destroying the pool
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]>
1 parent cd3c931 commit ee62ce7

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
@@ -4248,13 +4248,51 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);
42484248
#define VM_SEALED_SYSMAP VM_NONE
42494249
#endif
42504250

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

42594297
#ifdef CONFIG_PAGE_POOL
42604298
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
*
@@ -221,6 +225,8 @@ struct page_pool {
221225
void *mp_priv;
222226
const struct memory_provider_ops *mp_ops;
223227

228+
struct xarray dma_mapped;
229+
224230
#ifdef CONFIG_PAGE_POOL_STATS
225231
/* recycle stats are per-cpu to avoid locking */
226232
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
@@ -276,8 +276,7 @@ static int page_pool_init(struct page_pool *pool,
276276
/* Driver calling page_pool_create() also call page_pool_destroy() */
277277
refcount_set(&pool->user_cnt, 1);
278278

279-
if (pool->dma_map)
280-
get_device(pool->p.dev);
279+
xa_init_flags(&pool->dma_mapped, XA_FLAGS_ALLOC1);
281280

282281
if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) {
283282
netdev_assert_locked(pool->slow.netdev);
@@ -320,9 +319,7 @@ static int page_pool_init(struct page_pool *pool,
320319
static void page_pool_uninit(struct page_pool *pool)
321320
{
322321
ptr_ring_cleanup(&pool->ring, NULL);
323-
324-
if (pool->dma_map)
325-
put_device(pool->p.dev);
322+
xa_destroy(&pool->dma_mapped);
326323

327324
#ifdef CONFIG_PAGE_POOL_STATS
328325
if (!pool->system)
@@ -463,13 +460,21 @@ page_pool_dma_sync_for_device(const struct page_pool *pool,
463460
netmem_ref netmem,
464461
u32 dma_sync_size)
465462
{
466-
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev))
467-
__page_pool_dma_sync_for_device(pool, netmem, dma_sync_size);
463+
if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) {
464+
rcu_read_lock();
465+
/* re-check under rcu_read_lock() to sync with page_pool_scrub() */
466+
if (pool->dma_sync)
467+
__page_pool_dma_sync_for_device(pool, netmem,
468+
dma_sync_size);
469+
rcu_read_unlock();
470+
}
468471
}
469472

470-
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)
473+
static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t gfp)
471474
{
472475
dma_addr_t dma;
476+
int err;
477+
u32 id;
473478

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

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

507+
netmem_set_dma_index(netmem, id);
489508
page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len);
490509

491510
return true;
492511

512+
unset_failed:
513+
page_pool_set_dma_addr_netmem(netmem, 0);
493514
unmap_failed:
494-
WARN_ONCE(1, "unexpected DMA address, please report to netdev@");
495515
dma_unmap_page_attrs(pool->p.dev, dma,
496516
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
497517
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
@@ -508,7 +528,7 @@ static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
508528
if (unlikely(!page))
509529
return NULL;
510530

511-
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page)))) {
531+
if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page), gfp))) {
512532
put_page(page);
513533
return NULL;
514534
}
@@ -554,7 +574,7 @@ static noinline netmem_ref __page_pool_alloc_pages_slow(struct page_pool *pool,
554574
*/
555575
for (i = 0; i < nr_pages; i++) {
556576
netmem = pool->alloc.cache[i];
557-
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem))) {
577+
if (dma_map && unlikely(!page_pool_dma_map(pool, netmem, gfp))) {
558578
put_page(netmem_to_page(netmem));
559579
continue;
560580
}
@@ -656,6 +676,8 @@ void page_pool_clear_pp_info(netmem_ref netmem)
656676
static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
657677
netmem_ref netmem)
658678
{
679+
struct page *old, *page = netmem_to_page(netmem);
680+
unsigned long id;
659681
dma_addr_t dma;
660682

661683
if (!pool->dma_map)
@@ -664,13 +686,25 @@ static __always_inline void __page_pool_release_page_dma(struct page_pool *pool,
664686
*/
665687
return;
666688

689+
id = netmem_get_dma_index(netmem);
690+
if (!id)
691+
return;
692+
693+
if (in_softirq())
694+
old = xa_cmpxchg(&pool->dma_mapped, id, page, NULL, 0);
695+
else
696+
old = xa_cmpxchg_bh(&pool->dma_mapped, id, page, NULL, 0);
697+
if (old != page)
698+
return;
699+
667700
dma = page_pool_get_dma_addr_netmem(netmem);
668701

669702
/* When page is unmapped, it cannot be returned to our pool */
670703
dma_unmap_page_attrs(pool->p.dev, dma,
671704
PAGE_SIZE << pool->p.order, pool->p.dma_dir,
672705
DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
673706
page_pool_set_dma_addr_netmem(netmem, 0);
707+
netmem_set_dma_index(netmem, 0);
674708
}
675709

676710
/* Disconnects a page (from a page_pool). API users can have a need
@@ -1080,8 +1114,29 @@ static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
10801114

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

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

0 commit comments

Comments
 (0)