Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

From: Michal Hocko
Date: Wed Nov 22 2017 - 08:12:57 EST


On Tue 21-11-17 17:48:31, John Hubbard wrote:
[...]
> Hi Michal,
>
> Yes, it really is useful for user space. I'll use CUDA as an example, but I
> think anything that enforces a uniform virtual addressing scheme across CPUs
> and devices, probably has to do something eerily similar. CUDA does this:
>
> a) Searches /proc/<pid>/maps for a "suitable" region of available VA space.
> "Suitable" generally means it has to have a base address within a certain
> limited range (a particular device model might have odd limitations, for
> example), it has to be large enough, and alignment has to be large enough
> (again, various devices may have constraints that lead us to do this).
>
> This is of course subject to races with other threads in the process.
>
> Let's say it finds a region starting at va.
>
> b) Next it does:
> p = mmap(va, ...)
>
> *without* setting MAP_FIXED, of course (so va is just a hint), to attempt to
> safely reserve that region. If p != va, then in most cases, this is a failure
> (almost certainly due to another thread getting a mapping from that region
> before we did), and so this layer now has to call munmap(), before returning
> a "failure: retry" to upper layers.
>
> IMPROVEMENT: --> if instead, we could call this:
>
> p = mmap(va, ... MAP_FIXED_NO_CLOBBER ...)
>
> , then we could skip the munmap() call upon failure. This is a small thing,
> but it is useful here. (Thanks to Piotr Jaroszynski and Mark Hairgrove
> for helping me get that detail exactly right, btw.)
>
> c) After that, CUDA suballocates from p, via:
>
> q = mmap(sub_region_start, ... MAP_FIXED ...)
>
> Interestingly enough, "freeing" is also done via MAP_FIXED, and setting PROT_NONE
> to the subregion. Anyway, I just included (c) for general interest.

OK, I will add this to the changelog. This is basically the "Atomic
address range probing in the multithreaded programs" I've had in the
cover letter.

> I expect that as we continue working on the open source compute software stack,
> this new capability will be useful there, too.
>
> Oh, and on the naming, when I described how your implementation worked (without
> naming it) to Piotr, he said, "oh, something like map-fixed-no-clobber?". So I
> think my miniature sociology naming data point here can bolster the case ever so
> slightly for calling it MAP_FIXED_NO_CLOBBER. haha. :)

I will be probably stubborn and go with a shorter name I have currently.
I am not very fond-of-very-long-names.
--
Michal Hocko
SUSE Labs