kernel/events/hw_breakpoint_test.c

Source file repositories/reference/linux-study-clean/kernel/events/hw_breakpoint_test.c

File Facts

System
Linux kernel
Corpus path
kernel/events/hw_breakpoint_test.c
Extension
.c
Size
9796 bytes
Lines
333
Domain
Core OS
Bucket
Scheduler, Processes, Timers, Sync, And Syscalls
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

if ((slots) > get_test_bp_slots()) {					\
			kunit_skip((test), "Requires breakpoint slots: %d > %d", slots,	\
				   get_test_bp_slots());				\
		}									\
	} while (0)

#define TEST_EXPECT_NOSPC(expr) KUNIT_EXPECT_EQ(test, -ENOSPC, PTR_ERR(expr))

#define MAX_TEST_BREAKPOINTS 512

static char break_vars[MAX_TEST_BREAKPOINTS];
static struct perf_event *test_bps[MAX_TEST_BREAKPOINTS];
static struct task_struct *__other_task;

static struct perf_event *register_test_bp(int cpu, struct task_struct *tsk, int idx)
{
	struct perf_event_attr attr = {};

	if (WARN_ON(idx < 0 || idx >= MAX_TEST_BREAKPOINTS))
		return NULL;

	hw_breakpoint_init(&attr);
	attr.bp_addr = (unsigned long)&break_vars[idx];
	attr.bp_len = HW_BREAKPOINT_LEN_1;
	attr.bp_type = HW_BREAKPOINT_RW;
	return perf_event_create_kernel_counter(&attr, cpu, tsk, NULL, NULL);
}

static void unregister_test_bp(struct perf_event **bp)
{
	if (WARN_ON(IS_ERR(*bp)))
		return;
	if (WARN_ON(!*bp))
		return;
	unregister_hw_breakpoint(*bp);
	*bp = NULL;
}

static int get_test_bp_slots(void)
{
	static int slots;

	if (!slots)
		slots = hw_breakpoint_slots(TYPE_DATA);

	return slots;
}

static void fill_one_bp_slot(struct kunit *test, int *id, int cpu, struct task_struct *tsk)
{
	struct perf_event *bp = register_test_bp(cpu, tsk, *id);

	KUNIT_ASSERT_NOT_NULL(test, bp);
	KUNIT_ASSERT_FALSE(test, IS_ERR(bp));
	KUNIT_ASSERT_NULL(test, test_bps[*id]);
	test_bps[(*id)++] = bp;
}

/*
 * Fills up the given @cpu/@tsk with breakpoints, only leaving @skip slots free.
 *
 * Returns true if this can be called again, continuing at @id.
 */
static bool fill_bp_slots(struct kunit *test, int *id, int cpu, struct task_struct *tsk, int skip)
{
	for (int i = 0; i < get_test_bp_slots() - skip; ++i)
		fill_one_bp_slot(test, id, cpu, tsk);

	return *id + get_test_bp_slots() <= MAX_TEST_BREAKPOINTS;
}

static int dummy_kthread(void *arg)
{
	return 0;
}

static struct task_struct *get_other_task(struct kunit *test)
{
	struct task_struct *tsk;

	if (__other_task)
		return __other_task;

	tsk = kthread_create(dummy_kthread, NULL, "hw_breakpoint_dummy_task");
	KUNIT_ASSERT_FALSE(test, IS_ERR(tsk));
	__other_task = tsk;
	return __other_task;
}

static int get_test_cpu(int num)

Annotation

Implementation Notes