mm, hugetlb: add VM_NORESERVE check in vma_has_reserves()
mm, hugetlb: add VM_NORESERVE check in vma_has_reserves()

If we map the region with MAP_NORESERVE and MAP_SHARED, we can skip to
check reserve counting and eventually we cannot be ensured to allocate a
huge page in fault time.  With following example code, you can easily find
this situation.

Assume 2MB, nr_hugepages = 100

        fd = hugetlbfs_unlinked_fd();
        if (fd < 0)
                return 1;

        size = 200 * MB;
        flag = MAP_SHARED;
        p = mmap(NULL, size, PROT_READ|PROT_WRITE, flag, fd, 0);
        if (p == MAP_FAILED) {
                fprintf(stderr, "mmap() failed: %s\n", strerror(errno));
                return -1;
        }

        size = 2 * MB;
        flag = MAP_ANONYMOUS | MAP_SHARED | MAP_HUGETLB | MAP_NORESERVE;
        p = mmap(NULL, size, PROT_READ|PROT_WRITE, flag, -1, 0);
        if (p == MAP_FAILED) {
                fprintf(stderr, "mmap() failed: %s\n", strerror(errno));
        }
        p[0] = '0';
        sleep(10);

During executing sleep(10), run 'cat /proc/meminfo' on another process.

HugePages_Free:       99
HugePages_Rsvd:      100

Number of free should be higher or equal than number of reserve, but this
aren't.  This represent that non reserved shared mapping steal a reserved
page.  Non reserved shared mapping should not eat into reserve space.

If we consider VM_NORESERVE in vma_has_reserve() and return 0 which mean
that we don't have reserved pages, then we check that we have enough free
pages in dequeue_huge_page_vma().  This prevent to steal a reserved page.

With this change, above test generate a SIGBUG which is correct, because
all free pages are reserved and non reserved shared mapping can't get a
free page.

Signed-off-by: Joonsoo Kim <bb6c8cfe7699e0f11d1bee88022eb563a5f8e881@lge.com>
Reviewed-by: Wanpeng Li <86d530c4a0b810dc887df1e4e084ed44c9b7acac@linux.vnet.ibm.com>
Reviewed-by: Aneesh Kumar K.V <f511f818e8a0748edef6b9ee378967da9ed0c78f@linux.vnet.ibm.com>
Acked-by: Hillf Danton <74636c4e14be88f42bc2a57784d7913ef47ac9b0@gmail.com>
Acked-by: Michal Hocko <829500b200af6c2a0eca4dfd5c388e6b8fbe8dc6@suse.cz>
Cc: Naoya Horiguchi <8e7a128cd0264d3269a891fb3e87507c2985f8af@ah.jp.nec.com>
Cc: Rik van Riel <a21938f5d463ddf41aa718934c205ca2cce8ebbc@redhat.com>
Cc: Mel Gorman <39ea6a09710987173e32e37c86918024a0f441c1@suse.de>
Cc: "Aneesh Kumar K.V" <f511f818e8a0748edef6b9ee378967da9ed0c78f@linux.vnet.ibm.com>
Cc: KAMEZAWA Hiroyuki <634f508bd7c47cf0ee4126243675c3e598920fbc@jp.fujitsu.com>
Cc: Hugh Dickins <d3abdf3e2800e6cb849f11b81e6eb50b34d96431@google.com>
Cc: Davidlohr Bueso <d7ca2ed3e31360a26e5e60317ed1b792bd41becb@hp.com>
Cc: David Gibson <aa743a0aaec8f7d7a1f01442503957f4d7a2d634@gibson.dropbear.id.au>
Signed-off-by: Andrew Morton <5c1e68b099950c134891f0b6e179498a8ebe9cf9@linux-foundation.org>
Signed-off-by: Linus Torvalds <69652caca27c8b940640ad396ab71f93cacec34f@linux-foundation.org>

KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs