Thread: Kernel: Legal to kfree() a down()'ed semaphore

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Kernel: Legal to kfree() a down()'ed semaphore

    I have something like:
    Code:
    struct buffer {
            uint32_t head, tail;
            void **buf;
            struct semaphore sem;
    }
    that I'm using to control several input buffers in my device driver. As a particular part of the driver is released, I need to dispose of that buffer. What I want to do is something along these lines:
    Code:
            struct buffer *buf;
            down(buf->sem);
            kfree(buf->buf);
            kfree(buf);
            // Uh oh, where'd the semaphore go?
    I'm almost certain that this would be valid since the semaphore is controlled by me and ONLY by me. I believe that the kernel has no interest in the semaphore at any point, so I'm the one completely responsible for it.

    Am I right in these assumptions?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Further research leads me to believe that my assumptions are incorrect. It appears that I'll have to lock the list when I need to free the buffer. Otherwise, there could be a memory leak as there is a linked list on with int the struct sem that keeps track of who is in line for the semaphore next. To that end, if I hit a race condition where I want to free it, but I then tap the buf->sem and want to add data to it, I'll forever wait -- which would be "bad".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SOS - Can a monolithic kernel be used in a micro-kernel style OS?
    By sean in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-20-2008, 09:30 AM
  2. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  3. Semaphore Question
    By azamsharp1 in forum C Programming
    Replies: 4
    Last Post: 10-30-2005, 09:01 AM
  4. CreateSemaphore/ReleaseSemaphore
    By nrieger in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2005, 06:57 AM