Skip to contentSkip to conversation

extend vmalloc support for constrained allocations

PATCH Superseded Michal Hocko 4 participants 26 messages Oct 25, 2021 – Oct 29, 2021

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover extend vmalloc support for constrained allocations

Hi,
this has been posted as a RFC previously [1] and it seems there was no fundamental disagreement about the approach so I am dropping RFC and I have also integrated some feedback from that discussion.

Based on a recent discussion with Dave and Neil [2] I have tried to implement NOFS, NOIO, NOFAIL support for the vmalloc to make life of kvmalloc users easier.

A requirement for NOFAIL support for kvmalloc was new to me but this seems to be really needed by the xfs code.

NOFS/NOIO was a known and a long term problem which was hoped to be handled by the scope API. Those scope should have been used at the reclaim recursion boundaries both to document them and also to remove the necessity of NOFS/NOIO constrains for all allocations within that scope. Instead workarounds were developed to wrap a single allocation instead (like ceph_kvmalloc).

First patch implements NOFS/NOIO support for vmalloc. The second one adds NOFAIL support and the third one bundles all together into kvmalloc and drops ceph_kvmalloc which can use kvmalloc directly now.

Please note that this is RFC and I haven't done any testing on this yet. I hope I haven't missed anything in the vmalloc allocator. It would be really great if Christoph and Uladzislau could have a look.

Thanks!

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]

#1 lore raw

PATCH 1/4 mm/vmalloc: alloc GFP_NO{FS,IO} for vmalloc

From: Michal Hocko <[email protected]>

vmalloc historically hasn't supported GFP_NO{FS,IO} requests because page table allocations do not support externally provided gfp mask and performed GFP_KERNEL like allocations.

Since few years we have scope (memalloc_no{fs,io}_{save,restore}) APIs to enforce NOFS and NOIO constrains implicitly to all allocators within the scope. There was a hope that those scopes would be defined on a higher level when the reclaim recursion boundary starts/stops (e.g. when a lock required during the memory reclaim is required etc.). It seems that not all NOFS/NOIO users have adopted this approach and instead they have taken a workaround approach to wrap a single [k]vmalloc allocation by a scope API.

These workarounds do not serve the purpose of a better reclaim recursion documentation and reduction of explicit GFP_NO{FS,IO} usege so let's just provide them with the semantic they are asking for without a need for workarounds.

Add support for GFP_NOFS and GFP_NOIO to vmalloc directly. All internal allocations already comply with the given gfp_mask. The only current exception is vmap_pages_range which maps kernel page tables. Infer the proper scope API based on the given gfp mask.

---
 mm/vmalloc.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
mm/vmalloc.c +20 -2
index d77830ff604c..c6cc77d2f366 100644
@@ -2889,6 +2889,8 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned long array_size;
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
+	unsigned int flags;
+	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
 	gfp_mask |= __GFP_NOWARN;
@@ -2930,8 +2932,24 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		goto fail;
 	}
 
-	if (vmap_pages_range(addr, addr + size, prot, area->pages,
-			page_shift) < 0) {
+	/*
+	 * page tables allocations ignore external gfp mask, enforce it
+	 * by the scope API
+	 */
+	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+		flags = memalloc_nofs_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
+		flags = memalloc_noio_save();
+
+	ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+			page_shift);
+
+	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+		memalloc_nofs_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
+		memalloc_noio_restore(flags);
+
+	if (ret < 0) {
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, failed to map pages",
 			area->nr_pages * PAGE_SIZE);
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/4 mm/vmalloc: add support for __GFP_NOFAIL

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +9 -1
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();
 
-	ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+	do {
+		ret = vmap_pages_range(addr, addr + size, prot, area->pages,
 			page_shift);
+		if (ret < 0)
+			schedule_timeout_uninterruptible(1);
+	} while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 
 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);
@@ -3032,6 +3036,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, vm_struct allocation failed",
 			real_size);
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}
 		goto fail;
 	}
 
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 7 lines · Michal Hocko

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the

*large*

Quoted 35 lines

given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +5 -1
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
 		flags = memalloc_noio_save();
 
-	ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+	do {
+		ret = vmap_pages_range(addr, addr + size, prot, area->pages,
 			page_shift);
+		if (ret < 0)
+			schedule_timeout_uninterruptible(1);
+	} while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 
 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
 		memalloc_nofs_restore(flags);

mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3042

Quoted 7 lines
mm/vmalloc.c +4 -0
@@ -3032,6 +3036,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, vm_struct allocation failed",
 			real_size);
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}

Shouldn't the retry happen *before* the warning?

NeilBrown

Quoted 7 lines
mm/vmalloc.c +0 -0
 		goto fail;
 	}
 
Signature · 1 line
2.30.2
reviewer
#4 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 1 line · Neil Brown

On Tue, 26 Oct 2021, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3042

Quoted 8 lines
mm/vmalloc.c +4 -0
@@ -3032,6 +3036,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, vm_struct allocation failed",
 			real_size);
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}

Shouldn't the retry happen *before* the warning?

I've done it after to catch the "depleted or fragmented" vmalloc space. This is not related to the memory available and therefore it won't be handled by the oom killer. The error message shouldn't imply the vmalloc allocation failure IMHO but I am open to suggestions.

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 14 lines · Michal Hocko
Quoted 1 line · Neil Brown

On Tue, 26 Oct 2021, Michal Hocko wrote:

[...]

Quoted 10 lines

mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3042

Quoted 8 lines
mm/vmalloc.c +4 -0
@@ -3032,6 +3036,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, vm_struct allocation failed",
 			real_size);
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}

Shouldn't the retry happen *before* the warning?

Quoted 4 lines

I've done it after to catch the "depleted or fragmented" vmalloc space. This is not related to the memory available and therefore it won't be handled by the oom killer. The error message shouldn't imply the vmalloc allocation failure IMHO but I am open to suggestions.

The word "failed" does seem to imply what you don't want it to imply...

I guess it is reasonable to have this warning, but maybe add " -- retrying" if __GFP_NOFAIL.

Thanks,
NeilBrown

reviewer
#6 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 20 lines · Neil Brown
Quoted 14 lines · Michal Hocko
Quoted 1 line · Neil Brown

On Tue, 26 Oct 2021, Michal Hocko wrote:

[...]

Quoted 10 lines

Review void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3042

Quoted 8 lines
(quoted diff) +4 -0
@@ -3032,6 +3036,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, vm_struct allocation failed",
 			real_size);
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}

Shouldn't the retry happen *before* the warning?

Quoted 4 lines

I've done it after to catch the "depleted or fragmented" vmalloc space. This is not related to the memory available and therefore it won't be handled by the oom killer. The error message shouldn't imply the vmalloc allocation failure IMHO but I am open to suggestions.

Quoted 4 lines

The word "failed" does seem to imply what you don't want it to imply...

I guess it is reasonable to have this warning, but maybe add " -- retrying" if __GFP_NOFAIL.

I do not have a strong opinion on that. I can surely do

