1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2024 Benjamin Tissoires 3 */ 4 5#include "bpf_experimental.h" 6#include <bpf/bpf_helpers.h> 7#include "bpf_misc.h" 8#include "../bpf_testmod/bpf_testmod_kfunc.h" 9 10char _license[] SEC("license") = "GPL"; 11 12struct elem { 13 struct bpf_wq w; 14}; 15 16struct { 17 __uint(type, BPF_MAP_TYPE_ARRAY); 18 __uint(max_entries, 2); 19 __type(key, int); 20 __type(value, struct elem); 21} array SEC(".maps"); 22 23struct { 24 __uint(type, BPF_MAP_TYPE_LRU_HASH); 25 __uint(max_entries, 4); 26 __type(key, int); 27 __type(value, struct elem); 28} lru SEC(".maps"); 29 30/* callback for non sleepable workqueue */ 31static int wq_callback(void *map, int *key, struct bpf_wq *work) 32{ 33 bpf_kfunc_common_test(); 34 return 0; 35} 36 37/* callback for sleepable workqueue */ 38static int wq_cb_sleepable(void *map, int *key, struct bpf_wq *work) 39{ 40 bpf_kfunc_call_test_sleepable(); 41 return 0; 42} 43 44SEC("tc") 45/* test that bpf_wq_init takes a map as a second argument 46 */ 47__log_level(2) 48__flag(BPF_F_TEST_STATE_FREQ) 49__failure 50__msg(": (85) call bpf_wq_init#") /* anchor message */ 51__msg("pointer in R2 isn't map pointer") 52long test_wq_init_nomap(void *ctx) 53{ 54 struct bpf_wq *wq; 55 struct elem *val; 56 int key = 0; 57 58 val = bpf_map_lookup_elem(&array, &key); 59 if (!val) 60 return -1; 61 62 wq = &val->w; 63 if (bpf_wq_init(wq, &key, 0) != 0) 64 return -3; 65 66 return 0; 67} 68 69SEC("tc") 70/* test that the workqueue is part of the map in bpf_wq_init 71 */ 72__log_level(2) 73__flag(BPF_F_TEST_STATE_FREQ) 74__failure 75__msg(": (85) call bpf_wq_init#") /* anchor message */ 76__msg("workqueue pointer in R1 map_uid=0 doesn't match map pointer in R2 map_uid=0") 77long test_wq_init_wrong_map(void *ctx) 78{ 79 struct bpf_wq *wq; 80 struct elem *val; 81 int key = 0; 82 83 val = bpf_map_lookup_elem(&array, &key); 84 if (!val) 85 return -1; 86 87 wq = &val->w; 88 if (bpf_wq_init(wq, &lru, 0) != 0) 89 return -3; 90 91 return 0; 92} 93 94SEC("?tc") 95__log_level(2) 96__failure 97/* check that the first argument of bpf_wq_set_callback() 98 * is a correct bpf_wq pointer. 99 */ 100__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */ 101__msg("arg#0 doesn't point to a map value") 102long test_wrong_wq_pointer(void *ctx) 103{ 104 int key = 0; 105 struct bpf_wq *wq; 106 107 wq = bpf_map_lookup_elem(&array, &key); 108 if (!wq) 109 return 1; 110 111 if (bpf_wq_init(wq, &array, 0)) 112 return 2; 113 114 if (bpf_wq_set_callback((void *)&wq, wq_callback, 0)) 115 return 3; 116 117 return -22; 118} 119 120SEC("?tc") 121__log_level(2) 122__failure 123/* check that the first argument of bpf_wq_set_callback() 124 * is a correct bpf_wq pointer. 125 */ 126__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */ 127__msg("off 1 doesn't point to 'struct bpf_wq' that is at 0") 128long test_wrong_wq_pointer_offset(void *ctx) 129{ 130 int key = 0; 131 struct bpf_wq *wq; 132 133 wq = bpf_map_lookup_elem(&array, &key); 134 if (!wq) 135 return 1; 136 137 if (bpf_wq_init(wq, &array, 0)) 138 return 2; 139 140 if (bpf_wq_set_callback((void *)wq + 1, wq_cb_sleepable, 0)) 141 return 3; 142 143 return -22; 144} 145