Skip to contentSkip to conversation

sparse: possible false report of context imbalance

DISCUSSION Larry Finger 2 participants 3 messages Oct 17, 2013 Sparse checker

lore
Share & tools
lore
#0 lore raw

Hi,

Sparse reports the following:

CHECK drivers/staging/rtl8188eu/core/rtw_mlme.c
drivers/staging/rtl8188eu/core/rtw_mlme.c:1003:9: warning: context imbalance in 'rtw_free_assoc_resources' - different lock contexts for basic block

The code in question is as follows:

        if (lock_scanned_queue)
                spin_lock_bh(&(pmlmepriv->scanned_queue.lock));

        pwlan = rtw_find_network(&pmlmepriv->scanned_queue, 

tgt_network->network.MacAddress);

        if (lock_scanned_queue)
                spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));

As this fragment uses the identical test to unlock that is used to lock, and the test variable is not touched, I think this is a false indication. I am using version 0.4.4 of sparse.

Thanks,

Larry

#1 lore raw
Quoted 13 lines · Larry Finger

Sparse reports the following:

CHECK drivers/staging/rtl8188eu/core/rtw_mlme.c
drivers/staging/rtl8188eu/core/rtw_mlme.c:1003:9: warning: context imbalance in 'rtw_free_assoc_resources' - different lock contexts for basic block

The code in question is as follows:

        if (lock_scanned_queue)
                spin_lock_bh(&(pmlmepriv->scanned_queue.lock));

        pwlan = rtw_find_network(&pmlmepriv->scanned_queue,
Quoted 8 lines

tgt_network->network.MacAddress);

        if (lock_scanned_queue)
                spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));

As this fragment uses the identical test to unlock that is used to lock, and the test variable is not touched, I think this is a false indication. I am using version 0.4.4 of sparse.

Sparse can't track conditional contexts like this; sparse intentionally complains here that you're running the same basic block (the rtw_find_network call) with and without a lock held.

The following workaround works when this is legitimate, though it isn't ideal:

if (condition) {
lock
do_thing
unlock
} else {
do_thing
}

Ideally, Sparse should be able to track conditional contexts, but that would require some form of abstract evaluation, or as a simplistic hack, looking for identical side-effect-free conditional expressions.

- Josh Triplett

#2 lore raw
Quoted 38 lines · Josh Triplett
Quoted 13 lines · Larry Finger

Sparse reports the following:

CHECK drivers/staging/rtl8188eu/core/rtw_mlme.c
drivers/staging/rtl8188eu/core/rtw_mlme.c:1003:9: warning: context imbalance in 'rtw_free_assoc_resources' - different lock contexts for basic block

The code in question is as follows:

         if (lock_scanned_queue)
                 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));

         pwlan = rtw_find_network(&pmlmepriv->scanned_queue,
Quoted 8 lines

tgt_network->network.MacAddress);

         if (lock_scanned_queue)
                 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));

As this fragment uses the identical test to unlock that is used to lock, and the test variable is not touched, I think this is a false indication. I am using version 0.4.4 of sparse.

Sparse can't track conditional contexts like this; sparse intentionally complains here that you're running the same basic block (the rtw_find_network call) with and without a lock held.

The following workaround works when this is legitimate, though it isn't ideal:

if (condition) {
lock
do_thing
unlock
} else {
do_thing
}

Quoted 3 lines

Ideally, Sparse should be able to track conditional contexts, but that would require some form of abstract evaluation, or as a simplistic hack, looking for identical side-effect-free conditional expressions.

Thanks for the answer, which is about what I expected. Sparse is a great tool, particularly for endian-related issues.

Larry