mm/vmalloc.c +4 -3
index 602649919a9d..3489928fafa2 100644
@@ -3033,10 +3033,11 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 				  VM_UNINITIALIZED | vm_flags, start, end, node,
 				  gfp_mask, caller);
 	if (!area) {
+		bool nofail = gfp_mask & __GFP_NOFAIL;
 		warn_alloc(gfp_mask, NULL,
-			"vmalloc error: size %lu, vm_struct allocation failed",
-			real_size);
-		if (gfp_mask & __GFP_NOFAIL) {
+			"vmalloc error: size %lu, vm_struct allocation failed%s",
+			real_size, (nofail) ? ". Retrying." : "");
+		if (nofail) {
 			schedule_timeout_uninterruptible(1);
 			goto again;
 		}
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#7 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

In reply to Michal Hocko
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}

<snip>

does not make any sense anymore.

2.
Can we combine two places where we handle __GFP_NOFAIL into one place? That would look like as more sorted out.

Signature · 1 line
Uladzislau Rezki
reviewer
#8 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 50 lines · Uladzislau Rezki
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}
Quoted 3 lines

<snip>

does not make any sense anymore.

Why? Allocations without __GFP_NOFAIL can still fail, no?

Quoted 3 lines

2.
Can we combine two places where we handle __GFP_NOFAIL into one place? That would look like as more sorted out.

I have to admit I am not really fluent at vmalloc code so I wanted to make the code as simple as possible. How would I unwind all the allocated memory (already allocated as GFP_NOFAIL) before retrying at __vmalloc_node_range (if that is what you suggest). And isn't that a bit wasteful?

Or did you have anything else in mind?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#9 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 53 lines · Michal Hocko
Quoted 52 lines · Uladzislau Rezki
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}

<snip>

Quoted 4 lines
Quoted 1 line

does not make any sense anymore.

Why? Allocations without __GFP_NOFAIL can still fail, no?

Right. I meant one thing but wrote slightly differently. In case of vmap_pages_range() fails(if __GFP_NOFAIL is set) should we emit any warning message? Because either we can recover on a future iteration or it stuck there infinitely so a user does not understand what happened. From the other hand this is how __GFP_NOFAIL works, hm..

Another thing, i see that schedule_timeout_uninterruptible(1) is invoked for all cases even when __GFP_NOFAIL is not set, in that scenario we do not want to wait, instead we should return back to a caller asap. Or am i missing something here?

Quoted 4 lines
Quoted 3 lines

2.
Can we combine two places where we handle __GFP_NOFAIL into one place? That would look like as more sorted out.

Quoted 8 lines

I have to admit I am not really fluent at vmalloc code so I wanted to make the code as simple as possible. How would I unwind all the allocated memory (already allocated as GFP_NOFAIL) before retrying at __vmalloc_node_range (if that is what you suggest). And isn't that a bit wasteful?

Or did you have anything else in mind?

It depends on how often all this can fail. But let me double check if such combining is easy.

--
Vlad Rezki

reviewer
#10 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 56 lines · Uladzislau Rezki
Quoted 55 lines · Michal Hocko
Quoted 53 lines · Uladzislau Rezki
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}

<snip>

does not make any sense anymore.

Quoted 7 lines
Quoted 2 lines

Why? Allocations without __GFP_NOFAIL can still fail, no?

Right. I meant one thing but wrote slightly differently. In case of vmap_pages_range() fails(if __GFP_NOFAIL is set) should we emit any warning message? Because either we can recover on a future iteration or it stuck there infinitely so a user does not understand what happened. From the other hand this is how __GFP_NOFAIL works, hm..

Yes, the page allocator doesn't warn either and I would like to keep this in sync.

Quoted 4 lines

Another thing, i see that schedule_timeout_uninterruptible(1) is invoked for all cases even when __GFP_NOFAIL is not set, in that scenario we do not want to wait, instead we should return back to a caller asap. Or am i missing something here?

OK, I will change that.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#11 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

In reply to Uladzislau Rezki
Quoted 79 lines · Uladzislau Rezki
Quoted 53 lines · Michal Hocko
Quoted 52 lines · Uladzislau Rezki
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}

<snip>

Quoted 4 lines
Quoted 1 line

does not make any sense anymore.

Why? Allocations without __GFP_NOFAIL can still fail, no?

Right. I meant one thing but wrote slightly differently. In case of vmap_pages_range() fails(if __GFP_NOFAIL is set) should we emit any warning message? Because either we can recover on a future iteration or it stuck there infinitely so a user does not understand what happened. From the other hand this is how __GFP_NOFAIL works, hm..

Another thing, i see that schedule_timeout_uninterruptible(1) is invoked for all cases even when __GFP_NOFAIL is not set, in that scenario we do not want to wait, instead we should return back to a caller asap. Or am i missing something here?

Quoted 10 lines
Quoted 3 lines

2.
Can we combine two places where we handle __GFP_NOFAIL into one place? That would look like as more sorted out.

I have to admit I am not really fluent at vmalloc code so I wanted to make the code as simple as possible. How would I unwind all the allocated memory (already allocated as GFP_NOFAIL) before retrying at __vmalloc_node_range (if that is what you suggest). And isn't that a bit wasteful?

Quoted 5 lines
Quoted 2 lines

Or did you have anything else in mind?

It depends on how often all this can fail. But let me double check if such combining is easy.

I mean something like below. The idea is to not spread the __GFP_NOFAIL across the vmalloc file keeping it in one solid place:

<snip>

mm/vmalloc.c +43 -10
index d77830ff604c..f4b7927e217e 100644
@@ -2889,8 +2889,14 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned long array_size;
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
 	unsigned int page_order;
+	unsigned long flags;
+	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+
+	/*
+	 * This is i do not understand why we do not want to see warning messages.
+	 */
 	gfp_mask |= __GFP_NOWARN;
 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
 		gfp_mask |= __GFP_HIGHMEM;
@@ -2930,8 +2936,23 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		goto fail;
 	}
 
-	if (vmap_pages_range(addr, addr + size, prot, area->pages,
-			page_shift) < 0) {
+	/*
+	 * page tables allocations ignore external gfp mask, enforce it
+	 * by the scope API
+	 */
+	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+		flags = memalloc_nofs_save();
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
+		flags = memalloc_noio_save();
+
+	ret = vmap_pages_range(addr, addr + size, prot, area->pages, page_shift);
+
+	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
+		memalloc_nofs_restore(flags);
+	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
+		memalloc_noio_restore(flags);
+
+	if (ret < 0) {
 		warn_alloc(gfp_mask, NULL,
 			"vmalloc error: size %lu, failed to map pages",
 			area->nr_pages * PAGE_SIZE);
@@ -2984,6 +3005,12 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 		return NULL;
 	}
 
+	/*
+	 * Suppress all warnings for __GFP_NOFAIL allocation.
+	 */
+	if (gfp_mask & __GFP_NOFAIL)
+		gfp_mask |= __GFP_NOWARN;
+
 	if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) {
 		unsigned long size_per_node;
 
@@ -3010,16 +3037,22 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 	area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
 				  VM_UNINITIALIZED | vm_flags, start, end, node,
 				  gfp_mask, caller);
-	if (!area) {
-		warn_alloc(gfp_mask, NULL,
-			"vmalloc error: size %lu, vm_struct allocation failed",
-			real_size);
-		goto fail;
-	}
+	if (area)
+		addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
+
+	if (!area || !addr) {
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}
+
+		if (!area)
+			warn_alloc(gfp_mask, NULL,
+				"vmalloc error: size %lu, vm_struct allocation failed",
+				real_size);
 
-	addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
-	if (!addr)
 		goto fail;
+	}
 
 	/*
 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED

<snip>

--
Vlad Rezki

reviewer
#12 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 96 lines · Uladzislau Rezki
Quoted 79 lines · Uladzislau Rezki
Quoted 53 lines · Michal Hocko
Quoted 52 lines · Uladzislau Rezki
Quoted 29 lines

From: Michal Hocko <[email protected]>

Dave Chinner has mentioned that some of the xfs code would benefit from kvmalloc support for __GFP_NOFAIL because they have allocations that cannot fail and they do not fit into a single page.

The larg part of the vmalloc implementation already complies with the given gfp flags so there is no work for those to be done. The area and page table allocations are an exception to that. Implement a retry loop for those.

Add a short sleep before retrying. 1 jiffy is a completely random timeout. Ideally the retry would wait for an explicit event - e.g. a change to the vmalloc space change if the failure was caused by the space fragmentation or depletion. But there are multiple different reasons to retry and this could become much more complex. Keep the retry simple for now and just sleep to prevent from hogging CPUs.

---
 mm/vmalloc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index c6cc77d2f366..602649919a9d 100644
@@ -2941,8 +2941,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2950

Quoted 10 lines
mm/vmalloc.c +5 -1
                flags = memalloc_noio_save();
 
-       ret = vmap_pages_range(addr, addr + size, prot, area->pages,
+       do {
+               ret = vmap_pages_range(addr, addr + size, prot, area->pages,
                        page_shift);
+               if (ret < 0)
+                       schedule_timeout_uninterruptible(1);
+       } while ((gfp_mask & __GFP_NOFAIL) && (ret < 0));
 

1.
After that change a below code:

<snip>

if (ret < 0) {
    warn_alloc(orig_gfp_mask, NULL,
        "vmalloc error: size %lu, failed to map pages",
        area->nr_pages * PAGE_SIZE);
    goto fail;
}

<snip>

Quoted 4 lines
Quoted 1 line

does not make any sense anymore.

Why? Allocations without __GFP_NOFAIL can still fail, no?

Right. I meant one thing but wrote slightly differently. In case of vmap_pages_range() fails(if __GFP_NOFAIL is set) should we emit any warning message? Because either we can recover on a future iteration or it stuck there infinitely so a user does not understand what happened. From the other hand this is how __GFP_NOFAIL works, hm..

Another thing, i see that schedule_timeout_uninterruptible(1) is invoked for all cases even when __GFP_NOFAIL is not set, in that scenario we do not want to wait, instead we should return back to a caller asap. Or am i missing something here?

Quoted 10 lines
Quoted 3 lines

2.
Can we combine two places where we handle __GFP_NOFAIL into one place? That would look like as more sorted out.

I have to admit I am not really fluent at vmalloc code so I wanted to make the code as simple as possible. How would I unwind all the allocated memory (already allocated as GFP_NOFAIL) before retrying at __vmalloc_node_range (if that is what you suggest). And isn't that a bit wasteful?

Quoted 5 lines
Quoted 2 lines

Or did you have anything else in mind?

It depends on how often all this can fail. But let me double check if such combining is easy.

I mean something like below. The idea is to not spread the __GFP_NOFAIL across the vmalloc file keeping it in one solid place:

<snip>

mm/vmalloc.c +0 -0
index d77830ff604c..f4b7927e217e 100644
@@ -2889,8 +2889,14 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 	unsigned long array_size;
 	unsigned int nr_small_pages = size >> PAGE_SHIFT;

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2900

Quoted 10 lines
mm/vmalloc.c +6 -0
 	unsigned int page_order;
+	unsigned long flags;
+	int ret;
 
 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+
+	/*
+	 * This is i do not understand why we do not want to see warning messages.
+	 */
 	gfp_mask |= __GFP_NOWARN;

I suspect this is becauser vmalloc wants to have its own failure reporting.

[...]

Quoted 22 lines
mm/vmalloc.c +12 -6
@@ -3010,16 +3037,22 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
 	area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
 				  VM_UNINITIALIZED | vm_flags, start, end, node,
 				  gfp_mask, caller);
-	if (!area) {
-		warn_alloc(gfp_mask, NULL,
-			"vmalloc error: size %lu, vm_struct allocation failed",
-			real_size);
-		goto fail;
-	}
+	if (area)
+		addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
+
+	if (!area || !addr) {
+		if (gfp_mask & __GFP_NOFAIL) {
+			schedule_timeout_uninterruptible(1);
+			goto again;
+		}
+
+		if (!area)
+			warn_alloc(gfp_mask, NULL,
+				"vmalloc error: size %lu, vm_struct allocation failed",

mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3058

Quoted 10 lines
mm/vmalloc.c +2 -2
+				real_size);
 
-	addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
-	if (!addr)
 		goto fail;
+	}
 
 	/*
 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
<snip>

OK, this looks easier from the code reading but isn't it quite wasteful to throw all the pages backing the area (all of them allocated as __GFP_NOFAIL) just to then fail to allocate few page tables pages and drop all of that on the floor (this will happen in __vunmap AFAICS).

I mean I do not care all that strongly but it seems to me that more changes would need to be done here and optimizations can be done on top.

Is this something you feel strongly about?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#13 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 11 lines
Quoted 11 lines
mm/vmalloc.c +2 -0
index d77830ff604c..f4b7927e217e 100644
@@ -2889,8 +2889,14 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
      unsigned long array_size;
      unsigned int nr_small_pages = size >> PAGE_SHIFT;
      unsigned int page_order;
+     unsigned long flags;
+     int ret;
 
Quoted 10 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2900

Quoted 6 lines
mm/vmalloc.c +4 -0
      array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
+
+     /*
+      * This is i do not understand why we do not want to see warning messages.
+      */
      gfp_mask |= __GFP_NOWARN;

I suspect this is becauser vmalloc wants to have its own failure reporting.

But as i see it is broken. All three warn_alloc() reports in the __vmalloc_area_node()
are useless because the __GFP_NOWARN is added on top of gfp_mask:

void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)

{
        struct va_format vaf;
        va_list args;
        static DEFINE_RATELIMIT_STATE(nopage_rs, 10*HZ, 1);

        if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
                return;

...

everything with the __GFP_NOWARN is just reverted.

Quoted 39 lines

[...]

Quoted 22 lines
mm/vmalloc.c +12 -6
@@ -3010,16 +3037,22 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
      area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
                                VM_UNINITIALIZED | vm_flags, start, end, node,
                                gfp_mask, caller);
