Skip to content

Commit f313c51

Browse files
committed
execve: expand new process stack manually ahead of time
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]>
1 parent f440fa1 commit f313c51

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
@@ -200,34 +200,39 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
200200
int write)
201201
{
202202
struct page *page;
203+
struct vm_area_struct *vma = bprm->vma;
204+
struct mm_struct *mm = bprm->mm;
203205
int ret;
204-
unsigned int gup_flags = 0;
205206

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

218223
/*
219224
* We are doing an exec(). 'current' is the process
220-
* doing the exec and bprm->mm is the new process's mm.
225+
* doing the exec and 'mm' is the new process's mm.
221226
*/
222-
mmap_read_lock(bprm->mm);
223-
ret = get_user_pages_remote(bprm->mm, pos, 1, gup_flags,
227+
ret = get_user_pages_remote(mm, pos, 1,
228+
write ? FOLL_WRITE : 0,
224229
&page, NULL, NULL);
225-
mmap_read_unlock(bprm->mm);
230+
mmap_read_unlock(mm);
226231
if (ret <= 0)
227232
return NULL;
228233

229234
if (write)
230-
acct_arg_size(bprm, vma_pages(bprm->vma));
235+
acct_arg_size(bprm, vma_pages(vma));
231236

232237
return page;
233238
}

0 commit comments

Comments
 (0)