UPSTREAM: kasan: avoid overflowing quarantine size on low memory systems
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / mm / kasan / quarantine.c
index 7fd121d13b88af02db56b968a7fec5828bdc53a3..b6728a33a4aca104fde8022b90fdf2df5630af31 100644 (file)
@@ -198,7 +198,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-       size_t new_quarantine_size;
+       size_t new_quarantine_size, percpu_quarantines;
        unsigned long flags;
        struct qlist_head to_free = QLIST_INIT;
        size_t size_to_free = 0;
@@ -216,7 +216,12 @@ void quarantine_reduce(void)
         */
        new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
                QUARANTINE_FRACTION;
-       new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+       percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+       if (WARN_ONCE(new_quarantine_size < percpu_quarantines,
+               "Too little memory, disabling global KASAN quarantine.\n"))
+               new_quarantine_size = 0;
+       else
+               new_quarantine_size -= percpu_quarantines;
        WRITE_ONCE(quarantine_size, new_quarantine_size);
 
        last = global_quarantine.head;