-     if (!area) {
-             warn_alloc(gfp_mask, NULL,
-                     "vmalloc error: size %lu, vm_struct allocation failed",
-                     real_size);
-             goto fail;
-     }
+     if (area)
+             addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
+
+     if (!area || !addr) {
+             if (gfp_mask & __GFP_NOFAIL) {
+                     schedule_timeout_uninterruptible(1);
+                     goto again;
+             }
+
+             if (!area)
+                     warn_alloc(gfp_mask, NULL,
+                             "vmalloc error: size %lu, vm_struct allocation failed",

mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 3058

Quoted 10 lines
mm/vmalloc.c +2 -2
+                             real_size);
 
-     addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
-     if (!addr)
              goto fail;
+     }
 
      /*
       * In this function, newly allocated vm_struct has VM_UNINITIALIZED
<snip>

OK, this looks easier from the code reading but isn't it quite wasteful to throw all the pages backing the area (all of them allocated as __GFP_NOFAIL) just to then fail to allocate few page tables pages and drop all of that on the floor (this will happen in __vunmap AFAICS).

Quoted 5 lines

I mean I do not care all that strongly but it seems to me that more changes would need to be done here and optimizations can be done on top.

Is this something you feel strongly about?

Will try to provide some motivations :)

It depends on how to look at it. My view is as follows a more simple code is preferred. It is not considered as a hot path and it is rather a corner case to me. I think "unwinding" has some advantage. At least one motivation is to release a memory(on failure) before a delay that will prevent holding of extra memory in case of __GFP_NOFAIL infinitelly does not succeed, i.e. if a process stuck due to __GFP_NOFAIL it does not "hold" an extra memory forever.

Signature · 1 line
Uladzislau Rezki
reviewer
#14 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

On Fri 29-10-21 16:05:32, Uladzislau Rezki wrote:
[...]

Quoted 8 lines
Quoted 8 lines

OK, this looks easier from the code reading but isn't it quite wasteful to throw all the pages backing the area (all of them allocated as __GFP_NOFAIL) just to then fail to allocate few page tables pages and drop all of that on the floor (this will happen in __vunmap AFAICS).

I mean I do not care all that strongly but it seems to me that more changes would need to be done here and optimizations can be done on top.

Quoted 7 lines
Quoted 2 lines

Is this something you feel strongly about?

Will try to provide some motivations :)

It depends on how to look at it. My view is as follows a more simple code is preferred. It is not considered as a hot path and it is rather a corner case to me.

Yes, we are definitely talking about corner cases here. Even GFP_KERNEL allocations usually do not fail.

Quoted 5 lines

I think "unwinding" has some advantage. At least one motivation is to release a memory(on failure) before a delay that will prevent holding of extra memory in case of __GFP_NOFAIL infinitelly does not succeed, i.e. if a process stuck due to __GFP_NOFAIL it does not "hold" an extra memory forever.

Well, I suspect this is something that we can disagree on and both of us would be kinda right. I would see it as throwing baby out with the bathwater. The vast majority of the memory will be in the area pages and sacrificing that just to allocate few page tables or whatever that might fail in that code path is just a lot of cycles wasted.

So unless you really feel strongly about this then I would stick with this approach.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#15 lore raw

Re: [PATCH 2/4] mm/vmalloc: add support for __GFP_NOFAIL

Quoted 27 lines · Uladzislau Rezki

[...]

Quoted 8 lines
Quoted 8 lines

OK, this looks easier from the code reading but isn't it quite wasteful to throw all the pages backing the area (all of them allocated as __GFP_NOFAIL) just to then fail to allocate few page tables pages and drop all of that on the floor (this will happen in __vunmap AFAICS).

I mean I do not care all that strongly but it seems to me that more changes would need to be done here and optimizations can be done on top.

Quoted 7 lines
Quoted 2 lines

Is this something you feel strongly about?

Will try to provide some motivations :)

It depends on how to look at it. My view is as follows a more simple code is preferred. It is not considered as a hot path and it is rather a corner case to me.

Yes, we are definitely talking about corner cases here. Even GFP_KERNEL allocations usually do not fail.

Quoted 5 lines

I think "unwinding" has some advantage. At least one motivation is to release a memory(on failure) before a delay that will prevent holding of extra memory in case of __GFP_NOFAIL infinitelly does not succeed, i.e. if a process stuck due to __GFP_NOFAIL it does not "hold" an extra memory forever.

Quoted 6 lines

Well, I suspect this is something that we can disagree on and both of us would be kinda right. I would see it as throwing baby out with the bathwater. The vast majority of the memory will be in the area pages and sacrificing that just to allocate few page tables or whatever that might fail in that code path is just a lot of cycles wasted.

We are not talking about performance, no sense to measure cycles here :)

Quoted 4 lines

So unless you really feel strongly about this then I would stick with this approach.

I have raised one concern. The memory resource is shared between all process in case of __GFP_NOFAIL it might be that we never return back to user in that scenario i prefer to release hold memory for other needs instead of keeping it for nothing.

If you think it is not a problem, then i do not have much to say.

--
Vlad Rezki

#16 lore raw

PATCH 3/4 mm/vmalloc: be more explicit about supported gfp flags.

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

The core of the vmalloc allocator __vmalloc_area_node doesn't say anything about gfp mask argument. Not all gfp flags are supported though. Be more explicit about constrains.

---
 mm/vmalloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/vmalloc.c +10 -2
index 602649919a9d..2199d821c981 100644
@@ -2980,8 +2980,16 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  * @caller:		  caller's return address
  *
  * Allocate enough pages to cover @size from the page level
- * allocator with @gfp_mask flags.  Map them into contiguous
- * kernel virtual space, using a pagetable protection of @prot.
+ * allocator with @gfp_mask flags. Please note that the full set of gfp
+ * flags are not supported. GFP_KERNEL would be a preferred allocation mode
+ * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not
+ * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
+ * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka
+ * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).
+ * __GFP_NOWARN can be used to suppress error messages about failures.
+ * 
+ * Map them into contiguous kernel virtual space, using a pagetable
+ * protection of @prot.
  *
  * Return: the address of the area or %NULL on failure
  */
Signature · 1 line
2.30.2
#17 lore raw

Re: [PATCH 3/4] mm/vmalloc: be more explicit about supported gfp flags.

Quoted 16 lines · Michal Hocko

From: Michal Hocko <[email protected]>

The core of the vmalloc allocator __vmalloc_area_node doesn't say anything about gfp mask argument. Not all gfp flags are supported though. Be more explicit about constrains.

---
 mm/vmalloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/vmalloc.c +0 -0
index 602649919a9d..2199d821c981 100644

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2985

Quoted 8 lines
mm/vmalloc.c +3 -2
@@ -2980,8 +2980,16 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  * @caller:		  caller's return address
  *
  * Allocate enough pages to cover @size from the page level
- * allocator with @gfp_mask flags.  Map them into contiguous
- * kernel virtual space, using a pagetable protection of @prot.
+ * allocator with @gfp_mask flags. Please note that the full set of gfp
+ * flags are not supported. GFP_KERNEL would be a preferred allocation mode
+ * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not

In what sense is GFP_KERNEL "preferred"??
The choice of GFP_NOFS, when necessary, isn't based on preference but on need.

I understand that you would prefer no one ever used GFP_NOFs ever - just use the scope API. I even agree. But this is not the place to make that case.

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2987

Quoted 2 lines
mm/vmalloc.c +2 -0
+ * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
+ * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka

I don't think "aka" is the right thing to use here. It is short for "also known as" and there is nothing that is being known as something else.
It would be appropriate to say (i.e. GFP_NOWAIT is not supported). "i.e." is short for the Latin "id est" which means "that is" and normally introduces an alternate description (whereas aka introduces an alternate name).

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2988

Quoted 1 line
mm/vmalloc.c +1 -0
+ * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).

Why do you think __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported.

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2989

Quoted 1 line
mm/vmalloc.c +1 -0
+ * __GFP_NOWARN can be used to suppress error messages about failures.

Surely "NOWARN" suppresses warning messages, not error messages ....

