diff mbox series

[v15,4/8] mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page

Message ID 20210208085013.89436-5-songmuchun@bytedance.com (mailing list archive)
State New, archived
Headers show
Series Free some vmemmap pages of HugeTLB page | expand

Commit Message

Muchun Song Feb. 8, 2021, 8:50 a.m. UTC
When we free a HugeTLB page to the buddy allocator, we should allocate the
vmemmap pages associated with it. But we may cannot allocate vmemmap pages
when the system is under memory pressure, in this case, we just refuse to
free the HugeTLB page instead of looping forever trying to allocate the
pages.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 include/linux/mm.h   |  2 ++
 mm/hugetlb.c         | 19 ++++++++++++-
 mm/hugetlb_vmemmap.c | 30 +++++++++++++++++++++
 mm/hugetlb_vmemmap.h |  6 +++++
 mm/sparse-vmemmap.c  | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 130 insertions(+), 2 deletions(-)

Comments

Mike Kravetz Feb. 11, 2021, 6:05 p.m. UTC | #1
On 2/8/21 12:50 AM, Muchun Song wrote:
> When we free a HugeTLB page to the buddy allocator, we should allocate the
> vmemmap pages associated with it. But we may cannot allocate vmemmap pages
> when the system is under memory pressure, in this case, we just refuse to
> free the HugeTLB page instead of looping forever trying to allocate the
> pages.
> 
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  include/linux/mm.h   |  2 ++
>  mm/hugetlb.c         | 19 ++++++++++++-
>  mm/hugetlb_vmemmap.c | 30 +++++++++++++++++++++
>  mm/hugetlb_vmemmap.h |  6 +++++
>  mm/sparse-vmemmap.c  | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++-
>  5 files changed, 130 insertions(+), 2 deletions(-)

Muchun has done a great job simplifying this patch series and addressing
issues as they are brought up.  This patch addresses the issue which seems
to be the biggest stumbling block to this series.  The need to allocate
vmemmap pages to dissolve a hugetlb page to the buddy allocator.  The way
it is addressed in this patch is to simply fail to dissolve the hugetlb
page if the vmmemmap pages can not be allocated.  IMO, this is an 'acceptable'
strategy.  If we find ourselves in this situation then we are likely to be
hitting other corner cases in the system.  I wish there was a perfect way
to address this issue, but we have been unable to come up with one.

There was a decent discussion about this is a previous version of the
series starting here:
https://lore.kernel.org/linux-mm/20210126092942.GA10602@linux/
In this thread various other options were suggested and discussed.

I would like to come to some agreement on an acceptable way to handle this
specific issue.  IMO, it makes little sense to continue refining other
parts of this series if we can not figure out how to move forward on this
issue.

It would be great if David H, David R and Michal could share their opinions
on this.  No need to review details the code yet (unless you want), but
let's start a discussion on how to move past this issue if we can.
David Hildenbrand Feb. 12, 2021, 2:15 p.m. UTC | #2
On 11.02.21 19:05, Mike Kravetz wrote:
> On 2/8/21 12:50 AM, Muchun Song wrote:
>> When we free a HugeTLB page to the buddy allocator, we should allocate the
>> vmemmap pages associated with it. But we may cannot allocate vmemmap pages
>> when the system is under memory pressure, in this case, we just refuse to
>> free the HugeTLB page instead of looping forever trying to allocate the
>> pages.
>>
>> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
>> ---
>>   include/linux/mm.h   |  2 ++
>>   mm/hugetlb.c         | 19 ++++++++++++-
>>   mm/hugetlb_vmemmap.c | 30 +++++++++++++++++++++
>>   mm/hugetlb_vmemmap.h |  6 +++++
>>   mm/sparse-vmemmap.c  | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++-
>>   5 files changed, 130 insertions(+), 2 deletions(-)
> 
> Muchun has done a great job simplifying this patch series and addressing
> issues as they are brought up.  This patch addresses the issue which seems
> to be the biggest stumbling block to this series.  The need to allocate
> vmemmap pages to dissolve a hugetlb page to the buddy allocator.  The way
> it is addressed in this patch is to simply fail to dissolve the hugetlb
> page if the vmmemmap pages can not be allocated.  IMO, this is an 'acceptable'
> strategy.  If we find ourselves in this situation then we are likely to be
> hitting other corner cases in the system.  I wish there was a perfect way
> to address this issue, but we have been unable to come up with one.
> 
> There was a decent discussion about this is a previous version of the
> series starting here:
> https://lore.kernel.org/linux-mm/20210126092942.GA10602@linux/
> In this thread various other options were suggested and discussed.
> 
> I would like to come to some agreement on an acceptable way to handle this
> specific issue.  IMO, it makes little sense to continue refining other
> parts of this series if we can not figure out how to move forward on this
> issue.
> 
> It would be great if David H, David R and Michal could share their opinions
> on this.  No need to review details the code yet (unless you want), but
> let's start a discussion on how to move past this issue if we can.

So a summary from my side:

We might fail freeing a huge page at any point in time iff we are low on 
kernel (!CMA, !ZONE_MOVABLE) memory. While we could play games with 
allocating the vmemmap from a huge page itself in some cases (e.g., 
!CMA, !ZONE_MOVABLE), simply retrying is way easier and we don't turn 
the huge page forever unusable.

Corner cases might be having many huge pages in ZONE_MOVABLE, freeing 
them all at once and eating up a lot of kernel memory. But then, the 
same setup would already be problematic nowadays where we simply always 
consume that kernel memory for the vmemmap.

I think this problem only really becomes visible in corner cases. And 
someone actively has to enable new behavior.


1. Failing to free a huge page triggered by the user (decrease nr_pages):

Bad luck. Try again later.

2. Failing to free a surplus huge page when freed by the application:

Bad luck. But who will try again later?

3. Failing to dissolve a free huge page on ZONE_MOVABLE via offline_pages()

This is a bit unfortunate if we have plenty of ZONE_MOVABLE memory but 
are low on kernel memory. For example, migration of huge pages would 
still work, however, dissolving the free page does not work. I'd say 
this is a corner cases. When the system is that much under memory 
pressure, offlining/unplug can be expected to fail.

4. Failing to dissolve a huge page on CMA/ZONE_MOVABLE via 
alloc_contig_range() - once we have that handling in place. Mainly 
affects CMA and virtio-mem.

Similar to 3. However, we didn't even take care of huge pages *at all* 
for now (neither migrate nor dissolve). So actually don't make the 
current state any worse. virito-mem will handle migration errors 
gracefully. CMA might be able to fallback on other free areas within the 
CMA region.


I'd say, document the changed behavior properly so people are aware that 
there might be issues in corner cases with huge pages on CMA / ZONE_MOVABLE.
Michal Hocko Feb. 12, 2021, 3:32 p.m. UTC | #3
On Mon 08-02-21 16:50:09, Muchun Song wrote:
> When we free a HugeTLB page to the buddy allocator, we should allocate the
> vmemmap pages associated with it. But we may cannot allocate vmemmap pages
> when the system is under memory pressure, in this case, we just refuse to
> free the HugeTLB page instead of looping forever trying to allocate the
> pages.

Thanks for simplifying the implementation from your early proposal!

This will not be looping for ever. The allocation will usually trigger
the OOM killer and sooner or later there will be a memory to allocate
from or the system panics when there are no eligible tasks to kill. This
is just a side note.

