Skip to content

Commit 7fdc48c

Browse files
committed
drm/panfrost: Make sure MMU context lifetime is not bound to panfrost_priv
Jobs can be in-flight when the file descriptor is closed (either because the process did not terminate properly, or because it didn't wait for all GPU jobs to be finished), and apparently panfrost_job_close() does not cancel already running jobs. Let's refcount the MMU context object so it's lifetime is no longer bound to the FD lifetime and running jobs can finish properly without generating spurious page faults. Reported-by: Icecream95 <ixn@keemail.me> Fixes: 7282f76 ("drm/panfrost: Implement per FD address spaces") Cc: <stable@vger.kernel.org> Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com> Reviewed-by: Steven Price <steven.price@arm.com> Link: https://patchwork.freedesktop.org/patch/msgid/20210621133907.1683899-2-boris.brezillon@collabora.com
1 parent 7601d53 commit 7fdc48c

6 files changed

Lines changed: 136 additions & 111 deletions

File tree

‎drivers/gpu/drm/panfrost/panfrost_device.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ struct panfrost_device {
121121
};
122122

123123
struct panfrost_mmu {
124+
struct panfrost_device *pfdev;
125+
struct kref refcount;
124126
struct io_pgtable_cfg pgtbl_cfg;
125127
struct io_pgtable_ops *pgtbl_ops;
128+
struct drm_mm mm;
129+
spinlock_t mm_lock;
126130
int as;
127131
atomic_t as_count;
128132
struct list_head list;
@@ -133,9 +137,7 @@ struct panfrost_file_priv {
133137

134138
struct drm_sched_entity sched_entity[NUM_JOB_SLOTS];
135139

136-
struct panfrost_mmu mmu;
137-
struct drm_mm mm;
138-
spinlock_t mm_lock;
140+
struct panfrost_mmu *mmu;
139141
};
140142

141143
static inline struct panfrost_device *to_panfrost_device(struct drm_device *ddev)

‎drivers/gpu/drm/panfrost/panfrost_drv.c‎

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static int panfrost_ioctl_madvise(struct drm_device *dev, void *data,
412412
* anyway, so let's not bother.
413413
*/
414414
if (!list_is_singular(&bo->mappings.list) ||
415-
WARN_ON_ONCE(first->mmu != &priv->mmu)) {
415+
WARN_ON_ONCE(first->mmu != priv->mmu)) {
416416
ret = -EINVAL;
417417
goto out_unlock_mappings;
418418
}
@@ -444,32 +444,6 @@ int panfrost_unstable_ioctl_check(void)
444444
return 0;
445445
}
446446

447-
#define PFN_4G (SZ_4G >> PAGE_SHIFT)
448-
#define PFN_4G_MASK (PFN_4G - 1)
449-
#define PFN_16M (SZ_16M >> PAGE_SHIFT)
450-
451-
static void panfrost_drm_mm_color_adjust(const struct drm_mm_node *node,
452-
unsigned long color,
453-
u64 *start, u64 *end)
454-
{
455-
/* Executable buffers can't start or end on a 4GB boundary */
456-
if (!(color & PANFROST_BO_NOEXEC)) {
457-
u64 next_seg;
458-
459-
if ((*start & PFN_4G_MASK) == 0)
460-
(*start)++;
461-
462-
if ((*end & PFN_4G_MASK) == 0)
463-
(*end)--;
464-
465-
next_seg = ALIGN(*start, PFN_4G);
466-
if (next_seg - *start <= PFN_16M)
467-
*start = next_seg + 1;
468-
469-
*end = min(*end, ALIGN(*start, PFN_4G) - 1);
470-
}
471-
}
472-
473447
static int
474448
panfrost_open(struct drm_device *dev, struct drm_file *file)
475449
{
@@ -484,15 +458,11 @@ panfrost_open(struct drm_device *dev, struct drm_file *file)
484458
panfrost_priv->pfdev = pfdev;
485459
file->driver_priv = panfrost_priv;
486460

487-
spin_lock_init(&panfrost_priv->mm_lock);
488-
489-
/* 4G enough for now. can be 48-bit */
490-
drm_mm_init(&panfrost_priv->mm, SZ_32M >> PAGE_SHIFT, (SZ_4G - SZ_32M) >> PAGE_SHIFT);
491-
panfrost_priv->mm.color_adjust = panfrost_drm_mm_color_adjust;
492-
493-
ret = panfrost_mmu_pgtable_alloc(panfrost_priv);
494-
if (ret)
495-
goto err_pgtable;
461+
panfrost_priv->mmu = panfrost_mmu_ctx_create(pfdev);
462+
if (IS_ERR(panfrost_priv->mmu)) {
463+
ret = PTR_ERR(panfrost_priv->mmu);
464+
goto err_free;
465+
}
496466

497467
ret = panfrost_job_open(panfrost_priv);
498468
if (ret)
@@ -501,9 +471,8 @@ panfrost_open(struct drm_device *dev, struct drm_file *file)
501471
return 0;
502472

503473
err_job:
504-
panfrost_mmu_pgtable_free(panfrost_priv);
505-
err_pgtable:
506-
drm_mm_takedown(&panfrost_priv->mm);
474+
panfrost_mmu_ctx_put(panfrost_priv->mmu);
475+
err_free:
507476
kfree(panfrost_priv);
508477
return ret;
509478
}
@@ -516,8 +485,7 @@ panfrost_postclose(struct drm_device *dev, struct drm_file *file)
516485
panfrost_perfcnt_close(file);
517486
panfrost_job_close(panfrost_priv);
518487

519-
panfrost_mmu_pgtable_free(panfrost_priv);
520-
drm_mm_takedown(&panfrost_priv->mm);
488+
panfrost_mmu_ctx_put(panfrost_priv->mmu);
521489
kfree(panfrost_priv);
522490
}
523491

‎drivers/gpu/drm/panfrost/panfrost_gem.c‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo,
6060

6161
mutex_lock(&bo->mappings.lock);
6262
list_for_each_entry(iter, &bo->mappings.list, node) {
63-
if (iter->mmu == &priv->mmu) {
63+
if (iter->mmu == priv->mmu) {
6464
kref_get(&iter->refcount);
6565
mapping = iter;
6666
break;
@@ -74,16 +74,13 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo,
7474
static void
7575
panfrost_gem_teardown_mapping(struct panfrost_gem_mapping *mapping)
7676
{
77-
struct panfrost_file_priv *priv;
78-
7977
if (mapping->active)
8078
panfrost_mmu_unmap(mapping);
8179

82-
priv = container_of(mapping->mmu, struct panfrost_file_priv, mmu);
83-
spin_lock(&priv->mm_lock);
80+
spin_lock(&mapping->mmu->mm_lock);
8481
if (drm_mm_node_allocated(&mapping->mmnode))
8582
drm_mm_remove_node(&mapping->mmnode);
86-
spin_unlock(&priv->mm_lock);
83+
spin_unlock(&mapping->mmu->mm_lock);
8784
}
8885

8986
static void panfrost_gem_mapping_release(struct kref *kref)
@@ -94,6 +91,7 @@ static void panfrost_gem_mapping_release(struct kref *kref)
9491

9592
panfrost_gem_teardown_mapping(mapping);
9693
drm_gem_object_put(&mapping->obj->base.base);
94+
panfrost_mmu_ctx_put(mapping->mmu);
9795
kfree(mapping);
9896
}
9997

@@ -143,11 +141,11 @@ int panfrost_gem_open(struct drm_gem_object *obj, struct drm_file *file_priv)
143141
else
144142
align = size >= SZ_2M ? SZ_2M >> PAGE_SHIFT : 0;
145143

146-
mapping->mmu = &priv->mmu;
147-
spin_lock(&priv->mm_lock);
148-
ret = drm_mm_insert_node_generic(&priv->mm, &mapping->mmnode,
144+
mapping->mmu = panfrost_mmu_ctx_get(priv->mmu);
145+
spin_lock(&mapping->mmu->mm_lock);
146+
ret = drm_mm_insert_node_generic(&mapping->mmu->mm, &mapping->mmnode,
149147
size >> PAGE_SHIFT, align, color, 0);
150-
spin_unlock(&priv->mm_lock);
148+
spin_unlock(&mapping->mmu->mm_lock);
151149
if (ret)
152150
goto err;
153151

@@ -176,7 +174,7 @@ void panfrost_gem_close(struct drm_gem_object *obj, struct drm_file *file_priv)
176174

177175
mutex_lock(&bo->mappings.lock);
178176
list_for_each_entry(iter, &bo->mappings.list, node) {
179-
if (iter->mmu == &priv->mmu) {
177+
if (iter->mmu == priv->mmu) {
180178
mapping = iter;
181179
list_del(&iter->node);
182180
break;

‎drivers/gpu/drm/panfrost/panfrost_job.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static void panfrost_job_hw_submit(struct panfrost_job *job, int js)
165165
return;
166166
}
167167

168-
cfg = panfrost_mmu_as_get(pfdev, &job->file_priv->mmu);
168+
cfg = panfrost_mmu_as_get(pfdev, job->file_priv->mmu);
169169

170170
job_write(pfdev, JS_HEAD_NEXT_LO(js), jc_head & 0xFFFFFFFF);
171171
job_write(pfdev, JS_HEAD_NEXT_HI(js), jc_head >> 32);
@@ -516,7 +516,7 @@ static irqreturn_t panfrost_job_irq_handler(int irq, void *data)
516516
if (job) {
517517
pfdev->jobs[j] = NULL;
518518

519-
panfrost_mmu_as_put(pfdev, &job->file_priv->mmu);
519+
panfrost_mmu_as_put(pfdev, job->file_priv->mmu);
520520
panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
521521

522522
dma_fence_signal_locked(job->done_fence);

‎drivers/gpu/drm/panfrost/panfrost_mmu.c‎

Lines changed: 108 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3+
4+
#include <drm/panfrost_drm.h>
5+
36
#include <linux/atomic.h>
47
#include <linux/bitfield.h>
58
#include <linux/delay.h>
@@ -337,7 +340,7 @@ static void mmu_tlb_inv_context_s1(void *cookie)
337340

338341
static void mmu_tlb_sync_context(void *cookie)
339342
{
340-
//struct panfrost_device *pfdev = cookie;
343+
//struct panfrost_mmu *mmu = cookie;
341344
// TODO: Wait 1000 GPU cycles for HW_ISSUE_6367/T60X
342345
}
343346

@@ -352,57 +355,10 @@ static const struct iommu_flush_ops mmu_tlb_ops = {
352355
.tlb_flush_walk = mmu_tlb_flush_walk,
353356
};
354357

355-
int panfrost_mmu_pgtable_alloc(struct panfrost_file_priv *priv)
356-
{
357-
struct panfrost_mmu *mmu = &priv->mmu;
358-
struct panfrost_device *pfdev = priv->pfdev;
359-
360-
INIT_LIST_HEAD(&mmu->list);
361-
mmu->as = -1;
362-
363-
mmu->pgtbl_cfg = (struct io_pgtable_cfg) {
364-
.pgsize_bitmap = SZ_4K | SZ_2M,
365-
.ias = FIELD_GET(0xff, pfdev->features.mmu_features),
366-
.oas = FIELD_GET(0xff00, pfdev->features.mmu_features),
367-
.coherent_walk = pfdev->coherent,
368-
.tlb = &mmu_tlb_ops,
369-
.iommu_dev = pfdev->dev,
370-
};
371-
372-
mmu->pgtbl_ops = alloc_io_pgtable_ops(ARM_MALI_LPAE, &mmu->pgtbl_cfg,
373-
priv);
374-
if (!mmu->pgtbl_ops)
375-
return -EINVAL;
376-
377-
return 0;
378-
}
379-
380-
void panfrost_mmu_pgtable_free(struct panfrost_file_priv *priv)
381-
{
382-
struct panfrost_device *pfdev = priv->pfdev;
383-
struct panfrost_mmu *mmu = &priv->mmu;
384-
385-
spin_lock(&pfdev->as_lock);
386-
if (mmu->as >= 0) {
387-
pm_runtime_get_noresume(pfdev->dev);
388-
if (pm_runtime_active(pfdev->dev))
389-
panfrost_mmu_disable(pfdev, mmu->as);
390-
pm_runtime_put_autosuspend(pfdev->dev);
391-
392-
clear_bit(mmu->as, &pfdev->as_alloc_mask);
393-
clear_bit(mmu->as, &pfdev->as_in_use_mask);
394-
list_del(&mmu->list);
395-
}
396-
spin_unlock(&pfdev->as_lock);
397-
398-
free_io_pgtable_ops(mmu->pgtbl_ops);
399-
}
400-
401358
static struct panfrost_gem_mapping *
402359
addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
403360
{
404361
struct panfrost_gem_mapping *mapping = NULL;
405-
struct panfrost_file_priv *priv;
406362
struct drm_mm_node *node;
407363
u64 offset = addr >> PAGE_SHIFT;
408364
struct panfrost_mmu *mmu;
@@ -415,11 +371,10 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
415371
goto out;
416372

417373
found_mmu:
418-
priv = container_of(mmu, struct panfrost_file_priv, mmu);
419374

420-
spin_lock(&priv->mm_lock);
375+
spin_lock(&mmu->mm_lock);
421376

422-
drm_mm_for_each_node(node, &priv->mm) {
377+
drm_mm_for_each_node(node, &mmu->mm) {
423378
if (offset >= node->start &&
424379
offset < (node->start + node->size)) {
425380
mapping = drm_mm_node_to_panfrost_mapping(node);
@@ -429,7 +384,7 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
429384
}
430385
}
431386

432-
spin_unlock(&priv->mm_lock);
387+
spin_unlock(&mmu->mm_lock);
433388
out:
434389
spin_unlock(&pfdev->as_lock);
435390
return mapping;
@@ -542,6 +497,107 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
542497
return ret;
543498
}
544499

500+
static void panfrost_mmu_release_ctx(struct kref *kref)
501+
{
502+
struct panfrost_mmu *mmu = container_of(kref, struct panfrost_mmu,
503+
refcount);
504+
struct panfrost_device *pfdev = mmu->pfdev;
505+
506+
spin_lock(&pfdev->as_lock);
507+
if (mmu->as >= 0) {
508+
pm_runtime_get_noresume(pfdev->dev);
509+
if (pm_runtime_active(pfdev->dev))
510+
panfrost_mmu_disable(pfdev, mmu->as);
511+
pm_runtime_put_autosuspend(pfdev->dev);
512+
513+
clear_bit(mmu->as, &pfdev->as_alloc_mask);
514+
clear_bit(mmu->as, &pfdev->as_in_use_mask);
515+
list_del(&mmu->list);
516+
}
517+
spin_unlock(&pfdev->as_lock);
518+
519+
free_io_pgtable_ops(mmu->pgtbl_ops);
520+
drm_mm_takedown(&mmu->mm);
521+
kfree(mmu);
522+
}
523+
524+
void panfrost_mmu_ctx_put(struct panfrost_mmu *mmu)
525+
{
526+
kref_put(&mmu->refcount, panfrost_mmu_release_ctx);
527+
}
528+
529+
struct panfrost_mmu *panfrost_mmu_ctx_get(struct panfrost_mmu *mmu)
530+
{
531+
kref_get(&mmu->refcount);
532+
533+
return mmu;
534+
}
535+
536+
#define PFN_4G (SZ_4G >> PAGE_SHIFT)
537+
#define PFN_4G_MASK (PFN_4G - 1)
538+
#define PFN_16M (SZ_16M >> PAGE_SHIFT)
539+
540+
static void panfrost_drm_mm_color_adjust(const struct drm_mm_node *node,
541+
unsigned long color,
542+
u64 *start, u64 *end)
543+
{
544+
/* Executable buffers can't start or end on a 4GB boundary */
545+
if (!(color & PANFROST_BO_NOEXEC)) {
546+
u64 next_seg;
547+
548+
if ((*start & PFN_4G_MASK) == 0)
549+
(*start)++;
550+
551+
if ((*end & PFN_4G_MASK) == 0)
552+
(*end)--;
553+
554+
next_seg = ALIGN(*start, PFN_4G);
555+
if (next_seg - *start <= PFN_16M)
556+
*start = next_seg + 1;
557+
558+
*end = min(*end, ALIGN(*start, PFN_4G) - 1);
559+
}
560+
}
561+
562+
struct panfrost_mmu *panfrost_mmu_ctx_create(struct panfrost_device *pfdev)
563+
{
564+
struct panfrost_mmu *mmu;
565+
566+
mmu = kzalloc(sizeof(*mmu), GFP_KERNEL);
567+
if (!mmu)
568+
return ERR_PTR(-ENOMEM);
569+
570+
mmu->pfdev = pfdev;
571+
spin_lock_init(&mmu->mm_lock);
572+
573+
/* 4G enough for now. can be 48-bit */
574+
drm_mm_init(&mmu->mm, SZ_32M >> PAGE_SHIFT, (SZ_4G - SZ_32M) >> PAGE_SHIFT);
575+
mmu->mm.color_adjust = panfrost_drm_mm_color_adjust;
576+
577+
INIT_LIST_HEAD(&mmu->list);
578+
mmu->as = -1;
579+
580+
mmu->pgtbl_cfg = (struct io_pgtable_cfg) {
581+
.pgsize_bitmap = SZ_4K | SZ_2M,
582+
.ias = FIELD_GET(0xff, pfdev->features.mmu_features),
583+
.oas = FIELD_GET(0xff00, pfdev->features.mmu_features),
584+
.coherent_walk = pfdev->coherent,
585+
.tlb = &mmu_tlb_ops,
586+
.iommu_dev = pfdev->dev,
587+
};
588+
589+
mmu->pgtbl_ops = alloc_io_pgtable_ops(ARM_MALI_LPAE, &mmu->pgtbl_cfg,
590+
mmu);
591+
if (!mmu->pgtbl_ops) {
592+
kfree(mmu);
593+
return ERR_PTR(-EINVAL);
594+
}
595+
596+
kref_init(&mmu->refcount);
597+
598+
return mmu;
599+
}
600+
545601
static const char *access_type_name(struct panfrost_device *pfdev,
546602
u32 fault_status)
547603
{

0 commit comments

Comments
 (0)