Thanks,
NeilBrown

Quoted 10 lines
mm/vmalloc.c +3 -0
+ * 
+ * Map them into contiguous kernel virtual space, using a pagetable
+ * protection of @prot.
  *
  * Return: the address of the area or %NULL on failure
  */
Signature · 1 line
2.30.2
reviewer
#18 lore raw

Re: [PATCH 3/4] mm/vmalloc: be more explicit about supported gfp flags.

Quoted 30 lines · Neil Brown
Quoted 16 lines · Michal Hocko

From: Michal Hocko <[email protected]>

The core of the vmalloc allocator __vmalloc_area_node doesn't say anything about gfp mask argument. Not all gfp flags are supported though. Be more explicit about constrains.

---
 mm/vmalloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/vmalloc.c +0 -0
index 602649919a9d..2199d821c981 100644

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2985

Quoted 8 lines
mm/vmalloc.c +3 -2
@@ -2980,8 +2980,16 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  * @caller:		  caller's return address
  *
  * Allocate enough pages to cover @size from the page level
- * allocator with @gfp_mask flags.  Map them into contiguous
- * kernel virtual space, using a pagetable protection of @prot.
+ * allocator with @gfp_mask flags. Please note that the full set of gfp
+ * flags are not supported. GFP_KERNEL would be a preferred allocation mode
+ * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not

In what sense is GFP_KERNEL "preferred"??
The choice of GFP_NOFS, when necessary, isn't based on preference but on need.

Quoted 3 lines

I understand that you would prefer no one ever used GFP_NOFs ever - just use the scope API. I even agree. But this is not the place to make that case.

Any suggestion for a better wording?

Quoted 10 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2987

Quoted 2 lines
mm/vmalloc.c +2 -0
+ * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
+ * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka

I don't think "aka" is the right thing to use here. It is short for "also known as" and there is nothing that is being known as something else.
It would be appropriate to say (i.e. GFP_NOWAIT is not supported). "i.e." is short for the Latin "id est" which means "that is" and normally introduces an alternate description (whereas aka introduces an alternate name).

OK

Quoted 3 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2988

Quoted 1 line
mm/vmalloc.c +1 -0
+ * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).

Why do you think __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported.

Because they cannot be passed to the page table allocator. In both cases the allocation would fail when system is short on memory. GFP_KERNEL used for ptes implicitly doesn't behave that way.

Quoted 4 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2989

Quoted 2 lines
mm/vmalloc.c +1 -0
+ * __GFP_NOWARN can be used to suppress error messages about failures.

Surely "NOWARN" suppresses warning messages, not error messages ....

I am not sure I follow. NOWARN means "do not warn" independently on the log level chosen for the message. Is an allocation failure an error message? Is the "vmalloc error: size %lu, failed to map pages" an error message?

Anyway I will go with "__GFP_NOWARN can be used to suppress failure messages"

Is that better?

Signature · 2 lines
Michal Hocko
SUSE Labs
#19 lore raw

Re: [PATCH 3/4] mm/vmalloc: be more explicit about supported gfp flags.

Quoted 31 lines · Michal Hocko
Quoted 30 lines · Neil Brown
Quoted 16 lines · Michal Hocko

From: Michal Hocko <[email protected]>

The core of the vmalloc allocator __vmalloc_area_node doesn't say anything about gfp mask argument. Not all gfp flags are supported though. Be more explicit about constrains.

---
 mm/vmalloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/vmalloc.c +0 -0
index 602649919a9d..2199d821c981 100644

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2985

Quoted 8 lines
mm/vmalloc.c +3 -2
@@ -2980,8 +2980,16 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  * @caller:		  caller's return address
  *
  * Allocate enough pages to cover @size from the page level
- * allocator with @gfp_mask flags.  Map them into contiguous
- * kernel virtual space, using a pagetable protection of @prot.
+ * allocator with @gfp_mask flags. Please note that the full set of gfp
+ * flags are not supported. GFP_KERNEL would be a preferred allocation mode
+ * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not

In what sense is GFP_KERNEL "preferred"??
The choice of GFP_NOFS, when necessary, isn't based on preference but on need.

Quoted 5 lines
Quoted 3 lines

I understand that you would prefer no one ever used GFP_NOFs ever - just use the scope API. I even agree. But this is not the place to make that case.

Any suggestion for a better wording?

"GFP_KERNEL, GFP_NOFS, and GFP_NOIO are all supported".

Quoted 14 lines
Quoted 11 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2987

Quoted 3 lines
mm/vmalloc.c +2 -0
+ * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
+ * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka

I don't think "aka" is the right thing to use here. It is short for "also known as" and there is nothing that is being known as something else.
It would be appropriate to say (i.e. GFP_NOWAIT is not supported). "i.e." is short for the Latin "id est" which means "that is" and normally introduces an alternate description (whereas aka introduces an alternate name).

OK

Quoted 7 lines
Quoted 3 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2988

Quoted 1 line
mm/vmalloc.c +1 -0
+ * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).

Why do you think __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported.

Because they cannot be passed to the page table allocator. In both cases the allocation would fail when system is short on memory. GFP_KERNEL used for ptes implicitly doesn't behave that way.

Could you please point me to the particular allocation which uses GFP_KERNEL rather than the flags passed to __vmalloc_node()? I cannot find it.

Quoted 10 lines
Quoted 5 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2989

Quoted 3 lines
mm/vmalloc.c +1 -0
+ * __GFP_NOWARN can be used to suppress error messages about failures.

Surely "NOWARN" suppresses warning messages, not error messages ....

I am not sure I follow. NOWARN means "do not warn" independently on the log level chosen for the message. Is an allocation failure an error message? Is the "vmalloc error: size %lu, failed to map pages" an error message?

If guess working with a C compiler has trained me to think that "warnings" are different from "errors".

Quoted 4 lines

Anyway I will go with "__GFP_NOWARN can be used to suppress failure messages"

Is that better?

Yes, that's an excellent solution! Thanks.

NeilBrown

Quoted 5 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#20 lore raw

Re: [PATCH 3/4] mm/vmalloc: be more explicit about supported gfp flags.

Quoted 32 lines · Neil Brown
Quoted 31 lines · Michal Hocko
Quoted 30 lines · Neil Brown
Quoted 16 lines · Michal Hocko

From: Michal Hocko <[email protected]>

The core of the vmalloc allocator __vmalloc_area_node doesn't say anything about gfp mask argument. Not all gfp flags are supported though. Be more explicit about constrains.

---
 mm/vmalloc.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/vmalloc.c +0 -0
index 602649919a9d..2199d821c981 100644

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2985

Quoted 8 lines
mm/vmalloc.c +3 -2
@@ -2980,8 +2980,16 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  * @caller:		  caller's return address
  *
  * Allocate enough pages to cover @size from the page level
- * allocator with @gfp_mask flags.  Map them into contiguous
- * kernel virtual space, using a pagetable protection of @prot.
+ * allocator with @gfp_mask flags. Please note that the full set of gfp
+ * flags are not supported. GFP_KERNEL would be a preferred allocation mode
+ * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not

In what sense is GFP_KERNEL "preferred"??
The choice of GFP_NOFS, when necessary, isn't based on preference but on need.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I understand that you would prefer no one ever used GFP_NOFs ever - just use the scope API. I even agree. But this is not the place to make that case.

