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>
26 #include <linux/slab.h>
27 #include <linux/amd-iommu.h>
28 #include <linux/notifier.h>
29 #include <linux/compat.h>
30
31 struct mm_struct;
32
33 #include "kfd_priv.h"
34 #include "kfd_dbgmgr.h"
35
36 /*
37 * Initial size for the array of queues.
38 * The allocated size is doubled each time
39 * it is exceeded up to MAX_PROCESS_QUEUES.
40 */
41 #define INITIAL_QUEUE_ARRAY_SIZE 16
42
43 /*
44 * List of struct kfd_process (field kfd_process).
45 * Unique/indexed by mm_struct*
46 */
47 #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
48 static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
49 static DEFINE_MUTEX(kfd_processes_mutex);
50
51 DEFINE_STATIC_SRCU(kfd_processes_srcu);
52
53 static struct workqueue_struct *kfd_process_wq;
54
55 struct kfd_process_release_work {
56 struct work_struct kfd_work;
57 struct kfd_process *p;
58 };
59
60 static struct kfd_process *find_process(const struct task_struct *thread);
61 static struct kfd_process *create_process(const struct task_struct *thread);
62
kfd_process_create_wq(void)63 void kfd_process_create_wq(void)
64 {
65 if (!kfd_process_wq)
66 kfd_process_wq = create_workqueue("kfd_process_wq");
67 }
68
kfd_process_destroy_wq(void)69 void kfd_process_destroy_wq(void)
70 {
71 if (kfd_process_wq) {
72 flush_workqueue(kfd_process_wq);
73 destroy_workqueue(kfd_process_wq);
74 kfd_process_wq = NULL;
75 }
76 }
77
kfd_create_process(const struct task_struct * thread)78 struct kfd_process *kfd_create_process(const struct task_struct *thread)
79 {
80 struct kfd_process *process;
81
82 BUG_ON(!kfd_process_wq);
83
84 if (thread->mm == NULL)
85 return ERR_PTR(-EINVAL);
86
87 /* Only the pthreads threading model is supported. */
88 if (thread->group_leader->mm != thread->mm)
89 return ERR_PTR(-EINVAL);
90
91 /* Take mmap_sem because we call __mmu_notifier_register inside */
92 down_write(&thread->mm->mmap_sem);
93
94 /*
95 * take kfd processes mutex before starting of process creation
96 * so there won't be a case where two threads of the same process
97 * create two kfd_process structures
98 */
99 mutex_lock(&kfd_processes_mutex);
100
101 /* A prior open of /dev/kfd could have already created the process. */
102 process = find_process(thread);
103 if (process)
104 pr_debug("kfd: process already found\n");
105
106 if (!process)
107 process = create_process(thread);
108
109 mutex_unlock(&kfd_processes_mutex);
110
111 up_write(&thread->mm->mmap_sem);
112
113 return process;
114 }
115
kfd_get_process(const struct task_struct * thread)116 struct kfd_process *kfd_get_process(const struct task_struct *thread)
117 {
118 struct kfd_process *process;
119
120 if (thread->mm == NULL)
121 return ERR_PTR(-EINVAL);
122
123 /* Only the pthreads threading model is supported. */
124 if (thread->group_leader->mm != thread->mm)
125 return ERR_PTR(-EINVAL);
126
127 process = find_process(thread);
128 if (!process)
129 return ERR_PTR(-EINVAL);
130
131 return process;
132 }
133
find_process_by_mm(const struct mm_struct * mm)134 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
135 {
136 struct kfd_process *process;
137
138 hash_for_each_possible_rcu(kfd_processes_table, process,
139 kfd_processes, (uintptr_t)mm)
140 if (process->mm == mm)
141 return process;
142
143 return NULL;
144 }
145
find_process(const struct task_struct * thread)146 static struct kfd_process *find_process(const struct task_struct *thread)
147 {
148 struct kfd_process *p;
149 int idx;
150
151 idx = srcu_read_lock(&kfd_processes_srcu);
152 p = find_process_by_mm(thread->mm);
153 srcu_read_unlock(&kfd_processes_srcu, idx);
154
155 return p;
156 }
157
kfd_process_wq_release(struct work_struct * work)158 static void kfd_process_wq_release(struct work_struct *work)
159 {
160 struct kfd_process_release_work *my_work;
161 struct kfd_process_device *pdd, *temp;
162 struct kfd_process *p;
163
164 my_work = (struct kfd_process_release_work *) work;
165
166 p = my_work->p;
167
168 pr_debug("Releasing process (pasid %d) in workqueue\n",
169 p->pasid);
170
171 mutex_lock(&p->mutex);
172
173 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
174 per_device_list) {
175 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
176 pdd->dev->id, p->pasid);
177
178 if (pdd->reset_wavefronts)
179 dbgdev_wave_reset_wavefronts(pdd->dev, p);
180
181 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
182 list_del(&pdd->per_device_list);
183
184 kfree(pdd);
185 }
186
187 kfd_event_free_process(p);
188
189 kfd_pasid_free(p->pasid);
190
191 mutex_unlock(&p->mutex);
192
193 mutex_destroy(&p->mutex);
194
195 kfree(p->queues);
196
197 kfree(p);
198
199 kfree((void *)work);
200 }
201
kfd_process_destroy_delayed(struct rcu_head * rcu)202 static void kfd_process_destroy_delayed(struct rcu_head *rcu)
203 {
204 struct kfd_process_release_work *work;
205 struct kfd_process *p;
206
207 BUG_ON(!kfd_process_wq);
208
209 p = container_of(rcu, struct kfd_process, rcu);
210 BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
211
212 mmdrop(p->mm);
213
214 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
215
216 if (work) {
217 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
218 work->p = p;
219 queue_work(kfd_process_wq, (struct work_struct *) work);
220 }
221 }
222
kfd_process_notifier_release(struct mmu_notifier * mn,struct mm_struct * mm)223 static void kfd_process_notifier_release(struct mmu_notifier *mn,
224 struct mm_struct *mm)
225 {
226 struct kfd_process *p;
227 struct kfd_process_device *pdd = NULL;
228
229 /*
230 * The kfd_process structure can not be free because the
231 * mmu_notifier srcu is read locked
232 */
233 p = container_of(mn, struct kfd_process, mmu_notifier);
234 BUG_ON(p->mm != mm);
235
236 mutex_lock(&kfd_processes_mutex);
237 hash_del_rcu(&p->kfd_processes);
238 mutex_unlock(&kfd_processes_mutex);
239 synchronize_srcu(&kfd_processes_srcu);
240
241 mutex_lock(&p->mutex);
242
243 /* In case our notifier is called before IOMMU notifier */
244 pqm_uninit(&p->pqm);
245
246 /* Iterate over all process device data structure and check
247 * if we should delete debug managers and reset all wavefronts
248 */
249 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
250 if ((pdd->dev->dbgmgr) &&
251 (pdd->dev->dbgmgr->pasid == p->pasid))
252 kfd_dbgmgr_destroy(pdd->dev->dbgmgr);
253
254 if (pdd->reset_wavefronts) {
255 pr_warn("amdkfd: Resetting all wave fronts\n");
256 dbgdev_wave_reset_wavefronts(pdd->dev, p);
257 pdd->reset_wavefronts = false;
258 }
259 }
260
261 mutex_unlock(&p->mutex);
262
263 /*
264 * Because we drop mm_count inside kfd_process_destroy_delayed
265 * and because the mmu_notifier_unregister function also drop
266 * mm_count we need to take an extra count here.
267 */
268 atomic_inc(&p->mm->mm_count);
269 mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
270 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
271 }
272
273 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
274 .release = kfd_process_notifier_release,
275 };
276
create_process(const struct task_struct * thread)277 static struct kfd_process *create_process(const struct task_struct *thread)
278 {
279 struct kfd_process *process;
280 int err = -ENOMEM;
281
282 process = kzalloc(sizeof(*process), GFP_KERNEL);
283
284 if (!process)
285 goto err_alloc_process;
286
287 process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
288 sizeof(process->queues[0]), GFP_KERNEL);
289 if (!process->queues)
290 goto err_alloc_queues;
291
292 process->pasid = kfd_pasid_alloc();
293 if (process->pasid == 0)
294 goto err_alloc_pasid;
295
296 mutex_init(&process->mutex);
297
298 process->mm = thread->mm;
299
300 /* register notifier */
301 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
302 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
303 if (err)
304 goto err_mmu_notifier;
305
306 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
307 (uintptr_t)process->mm);
308
309 process->lead_thread = thread->group_leader;
310
311 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
312
313 INIT_LIST_HEAD(&process->per_device_data);
314
315 kfd_event_init_process(process);
316
317 err = pqm_init(&process->pqm, process);
318 if (err != 0)
319 goto err_process_pqm_init;
320
321 /* init process apertures*/
322 process->is_32bit_user_mode = is_compat_task();
323 if (kfd_init_apertures(process) != 0)
324 goto err_init_apretures;
325
326 return process;
327
328 err_init_apretures:
329 pqm_uninit(&process->pqm);
330 err_process_pqm_init:
331 hash_del_rcu(&process->kfd_processes);
332 synchronize_rcu();
333 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
334 err_mmu_notifier:
335 kfd_pasid_free(process->pasid);
336 err_alloc_pasid:
337 kfree(process->queues);
338 err_alloc_queues:
339 kfree(process);
340 err_alloc_process:
341 return ERR_PTR(err);
342 }
343
kfd_get_process_device_data(struct kfd_dev * dev,struct kfd_process * p)344 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
345 struct kfd_process *p)
346 {
347 struct kfd_process_device *pdd = NULL;
348
349 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
350 if (pdd->dev == dev)
351 break;
352
353 return pdd;
354 }
355
kfd_create_process_device_data(struct kfd_dev * dev,struct kfd_process * p)356 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
357 struct kfd_process *p)
358 {
359 struct kfd_process_device *pdd = NULL;
360
361 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
362 if (pdd != NULL) {
363 pdd->dev = dev;
364 INIT_LIST_HEAD(&pdd->qpd.queues_list);
365 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
366 pdd->qpd.dqm = dev->dqm;
367 pdd->reset_wavefronts = false;
368 list_add(&pdd->per_device_list, &p->per_device_data);
369 }
370
371 return pdd;
372 }
373
374 /*
375 * Direct the IOMMU to bind the process (specifically the pasid->mm)
376 * to the device.
377 * Unbinding occurs when the process dies or the device is removed.
378 *
379 * Assumes that the process lock is held.
380 */
kfd_bind_process_to_device(struct kfd_dev * dev,struct kfd_process * p)381 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
382 struct kfd_process *p)
383 {
384 struct kfd_process_device *pdd;
385 int err;
386
387 pdd = kfd_get_process_device_data(dev, p);
388 if (!pdd) {
389 pr_err("Process device data doesn't exist\n");
390 return ERR_PTR(-ENOMEM);
391 }
392
393 if (pdd->bound)
394 return pdd;
395
396 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
397 if (err < 0)
398 return ERR_PTR(err);
399
400 pdd->bound = true;
401
402 return pdd;
403 }
404
kfd_unbind_process_from_device(struct kfd_dev * dev,unsigned int pasid)405 void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
406 {
407 struct kfd_process *p;
408 struct kfd_process_device *pdd;
409 int idx, i;
410
411 BUG_ON(dev == NULL);
412
413 idx = srcu_read_lock(&kfd_processes_srcu);
414
415 /*
416 * Look for the process that matches the pasid. If there is no such
417 * process, we either released it in amdkfd's own notifier, or there
418 * is a bug. Unfortunately, there is no way to tell...
419 */
420 hash_for_each_rcu(kfd_processes_table, i, p, kfd_processes)
421 if (p->pasid == pasid) {
422
423 srcu_read_unlock(&kfd_processes_srcu, idx);
424
425 pr_debug("Unbinding process %d from IOMMU\n", pasid);
426
427 mutex_lock(&p->mutex);
428
429 if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
430 kfd_dbgmgr_destroy(dev->dbgmgr);
431
432 pqm_uninit(&p->pqm);
433
434 pdd = kfd_get_process_device_data(dev, p);
435
436 if (!pdd) {
437 mutex_unlock(&p->mutex);
438 return;
439 }
440
441 if (pdd->reset_wavefronts) {
442 dbgdev_wave_reset_wavefronts(pdd->dev, p);
443 pdd->reset_wavefronts = false;
444 }
445
446 /*
447 * Just mark pdd as unbound, because we still need it
448 * to call amd_iommu_unbind_pasid() in when the
449 * process exits.
450 * We don't call amd_iommu_unbind_pasid() here
451 * because the IOMMU called us.
452 */
453 pdd->bound = false;
454
455 mutex_unlock(&p->mutex);
456
457 return;
458 }
459
460 srcu_read_unlock(&kfd_processes_srcu, idx);
461 }
462
kfd_get_first_process_device_data(struct kfd_process * p)463 struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
464 {
465 return list_first_entry(&p->per_device_data,
466 struct kfd_process_device,
467 per_device_list);
468 }
469
kfd_get_next_process_device_data(struct kfd_process * p,struct kfd_process_device * pdd)470 struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
471 struct kfd_process_device *pdd)
472 {
473 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
474 return NULL;
475 return list_next_entry(pdd, per_device_list);
476 }
477
kfd_has_process_device_data(struct kfd_process * p)478 bool kfd_has_process_device_data(struct kfd_process *p)
479 {
480 return !(list_empty(&p->per_device_data));
481 }
482
483 /* This returns with process->mutex locked. */
kfd_lookup_process_by_pasid(unsigned int pasid)484 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
485 {
486 struct kfd_process *p;
487 unsigned int temp;
488
489 int idx = srcu_read_lock(&kfd_processes_srcu);
490
491 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
492 if (p->pasid == pasid) {
493 mutex_lock(&p->mutex);
494 break;
495 }
496 }
497
498 srcu_read_unlock(&kfd_processes_srcu, idx);
499
500 return p;
501 }
502