Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #define _GNU_SOURCE
0004 #include <sched.h>
0005 #include <test_progs.h>
0006 #include <time.h>
0007 #include <sys/mman.h>
0008 #include <sys/syscall.h>
0009 #include "fexit_sleep.lskel.h"
0010 
0011 static int do_sleep(void *skel)
0012 {
0013     struct fexit_sleep_lskel *fexit_skel = skel;
0014     struct timespec ts1 = { .tv_nsec = 1 };
0015     struct timespec ts2 = { .tv_sec = 10 };
0016 
0017     fexit_skel->bss->pid = getpid();
0018     (void)syscall(__NR_nanosleep, &ts1, NULL);
0019     (void)syscall(__NR_nanosleep, &ts2, NULL);
0020     return 0;
0021 }
0022 
0023 #define STACK_SIZE (1024 * 1024)
0024 static char child_stack[STACK_SIZE];
0025 
0026 void test_fexit_sleep(void)
0027 {
0028     struct fexit_sleep_lskel *fexit_skel = NULL;
0029     int wstatus, duration = 0;
0030     pid_t cpid;
0031     int err, fexit_cnt;
0032 
0033     fexit_skel = fexit_sleep_lskel__open_and_load();
0034     if (CHECK(!fexit_skel, "fexit_skel_load", "fexit skeleton failed\n"))
0035         goto cleanup;
0036 
0037     err = fexit_sleep_lskel__attach(fexit_skel);
0038     if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
0039         goto cleanup;
0040 
0041     cpid = clone(do_sleep, child_stack + STACK_SIZE, CLONE_FILES | SIGCHLD, fexit_skel);
0042     if (CHECK(cpid == -1, "clone", "%s\n", strerror(errno)))
0043         goto cleanup;
0044 
0045     /* wait until first sys_nanosleep ends and second sys_nanosleep starts */
0046     while (READ_ONCE(fexit_skel->bss->fentry_cnt) != 2);
0047     fexit_cnt = READ_ONCE(fexit_skel->bss->fexit_cnt);
0048     if (CHECK(fexit_cnt != 1, "fexit_cnt", "%d", fexit_cnt))
0049         goto cleanup;
0050 
0051     /* close progs and detach them. That will trigger two nop5->jmp5 rewrites
0052      * in the trampolines to skip nanosleep_fexit prog.
0053      * The nanosleep_fentry prog will get detached first.
0054      * The nanosleep_fexit prog will get detached second.
0055      * Detaching will trigger freeing of both progs JITed images.
0056      * There will be two dying bpf_tramp_image-s, but only the initial
0057      * bpf_tramp_image (with both _fentry and _fexit progs will be stuck
0058      * waiting for percpu_ref_kill to confirm). The other one
0059      * will be freed quickly.
0060      */
0061     close(fexit_skel->progs.nanosleep_fentry.prog_fd);
0062     close(fexit_skel->progs.nanosleep_fexit.prog_fd);
0063     fexit_sleep_lskel__detach(fexit_skel);
0064 
0065     /* kill the thread to unwind sys_nanosleep stack through the trampoline */
0066     kill(cpid, 9);
0067 
0068     if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", "%s\n", strerror(errno)))
0069         goto cleanup;
0070     if (CHECK(WEXITSTATUS(wstatus) != 0, "exitstatus", "failed"))
0071         goto cleanup;
0072 
0073     /* The bypassed nanosleep_fexit prog shouldn't have executed.
0074      * Unlike progs the maps were not freed and directly accessible.
0075      */
0076     fexit_cnt = READ_ONCE(fexit_skel->bss->fexit_cnt);
0077     if (CHECK(fexit_cnt != 1, "fexit_cnt", "%d", fexit_cnt))
0078         goto cleanup;
0079 
0080 cleanup:
0081     fexit_sleep_lskel__destroy(fexit_skel);
0082 }