Make ioremap'ed memory available through rtdm_mmap_to_user()
RFC PATCH 1/1 Make ioremap'ed memory available through rtdm_mmap_to_user()
Hi,
I need to be able to map an IO memory buffer to userspace from a RTDM driver.
rtdm_mmap_to_user() seems to do what I need, but it doesn't work. Its code thinks that all virtual addresses between VMALLOC_START and VMALLOC_END are obtained through vmalloc() and tries to call xnarch_remap_vm_page() on them, which fails.
Virtual addresses coming from ioremap() need to go through xnarch_remap_io_page_range(), and their physical address cannot be obtained with a simple virt_to_phys().
A working patch is attached below, but there might (should ?) be a better way to do it. Some of the code may also belong to asm-generic/system.h instead of the RTDM skin.
Note that you may also need to EXPORT_SYMBOL(vmlist and vmlist_lock) in mm/vmalloc.c if you want to build the RTDM skin as a module.
Comments ?
Stelian.
skins/rtdm/drvlib.c +15 -0
@@ -1377,6 +1377,7 @@ {struct rtdm_mmap_data *mmap_data = filp->private_data;unsigned long vaddr, maddr, size; +struct vm_struct *vm; vma->vm_ops = mmap_data->vm_ops; vma->vm_private_data = mmap_data->vm_private_data;
@@ -1385,7 +1386,21 @@ maddr = vma->vm_start; size = vma->vm_end - vma->vm_start; +write_lock (&vmlist_lock); +for (vm = vmlist; vm !=NULL ; vm = vm->next) { +if (vm->addr == (void *)vaddr) +break ; + } +write_unlock (&vmlist_lock); + +/* ioremap'ed memory */ +if (vm && vm->flags &VM_IOREMAP ) +return xnarch_remap_io_page_range (vma, maddr, + vm->phys_addr, + size,PAGE_SHARED ); + else#ifdef CONFIG_MMU +/* vmalloc'ed memory */ if ((vaddr >=VMALLOC_START ) && (vaddr <VMALLOC_END )) {unsigned long mapped_size =0 ;
Signature · 1 line
Stelian Pop <[email protected]>
Quoted 9 lines · Stelian Pop
Hi,
I need to be able to map an IO memory buffer to userspace from a RTDM driver.
rtdm_mmap_to_user()seems to do what I need, but it doesn't work. Its code thinks that all virtual addresses betweenVMALLOC_STARTandVMALLOC_ENDare obtained throughvmalloc()and tries to callxnarch_remap_vm_page()on them, which fails.
Ok, interesting (one never stops learning).
Quoted 5 lines
Virtual addresses coming from
ioremap()need to go throughxnarch_remap_io_page_range(), and their physical address cannot be obtained with a simplevirt_to_phys().
Quoted 8 lines
A working patch is attached below, but there might (should ?) be a better way to do it. Some of the code may also belong to
asm-generic/system.hinstead of the RTDM skin.Note that you may also need to
EXPORT_SYMBOL(vmlist and vmlist_lock)inmm/vmalloc.cif you want to build the RTDM skin as a module.Comments ?
In case no one comes up with an easy, portable way to detect remapped memory as well: What about some flags the caller of rtdm_mmap_to_user has to pass, telling what kind of memory it is? Would simplify the RTDM part, and the user normally knows quite well where the memory came from. And I love to break APIs. :)
I'm CC'ing the first users of this service to ask for some feedback from their POV. Hope gna does not -once again- mangles the CCs.
Jan
Quoted 5 lines · Jan Kiszka
In case no one comes up with an easy, portable way to detect remapped memory as well: What about some flags the caller of
rtdm_mmap_to_userhas to pass, telling what kind of memory it is? Would simplify the RTDM part, and the user normally knows quite well where the memory came from. And I love to break APIs. :)
This would be perfect. We could even reuse the prot field for that (PROT_READ | PROT_WRITE | PROT_VMALLOC | PROT_IOREMAP). Not the cleanest solution, but it won't break the API this way.
Or maybe we should lower the API level a little bit, and let the user specify the physical address of the mapping instead of the virtual one....
Stelian.
Signature · 1 line
Stelian Pop <[email protected]>
Quoted 11 lines · Stelian Pop
Quoted 5 lines · Jan Kiszka
In case no one comes up with an easy, portable way to detect remapped memory as well: What about some flags the caller of
rtdm_mmap_to_userhas to pass, telling what kind of memory it is? Would simplify the RTDM part, and the user normally knows quite well where the memory came from. And I love to break APIs. :)This would be perfect. We could even reuse the prot field for that (
PROT_READ|PROT_WRITE|PROT_VMALLOC|PROT_IOREMAP). Not the cleanest solution, but it won't break the API this way.
It would not change the function signature, but its argument semantics. We should better make a clean cut which signals via compiler errors that something changed.
Quoted 4 lines
Or maybe we should lower the API level a little bit, and let the user specify the physical address of the mapping instead of the virtual one....
How would this help? You still need the virtual address for VMA blocks in order to collect the pages.
I really think a separate mem-type argument is what we should do. And here comes a first patch in this direction. Only compile-tested so far, feedback is welcome.
Jan
rtdm/rtdm_driver.h +20 -1
@@ -1021,6 +1021,25 @@ int rtdm_mutex_timedlock(rtdm_mutex_t *m/* --- utility functions --- */ +/*! +* @addtogroup util +* @{ +*/ + +/*! +* @anchor RTDM_MEMTYPE_xxx @name Memory Types +* Flags defining the type of a memory region +* @{ +*/ +/** Allocated with kmalloc() */ +#define RTDM_MEMTYPE_KMALLOC 0x00 +/** Remapped physical memory */ +#define RTDM_MEMTYPE_IOREMAP 0x01 +/** Allocated with vmalloc() */ +#define RTDM_MEMTYPE_VMALLOC 0x02 +/** @} Memory Types */ +/** @} util */ +#define rtdm_printk (format, ...) printk(format, ##__VA_ARGS__)#ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
@@ -1036,7 +1055,7 @@ static inline void rtdm_free(void *ptr)#ifdef CONFIG_XENO_OPT_PERVASIVE int rtdm_mmap_to_user (rtdm_user_info_t *user_info,void *src_addr,size_t len, -int prot,void **pptr, +int mem_type,int prot,void **pptr,struct vm_operations_struct *vm_ops,void *vm_private_data);int rtdm_munmap (rtdm_user_info_t *user_info,void *ptr,size_t len);
skins/rtdm/drvlib.c +7 -3
@@ -1369,6 +1369,7 @@ void rtdm_nrtsig_pend(rtdm_nrtsig_t *nrt#if defined(CONFIG_XENO_OPT_PERVASIVE ) || defined(DOXYGEN_CPP )struct rtdm_mmap_data {void *src_addr; +int mem_type;struct vm_operations_struct *vm_ops;void *vm_private_data; };
@@ -1386,7 +1387,7 @@ static int rtdm_mmap_buffer(struct file size = vma->vm_end - vma->vm_start;#ifdef CONFIG_MMU -if ((vaddr >=VMALLOC_START ) && (vaddr <VMALLOC_END )) { +if (mmap_data->mem_type ==RTDM_MEMTYPE_VMALLOC ) {unsigned long mapped_size =0 ;XENO_ASSERT (RTDM , (vaddr ==PAGE_ALIGN (vaddr)), return -EINVAL );
@@ -1419,6 +1420,8 @@ static struct file_operations rtdm_mmap_* device operation handler * @param[in] src_addr Kernel address to be mapped * @param[in] len Length of the memory range +* @param[in] mem_type Type of the passed memory range, see +* @ref RTDM_MEMTYPE_xxx * @param[in] prot Protection flags for the user's memory range, typically * either PROT_READ or PROT_READ|PROT_WRITE * @param[in,out] pptr Address of a pointer containing the desired user
@@ -1463,11 +1466,12 @@ static struct file_operations rtdm_mmap_* Rescheduling: possible. */ int rtdm_mmap_to_user (rtdm_user_info_t *user_info,void *src_addr,size_t len, -int prot,void **pptr, +int mem_type,int prot,void **pptr,struct vm_operations_struct *vm_ops,void *vm_private_data) { -struct rtdm_mmap_data mmap_data = {src_addr, vm_ops, vm_private_data}; +struct rtdm_mmap_data mmap_data = { src_addr, mem_type, vm_ops, + vm_private_data };struct file *filp;const struct file_operations *old_fops;void *old_priv_data;
[Sorry for the delay, I was quite busy those days and unable to test the patch...]
Quoted 7 lines · Jan Kiszka
Quoted 4 lines
Or maybe we should lower the API level a little bit, and let the user specify the physical address of the mapping instead of the virtual one....
How would this help? You still need the virtual address for VMA blocks in order to collect the pages.
Actualy we need both the virtual address (for vmalloc case) and the physical address (for ioremap case).
Quoted 1 line
+ if (
mmap_data->mem_type==RTDM_MEMTYPE_VMALLOC) {
[...]
This still won't work because for RTDM_MEMTYPE_IOREMAP virt_to_phys((void *)vaddr) doesn't return the physical address.
So we still need to either:
a) walk the vmlist struct to find out the needed vm->phys_addr (which
will probably won't work in 2.4, or even in earlier 2.6 kernels)
b) pass both the physical and the virtual adresses in thertdm_mmap_to_user prototype (the physical address would be used only when RTDM_MEMTYPE_IOREMAP is specified)
c) make 'src_addr' be a virtual address in some cases, and a physical
address in ioremap case.
d) make a special rtdm_mmap_iomem_to_user() function...
The (tested) patch below implements the b) case.
rtdm/rtdm_driver.h +22 -2
@@ -1021,6 +1021,25 @@/* --- utility functions --- */ +/*! +* @addtogroup util +* @{ +*/ + +/*! +* @anchor RTDM_MEMTYPE_xxx @name Memory Types +* Flags defining the type of a memory region +* @{ +*/ +/** Allocated with kmalloc() */ +#define RTDM_MEMTYPE_KMALLOC 0x00 +/** Remapped physical memory */ +#define RTDM_MEMTYPE_IOREMAP 0x01 +/** Allocated with vmalloc() */ +#define RTDM_MEMTYPE_VMALLOC 0x02 +/** @} Memory Types */ +/** @} util */ +#define rtdm_printk (format, ...) printk(format, ##__VA_ARGS__)#ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
@@ -1035,8 +1054,9 @@ }#ifdef CONFIG_XENO_OPT_PERVASIVE -int rtdm_mmap_to_user (rtdm_user_info_t *user_info,void *src_addr,size_t len, -int prot,void **pptr, +int rtdm_mmap_to_user (rtdm_user_info_t *user_info, +void *src_vaddr,unsigned long src_paddr,size_t len, +int mem_type,int prot,void **pptr,struct vm_operations_struct *vm_ops,void *vm_private_data);int rtdm_munmap (rtdm_user_info_t *user_info,void *ptr,size_t len);
skins/rtdm/drvlib.c +22 -10
@@ -1368,7 +1368,9 @@#if defined(CONFIG_XENO_OPT_PERVASIVE ) || defined(DOXYGEN_CPP )struct rtdm_mmap_data { -void *src_addr; +void *src_vaddr; +unsigned long src_paddr; +int mem_type;struct vm_operations_struct *vm_ops;void *vm_private_data; };
@@ -1376,17 +1378,22 @@static int rtdm_mmap_buffer (struct file *filp,struct vm_area_struct *vma) {struct rtdm_mmap_data *mmap_data = filp->private_data; -unsigned long vaddr, maddr, size; +unsigned long vaddr, paddr, maddr, size; vma->vm_ops = mmap_data->vm_ops; vma->vm_private_data = mmap_data->vm_private_data; - vaddr = (unsigned long )mmap_data->src_addr; + vaddr = (unsigned long )mmap_data->src_vaddr; maddr = vma->vm_start; size = vma->vm_end - vma->vm_start; +if (mmap_data->mem_type ==RTDM_MEMTYPE_IOREMAP ) + paddr = mmap_data->src_paddr; +else + paddr =virt_to_phys ((void *)vaddr); +#ifdef CONFIG_MMU -if ((vaddr >=VMALLOC_START ) && (vaddr <VMALLOC_END )) { +if (mmap_data->mem_type ==RTDM_MEMTYPE_VMALLOC ) {unsigned long mapped_size =0 ;XENO_ASSERT (RTDM , (vaddr ==PAGE_ALIGN (vaddr)), return -EINVAL );
@@ -1403,8 +1410,7 @@ return 0; } else#endif /* CONFIG_MMU */ -return xnarch_remap_io_page_range (vma, maddr, -virt_to_phys ((void *)vaddr), +return xnarch_remap_io_page_range (vma, maddr, paddr, size,PAGE_SHARED ); }
@@ -1417,8 +1423,12 @@* * @param[in] user_info User information pointer as passed to the invoked * device operation handler -* @param[in] src_addr Kernel address to be mapped +* @param[in] src_vaddr Kernel virtual address to be mapped +* @param[in] src_paddr Kernel physical address to be mapped (used only for IO +* memory, i.e. mem_type=RTDM_MEMTYPE_IOREMAP) * @param[in] len Length of the memory range +* @param[in] mem_type Type of the passed memory range, see +* @ref RTDM_MEMTYPE_xxx * @param[in] prot Protection flags for the user's memory range, typically * either PROT_READ or PROT_READ|PROT_WRITE * @param[in,out] pptr Address of a pointer containing the desired user
@@ -1462,12 +1472,14 @@* * Rescheduling: possible. */ -int rtdm_mmap_to_user (rtdm_user_info_t *user_info,void *src_addr,size_t len, -int prot,void **pptr, +int rtdm_mmap_to_user (rtdm_user_info_t *user_info, +void *src_vaddr,unsigned long src_paddr,size_t len, +int mem_type,int prot,void **pptr,struct vm_operations_struct *vm_ops,void *vm_private_data) { -struct rtdm_mmap_data mmap_data = {src_addr, vm_ops, vm_private_data}; +struct rtdm_mmap_data mmap_data = { src_vaddr, src_paddr, mem_type, + vm_ops, vm_private_data };struct file *filp;const struct file_operations *old_fops;void *old_priv_data;
Signature · 1 line
Stelian Pop <[email protected]>
Quoted 14 lines · Stelian Pop
[Sorry for the delay, I was quite busy those days and unable to test the patch...]
Quoted 5 lines · Jan Kiszka
Quoted 3 lines
Or maybe we should lower the API level a little bit, and let the user specify the physical address of the mapping instead of the virtual one....
How would this help? You still need the virtual address for VMA blocks in order to collect the pages.
Actualy we need both the virtual address (for vmalloc case) and the physical address (for ioremap case).
Quoted 1 line
+ if (
mmap_data->mem_type==RTDM_MEMTYPE_VMALLOC) {
Quoted 8 lines
[...]
This still won't work because for
RTDM_MEMTYPE_IOREMAPvirt_to_phys((void *)vaddr)doesn't return the physical address.So we still need to either:
a) walk the vmlist struct to find out the needed vm->phys_addr (whichwill probably won't work in 2.4, or even in earlier 2.6 kernels)
Sounds fragile, indeed.
Quoted 4 lines
b) pass both the physical and the virtual adresses in the
rtdm_mmap_to_userprototype (the physical address would be used only whenRTDM_MEMTYPE_IOREMAPis specified)
Hmm, a bit ugly, this long argument list.
Quoted 3 lines
c) make '
src_addr' be a virtual address in some cases, and a physical
address in ioremap case.
That's so far the cleanest solution in my eyes.
Quoted 2 lines
d) make a special
rtdm_mmap_iomem_to_user()function...
Also an option. Specifically, it wouldn't break the existing API... What about rtdm_iomap_to_user? Would you like to work out a patch in this direction?
Jan
Quoted 5 lines · Jan Kiszka
Quoted 1 line
d) make a special
rtdm_mmap_iomem_to_user()function...Also an option. Specifically, it wouldn't break the existing API... What about
rtdm_iomap_to_user? Would you like to work out a patch in this direction?
Here it comes.
rtdm/rtdm_driver.h +7 -1
@@ -1035,10 +1035,16 @@ }#ifdef CONFIG_XENO_OPT_PERVASIVE -int rtdm_mmap_to_user (rtdm_user_info_t *user_info,void *src_addr,size_t len, +int rtdm_mmap_to_user (rtdm_user_info_t *user_info, +void *src_addr,size_t len,int prot,void **pptr,struct vm_operations_struct *vm_ops,void *vm_private_data); +int rtdm_iomap_to_user (rtdm_user_info_t *user_info, +unsigned long src_addr,size_t len, +int prot,void **pptr, +struct vm_operations_struct *vm_ops, +void *vm_private_data);int rtdm_munmap (rtdm_user_info_t *user_info,void *ptr,size_t len); static inline int rtdm_read_user_ok(rtdm_user_info_t *user_info,
skins/rtdm/drvlib.c +118 -39
211 lines not on this page · raw message
Signature · 1 line
Stelian Pop <[email protected]>
Quoted 8 lines · Stelian Pop
Quoted 4 lines · Jan Kiszka
Quoted 1 line
d) make a special
rtdm_mmap_iomem_to_user()function...Also an option. Specifically, it wouldn't break the existing API... What about
rtdm_iomap_to_user? Would you like to work out a patch in this direction?Here it comes.
Your patch looks very good. Assuming that you have tested it successfully, I'm going to merge it soon.
Thanks,
Jan
Quoted 4 lines · Jan Kiszka
Quoted 3 lines · Stelian Pop
Quoted 1 line · Jan Kiszka
Quoted 1 line
d) make a special
rtdm_mmap_iomem_to_user()function...
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Also an option. Specifically, it wouldn't break the existing API... What about
rtdm_iomap_to_user? Would you like to work out a patch in this direction?Here it comes.
Your patch looks very good. Assuming that you have tested it successfully,
I did.
Quoted 1 line
I'm going to merge it soon.
Thanks.
Signature · 1 line
Stelian Pop <[email protected]>