Public

> -----Original Message-----
> From: Kim, Jonathan <[email protected]>
> Sent: Thursday, November 19, 2026 5:55 AM
> To: Zhang, Jesse(Jie) <[email protected]>; amd-
> [email protected]
> Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix
> <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
> Subject: RE: [PATCH 3/3] drm/amdkfd: don't keep the debugger pointer when
> DBG_TRAP_ENABLE fails
>
> Public
>
> > -----Original Message-----
> > From: Jesse Zhang <[email protected]>
> > Sent: Wednesday, November 18, 2026 12:50 AM
> > To: [email protected]
> > Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix
> > <[email protected]>; Kim, Jonathan <[email protected]>;
> Zhang,
> > Jesse(Jie) <[email protected]>
> > Subject: [PATCH 3/3] drm/amdkfd: don't keep the debugger pointer when
> > DBG_TRAP_ENABLE fails
> >
> > KFD_IOC_DBG_TRAP_ENABLE stores the caller in target->debugger_process
> > before calling kfd_dbg_trap_enable(), and nothing resets it when that
> > fails (-EALREADY, -ENODEV, -EBUSY, -EBADF for a bad dbg_fd, -EFAULT).
> > The permission check at the top of kfd_ioctl_set_debug_trap() accepts
> > any caller equal to target->debugger_process, so the caller keeps
> > debug access after it stopped ptracing the target. The pointer is also
> > not reference counted, so it dangles once the caller exits, and a
> > later successful ENABLE by the target itself increments
> > debugged_process_count of the freed process.
> >
> > On navi31, a parent that ptraces its child, tries to enable debugging
> > with dbg_fd = -1 (-EBADF) and then detaches can still enable debugging
> > on the child with a valid fd:
> >
> >   1. ptraced, ENABLE with dbg_fd=-1      -> -9 (Bad file descriptor)
> >   2. ptrace detached
> >   3. not ptraced, ENABLE with valid fd  -> 0 (success)
> >
> > Restore the previous debugger when enabling fails. Step 3 now fails
> > with -EPERM.
> >
> > Return -EALREADY for an already enabled session before touching the
> > pointer at all: the exit cleanup of the current debugger looks for its
> > targets through target->debugger_process without holding
> > target->mutex, so even a temporary change of the pointer could make it
> > skip the target and leave a dangling pointer behind.
> >
> > Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and
> > disable")
> > Signed-off-by: Jesse Zhang <[email protected]>
> > ---
> >  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 18 +++++++++++++++++-
> >  1 file changed, 17 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > index 3d2136adb62b..c42c30b459a3 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > @@ -3226,7 +3226,19 @@ static int kfd_ioctl_set_debug_trap(struct file
> > *filep, struct kfd_process *p, v
> >       }
> >
> >       switch (args->op) {
> > -     case KFD_IOC_DBG_TRAP_ENABLE:
> > +     case KFD_IOC_DBG_TRAP_ENABLE: {
>
> Any reason for the brace?
The brace is only for the local old_debugger. I'll drop both in v2

>
> > +             struct kfd_process *old_debugger =
> > + target->debugger_process;
> > +
> > +             /*
> > +              * Don't touch the debugger of a session that is already 
> > enabled,
> > +              * not even temporarily: its exit cleanup looks the pointer up
> > +              * without target->mutex.
> > +              */
>
> I'm not sure what you mean by this.  Did you mean the mutex of the caller
> process instead of target to avoid a race during process release?
Sorry for the confusion. I'll clarify the comment in v2.
I meant the exit of the debugger owning the existing session.
Its kfd_process_notifier_release_internal() checks target->debugger_process 
before taking target->mutex.
If another caller's ENABLE temporarily replaces that pointer, cleanup could 
miss the target, leaving a dangling pointer
when it is restored and the original debugger exits.

Returning -EALREADY before changing the pointer avoids this window.
holding the caller's mutex would cover this case, since the existing debugger's 
cleanup doesn't take that mutex

Jesse

> We should probably hold the mutex for the process if process != target here
> and drop the condition below.
> That way either only enable or release can happen (but not both at the same
> time) and a bad return of kfd_dbg_trap_enable (including -EALREADY) will
> cleanup properly with your change below.
>
> > +             if (target->debug_trap_enabled) {
> > +                     r = -EALREADY;
> > +                     break;
> > +             }
> > +
> >               if (target != p)
> >                       target->debugger_process = p;
> >
> > @@ -3236,8 +3248,12 @@ static int kfd_ioctl_set_debug_trap(struct file
> > *filep, struct kfd_process *p, v
> >                                       &args->enable.rinfo_size);
> >               if (!r)
> >                       target->exception_enable_mask = args-
> > >enable.exception_mask;
> > +             else
> > +                     /* the debugger check above trusts this pointer */
> > +                     target->debugger_process = old_debugger;
>
> This probably only works if you hold the process mutex if process != target
> during enable to avoid race with process release.
>
> Jon
>
> >
> >               break;
> > +     }
> >       case KFD_IOC_DBG_TRAP_DISABLE:
> >               r = kfd_dbg_trap_disable(target, true);
> >               break;
> > --
> > 2.50.1
>

Reply via email to