From d884ba591618f8ece4dea3d470e633e2c2b4e901 Mon Sep 17 00:00:00 2001 From: Liangcai Fan Date: Mon, 6 Sep 2021 17:56:18 +0800 Subject: [PATCH] adds LO_FLAGS_AUTOCLEAR for loop device of zram backing device MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Userdata spaces leak when zram writeback is enable and repeatedly kill system_server(restart android). The reason is that per_boot‘s inode is hold by loop device although per_boot is deleted by user which become orphan inode in system. Adds LO_FLAGS_AUTOCLEAR for loop device of zram backing device, so loop device can release resouce. Bug: 200904398 Change-Id: Ifeee9c0b58b10cdf7698077fbcaf54d5faccc3b1 Signed-off-by: Liangcai Fan Signed-off-by: Hongyu Jin Signed-off-by: Jing Xia --- fs_mgr/fs_mgr.cpp | 3 +++ fs_mgr/libdm/include/libdm/loop_control.h | 3 +++ fs_mgr/libdm/loop_control.cpp | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp index 002b30239..07e1e6b08 100644 --- a/fs_mgr/fs_mgr.cpp +++ b/fs_mgr/fs_mgr.cpp @@ -2119,6 +2119,9 @@ static bool PrepareZramBackingDevice(off64_t size) { PERROR << "Cannot open " << loop_device; return false; } + if (!LoopControl::SetAutoClearStatus(loop_fd.get())) { + PERROR << "Failed set LO_FLAGS_AUTOCLEAR for " << loop_device; + } if (!LoopControl::EnableDirectIo(loop_fd.get())) { return false; } diff --git a/fs_mgr/libdm/include/libdm/loop_control.h b/fs_mgr/libdm/include/libdm/loop_control.h index ad53c11ab..f5190544e 100644 --- a/fs_mgr/libdm/include/libdm/loop_control.h +++ b/fs_mgr/libdm/include/libdm/loop_control.h @@ -46,6 +46,9 @@ class LoopControl final { // Enable Direct I/O on a loop device. This requires kernel 4.9+. static bool EnableDirectIo(int fd); + // Set LO_FLAGS_AUTOCLEAR on a loop device. + static bool SetAutoClearStatus(int fd); + LoopControl(const LoopControl&) = delete; LoopControl& operator=(const LoopControl&) = delete; LoopControl& operator=(LoopControl&&) = default; diff --git a/fs_mgr/libdm/loop_control.cpp b/fs_mgr/libdm/loop_control.cpp index 2e40a18e2..32d5f383e 100644 --- a/fs_mgr/libdm/loop_control.cpp +++ b/fs_mgr/libdm/loop_control.cpp @@ -133,6 +133,16 @@ bool LoopControl::EnableDirectIo(int fd) { return true; } +bool LoopControl::SetAutoClearStatus(int fd) { + struct loop_info64 info = {}; + + info.lo_flags |= LO_FLAGS_AUTOCLEAR; + if (ioctl(fd, LOOP_SET_STATUS64, &info)) { + return false; + } + return true; +} + LoopDevice::LoopDevice(android::base::borrowed_fd fd, const std::chrono::milliseconds& timeout_ms, bool auto_close) : fd_(fd), owned_fd_(-1) {