srikar sana
srikar sana

Reputation: 115

Use of spinlock_check() function

I have a question about the function spinlock_check() used in spin_lock_init() macro.

The code of spinlock_check is written below and it returns address of rlock

static __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock)
{
    return &lock->rlock;
}

It is used in the macro spin_lock_init. The code of this macro:

#define spin_lock_init(_lock)               \
do {                            \
   spinlock_check(_lock);               \
   raw_spin_lock_init(&(_lock)->rlock);     \
} while (0)

I saw a question about this topic in here. But i did not quite understand and I want to express doubts in my way.

The spin_lock_init() is a macro but spinlock_check() isnt a macro. Its an inline function. So I think there is no way for some compilation magic to happen here but I expect some magic during execution of those instructions. What effect does spinlock_check() has? Because nothing is using the return value of spinlock_check() function. Even though spinlock_check() return something the next step is going to get executed anyways. Because I saw its usage in one of the file and I thought that its different from min(x, y) macro.

Here is the usage which I found

#ifdef CONFIG_NUMA
static void do_numa_crng_init(struct work_struct *work)
{
    int i;
    struct crng_state *crng;
    struct crng_state **pool;

    pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL);
    for_each_online_node(i) {
        crng = kmalloc_node(sizeof(struct crng_state),
                    GFP_KERNEL | __GFP_NOFAIL, i);
        spin_lock_init(&crng->lock);
        crng_initialize(crng);
        pool[i] = crng;
    }
    mb();
    if (cmpxchg(&crng_node_pool, NULL, pool)) {
        for_each_node(i)
        kfree(pool[i]);
        kfree(pool);
    }
}

So here crng is dynamically allocated one. say I have missed the kmalloc code meaning I haven't allocated memory for crng but still I used the macro spin_lock_init(crng). Now what good the spinlock_check() function does ? Isn't it that after spinlock_check() function raw_spin_lock_init automatically executes ?

If it is going to execute then whats the use of spinlock_check() function? There should be some meaning but I can't figure it out.

Upvotes: 2

Views: 360

Answers (1)

xhienne
xhienne

Reputation: 6134

What you are missing is that spinlock_check() does not perform any check at run time. That's why its returned value is ignored. This instruction is expected to be removed during compilation by the optimizer.

Is it of any use, then? Yes! It's purpose is to ensure at compile time that the type of its parameter is spinlock_t *. If you don't give a pointer to spinlock_t as a parameter to spin_lock_init(), you will trigger a compilation error.

Upvotes: 3

Related Questions