Skip to contentSkip to conversation

mm/vmalloc: fix mischeck pfn valid in vmap_pfns

PATCH In review Huan Yang 6 participants 23 messages Mar 12, 2025 – Mar 28, 2025 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Huan Yang

author
#0 lore raw

PATCH 1/1 mm/vmalloc: fix mischeck pfn valid in vmap_pfns

When invoke vmap_pfns, it call vmap_pfn_apply to set pfn into pte. It check pfn is valid, if true then warn and return.

This is a mischeck, actually we need set a valid pfn into pte, not an invalid pfn.

This patch fix it.

---
 mm/vmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmalloc.c +1 -1
index 044af7088359..ebadb8ceeba7 100644
@@ -3488,7 +3488,7 @@ static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
 	unsigned long pfn = data->pfns[data->idx];
 	pte_t ptent;
 
-	if (WARN_ON_ONCE(pfn_valid(pfn)))
+	if (WARN_ON_ONCE(!pfn_valid(pfn)))
 		return -EINVAL;
 
 	ptent = pte_mkspecial(pfn_pte(pfn, data->prot));
Signature · 1 line
2.39.0
#1 lore raw
Quoted 5 lines · Huan Yang

When invoke vmap_pfns, it call vmap_pfn_apply to set pfn into pte. It check pfn is valid, if true then warn and return.

This is a mischeck, actually we need set a valid pfn into pte, not an invalid pfn.

As just discussed this is wrong. vmap_pfn is for mapping non-page PFNs and the check is what enforces that. What is the point of having that detailed discussion if you just send the broken patch anyway with a commit log not even acknowledging the facts?

author
#2 lore raw

HI Christoph,

