Skip to content

Commit bf67014

Browse files
huangruialexdeucher
authored andcommitted
drm/amdgpu: introduce new amdgpu_fence object to indicate the job embedded fence
The job embedded fence donesn't initialize the flags at dma_fence_init(). Then we will go a wrong way in amdgpu_fence_get_timeline_name callback and trigger a null pointer panic once we enabled the trace event here. So introduce new amdgpu_fence object to indicate the job embedded fence. [ 156.131790] BUG: kernel NULL pointer dereference, address: 00000000000002a0 [ 156.131804] #PF: supervisor read access in kernel mode [ 156.131811] #PF: error_code(0x0000) - not-present page [ 156.131817] PGD 0 P4D 0 [ 156.131824] Oops: 0000 [#1] PREEMPT SMP PTI [ 156.131832] CPU: 6 PID: 1404 Comm: sdma0 Tainted: G OE 5.16.0-rc1-custom #1 [ 156.131842] Hardware name: Gigabyte Technology Co., Ltd. Z170XP-SLI/Z170XP-SLI-CF, BIOS F20 11/04/2016 [ 156.131848] RIP: 0010:strlen+0x0/0x20 [ 156.131859] Code: 89 c0 c3 0f 1f 80 00 00 00 00 48 01 fe eb 0f 0f b6 07 38 d0 74 10 48 83 c7 01 84 c0 74 05 48 39 f7 75 ec 31 c0 c3 48 89 f8 c3 <80> 3f 00 74 10 48 89 f8 48 83 c0 01 80 38 00 75 f7 48 29 f8 c3 31 [ 156.131872] RSP: 0018:ffff9bd0018dbcf8 EFLAGS: 00010206 [ 156.131880] RAX: 00000000000002a0 RBX: ffff8d0305ef01b0 RCX: 000000000000000b [ 156.131888] RDX: ffff8d03772ab924 RSI: ffff8d0305ef01b0 RDI: 00000000000002a0 [ 156.131895] RBP: ffff9bd0018dbd60 R08: ffff8d03002094d0 R09: 0000000000000000 [ 156.131901] R10: 000000000000005e R11: 0000000000000065 R12: ffff8d03002094d0 [ 156.131907] R13: 000000000000001f R14: 0000000000070018 R15: 0000000000000007 [ 156.131914] FS: 0000000000000000(0000) GS:ffff8d062ed80000(0000) knlGS:0000000000000000 [ 156.131923] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 156.131929] CR2: 00000000000002a0 CR3: 000000001120a005 CR4: 00000000003706e0 [ 156.131937] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 156.131942] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 156.131949] Call Trace: [ 156.131953] <TASK> [ 156.131957] ? trace_event_raw_event_dma_fence+0xcc/0x200 [ 156.131973] ? ring_buffer_unlock_commit+0x23/0x130 [ 156.131982] dma_fence_init+0x92/0xb0 [ 156.131993] amdgpu_fence_emit+0x10d/0x2b0 [amdgpu] [ 156.132302] amdgpu_ib_schedule+0x2f9/0x580 [amdgpu] [ 156.132586] amdgpu_job_run+0xed/0x220 [amdgpu] v2: fix mismatch warning between the prototype and function name (Ray, kernel test robot) Signed-off-by: Huang Rui <[email protected]> Reviewed-by: Christian König <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent a2fbfd5 commit bf67014

File tree

3 files changed

+90
-51
lines changed

3 files changed

