Commit graph android_system_bpf/loader
Message SHA1
bpfloader: Allow overriding kernel version
This allows devices to pretend that they are on a different kernel version, in order to support different bpf features when kernel backports are in place.

Test: set ro.bpf.kver_override=5.10.239 in product.prop on sm8250 device
      based on 4.19 kernel with 5.10 bpf backported, skfilter_oom_kill
      loaded: bpftool prog | grep skfilter_oom_k

Change-Id: Ic32a279c562679408728483d5a5ed79a70af217d

kyasu   authored and   Thomas Turner   commited 2026-01-24 09:11:09 +00:00
6c5f4f8652
bpfloader: Add .kconfig to optionally pinned maps
Programs that use __kcfg values will have .kconfig sections that may not
have a pinned map descriptor. Allow them to be loaded.

Bug: 419635765
Test: manual. loaded program with .kconfig section.
Change-Id: Ifbfbfd443e034da21cbf8e7d456e7ad23dcd34b5
Signed-off-by: Tiffany Yang <ynaffit@google.com>

Tiffany Yang   commited 2025-09-08 19:17:17 -07:00
a80b309391
bpfloader: Add support for auto-attaching programs
Introduce an 'auto_attach' flag to the ProgDesc struct.

When this flag is true, bpfloader attaches the BPF program and pins
the resulting link. This makes the attachment persist across framework
reboots, only being removed when the device itself reboots.

This simplifies client-side logic, as clients no longer need to
manage attachments or handle missed events after a restart. They can
assume the program is always attached and simply read from the maps.

This is particularly useful for collecting statistics that would
otherwise be corrupted by framework restarts and for capturing events
that occur during early boot, before client processes are running.

Bug: 419635765
Bug: 438637519
Test: manual, load and auto attach ag/35064705
Change-Id: Iffcde8972778b9152f4732e65c221eb72523ea34
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>

Kalesh Singh   commited 2025-08-15 14:34:11 -07:00
809c5f5636
bpfloader: Sanitize BPF prog and map pin paths
Replace periods with underscores in prog/map pin paths to align with
libbpf's sanitize_pin_path() behavior. The BPF filesystem (bpffs)
disallows periods in filenames, and this change prevents pinning
errors for maps with names containing them, such as .rodata sections.

Bug: 419635765
Bug: 438637519
Test: manual, load maps names containing . from ag/35064705
Change-Id: I55dd4caf38c3ccc39bccd6ea670b647c330d6793
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>

Kalesh Singh   commited 2025-08-15 13:29:11 -07:00
d49152733c
BPFloader: Skip global map sections
Certain BPF progams have sections .rodata.str1.1 or <name>.data for
global data. These sections are processed/loaded implicitly by libbpf,
but it may be useful for the developer to pin them. Change the logic so
if a descriptor for these sections is found, pin them. If not, ignore
them and don't cause a failure.

Bug: 401458418
Test: Manual: program with bpf_printk and global data loads.
Change-Id: Ib3be55cf903e24c6692730b57b0ce90c9a9e0795
Signed-off-by: Neill Kapron <nkapron@google.com>

Neill Kapron   commited 2025-08-13 22:40:12 +00:00
2ba22f25e4
Improve error messages for missing BPF descriptors.
The error messages now specify whether a "Map descriptor" or a "Prog descriptor" was not found, providing more clarity.

Test: N/A, text only change
Flag: EXEMPT trivial
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7ce4806979a616f8330e5cca4d70c7ea2a1a256a

Maciej Żenczykowski   commited 2025-08-13 14:30:19 -07:00
a23e772683
Clippy lint fixes for rustc 1.88.0
Test: m rustClippyJson
Bug: 435702100
Flag: EXEMPT refactor
Change-Id: I3cbd23b2ddc51c369e7bdbe7c08eb356e82304a2

James Farrell   commited 2025-08-07 23:43:22 +00:00
3264f34d9c
Platform BpfLoader: remove support for /vendor/etc/bpf/filterPowerSupplyEvents.bpf
It doesn't exist in current builds, and we have the vendor bpf loader in C++ to handle /vendor/etc/bpf anyway.
It's unclear how to handle it from rust due to platform vs vendor skew...

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3ef707399445f91966e747d615dbf828d779a5ae

