ueventd: duplicate /dev/ashmem
We want ashmem to only be used via libcutils API, with long-term goal
being deprecation of ashmem with memfd. To do that we route libcutils to
a new source of ashmem fds. We then phase out uses of /dev/ashmem that
doesn't go through libcutils using SELinux.
In Q, we introduced ashmemd as the source of ashmem fds to libcutils.
However, having a separate process and, consequently, binder hops to
handle /dev/ashmem results in performance/memory overhead.
To address the overhead, replace ashmemd with a duplicate of
/dev/ashmem. Name it /dev/ashmem<boot_id>, where boot_id is a random
number generated on each boot. This way we make sure that developers
don't accidentally depend on /dev/ashmem<boot_id>, as that name can't be
hardcoded.
Bug: 139855428
Test: writing "add"/"remove" to /sys/class/misc/ashmem/uevent correctly
adds/removes /dev/ashmem and /dev/ashmem/boot_id
Change-Id: I36d23116048bfcd99903ba46cc133161835a2cfa
diff --git a/init/devices.cpp b/init/devices.cpp
index f6e453a..9fbec64 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -441,6 +441,23 @@
}
}
+void DeviceHandler::HandleAshmemUevent(const Uevent& uevent) {
+ if (uevent.device_name == "ashmem") {
+ static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
+ std::string boot_id;
+ if (!ReadFileToString(boot_id_path, &boot_id)) {
+ PLOG(ERROR) << "Cannot duplicate ashmem device node. Failed to read " << boot_id_path;
+ return;
+ };
+ boot_id = Trim(boot_id);
+
+ Uevent dup_ashmem_uevent = uevent;
+ dup_ashmem_uevent.device_name += boot_id;
+ dup_ashmem_uevent.path += boot_id;
+ HandleUevent(dup_ashmem_uevent);
+ }
+}
+
void DeviceHandler::HandleUevent(const Uevent& uevent) {
if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
FixupSysPermissions(uevent.path, uevent.subsystem);
@@ -485,6 +502,10 @@
mkdir_recursive(Dirname(devpath), 0755);
HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
+
+ // Duplicate /dev/ashmem device and name it /dev/ashmem<boot_id>.
+ // TODO(b/111903542): remove once all users of /dev/ashmem are migrated to libcutils API.
+ HandleAshmemUevent(uevent);
}
void DeviceHandler::ColdbootDone() {