Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/log2.h> |
| 25 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 26 | #include <linux/sched/mm.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 27 | #include <linux/slab.h> |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 28 | #include <linux/amd-iommu.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 29 | #include <linux/notifier.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 30 | #include <linux/compat.h> |
| 31 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 32 | struct mm_struct; |
| 33 | |
| 34 | #include "kfd_priv.h" |
Ben Goz | c3447e8 | 2015-05-20 18:05:44 +0300 | [diff] [blame] | 35 | #include "kfd_dbgmgr.h" |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Initial size for the array of queues. |
| 39 | * The allocated size is doubled each time |
| 40 | * it is exceeded up to MAX_PROCESS_QUEUES. |
| 41 | */ |
| 42 | #define INITIAL_QUEUE_ARRAY_SIZE 16 |
| 43 | |
| 44 | /* |
| 45 | * List of struct kfd_process (field kfd_process). |
| 46 | * Unique/indexed by mm_struct* |
| 47 | */ |
| 48 | #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ |
| 49 | static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); |
| 50 | static DEFINE_MUTEX(kfd_processes_mutex); |
| 51 | |
| 52 | DEFINE_STATIC_SRCU(kfd_processes_srcu); |
| 53 | |
| 54 | static struct workqueue_struct *kfd_process_wq; |
| 55 | |
| 56 | struct kfd_process_release_work { |
| 57 | struct work_struct kfd_work; |
| 58 | struct kfd_process *p; |
| 59 | }; |
| 60 | |
| 61 | static struct kfd_process *find_process(const struct task_struct *thread); |
| 62 | static struct kfd_process *create_process(const struct task_struct *thread); |
| 63 | |
| 64 | void kfd_process_create_wq(void) |
| 65 | { |
| 66 | if (!kfd_process_wq) |
Bhaktipriya Shridhar | fd320bf | 2016-05-29 21:14:11 +0530 | [diff] [blame] | 67 | kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void kfd_process_destroy_wq(void) |
| 71 | { |
| 72 | if (kfd_process_wq) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 73 | destroy_workqueue(kfd_process_wq); |
| 74 | kfd_process_wq = NULL; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | struct kfd_process *kfd_create_process(const struct task_struct *thread) |
| 79 | { |
| 80 | struct kfd_process *process; |
| 81 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 82 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 83 | return ERR_PTR(-EINVAL); |
| 84 | |
| 85 | /* Only the pthreads threading model is supported. */ |
| 86 | if (thread->group_leader->mm != thread->mm) |
| 87 | return ERR_PTR(-EINVAL); |
| 88 | |
| 89 | /* Take mmap_sem because we call __mmu_notifier_register inside */ |
| 90 | down_write(&thread->mm->mmap_sem); |
| 91 | |
| 92 | /* |
| 93 | * take kfd processes mutex before starting of process creation |
| 94 | * so there won't be a case where two threads of the same process |
| 95 | * create two kfd_process structures |
| 96 | */ |
| 97 | mutex_lock(&kfd_processes_mutex); |
| 98 | |
| 99 | /* A prior open of /dev/kfd could have already created the process. */ |
| 100 | process = find_process(thread); |
| 101 | if (process) |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 102 | pr_debug("Process already found\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 103 | |
| 104 | if (!process) |
| 105 | process = create_process(thread); |
| 106 | |
| 107 | mutex_unlock(&kfd_processes_mutex); |
| 108 | |
| 109 | up_write(&thread->mm->mmap_sem); |
| 110 | |
| 111 | return process; |
| 112 | } |
| 113 | |
| 114 | struct kfd_process *kfd_get_process(const struct task_struct *thread) |
| 115 | { |
| 116 | struct kfd_process *process; |
| 117 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 118 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 119 | return ERR_PTR(-EINVAL); |
| 120 | |
| 121 | /* Only the pthreads threading model is supported. */ |
| 122 | if (thread->group_leader->mm != thread->mm) |
| 123 | return ERR_PTR(-EINVAL); |
| 124 | |
| 125 | process = find_process(thread); |
| 126 | |
| 127 | return process; |
| 128 | } |
| 129 | |
| 130 | static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) |
| 131 | { |
| 132 | struct kfd_process *process; |
| 133 | |
| 134 | hash_for_each_possible_rcu(kfd_processes_table, process, |
| 135 | kfd_processes, (uintptr_t)mm) |
| 136 | if (process->mm == mm) |
| 137 | return process; |
| 138 | |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | static struct kfd_process *find_process(const struct task_struct *thread) |
| 143 | { |
| 144 | struct kfd_process *p; |
| 145 | int idx; |
| 146 | |
| 147 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 148 | p = find_process_by_mm(thread->mm); |
| 149 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 150 | |
| 151 | return p; |
| 152 | } |
| 153 | |
| 154 | static void kfd_process_wq_release(struct work_struct *work) |
| 155 | { |
| 156 | struct kfd_process_release_work *my_work; |
| 157 | struct kfd_process_device *pdd, *temp; |
| 158 | struct kfd_process *p; |
| 159 | |
| 160 | my_work = (struct kfd_process_release_work *) work; |
| 161 | |
| 162 | p = my_work->p; |
| 163 | |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 164 | pr_debug("Releasing process (pasid %d) in workqueue\n", |
| 165 | p->pasid); |
| 166 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 167 | mutex_lock(&p->mutex); |
| 168 | |
| 169 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
| 170 | per_device_list) { |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 171 | pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n", |
| 172 | pdd->dev->id, p->pasid); |
| 173 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 174 | if (pdd->bound == PDD_BOUND) |
| 175 | amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 176 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 177 | list_del(&pdd->per_device_list); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 178 | kfree(pdd); |
| 179 | } |
| 180 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 181 | kfd_event_free_process(p); |
| 182 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 183 | kfd_pasid_free(p->pasid); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 184 | kfd_free_process_doorbells(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 185 | |
| 186 | mutex_unlock(&p->mutex); |
| 187 | |
| 188 | mutex_destroy(&p->mutex); |
| 189 | |
| 190 | kfree(p->queues); |
| 191 | |
| 192 | kfree(p); |
| 193 | |
Amitoj Kaur Chawla | 642f0f2 | 2016-01-25 23:03:57 +0530 | [diff] [blame] | 194 | kfree(work); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void kfd_process_destroy_delayed(struct rcu_head *rcu) |
| 198 | { |
| 199 | struct kfd_process_release_work *work; |
| 200 | struct kfd_process *p; |
| 201 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 202 | p = container_of(rcu, struct kfd_process, rcu); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 203 | WARN_ON(atomic_read(&p->mm->mm_count) <= 0); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 204 | |
| 205 | mmdrop(p->mm); |
| 206 | |
Firo Yang | 1549fcd | 2015-04-23 17:58:05 +0800 | [diff] [blame] | 207 | work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 208 | |
| 209 | if (work) { |
| 210 | INIT_WORK((struct work_struct *) work, kfd_process_wq_release); |
| 211 | work->p = p; |
| 212 | queue_work(kfd_process_wq, (struct work_struct *) work); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void kfd_process_notifier_release(struct mmu_notifier *mn, |
| 217 | struct mm_struct *mm) |
| 218 | { |
| 219 | struct kfd_process *p; |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 220 | struct kfd_process_device *pdd = NULL; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * The kfd_process structure can not be free because the |
| 224 | * mmu_notifier srcu is read locked |
| 225 | */ |
| 226 | p = container_of(mn, struct kfd_process, mmu_notifier); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 227 | if (WARN_ON(p->mm != mm)) |
| 228 | return; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 229 | |
| 230 | mutex_lock(&kfd_processes_mutex); |
| 231 | hash_del_rcu(&p->kfd_processes); |
| 232 | mutex_unlock(&kfd_processes_mutex); |
| 233 | synchronize_srcu(&kfd_processes_srcu); |
| 234 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 235 | mutex_lock(&p->mutex); |
| 236 | |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 237 | kfd_process_dequeue_from_all_devices(p); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 238 | pqm_uninit(&p->pqm); |
| 239 | |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 240 | /* Iterate over all process device data structure and check |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 241 | * if we should delete debug managers |
Oded Gabbay | bc4755a | 2016-05-26 08:41:48 +0300 | [diff] [blame] | 242 | */ |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 243 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
Oded Gabbay | bc4755a | 2016-05-26 08:41:48 +0300 | [diff] [blame] | 244 | if ((pdd->dev->dbgmgr) && |
| 245 | (pdd->dev->dbgmgr->pasid == p->pasid)) |
| 246 | kfd_dbgmgr_destroy(pdd->dev->dbgmgr); |
| 247 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 248 | mutex_unlock(&p->mutex); |
| 249 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 250 | /* |
| 251 | * Because we drop mm_count inside kfd_process_destroy_delayed |
| 252 | * and because the mmu_notifier_unregister function also drop |
| 253 | * mm_count we need to take an extra count here. |
| 254 | */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 255 | mmgrab(p->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 256 | mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm); |
| 257 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 258 | } |
| 259 | |
| 260 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 261 | .release = kfd_process_notifier_release, |
| 262 | }; |
| 263 | |
| 264 | static struct kfd_process *create_process(const struct task_struct *thread) |
| 265 | { |
| 266 | struct kfd_process *process; |
| 267 | int err = -ENOMEM; |
| 268 | |
| 269 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 270 | |
| 271 | if (!process) |
| 272 | goto err_alloc_process; |
| 273 | |
| 274 | process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE, |
| 275 | sizeof(process->queues[0]), GFP_KERNEL); |
| 276 | if (!process->queues) |
| 277 | goto err_alloc_queues; |
| 278 | |
| 279 | process->pasid = kfd_pasid_alloc(); |
| 280 | if (process->pasid == 0) |
| 281 | goto err_alloc_pasid; |
| 282 | |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 283 | if (kfd_alloc_process_doorbells(process) < 0) |
| 284 | goto err_alloc_doorbells; |
| 285 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 286 | mutex_init(&process->mutex); |
| 287 | |
| 288 | process->mm = thread->mm; |
| 289 | |
| 290 | /* register notifier */ |
| 291 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
| 292 | err = __mmu_notifier_register(&process->mmu_notifier, process->mm); |
| 293 | if (err) |
| 294 | goto err_mmu_notifier; |
| 295 | |
| 296 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 297 | (uintptr_t)process->mm); |
| 298 | |
| 299 | process->lead_thread = thread->group_leader; |
| 300 | |
| 301 | process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE; |
| 302 | |
| 303 | INIT_LIST_HEAD(&process->per_device_data); |
| 304 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 305 | kfd_event_init_process(process); |
| 306 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 307 | err = pqm_init(&process->pqm, process); |
| 308 | if (err != 0) |
| 309 | goto err_process_pqm_init; |
| 310 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 311 | /* init process apertures*/ |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 312 | process->is_32bit_user_mode = in_compat_syscall(); |
Dan Carpenter | b312b2b | 2017-06-14 13:58:53 +0300 | [diff] [blame] | 313 | err = kfd_init_apertures(process); |
| 314 | if (err != 0) |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 315 | goto err_init_apertures; |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 316 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 317 | return process; |
| 318 | |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 319 | err_init_apertures: |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 320 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 321 | err_process_pqm_init: |
| 322 | hash_del_rcu(&process->kfd_processes); |
| 323 | synchronize_rcu(); |
| 324 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 325 | err_mmu_notifier: |
Oded Gabbay | 7fd5e03 | 2016-06-23 17:54:29 +0300 | [diff] [blame] | 326 | mutex_destroy(&process->mutex); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 327 | kfd_free_process_doorbells(process); |
| 328 | err_alloc_doorbells: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 329 | kfd_pasid_free(process->pasid); |
| 330 | err_alloc_pasid: |
| 331 | kfree(process->queues); |
| 332 | err_alloc_queues: |
| 333 | kfree(process); |
| 334 | err_alloc_process: |
| 335 | return ERR_PTR(err); |
| 336 | } |
| 337 | |
| 338 | struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 339 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 340 | { |
| 341 | struct kfd_process_device *pdd = NULL; |
| 342 | |
| 343 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 344 | if (pdd->dev == dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 345 | return pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 346 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 347 | return NULL; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 351 | struct kfd_process *p) |
| 352 | { |
| 353 | struct kfd_process_device *pdd = NULL; |
| 354 | |
| 355 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
| 356 | if (pdd != NULL) { |
| 357 | pdd->dev = dev; |
| 358 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 359 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 360 | pdd->qpd.dqm = dev->dqm; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 361 | pdd->process = p; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 362 | pdd->bound = PDD_UNBOUND; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 363 | pdd->already_dequeued = false; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 364 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | return pdd; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 372 | * to the device. |
| 373 | * Unbinding occurs when the process dies or the device is removed. |
| 374 | * |
| 375 | * Assumes that the process lock is held. |
| 376 | */ |
| 377 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 378 | struct kfd_process *p) |
| 379 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 380 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 381 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 382 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 383 | pdd = kfd_get_process_device_data(dev, p); |
| 384 | if (!pdd) { |
| 385 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 386 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 387 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 388 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 389 | if (pdd->bound == PDD_BOUND) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 390 | return pdd; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 391 | } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) { |
| 392 | pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n"); |
| 393 | return ERR_PTR(-EINVAL); |
| 394 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 395 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 396 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread); |
| 397 | if (err < 0) |
| 398 | return ERR_PTR(err); |
| 399 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 400 | pdd->bound = PDD_BOUND; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 401 | |
| 402 | return pdd; |
| 403 | } |
| 404 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 405 | /* |
| 406 | * Bind processes do the device that have been temporarily unbound |
| 407 | * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device. |
| 408 | */ |
| 409 | int kfd_bind_processes_to_device(struct kfd_dev *dev) |
| 410 | { |
| 411 | struct kfd_process_device *pdd; |
| 412 | struct kfd_process *p; |
| 413 | unsigned int temp; |
| 414 | int err = 0; |
| 415 | |
| 416 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 417 | |
| 418 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 419 | mutex_lock(&p->mutex); |
| 420 | pdd = kfd_get_process_device_data(dev, p); |
| 421 | if (pdd->bound != PDD_BOUND_SUSPENDED) { |
| 422 | mutex_unlock(&p->mutex); |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, |
| 427 | p->lead_thread); |
| 428 | if (err < 0) { |
| 429 | pr_err("unexpected pasid %d binding failure\n", |
| 430 | p->pasid); |
| 431 | mutex_unlock(&p->mutex); |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | pdd->bound = PDD_BOUND; |
| 436 | mutex_unlock(&p->mutex); |
| 437 | } |
| 438 | |
| 439 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 440 | |
| 441 | return err; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Temporarily unbind currently bound processes from the device and |
| 446 | * mark them as PDD_BOUND_SUSPENDED. These processes will be restored |
| 447 | * to PDD_BOUND state in kfd_bind_processes_to_device. |
| 448 | */ |
| 449 | void kfd_unbind_processes_from_device(struct kfd_dev *dev) |
| 450 | { |
| 451 | struct kfd_process_device *pdd; |
| 452 | struct kfd_process *p; |
| 453 | unsigned int temp, temp_bound, temp_pasid; |
| 454 | |
| 455 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 456 | |
| 457 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 458 | mutex_lock(&p->mutex); |
| 459 | pdd = kfd_get_process_device_data(dev, p); |
| 460 | temp_bound = pdd->bound; |
| 461 | temp_pasid = p->pasid; |
| 462 | if (pdd->bound == PDD_BOUND) |
| 463 | pdd->bound = PDD_BOUND_SUSPENDED; |
| 464 | mutex_unlock(&p->mutex); |
| 465 | |
| 466 | if (temp_bound == PDD_BOUND) |
| 467 | amd_iommu_unbind_pasid(dev->pdev, temp_pasid); |
| 468 | } |
| 469 | |
| 470 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 471 | } |
| 472 | |
| 473 | void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 474 | { |
| 475 | struct kfd_process *p; |
| 476 | struct kfd_process_device *pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 477 | |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 478 | /* |
| 479 | * Look for the process that matches the pasid. If there is no such |
| 480 | * process, we either released it in amdkfd's own notifier, or there |
| 481 | * is a bug. Unfortunately, there is no way to tell... |
| 482 | */ |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 483 | p = kfd_lookup_process_by_pasid(pasid); |
| 484 | if (!p) |
| 485 | return; |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 486 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 487 | pr_debug("Unbinding process %d from IOMMU\n", pasid); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 488 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 489 | if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid)) |
| 490 | kfd_dbgmgr_destroy(dev->dbgmgr); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 491 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 492 | pdd = kfd_get_process_device_data(dev, p); |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame^] | 493 | if (pdd) |
| 494 | /* For GPU relying on IOMMU, we need to dequeue here |
| 495 | * when PASID is still bound. |
| 496 | */ |
| 497 | kfd_process_dequeue_from_device(pdd); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 498 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 499 | mutex_unlock(&p->mutex); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 500 | } |
| 501 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 502 | struct kfd_process_device *kfd_get_first_process_device_data( |
| 503 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 504 | { |
| 505 | return list_first_entry(&p->per_device_data, |
| 506 | struct kfd_process_device, |
| 507 | per_device_list); |
| 508 | } |
| 509 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 510 | struct kfd_process_device *kfd_get_next_process_device_data( |
| 511 | struct kfd_process *p, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 512 | struct kfd_process_device *pdd) |
| 513 | { |
| 514 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 515 | return NULL; |
| 516 | return list_next_entry(pdd, per_device_list); |
| 517 | } |
| 518 | |
| 519 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 520 | { |
| 521 | return !(list_empty(&p->per_device_data)); |
| 522 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 523 | |
| 524 | /* This returns with process->mutex locked. */ |
| 525 | struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) |
| 526 | { |
| 527 | struct kfd_process *p; |
| 528 | unsigned int temp; |
| 529 | |
| 530 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 531 | |
| 532 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 533 | if (p->pasid == pasid) { |
| 534 | mutex_lock(&p->mutex); |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 540 | |
| 541 | return p; |
| 542 | } |