+90
-51
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,7 +4461,7 @@ int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
44614461
int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
44624462
struct amdgpu_reset_context *reset_context)
44634463
{
4464-
int i, j, r = 0;
4464+
int i, r = 0;
44654465
struct amdgpu_job *job = NULL;
44664466
bool need_full_reset =
44674467
test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
@@ -4483,15 +4483,8 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
44834483

44844484
/*clear job fence from fence drv to avoid force_completion
44854485
*leave NULL and vm flush fence in fence drv */
4486-
for (j = 0; j <= ring->fence_drv.num_fences_mask; j++) {
4487-
struct dma_fence *old, **ptr;
4486+
amdgpu_fence_driver_clear_job_fences(ring);
44884487

4489-
ptr = &ring->fence_drv.fences[j];
4490-
old = rcu_dereference_protected(*ptr, 1);
4491-
if (old && test_bit(AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT, &old->flags)) {
4492-
RCU_INIT_POINTER(*ptr, NULL);
4493-
}
4494-
}
44954488
/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
44964489
amdgpu_fence_driver_force_completion(ring);
44974490
}

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ void amdgpu_fence_slab_fini(void)
7777
* Cast helper
7878
*/
7979
static const struct dma_fence_ops amdgpu_fence_ops;
80+
static const struct dma_fence_ops amdgpu_job_fence_ops;
8081
static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
8182
{
8283
struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
8384

84-
if (__f->base.ops == &amdgpu_fence_ops)
85+
if (__f->base.ops == &amdgpu_fence_ops ||
86+
__f->base.ops == &amdgpu_job_fence_ops)
8587
return __f;
8688

8789
return NULL;
@@ -158,19 +160,18 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amd
158160
}
159161

160162
seq = ++ring->fence_drv.sync_seq;
161-
if (job != NULL && job->job_run_counter) {
163+
if (job && job->job_run_counter) {
162164
/* reinit seq for resubmitted jobs */
163165
fence->seqno = seq;
164166
} else {
165-
dma_fence_init(fence, &amdgpu_fence_ops,
166-
&ring->fence_drv.lock,
167-
adev->fence_context + ring->idx,
168-
seq);
169-
}
170-
171-
if (job != NULL) {
172-
/* mark this fence has a parent job */
173-
set_bit(AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT, &fence->flags);
167+
if (job)
168+
dma_fence_init(fence, &amdgpu_job_fence_ops,
169+
&ring->fence_drv.lock,
170+
adev->fence_context + ring->idx, seq);
171+
else
172+
dma_fence_init(fence, &amdgpu_fence_ops,
173+
&ring->fence_drv.lock,
174+
adev->fence_context + ring->idx, seq);
174175
}
175176

176177
amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
@@ -620,6 +621,25 @@ void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev)
620621
}
621622
}
622623

624+
/**
625+
* amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring
626+
*
627+
* @ring: fence of the ring to be cleared
628+
*
629+
*/
630+
void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring)
631+
{
632+
int i;
633+
struct dma_fence *old, **ptr;
634+
635+
for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) {
636+
ptr = &ring->fence_drv.fences[i];
637+
old = rcu_dereference_protected(*ptr, 1);
638+
if (old && old->ops == &amdgpu_job_fence_ops)
639+
RCU_INIT_POINTER(*ptr, NULL);
640+
}
641+
}
642+
623643
/**
624644
* amdgpu_fence_driver_force_completion - force signal latest fence of ring
625645
*
@@ -643,16 +663,14 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
643663

644664
static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
645665
{
646-
struct amdgpu_ring *ring;
666+
return (const char *)to_amdgpu_fence(f)->ring->name;
667+
}
647668

648-
if (test_bit(AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT, &f->flags)) {
649-
struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
669+
static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
670+
{
671+
struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
650672

651-
ring = to_amdgpu_ring(job->base.sched);
652-
} else {
653-
ring = to_amdgpu_fence(f)->ring;
654-
}
655-
return (const char *)ring->name;
673+
return (const char *)to_amdgpu_ring(job->base.sched)->name;
656674
}
657675

658676
/**
@@ -665,18 +683,25 @@ static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
665683
*/
666684
static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
667685
{
668-
struct amdgpu_ring *ring;
686+
if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
687+
amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
669688

670-
if (test_bit(AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT, &f->flags)) {
671-
struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
689+
return true;
690+
}
672691

673-
ring = to_amdgpu_ring(job->base.sched);
674-
} else {
675-
ring = to_amdgpu_fence(f)->ring;
676-
}
692+
/**
693+
* amdgpu_job_fence_enable_signaling - enable signalling on job fence
694+
* @f: fence
695+
*
696+
* This is the simliar function with amdgpu_fence_enable_signaling above, it
697+
* only handles the job embedded fence.
698+
*/
699+
static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
700+
{
701+
struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
677702

678-
if (!timer_pending(&ring->fence_drv.fallback_timer))
679-
amdgpu_fence_schedule_fallback(ring);
703+
if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
704+
amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
680705

681706
return true;
682707
}
@@ -692,19 +717,23 @@ static void amdgpu_fence_free(struct rcu_head *rcu)
692717
{
693718
struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
694719

695-
if (test_bit(AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT, &f->flags)) {
696-
/* free job if fence has a parent job */
697-
struct amdgpu_job *job;
698-
699-
job = container_of(f, struct amdgpu_job, hw_fence);
700-
kfree(job);
701-
} else {
702720
/* free fence_slab if it's separated fence*/
703-
struct amdgpu_fence *fence;
721+
kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f));
722+
}
704723

