Skip to contentSkip to conversation

Using shared memory, Xenomai 2.4.2

Xenomai-help Bosko Radivojevic 2 participants 16 messages Mar 28, 2008

lore
Share & tools
lore
#0 lore raw

Hi!

I'm using Xenomai 2.4.2 with Linux 2.6.20.21 (Adeos-ipipe 1.8-03) on ARM9 (AT91SAM9260 based). I have a problem with using shared memory between kernel and user space. I've tried on user space side both native and posix skins.

Here are snippets:

--- rt_shm.c - kernel module ---
RT_TASK rt_shm_task;
RT_HEAP heap_desc;
void *shared_mem;
[..]

  if (!rt_heap_create (&heap_desc, "MainSHM", 1024, H_SHARED)) {
    printk ("Error rt_heap_create");

}

  if (!rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared_mem)) {
    printk ("Error rt_heap_alloc");

}
[..]
============================
--- user.c - userspace app ---
RT_HEAP heap_desc;
void *shared_mem;
[..]
err = rt_heap_bind (&heap_desc, "MainSHM", TM_NONBLOCK);

  if (err) {
    printf ("rt_heap_bind error: %d!\n", err);
    return 0;

}

rt_heap_alloc (&heap_desc, 0, TM_NONBLOCK, &shared_mem);
[..]
============================
I load rt_shm.ko, I can see it running, no errors returned during rt_heap_* calls (I'm incrementing counter inside shared heap memory, and displaying every 10000th time). When I start user space application I get:

rt_heap_bind error: -11!

So, rt_heap_bind is returning -EWOULDBLOCK. Why?

Also, I've enable during kernel compilation in posix skin option 'shared memory'. Also, I tried with posix skin in user space:
[..]
fd = shm_open ("MainSHM", O_RDWR, 0);
[..]

When I start it, on the console I can see:
shm_open: No such file or directory
Xenomai: POSIX: destroyed thread c4802400

Any ideas?

Thanks!

#1 lore raw
Quoted 9 lines · Bosko Radivojevic

Also, I've enable during kernel compilation in posix skin option 'shared memory'. Also, I tried with posix skin in user space:
[..]
fd = shm_open ("MainSHM", O_RDWR, 0);
[..]

When I start it, on the console I can see:
shm_open: No such file or directory
Xenomai: POSIX: destroyed thread c4802400

Native and POSIX shared heap namespaces are separated. To create a shared heap for communications between kernel-space and user-space using posix interface, you have to pass O_CREAT to shm_open on either side.

Signature · 1 line
 Gilles
#2 lore raw

Ok. But, actually I dont need native-to-posix mode. I just need to share memory betweend kernel thread and user space application.

Quoted 20 lines · Gilles Chanteperdrix
Quoted 9 lines · Bosko Radivojevic

Also, I've enable during kernel compilation in posix skin option 'shared memory'. Also, I tried with posix skin in user space:
[..]
fd = shm_open ("MainSHM", O_RDWR, 0);
[..]

When I start it, on the console I can see:
shm_open: No such file or directory
Xenomai: POSIX: destroyed thread c4802400

Native and POSIX shared heap namespaces are separated. To create a shared heap for communications between kernel-space and user-space using posix interface, you have to pass O_CREAT to shm_open on either side.

--
Gilles

Signature · 1 line
Sent from Gmail for mobile | mobile.google.com
#3 lore raw
Quoted 1 line · Bosko Radivojevic

Ok. But, actually I dont need native-to-posix mode.

Good, because it is impossible.

I just need to

Quoted 1 line

share memory betweend kernel thread and user space application.

See the posix stach example.

Signature · 1 line
 Gilles
#4 lore raw
Quoted 6 lines · Gilles Chanteperdrix

On Fri, Mar 28, 2008 at 2:46 PM, Bosko Radivojevic

Quoted 1 line · <[email protected]> wrote:

Ok. But, actually I dont need native-to-posix mode.

Quoted 7 lines

Good, because it is impossible.

I just need to

Quoted 1 line

share memory betweend kernel thread and user space application.

See the posix stach example.

satch, not statch. It is in examples/posix/satch.c.

Signature · 1 line
 Gilles
#5 lore raw

Ok, I'll try that. But, is there something wrong with my code/approach or is it some kind of arm-specific bug?

Quoted 21 lines · Gilles Chanteperdrix
Quoted 6 lines · Gilles Chanteperdrix

On Fri, Mar 28, 2008 at 2:46 PM, Bosko Radivojevic

Quoted 1 line · <[email protected]> wrote:

Ok. But, actually I dont need native-to-posix mode.

Quoted 7 lines

Good, because it is impossible.

I just need to

Quoted 1 line

share memory betweend kernel thread and user space application.

See the posix stach example.

satch, not statch. It is in examples/posix/satch.c.

--
Gilles

Signature · 1 line
Sent from Gmail for mobile | mobile.google.com
#6 lore raw
Quoted 2 lines · Bosko Radivojevic

Ok, I'll try that. But, is there something wrong with my code/approach
or is it some kind of arm-specific bug?

Yes, as I said in my first answer, you are missing the O_CREAT flag in the call to shm_open.

The arm-specific bug will happen later (once the shared memory is mapped). Because the kernel-space and user-space will get different caches for the same memory area. The solution to this issue is to map the heap without cache, but to get this, you need modifications of Xenomai that only exist on my computer for now. So, if you are interested, I will send a patch.

Signature · 1 line
 Gilles
#7 lore raw

Ok, but Im using native (xeno) skin, both in kernel thread and in user space app. O_CREAT is flag for shm_open & other posix functions, right? And, yes, send me your patch please.

Quoted 18 lines · Gilles Chanteperdrix
Quoted 2 lines · Bosko Radivojevic

Ok, I'll try that. But, is there something wrong with my code/approach
or is it some kind of arm-specific bug?

Yes, as I said in my first answer, you are missing the O_CREAT flag in the call to shm_open.

The arm-specific bug will happen later (once the shared memory is mapped). Because the kernel-space and user-space will get different caches for the same memory area. The solution to this issue is to map the heap without cache, but to get this, you need modifications of Xenomai that only exist on my computer for now. So, if you are interested, I will send a patch.

--
Gilles

Signature · 1 line
Sent from Gmail for mobile | mobile.google.com
#8 lore raw
Quoted 27 lines · Bosko Radivojevic

Ok, but Im using native (xeno) skin, both in kernel thread and in user space app. O_CREAT is flag for shm_open & other posix functions, right? And, yes, send me your patch please.

Quoted 15 lines · Gilles Chanteperdrix
Quoted 2 lines · Bosko Radivojevic

Ok, I'll try that. But, is there something wrong with my code/approach
or is it some kind of arm-specific bug?

Yes, as I said in my first answer, you are missing the O_CREAT flag in the call to shm_open.

The arm-specific bug will happen later (once the shared memory is mapped). Because the kernel-space and user-space will get different caches for the same memory area. The solution to this issue is to map the heap without cache, but to get this, you need modifications of Xenomai that only exist on my computer for now. So, if you are interested, I will send a patch.

Quoted 3 lines

--
Gilles

--
Sent from Gmail for mobile | mobile.google.com

Signature · 1 line
Sent from Gmail for mobile | mobile.google.com
#9 lore raw
In reply to Bosko Radivojevic
Quoted 3 lines · <[email protected]> wrote:

Ok, but Im using native (xeno) skin, both in kernel thread and in user
space app. O_CREAT is flag for shm_open & other posix functions, right? And, yes, send me your patch please.

You have exactly the same problem with the native skin: you do not create the heap.

Signature · 1 line
 Gilles
#10 lore raw
In reply to Gilles Chanteperdrix

Or, better to say, why am I getting -EWOULDBLOCK when trying to bind to existing heap object created in kernel thread wih H_SHARED flag?

Quoted 18 lines · Gilles Chanteperdrix
Quoted 2 lines · Bosko Radivojevic

Ok, I'll try that. But, is there something wrong with my code/approach
or is it some kind of arm-specific bug?

Yes, as I said in my first answer, you are missing the O_CREAT flag in the call to shm_open.

The arm-specific bug will happen later (once the shared memory is mapped). Because the kernel-space and user-space will get different caches for the same memory area. The solution to this issue is to map the heap without cache, but to get this, you need modifications of Xenomai that only exist on my computer for now. So, if you are interested, I will send a patch.

--
Gilles

Signature · 1 line
Sent from Gmail for mobile | mobile.google.com
#11 lore raw
Quoted 2 lines · Bosko Radivojevic

Or, better to say, why am I getting -EWOULDBLOCK when trying to bind
to existing heap object created in kernel thread wih H_SHARED flag?

Because, as documented, the heap object creation in kernel space somehow failed.

Signature · 1 line
 Gilles
#12 lore raw

Yes, you are absolutely right. I moved rt_heap_create call to module initialization. What about patch you've mentioned? :)

Quoted 2 lines · Gilles Chanteperdrix

Because, as documented, the heap object creation in kernel space somehow failed.

#13 lore raw
Quoted 2 lines · Bosko Radivojevic

Yes, you are absolutely right. I moved rt_heap_create call to module
initialization. What about patch you've mentioned? :)

I do not have it at hand. Will send it tonight.

Signature · 1 line
 Gilles
#14 lore raw
Quoted 6 lines · Gilles Chanteperdrix
Quoted 2 lines · Bosko Radivojevic

Yes, you are absolutely right. I moved rt_heap_create call to module
initialization. What about patch you've mentioned? :)

I do not have it at hand. Will send it tonight.

Here comes the patch. It is unfinished, getting posix heaps to automatically use the new flag on ARM requires some more work.

Signature · 1 line
					    Gilles.
native/heap.h +1 -0
Index: include/native/heap.h
===================================================================
@@ -33,6 +33,7 @@
 #define H_MAPPABLE 0x200	/* Memory is mappable to user-space. */
 #define H_SINGLE   0x400	/* Manage as single-block area. */
 #define H_SHARED   (H_MAPPABLE|H_SINGLE) /* I.e. shared memory segment. */
+#define H_NONCACHED 0x800
 
 /** Structure containing heap-information useful to users.
  *
asm-generic/wrappers.h +8 -4
Index: include/asm-generic/wrappers.h
===================================================================
@@ -62,7 +62,7 @@ unsigned long __va_to_kva(unsigned long 
 
 #define wrap_remap_vm_page(vma,from,to) ({ \
     vma->vm_flags |= VM_RESERVED; \
-    remap_page_range(from,virt_to_phys((void *)__va_to_kva(to)),PAGE_SIZE,PAGE_SHARED); \
+    remap_page_range(from,virt_to_phys((void *)__va_to_kva(to)),PAGE_SIZE,vma->vm_page_prot); \
 })
 #define wrap_remap_io_page_range(vma,from,to,size,prot) ({ \
     vma->vm_flags |= VM_RESERVED; \
@@ -223,7 +223,7 @@ unsigned long __va_to_kva(unsigned long 
  * memory. Anyway, this legacy would only hit setups using pre-2.6.11
  * kernel revisions. */
 #define wrap_remap_vm_page(vma,from,to) \
-    remap_pfn_range(vma,from,virt_to_phys((void *)__va_to_kva(to)) >> PAGE_SHIFT,PAGE_SHIFT,PAGE_SHARED)
+    remap_pfn_range(vma,from,virt_to_phys((void *)__va_to_kva(to)) >> PAGE_SHIFT,PAGE_SHIFT,vma->vm_page_prot)
 #define wrap_remap_io_page_range(vma,from,to,size,prot)  ({		\
     (vma)->vm_page_prot = pgprot_noncached((vma)->vm_page_prot);	\
     /* Sets VM_RESERVED | VM_IO | VM_PFNMAP on the vma. */		\
@@ -236,7 +236,7 @@ unsigned long __va_to_kva(unsigned long 
 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10) */
 #define wrap_remap_vm_page(vma,from,to) ({ \
     vma->vm_flags |= VM_RESERVED; \
-    remap_page_range(from,virt_to_phys((void *)__va_to_kva(to)),PAGE_SIZE,PAGE_SHARED); \
+    remap_page_range(from,virt_to_phys((void *)__va_to_kva(to)),PAGE_SIZE,vma->vm_page_prot); \
 })
 #define wrap_remap_io_page_range(vma,from,to,size,prot) ({	\
       vma->vm_flags |= VM_RESERVED;				\
