Re: concurrency, locks, reentrant functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



aksingh@hss.hns.com wrote:



Hi,


I am writing a driver.

    I need to make my functions re-entrant, how do I go about writing my
   functions to make them so ?
You have to use synchronization techniques like semaphores or spinlocks.
    I will have  a hash function there, which maintains a list of
   structures, like the list_head structure(a standard eature of linux
   kernel) is there any such standard thing given even for hash tables ?
Yes I believe so but I don't remember. You have to look at it :)
   Also, though my system is not SMP, but still if I want to use spinlocks,
   whats the starting point to read and know about them. A user space
   process writes the value of a variable to the driver, and the driver has
   to check this value before doing anything, so I believe this is right
   place for a spinlock right ?, I dont want one part of the driver to read
   this variable while it is being updated. Or, do I not need a spin lock
   because everything would be sequential ? I am not very clear about this.

You should use semaphores instead of spinlocks. Like this :
down(sem)
CRITICAL SECTION
up(sem)
The critical is the read or write operation.
      Actually my driver would have a char device driver portion and
netfilter hook functions, a user level process would write to the char
device, and the netfilter hook functions wld read this variable to find
whats to be done when they catch packets. So in this scenario would spin
locks be needed ?

You have effectively to protect the reading or the writing of the variable. In this case, I thinl you can't use semaphores. You will have to use spinlocks. You cant't use semaphores in tasklet or interrupt handler. And your hook function will be called inside a tasklet or interrupt handler, right ?
So you can use spinlocks.


The main rules for spinlocks and semaphores are :
_ Don't hold a spinlock when a function which can sleep is called
_ Don't use semaphorse in a non process context (tasklet,bh,interrupt handler)
_ The spinlocks shouldn't be use if the critiical section is too big


a very good link for locking functions :
http://csworks.com:10080/linux/kernel-locking/book1.html


-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux