Avoid key_queue_mutex deadlock in waitkey()

Waitkey() is designed to obtain lock "key_queue_mutex" in
the very beginning of function.

  int RecoveryUI::WaitKey() {
    std::unique_lock<std::mutex> lk(key_queue_mutex);
    ...
  }

However, there's case "key_queue_mutex" being applied again in
waitkey(), thus cause deadlock. There are two reproduce
scenario:
1.Executing "fastboot reboot recovery" in userspace
  fastboot
2.Executing "adb reboot fastboot" in recovery os

When entering userspace fastboot/recovery, waitkey()
will wait there for user action. fastboot/adb commands
will trigger ui->interruptkey() to notify the thread
waitkey() in. In the next, waitkey() will move on and call
SetScreenSaveState(), which do LOG(ERROR) in fail case of
brightness set. LOG(ERROR) is designed to print log on
UI. Unfortunately, UI->print() applies lock "key_queue_mutex"
too, so deadlock happen.

Note:
Here is details how lock "key_queue_mutex" applied in
UI->print():
Function Print() call
   Function PrintV() call
      Function update_screen_locked() call
         Function draw_screen_locked() call
            Function draw_menu_and_test_buffer_locked() call
               Function IsLongPress()

  bool RecoveryUI::IsLongPress() {
    std::lock_guard<std::mutex> lg(key_queue_mutex);
    bool result = key_long_press;
    return result;
  }

Bug: 135078366
Test: no errors when running "fastboot reboot recovery" in userspace
      fastboot & "adb reboot fastboot" in recovery os

Change-Id: Ida6b3c4ba9896a70021373f02a94954f0a60cf31
Signed-off-by: Zhang, GaofengX <gaofengx.zhang@intel.com>
Signed-off-by: Xihua Chen <xihua.chen@intel.com>
1 file changed
tree: e470eec0499530bcb3c14e08f762351505a0308f
  1. applypatch/
  2. boot_control/
  3. bootloader_message/
  4. edify/
  5. etc/
  6. fastboot/
  7. fonts/
  8. fuse_sideload/
  9. install/
  10. minadbd/
  11. minui/
  12. otautil/
  13. recovery_ui/
  14. res-hdpi/
  15. res-mdpi/
  16. res-xhdpi/
  17. res-xxhdpi/
  18. res-xxxhdpi/
  19. tests/
  20. tools/
  21. uncrypt/
  22. update_verifier/
  23. updater/
  24. updater_sample/
  25. .clang-format
  26. Android.bp
  27. Android.mk
  28. bootloader.h
  29. CleanSpec.mk
  30. common.h
  31. fsck_unshare_blocks.cpp
  32. fsck_unshare_blocks.h
  33. interlace-frames.py
  34. NOTICE
  35. OWNERS
  36. PREUPLOAD.cfg
  37. README.md
  38. recovery-persist.cpp
  39. recovery-persist.rc
  40. recovery-refresh.cpp
  41. recovery-refresh.rc
  42. recovery.cpp
  43. recovery.h
  44. recovery_main.cpp
  45. TEST_MAPPING
README.md

The Recovery Image

Quick turn-around testing

mm -j && m ramdisk-nodeps && m recoveryimage-nodeps

# To boot into the new recovery image
# without flashing the recovery partition:
adb reboot bootloader
fastboot boot $ANDROID_PRODUCT_OUT/recovery.img

Running the tests

# After setting up environment and lunch.
mmma -j bootable/recovery

# Running the tests on device.
adb root
adb sync data

# 32-bit device
adb shell /data/nativetest/recovery_unit_test/recovery_unit_test
adb shell /data/nativetest/recovery_component_test/recovery_component_test

# Or 64-bit device
adb shell /data/nativetest64/recovery_unit_test/recovery_unit_test
adb shell /data/nativetest64/recovery_component_test/recovery_component_test

Running the manual tests

recovery-refresh and recovery-persist executables exist only on systems without /cache partition. And we need to follow special steps to run tests for them.

  • Execute the test on an A/B device first. The test should fail but it will log some contents to pmsg.

  • Reboot the device immediately and run the test again. The test should save the contents of pmsg buffer into /data/misc/recovery/inject.txt. Test will pass if this file has expected contents.

Using adb under recovery

When running recovery image from debuggable builds (i.e. -eng or -userdebug build variants, or ro.debuggable=1 in /prop.default), adbd service is enabled and started by default, which allows adb communication. A device should be listed under adb devices, either in recovery or sideload state.

$ adb devices
List of devices attached
1234567890abcdef    recovery

Although /system/bin/adbd is built from the same code base as the one in the normal boot, only a subset of adb commands are meaningful under recovery, such as adb root, adb shell, adb push, adb pull etc. Since Android Q, adb shell no longer requires manually mounting /system from recovery menu.

Troubleshooting

adb devices doesn't show the device.

$ adb devices
List of devices attached
  • Ensure adbd is built and running.

By default, adbd is always included into recovery image, as /system/bin/adbd. init starts adbd service automatically only in debuggable builds. This behavior is controlled by the recovery specific /init.rc, whose source code is at bootable/recovery/etc/init.rc.

The best way to confirm a running adbd is by checking the serial output, which shows a service start log as below.

[   18.961986] c1      1 init: starting service 'adbd'...
  • Ensure USB gadget has been enabled.

If adbd service has been started but device not shown under adb devices, use lsusb(8) (on host) to check if the device is visible to the host.

bootable/recovery/etc/init.rc disables Android USB gadget (via sysfs) as part of the fs action trigger, and will only re-enable it in debuggable builds (the on property rule will always run after on fs).

on fs
    write /sys/class/android_usb/android0/enable 0

# Always start adbd on userdebug and eng builds
on property:ro.debuggable=1
    write /sys/class/android_usb/android0/enable 1
    start adbd

If device is using configfs, check if configfs has been properly set up in init rc scripts. See the example configuration for Pixel 2 devices. Note that the flag set via sysfs (i.e. the one above) is no-op when using configfs.

adb devices shows the device, but in unauthorized state.

$ adb devices
List of devices attached
1234567890abcdef    unauthorized

recovery image doesn't honor the USB debugging toggle and the authorizations added under normal boot (because such authorization data stays in /data, which recovery doesn't mount), nor does it support authorizing a host device under recovery. We can use one of the following options instead.

  • Option 1 (Recommended): Authorize a host device with adb vendor keys.

For debuggable builds, an RSA keypair can be used to authorize a host device that has the private key. The public key, defined via PRODUCT_ADB_KEYS, will be copied to /adb_keys. When starting the host-side adbd, make sure the filename (or the directory) of the matching private key has been added to $ADB_VENDOR_KEYS.

$ export ADB_VENDOR_KEYS=/path/to/adb/private/key
$ adb kill-server
$ adb devices

-user builds filter out PRODUCT_ADB_KEYS, so no /adb_keys will be included there.

Note that this mechanism applies to both of normal boot and recovery modes.

  • Option 2: Allow adbd to connect without authentication.
    • adbd is compiled with ALLOW_ADBD_NO_AUTH (only on debuggable builds).
    • ro.adb.secure has a value of 0.

Both of the two conditions need to be satisfied. Although ro.adb.secure is a runtime property, its value is set at build time (written into /prop.default). It defaults to 1 on -user builds, and 0 for other build variants. The value is overridable via PRODUCT_DEFAULT_PROPERTY_OVERRIDES.