2 * Copyright (C) 2016 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
21 static bool msm_gem_shrinker_lock(struct drm_device
*dev
, bool *unlock
)
23 /* NOTE: we are *closer* to being able to get rid of
24 * mutex_trylock_recursive().. the msm_gem code itself does
25 * not need struct_mutex, although codepaths that can trigger
26 * shrinker are still called in code-paths that hold the
29 * Also, msm_obj->madv is protected by struct_mutex.
31 * The next step is probably split out a seperate lock for
32 * protecting inactive_list, so that shrinker does not need
35 switch (mutex_trylock_recursive(&dev
->struct_mutex
)) {
36 case MUTEX_TRYLOCK_FAILED
:
39 case MUTEX_TRYLOCK_SUCCESS
:
43 case MUTEX_TRYLOCK_RECURSIVE
:
52 msm_gem_shrinker_count(struct shrinker
*shrinker
, struct shrink_control
*sc
)
54 struct msm_drm_private
*priv
=
55 container_of(shrinker
, struct msm_drm_private
, shrinker
);
56 struct drm_device
*dev
= priv
->dev
;
57 struct msm_gem_object
*msm_obj
;
58 unsigned long count
= 0;
61 if (!msm_gem_shrinker_lock(dev
, &unlock
))
64 list_for_each_entry(msm_obj
, &priv
->inactive_list
, mm_list
) {
65 if (is_purgeable(msm_obj
))
66 count
+= msm_obj
->base
.size
>> PAGE_SHIFT
;
70 mutex_unlock(&dev
->struct_mutex
);
76 msm_gem_shrinker_scan(struct shrinker
*shrinker
, struct shrink_control
*sc
)
78 struct msm_drm_private
*priv
=
79 container_of(shrinker
, struct msm_drm_private
, shrinker
);
80 struct drm_device
*dev
= priv
->dev
;
81 struct msm_gem_object
*msm_obj
;
82 unsigned long freed
= 0;
85 if (!msm_gem_shrinker_lock(dev
, &unlock
))
88 list_for_each_entry(msm_obj
, &priv
->inactive_list
, mm_list
) {
89 if (freed
>= sc
->nr_to_scan
)
91 if (is_purgeable(msm_obj
)) {
92 msm_gem_purge(&msm_obj
->base
, OBJ_LOCK_SHRINKER
);
93 freed
+= msm_obj
->base
.size
>> PAGE_SHIFT
;
98 mutex_unlock(&dev
->struct_mutex
);
101 pr_info_ratelimited("Purging %lu bytes\n", freed
<< PAGE_SHIFT
);
107 msm_gem_shrinker_vmap(struct notifier_block
*nb
, unsigned long event
, void *ptr
)
109 struct msm_drm_private
*priv
=
110 container_of(nb
, struct msm_drm_private
, vmap_notifier
);
111 struct drm_device
*dev
= priv
->dev
;
112 struct msm_gem_object
*msm_obj
;
113 unsigned unmapped
= 0;
116 if (!msm_gem_shrinker_lock(dev
, &unlock
))
119 list_for_each_entry(msm_obj
, &priv
->inactive_list
, mm_list
) {
120 if (is_vunmapable(msm_obj
)) {
121 msm_gem_vunmap(&msm_obj
->base
, OBJ_LOCK_SHRINKER
);
122 /* since we don't know any better, lets bail after a few
123 * and if necessary the shrinker will be invoked again.
124 * Seems better than unmapping *everything*
126 if (++unmapped
>= 15)
132 mutex_unlock(&dev
->struct_mutex
);
134 *(unsigned long *)ptr
+= unmapped
;
137 pr_info_ratelimited("Purging %u vmaps\n", unmapped
);
143 * msm_gem_shrinker_init - Initialize msm shrinker
144 * @dev_priv: msm device
146 * This function registers and sets up the msm shrinker.
148 void msm_gem_shrinker_init(struct drm_device
*dev
)
150 struct msm_drm_private
*priv
= dev
->dev_private
;
151 priv
->shrinker
.count_objects
= msm_gem_shrinker_count
;
152 priv
->shrinker
.scan_objects
= msm_gem_shrinker_scan
;
153 priv
->shrinker
.seeks
= DEFAULT_SEEKS
;
154 WARN_ON(register_shrinker(&priv
->shrinker
));
156 priv
->vmap_notifier
.notifier_call
= msm_gem_shrinker_vmap
;
157 WARN_ON(register_vmap_purge_notifier(&priv
->vmap_notifier
));
161 * msm_gem_shrinker_cleanup - Clean up msm shrinker
162 * @dev_priv: msm device
164 * This function unregisters the msm shrinker.
166 void msm_gem_shrinker_cleanup(struct drm_device
*dev
)
168 struct msm_drm_private
*priv
= dev
->dev_private
;
170 if (priv
->shrinker
.nr_deferred
) {
171 WARN_ON(unregister_vmap_purge_notifier(&priv
->vmap_notifier
));
172 unregister_shrinker(&priv
->shrinker
);