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