Skip to contentSkip to conversation

mm: Explicitly check & doc fragsz limit

PATCH In review Haiyang Zhang 2 participants 6 messages Apr 3, 2025 – Apr 16, 2025

Subsystems (6)
lore
Share & tools
lore

Haiyang Zhang

author maintainer
#0 lore raw

cover mm: Explicitly check & doc fragsz limit

The page frag allocator is not designed for fragsz > PAGE_SIZE. Explicitly check it in the function & document the fragsz limit.

Haiyang Zhang (2):
mm: page_frag: Check fragsz at the beginning of __page_frag_alloc_align() docs/mm: Specify page frag size is not bigger than PAGE_SIZE

 Documentation/mm/page_frags.rst |  2 +-
 mm/page_frag_cache.c            | 22 +++++++++-------------
 2 files changed, 10 insertions(+), 14 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/2 mm: page_frag: Check fragsz at the beginning of __page_frag_alloc_align()

Frag allocator is not designed for fragsz > PAGE_SIZE. So, check and return the error at the beginning of __page_frag_alloc_align(), instead of succeed for a few times, then fail due to not refilling the cache.

---
 mm/page_frag_cache.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
mm/page_frag_cache.c +9 -13
index d2423f30577e..d6bf022087e7 100644
@@ -98,6 +98,15 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc,
 	unsigned int size, offset;
 	struct page *page;
 
+	if (unlikely(fragsz > PAGE_SIZE)) {
+		/*
+		 * The caller is trying to allocate a fragment
+		 * with fragsz > PAGE_SIZE which is not supported
+		 * by design. So we simply return NULL here.
+		 */
+		return NULL;
+	}
+
 	if (unlikely(!encoded_page)) {
 refill:
 		page = __page_frag_cache_refill(nc, gfp_mask);
@@ -119,19 +128,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc,
 	size = PAGE_SIZE << encoded_page_decode_order(encoded_page);
 	offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask);
 	if (unlikely(offset + fragsz > size)) {
-		if (unlikely(fragsz > PAGE_SIZE)) {
-			/*
-			 * The caller is trying to allocate a fragment
-			 * with fragsz > PAGE_SIZE but the cache isn't big
-			 * enough to satisfy the request, this may
-			 * happen in low memory conditions.
-			 * We don't release the cache page because
-			 * it could make memory pressure worse
-			 * so we simply return NULL here.
-			 */
-			return NULL;
-		}
-
 		page = encoded_page_decode_page(encoded_page);
 
 		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] mm: page_frag: Check fragsz at the beginning of __page_frag_alloc_align()

Quoted 15 lines · Haiyang Zhang

Frag allocator is not designed for fragsz > PAGE_SIZE. So, check and return the error at the beginning of __page_frag_alloc_align(), instead of succeed for a few times, then fail due to not refilling the cache.

---
 mm/page_frag_cache.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
mm/page_frag_cache.c +0 -0
index d2423f30577e..d6bf022087e7 100644
@@ -98,6 +98,15 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc,
 	unsigned int size, offset;

