Skip to contentSkip to conversation

huge ioremap from kernel 4.1

Xenomai Henning Schild 1 participant 5 messages Feb 4, 2016 – Feb 8, 2016 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hey Toshi,

i am working on the xenomai project where we recently rebased our patches on 4.1. The fact that io-regions may be mapped as huge pages caused some problem in our patch.

Whenever memory is vmalloced or ioremapped we make sure it is synched into all mm's to avoid page faults in that area. We use vmalloc_sync_one to do that. Now vmalloc_sync_one can not handle huge pages (i am talking about x86 32+64). Hence for areas where you introduced huge pages vmalloc_fault should never happen in any mm, otherwise your patchset would have introduced a regression in mainline.

Can you help me understand by which mechanism these io-regions are always mapped into all mm's, ruling out calls to vmalloc_fault and vmalloc_sync_one? Because calling this on these regions will treat you device memory as pagetables when huge pages are used.

Best regards,
Henning

#2 lore raw
In reply to a message not in the archive
Quoted 15 lines · On Thu, 4 Feb 2016 09:41:16 -0700 Toshi Kani <[email protected]> wrote:
Quoted 14 lines · Henning Schild

Hey Toshi,

i am working on the xenomai project where we recently rebased our patches on 4.1. The fact that io-regions may be mapped as huge pages caused some problem in our patch.

Whenever memory is vmalloced or ioremapped we make sure it is synched into all mm's to avoid page faults in that area. We use vmalloc_sync_one to do that. Now vmalloc_sync_one can not handle huge pages (i am talking about x86 32+64). Hence for areas where you introduced huge pages vmalloc_fault should never happen in any mm, otherwise your patchset would have introduced a regression in mainline.

Quoted 8 lines
Quoted 4 lines

Can you help me understand by which mechanism these io-regions are always mapped into all mm's, ruling out calls to vmalloc_fault and vmalloc_sync_one? Because calling this on these regions will treat you device memory as pagetables when huge pages are used.

The hugepage feature is enabled on x86 PAE and 64.  You can disable this hugepage feature by using "nohugeiomap" kernel option.  You can use this option to workaround the issue for the time being.

That is what we do at the moment but in the long run we would like our patch to be able to use the feature.

Quoted 3 lines

That said, vmalloc_sync_one() may not be used for ioremap'd ranges. (BTW, this function is defined on 32-bit only, so I assume you only call it on 32-bit, and do not see the problem on 64-bit..?)

Back in 4.1 there is a 32- and 64-bit version. I saw that the 64bit version is gone in the meantime, but probably just folded into its single user or something.

Do you happen to know the reason why it may not be used? Is there another reason except the now obvious "cant deal with huge pages" vs. "contains huge pages since 4.1".

Quoted 4 lines

ioremap
itself sets up the page tables for the entire range.  Hence, there is no page fault when you access to the ioremap'd range.  If you simply remove this call, I think you should be fine.

Removing the call is exactly what i plan to do. But before doing that i would like to understand why faults in that area can never happen. When calling ioremap the current mm will get the full mapping, nothing lazy here. But how do all the other pre-existing mm's also get that mapping?
Say a device interrupt with iomem-access in the handler hits the init-process. That wont cause a fault because somehow the mappings where synched into the init-mm from the modprobe-mm, right? How and where is that done?

One possible way would be sharing of pagetables.

regards,
Henning

#4 lore raw
In reply to a message not in the archive
Quoted 64 lines · On Thu, 4 Feb 2016 13:47:23 -0700 Toshi Kani <[email protected]> wrote:
Quoted 37 lines · Henning Schild
Quoted 21 lines · On Thu, 4 Feb 2016 09:41:16 -0700 Toshi Kani <[email protected]> wrote:
Quoted 14 lines · Henning Schild

Hey Toshi,

i am working on the xenomai project where we recently rebased our patches on 4.1. The fact that io-regions may be mapped as huge pages caused some problem in our patch.

Whenever memory is vmalloced or ioremapped we make sure it is synched into all mm's to avoid page faults in that area. We use vmalloc_sync_one to do that. Now vmalloc_sync_one can not handle huge pages (i am talking about x86 32+64). Hence for areas where you introduced huge pages vmalloc_fault should never happen in any mm, otherwise your patchset would have introduced a regression in mainline.

Quoted 5 lines

Can you help me understand by which mechanism these io-regions are always mapped into all mm's, ruling out calls to vmalloc_fault and vmalloc_sync_one? Because calling this on these regions will treat you device memory as pagetables when huge pages are used.

Quoted 4 lines

The hugepage feature is enabled on x86 PAE and 64.  You can disable this hugepage feature by using "nohugeiomap" kernel option.  You can use this option to workaround the issue for the time being.

That is what we do at the moment but in the long run we would like our patch to be able to use the feature.

Quoted 4 lines

That said, vmalloc_sync_one() may not be used for ioremap'd ranges. (BTW, this function is defined on 32-bit only, so I assume you only call it on 32-bit, and do not see the problem on 64-bit..?)

Quoted 7 lines

