From 47e2ba05eddea8a79aafbb5aaf0a8b24189490e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Wed, 30 Jul 2025 18:21:48 -0700 Subject: [PATCH] bpf: switch map 'pin_prefix' to 'pin_location' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit easy to do for maps now, but harder for programs Test: TreeHugger Flag: EXEMPT mainline Signed-off-by: Maciej Żenczykowski Change-Id: I0aedef7a8f28d8cc60ca4912f2666f920b9dbeb5 --- bpf/loader/NetBpfLoad.cpp | 32 +++++++++++++++----------------- bpf/progs/include/bpf_helpers.h | 2 +- bpf/progs/include/bpf_map_def.h | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index 93588575d8..9693007c53 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -772,7 +772,7 @@ static bool isBtfSupported(enum bpf_map_type type) { return type != BPF_MAP_TYPE_DEVMAP_HASH && type != BPF_MAP_TYPE_RINGBUF; } -static int pinMap(const borrowed_fd& fd, const struct bpf_map_def& mapDef, const string& mapPinLoc) { +static int pinMap(const borrowed_fd& fd, const struct bpf_map_def& mapDef) { int ret; if (mapDef.create_location[0]) { ret = bpfFdPin(fd, mapDef.create_location); @@ -782,32 +782,32 @@ static int pinMap(const borrowed_fd& fd, const struct bpf_map_def& mapDef, const return -err; } ret = renameat2(AT_FDCWD, mapDef.create_location, - AT_FDCWD, mapPinLoc.c_str(), RENAME_NOREPLACE); + AT_FDCWD, mapDef.pin_location, RENAME_NOREPLACE); if (ret) { const int err = errno; - ALOGE("rename %s %s -> %d [%d:%s]", mapDef.create_location, mapPinLoc.c_str(), ret, + ALOGE("rename %s %s -> %d [%d:%s]", mapDef.create_location, mapDef.pin_location, ret, err, strerror(err)); return -err; } } else { - ret = bpfFdPin(fd, mapPinLoc.c_str()); + ret = bpfFdPin(fd, mapDef.pin_location); if (ret) { const int err = errno; - ALOGE("pin %s -> %d [%d:%s]", mapPinLoc.c_str(), ret, err, strerror(err)); + ALOGE("pin %s -> %d [%d:%s]", mapDef.pin_location, ret, err, strerror(err)); return -err; } } - ret = chmod(mapPinLoc.c_str(), mapDef.mode); + ret = chmod(mapDef.pin_location, mapDef.mode); if (ret) { const int err = errno; - ALOGE("chmod(%s, 0%o) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.mode, ret, err, + ALOGE("chmod(%s, 0%o) = %d [%d:%s]", mapDef.pin_location, mapDef.mode, ret, err, strerror(err)); return -err; } - ret = chown(mapPinLoc.c_str(), (uid_t)mapDef.uid, (gid_t)mapDef.gid); + ret = chown(mapDef.pin_location, (uid_t)mapDef.uid, (gid_t)mapDef.gid); if (ret) { const int err = errno; - ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.uid, mapDef.gid, + ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapDef.pin_location, mapDef.uid, mapDef.gid, ret, err, strerror(err)); return -err; } @@ -819,7 +819,7 @@ static int pinMap(const borrowed_fd& fd, const struct bpf_map_def& mapDef, const ALOGE("bpfGetFdMapId failed, errno: %d", err); return -err; } - ALOGI("map %s id %d", mapPinLoc.c_str(), mapId); + ALOGI("map %s id %d", mapDef.pin_location, mapId); } return 0; } @@ -952,12 +952,11 @@ static int createMaps(ifstream& elfFile, vector& mapFds, if (max_entries < page_size) max_entries = page_size; } - string mapPinLoc = string(md[i].pin_prefix) + mapNames[i]; unique_fd fd; int saved_errno; - if (access(mapPinLoc.c_str(), F_OK) == 0) { - fd.reset(mapRetrieveRO(mapPinLoc.c_str())); + if (access(md[i].pin_location, F_OK) == 0) { + fd.reset(mapRetrieveRO(md[i].pin_location)); saved_errno = errno; ALOGD("bpf_create_map reusing map %s, ret: %d", mapNames[i].c_str(), fd.get()); abort(); @@ -1001,7 +1000,7 @@ static int createMaps(ifstream& elfFile, vector& mapFds, // We assume failure is due to pinned map mismatch, hence the 'NOT UNIQUE' return code. if (!mapMatchesExpectations(fd, mapNames[i], md[i], type)) return -ENOTUNIQ; - ret = pinMap(fd, md[i], mapPinLoc); + ret = pinMap(fd, md[i]); if (ret) return ret; mapFds.push_back(std::move(fd)); @@ -1344,13 +1343,12 @@ static int pinMaps(const struct bpf_object* obj, // This map was skipped if (!bpf_map__autocreate(m)) continue; - string mapPinLoc = string(md[i].pin_prefix) + mapNames[i]; - if (access(mapPinLoc.c_str(), F_OK) == 0) { + if (access(md[i].pin_location, F_OK) == 0) { ALOGE("Reusing map is not supported: %s", mapNames[i].c_str()); return -1; } - ret = pinMap(bpf_map__fd(m), md[i], mapPinLoc); + ret = pinMap(bpf_map__fd(m), md[i]); if (ret) return ret; } return 0; diff --git a/bpf/progs/include/bpf_helpers.h b/bpf/progs/include/bpf_helpers.h index 556aec386e..a5c7a3c99d 100644 --- a/bpf/progs/include/bpf_helpers.h +++ b/bpf/progs/include/bpf_helpers.h @@ -260,7 +260,7 @@ static int (*bpf_sk_storage_delete_unsafe) (const void* sk_storage, .min_kver = (minkver).kver, \ .max_kver = (maxkver).kver, \ .create_location = CREATE_LOCATION(selinux), \ - .pin_prefix = "/sys/fs/bpf/" pindir "/map_" BPF_OBJ_NAME "_", \ + .pin_location = "/sys/fs/bpf/" pindir "/map_" BPF_OBJ_NAME "_" #the_map, \ }; #define __uint(name, val) int (*name)[val] diff --git a/bpf/progs/include/bpf_map_def.h b/bpf/progs/include/bpf_map_def.h index e40c9b37a5..8e4a5def61 100644 --- a/bpf/progs/include/bpf_map_def.h +++ b/bpf/progs/include/bpf_map_def.h @@ -143,7 +143,7 @@ struct bpf_map_def { // These are fixed length ASCIIZ strings, padded with null bytes char create_location[BPF_DEF_CHAR_ARRAY_SIZE]; - char pin_prefix[BPF_DEF_CHAR_ARRAY_SIZE]; + char pin_location[BPF_DEF_CHAR_ARRAY_SIZE]; }; // This needs to be updated whenever the above structure definition is expanded.