Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 28a3571

Browse files
walken-googletorvalds
authored andcommitted
mm: use long type for page counts in mm_populate() and get_user_pages()
Use long type for page counts in mm_populate() so as to avoid integer overflow when running the following test code: int main(void) { void *p = mmap(NULL, 0x100000000000, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0); printf("p: %p\n", p); mlockall(MCL_CURRENT); printf("done\n"); return 0; } Signed-off-by: Michel Lespinasse <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Hugh Dickins <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e0fb581 commit 28a3571

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

include/linux/hugetlb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int,
4343
#endif
4444

4545
int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, struct vm_area_struct *);
46-
int follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
47-
struct page **, struct vm_area_struct **,
48-
unsigned long *, int *, int, unsigned int flags);
46+
long follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
47+
struct page **, struct vm_area_struct **,
48+
unsigned long *, unsigned long *, long, unsigned int);
4949
void unmap_hugepage_range(struct vm_area_struct *,
5050
unsigned long, unsigned long, struct page *);
5151
void __unmap_hugepage_range_final(struct mmu_gather *tlb,

include/linux/mm.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,13 +1013,14 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *
10131013
extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
10141014
void *buf, int len, int write);
10151015

1016-
int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1017-
unsigned long start, int len, unsigned int foll_flags,
1018-
struct page **pages, struct vm_area_struct **vmas,
1019-
int *nonblocking);
1020-
int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1021-
unsigned long start, int nr_pages, int write, int force,
1022-
struct page **pages, struct vm_area_struct **vmas);
1016+
long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1017+
unsigned long start, unsigned long nr_pages,
1018+
unsigned int foll_flags, struct page **pages,
1019+
struct vm_area_struct **vmas, int *nonblocking);
1020+
long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1021+
unsigned long start, unsigned long nr_pages,
1022+
int write, int force, struct page **pages,
1023+
struct vm_area_struct **vmas);
10231024
int get_user_pages_fast(unsigned long start, int nr_pages, int write,
10241025
struct page **pages);
10251026
struct kvec;

mm/hugetlb.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,14 +2920,14 @@ follow_huge_pud(struct mm_struct *mm, unsigned long address,
29202920
return NULL;
29212921
}
29222922

2923-
int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
2924-
struct page **pages, struct vm_area_struct **vmas,
2925-
unsigned long *position, int *length, int i,
2926-
unsigned int flags)
2923+
long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
2924+
struct page **pages, struct vm_area_struct **vmas,
2925+
unsigned long *position, unsigned long *nr_pages,
2926+
long i, unsigned int flags)
29272927
{
29282928
unsigned long pfn_offset;
29292929
unsigned long vaddr = *position;
2930-
int remainder = *length;
2930+
unsigned long remainder = *nr_pages;
29312931
struct hstate *h = hstate_vma(vma);
29322932

29332933
spin_lock(&mm->page_table_lock);
@@ -2997,7 +2997,7 @@ int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
29972997
}
29982998
}
29992999
spin_unlock(&mm->page_table_lock);
3000-
*length = remainder;
3000+
*nr_pages = remainder;
30013001
*position = vaddr;
30023002

30033003
return i ? i : -EFAULT;

mm/memory.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,15 +1677,15 @@ static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long add
16771677
* instead of __get_user_pages. __get_user_pages should be used only if
16781678
* you need some special @gup_flags.
16791679
*/
1680-
int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1681-
unsigned long start, int nr_pages, unsigned int gup_flags,
1682-
struct page **pages, struct vm_area_struct **vmas,
1683-
int *nonblocking)
1680+
long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1681+
unsigned long start, unsigned long nr_pages,
1682+
unsigned int gup_flags, struct page **pages,
1683+
struct vm_area_struct **vmas, int *nonblocking)
16841684
{
1685-
int i;
1685+
long i;
16861686
unsigned long vm_flags;
16871687

1688-
if (nr_pages <= 0)
1688+
if (!nr_pages)
16891689
return 0;
16901690

16911691
VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
@@ -1981,9 +1981,9 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
19811981
*
19821982
* See also get_user_pages_fast, for performance critical applications.
19831983
*/
1984-
int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1985-
unsigned long start, int nr_pages, int write, int force,
1986-
struct page **pages, struct vm_area_struct **vmas)
1984+
long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1985+
unsigned long start, unsigned long nr_pages, int write,
1986+
int force, struct page **pages, struct vm_area_struct **vmas)
19871987
{
19881988
int flags = FOLL_TOUCH;
19891989

mm/mlock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ long __mlock_vma_pages_range(struct vm_area_struct *vma,
160160
{
161161
struct mm_struct *mm = vma->vm_mm;
162162
unsigned long addr = start;
163-
int nr_pages = (end - start) / PAGE_SIZE;
163+
unsigned long nr_pages = (end - start) / PAGE_SIZE;
164164
int gup_flags;
165165

166166
VM_BUG_ON(start & ~PAGE_MASK);
@@ -382,7 +382,7 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
382382
unsigned long end, nstart, nend;
383383
struct vm_area_struct *vma = NULL;
384384
int locked = 0;
385-
int ret = 0;
385+
long ret = 0;
386386

387387
VM_BUG_ON(start & ~PAGE_MASK);
388388
VM_BUG_ON(len != PAGE_ALIGN(len));

mm/nommu.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ unsigned int kobjsize(const void *objp)
140140
return PAGE_SIZE << compound_order(page);
141141
}
142142

143-
int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
144-
unsigned long start, int nr_pages, unsigned int foll_flags,
145-
struct page **pages, struct vm_area_struct **vmas,
146-
int *retry)
143+
long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
144+
unsigned long start, unsigned long nr_pages,
145+
unsigned int foll_flags, struct page **pages,
146+
struct vm_area_struct **vmas, int *nonblocking)
147147
{
148148
struct vm_area_struct *vma;
149149
unsigned long vm_flags;
@@ -190,9 +190,10 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
190190
* slab page or a secondary page from a compound page
191191
* - don't permit access to VMAs that don't support it, such as I/O mappings
192192
*/
193-
int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
194-
unsigned long start, int nr_pages, int write, int force,
195-
struct page **pages, struct vm_area_struct **vmas)
193+
long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
194+
unsigned long start, unsigned long nr_pages,
195+
int write, int force, struct page **pages,
196+
struct vm_area_struct **vmas)
196197
{
197198
int flags = 0;
198199

0 commit comments

Comments
 (0)