Thanks for your reply, and I'm sorry for my late reply. Your response didn't appear in my email client; I only saw it on the website.:(

Quoted 2 lines
Quoted 2 lines · Huan Yang

When invoke vmap_pfns, it call vmap_pfn_apply to set pfn into pte. >> It check pfn is valid, if true then warn and return. >> >> This is

a mischeck, actually we need set a valid pfn into pte, not an >> invalid pfn. >

Quoted 1 line

As just discussed this is wrong. vmap_pfn is for mapping non-page

Thank you for your explanation. I now understand that the design of vmap_pfn is indeed intentional. It's design to do this.

Quoted 3 lines

PFNs and the check is what enforces that. What is the point of having that detailed discussion if you just send the broken patch anyway with a commit log not even acknowledging the facts?

Sorry for that.

We now have a new use case where, in udmabuf, memory is passed via memfd and can
be either shmem or hugetlb.
When the memory is hugetlb and HVO is enabled, the tail page's struct is no longer
reliable because it has been freed. Can't use vmap.
Therefore, when making modifications, I recorded the pfn of the folio base pfn + offset and called vmap_pfns. And, these pfns are valid. So rejected by vmap_pfns.

Can we just remove pfn_valid check in vmap_pfns, so make it suit for both of they? If you agree, I wanna send a new patch.

Thank you,
Huan Yang

#3 lore raw
Quoted 23 lines · Huan Yang

HI Christoph,

Thanks for your reply, and I'm sorry for my late reply. Your response didn't appear in my email client; I only saw it on the website.:(

Quoted 2 lines
Quoted 2 lines · Huan Yang

When invoke vmap_pfns, it call vmap_pfn_apply to set pfn into pte. >> It check pfn is valid, if true then warn and return. >> >> This is

a mischeck, actually we need set a valid pfn into pte, not an >> invalid pfn. >

Quoted 1 line

As just discussed this is wrong. vmap_pfn is for mapping non-page

Thank you for your explanation. I now understand that the design of vmap_pfn is indeed intentional. It's design to do this.

Quoted 3 lines

PFNs and the check is what enforces that. What is the point of having that detailed discussion if you just send the broken patch anyway with a commit log not even acknowledging the facts?

Sorry for that.

We now have a new use case where, in udmabuf, memory is passed via memfd and can
be either shmem or hugetlb.
When the memory is hugetlb and HVO is enabled, the tail page's struct is no longer
reliable because it has been freed. Can't use vmap.
Therefore, when making modifications, I recorded the pfn of the folio base pfn + offset and called vmap_pfns. And, these pfns are valid. So rejected by vmap_pfns.

Quoted 2 lines

Can we just remove pfn_valid check in vmap_pfns, so make it suit for both of they? If you agree, I wanna send a new patch.

Huan,

Why not update udmabuf to make it work with both vmap_pfns() and vmap()? As only the udmabuf knows it is actually working on?

I don't think it's a good idea to hack the common API, the WARN_ON() is really a mandatory check, and current case is a good example.

Quoted 4 lines

Thank you,
Huan Yang

Signature · 2 lines
Best regards,
Bingbu Cao
#4 lore raw
Quoted 5 lines · Bingbu Cao

Why not update udmabuf to make it work with both vmap_pfns() and vmap()? As only the udmabuf knows it is actually working on?

I don't think it's a good idea to hack the common API, the WARN_ON() is really a mandatory check, and current case is a good example.

What non-page backed memory does udmabuf try to work on, and more importantly how does it actually work on them given that the normal DMA APIs require page backed memory. Or is this just made it up and it doesn't work at all given that it also tries to dma map to the fake miscdevice struct device which can't work for most cases?

Mapping non-page memory is difficult and without having coherent theory of what non-page memory you are mapping and being very careful you are extremely unlikely to get it right.

author
#5 lore raw

HI Christoph

Quoted 2 lines · Bingbu Cao
Quoted 1 line

Why not update udmabuf to make it work with both vmap_pfns() and >> vmap()? As only the udmabuf knows it is actually working on? >> >>

I don't think it's a good idea to hack the common API, the WARN_ON() >> is really a mandatory check, and current case is a good example.

Quoted 1 line

What non-page backed memory does udmabuf try to work on, and more

It's HUGETLB which enabled VMEMMAP_OPTIMIZE, all tail page's struct will ref to head page struct, and then release tailed page struct.

Quoted 2 lines

importantly how does it actually work on them given that the normal DMA APIs require page backed memory. Or is this just made it up

udmabuf's sg_table ref only folio+offset, no any page struct ref. So, any DMA APIs just use folio based.
It pin each folio given by memfd&offset.(memfd can be shmem or hugetlb). So, shmem memfd can get page struct, hugetlb's may can't.

Quoted 3 lines

and it doesn't work at all given that it also tries to dma map to the fake miscdevice struct device which can't work for most cases?

This implement map_dma_buf&mmap&vmap&begin/end_cpu_access. It's simple implement.

Quoted 4 lines

Mapping non-page memory is difficult and without having coherent theory of what non-page memory you are mapping and being very careful you are extremely unlikely to get it right.

#6 lore raw

So you want a folio based version of vmap. Please work on that instead of doing crazy hacks using vmap_pfns which is just for memory not historically backed by pages and now folios.

author
#7 lore raw

HI Christoph

Quoted 1 line · Christoph Hellwig

So you want a folio based version of vmap. Please work on that instead

The folio-based vmap implementation you mentioned may indeed be necessary, but it has limited

direct relevance to the specific issue I'm currently addressing.

Quoted 1 line

of doing crazy hacks using vmap_pfns which is just for memory not

I mentioned that if we use HVO-based hugetlb, we cannot access the page structures corresponding to

the physical memory that needs to be mapped.

This prevents us from properly invoking vmap, which is why we have turned to using vmap_pfn instead.

Even if a folio-based vmap is implemented, it still cannot handle mapping multiple folio ranges of physical

memory to vmalloc regions. A range of folio is important, it maybe an offset in memfd, no need entire folio.

So, I still consider vmap_pfn to be the optimal solution for this specific scenario. :)

Quoted 1 line

historically backed by pages and now folios.

So by HVO, it also not backed by pages, only contains folio head, each tail pfn's page struct go away.

Do I misunderstood this part? Please correct me.

Thank you

Huan Yang

#8 lore raw
Quoted 7 lines · Huan Yang

This prevents us from properly invoking vmap, which is why we have turned to using vmap_pfn instead.

Even if a folio-based vmap is implemented, it still cannot handle mapping multiple folio ranges of physical

memory to vmalloc regions. A range of folio is important, it maybe an offset in memfd, no need entire folio.

So, I still consider vmap_pfn to be the optimal solution for this specific scenario. :)

No, vmap_pfn is entirely for memory not backed by pages or folios, i.e. PCIe BARs and similar memory. This must not be mixed with proper folio backed memory.

So you'll need a vmap for folios to support this use case.

Quoted 4 lines
Quoted 2 lines

historically backed by pages and now folios.

So by HVO, it also not backed by pages, only contains folio head, each tail pfn's page struct go away.

And a fully folios based vmap solves that problem.

author
#9 lore raw

HI Christoph

Quoted 5 lines · Christoph Hellwig

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 2 lines · Huan Yang

This prevents us from properly invoking vmap, which is why we have turned to using vmap_pfn instead.

Quoted 8 lines
Quoted 5 lines

Even if a folio-based vmap is implemented, it still cannot handle mapping multiple folio ranges of physical

memory to vmalloc regions. A range of folio is important, it maybe an offset in memfd, no need entire folio.

So, I still consider vmap_pfn to be the optimal solution for this specific scenario. :)

No, vmap_pfn is entirely for memory not backed by pages or folios, i.e. PCIe BARs and similar memory. This must not be mixed with proper folio backed memory.

OK, I learn it more.

Quoted 2 lines

So you'll need a vmap for folios to support this use case.

May can't

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

historically backed by pages and now folios.

So by HVO, it also not backed by pages, only contains folio head, each tail pfn's page struct go away.

And a fully folios based vmap solves that problem.

A folio may be 2MB or more large 1GB, what if we only need a little, 1M or 512MB, can vmap based on folio can solve it?

Normally, can offer 4k-page based array map it. But consider HVO, can't. That's why wanto base on pfn.

Thank you

Huan Yang

Quoted 1 line
#10 lore raw
Quoted 1 line · Huan Yang

A folio may be 2MB or more large 1GB, what if we only need a little, 1M or 512MB, can vmap based on folio can solve it?

Then you only map part of it by passing a length argument. Note that in general when you have these large folios you also don't have highmem, so if you only map one of them, or part of one of them you don't actually need vmap at all and can just use folio-address..

Quoted 1 line

Normally, can offer 4k-page based array map it. But consider HVO, can't. That's why wanto base on pfn.

Well, for any large folio using this 4k based page interface is actually highly inefficient. So let's fix that. And my loop in willy as Mr. Folio while you're at it.

author
#11 lore raw

HI Christoph

Quoted 8 lines · Christoph Hellwig

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 1 line · Huan Yang

A folio may be 2MB or more large 1GB, what if we only need a little, 1M or 512MB, can vmap based on folio can solve it?

Then you only map part of it by passing a length argument. Note that in general when you have these large folios you also don't have highmem, so if you only map one of them, or part of one of them you don't actually need vmap at all and can just use folio-address..

The situation remains complex. We are not only referring to this single folio, but it may be composed

of multiple folios and start with a small piece. For example:

Folio 1 2M - use 1M

Folio 2 2M - use 2M

....

Combine these folios into memory used by udmabuf. So, directly virtual memory useless. :)

Quoted 4 lines
Quoted 2 lines

Normally, can offer 4k-page based array map it. But consider HVO, can't. That's why wanto base on pfn.

Well, for any large folio using this 4k based page interface is actually highly inefficient. So let's fix that. And my loop in

Yes, indeed. You mentioned good to entire folio array mapped into vmalloc, I guest here are many user like it.

Quoted 1 line

willy as Mr. Folio while you're at it.

I'd like to research it, but not too fast. :)

Quoted 1 line

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
Quoted 2 lines · Hillf Danton

A quick fix is to add a vmap_pfn variant to walk around the pfn check in question, like the following diff (just for idea show).

No.

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#15 lore raw

HI Hillf,

Thanks fo your suggestion.

Quoted 9 lines · Hillf Danton
Quoted 5 lines · On Wed, 19 Mar 2025 09:08:51 +0100 Christoph Hellwig wrote:
Quoted 2 lines · Hillf Danton

A quick fix is to add a vmap_pfn variant to walk around the pfn check in question, like the following diff (just for idea show).

No.

Patient to see your fix, given no low hanging peach left for Mr Folio in case of HVO, (feel free to ignore, who is likely about 2.6-mile less tough than you could be).

I now believe there are two way to resolve the HVO folio can't vmap in udmabuf.

1. simple copy vmap_pfn code, don't bother common vmap_pfn, use by itself and remove pfn_valid check.

2. implement folio array based vmap(vmap_folios), which can given a range of each folio(offset, nr_pages), so can suit HVO folio's vmap.

1 is simple and can fast fix this issue, but may not too good. I need discuss with bot Christoph and udmabuf's maintainer.

2 is hard, but may worth to research, which I also will try to do. :)

#16 lore raw

Hi Huan,

Quoted 23 lines

Subject: Re: [PATCH] mm/vmalloc: fix mischeck pfn valid in vmap_pfns

HI Hillf,

Thanks fo your suggestion.

