From 8a3653cfe2732e27cc223eb3506a2b36bd714ef9 Mon Sep 17 00:00:00 2001 From: luwei9 Date: Wed, 14 Apr 2021 14:34:59 +0800 Subject: [PATCH] [Bugfix]Fix userspace-reboot failure when backing_dev exists but zram not swapped on '/sys/block/zram0/backing_dev' will exist even if zram is not swapped on in some devices. And there is no reason to ensure that zram is swapped on if '/sys/block/zram0/backing_dev' exists. So, if we want to kill backing_dev during userspace reboot, we should check if zram is swapped on first. TEST: as follow - adb root - adb shell swapoff /dev/block/zram0 - adb shell echo 1 > /sys/block/zram0/reset - adb shell setprop test.userspace.reboot.flag 1 - adb reboot userspace - (wait reboot ending) adb shell getprop test.userspace.reboot.flag (1 will be show if successful) Signed-off-by: luwei9 Change-Id: Icca569cf8d64bc024b867dae2ab789fc9e76445a --- init/reboot.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/init/reboot.cpp b/init/reboot.cpp index d9acee57f..ab0e48e9e 100644 --- a/init/reboot.cpp +++ b/init/reboot.cpp @@ -450,10 +450,22 @@ static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck, // zram is able to use backing device on top of a loopback device. // In order to unmount /data successfully, we have to kill the loopback device first -#define ZRAM_DEVICE "/dev/block/zram0" -#define ZRAM_RESET "/sys/block/zram0/reset" -#define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" +#define ZRAM_DEVICE "/dev/block/zram0" +#define ZRAM_RESET "/sys/block/zram0/reset" +#define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" +#define ZRAM_INITSTATE "/sys/block/zram0/initstate" static Result KillZramBackingDevice() { + std::string zram_initstate; + if (!android::base::ReadFileToString(ZRAM_INITSTATE, &zram_initstate)) { + return ErrnoError() << "Failed to read " << ZRAM_INITSTATE; + } + + zram_initstate.erase(zram_initstate.length() - 1); + if (zram_initstate == "0") { + LOG(INFO) << "Zram has not been swapped on"; + return {}; + } + if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) { LOG(INFO) << "No zram backing device configured"; return {};