Enable zram swap

It fixes a kernel panic on DB845c and RB5 while running
AOSP/external/stressapptest due to "System is deadlocked
on memory".

It also fixes a similar crash reported on RB5 while running CtsBionicTestCases, https://bugs.linaro.org/show_bug.cgi?id=6036,
while we are still diagnosing that crash for a proper fix.

Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
Change-Id: I81af210a50526050c00b65f55e85cf360fc39622
diff --git a/device-common.mk b/device-common.mk
index a37f726..a5a2f14 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -86,6 +86,9 @@
       device/linaro/dragonboard/fstab.common:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.$(TARGET_HARDWARE)
 endif
 
+PRODUCT_VENDOR_PROPERTIES += \
+    persist.sys.zram_enabled=1
+
 PRODUCT_COPY_FILES += \
     $(TARGET_KERNEL_DIR)/Image.gz:kernel \
     device/linaro/dragonboard/init.common.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.$(TARGET_HARDWARE).rc \
diff --git a/fstab.common b/fstab.common
index cde33c2..c618b7a 100644
--- a/fstab.common
+++ b/fstab.common
@@ -11,3 +11,4 @@
 product		/product	erofs	ro				wait,logical,first_stage_mount,slotselect
 system_dlkm	/system_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
 vendor_dlkm	/vendor_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
+/dev/block/zram0					none	swap	defaults	zramsize=75%
diff --git a/init.common.rc b/init.common.rc
index 51a42c6..42a582f 100644
--- a/init.common.rc
+++ b/init.common.rc
@@ -16,6 +16,9 @@
     mount_all /vendor/etc/fstab.${ro.hardware} --late
 
 on init
+    # ZRAM setup
+    write /sys/block/zram0/comp_algorithm lz4
+
     # For legacy support
     # See storage config details at http://source.android.com/tech/storage/
     # since /storage is mounted on post-fs in init.rc
@@ -59,6 +62,15 @@
     # little cores, not on bigs to be used only by init
     write /dev/cpuset/system-background/cpus 0-3
 
+    trigger sys-boot-completed-set
+
+# We want one opportunity per boot to enable zram, so we
+# use a trigger we fire from the above stanza. If
+# persist.sys.zram_enabled becomes true after boot,
+# we don't want to run swapon_all at that time.
+on sys-boot-completed-set && property:persist.sys.zram_enabled=1
+    swapon_all
+
 service wpa_supplicant /vendor/bin/hw/wpa_supplicant \
                        -Dnl80211 -g@android:wpa_wlan0
     interface aidl android.hardware.wifi.supplicant.ISupplicant/default
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 82370f6..660539c 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -5,6 +5,7 @@
 /dev/block/platform/soc@0/1d84000\.ufshc/by-name/super		u:object_r:super_block_device:s0
 /dev/block/platform/soc@0/1d84000\.ufshc/by-name/userdata	u:object_r:userdata_block_device:s0
 /dev/block/platform/soc@0/1d84000\.ufshc/by-name/misc		u:object_r:misc_block_device:s0
+/dev/block/zram0						u:object_r:swap_block_device:s0
 
 /dev/dri				u:object_r:dri_device:s0
 /dev/dri/card0				u:object_r:graphics_device:s0