Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4975724ybb; Tue, 24 Mar 2020 08:40:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvXYr1hUS+pEptZ4LJuGiAEZ++tPbBXE+w8DnAr4JboQNrvo+uZl/1lmZQr4m9B/i5pJ4I8 X-Received: by 2002:a05:6830:1e93:: with SMTP id n19mr2257340otr.153.1585064451211; Tue, 24 Mar 2020 08:40:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585064451; cv=none; d=google.com; s=arc-20160816; b=NJ5sFB35NlKm+u7xJ2DrIhLCqfiGJF480mR17yhr+FYshDph3RHRlvstM+ySameQ19 VpPOe+vNyShtmy/W2MjasoaekNxCD7teo4yAgIvKz8HgvZ0KMffxsiiuo39dXi7CHCxO JGLuHhBiVFY2OWH18Yp56inDyb6IxwH4XJ66RezTj+JGcRnx11IYd7g4AqAtJ6a5QxRd /t3TvRH4zsJh90XgUPyHqk14ZkuPxszT4M2Xde2nXTO75sHW3tiyJ68x1IY14f3HGOr5 ApmaQZdWVP0IzAcZfNGwxpJEovwdiCZgesMtJ2U/zZKIrQsm4yzzHP2OH6O+S1R/klx2 2nSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7/q8BtxzYI/6kM62Y19KQvvT473UiIcmsvlwff7TWso=; b=hhw/QARmhvBqPiL0O1cw3IsoNqwU4o2CPWB0BrT6G7l2qgxmvhFl0bqJOwH7LjaRVI kAtT+dxenPsd6BuZvIvu8c66nDy7YjsiIOOOorKVK2iZHvTBLYgVmf4+H5M5mqbnMek0 pZxOcA7tWIIUK+Bg1dhZCO2TfSJH5WhVkRlgcBxYxrHEerQ5j4p3HrmvGsAboN+Muws6 bStHw7+9HRje7oWWT9aj8PKyAiqbB3dzTxNE7rdDXpoaudkDT47qTKhAwp48TpqiSGlK kvU24anNy2DsDlXzNcvOlh1J1Zi5EwkJyVKnuD7NYdJ7Equs468Ig4HXvuzHywqX5CXA XEyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ySrv9lDo; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v17si3226368otq.250.2020.03.24.08.40.37; Tue, 24 Mar 2020 08:40:51 -0700 (PDT) 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=@kernel.org header.s=default header.b=ySrv9lDo; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728316AbgCXPii (ORCPT + 99 others); Tue, 24 Mar 2020 11:38:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:60356 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728618AbgCXPh2 (ORCPT ); Tue, 24 Mar 2020 11:37:28 -0400 Received: from localhost.localdomain (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E56B6208CA; Tue, 24 Mar 2020 15:37:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1585064247; bh=ePC0KcHwaeG27HJefCMue14nRgQlh3QPrh4rpDBAm+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ySrv9lDoir/ctvuoDCC59S/NRJYM3saGztZut9HosJtQrud9DmO2rEQYmy4m2Y3Uf 8/4lOkxE55lxjrkjgXa/MPyc+LGVBrLqSSxuS/aQ1xgM1o6Jpqe5meS7XeiNPV9her ptPCVWPMtE3vx0DYOli+qNSrP71EXJv5Re6PsHZA= From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Eric Dumazet , Jann Horn , Kees Cook , Maddie Stone , Marco Elver , "Paul E . McKenney" , Peter Zijlstra , Thomas Gleixner , kernel-team@android.com, kernel-hardening@lists.openwall.com Subject: [RFC PATCH 15/21] list_bl: Use CHECK_DATA_CORRUPTION instead of custom BUG_ON() wrapper Date: Tue, 24 Mar 2020 15:36:37 +0000 Message-Id: <20200324153643.15527-16-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200324153643.15527-1-will@kernel.org> References: <20200324153643.15527-1-will@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org CHECK_DATA_CORRUPTION() allows detected data corruption to result consistently in either a BUG() or a WARN() depending on CONFIG_BUG_ON_DATA_CORRUPTION. Use CHECK_DATA_CORRUPTION() to report list_bl integrity checking failures, rather than a custom wrapper around BUG_ON(). Cc: Kees Cook Cc: Paul E. McKenney Cc: Peter Zijlstra Signed-off-by: Will Deacon --- include/linux/list_bl.h | 55 +++++++++++++++++++++++++------------- include/linux/rculist_bl.h | 17 ++++-------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h index 9f8e29142324..f48d8acb15b4 100644 --- a/include/linux/list_bl.h +++ b/include/linux/list_bl.h @@ -24,13 +24,6 @@ #define LIST_BL_LOCKMASK 0UL #endif -#ifdef CONFIG_CHECK_INTEGRITY_LIST -#define LIST_BL_BUG_ON(x) BUG_ON(x) -#else -#define LIST_BL_BUG_ON(x) -#endif - - struct hlist_bl_head { struct hlist_bl_node *first; }; @@ -38,6 +31,37 @@ struct hlist_bl_head { struct hlist_bl_node { struct hlist_bl_node *next, **pprev; }; + +#ifdef CONFIG_CHECK_INTEGRITY_LIST +static inline bool __hlist_bl_add_head_valid(struct hlist_bl_head *h, + struct hlist_bl_node *n) +{ + unsigned long hlock = (unsigned long)h->first & LIST_BL_LOCKMASK; + unsigned long nlock = (unsigned long)n & LIST_BL_LOCKMASK; + + return !(CHECK_DATA_CORRUPTION(nlock, + "hlist_bl_add_head: node is locked\n") || + CHECK_DATA_CORRUPTION(hlock != LIST_BL_LOCKMASK, + "hlist_bl_add_head: head is unlocked\n")); +} + +static inline bool __hlist_bl_del_valid(struct hlist_bl_node *n) +{ + unsigned long nlock = (unsigned long)n & LIST_BL_LOCKMASK; + return !CHECK_DATA_CORRUPTION(nlock, "hlist_bl_del_valid: node locked"); +} +#else +static inline bool __hlist_bl_add_head_valid(struct hlist_bl_head *h, + struct hlist_bl_node *n) +{ + return true; +} +static inline bool __hlist_bl_del_valid(struct hlist_bl_node *n) +{ + return true; +} +#endif + #define INIT_HLIST_BL_HEAD(ptr) \ ((ptr)->first = NULL) @@ -60,15 +84,6 @@ static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h) ((unsigned long)h->first & ~LIST_BL_LOCKMASK); } -static inline void hlist_bl_set_first(struct hlist_bl_head *h, - struct hlist_bl_node *n) -{ - LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK); - LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) != - LIST_BL_LOCKMASK); - h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK); -} - static inline bool hlist_bl_empty(const struct hlist_bl_head *h) { unsigned long first = data_race((unsigned long)READ_ONCE(h->first)); @@ -80,11 +95,14 @@ static inline void hlist_bl_add_head(struct hlist_bl_node *n, { struct hlist_bl_node *first = hlist_bl_first(h); + if (!__hlist_bl_add_head_valid(h, n)) + return; + n->next = first; if (first) first->pprev = &n->next; n->pprev = &h->first; - hlist_bl_set_first(h, n); + h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK); } static inline void hlist_bl_add_before(struct hlist_bl_node *n, @@ -118,7 +136,8 @@ static inline void __hlist_bl_del(struct hlist_bl_node *n) struct hlist_bl_node *next = n->next; struct hlist_bl_node **pprev = n->pprev; - LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK); + if (!__hlist_bl_del_valid(n)) + return; /* pprev may be `first`, so be careful not to lose the lock bit */ WRITE_ONCE(*pprev, diff --git a/include/linux/rculist_bl.h b/include/linux/rculist_bl.h index 0b952d06eb0b..553ce3cde104 100644 --- a/include/linux/rculist_bl.h +++ b/include/linux/rculist_bl.h @@ -8,16 +8,6 @@ #include #include -static inline void hlist_bl_set_first_rcu(struct hlist_bl_head *h, - struct hlist_bl_node *n) -{ - LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK); - LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) != - LIST_BL_LOCKMASK); - rcu_assign_pointer(h->first, - (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK)); -} - static inline struct hlist_bl_node *hlist_bl_first_rcu(struct hlist_bl_head *h) { return (struct hlist_bl_node *) @@ -73,6 +63,9 @@ static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n, { struct hlist_bl_node *first; + if (!__hlist_bl_add_head_valid(h, n)) + return; + /* don't need hlist_bl_first_rcu because we're under lock */ first = hlist_bl_first(h); @@ -81,8 +74,8 @@ static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n, first->pprev = &n->next; n->pprev = &h->first; - /* need _rcu because we can have concurrent lock free readers */ - hlist_bl_set_first_rcu(h, n); + rcu_assign_pointer(h->first, + (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK)); } /** * hlist_bl_for_each_entry_rcu - iterate over rcu list of given type -- 2.20.1