bpfLoader: automatically bump ringbuffer size up to page size am: 8a117a374b am: 1cba4021bf am: f9ff12af51 am: a284f6434e am: 3c37c62ca3

Original change: https://android-review.googlesource.com/c/platform/system/bpf/+/2628969

Change-Id: I381c99dcd769ac8fbcb9362b15451cc1cbeb7bce
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp
index 71a5a64..231fe39 100644
--- a/libbpf_android/Loader.cpp
+++ b/libbpf_android/Loader.cpp
@@ -31,13 +31,13 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-// This is BpfLoader v0.39
+// This is BpfLoader v0.40
 // WARNING: If you ever hit cherrypick conflicts here you're doing it wrong:
 // You are NOT allowed to cherrypick bpfloader related patches out of order.
 // (indeed: cherrypicking is probably a bad idea and you should merge instead)
 // Mainline supports ONLY the published versions of the bpfloader for each Android release.
 #define BPFLOADER_VERSION_MAJOR 0u
-#define BPFLOADER_VERSION_MINOR 39u
+#define BPFLOADER_VERSION_MINOR 40u
 #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR)
 
 #include "BpfSyscallWrappers.h"
@@ -95,6 +95,8 @@
     return t;
 }
 
+static unsigned int page_size = static_cast<unsigned int>(getpagesize());
+
 constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") {
     switch (d) {
         case domain::unspecified:   return unspecified;
@@ -705,6 +707,11 @@
     if (type == BPF_MAP_TYPE_DEVMAP || type == BPF_MAP_TYPE_DEVMAP_HASH)
         desired_map_flags |= BPF_F_RDONLY_PROG;
 
+    unsigned int desired_max_entries = mapDef.max_entries;
+    if (type == BPF_MAP_TYPE_RINGBUF) {
+        if (desired_max_entries < page_size) desired_max_entries = page_size;
+    }
+
     // The following checks should *never* trigger, if one of them somehow does,
     // it probably means a bpf .o file has been changed/replaced at runtime
     // and bpfloader was manually rerun (normally it should only run *once*
@@ -715,7 +722,7 @@
     if ((fd_type == type) &&
         (fd_key_size == (int)mapDef.key_size) &&
         (fd_value_size == (int)mapDef.value_size) &&
-        (fd_max_entries == (int)mapDef.max_entries) &&
+        (fd_max_entries == (int)desired_max_entries) &&
         (fd_map_flags == desired_map_flags)) {
         return true;
     }
@@ -842,6 +849,11 @@
             type = BPF_MAP_TYPE_HASH;
         }
 
+        unsigned int max_entries = md[i].max_entries;
+        if (type == BPF_MAP_TYPE_RINGBUF) {
+            if (max_entries < page_size) max_entries = page_size;
+        }
+
         domain selinux_context = getDomainFromSelinuxContext(md[i].selinux_context);
         if (specified(selinux_context)) {
             if (!inDomainBitmask(selinux_context, allowedDomainBitmask)) {
@@ -887,7 +899,7 @@
                 .map_flags = md[i].map_flags,
                 .key_size = md[i].key_size,
                 .value_size = md[i].value_size,
-                .max_entries = md[i].max_entries,
+                .max_entries = max_entries,
             };
             if (btfFd.has_value() && btfTypeIdMap.find(mapNames[i]) != btfTypeIdMap.end()) {
                 attr.btf_fd = btfFd->get();