linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "C.Wehrmeyer" <c.wehrmeyer@gmx.de>
To: linux-mm@kvack.org
Subject: PROBLEM: Remapping hugepages mappings causes kernel to return EINVAL
Date: Sat, 7 Oct 2017 03:58:28 +0200	[thread overview]
Message-ID: <3ba05809-63a2-2969-e54f-fd0202fe336b@gmx.de> (raw)

This is my very first time reporting a Linux kernel problem. Despite my 
best efforts of doing homework I'm not yet used to the process.

Creating a private, anonymous mapping via mmap(2) which is supposed to 
use hugepages (test machine: x64 with 2-MiB pages) and later resizing 
that mapping via mremap(2) causes the kernel to return EINVAL. If the 
flags that create the hugepages mapping are omitted and thus allocating 
pages with the normal page size, mremap(2)succeeds.

The check to stop processing hugepages is in mm/mremap.c, function 
vma_to_resize:

if (is_vm_hugetlb_page(vma))
         return ERR_PTR(-EINVAL);

Furthermore, the code makes no further attempt to map page shifts and 
page sizes if a hugepage mapping has been detected. I have too little 
experience with programming in kernelland to provide a proper patch - a 
cobbler should stick to his last.

The problem was first encountered on a 4.8.10 kernel, but can still be 
encountered in the latest git snapshot from 3:30 AM, 2017-10-07 CEST.

Steps to reproduce: Enable at least two 2-MiB hugepages:
# echo 2 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
# cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages

Note that due to memory fragmentation the kernel might have problems 
allocating a big enough amount of memory. The second statement should 
print "2".

Then compile and run the following program:

#define USE_HUGEPAGES
#define _GNU_SOURCE

#include <errno.h> /*errno*/
#include <stdio.h> /*fprintf*/

#include <sys/mman.h> /*mmap*/
#include <asm/mman.h> /*hugepages flags*/

#define ALLOC_SIZE_1 (2 * 1024 * 1024)
#define ALLOC_SIZE_2 (2 * ALLOC_SIZE_1)

int main(void)
{
         int errno_tmp = 0;
         int prot  = PROT_READ | PROT_WRITE;

         const int flags_huge =
#ifdef USE_HUGEPAGES
         MAP_HUGETLB | MAP_HUGE_2MB
#else
         0
#endif
         ;
         const int flags = MAP_PRIVATE | MAP_ANONYMOUS | flags_huge;

         void *buf1;
         void *buf2;
         /*****/
         buf1 = mmap (
                 NULL,
                 ALLOC_SIZE_1,
                 prot,
                 flags,
                 -1,
                 0
         );

         if (MAP_FAILED == buf1) {
                 errno_tmp = errno;
                 fprintf(stderr,"mmap: %u\n",errno_tmp);
                 goto out;
         }
         /*****/
         buf2 = mremap (
                 buf1,
                 ALLOC_SIZE_1,
                 ALLOC_SIZE_2,
                 MREMAP_MAYMOVE
         );

         if (MAP_FAILED == buf2) {
                 errno_tmp = errno;
                 fprintf(stderr,"mremap: %u\n",errno_tmp);
                 munmap(buf1,ALLOC_SIZE_1);
                 goto out;
         }

         fputs("mremap succeeded!\n",stdout);
         munmap(buf2,ALLOC_SIZE_2);
out:
         return errno_tmp;
}

Note that by commenting out USE_HUGEPAGES you can omit the flags that 
cause mremap to fail.

Using hugetlbfs is not a fix, but at most a workaround for this problem, 
seeing as the problem occurs with memory mappings. File systems 
shouldn't have anything to say when it comes to (simple) memory allocations.

Thank you very much.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

             reply	other threads:[~2017-10-07  1:59 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-07  1:58 C.Wehrmeyer [this message]
2017-10-09 16:47 ` PROBLEM: Remapping hugepages mappings causes kernel to return EINVAL Mike Kravetz
     [not found] <93684e4b-9e60-ef3a-ba62-5719fdf7cff9@gmx.de>
2017-10-19  7:34 ` C.Wehrmeyer
2017-10-20 22:42   ` Mike Kravetz
2017-10-23 11:42     ` Michal Hocko
2017-10-23 12:22       ` C.Wehrmeyer
2017-10-23 12:41         ` Michal Hocko
2017-10-23 14:00           ` C.Wehrmeyer
2017-10-23 16:13             ` Michal Hocko
2017-10-23 16:46               ` C.Wehrmeyer
2017-10-23 16:57                 ` Michal Hocko
2017-10-23 17:52                   ` C.Wehrmeyer
2017-10-23 18:02                     ` Michal Hocko
2017-10-24  7:41                       ` C.Wehrmeyer
2017-10-24  8:12                         ` Michal Hocko
2017-10-24  8:32                           ` C.Wehrmeyer
2017-10-27 14:29                         ` Vlastimil Babka
2017-10-27 17:06                           ` Mike Kravetz
2017-10-27 17:31                           ` Kirill A. Shutemov
2017-10-23 18:51                     ` Mike Kravetz
2017-10-24  8:09                       ` C.Wehrmeyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ba05809-63a2-2969-e54f-fd0202fe336b@gmx.de \
    --to=c.wehrmeyer@gmx.de \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).