Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp1422704ybx; Thu, 7 Nov 2019 11:39:23 -0800 (PST) X-Google-Smtp-Source: APXvYqwOlbOZEQmnAVt70Rk5pzaQJJA+6VPkUrg+neZMkIQRDlZxMaLqTy5LNnDWVGeUVLcrb/0S X-Received: by 2002:a17:906:3582:: with SMTP id o2mr4798331ejb.54.1573155563552; Thu, 07 Nov 2019 11:39:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573155563; cv=none; d=google.com; s=arc-20160816; b=J1XM+c4eduEv5XhCE69CvvS14ChZJpRTizPJaMT45pqk0nPNtxecP9yit2G9olS78C Vujc6h6K+XZ4ta09GNgewsn2skiW8yBQwXbEIJHe0Xjac/umNd4WMEhxIArOSUj5YELv Veo4CIr/ub6IOd/sOl0xL6reL0DCMPKMqNsG3Z372V72tPu23kLjs+14xKGwPdkexzsF XqnHMqcR++oVVnH4aj9UCzeOAVt295sMHaOBn6ZAYPEFT6EIjIngo0ncBZRw9CO2Rkao pA134pRJdqmFFkKNdh9Iymr/takxC3EdT/rly2nLLju6POc3A4Q/H6NDqIh31pujCf1U 3Aww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=LJ5qu1d1qYTmvRrw2/bIxoaJkdqKaiuo2RqaAL/gLjQ=; b=GHCXfG/6wRXaVJZsrF1wljne8uj8S7VH0Ss5JK+sHy+86MkoBFzQAQN9ToekMH8yvD OzXEkiCK3HcaYn4tb6BbH1EH2aMPdXTlVGwOvTyVZEXuOIhVspmx+SqV2NE4VTRqC44T SmuNLH0LKFump4m7ZZJvUQotaaO7d+MrmHhOxTvvQNueZLlPTWoGQsGSpItLb8jIxWeX /sLl8B4DpmWJbeibVuU7uAPdKhBOp2lSqE7ZgVi2eBmRHGoWu3gFO3OaQfta5Sba0l1A RehwwB8DSgeFXPqDime0Uhs/xkCLklKqwMsbo34cABU8XvsoJXJiKZAwEAuByUA+lADP Fanw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nfaWlfTV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h43si2076958edb.89.2019.11.07.11.38.59; Thu, 07 Nov 2019 11:39:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nfaWlfTV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727076AbfKGTho (ORCPT + 99 others); Thu, 7 Nov 2019 14:37:44 -0500 Received: from mail-pg1-f201.google.com ([209.85.215.201]:33706 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726829AbfKGTho (ORCPT ); Thu, 7 Nov 2019 14:37:44 -0500 Received: by mail-pg1-f201.google.com with SMTP id y22so2662222pgc.0 for ; Thu, 07 Nov 2019 11:37:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=LJ5qu1d1qYTmvRrw2/bIxoaJkdqKaiuo2RqaAL/gLjQ=; b=nfaWlfTVssvz5xKSSFcy1G4yFKmpFodEY52ELUWfRbdELBywKboP3WuzeqObOCPVJ+ oibB5HCXQjUs6GGW1eBBLTPppMXXsLZPaPRTtLwL6w7ctbPO2FlLsZmKVJsaAGXkNHvy tv7Bdb+8EXr9w8CPJg41av4kkucclnrfkQ9Ucf95GgLoPFGlr3xtQE0x/DP78CO70VD+ AfwMmNStevSZSvISY/QBZ2PKZ3M39VnXDG7Js8X5b25apb4AIR1MNVWs9abMFAVuVadg zblVAYfF8BZenWVPnU3SRdE/5iK2Son+iGZJlVhgEVsbedn3OdLVJGUcrnWibxKg+1SN YpFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=LJ5qu1d1qYTmvRrw2/bIxoaJkdqKaiuo2RqaAL/gLjQ=; b=kE/YUNyaRMHukotUbbJ/yVAIsmYzMnGoBBJl0jr9AlH9HstEV9x6RtNreL5OHoGjSw 9LC8vfsYBpQ8TclahthEA9TW5FTAf0F7ZLvj8uK4QQ+VAcOJTaCbR4ZjWDPjTAFq4QfP /JWIvCFeDWsfLv9fXP3PgptSjlYJLxXg3jQZ+BOlTd9f9TvfvIEdvRZycUhKU5Mc6Pux FfKRiM/1/6ricHNODpK2RZp0CjVyw+bPxwGne8zORVDdMcNcDGQ3OMdNP4BALy7RKIyS drS2W/C+4YzBjNvrwvNZW/D2rxsX/31+yEWJ+OedZGux9JJ9Hby25bFZHB4eeqXonuUJ xIhw== X-Gm-Message-State: APjAAAXN/bKn+eIcHSYjHEXpkGdBLTTyViTElDBL2zc7NilLdqPnYrOO cTr/sF73Fna1MqGMcsmpiRhKVGKIHYxEaQ== X-Received: by 2002:a63:a5b:: with SMTP id z27mr6849735pgk.416.1573155461548; Thu, 07 Nov 2019 11:37:41 -0800 (PST) Date: Thu, 7 Nov 2019 11:37:37 -0800 Message-Id: <20191107193738.195914-1-edumazet@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.24.0.432.g9d3f5f5b63-goog Subject: [PATCH 1/2] list: add hlist_unhashed_lockless() From: Eric Dumazet To: Thomas Gleixner Cc: linux-kernel , Eric Dumazet , Eric Dumazet , "Paul E. McKenney" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Cc: "Paul E. McKenney" Cc: Thomas Gleixner --- include/linux/list.h | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/include/linux/list.h b/include/linux/list.h index 85c92555e31f85f019354e54d6efb8e79c2aee17..61f5aaf96192cdc4c1644741a415590b63c3c201 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -749,6 +749,16 @@ static inline int hlist_unhashed(const struct hlist_node *h) return !h->pprev; } +/* This variant of hlist_unhashed() must be used in lockless contexts + * to avoid potential load-tearing. + * The READ_ONCE() is paired with the various WRITE_ONCE() in hlist + * helpers that are defined below. + */ +static inline int hlist_unhashed_lockless(const struct hlist_node *h) +{ + return !READ_ONCE(h->pprev); +} + static inline int hlist_empty(const struct hlist_head *h) { return !READ_ONCE(h->first); @@ -761,7 +771,7 @@ static inline void __hlist_del(struct hlist_node *n) WRITE_ONCE(*pprev, next); if (next) - next->pprev = pprev; + WRITE_ONCE(next->pprev, pprev); } static inline void hlist_del(struct hlist_node *n) @@ -782,32 +792,32 @@ static inline void hlist_del_init(struct hlist_node *n) static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) { struct hlist_node *first = h->first; - n->next = first; + WRITE_ONCE(n->next, first); if (first) - first->pprev = &n->next; + WRITE_ONCE(first->pprev, &n->next); WRITE_ONCE(h->first, n); - n->pprev = &h->first; + WRITE_ONCE(n->pprev, &h->first); } /* next must be != NULL */ static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next) { - n->pprev = next->pprev; - n->next = next; - next->pprev = &n->next; + WRITE_ONCE(n->pprev, next->pprev); + WRITE_ONCE(n->next, next); + WRITE_ONCE(next->pprev, &n->next); WRITE_ONCE(*(n->pprev), n); } static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev) { - n->next = prev->next; - prev->next = n; - n->pprev = &prev->next; + WRITE_ONCE(n->next, prev->next); + WRITE_ONCE(prev->next, n); + WRITE_ONCE(n->pprev, &prev->next); if (n->next) - n->next->pprev = &n->next; + WRITE_ONCE(n->next->pprev, &n->next); } /* after that we'll appear to be on some hlist and hlist_del will work */ -- 2.24.0.432.g9d3f5f5b63-goog