Quoted 9 lines · Hillf Danton
Quoted 5 lines · On Wed, 19 Mar 2025 09:08:51 +0100 Christoph Hellwig wrote:
Quoted 2 lines · Hillf Danton

A quick fix is to add a vmap_pfn variant to walk around the pfn check in question, like the following diff (just for idea show).

No.

Patient to see your fix, given no low hanging peach left for Mr Folio in case of HVO, (feel free to ignore, who is likely about 2.6-mile less tough than you could be).

I now believe there are two way to resolve the HVO folio can't vmap in udmabuf.

1. simple copy vmap_pfn code, don't bother common vmap_pfn, use by itself and remove pfn_valid check.

Quoted 7 lines

2. implement folio array based vmap(vmap_folios), which can given a range of each folio(offset, nr_pages), so can suit HVO folio's vmap.

1 is simple and can fast fix this issue, but may not too good. I need discuss with bot Christoph and udmabuf's maintainer.

2 is hard, but may worth to research, which I also will try to do. :)

Another option is to just limit udmabuf's vmap() to only shmem folios but I guess it may not work as expected if THP is also enabled.
Bingbu, what exactly is your use-case? Could you please describe the scenario that requires you to use udmabuf's vmap()?

Thanks,
Vivek

Quoted 1 line
#17 lore raw

Vivek,

Quoted 28 lines · Vivek

Hi Huan,

Quoted 26 lines

Subject: Re: [PATCH] mm/vmalloc: fix mischeck pfn valid in vmap_pfns

HI Hillf,

Thanks fo your suggestion.

Quoted 9 lines · Hillf Danton
Quoted 5 lines · On Wed, 19 Mar 2025 09:08:51 +0100 Christoph Hellwig wrote:
Quoted 2 lines · Hillf Danton

A quick fix is to add a vmap_pfn variant to walk around the pfn check in question, like the following diff (just for idea show).

No.

Patient to see your fix, given no low hanging peach left for Mr Folio in case of HVO, (feel free to ignore, who is likely about 2.6-mile less tough than you could be).

I now believe there are two way to resolve the HVO folio can't vmap in udmabuf.

1. simple copy vmap_pfn code, don't bother common vmap_pfn, use by itself and remove pfn_valid check.

2. implement folio array based vmap(vmap_folios), which can given a range of each folio(offset, nr_pages), so can suit HVO folio's vmap.

Quoted 8 lines
Quoted 4 lines

1 is simple and can fast fix this issue, but may not too good. I need discuss with bot Christoph and udmabuf's maintainer.

2 is hard, but may worth to research, which I also will try to do. :)

Another option is to just limit udmabuf's vmap() to only shmem folios but I guess it may not work as expected if THP is also enabled.
Bingbu, what exactly is your use-case? Could you please describe the scenario that requires you to use udmabuf's vmap()?

I guess my case is a very simple one.

Userspace create the udmabuf and mmap:

dmabuf_fd = ioctl(udma_fd, UDMABUF_CREATE, &create); userptr = mmap(NULL, len, PROT_READ | PROT_WRITE,

	  MAP_SHARED, dmafd, 0);

And driver side call dma_buf_vmap() on the dmabuf to get a virtual address.

Quoted 6 lines

Thanks,
Vivek

Quoted 1 line
Signature · 2 lines
Best regards,
Bingbu Cao
#20 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 1 line · Huan Yang

A folio may be 2MB or more large 1GB, what if we only need a little, 1M or 512MB, can vmap based on folio can solve it?

Then you only map part of it by passing a length argument. Note that in general when you have these large folios you also don't have highmem, so if you only map one of them, or part of one of them you don't actually need vmap at all and can just use folio-address..

Quoted 5 lines
Quoted 1 line

Normally, can offer 4k-page based array map it. But consider HVO, can't. That's why wanto base on pfn.

Well, for any large folio using this 4k based page interface is actually highly inefficient. So let's fix that. And my loop in willy as Mr. Folio while you're at it.

The minimum map unit is page size instead of variable-size folio.

For many cases, vmap (to combine many partial folios) is useful (instead of split all folios to order-0 folios in advance) but I agree page array may be inefficient.

So I don't think another folio vmap() version is better overall anyway.

Thanks,
Gao Xiang

#21 lore raw
Quoted 8 lines · Gao Xiang

The minimum map unit is page size instead of variable-size folio.

For many cases, vmap (to combine many partial folios) is useful (instead of split all folios to order-0 folios in advance) but I agree page array may be inefficient.

