Skip to content

Commit 9beae1e

Browse files
ljskerneltorvalds
authored andcommitted
mm: replace get_user_pages_remote() write/force parameters with gup_flags
This removes the 'write' and 'force' from get_user_pages_remote() and replaces them with 'gup_flags' to make the use of FOLL_FORCE explicit in callers as use of this flag can result in surprising behaviour (and hence bugs) within the mm subsystem. Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com> Acked-by: Michal Hocko <mhocko@suse.com> Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 768ae30 commit 9beae1e

9 files changed

Lines changed: 40 additions & 27 deletions

File tree

‎drivers/gpu/drm/etnaviv/etnaviv_gem.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,19 +748,22 @@ static struct page **etnaviv_gem_userptr_do_get_pages(
748748
int ret = 0, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT;
749749
struct page **pvec;
750750
uintptr_t ptr;
751+
unsigned int flags = 0;
751752

752753
pvec = drm_malloc_ab(npages, sizeof(struct page *));
753754
if (!pvec)
754755
return ERR_PTR(-ENOMEM);
755756

757+
if (!etnaviv_obj->userptr.ro)
758+
flags |= FOLL_WRITE;
759+
756760
pinned = 0;
757761
ptr = etnaviv_obj->userptr.ptr;
758762

759763
down_read(&mm->mmap_sem);
760764
while (pinned < npages) {
761765
ret = get_user_pages_remote(task, mm, ptr, npages - pinned,
762-
!etnaviv_obj->userptr.ro, 0,
763-
pvec + pinned, NULL);
766+
flags, pvec + pinned, NULL);
764767
if (ret < 0)
765768
break;
766769

‎drivers/gpu/drm/i915/i915_gem_userptr.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
508508
pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY);
509509
if (pvec != NULL) {
510510
struct mm_struct *mm = obj->userptr.mm->mm;
511+
unsigned int flags = 0;
512+
513+
if (!obj->userptr.read_only)
514+
flags |= FOLL_WRITE;
511515

512516
ret = -EFAULT;
513517
if (atomic_inc_not_zero(&mm->mm_users)) {
@@ -517,7 +521,7 @@ __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
517521
(work->task, mm,
518522
obj->userptr.ptr + pinned * PAGE_SIZE,
519523
npages - pinned,
520-
!obj->userptr.read_only, 0,
524+
flags,
521525
pvec + pinned, NULL);
522526
if (ret < 0)
523527
break;

‎drivers/infiniband/core/umem_odp.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
527527
u64 off;
528528
int j, k, ret = 0, start_idx, npages = 0;
529529
u64 base_virt_addr;
530+
unsigned int flags = 0;
530531

531532
if (access_mask == 0)
532533
return -EINVAL;
@@ -556,6 +557,9 @@ int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
556557
goto out_put_task;
557558
}
558559

560+
if (access_mask & ODP_WRITE_ALLOWED_BIT)
561+
flags |= FOLL_WRITE;
562+
559563
start_idx = (user_virt - ib_umem_start(umem)) >> PAGE_SHIFT;
560564
k = start_idx;
561565

@@ -574,8 +578,7 @@ int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt,
574578
*/
575579
npages = get_user_pages_remote(owning_process, owning_mm,
576580
user_virt, gup_num_pages,
577-
access_mask & ODP_WRITE_ALLOWED_BIT,
578-
0, local_page_list, NULL);
581+
flags, local_page_list, NULL);
579582
up_read(&owning_mm->mmap_sem);
580583

581584
if (npages < 0)

