|  | 
|  | 1 | +// SPDX-License-Identifier: GPL-2.0 | 
|  | 2 | +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ | 
|  | 3 | + | 
|  | 4 | +#include <test_progs.h> | 
|  | 5 | +#include "testing_helpers.h" | 
|  | 6 | +#include "livepatch_trampoline.skel.h" | 
|  | 7 | + | 
|  | 8 | +static int load_livepatch(void) | 
|  | 9 | +{ | 
|  | 10 | +	return load_module("../../../../samples/livepatch/livepatch-sample.ko", false); | 
|  | 11 | +} | 
|  | 12 | + | 
|  | 13 | +static void unload_livepatch(void) | 
|  | 14 | +{ | 
|  | 15 | +	/* Disable the livepatch before unloading the module */ | 
|  | 16 | +	system("echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled"); | 
|  | 17 | + | 
|  | 18 | +	unload_module("livepatch_sample", false); | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +static void read_proc_cmdline(void) | 
|  | 22 | +{ | 
|  | 23 | +	char buf[4096]; | 
|  | 24 | +	int fd, ret; | 
|  | 25 | + | 
|  | 26 | +	fd = open("/proc/cmdline", O_RDONLY); | 
|  | 27 | +	if (!ASSERT_OK_FD(fd, "open /proc/cmdline")) | 
|  | 28 | +		return; | 
|  | 29 | + | 
|  | 30 | +	ret = read(fd, buf, sizeof(buf)); | 
|  | 31 | +	if (!ASSERT_GT(ret, 0, "read /proc/cmdline")) | 
|  | 32 | +		goto out; | 
|  | 33 | + | 
|  | 34 | +	ASSERT_OK(strncmp(buf, "this has been live patched", 26), "strncmp"); | 
|  | 35 | + | 
|  | 36 | +out: | 
|  | 37 | +	close(fd); | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +static void __test_livepatch_trampoline(bool fexit_first) | 
|  | 41 | +{ | 
|  | 42 | +	struct livepatch_trampoline *skel = NULL; | 
|  | 43 | +	int err; | 
|  | 44 | + | 
|  | 45 | +	skel = livepatch_trampoline__open_and_load(); | 
|  | 46 | +	if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) | 
|  | 47 | +		goto out; | 
|  | 48 | + | 
|  | 49 | +	skel->bss->my_pid = getpid(); | 
|  | 50 | + | 
|  | 51 | +	if (!fexit_first) { | 
|  | 52 | +		/* fentry program is loaded first by default */ | 
|  | 53 | +		err = livepatch_trampoline__attach(skel); | 
|  | 54 | +		if (!ASSERT_OK(err, "skel_attach")) | 
|  | 55 | +			goto out; | 
|  | 56 | +	} else { | 
|  | 57 | +		/* Manually load fexit program first. */ | 
|  | 58 | +		skel->links.fexit_cmdline = bpf_program__attach(skel->progs.fexit_cmdline); | 
|  | 59 | +		if (!ASSERT_OK_PTR(skel->links.fexit_cmdline, "attach_fexit")) | 
|  | 60 | +			goto out; | 
|  | 61 | + | 
|  | 62 | +		skel->links.fentry_cmdline = bpf_program__attach(skel->progs.fentry_cmdline); | 
|  | 63 | +		if (!ASSERT_OK_PTR(skel->links.fentry_cmdline, "attach_fentry")) | 
|  | 64 | +			goto out; | 
|  | 65 | +	} | 
|  | 66 | + | 
|  | 67 | +	read_proc_cmdline(); | 
|  | 68 | + | 
|  | 69 | +	ASSERT_EQ(skel->bss->fentry_hit, 1, "fentry_hit"); | 
|  | 70 | +	ASSERT_EQ(skel->bss->fexit_hit, 1, "fexit_hit"); | 
|  | 71 | +out: | 
|  | 72 | +	livepatch_trampoline__destroy(skel); | 
|  | 73 | +} | 
|  | 74 | + | 
|  | 75 | +void test_livepatch_trampoline(void) | 
|  | 76 | +{ | 
|  | 77 | +	int retry_cnt = 0; | 
|  | 78 | + | 
|  | 79 | +retry: | 
|  | 80 | +	if (load_livepatch()) { | 
|  | 81 | +		if (retry_cnt) { | 
|  | 82 | +			ASSERT_OK(1, "load_livepatch"); | 
|  | 83 | +			goto out; | 
|  | 84 | +		} | 
|  | 85 | +		/* | 
|  | 86 | +		 * Something else (previous run of the same test?) loaded | 
|  | 87 | +		 * the KLP module. Unload the KLP module and retry. | 
|  | 88 | +		 */ | 
|  | 89 | +		unload_livepatch(); | 
|  | 90 | +		retry_cnt++; | 
|  | 91 | +		goto retry; | 
|  | 92 | +	} | 
|  | 93 | + | 
|  | 94 | +	if (test__start_subtest("fentry_first")) | 
|  | 95 | +		__test_livepatch_trampoline(false); | 
|  | 96 | + | 
|  | 97 | +	if (test__start_subtest("fexit_first")) | 
|  | 98 | +		__test_livepatch_trampoline(true); | 
|  | 99 | +out: | 
|  | 100 | +	unload_livepatch(); | 
|  | 101 | +} | 
0 commit comments