I think the changelog could benefit from a more explicit documentation
of those error failures. There are different cases when the hugetlb page
is freed. It can be due to an admin intervention (decrease the pool),
overcommit, migration, dissolving and likely some others. Most of them
should be fine to stay in the pool which would just increase the surplus
pages in the pool. I am not so sure about dissolving path.
[...]
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index 0209b736e0b4..3d85e3ab7caa 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -169,6 +169,8 @@
>   * (last) level. So this type of HugeTLB page can be optimized only when its
>   * size of the struct page structs is greater than 2 pages.
>   */
> +#define pr_fmt(fmt)	"HugeTLB: " fmt
> +
>  #include "hugetlb_vmemmap.h"
>  
>  /*
> @@ -198,6 +200,34 @@ static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
>  	return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
>  }
>  
> +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> +{
> +	int ret;
> +	unsigned long vmemmap_addr = (unsigned long)head;
> +	unsigned long vmemmap_end, vmemmap_reuse;
> +
> +	if (!free_vmemmap_pages_per_hpage(h))
> +		return 0;
> +
> +	vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> +	vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> +	vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> +
> +	/*
> +	 * The pages which the vmemmap virtual address range [@vmemmap_addr,
> +	 * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> +	 * the range is mapped to the page which @vmemmap_reuse is mapped to.
> +	 * When a HugeTLB page is freed to the buddy allocator, previously
> +	 * discarded vmemmap pages must be allocated and remapping.
> +	 */
> +	ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> +				  GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);

I do not think that this is a good allocation mode. GFP_ATOMIC is a non
sleeping allocation and a medium memory pressure might cause it to
fail prematurely. I do not think this is really an atomic context which
couldn't afford memory reclaim. I also do not think we want to grant
access to memory reserve is reasonable. Just think of a huge number of
hugetlb pages being freed which can deplete the memory reserve for
atomic allocations. I think that you want 
	GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN | __GFP_THISNODE

for an initial implementation. The justification being that the
allocation should at least try to reclaim but it shouldn't cause any
major disruption because the failure is not fatal. If the failure rate
would be impractically high then just drop NORETRY part. You can replace
it by __GFP_RETRY_MAYFAIL but that shouldn't be strictly necessary
because __GFP_THISNODE on its own implies on OOM killer, but that is
kinda ugly to rely on.
Muchun Song Feb. 15, 2021, 10:05 a.m. UTC | #4
On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 08-02-21 16:50:09, Muchun Song wrote:
> > When we free a HugeTLB page to the buddy allocator, we should allocate the
> > vmemmap pages associated with it. But we may cannot allocate vmemmap pages
> > when the system is under memory pressure, in this case, we just refuse to
> > free the HugeTLB page instead of looping forever trying to allocate the
> > pages.
>
> Thanks for simplifying the implementation from your early proposal!
>
> This will not be looping for ever. The allocation will usually trigger
> the OOM killer and sooner or later there will be a memory to allocate
> from or the system panics when there are no eligible tasks to kill. This
> is just a side note.
>
> I think the changelog could benefit from a more explicit documentation
> of those error failures. There are different cases when the hugetlb page
> is freed. It can be due to an admin intervention (decrease the pool),
> overcommit, migration, dissolving and likely some others. Most of them
> should be fine to stay in the pool which would just increase the surplus
> pages in the pool. I am not so sure about dissolving path.

Thanks. I will update the changelog.

> [...]
> > diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> > index 0209b736e0b4..3d85e3ab7caa 100644
> > --- a/mm/hugetlb_vmemmap.c
> > +++ b/mm/hugetlb_vmemmap.c
> > @@ -169,6 +169,8 @@
> >   * (last) level. So this type of HugeTLB page can be optimized only when its
> >   * size of the struct page structs is greater than 2 pages.
> >   */
> > +#define pr_fmt(fmt)  "HugeTLB: " fmt
> > +
> >  #include "hugetlb_vmemmap.h"
> >
> >  /*
> > @@ -198,6 +200,34 @@ static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
> >       return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
> >  }
> >
> > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > +{
> > +     int ret;
> > +     unsigned long vmemmap_addr = (unsigned long)head;
> > +     unsigned long vmemmap_end, vmemmap_reuse;
> > +
> > +     if (!free_vmemmap_pages_per_hpage(h))
> > +             return 0;
> > +
> > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > +
> > +     /*
> > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > +      * discarded vmemmap pages must be allocated and remapping.
> > +      */
> > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
>
> I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> sleeping allocation and a medium memory pressure might cause it to
> fail prematurely. I do not think this is really an atomic context which
> couldn't afford memory reclaim. I also do not think we want to grant

Because alloc_huge_page_vmemmap is called under hugetlb_lock
now. So using GFP_ATOMIC indeed makes the code more simpler.
From the document of the kernel, I learned that __GFP_NOMEMALLOC
can be used to explicitly forbid access to emergency reserves. So if
we do not want to use the reserve memory. How about replacing it to

GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE

Thanks.

