alistair23-linux/tools/testing/selftests/vm/mlock2-tests.c
Greg Kroah-Hartman b24413180f License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.

By default all files without license information are under the default
license of the kernel, which is GPL version 2.

Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier.  The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.

This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.

How this work was done:

Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
 - file had no licensing information it it.
 - file was a */uapi/* one with no licensing information in it,
 - file was a */uapi/* one with existing licensing information,

Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.

The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne.  Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.

The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed.  Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.

Criteria used to select files for SPDX license identifier tagging was:
 - Files considered eligible had to be source code files.
 - Make and config files were included as candidates if they contained >5
   lines of source
 - File already had some variant of a license header in it (even if <5
   lines).

All documentation files were explicitly excluded.

The following heuristics were used to determine which SPDX license
identifiers to apply.

 - when both scanners couldn't find any license traces, file was
   considered to have no license information in it, and the top level
   COPYING file license applied.

   For non */uapi/* files that summary was:

   SPDX license identifier                            # files
   ---------------------------------------------------|-------
   GPL-2.0                                              11139

   and resulted in the first patch in this series.

   If that file was a */uapi/* path one, it was "GPL-2.0 WITH
   Linux-syscall-note" otherwise it was "GPL-2.0".  Results of that was:

   SPDX license identifier                            # files
   ---------------------------------------------------|-------
   GPL-2.0 WITH Linux-syscall-note                        930

   and resulted in the second patch in this series.

 - if a file had some form of licensing information in it, and was one
   of the */uapi/* ones, it was denoted with the Linux-syscall-note if
   any GPL family license was found in the file or had no licensing in
   it (per prior point).  Results summary:

   SPDX license identifier                            # files
   ---------------------------------------------------|------
   GPL-2.0 WITH Linux-syscall-note                       270
   GPL-2.0+ WITH Linux-syscall-note                      169
   ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause)    21
   ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)    17
   LGPL-2.1+ WITH Linux-syscall-note                      15
   GPL-1.0+ WITH Linux-syscall-note                       14
   ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause)    5
   LGPL-2.0+ WITH Linux-syscall-note                       4
   LGPL-2.1 WITH Linux-syscall-note                        3
   ((GPL-2.0 WITH Linux-syscall-note) OR MIT)              3
   ((GPL-2.0 WITH Linux-syscall-note) AND MIT)             1

   and that resulted in the third patch in this series.

 - when the two scanners agreed on the detected license(s), that became
   the concluded license(s).

 - when there was disagreement between the two scanners (one detected a
   license but the other didn't, or they both detected different
   licenses) a manual inspection of the file occurred.

 - In most cases a manual inspection of the information in the file
   resulted in a clear resolution of the license that should apply (and
   which scanner probably needed to revisit its heuristics).

 - When it was not immediately clear, the license identifier was
   confirmed with lawyers working with the Linux Foundation.

 - If there was any question as to the appropriate license identifier,
   the file was flagged for further research and to be revisited later
   in time.

In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.

Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights.  The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.

Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.

In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.

Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
 - a full scancode scan run, collecting the matched texts, detected
   license ids and scores
 - reviewing anything where there was a license detected (about 500+
   files) to ensure that the applied SPDX license was correct
 - reviewing anything where there was no detection but the patch license
   was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
   SPDX license was correct

This produced a worksheet with 20 files needing minor correction.  This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.

These .csv files were then reviewed by Greg.  Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected.  This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.)  Finally Greg ran the script using the .csv files to
generate the patches.

Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-02 11:10:55 +01:00

678 lines
14 KiB
C

// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdbool.h>
#include "mlock2.h"
struct vm_boundaries {
unsigned long start;
unsigned long end;
};
static int get_vm_area(unsigned long addr, struct vm_boundaries *area)
{
FILE *file;
int ret = 1;
char line[1024] = {0};
char *end_addr;
char *stop;
unsigned long start;
unsigned long end;
if (!area)
return ret;
file = fopen("/proc/self/maps", "r");
if (!file) {
perror("fopen");
return ret;
}
memset(area, 0, sizeof(struct vm_boundaries));
while(fgets(line, 1024, file)) {
end_addr = strchr(line, '-');
if (!end_addr) {
printf("cannot parse /proc/self/maps\n");
goto out;
}
*end_addr = '\0';
end_addr++;
stop = strchr(end_addr, ' ');
if (!stop) {
printf("cannot parse /proc/self/maps\n");
goto out;
}
stop = '\0';
sscanf(line, "%lx", &start);
sscanf(end_addr, "%lx", &end);
if (start <= addr && end > addr) {
area->start = start;
area->end = end;
ret = 0;
goto out;
}
}
out:
fclose(file);
return ret;
}
static uint64_t get_pageflags(unsigned long addr)
{
FILE *file;
uint64_t pfn;
unsigned long offset;
file = fopen("/proc/self/pagemap", "r");
if (!file) {
perror("fopen pagemap");
_exit(1);
}
offset = addr / getpagesize() * sizeof(pfn);
if (fseek(file, offset, SEEK_SET)) {
perror("fseek pagemap");
_exit(1);
}
if (fread(&pfn, sizeof(pfn), 1, file) != 1) {
perror("fread pagemap");
_exit(1);
}
fclose(file);
return pfn;
}
static uint64_t get_kpageflags(unsigned long pfn)
{
uint64_t flags;
FILE *file;
file = fopen("/proc/kpageflags", "r");
if (!file) {
perror("fopen kpageflags");
_exit(1);
}
if (fseek(file, pfn * sizeof(flags), SEEK_SET)) {
perror("fseek kpageflags");
_exit(1);
}
if (fread(&flags, sizeof(flags), 1, file) != 1) {
perror("fread kpageflags");
_exit(1);
}
fclose(file);
return flags;
}
#define VMFLAGS "VmFlags:"
static bool is_vmflag_set(unsigned long addr, const char *vmflag)
{
char *line = NULL;
char *flags;
size_t size = 0;
bool ret = false;
FILE *smaps;
smaps = seek_to_smaps_entry(addr);
if (!smaps) {
printf("Unable to parse /proc/self/smaps\n");
goto out;
}
while (getline(&line, &size, smaps) > 0) {
if (!strstr(line, VMFLAGS)) {
free(line);
line = NULL;
size = 0;
continue;
}
flags = line + strlen(VMFLAGS);
ret = (strstr(flags, vmflag) != NULL);
goto out;
}
out:
free(line);
fclose(smaps);
return ret;
}
#define SIZE "Size:"
#define RSS "Rss:"
#define LOCKED "lo"
static bool is_vma_lock_on_fault(unsigned long addr)
{
bool ret = false;
bool locked;
FILE *smaps = NULL;
unsigned long vma_size, vma_rss;
char *line = NULL;
char *value;
size_t size = 0;
locked = is_vmflag_set(addr, LOCKED);
if (!locked)
goto out;
smaps = seek_to_smaps_entry(addr);
if (!smaps) {
printf("Unable to parse /proc/self/smaps\n");
goto out;
}
while (getline(&line, &size, smaps) > 0) {
if (!strstr(line, SIZE)) {
free(line);
line = NULL;
size = 0;
continue;
}
value = line + strlen(SIZE);
if (sscanf(value, "%lu kB", &vma_size) < 1) {
printf("Unable to parse smaps entry for Size\n");
goto out;
}
break;
}
while (getline(&line, &size, smaps) > 0) {
if (!strstr(line, RSS)) {
free(line);
line = NULL;
size = 0;
continue;
}
value = line + strlen(RSS);
if (sscanf(value, "%lu kB", &vma_rss) < 1) {
printf("Unable to parse smaps entry for Rss\n");
goto out;
}
break;
}
ret = locked && (vma_rss < vma_size);
out:
free(line);
if (smaps)
fclose(smaps);
return ret;
}
#define PRESENT_BIT 0x8000000000000000ULL
#define PFN_MASK 0x007FFFFFFFFFFFFFULL
#define UNEVICTABLE_BIT (1UL << 18)
static int lock_check(char *map)
{
unsigned long page_size = getpagesize();
uint64_t page1_flags, page2_flags;
page1_flags = get_pageflags((unsigned long)map);
page2_flags = get_pageflags((unsigned long)map + page_size);
/* Both pages should be present */
if (((page1_flags & PRESENT_BIT) == 0) ||
((page2_flags & PRESENT_BIT) == 0)) {
printf("Failed to make both pages present\n");
return 1;
}
page1_flags = get_kpageflags(page1_flags & PFN_MASK);
page2_flags = get_kpageflags(page2_flags & PFN_MASK);
/* Both pages should be unevictable */
if (((page1_flags & UNEVICTABLE_BIT) == 0) ||
((page2_flags & UNEVICTABLE_BIT) == 0)) {
printf("Failed to make both pages unevictable\n");
return 1;
}
if (!is_vmflag_set((unsigned long)map, LOCKED)) {
printf("VMA flag %s is missing on page 1\n", LOCKED);
return 1;
}
if (!is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
printf("VMA flag %s is missing on page 2\n", LOCKED);
return 1;
}
return 0;
}
static int unlock_lock_check(char *map)
{
unsigned long page_size = getpagesize();
uint64_t page1_flags, page2_flags;
page1_flags = get_pageflags((unsigned long)map);
page2_flags = get_pageflags((unsigned long)map + page_size);
page1_flags = get_kpageflags(page1_flags & PFN_MASK);
page2_flags = get_kpageflags(page2_flags & PFN_MASK);
if ((page1_flags & UNEVICTABLE_BIT) || (page2_flags & UNEVICTABLE_BIT)) {
printf("A page is still marked unevictable after unlock\n");
return 1;
}
if (is_vmflag_set((unsigned long)map, LOCKED)) {
printf("VMA flag %s is present on page 1 after unlock\n", LOCKED);
return 1;
}
if (is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
printf("VMA flag %s is present on page 2 after unlock\n", LOCKED);
return 1;
}
return 0;
}
static int test_mlock_lock()
{
char *map;
int ret = 1;
unsigned long page_size = getpagesize();
map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("test_mlock_locked mmap");
goto out;
}
if (mlock2_(map, 2 * page_size, 0)) {
if (errno == ENOSYS) {
printf("Cannot call new mlock family, skipping test\n");
_exit(0);
}
perror("mlock2(0)");
goto unmap;
}
if (lock_check(map))
goto unmap;
/* Now unlock and recheck attributes */
if (munlock(map, 2 * page_size)) {
perror("munlock()");
goto unmap;
}
ret = unlock_lock_check(map);
unmap:
munmap(map, 2 * page_size);
out:
return ret;
}
static int onfault_check(char *map)
{
unsigned long page_size = getpagesize();
uint64_t page1_flags, page2_flags;
page1_flags = get_pageflags((unsigned long)map);
page2_flags = get_pageflags((unsigned long)map + page_size);
/* Neither page should be present */
if ((page1_flags & PRESENT_BIT) || (page2_flags & PRESENT_BIT)) {
printf("Pages were made present by MLOCK_ONFAULT\n");
return 1;
}
*map = 'a';
page1_flags = get_pageflags((unsigned long)map);
page2_flags = get_pageflags((unsigned long)map + page_size);
/* Only page 1 should be present */
if ((page1_flags & PRESENT_BIT) == 0) {
printf("Page 1 is not present after fault\n");
return 1;
} else if (page2_flags & PRESENT_BIT) {
printf("Page 2 was made present\n");
return 1;
}
page1_flags = get_kpageflags(page1_flags & PFN_MASK);
/* Page 1 should be unevictable */
if ((page1_flags & UNEVICTABLE_BIT) == 0) {
printf("Failed to make faulted page unevictable\n");
return 1;
}
if (!is_vma_lock_on_fault((unsigned long)map)) {
printf("VMA is not marked for lock on fault\n");
return 1;
}
if (!is_vma_lock_on_fault((unsigned long)map + page_size)) {
printf("VMA is not marked for lock on fault\n");
return 1;
}
return 0;
}
static int unlock_onfault_check(char *map)
{
unsigned long page_size = getpagesize();
uint64_t page1_flags;
page1_flags = get_pageflags((unsigned long)map);
page1_flags = get_kpageflags(page1_flags & PFN_MASK);
if (page1_flags & UNEVICTABLE_BIT) {
printf("Page 1 is still marked unevictable after unlock\n");
return 1;
}
if (is_vma_lock_on_fault((unsigned long)map) ||
is_vma_lock_on_fault((unsigned long)map + page_size)) {
printf("VMA is still lock on fault after unlock\n");
return 1;
}
return 0;
}
static int test_mlock_onfault()
{
char *map;
int ret = 1;
unsigned long page_size = getpagesize();
map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("test_mlock_locked mmap");
goto out;
}
if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
if (errno == ENOSYS) {
printf("Cannot call new mlock family, skipping test\n");
_exit(0);
}
perror("mlock2(MLOCK_ONFAULT)");
goto unmap;
}
if (onfault_check(map))
goto unmap;
/* Now unlock and recheck attributes */
if (munlock(map, 2 * page_size)) {
if (errno == ENOSYS) {
printf("Cannot call new mlock family, skipping test\n");
_exit(0);
}
perror("munlock()");
goto unmap;
}
ret = unlock_onfault_check(map);
unmap:
munmap(map, 2 * page_size);
out:
return ret;
}
static int test_lock_onfault_of_present()
{
char *map;
int ret = 1;
unsigned long page_size = getpagesize();
uint64_t page1_flags, page2_flags;
map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("test_mlock_locked mmap");
goto out;
}
*map = 'a';
if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
if (errno == ENOSYS) {
printf("Cannot call new mlock family, skipping test\n");
_exit(0);
}
perror("mlock2(MLOCK_ONFAULT)");
goto unmap;
}
page1_flags = get_pageflags((unsigned long)map);
page2_flags = get_pageflags((unsigned long)map + page_size);
page1_flags = get_kpageflags(page1_flags & PFN_MASK);
page2_flags = get_kpageflags(page2_flags & PFN_MASK);
/* Page 1 should be unevictable */
if ((page1_flags & UNEVICTABLE_BIT) == 0) {
printf("Failed to make present page unevictable\n");
goto unmap;
}
if (!is_vma_lock_on_fault((unsigned long)map) ||
!is_vma_lock_on_fault((unsigned long)map + page_size)) {
printf("VMA with present pages is not marked lock on fault\n");
goto unmap;
}
ret = 0;
unmap:
munmap(map, 2 * page_size);
out:
return ret;
}
static int test_munlockall()
{
char *map;
int ret = 1;
unsigned long page_size = getpagesize();
map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("test_munlockall mmap");
goto out;
}
if (mlockall(MCL_CURRENT)) {
perror("mlockall(MCL_CURRENT)");
goto out;
}
if (lock_check(map))
goto unmap;
if (munlockall()) {
perror("munlockall()");
goto unmap;
}
if (unlock_lock_check(map))
goto unmap;
munmap(map, 2 * page_size);
map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("test_munlockall second mmap");
goto out;
}
if (mlockall(MCL_CURRENT | MCL_ONFAULT)) {
perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
goto unmap;
}
if (onfault_check(map))
goto unmap;
if (munlockall()) {
perror("munlockall()");
goto unmap;
}
if (unlock_onfault_check(map))
goto unmap;
if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
goto out;
}
if (lock_check(map))
goto unmap;
if (munlockall()) {
perror("munlockall()");
goto unmap;
}
ret = unlock_lock_check(map);
unmap:
munmap(map, 2 * page_size);
out:
munlockall();
return ret;
}
static int test_vma_management(bool call_mlock)
{
int ret = 1;
void *map;
unsigned long page_size = getpagesize();
struct vm_boundaries page1;
struct vm_boundaries page2;
struct vm_boundaries page3;
map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (map == MAP_FAILED) {
perror("mmap()");
return ret;
}
if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) {
if (errno == ENOSYS) {
printf("Cannot call new mlock family, skipping test\n");
_exit(0);
}
perror("mlock(ONFAULT)\n");
goto out;
}
if (get_vm_area((unsigned long)map, &page1) ||
get_vm_area((unsigned long)map + page_size, &page2) ||
get_vm_area((unsigned long)map + page_size * 2, &page3)) {
printf("couldn't find mapping in /proc/self/maps\n");
goto out;
}
/*
* Before we unlock a portion, we need to that all three pages are in
* the same VMA. If they are not we abort this test (Note that this is
* not a failure)
*/
if (page1.start != page2.start || page2.start != page3.start) {
printf("VMAs are not merged to start, aborting test\n");
ret = 0;
goto out;
}
if (munlock(map + page_size, page_size)) {
perror("munlock()");
goto out;
}
if (get_vm_area((unsigned long)map, &page1) ||
get_vm_area((unsigned long)map + page_size, &page2) ||
get_vm_area((unsigned long)map + page_size * 2, &page3)) {
printf("couldn't find mapping in /proc/self/maps\n");
goto out;
}
/* All three VMAs should be different */
if (page1.start == page2.start || page2.start == page3.start) {
printf("failed to split VMA for munlock\n");
goto out;
}
/* Now unlock the first and third page and check the VMAs again */
if (munlock(map, page_size * 3)) {
perror("munlock()");
goto out;
}
if (get_vm_area((unsigned long)map, &page1) ||
get_vm_area((unsigned long)map + page_size, &page2) ||
get_vm_area((unsigned long)map + page_size * 2, &page3)) {
printf("couldn't find mapping in /proc/self/maps\n");
goto out;
}
/* Now all three VMAs should be the same */
if (page1.start != page2.start || page2.start != page3.start) {
printf("failed to merge VMAs after munlock\n");
goto out;
}
ret = 0;
out:
munmap(map, 3 * page_size);
return ret;
}
static int test_mlockall(int (test_function)(bool call_mlock))
{
int ret = 1;
if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) {
perror("mlockall");
return ret;
}
ret = test_function(false);
munlockall();
return ret;
}
int main(int argc, char **argv)
{
int ret = 0;
ret += test_mlock_lock();
ret += test_mlock_onfault();
ret += test_munlockall();
ret += test_lock_onfault_of_present();
ret += test_vma_management(true);
ret += test_mlockall(test_vma_management);
return ret;
}