Subject: Re: Kernel memory allocation
To: Park Chan-youn <phygeeks@gmail.com>
From: Jaromir Dolecek <jdolecek@NetBSD.org>
List: current-users
Date: 02/05/2005 16:04:14
Park Chan-youn wrote:
> I'm writing a USB device driver for a system based on PowerPC/Sandpoint, 
> and implemented read/write by using SIMPLEQ(3) as a fifo queue. Each 
> queue element has a pointer to a buffer, and kernel memory is allocated 
> to the buffer using malloc(9) and free(9). For arguments of the 
> functions, I used the following.
> 
> malloc(size, M_USBDEV, M_WAITOK)
> free(p, M_USBDEV)
> 
> When I input data of size 2MB into the queue, as I checked the memory 
> status with vmstat(8) - with my kernel having KMEMSTATS turned on - free 
> memory indicated by 'fre' decreased as expected amount, around 2MB. 
> Also, when checked with -m option, 'Memory statistics by type' shows 2MB 
> are used for the USB device.
> 
> But when I had flushed the queue, and thereby freed the allocated 
> memory, 'Memory statistics by type' showed that the USB device was not 
> using the allocated memory any longer, but still 'fre' was in the 
> decreased size.

Most likely problem is that you are not actually freeing all
allocated elements in the queue. If you access the queue from
interrupt context, the queue might have been corrupted.

Doing splusb()/splx() around all queue operations might
fix this - worth trying at least.

I'd try to add some intrumentation to detect possible queue item
leak, such as have some counter incremented/decremented when items
are added/removed.

Jaromir
-- 
Jaromir Dolecek <jdolecek@NetBSD.org>            http://www.NetBSD.cz/
-=- We can walk our road together if our goals are all the same;     -=-
-=- We can run alone and free if we pursue a different aim.          -=-