Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e8aed686 authored by Lai Jiangshan's avatar Lai Jiangshan Committed by Ingo Molnar
Browse files

doc/RCU: fix pseudocode in rcuref.txt



atomic_inc_not_zero(v) return 0 if *v = 0.
use spin_lock instead of write_lock for update lock.

Signed-off-by: default avatarLai Jiangshan <laijs@cn.fujitsu.com>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 429b022a
Loading
Loading
Loading
Loading
+8 −8
Original line number Original line Diff line number Diff line
@@ -29,9 +29,9 @@ release_referenced() delete()
					}
					}


If this list/array is made lock free using RCU as in changing the
If this list/array is made lock free using RCU as in changing the
write_lock() in add() and delete() to spin_lock and changing read_lock
write_lock() in add() and delete() to spin_lock() and changing read_lock()
in search_and_reference to rcu_read_lock(), the atomic_get in
in search_and_reference() to rcu_read_lock(), the atomic_inc() in
search_and_reference could potentially hold reference to an element which
search_and_reference() could potentially hold reference to an element which
has already been deleted from the list/array.  Use atomic_inc_not_zero()
has already been deleted from the list/array.  Use atomic_inc_not_zero()
in this scenario as follows:
in this scenario as follows:


@@ -40,20 +40,20 @@ add() search_and_reference()
{					{
{					{
    alloc_object			    rcu_read_lock();
    alloc_object			    rcu_read_lock();
    ...					    search_for_element
    ...					    search_for_element
    atomic_set(&el->rc, 1);		    if (atomic_inc_not_zero(&el->rc)) {
    atomic_set(&el->rc, 1);		    if (!atomic_inc_not_zero(&el->rc)) {
    write_lock(&list_lock);		        rcu_read_unlock();
    spin_lock(&list_lock);		        rcu_read_unlock();
					        return FAIL;
					        return FAIL;
    add_element				    }
    add_element				    }
    ...					    ...
    ...					    ...
    write_unlock(&list_lock);		    rcu_read_unlock();
    spin_unlock(&list_lock);		    rcu_read_unlock();
}					}
}					}
3.					4.
3.					4.
release_referenced()			delete()
release_referenced()			delete()
{					{
{					{
    ...					    write_lock(&list_lock);
    ...					    spin_lock(&list_lock);
    if (atomic_dec_and_test(&el->rc))       ...
    if (atomic_dec_and_test(&el->rc))       ...
        call_rcu(&el->head, el_free);       delete_element
        call_rcu(&el->head, el_free);       delete_element
    ...                                     write_unlock(&list_lock);
    ...                                     spin_unlock(&list_lock);
} 					    ...
} 					    ...
					    if (atomic_dec_and_test(&el->rc))
					    if (atomic_dec_and_test(&el->rc))
					        call_rcu(&el->head, el_free);
					        call_rcu(&el->head, el_free);