device driver 2.4.20 vs. 2.6.9 virtual memory

Greg Frinchaboy greg at visionpro.com
Fri Feb 18 15:35:48 UTC 2005


We share some memory between user space and the driver by allocating it 
in the driver and mmapping in the user program.  The following code has 
been working for couple of years using RH7.3 with the 2.4.20 kernel:

In the kernel driver:

typedef struct shared {
	unsigned long	event_addr;
	time_t		ntimes_stamp;
} OurShared;

dev_sp->shared_host_mem is a pointer to an OurShared;

static int
alloc_usr_shared(OurSoftDev *our_dev_sp)
{
	unsigned long virt_addr;
	dev_sp->shared_host_mem = (OurShared *)kmalloc(SOME_SIZE, GFP_KERNEL); 
  /* SOME_SIZE is usually PAGE_SIZE */
	if (dev_sp->shared_host_mem == NULL)
		return error, etc.

	for (virt_addr = (unsigned long) dev_sp->shared_host_mem;
		virt_addr < (unsigned long) dev_sp->shared_host_mem + SOME_SIZE;
		virt_addr += PAGE_SIZE)
	{
		/* reserve all pages to make them remapable */
		mem_map_reserve(virt_to_page(virt_addr));
	}
	return 0;
}

mem_map_reserve is a macro in wrapper.h to set the Pg_reserved bit.

In the user space:

void *addr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 
offset);
_kernel._our_dev_common = reinterpret_cast<OurShared *>(addr);

gives us access to this "common or shared memory".  One of purposes of 
this shared memory is to keep track of how many times an event 
occurred.  The "event" gets set to 0 in usr space and counted up every 
interrupt in kernel space.  The user program queries the event count 
from time to time.

Using Fedora
In 2.6, wrapper.h is gone.  However, SetPageReserved does the same 
thing a mem_map_reserve, so I used the SetPageReserved macro.

When I try to run the system in 2.6, the system crashes hard - 
sometimes I get an oops, Unable to handle Kernel NULL pointer 
dereference virtual address 00000004.  Other times the system locks up 
before it can log a message.

I placed a printk in the interrupt handler and saw the shared memory 
location which should be 0 to start is some random number.  This leads 
me to believe the mechanism using  kmalloc and trying to lock it down 
using mmap no longer works in the 2.6 kernel.

Does anyone know how to lock some "virtual" shared memory between user 
space and kernel space in the 2.6 kernel?




More information about the redhat-list mailing list