mm/page_frag_cache.c void *__page_frag_alloc_align(struct page_frag_cache *nc, · line 108

Quoted 10 lines
mm/page_frag_cache.c +8 -0
 	struct page *page;
 
+	if (unlikely(fragsz > PAGE_SIZE)) {
+		/*
+		 * The caller is trying to allocate a fragment
+		 * with fragsz > PAGE_SIZE which is not supported
+		 * by design. So we simply return NULL here.
+		 */
+		return NULL;
+	}

The checking is done at below to avoid doing the checking for the likely case of cache being enough as the frag API is mostly used to allocate small memory.

And it seems my recent refactoring to frag API have made two frag API misuse more obvious if I recalled it correctly. If more explicit about that for all the codepath is really helpful, perhaps VM_BUG_ON() is an option to make it more explicit while avoiding the checking as much as possible.

Quoted 24 lines
mm/page_frag_cache.c +1 -13
+
 	if (unlikely(!encoded_page)) {
 refill:
 		page = __page_frag_cache_refill(nc, gfp_mask);
@@ -119,19 +128,6 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc,
 	size = PAGE_SIZE << encoded_page_decode_order(encoded_page);
 	offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask);
 	if (unlikely(offset + fragsz > size)) {
-		if (unlikely(fragsz > PAGE_SIZE)) {
-			/*
-			 * The caller is trying to allocate a fragment
-			 * with fragsz > PAGE_SIZE but the cache isn't big
-			 * enough to satisfy the request, this may
-			 * happen in low memory conditions.
-			 * We don't release the cache page because
-			 * it could make memory pressure worse
-			 * so we simply return NULL here.
-			 */
-			return NULL;
-		}
-
 		page = encoded_page_decode_page(encoded_page);
 
 		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
author maintainer
#3 lore raw

RE: [EXTERNAL] Re: [PATCH 1/2] mm: page_frag: Check fragsz at the beginning of __page_frag_alloc_align()

Quoted 48 lines

-----Original Message-----
From: Yunsheng Lin <[email protected]>
Sent: Wednesday, April 16, 2025 3:38 AM
To: Haiyang Zhang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Cc: Dexuan Cui <[email protected]>; KY Srinivasan <[email protected]>; Paul Rosswurm <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Long Li <[email protected]>; [email protected]
Subject: [EXTERNAL] Re: [PATCH 1/2] mm: page_frag: Check fragsz at the beginning of __page_frag_alloc_align()

Quoted 1 line · Haiyang Zhang

Frag allocator is not designed for fragsz > PAGE_SIZE. So, check and

return

mm/page_frag_cache.c void *__page_frag_alloc_align(struct page_frag_cache

Quoted 13 lines

the error at the beginning of __page_frag_alloc_align(), instead of succeed for a few times, then fail due to not refilling the cache.

---
 mm/page_frag_cache.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
mm/page_frag_cache.c +0 -0
index d2423f30577e..d6bf022087e7 100644
@@ -98,6 +98,15 @@ void *__page_frag_alloc_align(struct page_frag_cache

*nc,

mm/page_frag_cache.c void *__page_frag_alloc_align(struct page_frag_cache *nc, · line 108

Quoted 11 lines
mm/page_frag_cache.c +8 -0
 	unsigned int size, offset;
 	struct page *page;
 
+	if (unlikely(fragsz > PAGE_SIZE)) {
+		/*
+		 * The caller is trying to allocate a fragment
+		 * with fragsz > PAGE_SIZE which is not supported
+		 * by design. So we simply return NULL here.
+		 */
+		return NULL;
+	}

The checking is done at below to avoid doing the checking for the likely case of cache being enough as the frag API is mostly used to allocate small memory.

Quoted 5 lines

And it seems my recent refactoring to frag API have made two frag API misuse more obvious if I recalled it correctly. If more explicit about that for all the codepath is really helpful, perhaps VM_BUG_ON() is an option to make it more explicit while avoiding the checking as much as possible.

Thanks, I will use VM_BUG_ON() instead of a checking here. Also, will keep the checking below unchanged.

Quoted 25 lines

mm/page_frag_cache.c void *__page_frag_alloc_align(struct

Quoted 5 lines
mm/page_frag_cache.c +1 -0
+
 	if (unlikely(!encoded_page)) {
 refill:
 		page = __page_frag_cache_refill(nc, gfp_mask);
@@ -119,19 +128,6 @@ void *__page_frag_alloc_align(struct

page_frag_cache *nc,

Quoted 19 lines
mm/page_frag_cache.c +0 -13
 	size = PAGE_SIZE << encoded_page_decode_order(encoded_page);
 	offset = __ALIGN_KERNEL_MASK(nc->offset, ~align_mask);
 	if (unlikely(offset + fragsz > size)) {
-		if (unlikely(fragsz > PAGE_SIZE)) {
-			/*
-			 * The caller is trying to allocate a fragment
-			 * with fragsz > PAGE_SIZE but the cache isn't big
-			 * enough to satisfy the request, this may
-			 * happen in low memory conditions.
-			 * We don't release the cache page because
-			 * it could make memory pressure worse
-			 * so we simply return NULL here.
-			 */
-			return NULL;
-		}
-
 		page = encoded_page_decode_page(encoded_page);
 
 		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
author maintainer
#4 lore raw

PATCH 2/2 docs/mm: Specify page frag size is not bigger than PAGE_SIZE

In reply to Haiyang Zhang

The page frag allocator is not designed for fragsz > PAGE_SIZE. Specify this in the document.

---
 Documentation/mm/page_frags.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/mm/page_frags.rst +1 -1
index 503ca6cdb804..212ecc69dd74 100644
@@ -2,7 +2,7 @@
 Page fragments
 ==============
 
-A page fragment is an arbitrary-length arbitrary-offset area of memory
+A page fragment is a len <= PAGE_SIZE, arbitrary-offset area of memory
 which resides within a 0 or higher order compound page.  Multiple
 fragments within that page are individually refcounted, in the page's
 reference counter.
Signature · 1 line
2.34.1
author maintainer
#5 lore raw
In reply to Haiyang Zhang
Quoted 17 lines

-----Original Message-----
From: LKML haiyangz <[email protected]> On Behalf Of Haiyang Zhang
Sent: Thursday, April 3, 2025 5:22 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: Haiyang Zhang <[email protected]>; Dexuan Cui <[email protected]>; KY Srinivasan <[email protected]>; Paul Rosswurm <[email protected]>; [email protected]; vkuznets <[email protected]>; [email protected]; [email protected]; Long Li <[email protected]>; [email protected]
Subject: [PATCH 0/2] mm: Explicitly check & doc fragsz limit

The page frag allocator is not designed for fragsz > PAGE_SIZE. Explicitly check it in the function & document the fragsz limit.

Quoted 9 lines

Haiyang Zhang (2):
mm: page_frag: Check fragsz at the beginning of
__page_frag_alloc_align()
docs/mm: Specify page frag size is not bigger than PAGE_SIZE

 Documentation/mm/page_frags.rst |  2 +-
 mm/page_frag_cache.c            | 22 +++++++++-------------
 2 files changed, 10 insertions(+), 14 deletions(-)

+@Mike Rapoport (Microsoft) <[email protected]>
Hi Mike, since you have a lot experience in mm subsystem, could you please review my patch set?

Thanks,
- Haiyang