Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm, selftests: test return value of munmap for MAP_HUGETLB memory

When MAP_HUGETLB memory is unmapped, the length must be hugepage aligned,
otherwise it fails with -EINVAL.

All tests currently behave correctly, but it's better to explcitly test
the return value for completeness and document the requirement, especially
if users copy map_hugetlb.c as a sample implementation.

Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Davide Libenzi <davidel@xmailserver.org>
Cc: Luiz Capitulino <lcapitulino@redhat.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Joern Engel <joern@logfs.org>
Cc: Jianguo Wu <wujianguo@huawei.com>
Cc: Eric B Munson <emunson@akamai.com>
Acked-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

David Rientjes and committed by
Linus Torvalds
215ba781 80d6b94b

+14 -4
+6 -2
tools/testing/selftests/powerpc/mm/hugetlb_vs_thp_test.c
··· 21 21 * Typically the mmap will fail because no huge pages are 22 22 * allocated on the system. But if there are huge pages 23 23 * allocated the mmap will succeed. That's fine too, we just 24 - * munmap here before continuing. 24 + * munmap here before continuing. munmap() length of 25 + * MAP_HUGETLB memory must be hugepage aligned. 25 26 */ 26 - munmap(addr, SIZE); 27 + if (munmap(addr, SIZE)) { 28 + perror("munmap"); 29 + return 1; 30 + } 27 31 } 28 32 29 33 p = mmap(addr, SIZE, PROT_READ | PROT_WRITE,
+3 -1
tools/testing/selftests/vm/hugetlbfstest.c
··· 34 34 int *p; 35 35 int flags = MAP_PRIVATE | MAP_POPULATE | extra_flags; 36 36 u64 before, after; 37 + int ret; 37 38 38 39 before = read_rss(); 39 40 p = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0); ··· 45 44 !"rss didn't grow as expected"); 46 45 if (!unmap) 47 46 return; 48 - munmap(p, length); 47 + ret = munmap(p, length); 48 + assert(!ret || !"munmap returned an unexpected error"); 49 49 after = read_rss(); 50 50 assert(llabs(after - before) < 0x40000 || 51 51 !"rss didn't shrink as expected");
+5 -1
tools/testing/selftests/vm/map_hugetlb.c
··· 73 73 write_bytes(addr); 74 74 ret = read_bytes(addr); 75 75 76 - munmap(addr, LENGTH); 76 + /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ 77 + if (munmap(addr, LENGTH)) { 78 + perror("munmap"); 79 + exit(1); 80 + } 77 81 78 82 return ret; 79 83 }