Skip to content

Commit 6347e8d

Browse files
ljskerneltorvalds
authored andcommitted
mm: replace access_remote_vm() write parameter with gup_flags
This removes the 'write' argument from access_remote_vm() and replaces it with 'gup_flags' as use of this function previously silently implied FOLL_FORCE, whereas after this patch callers explicitly pass this flag. We make this explicit as use of FOLL_FORCE 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> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 442486e commit 6347e8d

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
252252
* Inherently racy -- command line shares address space
253253
* with code and data.
254254
*/
255-
rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
255+
rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_FORCE);
256256
if (rv <= 0)
257257
goto out_free_page;
258258

@@ -270,7 +270,8 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
270270
int nr_read;
271271

272272
_count = min3(count, len, PAGE_SIZE);
273-
nr_read = access_remote_vm(mm, p, page, _count, 0);
273+
nr_read = access_remote_vm(mm, p, page, _count,
274+
FOLL_FORCE);
274275
if (nr_read < 0)
275276
rv = nr_read;
276277
if (nr_read <= 0)
@@ -305,7 +306,8 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
305306
bool final;
306307

307308
_count = min3(count, len, PAGE_SIZE);
308-
nr_read = access_remote_vm(mm, p, page, _count, 0);
309+
nr_read = access_remote_vm(mm, p, page, _count,
310+
FOLL_FORCE);
309311
if (nr_read < 0)
310312
rv = nr_read;
311313
if (nr_read <= 0)
@@ -354,7 +356,8 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
354356
bool final;
355357

356358
_count = min3(count, len, PAGE_SIZE);
357-
nr_read = access_remote_vm(mm, p, page, _count, 0);
359+
nr_read = access_remote_vm(mm, p, page, _count,
360+
FOLL_FORCE);
358361
if (nr_read < 0)
359362
rv = nr_read;
360363
if (nr_read <= 0)
@@ -832,6 +835,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
832835
unsigned long addr = *ppos;
833836
ssize_t copied;
834837
char *page;
838+
unsigned int flags = FOLL_FORCE;
835839

836840
if (!mm)
837841
return 0;
@@ -844,6 +848,9 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
844848
if (!atomic_inc_not_zero(&mm->mm_users))
845849
goto free;
846850

851+
if (write)
852+
flags |= FOLL_WRITE;
853+
847854
while (count > 0) {
848855
int this_len = min_t(int, count, PAGE_SIZE);
849856

@@ -852,7 +859,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
852859
break;
853860
}
854861

855-
this_len = access_remote_vm(mm, addr, page, this_len, write);
862+
this_len = access_remote_vm(mm, addr, page, this_len, flags);
856863
if (!this_len) {
857864
if (!copied)
858865
copied = -EIO;
@@ -965,7 +972,7 @@ static ssize_t environ_read(struct file *file, char __user *buf,
965972
this_len = min(max_len, this_len);
966973

967974
retval = access_remote_vm(mm, (env_start + src),
968-
page, this_len, 0);
975+
page, this_len, FOLL_FORCE);
969976

970977
if (retval <= 0) {
971978
ret = retval;

‎include/linux/mm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ static inline int fixup_user_fault(struct task_struct *tsk,
12681268

12691269
extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
12701270
extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1271-
void *buf, int len, int write);
1271+
void *buf, int len, unsigned int gup_flags);
12721272

12731273
long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
12741274
unsigned long start, unsigned long nr_pages,

‎mm/memory.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,19 +3935,14 @@ static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
39353935
* @addr: start address to access
39363936
* @buf: source or destination buffer
39373937
* @len: number of bytes to transfer
3938-
* @write: whether the access is a write
3938+
* @gup_flags: flags modifying lookup behaviour
39393939
*
39403940
* The caller must hold a reference on @mm.
39413941
*/
39423942
int access_remote_vm(struct mm_struct *mm, unsigned long addr,
3943-
void *buf, int len, int write)
3943+
void *buf, int len, unsigned int gup_flags)
39443944
{
3945-
unsigned int flags = FOLL_FORCE;
3946-
3947-
if (write)
3948-
flags |= FOLL_WRITE;
3949-
3950-
return __access_remote_vm(NULL, mm, addr, buf, len, flags);
3945+
return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
39513946
}
39523947

39533948
/*

‎mm/nommu.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,15 +1847,14 @@ static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
18471847
* @addr: start address to access
18481848
* @buf: source or destination buffer
18491849
* @len: number of bytes to transfer
1850-
* @write: whether the access is a write
1850+
* @gup_flags: flags modifying lookup behaviour
18511851
*
18521852
* The caller must hold a reference on @mm.
18531853
*/
18541854
int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1855-
void *buf, int len, int write)
1855+
void *buf, int len, unsigned int gup_flags)
18561856
{
1857-
return __access_remote_vm(NULL, mm, addr, buf, len,
1858-
write ? FOLL_WRITE : 0);
1857+
return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
18591858
}
18601859

18611860
/*

0 commit comments

Comments
 (0)