Correctly locking a Block Device Request Handler

Marcel Müller neikos at
Mon Nov 16 11:32:49 EST 2015

Hello everyone,

I'm currently writing a block device driver and got stuck at trying to
understand how to correctly handle the locking
in the reqfn one passes to `blk_init_queue`.

My code looks like this:

    static DEFINE_SPINLOCK(rblk_lock);

    /* Code */

    static void rblk_request_handler(struct request_queue *q)
    __releases(q->queue_lock) __acquires(q->queue_lock)
        struct request* req;
        unsigned long flags = 0;
        printk(KERN_INFO "rblk: Got request(s) \n");
        while ((req = blk_fetch_request(q)) != NULL) {
            printk(KERN_INFO "rblk: Handling request \n"); // <- Gets
            spin_lock_irqsave(q->queue_lock, flags);
            blk_end_request_all(req, -ENOTTY);
            spin_unlock_irqrestore(q->queue_lock, flags);
            printk(KERN_INFO "rblk: Handled request \n"); // <- Does not
get printed

    static in rblk_init() {
        /* Get major number, allocate devices */
        for (i = 0; i < rblk_cnt; i++) { // For each device
            /* alloc_disk, check for allocation fail */

            disk->queue = blk_init_queue(rblk_request_handler, &rblk_lock);

This didn't work, and it was obvious to me that it was hanging in the
spinlock. So I tried
removing the locking, (this SO answer says that the queue is already
however, I still get the problem that the queue locks up and the second
message never hits the message

What /does/ work is if I invert the order of locking. As in, unlock
first, end_request and then lock again.
However that doesn't seem to be the correct way. What am I doing
completely wrong, what did I misunderstand?

Full code:


More information about the Kernelnewbies mailing list