linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	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>
Subject: [PATCH 09/35] tools include: Adopt kernel's refcount.h
Date: Mon,  6 Mar 2017 16:37:59 -0300	[thread overview]
Message-ID: <20170306193825.24011-10-acme@kernel.org> (raw)
In-Reply-To: <20170306193825.24011-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

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>
---
 tools/include/linux/refcount.h | 151 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/MANIFEST            |   1 +
 2 files changed, 152 insertions(+)
 create mode 100644 tools/include/linux/refcount.h

diff --git a/tools/include/linux/refcount.h b/tools/include/linux/refcount.h
new file mode 100644
index 000000000000..a0177c1f55b1
--- /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 */
diff --git a/tools/perf/MANIFEST b/tools/perf/MANIFEST
index e2c52190cf28..28648c09dcd6 100644
--- a/tools/perf/MANIFEST
+++ b/tools/perf/MANIFEST
@@ -79,6 +79,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
-- 
2.9.3

  parent reply	other threads:[~2017-03-06 19:44 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-06 19:37 [GIT PULL 00/35] perf/core improvements and fixes Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 01/35] perf vendor events: Add mapping for KnightsMill PMU events Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 02/35] perf stat: Issue a HW watchdog disable hint Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 03/35] tools include: Adopt __compiletime_error Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 04/35] tools arch x86: Include asm/cmpxchg.h Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 05/35] tools arch x86: Introduce atomic_cmpxchg() Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 06/35] tools include: Introduce atomic_cmpxchg_{relaxed,release}() Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 07/35] tools include: Provide gcc based cmpxchg fallback for !x86 Arnaldo Carvalho de Melo
2017-03-06 19:37 ` [PATCH 08/35] tools include: Add UINT_MAX def to kernel.h Arnaldo Carvalho de Melo
2017-03-06 19:37 ` Arnaldo Carvalho de Melo [this message]
2017-03-06 19:38 ` [PATCH 10/35] perf cgroup: Convert cgroup_sel.refcnt from atomic_t to refcount_t Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 11/35] perf cpumap: Convert cpu_map.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 12/35] perf comm: Convert comm_str.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 13/35] perf dso: Convert dso.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 14/35] perf map: Convert map.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 15/35] perf map: Convert map_groups.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 16/35] perf evlist: Convert perf_map.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 17/35] perf thread: convert thread.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 18/35] perf thread_map: Convert thread_map.refcnt " Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 19/35] perf evlist: Clarify a bit the use of perf_mmap->refcnt Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 20/35] perf tools: Allow sorting by symbol size Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 21/35] perf ftrace: Add support for --pid option Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 22/35] perf cpumap: Introduce cpu_map__snprint_mask() Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 23/35] perf ftrace: Add support for -a and -C option Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 24/35] perf ftrace: Use pager for displaying result Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 25/35] kretprobes: Ensure probe location is at function entry Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 26/35] trace/kprobes: Allow return probes with offsets and absolute addresses Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 27/35] perf probe: Generalize probe event file open routine Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 28/35] perf intel-PT/BTS: Add missing initialization Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 29/35] trace/kprobes: Add back warning about offset in return probes Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 30/35] perf tools: Force uncore events to system wide monitoring Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 31/35] tools build: Add test for sched_getcpu() Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 32/35] perf bench futex: Use __maybe_unused Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 33/35] perf bench futex: Fix build on musl + clang Arnaldo Carvalho de Melo
2017-09-08  8:04   ` Jörg Krause
2017-09-08 13:47     ` Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 34/35] tools build: Use the same CC for feature detection and actual build Arnaldo Carvalho de Melo
2017-03-06 19:38 ` [PATCH 35/35] perf bench numa: Add more comment for -c option Arnaldo Carvalho de Melo
2017-03-07  7:17 ` [GIT PULL 00/35] perf/core improvements and fixes Ingo Molnar

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=20170306193825.24011-10-acme@kernel.org \
    --to=acme@kernel.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=mingo@kernel.org \
    --cc=namhyung@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).