Back in 4.1 there is a 32- and 64-bit version. I saw that the 64bit version is gone in the meantime, but probably just folded into its single user or something.

Do you happen to know the reason why it may not be used? Is there another reason except the now obvious "cant deal with huge pages" vs. "contains huge pages since 4.1".

No, and now that I think this should be cleaned up...

Quoted 16 lines
Quoted 4 lines

ioremap
itself sets up the page tables for the entire range.  Hence, there is no page fault when you access to the ioremap'd range. If you simply remove this call, I think you should be fine.

Removing the call is exactly what i plan to do. But before doing that i would like to understand why faults in that area can never happen. When calling ioremap the current mm will get the full mapping, nothing lazy here. But how do all the other pre-existing mm's also get that mapping?
Say a device interrupt with iomem-access in the handler hits the init-process. That wont cause a fault because somehow the mappings where synched into the init-mm from the modprobe-mm, right? How and
where is that done?

Quoted 6 lines
Quoted 1 line

One possible way would be sharing of pagetables.

When a process is created via fork(), it calls pgd_alloc(), which then calls pgd_ctor().  This function copies the kernel's pgd entries to the process's pgd for the kernel ranges.  So, process's pgd shares the same page tables for the kernel space.

Sure a new process will inherit the current kernel reference. The pinning we do is for pre-existing mm's that did not inherit those parts when they where forked. Hence the example with init and a later insmod causing ioremap. There has to be a way to sync the changes from the insmod mm into the init-mm. Otherwise running the device driver in the init-mm could cause #PFs.
So i am afraid it is still not completely clear to me.

Quoted 1 line

Let me know if I am missing something.

Sorry to bother you with these questions, but you obviously know that subsystem better than me.

regards,
Henning

#6 lore raw
In reply to a message not in the archive
Quoted 23 lines · On Fri, 5 Feb 2016 12:12:23 -0700 Toshi Kani <[email protected]> wrote:
Quoted 2 lines · Henning Schild

On Thu, 4 Feb 2016 13:47:23 -0700
Toshi Kani <[email protected]> wrote:

 :

Quoted 6 lines
Quoted 5 lines

When a process is created via fork(), it calls pgd_alloc(), which then calls pgd_ctor().  This function copies the kernel's pgd entries to the process's pgd for the kernel ranges.  So, process's pgd shares the same page tables for the kernel space.

Quoted 7 lines

Sure a new process will inherit the current kernel reference. The pinning we do is for pre-existing mm's that did not inherit those parts when they where forked. Hence the example with init and a later insmod causing ioremap. There has to be a way to sync the changes from the insmod mm into the init-mm. Otherwise running the device driver in the init-mm could cause #PFs.
So i am afraid it is still not completely clear to me.

Agreed.  We need to sync when a run-time call to ioremap caused allocation of a new pud table.

Quoted 2 lines
Quoted 1 line

Let me know if I am missing something.

Quoted 8 lines
Quoted 2 lines

Sorry to bother you with these questions, but you obviously know that subsystem better than me.

Thanks for pointing this out.  I will work on the fix. -Toshi

ps.
xenomai list rejects my reply since I'm not a subscriber.

I do not know how this list in configured, but that should probably be fixed. But this answer to your mail should make sure it ends up in the archive.

Henning

#8 lore raw
In reply to a message not in the archive
Quoted 27 lines · On Fri, 5 Feb 2016 16:53:25 -0700 Toshi Kani <[email protected]> wrote:
Quoted 26 lines · Toshi Kani
Quoted 2 lines · Henning Schild

On Thu, 4 Feb 2016 13:47:23 -0700
Toshi Kani <[email protected]> wrote:

 :

Quoted 8 lines
Quoted 7 lines

When a process is created via fork(), it calls pgd_alloc(), which then
calls pgd_ctor().  This function copies the kernel's pgd entries to the process's pgd for the kernel ranges.  So, process's pgd shares the same page tables for the kernel space.

Quoted 7 lines

Sure a new process will inherit the current kernel reference. The pinning we do is for pre-existing mm's that did not inherit those parts when they where forked. Hence the example with init and a later insmod causing ioremap. There has to be a way to sync the changes from the insmod mm into the init-mm. Otherwise running the device driver in the init-mm could cause #PFs.
So i am afraid it is still not completely clear to me.

Agreed.  We need to sync when a run-time call to ioremap caused allocation
of a new pud table.

Quoted 2 lines
Quoted 1 line

Let me know if I am missing something.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Sorry to bother you with these questions, but you obviously know that subsystem better than me.

Thanks for pointing this out.  I will work on the fix.

Can you test the attached patch to see if it solves your problem?  I have tested 64-bit, but have not been able to test 32-bit yet due to an issue in my setup.

I will give it a try, but since we are still at 4.1 i need to backport it. Plus it actually looks exactly like what i already came up with.

http://xenomai.org/pipermail/xenomai/2016-February/035822.html

But before making that part of "our" xenomai patch-stack we wanted to understand why mainline did not have the problem we experienced.

That looks like the kind of patch that needs backporting all the way to 4.1. Who is responsible for such a backport?

Henning