Any suggestion for a better wording?

"GFP_KERNEL, GFP_NOFS, and GFP_NOIO are all supported".

OK. Check the incremental update at the end of the email

Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2987

Quoted 2 lines
mm/vmalloc.c +2 -0
+ * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
+ * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka

I don't think "aka" is the right thing to use here. It is short for "also known as" and there is nothing that is being known as something else.
It would be appropriate to say (i.e. GFP_NOWAIT is not supported). "i.e." is short for the Latin "id est" which means "that is" and normally introduces an alternate description (whereas aka introduces an alternate name).

OK

Quoted 3 lines

mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 2988

Quoted 1 line
mm/vmalloc.c +1 -0
+ * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).

Why do you think __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported.

Quoted 8 lines
Quoted 3 lines

Because they cannot be passed to the page table allocator. In both cases the allocation would fail when system is short on memory. GFP_KERNEL used for ptes implicitly doesn't behave that way.

Could you please point me to the particular allocation which uses GFP_KERNEL rather than the flags passed to __vmalloc_node()? I cannot find it.

It is dug
__vmalloc_area_node
vmap_pages_range

    vmap_pages_range_noflush
      vmap_range_noflush || vmap_small_pages_range_noflush
        vmap_p4d_range
	  p4d_alloc_track
	    __p4d_alloc
	      p4d_alloc_one
	        get_zeroed_page(GFP_KERNEL_ACCOUNT)

the same applies for all other levels of page tables.

This is what I have currently
commit ae7fc6c2ef6949a76d697fc61bb350197dfca330

Author: Michal Hocko <[email protected]>
Date:   Tue Oct 26 14:16:32 2021 +0200

fold me "mm/vmalloc: be more explicit about supported gfp flags."

mm/vmalloc.c +8 -6
index 2ddaa9410aee..82a07b04317e 100644
@@ -2981,12 +2981,14 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  *
  * Allocate enough pages to cover @size from the page level
  * allocator with @gfp_mask flags. Please note that the full set of gfp
- * flags are not supported. GFP_KERNEL would be a preferred allocation mode
- * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not
- * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
- * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka
- * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).
- * __GFP_NOWARN can be used to suppress error messages about failures.
+ * flags are not supported. GFP_KERNEL, GFP_NOFS, and GFP_NOIO are all
+ * supported.
+ * Zone modifiers are not supported. From the reclaim modifiers
+ * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported)
+ * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and 
+ * __GFP_RETRY_MAYFAIL are not supported).
+ *
+ * __GFP_NOWARN can be used to suppress failures messages.
  * 
  * Map them into contiguous kernel virtual space, using a pagetable
  * protection of @prot.
Signature · 2 lines
Michal Hocko
SUSE Labs
#21 lore raw

PATCH 4/4 mm: allow !GFP_KERNEL allocations for kvmalloc

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

A support for GFP_NO{FS,IO} and __GFP_NOFAIL has been implemented by previous patches so we can allow the support for kvmalloc. This will allow some external users to simplify or completely remove their helpers.

GFP_NOWAIT semantic hasn't been supported so far but it hasn't been explicitly documented so let's add a note about that.

ceph_kvmalloc is the first helper to be dropped and changed to kvmalloc.

---
 include/linux/ceph/libceph.h |  1 -
 mm/util.c                    | 15 ++++-----------
 net/ceph/buffer.c            |  4 ++--
 net/ceph/ceph_common.c       | 27 ---------------------------
 net/ceph/crypto.c            |  2 +-
 net/ceph/messenger.c         |  2 +-
 net/ceph/messenger_v2.c      |  2 +-
 net/ceph/osdmap.c            | 12 ++++++------
 8 files changed, 15 insertions(+), 50 deletions(-)
include/linux/ceph/libceph.h +0 -1
index 409d8c29bc4f..309acbcb5a8a 100644
@@ -295,7 +295,6 @@ extern bool libceph_compatible(void *data);
 
 extern const char *ceph_msg_type_name(int type);
 extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid);
-extern void *ceph_kvmalloc(size_t size, gfp_t flags);
 
 struct fs_parameter;
 struct fc_log;
mm/util.c +4 -11
index bacabe446906..fdec6b4b1267 100644
@@ -549,13 +549,10 @@ EXPORT_SYMBOL(vm_mmap);
  * Uses kmalloc to get the memory but if the allocation fails then falls back
  * to the vmalloc allocator. Use kvfree for freeing the memory.
  *
- * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
+ * Reclaim modifiers - __GFP_NORETRY and GFP_NOWAIT are not supported.
  * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
  * preferable to the vmalloc fallback, due to visible performance drawbacks.
  *
- * Please note that any use of gfp flags outside of GFP_KERNEL is careful to not
- * fall back to vmalloc.
- *
  * Return: pointer to the allocated memory of %NULL in case of failure
  */
 void *kvmalloc_node(size_t size, gfp_t flags, int node)
@@ -563,13 +560,6 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node)
 	gfp_t kmalloc_flags = flags;
 	void *ret;
 
-	/*
-	 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
-	 * so the given set of flags has to be compatible.
-	 */
-	if ((flags & GFP_KERNEL) != GFP_KERNEL)
-		return kmalloc_node(size, flags, node);
-
 	/*
 	 * We want to attempt a large physically contiguous block first because
 	 * it is less likely to fragment multiple larger blocks and therefore
@@ -582,6 +572,9 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node)
 
 		if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
 			kmalloc_flags |= __GFP_NORETRY;
+
+		/* nofail semantic is implemented by the vmalloc fallback */
+		kmalloc_flags &= ~__GFP_NOFAIL;
 	}
 
 	ret = kmalloc_node(size, kmalloc_flags, node);
net/ceph/buffer.c +2 -2
index 5622763ad402..7e51f128045d 100644
@@ -7,7 +7,7 @@
 
 #include <linux/ceph/buffer.h>
 #include <linux/ceph/decode.h>
