[lkml]   [2019]   [Jul]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [PATCH] mm/mmap: fix the adjusted length error
On Fri, Jul 12, 2019 at 3:53 AM chenjianhong (A)
<> wrote:
> Thank you for your reply!
> > How significant is this problem in real-world use cases? How much trouble is it causing?
> In my opinion, this problem is very rare in real-world use cases. In arm64
> or x86 environment, the virtual memory is enough. In arm32 environment,
> each process has only 3G or 4G or less, but we seldom use out all of the virtual memory,
> it only happens in some special environment. They almost use out all the virtual memory, and
> in some moment, they will change their working mode so they will release and allocate
> memory again. This current length limitation will cause this problem. I explain it's the memory
> length limitation. But they can't accept the reason, it is unreasonable that we fail to allocate
> memory even though the memory gap is enough.

Right. So to summarize, you have a customer accidentally hitting this
and asking you about it ? and I assume their workload is not public ?

> > Have you looked further into this? Michel is concerned about the performance cost of the current solution.
> The current algorithm(change before) is wonderful, and it has been used for a long time, I don't
> think it is worthy to change the whole algorithm in order to fix this problem. Therefore, I just
> adjust the gap_start and gap_end value in place of the length. My change really affects the
> performance because I calculate the gap_start and gap_end value again and again. Does it affect
> too much performance? I have no complex environment, so I can't test it, but I don't think it will cause
> too much performance loss. First, I don't change the whole algorithm. Second, unmapped_area and
> unmapped_area_topdown function aren't used frequently. Maybe there are some big performance problems
> I'm not concerned about. But I think if that's not a problem, there should be a limitation description.

The case I am worried about is if there are a lot of gaps that are
large enough for an unaligned allocation, but too small for an aligned

You could create the bad case as follows:
- Allocate a huge memory block (no need to populate it, so it can
really be as large as virtual memory will allow)
- Free a bunch of 2M holes in that block, but none of them are aligned
- Try to force allocation of a 2M aligned block

With the current code, the allocation will quickly skip over the
unaligned 2M holes. It will either find a 4M gap and allocate a 2M
aligned block from it, or it will fail, but it will be quick in either
case. With the suggested change, the allocation would try each of the
unaligned 2M holes, which could take a long time, before eventually
either finding a large enough aligned gap, or failing.

I can see two ways around this:
- the code could search for a 4M gap at first, like it currently does,
and that fails it could look at all 2M gaps and see if one of them is
aligned. So, there would still be the slow case, but only if the
initial (fast) check failed. Maybe there should be a sysfs setting to
enable the second pass, which would be disabled by default at least on
64-bit systems.
- If the issue only happens when allocating huge pages, and we know
the possible huge page sizes for a process from the start, we could
maintain more information about the gaps so that we could quickly
search for a suitable aligned gaps. that is, for each subtree we would
store both the highest 4K aligned size that can be allocated, and the
highest 2M aligned size as well. That makes a more complete solution
but probably overkill as we are not hitting this frequently enough to
justify the complications.

> -----Original Message-----
> From: Andrew Morton []
> Sent: Friday, July 12, 2019 9:20 AM
> To: chenjianhong (A) <>
> Cc: Michel Lespinasse <>; Greg Kroah-Hartman <>;; Vlastimil Babka <>; Kirill A. Shutemov <>; Yang Shi <>;;;; LKML <>; linux-mm <>;;
> Subject: Re: [PATCH] mm/mmap: fix the adjusted length error
> On Sat, 18 May 2019 07:05:07 +0000 "chenjianhong (A)" <> wrote:
> > I explain my test code and the problem in detail. This problem is
> > found in 32-bit user process, because its virtual is limited, 3G or 4G.
> >
> > First, I explain the bug I found. Function unmapped_area and
> > unmapped_area_topdowns adjust search length to account for worst case
> > alignment overhead, the code is ' length = info->length + info->align_mask; '.
> > The variable info->length is the length we allocate and the variable
> > info->align_mask accounts for the alignment, because the gap_start or
> > info->gap_end
> > value also should be an alignment address, but we can't know the alignment offset.
> > So in the current algorithm, it uses the max alignment offset, this
> > value maybe zero or other(0x1ff000 for shmat function).
> > Is it reasonable way? The required value is longer than what I allocate.
> > What's more, why for the first time I can allocate the memory
> > successfully Via shmat, but after releasing the memory via shmdt and I
> > want to attach again, it fails. This is not acceptable for many people.
> >
> > Second, I explain my test code. The code I have sent an email. The
> > following is the step. I don't think it's something unusual or
> > unreasonable, because the virtual memory space is enough, but the
> > process can allocate from it. And we can't pass explicit addresses to
> > function mmap or shmat, the address is getting from the left vma gap.
> > 1, we allocat large virtual memory;
> > 2, we allocate hugepage memory via shmat, and release one of the
> > hugepage memory block; 3, we allocate hugepage memory by shmat again,
> > this will fail.
> How significant is this problem in real-world use cases? How much trouble is it causing?
> > Third, I want to introduce my change in the current algorithm. I don't
> > change the current algorithm. Also, I think there maybe a better way
> > to fix this error. Nowadays, I can just adjust the gap_start value.
> Have you looked further into this? Michel is concerned about the performance cost of the current solution.

Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.

 \ /
  Last update: 2019-07-12 19:01    [W:0.045 / U:31.704 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site