Find the answer to your Linux question:
Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By docbop
  • 1 Post By gregm
Hello, Can anyone tell me what is the purpose of using the function lock_sock(struct sock *sk) in the kernel space? And how does it effect the corresponding socket in the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2014
    Posts
    15

    Question Linux Kernel lock_sock Function


    Hello,

    Can anyone tell me what is the purpose of using the function lock_sock(struct sock *sk) in the kernel space? And how does it effect the corresponding socket in the user space, and will the OS be able to send packets after locking the sock?

    Thanks a lot.

  2. #2
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    902
    Here is a short description and the code, basicly puts a lock on the socket to protect the state from being changed by another connection.

    http://people.cs.clemson.edu/~westal...es/tcprecv.pdf
    ha-as likes this.
    A lion does not lose sleep, over the opinion of sheep.

  3. #3
    Just Joined!
    Join Date
    May 2014
    Posts
    15
    Thanks a lot for your reply. So my question if I want to copy the state of the sock structure, assuming that the socket is in listening mode and doing sending and receving of TCP segments.

    Is it enough just to lock it only? Or I should drop the incoming and outgoing packets using NetFilter and then lock the socket?

  4. #4
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    902
    Quote Originally Posted by ha-as View Post
    Thanks a lot for your reply. So my question if I want to copy the state of the sock structure, assuming that the socket is in listening mode and doing sending and receving of TCP segments.

    Is it enough just to lock it only? Or I should drop the incoming and outgoing packets using NetFilter and then lock the socket?
    Beem awhile since I've done socket programming so I'll let gregm or one of the others more current answer this.
    Last edited by docbop; 06-02-2014 at 02:48 PM.
    A lion does not lose sleep, over the opinion of sheep.

  5. #5
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,048
    The network stack is multithreaded so in order to serialize it and keep the queues FIFO (first in first out) locks are used. While you hold a lock processing can continue else where but the flow toward user space is halted so that the packet you are working on maintains it place in the queue. Lock_sock is a spin lock so when you call the function it locks the bottom processes and, if there is an owner, spins until the owner comes free at which time it becomes the owner. You do your thing and release the lock so processing can continue.

    The user process waiting on those packets can read the packets upstream but until a lock is released other packets are being backlogged so a lock should be released as quickly as possible.

    If all you are doing is copying you can call sock_hold instead of lock_sock. You don't need a lock -you just want to be sure that the memory doesn't get freed while you're using it.
    ha-as likes this.

  6. #6
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    150
    If the socket is in a LISTEN state then it's not doing any I/O, since it's waiting for an incoming connection by definition.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •