Skip to contentSkip to conversation

rt_heap cleanup issue

Xenomai-help Christoph Permes 3 participants 3 messages Sep 8, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

I have a kernel module that allocates a shared memory and a non-realtime userspace application that reads data from the shared memory.

When loading and unloading kernel module and application multiple times, sometimes the shared memory is not cleaned up. The shared memory remains in /proc/xenomai/registry/native/heaps with size=0. A try to load the module again results in a kernel null pointer exception.

I have written a small example to reproduce the issue:

/* kernel module */
#include <linux/module.h>

#include <native/heap.h>

#define MY_SHARED_MEMORY_SIZE (1024 * 1024)

// shared memory descriptor
static RT_HEAP my_heap_desc;

// global shared memory pointer
void* my_shm_pointer = NULL;

int init_module()

{
    char heap_name[] = "MY_SHARED_MEMORY";
    int result;

    // create the heap descriptor
    result = rt_heap_create( &my_heap_desc, heap_name, MY_SHARED_MEMORY_SIZE, H_SHARED );
    if ( result != 0 ) {
        printk( KERN_ERR "Failed to create shared memory %s: %d\n", heap_name, result );
        goto cleanup_return;
    }

    // allocate memory
    result = rt_heap_alloc( &my_heap_desc, 0, TM_NONBLOCK, &my_shm_pointer );
    if ( ( result != 0 ) || ( my_shm_pointer == NULL ) ) {
        printk( KERN_ERR "KEC::SHM: Failed to allocate base shared memory %s: %d\n", heap_name, result );
        goto cleanup_heap;
    }

    // initialize base memory
    memset( my_shm_pointer, 0, MY_SHARED_MEMORY_SIZE );

    printk( KERN_INFO "Shared memory %s opened (0x%08x).\n", heap_name, (unsigned int)my_shm_pointer );


    return 0;

cleanup_heap:

    my_shm_pointer = NULL;
    rt_heap_delete( &my_heap_desc );

cleanup_return:

    return -1;
}

void cleanup_module()

{
    int result = 0;

    // delete heap descriptor
    my_shm_pointer = NULL;

    result = rt_heap_delete( &my_heap_desc );

    printk( KERN_INFO "Shared memory closed (%d).\n", result );
}

/* userspace application */
#include <signal.h>
#include <stdio.h>

#include <native/heap.h>

int my_stop = 0;

void my_signal_handler(int);

int main()
{
char heap_name[] = "MY_SHARED_MEMORY";
RT_HEAP my_heap;
void* my_shm_pointer = NULL;
int result = 0;

  signal( SIGTERM, my_signal_handler );
  signal( SIGINT, my_signal_handler );

  result = rt_heap_bind( &my_heap, heap_name, TM_NONBLOCK );
  if ( result != 0 ) {
    printf( "rt_heap_bind() returned %d\n", result );
    return -1;
  }

  result = rt_heap_alloc( &my_heap, 0, TM_NONBLOCK, &my_shm_pointer );
  if ( result != 0 ) {
    printf( "rt_heap_alloc() returned %d\n", result );
    rt_heap_unbind( &my_heap );
    return -1;
  }

  while ( !my_stop ) {
    sleep( 1 );
  }

  my_shm_pointer = NULL;
  rt_heap_unbind( &my_heap );

return 0;
}

void my_signal_handler(int sig)

{
    my_stop = 1;
}

I'm using kernel 2.6.29.5, Xenomai 2.4.9 on x86.

The steps to reproduce the issue are:

# insmod my_shm.ko
# ./my_shm &
# rmmod my_shm
# killall my_shm
# insmod my_shm.ko
# ./my_shm &
# rmmod my_shm
# killall my_shm
# cat /proc/xenomai/registry/native/heaps/MY_SHARED_MEMORY
type=kernel:size=0:used:0:numaps=0    <--- strange
# insmod my_shm.ko   <--- null pointer exception, system freeze

There is no null pointer exception when killing the application before rmmod.

Is it generally a problem if kernel modules are unloaded before all applications that use the shared memory are terminated?
With userspace realtime applications cleanup works without any problems regardless of the order the applications are started/terminated. Maybe I am doing something wrong in the example code above?

As my real application consists of several userspace applications (realtime and non-realtime) the startup and shutdown procedure is more complex so that it is not guaranteed that all userspace applications are finished when the kernel module is unloaded.

Thanks,

Christoph

#1 lore raw
Quoted 10 lines · Christoph Permes

Hi,

I have a kernel module that allocates a shared memory and a non-realtime userspace application that reads data from the shared memory.

When loading and unloading kernel module and application multiple times, sometimes the shared memory is not cleaned up. The shared memory remains in /proc/xenomai/registry/native/heaps with size=0. A try to load the module again results in a kernel null pointer exception.

Quoted 5 lines

There is no null pointer exception when killing the application before rmmod.

Is it generally a problem if kernel modules are unloaded before all applications that use the shared memory are terminated?

Actually, you should not be able to do that at all. The bug here is that we should prevent you from unloading the module until all apps have removed their references on the heap.

Quoted 19 lines

With userspace realtime applications cleanup works without any problems regardless of the order the applications are started/terminated. Maybe I am doing something wrong in the example code above?

As my real application consists of several userspace applications (realtime and non-realtime) the startup and shutdown procedure is more complex so that it is not guaranteed that all userspace applications are finished when the kernel module is unloaded.

Thanks,

Christoph

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
#2 lore raw
In reply to Christoph Permes
Quoted 8 lines · Christoph Permes

void cleanup_module()
{
    int result = 0;
    // delete heap descriptor
    my_shm_pointer = NULL;
    result = rt_heap_delete( &my_heap_desc );
    printk( KERN_INFO "Shared memory closed (%d).\n", result ); }

void cleanup_module()

{
     int result = 0;
     while ((result = rt_heap_delete( &my_heap_desc )) == -EBUSY) {
	set_current_state(TASK_INTERRUPTIBLE);
	schedule_timeout(100);
    }
     printk( KERN_INFO "Shared memory closed (%d).\n", result );
}

I'm sure Gilles will correct me if the code snippet is wrong, but this is one method I use to ensure shared resources do not cause a segfault (or worse) when unloading a kernel module.

Regards, Paul.