Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libperf: Add reference count checking macros

The macros serve as a way to debug use of a reference counted struct.

The macros add a memory allocated pointer that is interposed between
the reference counted original struct at a get and freed by a put.

The pointer replaces the original struct, so use of the struct name
via APIs remains unchanged.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Hao Luo <haoluo@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miaoqian Lin <linmq006@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Yury Norov <yury.norov@gmail.com>
Link: http://lore.kernel.org/lkml/20230407230405.2931830-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
a9b867f6 4121234a

+94
+94
tools/lib/perf/include/internal/rc_check.h
··· 1 + /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 + #ifndef __LIBPERF_INTERNAL_RC_CHECK_H 3 + #define __LIBPERF_INTERNAL_RC_CHECK_H 4 + 5 + #include <stdlib.h> 6 + #include <linux/zalloc.h> 7 + 8 + /* 9 + * Shared reference count checking macros. 10 + * 11 + * Reference count checking is an approach to sanitizing the use of reference 12 + * counted structs. It leverages address and leak sanitizers to make sure gets 13 + * are paired with a put. Reference count checking adds a malloc-ed layer of 14 + * indirection on a get, and frees it on a put. A missed put will be reported as 15 + * a memory leak. A double put will be reported as a double free. Accessing 16 + * after a put will cause a use-after-free and/or a segfault. 17 + */ 18 + 19 + #ifndef REFCNT_CHECKING 20 + /* Replaces "struct foo" so that the pointer may be interposed. */ 21 + #define DECLARE_RC_STRUCT(struct_name) \ 22 + struct struct_name 23 + 24 + /* Declare a reference counted struct variable. */ 25 + #define RC_STRUCT(struct_name) struct struct_name 26 + 27 + /* 28 + * Interpose the indirection. Result will hold the indirection and object is the 29 + * reference counted struct. 30 + */ 31 + #define ADD_RC_CHK(result, object) (result = object, object) 32 + 33 + /* Strip the indirection layer. */ 34 + #define RC_CHK_ACCESS(object) object 35 + 36 + /* Frees the object and the indirection layer. */ 37 + #define RC_CHK_FREE(object) free(object) 38 + 39 + /* A get operation adding the indirection layer. */ 40 + #define RC_CHK_GET(result, object) ADD_RC_CHK(result, object) 41 + 42 + /* A put operation removing the indirection layer. */ 43 + #define RC_CHK_PUT(object) {} 44 + 45 + #else 46 + 47 + /* Replaces "struct foo" so that the pointer may be interposed. */ 48 + #define DECLARE_RC_STRUCT(struct_name) \ 49 + struct original_##struct_name; \ 50 + struct struct_name { \ 51 + struct original_##struct_name *orig; \ 52 + }; \ 53 + struct original_##struct_name 54 + 55 + /* Declare a reference counted struct variable. */ 56 + #define RC_STRUCT(struct_name) struct original_##struct_name 57 + 58 + /* 59 + * Interpose the indirection. Result will hold the indirection and object is the 60 + * reference counted struct. 61 + */ 62 + #define ADD_RC_CHK(result, object) \ 63 + ( \ 64 + object ? (result = malloc(sizeof(*result)), \ 65 + result ? (result->orig = object, result) \ 66 + : (result = NULL, NULL)) \ 67 + : (result = NULL, NULL) \ 68 + ) 69 + 70 + /* Strip the indirection layer. */ 71 + #define RC_CHK_ACCESS(object) object->orig 72 + 73 + /* Frees the object and the indirection layer. */ 74 + #define RC_CHK_FREE(object) \ 75 + do { \ 76 + zfree(&object->orig); \ 77 + free(object); \ 78 + } while(0) 79 + 80 + /* A get operation adding the indirection layer. */ 81 + #define RC_CHK_GET(result, object) ADD_RC_CHK(result, (object ? object->orig : NULL)) 82 + 83 + /* A put operation removing the indirection layer. */ 84 + #define RC_CHK_PUT(object) \ 85 + do { \ 86 + if (object) { \ 87 + object->orig = NULL; \ 88 + free(object); \ 89 + } \ 90 + } while(0) 91 + 92 + #endif 93 + 94 + #endif /* __LIBPERF_INTERNAL_RC_CHECK_H */