@@ -248,7 +248,11 @@ unsigned long __va_to_kva(unsigned long 
     })
 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) */
 
-#define wrap_switch_mm(prev,next,task)	\
+#ifndef __GFP_BITS_SHIFT
+#define __GFP_BITS_SHIFT 20
+#endif
+
+#define wrap_switch_mm(prev,next,task)		\
     switch_mm(prev,next,task)
 #define wrap_enter_lazy_tlb(mm,task)	\
     enter_lazy_tlb(mm,task)
nucleus/heap.h +2 -0
Index: include/nucleus/heap.h
===================================================================
@@ -57,6 +57,8 @@
 #define XNHEAP_PCONT   1
 #define XNHEAP_PLIST   2
 
+#define XNHEAP_GFP_NONCACHED (1 << __GFP_BITS_SHIFT)
+
 typedef struct xnextent {
 
 	xnholder_t link;
skins/native/heap.c +7 -1
Index: ksrc/skins/native/heap.c
===================================================================
@@ -205,6 +205,10 @@ static void __heap_flush_private(xnheap_
  * operations with I/O devices. The physical address of the
  * heap can be obtained by a call to rt_heap_inquire().
  *
+ * - H_NONCACHED causes the heap not to be cached. This is necessary on
+ * platforms such as ARM to share a heap between kernel and user-space.
+ * Note that this flag is not compatible with the H_DMA flag.
+ *
  * @return 0 is returned upon success. Otherwise:
  *
  * - -EEXIST is returned if the @a name is already in use by some
@@ -260,7 +264,9 @@ int rt_heap_create(RT_HEAP *heap, const 
 
 		err = xnheap_init_mapped(&heap->heap_base,
 					 heapsize,
-					 (mode & H_DMA) ? GFP_DMA : 0);
+					 ((mode & H_DMA) ? GFP_DMA : 0)
+					 | ((mode & H_NONCACHED) ?
+					    XNHEAP_GFP_NONCACHED : 0));
 		if (err)
 			return err;
 
nucleus/heap.c +17 -4
Index: ksrc/nucleus/heap.c
===================================================================
@@ -1097,9 +1097,13 @@ static int xnheap_mmap(struct file *file
 
 	vaddr = (unsigned long)heap->archdep.heapbase;
 
-	if (!heap->archdep.kmflags) {
+	if (!heap->archdep.kmflags
+	    || heap->archdep.kmflags == XNHEAP_GFP_NONCACHED) {
 		unsigned long maddr = vma->vm_start;
 
+		if (heap->archdep.kmflags == XNHEAP_GFP_NONCACHED)
+			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
 		while (size > 0) {
 			if (xnarch_remap_vm_page(vma, maddr, vaddr))
 				return -EAGAIN;
@@ -1174,9 +1178,13 @@ static inline void *__alloc_and_reserve_
 
 	/* Size must be page-aligned. */
 
-	if (!kmflags) {
-		ptr = vmalloc(size);
-
+	if (!kmflags || kmflags == XNHEAP_GFP_NONCACHED) {
+		if (!kmflags)
+			ptr = vmalloc(size);
+		else
+			ptr = __vmalloc(size,
+					GFP_KERNEL | __GFP_HIGHMEM,
+					pgprot_noncached(PAGE_KERNEL));
 		if (!ptr)
 			return NULL;
 
@@ -1241,6 +1249,11 @@ int xnheap_init_mapped(xnheap_t *heap, u
 
 	/* Caller must have accounted for internal overhead. */
 	heapsize = xnheap_align(heapsize, PAGE_SIZE);
+
+	if ((memflags & XNHEAP_GFP_NONCACHED)
+	    && memflags != XNHEAP_GFP_NONCACHED)
+		return -EINVAL;
+
 	heapbase = __alloc_and_reserve_heap(heapsize, memflags);
 
 	if (!heapbase)
#15 lore raw
In reply to Bosko Radivojevic
Quoted 8 lines

--- rt_shm.c - kernel module ---
RT_TASK rt_shm_task;
RT_HEAP heap_desc;
void *shared_mem;
[..]

  if (!rt_heap_create (&heap_desc, "MainSHM", 1024, H_SHARED)) {
    printk ("Error rt_heap_create");

}

This is wrong. rt_heap_create returns 0 when it works and non zero when it fails. So, if you do not see the "Error rt_heap_create" message in the console, rt_heap_create actually fails. rt_heap_create error codes are documented here:

http://www.xenomai.org/documentation/trunk/html/api/group__native__heap.html#g1d19ad24dc9f94b969aa0f574170bdc4

Signature · 1 line
 Gilles