mirror of
https://github.com/torvalds/linux.git
synced 2026-11-28 01:13:11 +00:00
Enabled when REFCNT_CHECKING is defined. The change adds a memory allocated pointer that is interposed between the reference counted cpu map at a get and freed by a put. The pointer replaces the original perf_cpu_map struct, so use of the perf_cpu_map via APIs remains unchanged. Any use of the cpu map without the API requires two versions, handled via the RC_CHK_ACCESS macro. This change is intended to catch: - use after put: using a cpumap after you have put it will cause a segv. - unbalanced puts: two puts for a get will result in a double free that can be captured and reported by tools like address sanitizer, including with the associated stack traces of allocation and frees. - missing puts: if a put is missing then the get turns into a memory leak that can be reported by leak sanitizer, including the stack trace at the point the get occurs. Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexey Bayduraev <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Darren Hart <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: Dmitriy Vyukov <[email protected]> Cc: Eric Dumazet <[email protected]> Cc: German Gomez <[email protected]> Cc: Hao Luo <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Garry <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Miaoqian Lin <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Riccardo Mancini <[email protected]> Cc: Shunsuke Nakamura <[email protected]> Cc: Song Liu <[email protected]> Cc: Stephen Brennan <[email protected]> Cc: Steven Rostedt (VMware) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Thomas Richter <[email protected]>, Cc: Yury Norov <[email protected]> Link: https://lore.kernel.org/lkml/[email protected] [ Extracted from a larger patch ] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
39 lines
1.2 KiB
C
39 lines
1.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef __LIBPERF_INTERNAL_CPUMAP_H
|
|
#define __LIBPERF_INTERNAL_CPUMAP_H
|
|
|
|
#include <linux/refcount.h>
|
|
#include <perf/cpumap.h>
|
|
#include <internal/rc_check.h>
|
|
|
|
/**
|
|
* A sized, reference counted, sorted array of integers representing CPU
|
|
* numbers. This is commonly used to capture which CPUs a PMU is associated
|
|
* with. The indices into the cpumap are frequently used as they avoid having
|
|
* gaps if CPU numbers were used. For events associated with a pid, rather than
|
|
* a CPU, a single dummy map with an entry of -1 is used.
|
|
*/
|
|
DECLARE_RC_STRUCT(perf_cpu_map) {
|
|
refcount_t refcnt;
|
|
/** Length of the map array. */
|
|
int nr;
|
|
/** The CPU values. */
|
|
struct perf_cpu map[];
|
|
};
|
|
|
|
#ifndef MAX_NR_CPUS
|
|
#define MAX_NR_CPUS 2048
|
|
#endif
|
|
|
|
struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus);
|
|
int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu);
|
|
bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpu_map *b);
|
|
|
|
void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus);
|
|
|
|
static inline refcount_t *perf_cpu_map__refcnt(struct perf_cpu_map *map)
|
|
{
|
|
return &RC_CHK_ACCESS(map)->refcnt;
|
|
}
|
|
#endif /* __LIBPERF_INTERNAL_CPUMAP_H */
|