kernel: backport fix for a page pool related race condition
[openwrt/staging/dedeckeh.git] / target / linux / generic / backport-5.10 / 633-v6.3-skbuff-Fix-a-race-between-coalescing-and-releasing-S.patch
1 From: Liang Chen <liangchen.linux@gmail.com>
2 Date: Thu, 13 Apr 2023 17:03:53 +0800
3 Subject: [PATCH] skbuff: Fix a race between coalescing and releasing SKBs
4
5 Commit 1effe8ca4e34 ("skbuff: fix coalescing for page_pool fragment
6 recycling") allowed coalescing to proceed with non page pool page and page
7 pool page when @from is cloned, i.e.
8
9 to->pp_recycle --> false
10 from->pp_recycle --> true
11 skb_cloned(from) --> true
12
13 However, it actually requires skb_cloned(@from) to hold true until
14 coalescing finishes in this situation. If the other cloned SKB is
15 released while the merging is in process, from_shinfo->nr_frags will be
16 set to 0 toward the end of the function, causing the increment of frag
17 page _refcount to be unexpectedly skipped resulting in inconsistent
18 reference counts. Later when SKB(@to) is released, it frees the page
19 directly even though the page pool page is still in use, leading to
20 use-after-free or double-free errors. So it should be prohibited.
21
22 The double-free error message below prompted us to investigate:
23 BUG: Bad page state in process swapper/1 pfn:0e0d1
24 page:00000000c6548b28 refcount:-1 mapcount:0 mapping:0000000000000000
25 index:0x2 pfn:0xe0d1
26 flags: 0xfffffc0000000(node=0|zone=1|lastcpupid=0x1fffff)
27 raw: 000fffffc0000000 0000000000000000 ffffffff00000101 0000000000000000
28 raw: 0000000000000002 0000000000000000 ffffffffffffffff 0000000000000000
29 page dumped because: nonzero _refcount
30
31 CPU: 1 PID: 0 Comm: swapper/1 Tainted: G E 6.2.0+
32 Call Trace:
33 <IRQ>
34 dump_stack_lvl+0x32/0x50
35 bad_page+0x69/0xf0
36 free_pcp_prepare+0x260/0x2f0
37 free_unref_page+0x20/0x1c0
38 skb_release_data+0x10b/0x1a0
39 napi_consume_skb+0x56/0x150
40 net_rx_action+0xf0/0x350
41 ? __napi_schedule+0x79/0x90
42 __do_softirq+0xc8/0x2b1
43 __irq_exit_rcu+0xb9/0xf0
44 common_interrupt+0x82/0xa0
45 </IRQ>
46 <TASK>
47 asm_common_interrupt+0x22/0x40
48 RIP: 0010:default_idle+0xb/0x20
49
50 Fixes: 53e0961da1c7 ("page_pool: add frag page recycling support in page pool")
51 Signed-off-by: Liang Chen <liangchen.linux@gmail.com>
52 Reviewed-by: Eric Dumazet <edumazet@google.com>
53 Link: https://lore.kernel.org/r/20230413090353.14448-1-liangchen.linux@gmail.com
54 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
55 ---
56
57 --- a/net/core/skbuff.c
58 +++ b/net/core/skbuff.c
59 @@ -5208,18 +5208,18 @@ bool skb_try_coalesce(struct sk_buff *to
60 if (skb_cloned(to))
61 return false;
62
63 - /* In general, avoid mixing slab allocated and page_pool allocated
64 - * pages within the same SKB. However when @to is not pp_recycle and
65 - * @from is cloned, we can transition frag pages from page_pool to
66 - * reference counted.
67 - *
68 - * On the other hand, don't allow coalescing two pp_recycle SKBs if
69 - * @from is cloned, in case the SKB is using page_pool fragment
70 + /* In general, avoid mixing page_pool and non-page_pool allocated
71 + * pages within the same SKB. Additionally avoid dealing with clones
72 + * with page_pool pages, in case the SKB is using page_pool fragment
73 * references (PP_FLAG_PAGE_FRAG). Since we only take full page
74 * references for cloned SKBs at the moment that would result in
75 * inconsistent reference counts.
76 + * In theory we could take full references if @from is cloned and
77 + * !@to->pp_recycle but its tricky (due to potential race with
78 + * the clone disappearing) and rare, so not worth dealing with.
79 */
80 - if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from)))
81 + if (to->pp_recycle != from->pp_recycle ||
82 + (from->pp_recycle && skb_cloned(from)))
83 return false;
84
85 if (len <= skb_tailroom(to)) {