Skip to content

Commit 9978ad5

Browse files
Lee Schermerhorntorvalds
authored andcommitted
mlock: make mlock error return Posixly Correct
Rework Posix error return for mlock(). Posix requires error code for mlock*() system calls for some conditions that differ from what kernel low level functions, such as get_user_pages(), return for those conditions. For more info, see: http://marc.info/?l=linux-kernel&m=121750892930775&w=2 This patch provides the same translation of get_user_pages() error codes to posix specified error codes in the context of the mlock rework for unevictable lru. [[email protected]: fix build] Signed-off-by: KOSAKI Motohiro <[email protected]> Signed-off-by: Lee Schermerhorn <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c11d69d commit 9978ad5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

mm/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,7 @@ int make_pages_present(unsigned long addr, unsigned long end)
28212821
len, write, 0, NULL, NULL);
28222822
if (ret < 0)
28232823
return ret;
2824-
return ret == len ? 0 : -1;
2824+
return ret == len ? 0 : -EFAULT;
28252825
}
28262826

28272827
#if !defined(__HAVE_ARCH_GATE_AREA)

mm/mlock.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,24 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,
248248
addr += PAGE_SIZE; /* for next get_user_pages() */
249249
nr_pages--;
250250
}
251+
ret = 0;
251252
}
252253

253254
lru_add_drain_all(); /* to update stats */
254255

255-
return 0; /* count entire vma as locked_vm */
256+
return ret; /* count entire vma as locked_vm */
257+
}
258+
259+
/*
260+
* convert get_user_pages() return value to posix mlock() error
261+
*/
262+
static int __mlock_posix_error_return(long retval)
263+
{
264+
if (retval == -EFAULT)
265+
retval = -ENOMEM;
266+
else if (retval == -ENOMEM)
267+
retval = -EAGAIN;
268+
return retval;
256269
}
257270

258271
#else /* CONFIG_UNEVICTABLE_LRU */
@@ -265,9 +278,15 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,
265278
int mlock)
266279
{
267280
if (mlock && (vma->vm_flags & VM_LOCKED))
268-
make_pages_present(start, end);
281+
return make_pages_present(start, end);
282+
return 0;
283+
}
284+
285+
static inline int __mlock_posix_error_return(long retval)
286+
{
269287
return 0;
270288
}
289+
271290
#endif /* CONFIG_UNEVICTABLE_LRU */
272291

273292
/**
@@ -434,10 +453,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
434453
downgrade_write(&mm->mmap_sem);
435454

436455
ret = __mlock_vma_pages_range(vma, start, end, 1);
437-
if (ret > 0) {
438-
mm->locked_vm -= ret;
439-
ret = 0;
440-
}
456+
441457
/*
442458
* Need to reacquire mmap sem in write mode, as our callers
443459
* expect this. We have no support for atomically upgrading
@@ -451,6 +467,11 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
451467
/* non-NULL *prev must contain @start, but need to check @end */
452468
if (!(*prev) || end > (*prev)->vm_end)
453469
ret = -ENOMEM;
470+
else if (ret > 0) {
471+
mm->locked_vm -= ret;
472+
ret = 0;
473+
} else
474+
ret = __mlock_posix_error_return(ret); /* translate if needed */
454475
} else {
455476
/*
456477
* TODO: for unlocking, pages will already be resident, so

0 commit comments

Comments
 (0)