Skip to content

Commit c54a274

Browse files
edumazetpaulmckrcu
authored andcommitted
list: Add hlist_unhashed_lockless()
We would like to use hlist_unhashed() from timer_pending(), which runs without protection of a lock. Note that other callers might also want to use this variant. Instead of forcing a READ_ONCE() for all hlist_unhashed() callers, add a new helper with an explicit _lockless suffix in the name to better document what is going on. Also add various WRITE_ONCE() in __hlist_del(), hlist_add_head() and hlist_add_before()/hlist_add_behind() to pair with the READ_ONCE(). Signed-off-by: Eric Dumazet <[email protected]> Cc: Thomas Gleixner <[email protected]> [ paulmck: Also add WRITE_ONCE() to rculist.h. ] Signed-off-by: Paul E. McKenney <[email protected]>
1 parent f452ee0 commit c54a274

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

include/linux/list.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,16 @@ static inline int hlist_unhashed(const struct hlist_node *h)
749749
return !h->pprev;
750750
}
751751

752+
/* This variant of hlist_unhashed() must be used in lockless contexts
753+
* to avoid potential load-tearing.
754+
* The READ_ONCE() is paired with the various WRITE_ONCE() in hlist
755+
* helpers that are defined below.
756+
*/
757+
static inline int hlist_unhashed_lockless(const struct hlist_node *h)
758+
{
759+
return !READ_ONCE(h->pprev);
760+
}
761+
752762
static inline int hlist_empty(const struct hlist_head *h)
753763
{
754764
return !READ_ONCE(h->first);
@@ -761,7 +771,7 @@ static inline void __hlist_del(struct hlist_node *n)
761771

762772
WRITE_ONCE(*pprev, next);
763773
if (next)
764-
next->pprev = pprev;
774+
WRITE_ONCE(next->pprev, pprev);
765775
}
766776

767777
static inline void hlist_del(struct hlist_node *n)
@@ -782,32 +792,32 @@ static inline void hlist_del_init(struct hlist_node *n)
782792
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
783793
{
784794
struct hlist_node *first = h->first;
785-
n->next = first;
795+
WRITE_ONCE(n->next, first);
786796
if (first)
787-
first->pprev = &n->next;
797+
WRITE_ONCE(first->pprev, &n->next);
788798
WRITE_ONCE(h->first, n);
789-
n->pprev = &h->first;
799+
WRITE_ONCE(n->pprev, &h->first);
790800
}
791801

792802
/* next must be != NULL */
793803
static inline void hlist_add_before(struct hlist_node *n,
794804
struct hlist_node *next)
795805
{
796-
n->pprev = next->pprev;
797-
n->next = next;
798-
next->pprev = &n->next;
806+
WRITE_ONCE(n->pprev, next->pprev);
807+
WRITE_ONCE(n->next, next);
808+
WRITE_ONCE(next->pprev, &n->next);
799809
WRITE_ONCE(*(n->pprev), n);
800810
}
801811

802812
static inline void hlist_add_behind(struct hlist_node *n,
803813
struct hlist_node *prev)
804814
{
805-
n->next = prev->next;
806-
prev->next = n;
807-
n->pprev = &prev->next;
815+
WRITE_ONCE(n->next, prev->next);
816+
WRITE_ONCE(prev->next, n);
817+
WRITE_ONCE(n->pprev, &prev->next);
808818

809819
if (n->next)
810-
n->next->pprev = &n->next;
820+
WRITE_ONCE(n->next->pprev, &n->next);
811821
}
812822

813823
/* after that we'll appear to be on some hlist and hlist_del will work */

include/linux/rculist.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static inline void hlist_del_init_rcu(struct hlist_node *n)
173173
{
174174
if (!hlist_unhashed(n)) {
175175
__hlist_del(n);
176-
n->pprev = NULL;
176+
WRITE_ONCE(n->pprev, NULL);
177177
}
178178
}
179179

@@ -473,7 +473,7 @@ static inline void list_splice_tail_init_rcu(struct list_head *list,
473473
static inline void hlist_del_rcu(struct hlist_node *n)
474474
{
475475
__hlist_del(n);
476-
n->pprev = LIST_POISON2;
476+
WRITE_ONCE(n->pprev, LIST_POISON2);
477477
}
478478

479479
/**
@@ -489,11 +489,11 @@ static inline void hlist_replace_rcu(struct hlist_node *old,
489489
struct hlist_node *next = old->next;
490490

491491
new->next = next;
492-
new->pprev = old->pprev;
492+
WRITE_ONCE(new->pprev, old->pprev);
493493
rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
494494
if (next)
495-
new->next->pprev = &new->next;
496-
old->pprev = LIST_POISON2;
495+
WRITE_ONCE(new->next->pprev, &new->next);
496+
WRITE_ONCE(old->pprev, LIST_POISON2);
497497
}
498498

499499
/*
@@ -528,10 +528,10 @@ static inline void hlist_add_head_rcu(struct hlist_node *n,
528528
struct hlist_node *first = h->first;
529529

530530
n->next = first;
531-
n->pprev = &h->first;
531+
WRITE_ONCE(n->pprev, &h->first);
532532
rcu_assign_pointer(hlist_first_rcu(h), n);
533533
if (first)
534-
first->pprev = &n->next;
534+
WRITE_ONCE(first->pprev, &n->next);
535535
}
536536

537537
/**
@@ -564,7 +564,7 @@ static inline void hlist_add_tail_rcu(struct hlist_node *n,
564564

565565
if (last) {
566566
n->next = last->next;
567-
n->pprev = &last->next;
567+
WRITE_ONCE(n->pprev, &last->next);
568568
rcu_assign_pointer(hlist_next_rcu(last), n);
569569
} else {
570570
hlist_add_head_rcu(n, h);
@@ -592,10 +592,10 @@ static inline void hlist_add_tail_rcu(struct hlist_node *n,
592592
static inline void hlist_add_before_rcu(struct hlist_node *n,
593593
struct hlist_node *next)
594594
{
595-
n->pprev = next->pprev;
595+
WRITE_ONCE(n->pprev, next->pprev);
596596
n->next = next;
597597
rcu_assign_pointer(hlist_pprev_rcu(n), n);
598-
next->pprev = &n->next;
598+
WRITE_ONCE(next->pprev, &n->next);
599599
}
600600

601601
/**
@@ -620,10 +620,10 @@ static inline void hlist_add_behind_rcu(struct hlist_node *n,
620620
struct hlist_node *prev)
621621
{
622622
n->next = prev->next;
623-
n->pprev = &prev->next;
623+
WRITE_ONCE(n->pprev, &prev->next);
624624
rcu_assign_pointer(hlist_next_rcu(prev), n);
625625
if (n->next)
626-
n->next->pprev = &n->next;
626+
WRITE_ONCE(n->next->pprev, &n->next);
627627
}
628628

629629
#define __hlist_for_each_rcu(pos, head) \

0 commit comments

Comments
 (0)