From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
stable@vger.kernel.org, Adrian Hunter <adrian.hunter@intel.com>,
David Ahern <dsahern@gmail.com>,
Elena Reshetova <elena.reshetova@intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Wang Nan <wangnan0@huawei.com>,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 4.9 11/29] tools include: Adopt kernels refcount.h
Date: Mon, 4 Jun 2018 08:58:07 +0200 [thread overview]
Message-ID: <20180604065802.606741436@linuxfoundation.org> (raw)
In-Reply-To: <20180604065802.157744637@linuxfoundation.org>
4.9-stable review patch. If anyone has any objections, please let me know.
------------------
From: Arnaldo Carvalho de Melo <acme@redhat.com>
commit 73a9bf95ed1c05698ecabe2f28c47aedfa61b52b upstream.
To aid in catching bugs when using atomics as a reference count.
This is a trimmed down version with just what is used by tools/ at
this point.
After this, the patches submitted by Elena for tools/ doing the
conversion from atomic_ to recount_ methods can be applied and tested.
To activate it, buint perf with:
make DEBUG=1 -C tools/perf
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-dqtxsumns9ov0l9r5x398f19@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
tools/include/linux/refcount.h | 151 +++++++++++++++++++++++++++++++++++++++++
tools/perf/MANIFEST | 1
2 files changed, 152 insertions(+)
--- /dev/null
+++ b/tools/include/linux/refcount.h
@@ -0,0 +1,151 @@
+#ifndef _TOOLS_LINUX_REFCOUNT_H
+#define _TOOLS_LINUX_REFCOUNT_H
+
+/*
+ * Variant of atomic_t specialized for reference counts.
+ *
+ * The interface matches the atomic_t interface (to aid in porting) but only
+ * provides the few functions one should use for reference counting.
+ *
+ * It differs in that the counter saturates at UINT_MAX and will not move once
+ * there. This avoids wrapping the counter and causing 'spurious'
+ * use-after-free issues.
+ *
+ * Memory ordering rules are slightly relaxed wrt regular atomic_t functions
+ * and provide only what is strictly required for refcounts.
+ *
+ * The increments are fully relaxed; these will not provide ordering. The
+ * rationale is that whatever is used to obtain the object we're increasing the
+ * reference count on will provide the ordering. For locked data structures,
+ * its the lock acquire, for RCU/lockless data structures its the dependent
+ * load.
+ *
+ * Do note that inc_not_zero() provides a control dependency which will order
+ * future stores against the inc, this ensures we'll never modify the object
+ * if we did not in fact acquire a reference.
+ *
+ * The decrements will provide release order, such that all the prior loads and
+ * stores will be issued before, it also provides a control dependency, which
+ * will order us against the subsequent free().
+ *
+ * The control dependency is against the load of the cmpxchg (ll/sc) that
+ * succeeded. This means the stores aren't fully ordered, but this is fine
+ * because the 1->0 transition indicates no concurrency.
+ *
+ * Note that the allocator is responsible for ordering things between free()
+ * and alloc().
+ *
+ */
+
+#include <linux/atomic.h>
+#include <linux/kernel.h>
+
+#ifdef NDEBUG
+#define REFCOUNT_WARN(cond, str) (void)(cond)
+#define __refcount_check
+#else
+#define REFCOUNT_WARN(cond, str) BUG_ON(cond)
+#define __refcount_check __must_check
+#endif
+
+typedef struct refcount_struct {
+ atomic_t refs;
+} refcount_t;
+
+#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
+
+static inline void refcount_set(refcount_t *r, unsigned int n)
+{
+ atomic_set(&r->refs, n);
+}
+
+static inline unsigned int refcount_read(const refcount_t *r)
+{
+ return atomic_read(&r->refs);
+}
+
+/*
+ * Similar to atomic_inc_not_zero(), will saturate at UINT_MAX and WARN.
+ *
+ * Provides no memory ordering, it is assumed the caller has guaranteed the
+ * object memory to be stable (RCU, etc.). It does provide a control dependency
+ * and thereby orders future stores. See the comment on top.
+ */
+static inline __refcount_check
+bool refcount_inc_not_zero(refcount_t *r)
+{
+ unsigned int old, new, val = atomic_read(&r->refs);
+
+ for (;;) {
+ new = val + 1;
+
+ if (!val)
+ return false;
+
+ if (unlikely(!new))
+ return true;
+
+ old = atomic_cmpxchg_relaxed(&r->refs, val, new);
+ if (old == val)
+ break;
+
+ val = old;
+ }
+
+ REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");
+
+ return true;
+}
+
+/*
+ * Similar to atomic_inc(), will saturate at UINT_MAX and WARN.
+ *
+ * Provides no memory ordering, it is assumed the caller already has a
+ * reference on the object, will WARN when this is not so.
+ */
+static inline void refcount_inc(refcount_t *r)
+{
+ REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n");
+}
+
+/*
+ * Similar to atomic_dec_and_test(), it will WARN on underflow and fail to
+ * decrement when saturated at UINT_MAX.
+ *
+ * Provides release memory ordering, such that prior loads and stores are done
+ * before, and provides a control dependency such that free() must come after.
+ * See the comment on top.
+ */
+static inline __refcount_check
+bool refcount_sub_and_test(unsigned int i, refcount_t *r)
+{
+ unsigned int old, new, val = atomic_read(&r->refs);
+
+ for (;;) {
+ if (unlikely(val == UINT_MAX))
+ return false;
+
+ new = val - i;
+ if (new > val) {
+ REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n");
+ return false;
+ }
+
+ old = atomic_cmpxchg_release(&r->refs, val, new);
+ if (old == val)
+ break;
+
+ val = old;
+ }
+
+ return !new;
+}
+
+static inline __refcount_check
+bool refcount_dec_and_test(refcount_t *r)
+{
+ return refcount_sub_and_test(1, r);
+}
+
+
+#endif /* _ATOMIC_LINUX_REFCOUNT_H */
--- a/tools/perf/MANIFEST
+++ b/tools/perf/MANIFEST
@@ -78,6 +78,7 @@ tools/include/uapi/linux/perf_event.h
tools/include/linux/poison.h
tools/include/linux/rbtree.h
tools/include/linux/rbtree_augmented.h
+tools/include/linux/refcount.h
tools/include/linux/string.h
tools/include/linux/stringify.h
tools/include/linux/types.h
next prev parent reply other threads:[~2018-06-04 6:59 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-04 6:57 [PATCH 4.9 00/29] 4.9.106-stable review Greg Kroah-Hartman
2018-06-04 6:57 ` [PATCH 4.9 01/29] objtool: Improve detection of BUG() and other dead ends Greg Kroah-Hartman
2018-06-04 6:57 ` [PATCH 4.9 02/29] objtool: Move checking code to check.c Greg Kroah-Hartman
2018-06-04 6:57 ` [PATCH 4.9 03/29] tools lib: Add for_each_clear_bit macro Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 04/29] tools: add more bitmap functions Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 05/29] tools: enable endian checks for all sparse builds Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 06/29] tools include: Introduce linux/compiler-gcc.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 07/29] radix tree test suite: Remove types.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 08/29] tools include: Adopt __compiletime_error Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 09/29] tools include: Introduce atomic_cmpxchg_{relaxed,release}() Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 10/29] tools include: Add UINT_MAX def to kernel.h Greg Kroah-Hartman
2018-06-04 6:58 ` Greg Kroah-Hartman [this message]
2018-06-04 6:58 ` [PATCH 4.9 12/29] perf tools: Force fixdep compilation at the start of the build Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 13/29] perf tools: Move headers check into bash script Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 14/29] tools include uapi: Grab copies of stat.h and fcntl.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 15/29] tools include: Introduce linux/bug.h, from the kernel sources Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 16/29] tools include: Adopt __same_type() and __must_be_array() from the kernel Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 17/29] tools include: Move ARRAY_SIZE() to linux/kernel.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 18/29] tools include: Drop ARRAY_SIZE() definition from linux/hashtable.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 19/29] tools include: Include missing headers for fls() and types in linux/log2.h Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 20/29] objtool: sync up with the 4.14.47 version of objtool Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 21/29] objtool: Support GCC 8s cold subfunctions Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 22/29] objtool: Support GCC 8 switch tables Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 23/29] objtool: Detect RIP-relative switch table references Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 24/29] objtool: Detect RIP-relative switch table references, part 2 Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 25/29] objtool: Fix "noreturn" detection for recursive sibling calls Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 26/29] objtool, x86: Add several functions and files to the objtool whitelist Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 27/29] perf/tools: header file sync up Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 28/29] objtool: header file sync-up Greg Kroah-Hartman
2018-06-04 6:58 ` [PATCH 4.9 29/29] x86/xen: Add unwind hint annotations to xen_setup_gdt Greg Kroah-Hartman
2018-06-04 10:15 ` [PATCH 4.9 00/29] 4.9.106-stable review Guenter Roeck
2018-06-04 11:27 ` Greg Kroah-Hartman
2018-06-04 12:21 ` Greg Kroah-Hartman
2018-06-04 16:16 ` Guenter Roeck
2018-06-04 16:54 ` Guenter Roeck
2018-06-04 16:57 ` Greg Kroah-Hartman
2018-06-04 17:04 ` Guenter Roeck
2018-06-04 16:58 ` Greg Kroah-Hartman
2018-06-04 19:46 ` Shuah Khan
2018-06-05 6:14 ` Naresh Kamboju
[not found] ` <5b152b7c.1c69fb81.3f04a.096f@mx.google.com>
[not found] ` <7hr2lmjgcd.fsf@baylibre.com>
2018-06-05 10:51 ` Mark Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180604065802.606741436@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=dsahern@gmail.com \
--cc=elena.reshetova@intel.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=namhyung@kernel.org \
--cc=stable@vger.kernel.org \
--cc=wangnan0@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).