linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill@shutemov.name>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Nicholas Piggin <npiggin@gmail.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCHv2 1/2] x86/mm: Do not allow non-MAP_FIXED mapping across DEFAULT_MAP_WINDOW border
Date: Wed, 15 Nov 2017 17:13:27 +0300	[thread overview]
Message-ID: <20171115141326.xzsbkycdwq4vafxf@node.shutemov.name> (raw)
In-Reply-To: <20171115140426.bgvcd3bmegqadm5q@node.shutemov.name>

On Wed, Nov 15, 2017 at 05:04:26PM +0300, Kirill A. Shutemov wrote:
> On Wed, Nov 15, 2017 at 03:10:42PM +0300, Kirill A. Shutemov wrote:
> > On Wed, Nov 15, 2017 at 12:39:40PM +0100, Thomas Gleixner wrote:
> > > On Wed, 15 Nov 2017, Kirill A. Shutemov wrote:
> > > > On Wed, Nov 15, 2017 at 12:00:46AM +0100, Thomas Gleixner wrote:
> > > > > On Wed, 15 Nov 2017, Kirill A. Shutemov wrote:
> > > > > > On Tue, Nov 14, 2017 at 09:54:52PM +0100, Thomas Gleixner wrote:
> > > > > > > On Tue, 14 Nov 2017, Kirill A. Shutemov wrote:
> > > > > > > 
> > > > > > > > On Tue, Nov 14, 2017 at 05:01:50PM +0100, Thomas Gleixner wrote:
> > > > > > > > > @@ -198,11 +199,14 @@ arch_get_unmapped_area_topdown(struct fi
> > > > > > > > >  	/* requesting a specific address */
> > > > > > > > >  	if (addr) {
> > > > > > > > >  		addr = PAGE_ALIGN(addr);
> > > > > > > > > +		if (!mmap_address_hint_valid(addr, len))
> > > > > > > > > +			goto get_unmapped_area;
> > > > > > > > > +
> > > > > > > > 
> > > > > > > > Here and in hugetlb_get_unmapped_area(), we should align the addr after
> > > > > > > > the check, not before. Otherwise the alignment itself can bring us over
> > > > > > > > the borderline as we align up.
> > > > > > > 
> > > > > > > Hmm, then I wonder whether the next check against vm_start_gap() which
> > > > > > > checks against the aligned address is correct:
> > > > > > > 
> > > > > > >                 addr = PAGE_ALIGN(addr);
> > > > > > >                 vma = find_vma(mm, addr);
> > > > > > > 
> > > > > > >                 if (end - len >= addr &&
> > > > > > >                     (!vma || addr + len <= vm_start_gap(vma)))
> > > > > > >                         return addr;
> > > > > > 
> > > > > > I think the check is correct. The check is against resulting addresses
> > > > > > that end up in vm_start/vm_end. In our case we want to figure out what
> > > > > > user asked for.
> > > > > 
> > > > > Well, but then checking just against the user supplied addr is only half of
> > > > > the story.
> > > > > 
> > > > >     addr = boundary - PAGE_SIZE - PAGE_SIZE / 2;
> > > > >     len = PAGE_SIZE - PAGE_SIZE / 2;
> > > > > 
> > > > > That fits, but then after alignment we end up with
> > > > > 
> > > > >     addr = boudary - PAGE_SIZE;
> > > > > 
> > > > > and due to len > PAGE_SIZE this will result in a mapping which crosses the
> > > > > boundary, right? So checking against the PAGE_ALIGN(addr) should be the
> > > > > right thing to do.
> > > > 
> > > > IIUC, this is only the case if 'len' is not aligned, right?
> > > > 
> > > > >From what I see we expect caller to align it (and mm/mmap.c does this, I
> > > > haven't checked other callers).
> > > > 
> > > > And hugetlb would actively reject non-aligned len.
> > > > 
> > > > I *think* we should be fine with checking unaligned 'addr'.
> > > 
> > > I think we should keep it consistent for the normal and the huge case and
> > > just check aligned and be done with it.
> > 
> > Aligned 'addr'? Or 'len'? Both?
> > 
> > We would have problem with checking aligned addr. I steped it in hugetlb
> > case:
> > 
> >   - User asks for mmap((1UL << 47) - PAGE_SIZE, 2 << 20, MAP_HUGETLB);
> > 
> >   - On 4-level paging machine this gives us invalid hint address as
> >     'TASK_SIZE - len' is more than 'addr'. Goto get_unmapped_area.
> > 
> >   - On 5-level paging machine hint address gets rounded up to next 2MB
> >     boundary that is exactly 1UL << 47 and we happily allocate from full
> >     address space which may lead to trouble.
> 
> Below is updated patch with self-test.
> 
> Output on 5-level paging machine:
> 
> mmap(NULL): 0x7fbbad1f3000 - OK
> mmap(LOW_ADDR): 0x40000000 - OK
> mmap(HIGH_ADDR): 0x4000000000000 - OK
> mmap(HIGH_ADDR) again: 0xffffbbad1fb000 - OK
> mmap(HIGH_ADDR, MAP_FIXED): 0x4000000000000 - OK
> mmap(-1): 0xffffbbad1f9000 - OK
> mmap(-1) again: 0xffffbbad1f7000 - OK
> mmap((1UL << 47), 2 * PAGE_SIZE): 0x7fbbad1f3000 - OK
> mmap((1UL << 47), 2 * PAGE_SIZE / 2): 0x7fbbad1f1000 - OK
> mmap((1UL << 47) - PAGE_SIZE, 2 * PAGE_SIZE, MAP_FIXED): 0x7ffffffff000 - OK
> mmap(NULL, MAP_HUGETLB): 0x7fbbac400000 - OK
> mmap(LOW_ADDR, MAP_HUGETLB): 0x40000000 - OK
> mmap(HIGH_ADDR, MAP_HUGETLB): 0x4000000000000 - OK
> mmap(HIGH_ADDR, MAP_HUGETLB) again: 0xffffbbace00000 - OK
> mmap(HIGH_ADDR, MAP_FIXED | MAP_HUGETLB): 0x4000000000000 - OK
> mmap(-1, MAP_HUGETLB): (nil) - OK
> mmap(-1, MAP_HUGETLB) again: 0x7fbbac400000 - OK
> mmap((1UL << 47), 2UL << 20, MAP_HUGETLB): 0x800000000000 - FAILED
> mmap((1UL << 47) - (2UL << 20), 4UL << 20, MAP_FIXED | MAP_HUGETLB): 0x7fffffe00000 - OK
> 
> So, only hugetlb is problematic. mmap() aligns addr to PAGE_SIZE.
> See round_hint_to_min(). In this case we round address *down* and it works
> fine.
> 
> Replacing 'addr = ALIGN(addr, huge_page_size(h))' in hugetlbpage.c with
> 'addr &= huge_page_mask(h)' fixes the issue.

What about this fixup:

diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c
index 835b78720ca2..676774b9bb8d 100644
--- a/arch/x86/kernel/sys_x86_64.c
+++ b/arch/x86/kernel/sys_x86_64.c
@@ -198,7 +198,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
 
 	/* requesting a specific address */
 	if (addr) {
-		addr = PAGE_ALIGN(addr);
+		addr &= PAGE_MASK;
 		if (!mmap_address_hint_valid(addr, len))
 			goto get_unmapped_area;
 
diff --git a/arch/x86/mm/hugetlbpage.c b/arch/x86/mm/hugetlbpage.c
index 92db903c3dad..00b296617ca4 100644
--- a/arch/x86/mm/hugetlbpage.c
+++ b/arch/x86/mm/hugetlbpage.c
@@ -166,7 +166,7 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	}
 
 	if (addr) {
-		addr = ALIGN(addr, huge_page_size(h));
+		addr &= huge_page_mask(h);
 		if (!mmap_address_hint_valid(addr, len))
 			goto get_unmapped_area;
 
-- 
 Kirill A. Shutemov

--
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-11-15 14:13 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-14 13:43 Kirill A. Shutemov
2017-11-14 13:43 ` [PATCHv2 2/2] x86/selftests: Add test for mapping placement for 5-level paging Kirill A. Shutemov
2017-11-14 16:01 ` [PATCHv2 1/2] x86/mm: Do not allow non-MAP_FIXED mapping across DEFAULT_MAP_WINDOW border Thomas Gleixner
2017-11-14 19:21   ` Kirill A. Shutemov
2017-11-14 20:29     ` Thomas Gleixner
2017-11-14 20:21   ` Kirill A. Shutemov
2017-11-14 20:54     ` Thomas Gleixner
2017-11-14 22:27       ` Kirill A. Shutemov
2017-11-14 23:00         ` Thomas Gleixner
2017-11-15 11:27           ` Kirill A. Shutemov
2017-11-15 11:39             ` Thomas Gleixner
2017-11-15 12:10               ` Kirill A. Shutemov
2017-11-15 14:04                 ` Kirill A. Shutemov
2017-11-15 14:13                   ` Kirill A. Shutemov [this message]
2017-11-15 14:23                     ` Thomas Gleixner
2017-11-15 14:24                       ` Kirill A. Shutemov
2017-11-15 14:18                 ` Thomas Gleixner

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=20171115141326.xzsbkycdwq4vafxf@node.shutemov.name \
    --to=kirill@shutemov.name \
    --cc=hpa@zytor.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@amacapital.net \
    --cc=mingo@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=x86@kernel.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