aboutsummaryrefslogtreecommitdiffstats
path: root/mm/page_alloc.c
diff options
context:
space:
mode:
authorJann Horn <jannh@google.com>2019-02-13 16:45:59 -0500
committerDavid S. Miller <davem@davemloft.net>2019-02-14 12:12:17 -0500
commit2c2ade81741c66082f8211f0b96cf509cc4c0218 (patch)
treed4bad2a5d24ee88ef3d67dcc84a29c6309dd99e4 /mm/page_alloc.c
parent61c4c0bcff87773cd65d1cf487e2b2a34bc6730f (diff)
mm: page_alloc: fix ref bias in page_frag_alloc() for 1-byte allocs
The basic idea behind ->pagecnt_bias is: If we pre-allocate the maximum number of references that we might need to create in the fastpath later, the bump-allocation fastpath only has to modify the non-atomic bias value that tracks the number of extra references we hold instead of the atomic refcount. The maximum number of allocations we can serve (under the assumption that no allocation is made with size 0) is nc->size, so that's the bias used. However, even when all memory in the allocation has been given away, a reference to the page is still held; and in the `offset < 0` slowpath, the page may be reused if everyone else has dropped their references. This means that the necessary number of references is actually `nc->size+1`. Luckily, from a quick grep, it looks like the only path that can call page_frag_alloc(fragsz=1) is TAP with the IFF_NAPI_FRAGS flag, which requires CAP_NET_ADMIN in the init namespace and is only intended to be used for kernel testing and fuzzing. To test for this issue, put a `WARN_ON(page_ref_count(page) == 0)` in the `offset < 0` path, below the virt_to_page() call, and then repeatedly call writev() on a TAP device with IFF_TAP|IFF_NO_PI|IFF_NAPI_FRAGS|IFF_NAPI, with a vector consisting of 15 elements containing 1 byte each. Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'mm/page_alloc.c')
-rw-r--r--mm/page_alloc.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 35fdde041f5c..46285d28e43b 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -4675,11 +4675,11 @@ refill:
4675 /* Even if we own the page, we do not use atomic_set(). 4675 /* Even if we own the page, we do not use atomic_set().
4676 * This would break get_page_unless_zero() users. 4676 * This would break get_page_unless_zero() users.
4677 */ 4677 */
4678 page_ref_add(page, size - 1); 4678 page_ref_add(page, size);
4679 4679
4680 /* reset page count bias and offset to start of new frag */ 4680 /* reset page count bias and offset to start of new frag */
4681 nc->pfmemalloc = page_is_pfmemalloc(page); 4681 nc->pfmemalloc = page_is_pfmemalloc(page);
4682 nc->pagecnt_bias = size; 4682 nc->pagecnt_bias = size + 1;
4683 nc->offset = size; 4683 nc->offset = size;
4684 } 4684 }
4685 4685
@@ -4695,10 +4695,10 @@ refill:
4695 size = nc->size; 4695 size = nc->size;
4696#endif 4696#endif
4697 /* OK, page count is 0, we can safely set it */ 4697 /* OK, page count is 0, we can safely set it */
4698 set_page_count(page, size); 4698 set_page_count(page, size + 1);
4699 4699
4700 /* reset page count bias and offset to start of new frag */ 4700 /* reset page count bias and offset to start of new frag */
4701 nc->pagecnt_bias = size; 4701 nc->pagecnt_bias = size + 1;
4702 offset = size - fragsz; 4702 offset = size - fragsz;
4703 } 4703 }
4704 4704