Skip to content

Commit 932b59e

Browse files
YJNWorkstationakpm00
authored andcommitted
mm: fix process_vm_rw page counts
1. There is a "-1" missing in the page number calculation in process_vm_rw_core. While this can't break anything, it can cause unnecessary allocations in certain cases: Consider handling an iovec ranging over PVM_MAX_PP_ARRAY_COUNT pages that is also aligned to a page boundary. While pp_stack could hold references to such an amount of pinned pages, nr_pages yields (PVM_MAX_PP_ARRAY + 1) in process_vm_rw_core. Consequently, a larger buffer is allocated with kmalloc for no reason. For any page boundary aligned iovec that is a multiple of PAGE_SIZE and larger than PVM_MAX_PP_ARRAY_COUNT pages, nr_pages will be too big by 1 and thus kmalloc allocates excess space for one more pointer. 2. max_pages_per_loop is constant and there is no reason to have it as a variable. A macro does the job just fine and saves memory. 3. Replaced "sizeof(struct pages *)" with "sizeof(struct page *)" to have matching types for allocation and prevent confusion. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: York Jasper Niebuhr <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 69e583e commit 932b59e

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

mm/process_vm_access.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ static int process_vm_rw_pages(struct page **pages,
5353
}
5454

5555
/* Maximum number of pages kmalloc'd to hold struct page's during copy */
56-
#define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
56+
#define PVM_MAX_KMALLOC_PAGES 2
57+
58+
/* Maximum number of pages that can be stored at a time */
59+
#define PVM_MAX_USER_PAGES (PVM_MAX_KMALLOC_PAGES * PAGE_SIZE / sizeof(struct page *))
5760

5861
/**
5962
* process_vm_rw_single_vec - read/write pages from task specified
@@ -79,8 +82,6 @@ static int process_vm_rw_single_vec(unsigned long addr,
7982
unsigned long start_offset = addr - pa;
8083
unsigned long nr_pages;
8184
ssize_t rc = 0;
82-
unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
83-
/ sizeof(struct pages *);
8485
unsigned int flags = 0;
8586

8687
/* Work out address and page range required */
@@ -92,7 +93,7 @@ static int process_vm_rw_single_vec(unsigned long addr,
9293
flags |= FOLL_WRITE;
9394

9495
while (!rc && nr_pages && iov_iter_count(iter)) {
95-
int pinned_pages = min(nr_pages, max_pages_per_loop);
96+
int pinned_pages = min_t(unsigned long, nr_pages, PVM_MAX_USER_PAGES);
9697
int locked = 1;
9798
size_t bytes;
9899

@@ -171,7 +172,7 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
171172
iov_len = rvec[i].iov_len;
172173
if (iov_len > 0) {
173174
nr_pages_iov = ((unsigned long)rvec[i].iov_base
174-
+ iov_len)
175+
+ iov_len - 1)
175176
/ PAGE_SIZE - (unsigned long)rvec[i].iov_base
176177
/ PAGE_SIZE + 1;
177178
nr_pages = max(nr_pages, nr_pages_iov);
@@ -184,8 +185,8 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
184185
if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
185186
/* For reliability don't try to kmalloc more than
186187
2 pages worth */
187-
process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
188-
sizeof(struct pages *)*nr_pages),
188+
process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES * PAGE_SIZE,
189+
sizeof(struct page *)*nr_pages),
189190
GFP_KERNEL);
190191

191192
if (!process_pages)

0 commit comments

Comments
 (0)