Skip to contentSkip to conversation

mm/page_owner: clamp read count to PAGE_SIZE

PATCH In review [email protected] 6 participants 7 messages Nov 1, 2018 – Nov 6, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 mm/page_owner: clamp read count to PAGE_SIZE

From: Miles Chen <[email protected]>

The page owner read might allocate a large size of memory with a large read count. Allocation fails can easily occur when doing high order allocations.

Clamp buffer size to PAGE_SIZE to avoid arbitrary size allocation and avoid allocation fails due to high order allocation.

Change since v3:
- remove the change in kvmalloc
- keep kmalloc in page_owner.c

Change since v2:
- improve kvmalloc, allow sub page allocations fallback to vmalloc when CONFIG_HIGHMEM=y

Change since v1:
- use kvmalloc()
- clamp buffer size to PAGE_SIZE

Cc Joe Perches <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/page_owner.c | 1 +
 1 file changed, 1 insertion(+)
mm/page_owner.c +1 -0
index 87bc0dfdb52b..b83f295e4eca 100644
@@ -351,6 +351,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		.skip = 0
 	};
 
+	count = count > PAGE_SIZE ? PAGE_SIZE : count;
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
Signature · 1 line
2.18.0
Acked-by on v4 1/1
#1 lore raw
Quoted 8 lines · miles.chen at mediatek.com

From: Miles Chen <[email protected]>

The page owner read might allocate a large size of memory with a large read count. Allocation fails can easily occur when doing high order allocations.

Clamp buffer size to PAGE_SIZE to avoid arbitrary size allocation and avoid allocation fails due to high order allocation.

It is good to mention that interface is root only so the harm due to unbounded allocation request is somehow reduced.

I believe we want to use seq_file infrastructure in the long term solution.

Quoted 8 lines

Change since v3:
- remove the change in kvmalloc
- keep kmalloc in page_owner.c

Change since v2:
- improve kvmalloc, allow sub page allocations fallback to vmalloc when CONFIG_HIGHMEM=y

Quoted 8 lines

Change since v1:
- use kvmalloc()
- clamp buffer size to PAGE_SIZE

Cc Joe Perches <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Michal Hocko <[email protected]>

Thanks!

Quoted 19 lines
---
 mm/page_owner.c | 1 +
 1 file changed, 1 insertion(+)
mm/page_owner.c +1 -0
index 87bc0dfdb52b..b83f295e4eca 100644
@@ -351,6 +351,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		.skip = 0
 	};
 
+	count = count > PAGE_SIZE ? PAGE_SIZE : count;
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
Signature · 1 line
2.18.0
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#2 lore raw
In reply to [email protected]
Quoted 14 lines · On Fri, 2 Nov 2018 01:00:07 +0800 <[email protected]> wrote:

From: Miles Chen <[email protected]>

The page owner read might allocate a large size of memory with a large read count. Allocation fails can easily occur when doing high order allocations.

Clamp buffer size to PAGE_SIZE to avoid arbitrary size allocation and avoid allocation fails due to high order allocation.

...

mm/page_owner.c print_page_owner(char __user *buf, size_t count, unsigned long pfn, · line 357

Quoted 7 lines
mm/page_owner.c +1 -0
@@ -351,6 +351,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		.skip = 0
 	};
 
+	count = count > PAGE_SIZE ? PAGE_SIZE : count;
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;

A bit tidier:

mm/page_owner.c renamed from mm/page_owner.c~mm-page_owner-clamp-read-count-to-page_size-fix +1 -1
@@ -351,7 +351,7 @@ print_page_owner(char __user *buf, size_
 		.skip = 0
 	};
 
-	count = count > PAGE_SIZE ? PAGE_SIZE : count;
+	count = min_t(size_t, count, PAGE_SIZE);
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
#3 lore raw
Quoted 29 lines · Andrew Morton
Quoted 14 lines · On Fri, 2 Nov 2018 01:00:07 +0800 <[email protected]> wrote:

From: Miles Chen <[email protected]>

The page owner read might allocate a large size of memory with a large read count. Allocation fails can easily occur when doing high order allocations.

Clamp buffer size to PAGE_SIZE to avoid arbitrary size allocation and avoid allocation fails due to high order allocation.

...

mm/page_owner.c print_page_owner(char __user *buf, size_t count, unsigned long pfn, · line 357

Quoted 7 lines
mm/page_owner.c +1 -0
@@ -351,6 +351,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		.skip = 0
 	};
 
+	count = count > PAGE_SIZE ? PAGE_SIZE : count;
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;

A bit tidier:

mm/page_owner.c print_page_owner(char __user *buf, size_ · line 357

Quoted 8 lines
mm/page_owner.c renamed from mm/page_owner.c~mm-page_owner-clamp-read-count-to-page_size-fix +1 -1
@@ -351,7 +351,7 @@ print_page_owner(char __user *buf, size_
 		.skip = 0
 	};
 
-	count = count > PAGE_SIZE ? PAGE_SIZE : count;
+	count = min_t(size_t, count, PAGE_SIZE);
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;

A bit tidier still might be

	if (count > PAGE_SIZE)
		count = PAGE_SIZE;

as that would not always cause a write back to count.

#4 lore raw
Quoted 14 lines · Joe Perches

mm/page_owner.c print_page_owner(char __user *buf, size_ · line 357

Quoted 10 lines · Andrew Morton

+++ a/mm/page_owner.c

mm/page_owner.c renamed from mm/page_owner.c~mm-page_owner-clamp-read-count-to-page_size-fix +1 -1
@@ -351,7 +351,7 @@ print_page_owner(char __user *buf, size_
 		.skip = 0
 	};
 
-	count = count > PAGE_SIZE ? PAGE_SIZE : count;
+	count = min_t(size_t, count, PAGE_SIZE);
 	kbuf = kmalloc(count, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;

A bit tidier still might be

Quoted 4 lines
	if (count > PAGE_SIZE)
		count = PAGE_SIZE;

as that would not always cause a write back to count.

90% chance 'count' is already in a register and will stay there. 99.9% chance that if it's not in a register, it's on the top of the stack, which is by definition a hot, local, dirty cacheline.

What you're saying makes sense for a struct which might well be in a shared cacheline state. But for a function-local variable? No.

#5 lore raw
In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
mm/page_owner.c renamed from mm/page_owner.c~mm-page_owner-clamp-read-count-to-page_size-fix +1 -1
-	count = count > PAGE_SIZE ? PAGE_SIZE : count;
+	count = min_t(size_t, count, PAGE_SIZE);
	kbuf = kmalloc(count, GFP_KERNEL);
	if (!kbuf)
		return -ENOMEM;

Is the use of min_t vs. the C conditional mostly to be more self-documenting?

The compiler-generated assembly between the two versions seems mostly a wash.

William Kucharski

maintainer
#6 lore raw
Quoted 11 lines · On Thu, 1 Nov 2018 18:41:33 -0600 William Kucharski…
Quoted 9 lines · Andrew Morton
mm/page_owner.c renamed from mm/page_owner.c~mm-page_owner-clamp-read-count-to-page_size-fix +1 -1
-	count = count > PAGE_SIZE ? PAGE_SIZE : count;
+	count = min_t(size_t, count, PAGE_SIZE);
	kbuf = kmalloc(count, GFP_KERNEL);
	if (!kbuf)
		return -ENOMEM;

Is the use of min_t vs. the C conditional mostly to be more self-documenting?

Yup. It saves the reader from having to parse the code to figure out "this is a min operation".