lmkd: Add a margin for watermark when swap free is low

When swap is depleted file cache thrashing might result in allocations
quickly pushing memory below low watermark and kswapd quickly pushing
it back above high watermark. In this situation free memory stays above
high watermark most of the time and lmkd during its periodic wake-ups
has low chance of detecting low memory conditions. Add a 15% margin
for high watermark which would allow lmkd to kill if swap is low,some
memory was reclaimed since the last wakeup and free memory is just
above the high watermark limit.

Bug: 163134367
Test: heavy loading launch
Signed-off-by: Martin Liu <liumartin@google.com>
Change-Id: I5694736b04bafcd13c01f4b51e242e2ac4ff55a8
(cherry picked from commit 95551f816ae87fac700f97c7e6d1bf3302f2b85b)
diff --git a/lmkd.cpp b/lmkd.cpp
index a2820b8..b7eb18f 100644
--- a/lmkd.cpp
+++ b/lmkd.cpp
@@ -2239,9 +2239,11 @@
  * Returns lowest breached watermark or WMARK_NONE.
  */
 static enum zone_watermark get_lowest_watermark(union meminfo *mi,
-                                                struct zone_watermarks *watermarks)
+                                                struct zone_watermarks *watermarks,
+                                                long margin)
 {
     int64_t nr_free_pages = mi->field.nr_free_pages - mi->field.cma_free;
+    int64_t high_wmark = (watermarks->high_wmark * margin) / 100;
 
     if (nr_free_pages < watermarks->min_wmark) {
         return WMARK_MIN;
@@ -2249,7 +2251,7 @@
     if (nr_free_pages < watermarks->low_wmark) {
         return WMARK_LOW;
     }
-    if (nr_free_pages < watermarks->high_wmark) {
+    if (nr_free_pages < high_wmark) {
         return WMARK_HIGH;
     }
     return WMARK_NONE;
@@ -2406,7 +2408,7 @@
      }
 
     /* Find out which watermark is breached if any */
-    wmark = get_lowest_watermark(&mi, &watermarks);
+    wmark = get_lowest_watermark(&mi, &watermarks, swap_is_low ? 115 : 100);
 
     /*
      * TODO: move this logic into a separate function