blob: b81ad8164da603a72d88753bd75fd0b8bfe725cb [file] [log] [blame]
Oded Gabbay19f6d2a2014-07-16 23:25:31 +03001/*
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 Molnar6e84f312017-02-08 18:51:29 +010026#include <linux/sched/mm.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030027#include <linux/slab.h>
Oded Gabbayb17f0682014-07-17 00:06:27 +030028#include <linux/amd-iommu.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030029#include <linux/notifier.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020030#include <linux/compat.h>
31
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030032struct mm_struct;
33
34#include "kfd_priv.h"
Ben Gozc3447e82015-05-20 18:05:44 +030035#include "kfd_dbgmgr.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030036
37/*
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030038 * List of struct kfd_process (field kfd_process).
39 * Unique/indexed by mm_struct*
40 */
41#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
42static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
43static DEFINE_MUTEX(kfd_processes_mutex);
44
45DEFINE_STATIC_SRCU(kfd_processes_srcu);
46
47static struct workqueue_struct *kfd_process_wq;
48
49struct kfd_process_release_work {
50 struct work_struct kfd_work;
51 struct kfd_process *p;
52};
53
54static struct kfd_process *find_process(const struct task_struct *thread);
55static struct kfd_process *create_process(const struct task_struct *thread);
56
57void kfd_process_create_wq(void)
58{
59 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053060 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030061}
62
63void kfd_process_destroy_wq(void)
64{
65 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030066 destroy_workqueue(kfd_process_wq);
67 kfd_process_wq = NULL;
68 }
69}
70
71struct kfd_process *kfd_create_process(const struct task_struct *thread)
72{
73 struct kfd_process *process;
74
Kent Russell4eacc26b2017-08-15 23:00:06 -040075 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030076 return ERR_PTR(-EINVAL);
77
78 /* Only the pthreads threading model is supported. */
79 if (thread->group_leader->mm != thread->mm)
80 return ERR_PTR(-EINVAL);
81
82 /* Take mmap_sem because we call __mmu_notifier_register inside */
83 down_write(&thread->mm->mmap_sem);
84
85 /*
86 * take kfd processes mutex before starting of process creation
87 * so there won't be a case where two threads of the same process
88 * create two kfd_process structures
89 */
90 mutex_lock(&kfd_processes_mutex);
91
92 /* A prior open of /dev/kfd could have already created the process. */
93 process = find_process(thread);
94 if (process)
Kent Russell79775b62017-08-15 23:00:05 -040095 pr_debug("Process already found\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030096
97 if (!process)
98 process = create_process(thread);
99
100 mutex_unlock(&kfd_processes_mutex);
101
102 up_write(&thread->mm->mmap_sem);
103
104 return process;
105}
106
107struct kfd_process *kfd_get_process(const struct task_struct *thread)
108{
109 struct kfd_process *process;
110
Kent Russell4eacc26b2017-08-15 23:00:06 -0400111 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300112 return ERR_PTR(-EINVAL);
113
114 /* Only the pthreads threading model is supported. */
115 if (thread->group_leader->mm != thread->mm)
116 return ERR_PTR(-EINVAL);
117
118 process = find_process(thread);
119
120 return process;
121}
122
123static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
124{
125 struct kfd_process *process;
126
127 hash_for_each_possible_rcu(kfd_processes_table, process,
128 kfd_processes, (uintptr_t)mm)
129 if (process->mm == mm)
130 return process;
131
132 return NULL;
133}
134
135static struct kfd_process *find_process(const struct task_struct *thread)
136{
137 struct kfd_process *p;
138 int idx;
139
140 idx = srcu_read_lock(&kfd_processes_srcu);
141 p = find_process_by_mm(thread->mm);
142 srcu_read_unlock(&kfd_processes_srcu, idx);
143
144 return p;
145}
146
147static void kfd_process_wq_release(struct work_struct *work)
148{
149 struct kfd_process_release_work *my_work;
150 struct kfd_process_device *pdd, *temp;
151 struct kfd_process *p;
152
153 my_work = (struct kfd_process_release_work *) work;
154
155 p = my_work->p;
156
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200157 pr_debug("Releasing process (pasid %d) in workqueue\n",
158 p->pasid);
159
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300160 mutex_lock(&p->mutex);
161
162 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
163 per_device_list) {
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200164 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
165 pdd->dev->id, p->pasid);
166
Yong Zhao733fa1f2017-09-20 18:10:14 -0400167 if (pdd->bound == PDD_BOUND)
168 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300169
Yong Zhao733fa1f2017-09-20 18:10:14 -0400170 list_del(&pdd->per_device_list);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300171 kfree(pdd);
172 }
173
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300174 kfd_event_free_process(p);
175
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300176 kfd_pasid_free(p->pasid);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400177 kfd_free_process_doorbells(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300178
179 mutex_unlock(&p->mutex);
180
181 mutex_destroy(&p->mutex);
182
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300183 kfree(p);
184
Amitoj Kaur Chawla642f0f22016-01-25 23:03:57 +0530185 kfree(work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300186}
187
188static void kfd_process_destroy_delayed(struct rcu_head *rcu)
189{
190 struct kfd_process_release_work *work;
191 struct kfd_process *p;
192
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300193 p = container_of(rcu, struct kfd_process, rcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300194
195 mmdrop(p->mm);
196
Firo Yang1549fcd2015-04-23 17:58:05 +0800197 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300198
199 if (work) {
200 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
201 work->p = p;
202 queue_work(kfd_process_wq, (struct work_struct *) work);
203 }
204}
205
206static void kfd_process_notifier_release(struct mmu_notifier *mn,
207 struct mm_struct *mm)
208{
209 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200210 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300211
212 /*
213 * The kfd_process structure can not be free because the
214 * mmu_notifier srcu is read locked
215 */
216 p = container_of(mn, struct kfd_process, mmu_notifier);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400217 if (WARN_ON(p->mm != mm))
218 return;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300219
220 mutex_lock(&kfd_processes_mutex);
221 hash_del_rcu(&p->kfd_processes);
222 mutex_unlock(&kfd_processes_mutex);
223 synchronize_srcu(&kfd_processes_srcu);
224
Ben Goz45102042014-07-17 01:04:10 +0300225 mutex_lock(&p->mutex);
226
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400227 kfd_process_dequeue_from_all_devices(p);
Ben Goz45102042014-07-17 01:04:10 +0300228 pqm_uninit(&p->pqm);
229
Ben Goza82918f2015-03-25 13:12:20 +0200230 /* Iterate over all process device data structure and check
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400231 * if we should delete debug managers
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300232 */
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400233 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300234 if ((pdd->dev->dbgmgr) &&
235 (pdd->dev->dbgmgr->pasid == p->pasid))
236 kfd_dbgmgr_destroy(pdd->dev->dbgmgr);
237
Ben Goz45102042014-07-17 01:04:10 +0300238 mutex_unlock(&p->mutex);
239
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300240 /*
241 * Because we drop mm_count inside kfd_process_destroy_delayed
242 * and because the mmu_notifier_unregister function also drop
243 * mm_count we need to take an extra count here.
244 */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800245 mmgrab(p->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300246 mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
247 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
248}
249
250static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
251 .release = kfd_process_notifier_release,
252};
253
254static struct kfd_process *create_process(const struct task_struct *thread)
255{
256 struct kfd_process *process;
257 int err = -ENOMEM;
258
259 process = kzalloc(sizeof(*process), GFP_KERNEL);
260
261 if (!process)
262 goto err_alloc_process;
263
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300264 process->pasid = kfd_pasid_alloc();
265 if (process->pasid == 0)
266 goto err_alloc_pasid;
267
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400268 if (kfd_alloc_process_doorbells(process) < 0)
269 goto err_alloc_doorbells;
270
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300271 mutex_init(&process->mutex);
272
273 process->mm = thread->mm;
274
275 /* register notifier */
276 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
277 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
278 if (err)
279 goto err_mmu_notifier;
280
281 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
282 (uintptr_t)process->mm);
283
284 process->lead_thread = thread->group_leader;
285
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300286 INIT_LIST_HEAD(&process->per_device_data);
287
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300288 kfd_event_init_process(process);
289
Ben Goz45102042014-07-17 01:04:10 +0300290 err = pqm_init(&process->pqm, process);
291 if (err != 0)
292 goto err_process_pqm_init;
293
Alexey Skidanovdd592392014-11-18 13:56:23 +0200294 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700295 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenterb312b2b2017-06-14 13:58:53 +0300296 err = kfd_init_apertures(process);
297 if (err != 0)
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200298 goto err_init_apertures;
Alexey Skidanovdd592392014-11-18 13:56:23 +0200299
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300300 return process;
301
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200302err_init_apertures:
Alexey Skidanovdd592392014-11-18 13:56:23 +0200303 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300304err_process_pqm_init:
305 hash_del_rcu(&process->kfd_processes);
306 synchronize_rcu();
307 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300308err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300309 mutex_destroy(&process->mutex);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400310 kfd_free_process_doorbells(process);
311err_alloc_doorbells:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300312 kfd_pasid_free(process->pasid);
313err_alloc_pasid:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300314 kfree(process);
315err_alloc_process:
316 return ERR_PTR(err);
317}
318
319struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200320 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300321{
322 struct kfd_process_device *pdd = NULL;
323
324 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
325 if (pdd->dev == dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400326 return pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300327
Yong Zhao733fa1f2017-09-20 18:10:14 -0400328 return NULL;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200329}
330
331struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
332 struct kfd_process *p)
333{
334 struct kfd_process_device *pdd = NULL;
335
336 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
337 if (pdd != NULL) {
338 pdd->dev = dev;
339 INIT_LIST_HEAD(&pdd->qpd.queues_list);
340 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
341 pdd->qpd.dqm = dev->dqm;
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400342 pdd->process = p;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400343 pdd->bound = PDD_UNBOUND;
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400344 pdd->already_dequeued = false;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200345 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300346 }
347
348 return pdd;
349}
350
351/*
352 * Direct the IOMMU to bind the process (specifically the pasid->mm)
353 * to the device.
354 * Unbinding occurs when the process dies or the device is removed.
355 *
356 * Assumes that the process lock is held.
357 */
358struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
359 struct kfd_process *p)
360{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200361 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300362 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300363
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200364 pdd = kfd_get_process_device_data(dev, p);
365 if (!pdd) {
366 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300367 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200368 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300369
Yong Zhao733fa1f2017-09-20 18:10:14 -0400370 if (pdd->bound == PDD_BOUND) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300371 return pdd;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400372 } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
373 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
374 return ERR_PTR(-EINVAL);
375 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300376
Oded Gabbayb17f0682014-07-17 00:06:27 +0300377 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
378 if (err < 0)
379 return ERR_PTR(err);
380
Yong Zhao733fa1f2017-09-20 18:10:14 -0400381 pdd->bound = PDD_BOUND;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300382
383 return pdd;
384}
385
Yong Zhao733fa1f2017-09-20 18:10:14 -0400386/*
387 * Bind processes do the device that have been temporarily unbound
388 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
389 */
390int kfd_bind_processes_to_device(struct kfd_dev *dev)
391{
392 struct kfd_process_device *pdd;
393 struct kfd_process *p;
394 unsigned int temp;
395 int err = 0;
396
397 int idx = srcu_read_lock(&kfd_processes_srcu);
398
399 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
400 mutex_lock(&p->mutex);
401 pdd = kfd_get_process_device_data(dev, p);
402 if (pdd->bound != PDD_BOUND_SUSPENDED) {
403 mutex_unlock(&p->mutex);
404 continue;
405 }
406
407 err = amd_iommu_bind_pasid(dev->pdev, p->pasid,
408 p->lead_thread);
409 if (err < 0) {
410 pr_err("unexpected pasid %d binding failure\n",
411 p->pasid);
412 mutex_unlock(&p->mutex);
413 break;
414 }
415
416 pdd->bound = PDD_BOUND;
417 mutex_unlock(&p->mutex);
418 }
419
420 srcu_read_unlock(&kfd_processes_srcu, idx);
421
422 return err;
423}
424
425/*
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400426 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
427 * processes will be restored to PDD_BOUND state in
428 * kfd_bind_processes_to_device.
Yong Zhao733fa1f2017-09-20 18:10:14 -0400429 */
430void kfd_unbind_processes_from_device(struct kfd_dev *dev)
431{
432 struct kfd_process_device *pdd;
433 struct kfd_process *p;
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400434 unsigned int temp;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400435
436 int idx = srcu_read_lock(&kfd_processes_srcu);
437
438 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
439 mutex_lock(&p->mutex);
440 pdd = kfd_get_process_device_data(dev, p);
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400441
Yong Zhao733fa1f2017-09-20 18:10:14 -0400442 if (pdd->bound == PDD_BOUND)
443 pdd->bound = PDD_BOUND_SUSPENDED;
444 mutex_unlock(&p->mutex);
Yong Zhao733fa1f2017-09-20 18:10:14 -0400445 }
446
447 srcu_read_unlock(&kfd_processes_srcu, idx);
448}
449
450void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300451{
452 struct kfd_process *p;
453 struct kfd_process_device *pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300454
Oded Gabbay121b78e2016-05-26 08:41:08 +0300455 /*
456 * Look for the process that matches the pasid. If there is no such
457 * process, we either released it in amdkfd's own notifier, or there
458 * is a bug. Unfortunately, there is no way to tell...
459 */
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000460 p = kfd_lookup_process_by_pasid(pasid);
461 if (!p)
462 return;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300463
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000464 pr_debug("Unbinding process %d from IOMMU\n", pasid);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300465
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000466 if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
467 kfd_dbgmgr_destroy(dev->dbgmgr);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300468
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000469 pdd = kfd_get_process_device_data(dev, p);
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400470 if (pdd)
471 /* For GPU relying on IOMMU, we need to dequeue here
472 * when PASID is still bound.
473 */
474 kfd_process_dequeue_from_device(pdd);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300475
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000476 mutex_unlock(&p->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300477}
478
Kent Russell8eabaf52017-08-15 23:00:04 -0400479struct kfd_process_device *kfd_get_first_process_device_data(
480 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300481{
482 return list_first_entry(&p->per_device_data,
483 struct kfd_process_device,
484 per_device_list);
485}
486
Kent Russell8eabaf52017-08-15 23:00:04 -0400487struct kfd_process_device *kfd_get_next_process_device_data(
488 struct kfd_process *p,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300489 struct kfd_process_device *pdd)
490{
491 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
492 return NULL;
493 return list_next_entry(pdd, per_device_list);
494}
495
496bool kfd_has_process_device_data(struct kfd_process *p)
497{
498 return !(list_empty(&p->per_device_data));
499}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300500
501/* This returns with process->mutex locked. */
502struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
503{
504 struct kfd_process *p;
505 unsigned int temp;
506
507 int idx = srcu_read_lock(&kfd_processes_srcu);
508
509 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
510 if (p->pasid == pasid) {
511 mutex_lock(&p->mutex);
512 break;
513 }
514 }
515
516 srcu_read_unlock(&kfd_processes_srcu, idx);
517
518 return p;
519}