Maciej Żenczykowski   commited 2025-07-28 17:56:52 -07:00
8ce82c3dc4
Platform BpfLoader: make missing .bpf files fatal
as all are already present

(while at it add 'inheritance' from a default struct)

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I94258b8f5f8565193bfb688d50422733caf8dbf3

Maciej Żenczykowski   commited 2025-07-28 17:55:20 -07:00
a78ebbd0b1
Platform BpfLoader: replace '/etc' with '/system/etc'
(/etc is a symlink to /system/etc anyway)

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4d5b1b34066b97589779a43b8eaeed785edb611c

Maciej Żenczykowski   commited 2025-07-25 21:28:22 -07:00
0b7aa3aefe
Platform BpfLoader: eliminate 'dir' - just use fully qualified 'filename'
Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iaea2cff7ef0e96e600ea7429538d85b6c515e062

Maciej Żenczykowski   commited 2025-07-25 21:28:22 -07:00
f9d0b38967
Platform BpfLoader: add initial / to /vendor/etc/bpf/ path
(Technically a no-op, since the file doesn't exist yet)

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie72de49e452994a5dca97559d86fa2d7bb4bedcb

Maciej Żenczykowski   commited 2025-07-25 21:01:36 -07:00
25247915db
vendorBpfLoader: error handling simplifications
We continue to have:
  $ adb shell ls -lZ /sys/fs/bpf/vendor
  total 0
  -r--r----- 1 root system u:object_r:fs_bpf_vendor:s0  0 2025-07-16 09:37 prog_filterPowerSupplyEvents_skfilter_power_supply

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2be675467688cf5f9c651b8311dceb68744100a8

Maciej Żenczykowski   commited 2025-07-16 09:38:26 -07:00
b073360177
vendorBpfLoader: templatize readSectionByName
Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id793381411d559ba338aaad83815e5f429848286

Maciej Żenczykowski   commited 2025-07-16 09:17:47 -07:00
424eb2ad51
vendorBpfLoader: less copies
Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3572cf9dca5caf910d4870788b0ce41a02ba14ad

Maciej Żenczykowski   commited 2025-07-16 09:01:26 -07:00
97810f306d
vendorBpfLoader: trivialize getSectionType()
since we only support skfilter/ we can simplify this

(support for any future other vendor types should be added to the rust loader)

Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I72ad38c056728ef768ee70bc38471c3e9d368f60

Maciej Żenczykowski   commited 2025-07-16 08:55:36 -07:00
a3ecd74594
vendorBpfLoader: remove unused readSectionUint()
Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1a9e8f5e419eb4cde2dd734720d314bcee2dbf9c

Maciej Żenczykowski   commited 2025-07-16 08:51:41 -07:00
b3ad463c44
vendorBpfLoader: remove printing of map/prog ids
Really just spurious logging code.

If you're working on a device (as opposed to looking at bugreports)
you can get this information with for example 'bpftool':

$ adb shell bpftool map show pinned '/sys/fs/bpf/map_bpfRingbufProg_test_ringbuf'
77: ringbuf  name test_ringbuf  flags 0x0
        key 0B  value 0B  max_entries 4096  memlock 16696B

$ adb shell bpftool prog show pinned /sys/fs/bpf/vendor/prog_filterPowerSupplyEvents_skfilter_power_supply
82: socket_filter  name skfilter_power_  tag b8b4693584760391
        loaded_at 2025-07-16T08:44:56-0700  uid 0
        xlated 848B  jited 470B  memlock 4096B

Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie7baf7cf5df9c25ad2d159767584670361e8c6eb

Maciej Żenczykowski   commited 2025-07-16 08:47:49 -07:00
77f8e9e373
vendorBpfLoader: remove mapMatchesExpectations() check
This was primarily meant as a safety check for mainline,
it's not really needed in platform bpf loader.

Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I615f54f10baa75f641129507ecce9159ff31a740

Maciej Żenczykowski   commited 2025-07-16 08:39:23 -07:00
d9201c1612
vendorBpfLoader: change single line, end-of-line /* */ comments to //
Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibc2b32b3a07a1115489b373a97827625fa27ff10

Maciej Żenczykowski   commited 2025-07-16 08:35:43 -07:00
b902e1c254
vendorBpfLoader: kernel is guaranteed to be 5.4+
(even 5.10+)

Test: TreeHugger
Flag: EXEMPT cleanup
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4c9a2a222071095559b1605d4417f93eecf4667f

Maciej Żenczykowski   commited 2025-07-16 08:33:02 -07:00
174cddec91
vendorBpfLoader: move /sys/fs/bpf/vendor creation to rust
We continue to have:
  $ adbz shell ls -lZ /sys/fs/bpf /sys/fs/bpf/vendor
  /sys/fs/bpf:
  total 0
  ...
  drwxrwxrwt 2 root root     u:object_r:fs_bpf_vendor:s0          0 2025-07-16 00:09 vendor

  /sys/fs/bpf/vendor:
  total 0
  -r--r----- 1 root system u:object_r:fs_bpf_vendor:s0  0 2025-07-16 00:09 prog_filterPowerSupplyEvents_skfilter_power_supply

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I74d87a6f382e3b1bc10a8eb82915bc340568f2a8

Maciej Żenczykowski   commited 2025-07-16 00:09:47 -07:00
d29bd0b4ca
vendorBpfLoader: no need for locations array
Afterwards we still have:
  I BpfLoader: Platform BpfLoader loading critical for healthd ELF object /vendor/etc/bpf/filterPowerSupplyEvents.o with license Apache 2.0
  W BpfLoader: No maps section could be found in elf object
  D BpfLoader: prog /sys/fs/bpf/vendor/prog_filterPowerSupplyEvents_skfilter_power_supply id 82

  $ adbz shell ls -lZ /sys/fs/bpf /sys/fs/bpf/vendor
  /sys/fs/bpf:
  total 0
  ...
  drwxrwxrwt 2 root root     u:object_r:fs_bpf_vendor:s0          0 2025-07-15 23:53 vendor

  /sys/fs/bpf/vendor:
  total 0
  -r--r----- 1 root system u:object_r:fs_bpf_vendor:s0  0 2025-07-15 23:53 prog_filterPowerSupplyEvents_skfilter_power_supply

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I66a6fc3abf8f5c4c52f6a47476c251e97f1f6e14

Maciej Żenczykowski   commited 2025-07-15 23:57:34 -07:00
c30b4f8bee
vendorBpfLoader: no need for allowedProgTypes
(while at it remove fuse bpf support, which isn't needed for vendor)

Test: TreeHugger
Flag: EXEMPT no-op
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic7293b94644d933a908a252ab794ae64f88b5865

Maciej Żenczykowski   commited 2025-07-15 23:50:52 -07:00
0e2f1cc28b
simplify libbpf_android.h
Test: TreeHugger
Flag: EXEMPT trivial
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I39f73afef65b22b1d275742758aff7745991d9e7

Maciej Żenczykowski   commited 2025-07-15 23:40:18 -07:00
8b3ea0a274
Give all users access to CPU time in state bpf maps.
We want to read this from traced_probes, which is AID_NOBODY. And
SELinux does a far more thorough job of controlling access than Unix
permissions do.

Bug: 422976190

Test: ls -l, and checked WIP traced_probes implementation.
Change-Id: I51a6486c5d7bf85f922f79a89a7a7fbdb9bc79f2

Simon MacMullen   commited 2025-06-30 08:54:29 -07:00
5781bd4a22
Move CPU time in state bpf maps to their own dir,
This will allow us to use a genfscon SELinux rule to get finer grained access control.

Bug: 422976190

Test: Recorded a Perfetto trace with statsd atom  CPU_CYCLES_PER_UID_CLUSTER, tested manually with go/atomtester.
Change-Id: Ic16da605b9d7150d42176c428f0e1ecaf4a9472f

Simon MacMullen   commited 2025-06-26 16:31:52 +00:00
8546ea34c6
Merge "Use platform bpf_helpers.h and bpf_map_def.h for platform bpfloader" into main
Maciej Żenczykowski   authored and   Android (Google) Code Review   commited 2025-06-18 04:40:48 -07:00
16e017f20e
Merge "bpfloader: refactor C++ loader to just one function" into main
Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-06-17 05:56:59 -07:00
32075d5ef9
bpfloader: refactor C++ loader to just one function
no need for the complexity, as it is only for vendor now

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia807f7ee8aeca25d92e60e46f180ffe536eef87c

Maciej Żenczykowski   commited 2025-06-17 04:18:41 -07:00
d689e6e01a
make platform bpfloader require bpftool cli
Test: TreeHugger
Flag: EXEMPT adds as yet unused cli tool
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib6c2be3c40338743f3482010d7500fef8964adc6

Maciej Żenczykowski   commited 2025-06-17 03:55:14 -07:00
7d62694b8f
bpf: remove /system/etc/bpf from the loader
These directories are not populated on real devices
and are not part of the test suite.
This change removes them from the BPF loader.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I719d0feb9ef1a6f0a1e41265af47916410590ac7

Maciej Żenczykowski   commited 2025-06-16 01:28:45 -07:00
c041c4825b
Use platform bpf_helpers.h and bpf_map_def.h for platform bpfloader
Platform BPF programs rely on platform bpf_helpers.h and bpf_map_def.h.
To ensure consistency and correctness, the loader must use these platform
headers instead of those from p/m/Connectivity.

Bug: 424341374
Test: TH
Change-Id: Id5f1bc8fdd29e54c28a3ec9e01f6a9292f6bb9e1

Motomu Utsumi   commited 2025-06-13 13:04:02 +09:00
1f5cd4b9a1
Load bpfRingbufProg by rust bpfloader
No behavior change is expected.

Before this CL
"""
$ adb shell ls /etc/bpf/bpfRingbufProg.o
/etc/bpf/bpfRingbufProg.o
$ adb shell ls -lZ /sys/fs/bpf/*bpfRingbufProg*
-rw-rw---- 1 root root u:object_r:fs_bpf:s0  0 2025-05-28 12:40 /sys/fs/bpf/map_bpfRingbufProg_test_ringbuf
-r--r----- 1 root root u:object_r:fs_bpf:s0  0 2025-05-28 12:40 /sys/fs/bpf/prog_bpfRingbufProg_skfilter_ringbuf_test
"""

After this CL
"""
$ adb shell ls /etc/bpf/bpfRingbufProg.bpf
/etc/bpf/bpfRingbufProg.bpf
$ adb shell ls -lZ /sys/fs/bpf/*bpfRingbufProg*
-rw-rw---- 1 root root u:object_r:fs_bpf:s0  0 2025-05-28 15:12 /sys/fs/bpf/map_bpfRingbufProg_test_ringbuf
-r--r----- 1 root root u:object_r:fs_bpf:s0  0 2025-05-28 15:12 /sys/fs/bpf/prog_bpfRingbufProg_skfilter_ringbuf_test
"""

Test: Loaded successfully on kernel 5.10, 5.15, 6.1, 6.12
Test: Load is skipped on user build
Bug: 410982483
Change-Id: I17563ea55a34d216c0d3ccd100a19ca52aa4d7ac

Motomu Utsumi   commited 2025-05-30 16:15:21 +09:00
87f696e2c3
Fix the group of gpu_mem_total_map
Test: adb shell ls -lZ /sys/fs/bpf/*gpuMem_*
Bug: 410982483
Change-Id: I4576d219c5a22164c3b7d57a27f58bde272802cf

Motomu Utsumi   commited 2025-05-28 16:19:27 +09:00
bebda53162
Skip loading bpfMemEventsTest on user build
bpfMemEventsTest is installed only on debuggable devices.

Test: TH
Bug: 410982483
Change-Id: Ibf21f0cd639881058580d473dd0aae80bf3c480d

Motomu Utsumi   commited 2025-05-28 15:48:21 +09:00
9f53399e6c
Merge changes I471532a8,Ic1301be9,I076aed22,I156024bf,I2d2f4d09, ... into main
* changes:
  Allow rust bpfloader to load filterPowerSupplyEvents.bpf
  Allow rust bpfloader to load bpfRingbufProg.bpf
  Allow rust bpfloader to load bpfMemEventsTest.bpf
  Allow rust bpfloader to load bpfMemEvents.bpf
  Allow rust bpfloader to load gpuWork.bpf
  Allow rust bpfloader to load gpuMem.bpf

Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-05-26 06:22:24 -07:00
7714bcfe30
Merge "Allow bpfloader to skip loading bpf object on user build" into main
Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-05-26 01:53:21 -07:00
5be3ce428e
Merge "Fix C++23 build: add missing #include." into main
Treehugger Robot   authored and   Android (Google) Code Review   commited 2025-05-25 19:41:11 -07:00
87ff39a16b
Allow rust bpfloader to load filterPowerSupplyEvents.bpf
Test: TH
Bug: 410982483
Change-Id: I471532a80ea872191f1ae880d3c5de2937d289ba

Motomu Utsumi   commited 2025-05-26 10:13:24 +09:00
f89e35a924
Allow rust bpfloader to load bpfRingbufProg.bpf
Test: TH
Bug: 410982483
Change-Id: Ic1301be9a49935f4845d09d70d6b80d215dce5fa

Motomu Utsumi   commited 2025-05-26 10:13:21 +09:00
256af2c856
Allow rust bpfloader to load bpfMemEventsTest.bpf
Test: TH
Bug: 410982483
Change-Id: I076aed2206e47c9432628d4383edf9e64159ee67

Motomu Utsumi   commited 2025-05-23 11:46:25 +09:00
f088c4a66e
Allow rust bpfloader to load bpfMemEvents.bpf
Test: TH
Bug: 410982483
Change-Id: I156024bf130c1a808d657b9b501b64d2ae0d607b

Motomu Utsumi   commited 2025-05-23 11:46:25 +09:00
d8506e9151
Allow rust bpfloader to load gpuWork.bpf
Test: TH
Bug: 410982483
Change-Id: I2d2f4d0995eb8533751c91465afcdc71619375cd

Motomu Utsumi   commited 2025-05-23 11:46:25 +09:00
3023601201
Allow rust bpfloader to load gpuMem.bpf
Test: TH
Bug: 410982483
Change-Id: Ie801f58d8cb338bb99af2010c8a693c13c16579d

Motomu Utsumi   commited 2025-05-23 11:46:25 +09:00
8b9682001f
Allow bpfloader to skip loading bpf object on user build
Test: TH
Bug: 410982483
Change-Id: Ic13708deccefa330ce0c584fb6b0d0151fb872ed

Motomu Utsumi   commited 2025-05-23 11:45:51 +09:00
a1f21dd717
Remove platform C++ bpfloader test
C/C++ platform bpf loader is being replaced by rust version.

This test never had much value, and additional relies on
a sepolicy violation being allowed by su.

The bpf ring buf test is far better.

Test: TreeHugger
Flag: EXEMPT test code removal
Signed-off-by: Maciej Żenczykowski maze@google.com>
Change-Id: I448cc543aa9a0c161515ae2189020bf98db7bca3

Maciej Żenczykowski   commited 2025-05-22 14:10:18 -07:00
d93083a979
Fix C++23 build: add missing #include.
Change-Id: I21ab3f589ba1c9c0df4570264c9a25bc8c2ee427

Elliott Hughes   commited 2025-05-22 15:42:22 -04:00
7d08e8803d
Fix kernel version check for program loading
Before this CL, the kernel version check occurred after all programs
were loaded by `open_file.load()`.

This CL moves the check before `open_file.load()`. It calls
`prog.set_autoload(false)` for programs incompatible with the current
kernel, preventing their auto-load by libbpf.

Test: Verify the program is not loaded if the kver is out of range
Bug: 410982483
Change-Id: Ifacd9dd8696a7bb4fd186ad1faf01fba9f021c6d

Motomu Utsumi   commited 2025-05-21 15:29:27 +09:00
315feea5fc
Fix kernel version check for map loading
Before this CL, the kernel version check occurred after all maps
were loaded by `open_file.load()`.

This CL moves the check before `open_file.load()`. It calls
`map.set_autocreate(false)` for maps incompatible with the current
kernel, preventing their auto-creation by libbpf.

Test: Verify the map is not loaded if the kver is out of range
Bug: 410982483
Change-Id: I7a102dd2ef4b24f87c6efa5a105848fddc08d501

Motomu Utsumi   commited 2025-05-21 15:10:28 +09:00
91e5b0ba66