-#include <linux/ceph/libceph.h> /* for ceph_kvmalloc */
+#include <linux/ceph/libceph.h> /* for kvmalloc */
 
 struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
 {
@@ -17,7 +17,7 @@ struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
 	if (!b)
 		return NULL;
 
-	b->vec.iov_base = ceph_kvmalloc(len, gfp);
+	b->vec.iov_base = kvmalloc(len, gfp);
 	if (!b->vec.iov_base) {
 		kfree(b);
 		return NULL;
net/ceph/ceph_common.c +0 -27
index 97d6ea763e32..9441b4a4912b 100644
@@ -190,33 +190,6 @@ int ceph_compare_options(struct ceph_options *new_opt,
 }
 EXPORT_SYMBOL(ceph_compare_options);
 
-/*
- * kvmalloc() doesn't fall back to the vmalloc allocator unless flags are
- * compatible with (a superset of) GFP_KERNEL.  This is because while the
- * actual pages are allocated with the specified flags, the page table pages
- * are always allocated with GFP_KERNEL.
- *
- * ceph_kvmalloc() may be called with GFP_KERNEL, GFP_NOFS or GFP_NOIO.
- */
-void *ceph_kvmalloc(size_t size, gfp_t flags)
-{
-	void *p;
-
-	if ((flags & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) {
-		p = kvmalloc(size, flags);
-	} else if ((flags & (__GFP_IO | __GFP_FS)) == __GFP_IO) {
-		unsigned int nofs_flag = memalloc_nofs_save();
-		p = kvmalloc(size, GFP_KERNEL);
-		memalloc_nofs_restore(nofs_flag);
-	} else {
-		unsigned int noio_flag = memalloc_noio_save();
-		p = kvmalloc(size, GFP_KERNEL);
-		memalloc_noio_restore(noio_flag);
-	}
-
-	return p;
-}
-
 static int parse_fsid(const char *str, struct ceph_fsid *fsid)
 {
 	int i = 0;
net/ceph/crypto.c +1 -1
index 92d89b331645..051d22c0e4ad 100644
@@ -147,7 +147,7 @@ void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
 static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
 
 /*
- * Should be used for buffers allocated with ceph_kvmalloc().
+ * Should be used for buffers allocated with kvmalloc().
  * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
  * in-buffer (msg front).
  *
net/ceph/messenger.c +1 -1
index 57d043b382ed..7b891be799d2 100644
@@ -1920,7 +1920,7 @@ struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
 
 	/* front */
 	if (front_len) {
-		m->front.iov_base = ceph_kvmalloc(front_len, flags);
+		m->front.iov_base = kvmalloc(front_len, flags);
 		if (m->front.iov_base == NULL) {
 			dout("ceph_msg_new can't allocate %d bytes\n",
 			     front_len);
net/ceph/messenger_v2.c +1 -1
index cc40ce4e02fb..c4099b641b38 100644
@@ -308,7 +308,7 @@ static void *alloc_conn_buf(struct ceph_connection *con, int len)
 	if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs)))
 		return NULL;
 
-	buf = ceph_kvmalloc(len, GFP_NOIO);
+	buf = kvmalloc(len, GFP_NOIO);
 	if (!buf)
 		return NULL;
 
net/ceph/osdmap.c +6 -6
index 75b738083523..2823bb3cff55 100644
@@ -980,7 +980,7 @@ static struct crush_work *alloc_workspace(const struct crush_map *c)
 	work_size = crush_work_size(c, CEPH_PG_MAX_SIZE);
 	dout("%s work_size %zu bytes\n", __func__, work_size);
 
-	work = ceph_kvmalloc(work_size, GFP_NOIO);
+	work = kvmalloc(work_size, GFP_NOIO);
 	if (!work)
 		return NULL;
 
@@ -1190,9 +1190,9 @@ static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
 	if (max == map->max_osd)
 		return 0;
 
-	state = ceph_kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS);
-	weight = ceph_kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS);
-	addr = ceph_kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS);
+	state = kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS);
+	weight = kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS);
+	addr = kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS);
 	if (!state || !weight || !addr) {
 		kvfree(state);
 		kvfree(weight);
@@ -1222,7 +1222,7 @@ static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
 	if (map->osd_primary_affinity) {
 		u32 *affinity;
 
-		affinity = ceph_kvmalloc(array_size(max, sizeof(*affinity)),
+		affinity = kvmalloc(array_size(max, sizeof(*affinity)),
 					 GFP_NOFS);
 		if (!affinity)
 			return -ENOMEM;
@@ -1503,7 +1503,7 @@ static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
 	if (!map->osd_primary_affinity) {
 		int i;
 
-		map->osd_primary_affinity = ceph_kvmalloc(
+		map->osd_primary_affinity = kvmalloc(
 		    array_size(map->max_osd, sizeof(*map->osd_primary_affinity)),
 		    GFP_NOFS);
 		if (!map->osd_primary_affinity)
Signature · 1 line
2.30.2
#22 lore raw

Re: [PATCH 4/4] mm: allow !GFP_KERNEL allocations for kvmalloc

Quoted 42 lines · Michal Hocko

From: Michal Hocko <[email protected]>

A support for GFP_NO{FS,IO} and __GFP_NOFAIL has been implemented by previous patches so we can allow the support for kvmalloc. This will allow some external users to simplify or completely remove their helpers.

GFP_NOWAIT semantic hasn't been supported so far but it hasn't been explicitly documented so let's add a note about that.

ceph_kvmalloc is the first helper to be dropped and changed to kvmalloc.

---
 include/linux/ceph/libceph.h |  1 -
 mm/util.c                    | 15 ++++-----------
 net/ceph/buffer.c            |  4 ++--
 net/ceph/ceph_common.c       | 27 ---------------------------
 net/ceph/crypto.c            |  2 +-
 net/ceph/messenger.c         |  2 +-
 net/ceph/messenger_v2.c      |  2 +-
 net/ceph/osdmap.c            | 12 ++++++------
 8 files changed, 15 insertions(+), 50 deletions(-)
include/linux/ceph/libceph.h +0 -1
index 409d8c29bc4f..309acbcb5a8a 100644
@@ -295,7 +295,6 @@ extern bool libceph_compatible(void *data);
 
 extern const char *ceph_msg_type_name(int type);
 extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid);
-extern void *ceph_kvmalloc(size_t size, gfp_t flags);
 
 struct fs_parameter;
 struct fc_log;
mm/util.c +0 -0
index bacabe446906..fdec6b4b1267 100644

mm/util.c EXPORT_SYMBOL(vm_mmap); · line 552

Quoted 5 lines
mm/util.c +1 -1
@@ -549,13 +549,10 @@ EXPORT_SYMBOL(vm_mmap);
  * Uses kmalloc to get the memory but if the allocation fails then falls back
  * to the vmalloc allocator. Use kvfree for freeing the memory.
  *
- * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
+ * Reclaim modifiers - __GFP_NORETRY and GFP_NOWAIT are not supported.

GFP_NOWAIT is not a modifier. It is a base value that can be modified. I think you mean that
__GFP_NORETRY is not supported and __GFP_DIRECT_RECLAIM is required

But I really cannot see why either of these statements are true.

Before your patch, __GFP_NORETRY would have forced use of kmalloc, so that would mean it isn't really supported. But that doesn't happen any more.

Thanks,
NeilBrown

reviewer
#23 lore raw

Re: [PATCH 4/4] mm: allow !GFP_KERNEL allocations for kvmalloc

Quoted 36 lines · Neil Brown
Quoted 35 lines · Michal Hocko

From: Michal Hocko <[email protected]>

A support for GFP_NO{FS,IO} and __GFP_NOFAIL has been implemented by previous patches so we can allow the support for kvmalloc. This will allow some external users to simplify or completely remove their helpers.

GFP_NOWAIT semantic hasn't been supported so far but it hasn't been explicitly documented so let's add a note about that.

ceph_kvmalloc is the first helper to be dropped and changed to kvmalloc.

---
 include/linux/ceph/libceph.h |  1 -
 mm/util.c                    | 15 ++++-----------
 net/ceph/buffer.c            |  4 ++--
 net/ceph/ceph_common.c       | 27 ---------------------------
 net/ceph/crypto.c            |  2 +-
 net/ceph/messenger.c         |  2 +-
 net/ceph/messenger_v2.c      |  2 +-
 net/ceph/osdmap.c            | 12 ++++++------
 8 files changed, 15 insertions(+), 50 deletions(-)
include/linux/ceph/libceph.h +0 -1
index 409d8c29bc4f..309acbcb5a8a 100644
@@ -295,7 +295,6 @@ extern bool libceph_compatible(void *data);
 
 extern const char *ceph_msg_type_name(int type);
 extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid);
-extern void *ceph_kvmalloc(size_t size, gfp_t flags);
 
Quoted 16 lines

mm/util.c EXPORT_SYMBOL(vm_mmap); · line 552

Quoted 12 lines
include/linux/ceph/libceph.h +0 -0
 struct fs_parameter;
 struct fc_log;
mm/util.c +1 -1
index bacabe446906..fdec6b4b1267 100644
@@ -549,13 +549,10 @@ EXPORT_SYMBOL(vm_mmap);
  * Uses kmalloc to get the memory but if the allocation fails then falls back
  * to the vmalloc allocator. Use kvfree for freeing the memory.
  *
- * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
+ * Reclaim modifiers - __GFP_NORETRY and GFP_NOWAIT are not supported.

GFP_NOWAIT is not a modifier. It is a base value that can be modified. I think you mean that
__GFP_NORETRY is not supported and __GFP_DIRECT_RECLAIM is required

I thought naming the higher level gfp mask would be more helpful here. Most people do not tend to think in terms of __GFP_DIRECT_RECLAIM but rather GFP_NOWAIT or GFP_ATOMIC.

Quoted 1 line

But I really cannot see why either of these statements are true.

The reason is same as why vmalloc do not support neither of them.

Quoted 2 lines

Before your patch, __GFP_NORETRY would have forced use of kmalloc, so that would mean it isn't really supported. But that doesn't happen any more.

__GFP_NORETRY is used internaly by kvmalloc but that doesn't mean it is supported by the caller. In fact __GFP_NORETRY is used to implement a higher level logic of the prioritization between kmalloc and vmalloc fallback because some users would rather see vmalloc fallback even for smaller allocations which do not really fail otherwise (e.g. < order-4).

Signature · 2 lines
Michal Hocko
SUSE Labs
#24 lore raw

Re: [PATCH 4/4] mm: allow !GFP_KERNEL allocations for kvmalloc

Quoted 54 lines · Michal Hocko
Quoted 36 lines · Neil Brown
Quoted 35 lines · Michal Hocko

From: Michal Hocko <[email protected]>

A support for GFP_NO{FS,IO} and __GFP_NOFAIL has been implemented by previous patches so we can allow the support for kvmalloc. This will allow some external users to simplify or completely remove their helpers.

GFP_NOWAIT semantic hasn't been supported so far but it hasn't been explicitly documented so let's add a note about that.

ceph_kvmalloc is the first helper to be dropped and changed to kvmalloc.

---
 include/linux/ceph/libceph.h |  1 -
 mm/util.c                    | 15 ++++-----------
 net/ceph/buffer.c            |  4 ++--
 net/ceph/ceph_common.c       | 27 ---------------------------
 net/ceph/crypto.c            |  2 +-
 net/ceph/messenger.c         |  2 +-
 net/ceph/messenger_v2.c      |  2 +-
 net/ceph/osdmap.c            | 12 ++++++------
 8 files changed, 15 insertions(+), 50 deletions(-)
include/linux/ceph/libceph.h +0 -1
index 409d8c29bc4f..309acbcb5a8a 100644
@@ -295,7 +295,6 @@ extern bool libceph_compatible(void *data);
 
 extern const char *ceph_msg_type_name(int type);
 extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid);
-extern void *ceph_kvmalloc(size_t size, gfp_t flags);
 
Quoted 16 lines

mm/util.c EXPORT_SYMBOL(vm_mmap); · line 552

Quoted 12 lines
include/linux/ceph/libceph.h +0 -0
 struct fs_parameter;
 struct fc_log;
mm/util.c +1 -1
index bacabe446906..fdec6b4b1267 100644
@@ -549,13 +549,10 @@ EXPORT_SYMBOL(vm_mmap);
  * Uses kmalloc to get the memory but if the allocation fails then falls back
  * to the vmalloc allocator. Use kvfree for freeing the memory.
  *
- * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
+ * Reclaim modifiers - __GFP_NORETRY and GFP_NOWAIT are not supported.

GFP_NOWAIT is not a modifier. It is a base value that can be modified. I think you mean that
__GFP_NORETRY is not supported and __GFP_DIRECT_RECLAIM is required

Quoted 3 lines

I thought naming the higher level gfp mask would be more helpful here. Most people do not tend to think in terms of __GFP_DIRECT_RECLAIM but rather GFP_NOWAIT or GFP_ATOMIC.

Maybe it would. But the text says "Reclaim modifiers" and then lists one modifier and one mask. That is confusing.
If you want to mention both, keep them separate.

GFP_NOWAIT and GFP_ATOMIC are not supported, neither is the __GFP_NORETRY modifier.

or something like that.

Thanks,
NeilBrown

Quoted 18 lines
Quoted 2 lines

But I really cannot see why either of these statements are true.

The reason is same as why vmalloc do not support neither of them.

Quoted 2 lines

Before your patch, __GFP_NORETRY would have forced use of kmalloc, so that would mean it isn't really supported. But that doesn't happen any more.

__GFP_NORETRY is used internaly by kvmalloc but that doesn't mean it is supported by the caller. In fact __GFP_NORETRY is used to implement a higher level logic of the prioritization between kmalloc and vmalloc fallback because some users would rather see vmalloc fallback even for smaller allocations which do not really fail otherwise (e.g. < order-4).

Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#25 lore raw

Re: [PATCH 4/4] mm: allow !GFP_KERNEL allocations for kvmalloc

Quoted 1 line · Neil Brown

On Tue, 26 Oct 2021, Michal Hocko wrote:

[...]

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

GFP_NOWAIT is not a modifier. It is a base value that can be modified. I think you mean that
__GFP_NORETRY is not supported and __GFP_DIRECT_RECLAIM is required

I thought naming the higher level gfp mask would be more helpful here. Most people do not tend to think in terms of __GFP_DIRECT_RECLAIM but rather GFP_NOWAIT or GFP_ATOMIC.

Quoted 8 lines

Maybe it would. But the text says "Reclaim modifiers" and then lists one modifier and one mask. That is confusing.
If you want to mention both, keep them separate.

GFP_NOWAIT and GFP_ATOMIC are not supported, neither is the __GFP_NORETRY modifier.

or something like that.

Fair enough. I went with this
commit fb93996c217cea864a3b3ffa8a8cd482bf0a1f62

Author: Michal Hocko <[email protected]>
Date:   Tue Oct 26 14:23:00 2021 +0200

fold me "mm: allow !GFP_KERNEL allocations for kvmalloc"

mm/util.c +1 -1
index fdec6b4b1267..1fb6dd907bb0 100644
@@ -549,7 +549,7 @@ EXPORT_SYMBOL(vm_mmap);
  * Uses kmalloc to get the memory but if the allocation fails then falls back
  * to the vmalloc allocator. Use kvfree for freeing the memory.
  *
- * Reclaim modifiers - __GFP_NORETRY and GFP_NOWAIT are not supported.
+ * GFP_NOWAIT and GFP_ATOMIC are not supported, neither is the __GFP_NORETRY modifier.
  * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
  * preferable to the vmalloc fallback, due to visible performance drawbacks.
  *
Signature · 2 lines
Michal Hocko
SUSE Labs