705-
fence = to_amdgpu_fence(f);
706-
kmem_cache_free(amdgpu_fence_slab, fence);
707-
}
724+
/**
725+
* amdgpu_job_fence_free - free up the job with embedded fence
726+
*
727+
* @rcu: RCU callback head
728+
*
729+
* Free up the job with embedded fence after the RCU grace period.
730+
*/
731+
static void amdgpu_job_fence_free(struct rcu_head *rcu)
732+
{
733+
struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
734+
735+
/* free job if fence has a parent job */
736+
kfree(container_of(f, struct amdgpu_job, hw_fence));
708737
}
709738

710739
/**
@@ -720,13 +749,32 @@ static void amdgpu_fence_release(struct dma_fence *f)
720749
call_rcu(&f->rcu, amdgpu_fence_free);
721750
}
722751

752+
/**
753+
* amdgpu_job_fence_release - callback that job embedded fence can be freed
754+
*
755+
* @f: fence
756+
*
757+
* This is the simliar function with amdgpu_fence_release above, it
758+
* only handles the job embedded fence.
759+
*/
760+
static void amdgpu_job_fence_release(struct dma_fence *f)
761+
{
762+
call_rcu(&f->rcu, amdgpu_job_fence_free);
763+
}
764+
723765
static const struct dma_fence_ops amdgpu_fence_ops = {
724766
.get_driver_name = amdgpu_fence_get_driver_name,
725767
.get_timeline_name = amdgpu_fence_get_timeline_name,
726768
.enable_signaling = amdgpu_fence_enable_signaling,
727769
.release = amdgpu_fence_release,
728770
};
729771

772+
static const struct dma_fence_ops amdgpu_job_fence_ops = {
773+
.get_driver_name = amdgpu_fence_get_driver_name,
774+
.get_timeline_name = amdgpu_job_fence_get_timeline_name,
775+
.enable_signaling = amdgpu_job_fence_enable_signaling,
776+
.release = amdgpu_job_fence_release,
777+
};
730778

731779
/*
732780
* Fence debugfs

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ enum amdgpu_ring_priority_level {
5353
#define AMDGPU_FENCE_FLAG_INT (1 << 1)
5454
#define AMDGPU_FENCE_FLAG_TC_WB_ONLY (1 << 2)
5555

56-
/* fence flag bit to indicate the face is embedded in job*/
57-
#define AMDGPU_FENCE_FLAG_EMBED_IN_JOB_BIT (DMA_FENCE_FLAG_USER_BITS + 1)
58-
5956
#define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)
6057

6158
#define AMDGPU_IB_POOL_SIZE (1024 * 1024)
@@ -114,6 +111,7 @@ struct amdgpu_fence_driver {
114111
struct dma_fence **fences;
115112
};
116113

114+
void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
117115
void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
118116

119117
int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,

0 commit comments

Comments
 (0)