> access to memory reserve is reasonable. Just think of a huge number of
> hugetlb pages being freed which can deplete the memory reserve for
> atomic allocations. I think that you want
>         GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN | __GFP_THISNODE
>
> for an initial implementation. The justification being that the
> allocation should at least try to reclaim but it shouldn't cause any
> major disruption because the failure is not fatal. If the failure rate
> would be impractically high then just drop NORETRY part. You can replace
> it by __GFP_RETRY_MAYFAIL but that shouldn't be strictly necessary
> because __GFP_THISNODE on its own implies on OOM killer, but that is
> kinda ugly to rely on.
> --
> Michal Hocko
> SUSE Labs
Michal Hocko Feb. 15, 2021, 10:33 a.m. UTC | #5
On Mon 15-02-21 18:05:06, Muchun Song wrote:
> On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
[...]
> > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > +{
> > > +     int ret;
> > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > +
> > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > +             return 0;
> > > +
> > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > +
> > > +     /*
> > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > +      * discarded vmemmap pages must be allocated and remapping.
> > > +      */
> > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> >
> > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > sleeping allocation and a medium memory pressure might cause it to
> > fail prematurely. I do not think this is really an atomic context which
> > couldn't afford memory reclaim. I also do not think we want to grant
> 
> Because alloc_huge_page_vmemmap is called under hugetlb_lock
> now. So using GFP_ATOMIC indeed makes the code more simpler.

You can have a preallocated list of pages prior taking the lock.
Moreover do we want to manipulate vmemmaps from under spinlock in
general. I have to say I have missed that detail when reviewing. Need to
think more.

> From the document of the kernel, I learned that __GFP_NOMEMALLOC
> can be used to explicitly forbid access to emergency reserves. So if
> we do not want to use the reserve memory. How about replacing it to
> 
> GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE

The whole point of GFP_ATOMIC is to grant access to memory reserves so
the above is quite dubious. If you do not want access to memory reserves
then use GFP_NOWAIT instead. But failures are much more easier to happen
then.

NOMEMALLOC is meant to be used from paths which are allowed to consume
memory reserves - e.g. when invoked from the memory reclaim path.
Muchun Song Feb. 15, 2021, 11:51 a.m. UTC | #6
On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> [...]
> > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > +{
> > > > +     int ret;
> > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > +
> > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > +             return 0;
> > > > +
> > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > +
> > > > +     /*
> > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > +      */
> > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > >
> > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > sleeping allocation and a medium memory pressure might cause it to
> > > fail prematurely. I do not think this is really an atomic context which
> > > couldn't afford memory reclaim. I also do not think we want to grant
> >
> > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > now. So using GFP_ATOMIC indeed makes the code more simpler.
>
> You can have a preallocated list of pages prior taking the lock.

A discussion about this can refer to here:

https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/

> Moreover do we want to manipulate vmemmaps from under spinlock in
> general. I have to say I have missed that detail when reviewing. Need to
> think more.
>
> > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > can be used to explicitly forbid access to emergency reserves. So if
> > we do not want to use the reserve memory. How about replacing it to
> >
> > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
>
> The whole point of GFP_ATOMIC is to grant access to memory reserves so
> the above is quite dubious. If you do not want access to memory reserves

Look at the code of gfp_to_alloc_flags().

static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
{
        [...]
        if (gfp_mask & __GFP_ATOMIC) {
        /*
         * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
         * if it can't schedule.
         */
        if (!(gfp_mask & __GFP_NOMEMALLOC))
                alloc_flags |= ALLOC_HARDER;
       [...]
}

Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).

> then use GFP_NOWAIT instead. But failures are much more easier to happen
> then.
>
> NOMEMALLOC is meant to be used from paths which are allowed to consume
> memory reserves - e.g. when invoked from the memory reclaim path.
> --
> Michal Hocko
> SUSE Labs
Muchun Song Feb. 15, 2021, noon UTC | #7
On Mon, Feb 15, 2021 at 7:51 PM Muchun Song <songmuchun@bytedance.com> wrote:
>
> On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > [...]
> > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > +{
> > > > > +     int ret;
> > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > +
> > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > +             return 0;
> > > > > +
> > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > +
> > > > > +     /*
> > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > +      */
> > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > >
> > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > sleeping allocation and a medium memory pressure might cause it to
> > > > fail prematurely. I do not think this is really an atomic context which
> > > > couldn't afford memory reclaim. I also do not think we want to grant
> > >
> > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> >
> > You can have a preallocated list of pages prior taking the lock.
>
> A discussion about this can refer to here:
>
> https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/
>
> > Moreover do we want to manipulate vmemmaps from under spinlock in
> > general. I have to say I have missed that detail when reviewing. Need to
> > think more.
> >
> > > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > > can be used to explicitly forbid access to emergency reserves. So if
> > > we do not want to use the reserve memory. How about replacing it to
> > >
> > > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
> >
> > The whole point of GFP_ATOMIC is to grant access to memory reserves so
> > the above is quite dubious. If you do not want access to memory reserves
>
> Look at the code of gfp_to_alloc_flags().
>
> static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
> {
>         [...]
>         if (gfp_mask & __GFP_ATOMIC) {
>         /*
>          * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
>          * if it can't schedule.
>          */
>         if (!(gfp_mask & __GFP_NOMEMALLOC))
>                 alloc_flags |= ALLOC_HARDER;
>        [...]
> }
>
> Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).

I also found similar users.

netdev_alloc_frag()
    page_frag_alloc(GFP_ATOMIC)
        __page_frag_cache_refill(GFP_ATOMIC)
            alloc_pages_node(GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN |
__GFP_NORETRY |
                                                __GFP_NOMEMALLOC)

>
> > then use GFP_NOWAIT instead. But failures are much more easier to happen
> > then.
> >
> > NOMEMALLOC is meant to be used from paths which are allowed to consume
> > memory reserves - e.g. when invoked from the memory reclaim path.
> > --
> > Michal Hocko
> > SUSE Labs
Michal Hocko Feb. 15, 2021, 12:18 p.m. UTC | #8
On Mon 15-02-21 20:00:07, Muchun Song wrote:
> On Mon, Feb 15, 2021 at 7:51 PM Muchun Song <songmuchun@bytedance.com> wrote:
> >
> > On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> > >
> > > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > > [...]
> > > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > > +{
> > > > > > +     int ret;
> > > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > > +
> > > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > > +             return 0;
> > > > > > +
> > > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > > +
> > > > > > +     /*
> > > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > > +      */
> > > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > > >
> > > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > > sleeping allocation and a medium memory pressure might cause it to
> > > > > fail prematurely. I do not think this is really an atomic context which
> > > > > couldn't afford memory reclaim. I also do not think we want to grant
> > > >
> > > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> > >
> > > You can have a preallocated list of pages prior taking the lock.
> >
> > A discussion about this can refer to here:
> >
> > https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/
> >
> > > Moreover do we want to manipulate vmemmaps from under spinlock in
> > > general. I have to say I have missed that detail when reviewing. Need to
> > > think more.
> > >
> > > > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > > > can be used to explicitly forbid access to emergency reserves. So if
> > > > we do not want to use the reserve memory. How about replacing it to
> > > >
> > > > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
> > >
> > > The whole point of GFP_ATOMIC is to grant access to memory reserves so
> > > the above is quite dubious. If you do not want access to memory reserves
> >
> > Look at the code of gfp_to_alloc_flags().
> >
> > static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
> > {
> >         [...]
> >         if (gfp_mask & __GFP_ATOMIC) {
> >         /*
> >          * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
> >          * if it can't schedule.
> >          */
> >         if (!(gfp_mask & __GFP_NOMEMALLOC))
> >                 alloc_flags |= ALLOC_HARDER;
> >        [...]
> > }
> >
> > Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).

Please read my response again more carefully. I am not claiming that
combination is not allowed. I have said it doesn't make any sense in
this context.
Michal Hocko Feb. 15, 2021, 12:24 p.m. UTC | #9
On Mon 15-02-21 19:51:26, Muchun Song wrote:
> On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > [...]
> > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > +{
> > > > > +     int ret;
> > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > +
> > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > +             return 0;
> > > > > +
> > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > +
> > > > > +     /*
> > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > +      */
> > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > >
> > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > sleeping allocation and a medium memory pressure might cause it to
> > > > fail prematurely. I do not think this is really an atomic context which
> > > > couldn't afford memory reclaim. I also do not think we want to grant
> > >
> > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> >
> > You can have a preallocated list of pages prior taking the lock.
> 
> A discussion about this can refer to here:
> 
> https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/

I do not see any real response to the pre-allocation argument except
that put_page can be called from an atomic context. Which might be true
in general but it is not the case for hugetlb pages. hugetlb_lock would
have to be irq safe otherwise. Also the whole operation can be scheduled
to a kworker context for a stronger allocation context.
Muchun Song Feb. 15, 2021, 12:44 p.m. UTC | #10
On Mon, Feb 15, 2021 at 8:18 PM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 15-02-21 20:00:07, Muchun Song wrote:
> > On Mon, Feb 15, 2021 at 7:51 PM Muchun Song <songmuchun@bytedance.com> wrote:
> > >
> > > On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> > > >
> > > > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > > > [...]
> > > > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > > > +{
> > > > > > > +     int ret;
> > > > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > > > +
> > > > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > > > +             return 0;
> > > > > > > +
> > > > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > > > +
> > > > > > > +     /*
> > > > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > > > +      */
> > > > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > > > >
> > > > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > > > sleeping allocation and a medium memory pressure might cause it to
> > > > > > fail prematurely. I do not think this is really an atomic context which
> > > > > > couldn't afford memory reclaim. I also do not think we want to grant
> > > > >
> > > > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> > > >
> > > > You can have a preallocated list of pages prior taking the lock.
> > >
> > > A discussion about this can refer to here:
> > >
> > > https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/
> > >
> > > > Moreover do we want to manipulate vmemmaps from under spinlock in
> > > > general. I have to say I have missed that detail when reviewing. Need to
> > > > think more.
> > > >
> > > > > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > > > > can be used to explicitly forbid access to emergency reserves. So if
> > > > > we do not want to use the reserve memory. How about replacing it to
> > > > >
> > > > > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
> > > >
> > > > The whole point of GFP_ATOMIC is to grant access to memory reserves so
> > > > the above is quite dubious. If you do not want access to memory reserves
> > >
> > > Look at the code of gfp_to_alloc_flags().
> > >
> > > static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
> > > {
> > >         [...]
> > >         if (gfp_mask & __GFP_ATOMIC) {
> > >         /*
> > >          * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
> > >          * if it can't schedule.
> > >          */
> > >         if (!(gfp_mask & __GFP_NOMEMALLOC))
> > >                 alloc_flags |= ALLOC_HARDER;
> > >        [...]
> > > }
> > >
> > > Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).
>
> Please read my response again more carefully. I am not claiming that
> combination is not allowed. I have said it doesn't make any sense in
> this context.

I see you are worried that using GFP_ATOMIC will use reverse memory
unlimited. So I think that __GFP_NOMEMALLOC may be suitable for us.
Sorry, I may not understand the point you said. What I missed?

>
> --
> Michal Hocko
> SUSE Labs
Michal Hocko Feb. 15, 2021, 1:19 p.m. UTC | #11
On Mon 15-02-21 20:44:57, Muchun Song wrote:
> On Mon, Feb 15, 2021 at 8:18 PM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Mon 15-02-21 20:00:07, Muchun Song wrote:
> > > On Mon, Feb 15, 2021 at 7:51 PM Muchun Song <songmuchun@bytedance.com> wrote:
> > > >
> > > > On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> > > > >
> > > > > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > > > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > > > > [...]
> > > > > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > > > > +{
> > > > > > > > +     int ret;
> > > > > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > > > > +
> > > > > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > > > > +             return 0;
> > > > > > > > +
> > > > > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > > > > +
> > > > > > > > +     /*
> > > > > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > > > > +      */
> > > > > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > > > > >
> > > > > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > > > > sleeping allocation and a medium memory pressure might cause it to
> > > > > > > fail prematurely. I do not think this is really an atomic context which
> > > > > > > couldn't afford memory reclaim. I also do not think we want to grant
> > > > > >
> > > > > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > > > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> > > > >
> > > > > You can have a preallocated list of pages prior taking the lock.
> > > >
> > > > A discussion about this can refer to here:
> > > >
> > > > https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/
> > > >
> > > > > Moreover do we want to manipulate vmemmaps from under spinlock in
> > > > > general. I have to say I have missed that detail when reviewing. Need to
> > > > > think more.
> > > > >
> > > > > > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > > > > > can be used to explicitly forbid access to emergency reserves. So if
> > > > > > we do not want to use the reserve memory. How about replacing it to
> > > > > >
> > > > > > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
> > > > >
> > > > > The whole point of GFP_ATOMIC is to grant access to memory reserves so
> > > > > the above is quite dubious. If you do not want access to memory reserves
> > > >
> > > > Look at the code of gfp_to_alloc_flags().
> > > >
> > > > static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
> > > > {
> > > >         [...]
> > > >         if (gfp_mask & __GFP_ATOMIC) {
> > > >         /*
> > > >          * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
> > > >          * if it can't schedule.
> > > >          */
> > > >         if (!(gfp_mask & __GFP_NOMEMALLOC))
> > > >                 alloc_flags |= ALLOC_HARDER;
> > > >        [...]
> > > > }
> > > >
> > > > Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).
> >
> > Please read my response again more carefully. I am not claiming that
> > combination is not allowed. I have said it doesn't make any sense in
> > this context.
> 
> I see you are worried that using GFP_ATOMIC will use reverse memory
> unlimited. So I think that __GFP_NOMEMALLOC may be suitable for us.
> Sorry, I may not understand the point you said. What I missed?

OK, let me try to explain again. GFP_ATOMIC is not only a non-sleeping
allocation request. It also grants access to memory reserves. The later
is a bit more involved because there are more layers of memory reserves
to access but that is not really important. Non-sleeping semantic can be
achieved by GFP_NOWAIT which will not grant access to reserves unless
explicitly stated - e.g. by __GFP_HIGH or __GFP_ATOMIC.
Is that more clear?

Now again why I do not think access to memory reserves is suitable.
Hugetlb pages can be released in a large batches and that might cause a
peak depletion of memory reserves which are normally used by other
consumers as well. Other GFP_ATOMIC users might see allocation failures.
Those shouldn't be really fatal as nobody should be relying on those and
a failure usually mean a hand over to a different, less constrained,
context. So this concern is more about a more well behaved behavior from
the hugetlb side than a correctness.
Is that more clear?

There shouldn't be any real reason why the memory allocation for
vmemmaps, or handling vmemmap in general, has to be done from within the
hugetlb lock and therefore requiring a non-sleeping semantic. All that
can be deferred to a more relaxed context. If you want to make a
GFP_NOWAIT optimistic attempt in the direct free path then no problem
but you have to expect failures under memory pressure. If you want to
have a more robust allocation request then you have to go outside of the
spin lock and use GFP_KERNEL | __GFP_NORETRY or GFP_KERNEL |
__GFP_RETRY_MAYFAIL depending on how hard you want to try.
__GFP_THISNODE makes a slight difference here but something that I would
recommend not depending on.
Is that more clear?
Muchun Song Feb. 15, 2021, 3:36 p.m. UTC | #12
On Mon, Feb 15, 2021 at 9:19 PM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 15-02-21 20:44:57, Muchun Song wrote:
> > On Mon, Feb 15, 2021 at 8:18 PM Michal Hocko <mhocko@suse.com> wrote:
> > >
> > > On Mon 15-02-21 20:00:07, Muchun Song wrote:
> > > > On Mon, Feb 15, 2021 at 7:51 PM Muchun Song <songmuchun@bytedance.com> wrote:
> > > > >
> > > > > On Mon, Feb 15, 2021 at 6:33 PM Michal Hocko <mhocko@suse.com> wrote:
> > > > > >
> > > > > > On Mon 15-02-21 18:05:06, Muchun Song wrote:
> > > > > > > On Fri, Feb 12, 2021 at 11:32 PM Michal Hocko <mhocko@suse.com> wrote:
> > > > > > [...]
> > > > > > > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
> > > > > > > > > +{
> > > > > > > > > +     int ret;
> > > > > > > > > +     unsigned long vmemmap_addr = (unsigned long)head;
> > > > > > > > > +     unsigned long vmemmap_end, vmemmap_reuse;
> > > > > > > > > +
> > > > > > > > > +     if (!free_vmemmap_pages_per_hpage(h))
> > > > > > > > > +             return 0;
> > > > > > > > > +
> > > > > > > > > +     vmemmap_addr += RESERVE_VMEMMAP_SIZE;
> > > > > > > > > +     vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
> > > > > > > > > +     vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
> > > > > > > > > +
> > > > > > > > > +     /*
> > > > > > > > > +      * The pages which the vmemmap virtual address range [@vmemmap_addr,
> > > > > > > > > +      * @vmemmap_end) are mapped to are freed to the buddy allocator, and
> > > > > > > > > +      * the range is mapped to the page which @vmemmap_reuse is mapped to.
> > > > > > > > > +      * When a HugeTLB page is freed to the buddy allocator, previously
> > > > > > > > > +      * discarded vmemmap pages must be allocated and remapping.
> > > > > > > > > +      */
> > > > > > > > > +     ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
> > > > > > > > > +                               GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
> > > > > > > >
> > > > > > > > I do not think that this is a good allocation mode. GFP_ATOMIC is a non
> > > > > > > > sleeping allocation and a medium memory pressure might cause it to
> > > > > > > > fail prematurely. I do not think this is really an atomic context which
> > > > > > > > couldn't afford memory reclaim. I also do not think we want to grant
> > > > > > >
> > > > > > > Because alloc_huge_page_vmemmap is called under hugetlb_lock
> > > > > > > now. So using GFP_ATOMIC indeed makes the code more simpler.
> > > > > >
> > > > > > You can have a preallocated list of pages prior taking the lock.
> > > > >
> > > > > A discussion about this can refer to here:
> > > > >
> > > > > https://patchwork.kernel.org/project/linux-mm/patch/20210117151053.24600-5-songmuchun@bytedance.com/
> > > > >
> > > > > > Moreover do we want to manipulate vmemmaps from under spinlock in
> > > > > > general. I have to say I have missed that detail when reviewing. Need to
> > > > > > think more.
> > > > > >
> > > > > > > From the document of the kernel, I learned that __GFP_NOMEMALLOC
> > > > > > > can be used to explicitly forbid access to emergency reserves. So if
> > > > > > > we do not want to use the reserve memory. How about replacing it to
> > > > > > >
> > > > > > > GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_THISNODE
> > > > > >
> > > > > > The whole point of GFP_ATOMIC is to grant access to memory reserves so
> > > > > > the above is quite dubious. If you do not want access to memory reserves
> > > > >
> > > > > Look at the code of gfp_to_alloc_flags().
> > > > >
> > > > > static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask)
> > > > > {
> > > > >         [...]
> > > > >         if (gfp_mask & __GFP_ATOMIC) {
> > > > >         /*
> > > > >          * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
> > > > >          * if it can't schedule.
> > > > >          */
> > > > >         if (!(gfp_mask & __GFP_NOMEMALLOC))
> > > > >                 alloc_flags |= ALLOC_HARDER;
> > > > >        [...]
> > > > > }
> > > > >
> > > > > Seems to allow this operation (GFP_ATOMIC | __GFP_NOMEMALLOC).
> > >
> > > Please read my response again more carefully. I am not claiming that
> > > combination is not allowed. I have said it doesn't make any sense in
> > > this context.
> >
> > I see you are worried that using GFP_ATOMIC will use reverse memory
> > unlimited. So I think that __GFP_NOMEMALLOC may be suitable for us.
> > Sorry, I may not understand the point you said. What I missed?
>
> OK, let me try to explain again. GFP_ATOMIC is not only a non-sleeping
> allocation request. It also grants access to memory reserves. The later
> is a bit more involved because there are more layers of memory reserves
> to access but that is not really important. Non-sleeping semantic can be
> achieved by GFP_NOWAIT which will not grant access to reserves unless
> explicitly stated - e.g. by __GFP_HIGH or __GFP_ATOMIC.
> Is that more clear?
>
> Now again why I do not think access to memory reserves is suitable.
> Hugetlb pages can be released in a large batches and that might cause a
> peak depletion of memory reserves which are normally used by other
> consumers as well. Other GFP_ATOMIC users might see allocation failures.
> Those shouldn't be really fatal as nobody should be relying on those and
> a failure usually mean a hand over to a different, less constrained,
> context. So this concern is more about a more well behaved behavior from
> the hugetlb side than a correctness.
> Is that more clear?

Ok. It is very clear. Very thanks for your patient explanations.

>
> There shouldn't be any real reason why the memory allocation for
> vmemmaps, or handling vmemmap in general, has to be done from within the
> hugetlb lock and therefore requiring a non-sleeping semantic. All that
> can be deferred to a more relaxed context. If you want to make a

Yeah, you are right. We can put the freeing hugetlb routine to a
workqueue. Just like I do in the previous version (before v13) patch.
I will pick up these patches.

> GFP_NOWAIT optimistic attempt in the direct free path then no problem
> but you have to expect failures under memory pressure. If you want to
> have a more robust allocation request then you have to go outside of the
> spin lock and use GFP_KERNEL | __GFP_NORETRY or GFP_KERNEL |
> __GFP_RETRY_MAYFAIL depending on how hard you want to try.
> __GFP_THISNODE makes a slight difference here but something that I would
> recommend not depending on.
> Is that more clear?

OK. I will use GFP_KERNEL instead of GFP_ATOMIC. Thanks for your
suggestions.


> --
> Michal Hocko
> SUSE Labs
Michal Hocko Feb. 15, 2021, 4:27 p.m. UTC | #13
On Mon 15-02-21 23:36:49, Muchun Song wrote:
[...]
> > There shouldn't be any real reason why the memory allocation for
> > vmemmaps, or handling vmemmap in general, has to be done from within the
> > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > can be deferred to a more relaxed context. If you want to make a
> 
> Yeah, you are right. We can put the freeing hugetlb routine to a
> workqueue. Just like I do in the previous version (before v13) patch.
> I will pick up these patches.

I haven't seen your v13 and I will unlikely have time to revisit that
version. I just wanted to point out that the actual allocation doesn't
have to happen from under the spinlock. There are multiple ways to go
around that. Dropping the lock would be one of them. Preallocation
before the spin lock is taken is another. WQ is certainly an option but
I would take it as the last resort when other paths are not feasible.
Muchun Song Feb. 15, 2021, 5:48 p.m. UTC | #14
On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Mon 15-02-21 23:36:49, Muchun Song wrote:
> [...]
> > > There shouldn't be any real reason why the memory allocation for
> > > vmemmaps, or handling vmemmap in general, has to be done from within the
> > > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > > can be deferred to a more relaxed context. If you want to make a
> >
> > Yeah, you are right. We can put the freeing hugetlb routine to a
> > workqueue. Just like I do in the previous version (before v13) patch.
> > I will pick up these patches.
>
> I haven't seen your v13 and I will unlikely have time to revisit that
> version. I just wanted to point out that the actual allocation doesn't
> have to happen from under the spinlock. There are multiple ways to go
> around that. Dropping the lock would be one of them. Preallocation
> before the spin lock is taken is another. WQ is certainly an option but
> I would take it as the last resort when other paths are not feasible.
>

"Dropping the lock" and "Preallocation before the spin lock" can limit
the context of put_page to non-atomic context. I am not sure if there
is a page puted somewhere under an atomic context. e.g. compaction.
I am not an expert on this.

> --
> Michal Hocko
> SUSE Labs
Muchun Song Feb. 15, 2021, 6:19 p.m. UTC | #15
On Tue, Feb 16, 2021 at 1:48 AM Muchun Song <songmuchun@bytedance.com> wrote:
>
> On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Mon 15-02-21 23:36:49, Muchun Song wrote:
> > [...]
> > > > There shouldn't be any real reason why the memory allocation for
> > > > vmemmaps, or handling vmemmap in general, has to be done from within the
> > > > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > > > can be deferred to a more relaxed context. If you want to make a
> > >
> > > Yeah, you are right. We can put the freeing hugetlb routine to a
> > > workqueue. Just like I do in the previous version (before v13) patch.
> > > I will pick up these patches.
> >
> > I haven't seen your v13 and I will unlikely have time to revisit that
> > version. I just wanted to point out that the actual allocation doesn't
> > have to happen from under the spinlock. There are multiple ways to go
> > around that. Dropping the lock would be one of them. Preallocation
> > before the spin lock is taken is another. WQ is certainly an option but
> > I would take it as the last resort when other paths are not feasible.
> >
>
> "Dropping the lock" and "Preallocation before the spin lock" can limit
> the context of put_page to non-atomic context. I am not sure if there
> is a page puted somewhere under an atomic context. e.g. compaction.
> I am not an expert on this.

Using GFP_KERNEL will also use the current task cpuset to allocate
memory. Do we have an interface to ignore current task cpuset?If not,
WQ may be the only option and it also will not limit the context of
put_page. Right?

>
> > --
> > Michal Hocko
> > SUSE Labs
Michal Hocko Feb. 15, 2021, 7:02 p.m. UTC | #16
On Tue 16-02-21 01:48:29, Muchun Song wrote:
> On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Mon 15-02-21 23:36:49, Muchun Song wrote:
> > [...]
> > > > There shouldn't be any real reason why the memory allocation for
> > > > vmemmaps, or handling vmemmap in general, has to be done from within the
> > > > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > > > can be deferred to a more relaxed context. If you want to make a
> > >
> > > Yeah, you are right. We can put the freeing hugetlb routine to a
> > > workqueue. Just like I do in the previous version (before v13) patch.
> > > I will pick up these patches.
> >
> > I haven't seen your v13 and I will unlikely have time to revisit that
> > version. I just wanted to point out that the actual allocation doesn't
> > have to happen from under the spinlock. There are multiple ways to go
> > around that. Dropping the lock would be one of them. Preallocation
> > before the spin lock is taken is another. WQ is certainly an option but
> > I would take it as the last resort when other paths are not feasible.
> >
> 
> "Dropping the lock" and "Preallocation before the spin lock" can limit
> the context of put_page to non-atomic context. I am not sure if there
> is a page puted somewhere under an atomic context. e.g. compaction.
> I am not an expert on this.

Then do a due research or ask for a help from the MM community. Do
not just try to go around harder problems and somehow duct tape a
solution. I am sorry for sounding harsh here but this is a repetitive
pattern.

Now to the merit. put_page can indeed be called from all sorts of
contexts. And it might be indeed impossible to guarantee that hugetlb
pages are never freed up from an atomic context. Requiring that would be
even hard to maintain longterm. There are ways around that, I believe,
though.

The most simple one that I can think of right now would be using
in_atomic() rather than in_task() check free_huge_page. IIRC recent
changes would allow in_atomic to be reliable also on !PREEMPT kernels
(via RCU tree, not sure where this stands right now). That would make
__free_huge_page always run in a non-atomic context which sounds like an
easy enough solution.
Another way would be to keep a pool of ready pages to use in case of
GFP_NOWAIT allocation fails and have means to keep that pool replenished
when needed. Would it be feasible to reused parts of the freed page in
the worst case?
Michal Hocko Feb. 15, 2021, 7:39 p.m. UTC | #17
On Tue 16-02-21 02:19:20, Muchun Song wrote:
> On Tue, Feb 16, 2021 at 1:48 AM Muchun Song <songmuchun@bytedance.com> wrote:
> >
> > On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
> > >
> > > On Mon 15-02-21 23:36:49, Muchun Song wrote:
> > > [...]
> > > > > There shouldn't be any real reason why the memory allocation for
> > > > > vmemmaps, or handling vmemmap in general, has to be done from within the
> > > > > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > > > > can be deferred to a more relaxed context. If you want to make a
> > > >
> > > > Yeah, you are right. We can put the freeing hugetlb routine to a
> > > > workqueue. Just like I do in the previous version (before v13) patch.
> > > > I will pick up these patches.
> > >
> > > I haven't seen your v13 and I will unlikely have time to revisit that
> > > version. I just wanted to point out that the actual allocation doesn't
> > > have to happen from under the spinlock. There are multiple ways to go
> > > around that. Dropping the lock would be one of them. Preallocation
> > > before the spin lock is taken is another. WQ is certainly an option but
> > > I would take it as the last resort when other paths are not feasible.
> > >
> >
> > "Dropping the lock" and "Preallocation before the spin lock" can limit
> > the context of put_page to non-atomic context. I am not sure if there
> > is a page puted somewhere under an atomic context. e.g. compaction.
> > I am not an expert on this.
> 
> Using GFP_KERNEL will also use the current task cpuset to allocate
> memory. Do we have an interface to ignore current task cpuset?If not,
> WQ may be the only option and it also will not limit the context of
> put_page. Right?

Well, GFP_KERNEL is constrained to the task cpuset only if the said
cpuset is hardwalled IIRC. But I do not see why this is a problem.
Muchun Song Feb. 16, 2021, 4:34 a.m. UTC | #18
On Tue, Feb 16, 2021 at 3:39 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Tue 16-02-21 02:19:20, Muchun Song wrote:
> > On Tue, Feb 16, 2021 at 1:48 AM Muchun Song <songmuchun@bytedance.com> wrote:
> > >
> > > On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
> > > >
> > > > On Mon 15-02-21 23:36:49, Muchun Song wrote:
> > > > [...]
> > > > > > There shouldn't be any real reason why the memory allocation for
> > > > > > vmemmaps, or handling vmemmap in general, has to be done from within the
> > > > > > hugetlb lock and therefore requiring a non-sleeping semantic. All that
> > > > > > can be deferred to a more relaxed context. If you want to make a
> > > > >
> > > > > Yeah, you are right. We can put the freeing hugetlb routine to a
> > > > > workqueue. Just like I do in the previous version (before v13) patch.
> > > > > I will pick up these patches.
> > > >
> > > > I haven't seen your v13 and I will unlikely have time to revisit that
> > > > version. I just wanted to point out that the actual allocation doesn't
> > > > have to happen from under the spinlock. There are multiple ways to go
> > > > around that. Dropping the lock would be one of them. Preallocation
> > > > before the spin lock is taken is another. WQ is certainly an option but
> > > > I would take it as the last resort when other paths are not feasible.
> > > >
> > >
> > > "Dropping the lock" and "Preallocation before the spin lock" can limit
> > > the context of put_page to non-atomic context. I am not sure if there
> > > is a page puted somewhere under an atomic context. e.g. compaction.
> > > I am not an expert on this.
> >
> > Using GFP_KERNEL will also use the current task cpuset to allocate
> > memory. Do we have an interface to ignore current task cpuset?If not,
> > WQ may be the only option and it also will not limit the context of
> > put_page. Right?
>
> Well, GFP_KERNEL is constrained to the task cpuset only if the said
> cpuset is hardwalled IIRC. But I do not see why this is a problem.

I mean that if there are more than one node in the system,
but the current task cpuset only allows one node. If current
node has no memory and other nodes have enough memory.
We can fail to allocate vmemmap pages. But actually it is
suitable to allocate vmemmap pages from other nodes.
Right?

Thanks.

> --
> Michal Hocko
> SUSE Labs
David Hildenbrand Feb. 16, 2021, 8:13 a.m. UTC | #19
On 15.02.21 20:02, Michal Hocko wrote:
> On Tue 16-02-21 01:48:29, Muchun Song wrote:
>> On Tue, Feb 16, 2021 at 12:28 AM Michal Hocko <mhocko@suse.com> wrote:
>>>
>>> On Mon 15-02-21 23:36:49, Muchun Song wrote:
>>> [...]
>>>>> There shouldn't be any real reason why the memory allocation for
>>>>> vmemmaps, or handling vmemmap in general, has to be done from within the
>>>>> hugetlb lock and therefore requiring a non-sleeping semantic. All that
>>>>> can be deferred to a more relaxed context. If you want to make a
>>>>
>>>> Yeah, you are right. We can put the freeing hugetlb routine to a
>>>> workqueue. Just like I do in the previous version (before v13) patch.
>>>> I will pick up these patches.
>>>
>>> I haven't seen your v13 and I will unlikely have time to revisit that
>>> version. I just wanted to point out that the actual allocation doesn't
>>> have to happen from under the spinlock. There are multiple ways to go
>>> around that. Dropping the lock would be one of them. Preallocation
>>> before the spin lock is taken is another. WQ is certainly an option but
>>> I would take it as the last resort when other paths are not feasible.
>>>
>>
>> "Dropping the lock" and "Preallocation before the spin lock" can limit
>> the context of put_page to non-atomic context. I am not sure if there
>> is a page puted somewhere under an atomic context. e.g. compaction.
>> I am not an expert on this.
> 
> Then do a due research or ask for a help from the MM community. Do
> not just try to go around harder problems and somehow duct tape a
> solution. I am sorry for sounding harsh here but this is a repetitive
> pattern.
> 
> Now to the merit. put_page can indeed be called from all sorts of
> contexts. And it might be indeed impossible to guarantee that hugetlb
> pages are never freed up from an atomic context. Requiring that would be
> even hard to maintain longterm. There are ways around that, I believe,
> though.
> 
> The most simple one that I can think of right now would be using
> in_atomic() rather than in_task() check free_huge_page. IIRC recent
> changes would allow in_atomic to be reliable also on !PREEMPT kernels
> (via RCU tree, not sure where this stands right now). That would make
> __free_huge_page always run in a non-atomic context which sounds like an
> easy enough solution.
> Another way would be to keep a pool of ready pages to use in case of
> GFP_NOWAIT allocation fails and have means to keep that pool replenished
> when needed. Would it be feasible to reused parts of the freed page in
> the worst case?

As already discussed, this is only possible when the huge page does not 
reside on ZONE_MOVABLE/CMA.

In addition, we can no longer form a huge page at that memory location ever.
Michal Hocko Feb. 16, 2021, 8:15 a.m. UTC | #20
On Tue 16-02-21 12:34:41, Muchun Song wrote:
> On Tue, Feb 16, 2021 at 3:39 AM Michal Hocko <mhocko@suse.com> wrote:
[...]
> > > Using GFP_KERNEL will also use the current task cpuset to allocate
> > > memory. Do we have an interface to ignore current task cpuset?If not,
> > > WQ may be the only option and it also will not limit the context of
> > > put_page. Right?
> >
> > Well, GFP_KERNEL is constrained to the task cpuset only if the said
> > cpuset is hardwalled IIRC. But I do not see why this is a problem.
> 
> I mean that if there are more than one node in the system,
> but the current task cpuset only allows one node.

How would that cpuset get a huge pages from a node which is not part of
the cpuset? Well, that would be possible if the cpuset was dynamic but I
am not sure that such a configuration would be very sensible along with
hardwall setup.

> If current
> node has no memory and other nodes have enough memory.
> We can fail to allocate vmemmap pages. But actually it is
> suitable to allocate vmemmap pages from other nodes.
> Right?

Falling back to a different node would be very suboptimal because then
you would have vmemmap back by remote pages. We do not want that.
David Hildenbrand Feb. 16, 2021, 8:20 a.m. UTC | #21
>> If current
>> node has no memory and other nodes have enough memory.
>> We can fail to allocate vmemmap pages. But actually it is
>> suitable to allocate vmemmap pages from other nodes.
>> Right?
> 
> Falling back to a different node would be very suboptimal because then
> you would have vmemmap back by remote pages. We do not want that.

... and we even warn when this happens right now:

mm/sparse-vmemmap.c:vmemmap_verify()
Michal Hocko Feb. 16, 2021, 8:21 a.m. UTC | #22
On Tue 16-02-21 09:13:09, David Hildenbrand wrote:
> On 15.02.21 20:02, Michal Hocko wrote:
> > Would it be feasible to reused parts of the freed page in
> > the worst case?
> 
> As already discussed, this is only possible when the huge page does not
> reside on ZONE_MOVABLE/CMA.

Right. But usually this is not the case so it would be at least
something.

> In addition, we can no longer form a huge page at that memory location ever.

Yes, that's why I am saying in the worst case. E.g. when dissolving is
really necessary like in hwpoison case.

Maybe we are really far from needing something like that. I just wanted
to mention this option and I was not aware this has been discussed
previously.
Muchun Song Feb. 16, 2021, 9:03 a.m. UTC | #23
On Tue, Feb 16, 2021 at 4:15 PM Michal Hocko <mhocko@suse.com> wrote:
>
> On Tue 16-02-21 12:34:41, Muchun Song wrote:
> > On Tue, Feb 16, 2021 at 3:39 AM Michal Hocko <mhocko@suse.com> wrote:
> [...]
> > > > Using GFP_KERNEL will also use the current task cpuset to allocate
> > > > memory. Do we have an interface to ignore current task cpuset?If not,
> > > > WQ may be the only option and it also will not limit the context of
> > > > put_page. Right?
> > >
> > > Well, GFP_KERNEL is constrained to the task cpuset only if the said
> > > cpuset is hardwalled IIRC. But I do not see why this is a problem.
> >
> > I mean that if there are more than one node in the system,
> > but the current task cpuset only allows one node.
>
> How would that cpuset get a huge pages from a node which is not part of
> the cpuset? Well, that would be possible if the cpuset was dynamic but I
> am not sure that such a configuration would be very sensible along with
> hardwall setup.

Got it. I didn't realize this before. Thanks.

>
> > If current
> > node has no memory and other nodes have enough memory.
> > We can fail to allocate vmemmap pages. But actually it is
> > suitable to allocate vmemmap pages from other nodes.
> > Right?
>
> Falling back to a different node would be very suboptimal because then
> you would have vmemmap back by remote pages. We do not want that.
> --
> Michal Hocko
> SUSE Labs
Mike Kravetz Feb. 16, 2021, 7:44 p.m. UTC | #24
On 2/15/21 8:27 AM, Michal Hocko wrote:
> On Mon 15-02-21 23:36:49, Muchun Song wrote:
> [...]
>>> There shouldn't be any real reason why the memory allocation for
>>> vmemmaps, or handling vmemmap in general, has to be done from within the
>>> hugetlb lock and therefore requiring a non-sleeping semantic. All that
>>> can be deferred to a more relaxed context. If you want to make a
>>
>> Yeah, you are right. We can put the freeing hugetlb routine to a
>> workqueue. Just like I do in the previous version (before v13) patch.
>> I will pick up these patches.
> 
> I haven't seen your v13 and I will unlikely have time to revisit that
> version. I just wanted to point out that the actual allocation doesn't
> have to happen from under the spinlock. There are multiple ways to go
> around that. Dropping the lock would be one of them. Preallocation
> before the spin lock is taken is another. WQ is certainly an option but
> I would take it as the last resort when other paths are not feasible.

Sorry for jumping in late, Monday was a US holiday ...

IIRC, the point of moving the vmemmap allocations under the hugetlb_lock
was just for simplicity.  The idea was to modify the allocations to be
non-blocking so that allocating pages and restoring vmemmap could be done
as part of normal huge page freeing where we are holding the lock.  Perhaps
that is too simplistic of an approach.

IMO, using the workque approach as done in previous patches introduces
too much complexity.

Michal did bring up the question "Do we really want to do all the vmemmap
allocation (even non-blocking) and manipulation under the hugetlb lock?
I'm thinking the answer may be no.  For 1G pages, this will require 4094
calls to alloc_pages.  Even with non-blocking calls this seems like a long
time.

If we are not going to do the allocations under the lock, then we will need
to either preallocate or take the workqueue approach.  One complication with
preallocation is that we do not for sure we will be freeing the huge page
to buddy until we take the hugetlb_lock.  This is because the decision to
free or not is based on counters protected by the lock.  We could of course
check counters without the lock to guess if we will be freeing the page,
and then check again after acquiring the lock.  This may not be too bad in
the case of freeing a single page, but would become more complex when doing
bulk freeing.  After a little thought, the workqueue approach may even end
up simpler.  However, I would suggest a very simple workqueue implementation
with non-blocking allocations.  If we can not quickly get vmemmap pages,
put the page back on the hugetlb free list and treat as a surplus page.
Michal Hocko Feb. 17, 2021, 8:13 a.m. UTC | #25
On Tue 16-02-21 11:44:34, Mike Kravetz wrote:
[...]
> If we are not going to do the allocations under the lock, then we will need
> to either preallocate or take the workqueue approach.

We can still drop the lock temporarily right? As we already do before
calling destroy_compound_gigantic_page...
Mike Kravetz Feb. 18, 2021, 1 a.m. UTC | #26
On 2/17/21 12:13 AM, Michal Hocko wrote:
> On Tue 16-02-21 11:44:34, Mike Kravetz wrote:
> [...]
>> If we are not going to do the allocations under the lock, then we will need
>> to either preallocate or take the workqueue approach.
> 
> We can still drop the lock temporarily right? As we already do before
> calling destroy_compound_gigantic_page...
> 

Yes we can.  I forgot about that.

Actually, very little of what update_and_free_page does needs to be done
under the lock.  Perhaps, just decrementing the global count and clearing
the destructor so PageHuge() is no longer true.
Muchun Song Feb. 18, 2021, 3:20 a.m. UTC | #27
On Thu, Feb 18, 2021 at 9:00 AM Mike Kravetz <mike.kravetz@oracle.com> wrote:
>
> On 2/17/21 12:13 AM, Michal Hocko wrote:
> > On Tue 16-02-21 11:44:34, Mike Kravetz wrote:
> > [...]
> >> If we are not going to do the allocations under the lock, then we will need
> >> to either preallocate or take the workqueue approach.
> >
> > We can still drop the lock temporarily right? As we already do before
> > calling destroy_compound_gigantic_page...
> >
>
> Yes we can.  I forgot about that.
>
> Actually, very little of what update_and_free_page does needs to be done
> under the lock.  Perhaps, just decrementing the global count and clearing
> the destructor so PageHuge() is no longer true.

Right. I have another question about using GFP flags. Michal
suggested using GFP_KERNEL instead of GFP_ATOMIC to
save reserve memory. From your last email, you suggested
using non-blocking allocation GFP flags (perhaps GFP_NOWAIT).

Hi Mike and Michal,

What is the consensus we finally reached? Thanks.
Michal Hocko Feb. 18, 2021, 8:21 a.m. UTC | #28
On Thu 18-02-21 11:20:51, Muchun Song wrote:
> On Thu, Feb 18, 2021 at 9:00 AM Mike Kravetz <mike.kravetz@oracle.com> wrote:
> >
> > On 2/17/21 12:13 AM, Michal Hocko wrote:
> > > On Tue 16-02-21 11:44:34, Mike Kravetz wrote:
> > > [...]
> > >> If we are not going to do the allocations under the lock, then we will need
> > >> to either preallocate or take the workqueue approach.
> > >
> > > We can still drop the lock temporarily right? As we already do before
> > > calling destroy_compound_gigantic_page...
> > >
> >
> > Yes we can.  I forgot about that.
> >
> > Actually, very little of what update_and_free_page does needs to be done
> > under the lock.  Perhaps, just decrementing the global count and clearing
> > the destructor so PageHuge() is no longer true.
> 
> Right. I have another question about using GFP flags. Michal
> suggested using GFP_KERNEL instead of GFP_ATOMIC to
> save reserve memory. From your last email, you suggested
> using non-blocking allocation GFP flags (perhaps GFP_NOWAIT).
> 
> Hi Mike and Michal,
> 
> What is the consensus we finally reached? Thanks.

If the lock can be dropped and you make sure the final put on page is
not called from an atomic context then use (for starter)
GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE. I have intentionaly dropped
__GFP_NOWARN because likely want to hear about the failure so that we
can evaluate how often this happens.

This would be my recommendation.
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index d7dddf334779..33c5911afe18 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2981,6 +2981,8 @@  static inline void print_vma_addr(char *prefix, unsigned long rip)
 
 void vmemmap_remap_free(unsigned long start, unsigned long end,
 			unsigned long reuse);
+int vmemmap_remap_alloc(unsigned long start, unsigned long end,
+			unsigned long reuse, gfp_t gfp_mask);
 
 void *sparse_buffer_alloc(unsigned long size);
 struct page * __populate_section_memmap(unsigned long pfn,
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 4cfca27c6d32..69dcbaa2e6db 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1397,16 +1397,26 @@  static void __free_huge_page(struct page *page)
 		h->resv_huge_pages++;
 
 	if (HPageTemporary(page)) {
-		list_del(&page->lru);
 		ClearHPageTemporary(page);
+
+		if (alloc_huge_page_vmemmap(h, page)) {
+			h->surplus_huge_pages++;
+			h->surplus_huge_pages_node[nid]++;
+			goto enqueue;
+		}
+		list_del(&page->lru);
 		update_and_free_page(h, page);
 	} else if (h->surplus_huge_pages_node[nid]) {
+		if (alloc_huge_page_vmemmap(h, page))
+			goto enqueue;
+
 		/* remove the page from active list */
 		list_del(&page->lru);
 		update_and_free_page(h, page);
 		h->surplus_huge_pages--;
 		h->surplus_huge_pages_node[nid]--;
 	} else {
+enqueue:
 		arch_clear_hugepage_flags(page);
 		enqueue_huge_page(h, page);
 	}
@@ -1693,6 +1703,10 @@  static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
 			struct page *page =
 				list_entry(h->hugepage_freelists[node].next,
 					  struct page, lru);
+
+			if (alloc_huge_page_vmemmap(h, page))
+				break;
+
 			list_del(&page->lru);
 			h->free_huge_pages--;
 			h->free_huge_pages_node[node]--;
@@ -1760,6 +1774,9 @@  int dissolve_free_huge_page(struct page *page)
 			goto retry;
 		}
 
+		if (alloc_huge_page_vmemmap(h, head))
+			goto out;
+
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
 		 * which makes any subpages rather than the error page reusable.
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 0209b736e0b4..3d85e3ab7caa 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -169,6 +169,8 @@ 
  * (last) level. So this type of HugeTLB page can be optimized only when its
  * size of the struct page structs is greater than 2 pages.
  */
+#define pr_fmt(fmt)	"HugeTLB: " fmt
+
 #include "hugetlb_vmemmap.h"
 
 /*
@@ -198,6 +200,34 @@  static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
 	return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
 }
 
+int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
+{
+	int ret;
+	unsigned long vmemmap_addr = (unsigned long)head;
+	unsigned long vmemmap_end, vmemmap_reuse;
+
+	if (!free_vmemmap_pages_per_hpage(h))
+		return 0;
+
+	vmemmap_addr += RESERVE_VMEMMAP_SIZE;
+	vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
+	vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
+
+	/*
+	 * The pages which the vmemmap virtual address range [@vmemmap_addr,
+	 * @vmemmap_end) are mapped to are freed to the buddy allocator, and
+	 * the range is mapped to the page which @vmemmap_reuse is mapped to.
+	 * When a HugeTLB page is freed to the buddy allocator, previously
+	 * discarded vmemmap pages must be allocated and remapping.
+	 */
+	ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
+				  GFP_ATOMIC | __GFP_NOWARN | __GFP_THISNODE);
+	if (ret == -ENOMEM)
+		pr_info("cannot alloc vmemmap pages\n");
+
+	return ret;
+}
+
 void free_huge_page_vmemmap(struct hstate *h, struct page *head)
 {
 	unsigned long vmemmap_addr = (unsigned long)head;
diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h
index 6923f03534d5..e5547d53b9f5 100644
--- a/mm/hugetlb_vmemmap.h
+++ b/mm/hugetlb_vmemmap.h
@@ -11,8 +11,14 @@ 
 #include <linux/hugetlb.h>
 
 #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP
+int alloc_huge_page_vmemmap(struct hstate *h, struct page *head);
 void free_huge_page_vmemmap(struct hstate *h, struct page *head);
 #else
+static inline int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
+{
+	return 0;
+}
+
 static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head)
 {
 }
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index d3076a7a3783..60fc6cd6cd23 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -40,7 +40,8 @@ 
  * @remap_pte:		called for each lowest-level entry (PTE).
  * @reuse_page:		the page which is reused for the tail vmemmap pages.
  * @reuse_addr:		the virtual address of the @reuse_page page.
- * @vmemmap_pages:	the list head of the vmemmap pages that can be freed.
+ * @vmemmap_pages:	the list head of the vmemmap pages that can be freed
+ *			or is mapped from.
  */
 struct vmemmap_remap_walk {
 	void (*remap_pte)(pte_t *pte, unsigned long addr,
@@ -237,6 +238,78 @@  void vmemmap_remap_free(unsigned long start, unsigned long end,
 	free_vmemmap_page_list(&vmemmap_pages);
 }
 
+static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
+				struct vmemmap_remap_walk *walk)
+{
+	pgprot_t pgprot = PAGE_KERNEL;
+	struct page *page;
+	void *to;
+
+	BUG_ON(pte_page(*pte) != walk->reuse_page);
+
+	page = list_first_entry(walk->vmemmap_pages, struct page, lru);
+	list_del(&page->lru);
+	to = page_to_virt(page);
+	copy_page(to, (void *)walk->reuse_addr);
+
+	set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
+}
+
+static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
+				   gfp_t gfp_mask, struct list_head *list)
+{
+	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+	int nid = page_to_nid((struct page *)start);
+	struct page *page, *next;
+
+	while (nr_pages--) {
+		page = alloc_pages_node(nid, gfp_mask, 0);
+		if (!page)
+			goto out;
+		list_add_tail(&page->lru, list);
+	}
+
+	return 0;
+out:
+	list_for_each_entry_safe(page, next, list, lru)
+		__free_pages(page, 0);
+	return -ENOMEM;
+}
+
+/**
+ * vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end)
+ *			 to the page which is from the @vmemmap_pages
+ *			 respectively.
+ * @start:	start address of the vmemmap virtual address range that we want
+ *		to remap.
+ * @end:	end address of the vmemmap virtual address range that we want to
+ *		remap.
+ * @reuse:	reuse address.
+ * @gpf_mask:	GFP flag for allocating vmemmap pages.
+ */
+int vmemmap_remap_alloc(unsigned long start, unsigned long end,
+			unsigned long reuse, gfp_t gfp_mask)
+{
+	LIST_HEAD(vmemmap_pages);
+	struct vmemmap_remap_walk walk = {
+		.remap_pte	= vmemmap_restore_pte,
+		.reuse_addr	= reuse,
+		.vmemmap_pages	= &vmemmap_pages,
+	};
+
+	/* See the comment in the vmemmap_remap_free(). */
+	BUG_ON(start - reuse != PAGE_SIZE);
+
+	might_sleep_if(gfpflags_allow_blocking(gfp_mask));
+
+	if (alloc_vmemmap_page_list(start, end, gfp_mask, &vmemmap_pages))
+		return -ENOMEM;
+
+	vmemmap_remap_range(reuse, end, &walk);
+
+	return 0;
+}
+
 /*
  * Allocate a block of memory to be used to back the virtual memory map
  * or to back the page tables that are used to create the mapping.