^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Deferred dmabuf freeing helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2020 Linaro, Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on the ION page pool code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2011 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "deferred-free-helper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static LIST_HEAD(free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static size_t list_nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) wait_queue_head_t freelist_waitqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct task_struct *freelist_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static DEFINE_SPINLOCK(free_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void deferred_free(struct deferred_freelist_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void (*free)(struct deferred_freelist_item*,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum df_reason),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) size_t nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) INIT_LIST_HEAD(&item->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) item->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) item->free = free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) spin_lock_irqsave(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) list_add(&item->list, &free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) list_nr_pages += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) spin_unlock_irqrestore(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) wake_up(&freelist_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) EXPORT_SYMBOL_GPL(deferred_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static size_t free_one_item(enum df_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) size_t nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct deferred_freelist_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) spin_lock_irqsave(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (list_empty(&free_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) spin_unlock_irqrestore(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) item = list_first_entry(&free_list, struct deferred_freelist_item, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) list_del(&item->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) nr_pages = item->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) list_nr_pages -= nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) spin_unlock_irqrestore(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) item->free(item, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long get_freelist_nr_pages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) spin_lock_irqsave(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) nr_pages = list_nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spin_unlock_irqrestore(&free_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) EXPORT_SYMBOL_GPL(get_freelist_nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static unsigned long freelist_shrink_count(struct shrinker *shrinker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct shrink_control *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return get_freelist_nr_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static unsigned long freelist_shrink_scan(struct shrinker *shrinker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct shrink_control *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned long total_freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (sc->nr_to_scan == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) while (total_freed < sc->nr_to_scan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) size_t pages_freed = free_one_item(DF_UNDER_PRESSURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!pages_freed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) total_freed += pages_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return total_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct shrinker freelist_shrinker = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .count_objects = freelist_shrink_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .scan_objects = freelist_shrink_scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .seeks = DEFAULT_SEEKS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .batch = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int deferred_free_thread(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) wait_event_freezable(freelist_waitqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) get_freelist_nr_pages() > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) free_one_item(DF_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int deferred_freelist_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) list_nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) init_waitqueue_head(&freelist_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) freelist_task = kthread_run(deferred_free_thread, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "%s", "dmabuf-deferred-free-worker");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (IS_ERR(freelist_task)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) pr_err("Creating thread for deferred free failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sched_set_normal(freelist_task, 19);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return register_shrinker(&freelist_shrinker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) module_init(deferred_freelist_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)