‎fs/exec.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
191191
{
192192
struct page *page;
193193
int ret;
194+
unsigned int gup_flags = FOLL_FORCE;
194195

195196
#ifdef CONFIG_STACK_GROWSUP
196197
if (write) {
@@ -199,12 +200,16 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
199200
return NULL;
200201
}
201202
#endif
203+
204+
if (write)
205+
gup_flags |= FOLL_WRITE;
206+
202207
/*
203208
* We are doing an exec(). 'current' is the process
204209
* doing the exec and bprm->mm is the new process's mm.
205210
*/
206-
ret = get_user_pages_remote(current, bprm->mm, pos, 1, write,
207-
1, &page, NULL);
211+
ret = get_user_pages_remote(current, bprm->mm, pos, 1, gup_flags,
212+
&page, NULL);
208213
if (ret <= 0)
209214
return NULL;
210215

‎include/linux/mm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
12761276
struct vm_area_struct **vmas, int *nonblocking);
12771277
long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
12781278
unsigned long start, unsigned long nr_pages,
1279-
int write, int force, struct page **pages,
1279+
unsigned int gup_flags, struct page **pages,
12801280
struct vm_area_struct **vmas);
12811281
long get_user_pages(unsigned long start, unsigned long nr_pages,
12821282
unsigned int gup_flags, struct page **pages,

‎kernel/events/uprobes.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr,
300300

301301
retry:
302302
/* Read the page with vaddr into memory */
303-
ret = get_user_pages_remote(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
303+
ret = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &old_page,
304+
&vma);
304305
if (ret <= 0)
305306
return ret;
306307

@@ -1710,7 +1711,8 @@ static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
17101711
* but we treat this as a 'remote' access since it is
17111712
* essentially a kernel access to the memory.
17121713
*/
1713-
result = get_user_pages_remote(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1714+
result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page,
1715+
NULL);
17141716
if (result < 0)
17151717
return result;
17161718

‎mm/gup.c‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,7 @@ EXPORT_SYMBOL(get_user_pages_unlocked);
915915
* @mm: mm_struct of target mm
916916
* @start: starting user address
917917
* @nr_pages: number of pages from start to pin
918-
* @write: whether pages will be written to by the caller
919-
* @force: whether to force access even when user mapping is currently
920-
* protected (but never forces write access to shared mapping).
918+
* @gup_flags: flags modifying lookup behaviour
921919
* @pages: array that receives pointers to the pages pinned.
922920
* Should be at least nr_pages long. Or NULL, if caller
923921
* only intends to ensure the pages are faulted in.
@@ -946,9 +944,9 @@ EXPORT_SYMBOL(get_user_pages_unlocked);
946944
* or similar operation cannot guarantee anything stronger anyway because
947945
* locks can't be held over the syscall boundary.
948946
*
949-
* If write=0, the page must not be written to. If the page is written to,
950-
* set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
951-
* after the page is finished with, and before put_page is called.
947+
* If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page
948+
* is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must
949+
* be called after the page is finished with, and before put_page is called.
952950
*
953951
* get_user_pages is typically used for fewer-copy IO operations, to get a
954952
* handle on the memory by some means other than accesses via the user virtual
@@ -965,18 +963,12 @@ EXPORT_SYMBOL(get_user_pages_unlocked);
965963
*/
966964
long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
967965
unsigned long start, unsigned long nr_pages,
968-
int write, int force, struct page **pages,
966+
unsigned int gup_flags, struct page **pages,
969967
struct vm_area_struct **vmas)
970968
{
971-
unsigned int flags = FOLL_TOUCH | FOLL_REMOTE;
972-
973-
if (write)
974-
flags |= FOLL_WRITE;
975-
if (force)
976-
flags |= FOLL_FORCE;
977-
978969
return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
979-
NULL, false, flags);
970+
NULL, false,
971+
gup_flags | FOLL_TOUCH | FOLL_REMOTE);
980972
}
981973
EXPORT_SYMBOL(get_user_pages_remote);
982974

‎mm/memory.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3873,6 +3873,10 @@ static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
38733873
{
38743874
struct vm_area_struct *vma;
38753875
void *old_buf = buf;
3876+
unsigned int flags = FOLL_FORCE;
3877+
3878+
if (write)
3879+
flags |= FOLL_WRITE;
38763880

38773881
down_read(&mm->mmap_sem);
38783882
/* ignore errors, just check how much was successfully transferred */
@@ -3882,7 +3886,7 @@ static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
38823886
struct page *page = NULL;
38833887

38843888
ret = get_user_pages_remote(tsk, mm, addr, 1,
3885-
write, 1, &page, &vma);
3889+
flags, &page, &vma);
38863890
if (ret <= 0) {
38873891
#ifndef CONFIG_HAVE_IOREMAP_PROT
38883892
break;

‎security/tomoyo/domain.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
881881
* the execve().
882882
*/
883883
if (get_user_pages_remote(current, bprm->mm, pos, 1,
884-
0, 1, &page, NULL) <= 0)
884+
FOLL_FORCE, &page, NULL) <= 0)
885885
return false;
886886
#else
887887
page = bprm->page[pos / PAGE_SIZE];

0 commit comments

Comments
 (0)