Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/compiler.h>
0003 #include <linux/kernel.h>
0004 #include "tests.h"
0005 #include "map.h"
0006 #include "maps.h"
0007 #include "dso.h"
0008 #include "debug.h"
0009 
0010 struct map_def {
0011     const char *name;
0012     u64 start;
0013     u64 end;
0014 };
0015 
0016 static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
0017 {
0018     struct map *map;
0019     unsigned int i = 0;
0020 
0021     maps__for_each_entry(maps, map) {
0022         if (i > 0)
0023             TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i == size));
0024 
0025         TEST_ASSERT_VAL("wrong map start",  map->start == merged[i].start);
0026         TEST_ASSERT_VAL("wrong map end",    map->end == merged[i].end);
0027         TEST_ASSERT_VAL("wrong map name",  !strcmp(map->dso->name, merged[i].name));
0028         TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) == 1);
0029 
0030         i++;
0031     }
0032 
0033     return TEST_OK;
0034 }
0035 
0036 static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
0037 {
0038     unsigned int i;
0039     struct map_def bpf_progs[] = {
0040         { "bpf_prog_1", 200, 300 },
0041         { "bpf_prog_2", 500, 600 },
0042         { "bpf_prog_3", 800, 900 },
0043     };
0044     struct map_def merged12[] = {
0045         { "kcore1",     100,  200 },
0046         { "bpf_prog_1", 200,  300 },
0047         { "kcore1",     300,  500 },
0048         { "bpf_prog_2", 500,  600 },
0049         { "kcore1",     600,  800 },
0050         { "bpf_prog_3", 800,  900 },
0051         { "kcore1",     900, 1000 },
0052     };
0053     struct map_def merged3[] = {
0054         { "kcore1",      100,  200 },
0055         { "bpf_prog_1",  200,  300 },
0056         { "kcore1",      300,  500 },
0057         { "bpf_prog_2",  500,  600 },
0058         { "kcore1",      600,  800 },
0059         { "bpf_prog_3",  800,  900 },
0060         { "kcore1",      900, 1000 },
0061         { "kcore3",     1000, 1100 },
0062     };
0063     struct map *map_kcore1, *map_kcore2, *map_kcore3;
0064     int ret;
0065     struct maps *maps = maps__new(NULL);
0066 
0067     TEST_ASSERT_VAL("failed to create maps", maps);
0068 
0069     for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
0070         struct map *map;
0071 
0072         map = dso__new_map(bpf_progs[i].name);
0073         TEST_ASSERT_VAL("failed to create map", map);
0074 
0075         map->start = bpf_progs[i].start;
0076         map->end   = bpf_progs[i].end;
0077         maps__insert(maps, map);
0078         map__put(map);
0079     }
0080 
0081     map_kcore1 = dso__new_map("kcore1");
0082     TEST_ASSERT_VAL("failed to create map", map_kcore1);
0083 
0084     map_kcore2 = dso__new_map("kcore2");
0085     TEST_ASSERT_VAL("failed to create map", map_kcore2);
0086 
0087     map_kcore3 = dso__new_map("kcore3");
0088     TEST_ASSERT_VAL("failed to create map", map_kcore3);
0089 
0090     /* kcore1 map overlaps over all bpf maps */
0091     map_kcore1->start = 100;
0092     map_kcore1->end   = 1000;
0093 
0094     /* kcore2 map hides behind bpf_prog_2 */
0095     map_kcore2->start = 550;
0096     map_kcore2->end   = 570;
0097 
0098     /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
0099     map_kcore3->start = 880;
0100     map_kcore3->end   = 1100;
0101 
0102     ret = maps__merge_in(maps, map_kcore1);
0103     TEST_ASSERT_VAL("failed to merge map", !ret);
0104 
0105     ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
0106     TEST_ASSERT_VAL("merge check failed", !ret);
0107 
0108     ret = maps__merge_in(maps, map_kcore2);
0109     TEST_ASSERT_VAL("failed to merge map", !ret);
0110 
0111     ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
0112     TEST_ASSERT_VAL("merge check failed", !ret);
0113 
0114     ret = maps__merge_in(maps, map_kcore3);
0115     TEST_ASSERT_VAL("failed to merge map", !ret);
0116 
0117     ret = check_maps(merged3, ARRAY_SIZE(merged3), maps);
0118     TEST_ASSERT_VAL("merge check failed", !ret);
0119 
0120     maps__delete(maps);
0121     return TEST_OK;
0122 }
0123 
0124 DEFINE_SUITE("maps__merge_in", maps__merge_in);