Skip to content

Commit c4b31d1

Browse files
torvaldsgregkh
authored andcommitted
execve: expand new process stack manually ahead of time
commit f313c51 upstream. This is a small step towards a model where GUP itself would not expand the stack, and any user that needs GUP to not look up existing mappings, but actually expand on them, would have to do so manually before-hand, and with the mm lock held for writing. It turns out that execve() already did almost exactly that, except it didn't take the mm lock at all (it's single-threaded so no locking technically needed, but it could cause lockdep errors). And it only did it for the CONFIG_STACK_GROWSUP case, since in that case GUP has obviously never expanded the stack downwards. So just make that CONFIG_STACK_GROWSUP case do the right thing with locking, and enable it generally. This will eventually help GUP, and in the meantime avoids a special case and the lockdep issue. Signed-off-by: Linus Torvalds <[email protected]> [6.1 Minor context from still having FOLL_FORCE flags set] Signed-off-by: Samuel Mendoza-Jonas <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6a6b561 commit c4b31d1

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

fs/exec.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,39 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
198198
int write)
199199
{
200200
struct page *page;
201+
struct vm_area_struct *vma = bprm->vma;
202+
struct mm_struct *mm = bprm->mm;
201203
int ret;
202-
unsigned int gup_flags = FOLL_FORCE;
203204

204-
#ifdef CONFIG_STACK_GROWSUP
205-
if (write) {
206-
/* We claim to hold the lock - nobody to race with */
207-
ret = expand_downwards(bprm->vma, pos, true);
208-
if (ret < 0)
205+
/*
206+
* Avoid relying on expanding the stack down in GUP (which
207+
* does not work for STACK_GROWSUP anyway), and just do it
208+
* by hand ahead of time.
209+
*/
210+
if (write && pos < vma->vm_start) {
211+
mmap_write_lock(mm);
212+
ret = expand_downwards(vma, pos, true);
213+
if (unlikely(ret < 0)) {
214+
mmap_write_unlock(mm);
209215
return NULL;
210-
}
211-
#endif
212-
213-
if (write)
214-
gup_flags |= FOLL_WRITE;
216+
}
217+
mmap_write_downgrade(mm);
218+
} else
219+
mmap_read_lock(mm);
215220

216221
/*
217222
* We are doing an exec(). 'current' is the process
218-
* doing the exec and bprm->mm is the new process's mm.
223+
* doing the exec and 'mm' is the new process's mm.
219224
*/
220-
mmap_read_lock(bprm->mm);
221-
ret = get_user_pages_remote(bprm->mm, pos, 1, gup_flags,
225+
ret = get_user_pages_remote(mm, pos, 1,
226+
write ? FOLL_WRITE : 0,
222227
&page, NULL, NULL);
223-
mmap_read_unlock(bprm->mm);
228+
mmap_read_unlock(mm);
224229
if (ret <= 0)
225230
return NULL;
226231

227232
if (write)
228-
acct_arg_size(bprm, vma_pages(bprm->vma));
233+
acct_arg_size(bprm, vma_pages(vma));
229234

230235
return page;
231236
}

0 commit comments

Comments
 (0)