So I don't think another folio vmap() version is better overall anyway.

Then just reject the mappings for now. vmap/vm_map_ram isn't really intended for mapping totally arbitrary scattered memory anyway. As mentioned before udmabuf also has a grave bug in the dma mapping part, so just marking it broken would be another very sensible option.

author
#22 lore raw

HI Christoph

Sorry for the later reply.

Quoted 2 lines · Christoph Hellwig

As mentioned before udmabuf also has a grave bug in the dma mapping part, so just marking it broken would be another very sensible

Could you please provide a more detailed description of this?

I'm not the maintainer, but I see the udmabuf's begin patch mentioned as:

A driver to let userspace turn memfd regions into dma-bufs.

Use case:  Allows qemu create dmabufs for the vga framebuffer or virtio-gpu ressources.  Then they can be passed around to display those guest things on the host.  To spice client for classic full framebuffer display, and hopefully some day to wayland server for seamless guest window display.

https://patchwork.freedesktop.org/patch/246100/

Did it also mean to do it?

I'd like to learn your thinking

Thank you,

Huan Yang

Quoted 1 line

option.

#23 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Huan Yang

This prevents us from properly invoking vmap, which is why we have turned to using vmap_pfn instead.

Even if a folio-based vmap is implemented, it still cannot handle mapping multiple folio ranges of physical

memory to vmalloc regions. A range of folio is important, it maybe an offset in memfd, no need entire folio.

Quoted 7 lines
Quoted 1 line

So, I still consider vmap_pfn to be the optimal solution for this specific scenario. :)

No, vmap_pfn is entirely for memory not backed by pages or folios, i.e. PCIe BARs and similar memory. This must not be mixed with proper folio backed memory.

So you'll need a vmap for folios to support this use case.

https://lore.kernel.org/linux-mm/[email protected]/
seems like a good match for "we need to vmap a range from a file".

Vishal has some updates since this version, and I'm sure he can send them out after LSFMM.

author
#24 lore raw
In reply to Bingbu Cao

Hi Bingbu

Quoted 24 lines · Bingbu Cao
Quoted 23 lines · Huan Yang

HI Christoph,

Thanks for your reply, and I'm sorry for my late reply. Your response didn't appear in my email client; I only saw it on the website.:(

Quoted 2 lines
Quoted 2 lines · Huan Yang

When invoke vmap_pfns, it call vmap_pfn_apply to set pfn into pte. >> It check pfn is valid, if true then warn and return. >> >> This is

a mischeck, actually we need set a valid pfn into pte, not an >> invalid pfn. >

Quoted 1 line

As just discussed this is wrong. vmap_pfn is for mapping non-page

Thank you for your explanation. I now understand that the design of vmap_pfn is indeed intentional. It's design to do this.

Quoted 3 lines

PFNs and the check is what enforces that. What is the point of having that detailed discussion if you just send the broken patch anyway with a commit log not even acknowledging the facts?

Sorry for that.

We now have a new use case where, in udmabuf, memory is passed via memfd and can
be either shmem or hugetlb.
When the memory is hugetlb and HVO is enabled, the tail page's struct is no longer
reliable because it has been freed. Can't use vmap.
Therefore, when making modifications, I recorded the pfn of the folio base pfn + offset and called vmap_pfns. And, these pfns are valid. So rejected by vmap_pfns.

Quoted 6 lines
Quoted 2 lines

Can we just remove pfn_valid check in vmap_pfns, so make it suit for both of they? If you agree, I wanna send a new patch.

Huan,

Why not update udmabuf to make it work with both vmap_pfns() and vmap()? As only the udmabuf knows it is actually working on?

You mean, If udmabuf invoke vmap if it's normal page-based folio range, else invoke vmap_pfns

if it's in HVO based?

udmabuf can contained a rane in folio and offset, what if it contains folio's head(with page struct) and

remain tail(without page struct, freed by HVO).

I think there are no suitable way to map it into vmalloc area.:)

Or else, just block hugetlb's folio mapped into vmalloc area? Which I don't think it's a good way.

Quoted 2 lines

I don't think it's a good idea to hack the common API, the WARN_ON()

You mean common, but I think vmalloc can provide a more common API that not care if page it's

exist, if provide pfn, just map? :)

Or else, document it that vmap_pfn just do not welcome page based pfn map?(Just IMO)

Thanks,

Huan Yang

Quoted 5 lines

is really a mandatory check, and current case is a good example.

Quoted 3 lines

Thank you,
Huan Yang