All of lore.kernel.org
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Thomas Gleixner <tglx@linutronix.de>,
	Zhangfei Gao <zhangfei.gao@foxmail.com>,
	Fenghua Yu <fenghua.yu@intel.com>,
	Jean-Philippe Brucker <jean-philippe@linaro.org>,
	Jacob Pan <jacob.jun.pan@linux.intel.com>,
	Dave Hansen <dave.hansen@intel.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	"the arch/x86 maintainers" <x86@kernel.org>
Subject: Re: [GIT pull] core/urgent for v5.18-rc6
Date: Sun, 8 May 2022 11:36:23 -0700	[thread overview]
Message-ID: <CAHk-=wiEC-Jdx4iTP5NQ0a8stsU+SL0DjbTwhQ+mvPGNBXyMag@mail.gmail.com> (raw)
In-Reply-To: <CAHk-=whj6m0B9ys4XyE4AAPPoXE7XpBtxL7mD3Rk6EYX1=Ugng@mail.gmail.com>

On Sun, May 8, 2022 at 11:09 AM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> Looks like it is
>
>  ->(*sva_bind)()
>     -> intel_svm_bind_mm()
>       -> mmu_notifier_register(&svm->notifier, mm)
>
> and yes, the mmu notifiers annoyingly end up doing an mmgrab [..]

Side note: quite independently of this mmgrab issue, I think the code
in question is *very* suspect and horrendously fragile.

In particular, the code ends up being called through things like this:

     handle = iommu_sva_bind_device(uacce->parent, current->mm, NULL);

and then that Intel svm.c code does this:

                svm->pasid = mm->pasid;
                svm->mm = mm;
                svm->flags = flags;

and saves off that mm pointer in the 'svm' structure.

AND IT NEVER TAKES ANY REFERENCE TO IT AT ALL!

It then does

        mm = svm->mm;

later at some unspecified time, and the 'mm' might long since have died.

In other words, the code works almost by accident - the only user of
the 'mm' pointer seems to be that mmu_notifier_register() thing, so it
basically treats the 'struct mm_struct' as something as a random
cookie.

And yes, the mmu notifiers do then take that mmgrab reference to the
mm, so it all works.

But it sure looks horrendously ugly. Saving off a 'struct mm_struct'
pointer with having basically an accidental reference to it is WRONG.

In fact, it will save off that pointer whether it then actually does
the mmu_notifier thing on it, because the code actually does

                [...]
                svm->mm = mm;
                svm->flags = flags;
                INIT_LIST_HEAD_RCU(&svm->devs);

                if (!(flags & SVM_FLAG_SUPERVISOR_MODE)) {
                        svm->notifier.ops = &intel_mmuops;
                        ret = mmu_notifier_register(&svm->notifier, mm);
                [...]

so that mmu_notifier_register() call is conditional. On the freeing
path, it then uses that "svm->notifier.ops" pointer as a "did we
register this thing or not" flag, so again - it all technically
*works*, but this is all horrendously ugly and wrong on so many
levels, keeping pointers around with very dubious reference counting
indeed.

               Linus

  reply	other threads:[~2022-05-08 19:02 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-08 12:05 [GIT pull] core/urgent for v5.18-rc6 Thomas Gleixner
2022-05-08 12:05 ` [GIT pull] irq/urgent " Thomas Gleixner
2022-05-08 18:40   ` pr-tracker-bot
2022-05-08 12:05 ` [GIT pull] locking/urgent " Thomas Gleixner
2022-05-08 18:40   ` pr-tracker-bot
2022-05-08 12:05 ` [GIT pull] timers/urgent " Thomas Gleixner
2022-05-08 18:40   ` pr-tracker-bot
2022-05-08 12:07 ` [GIT pull] x86/urgent " Thomas Gleixner
2022-05-08 18:40   ` pr-tracker-bot
2022-05-08 18:00 ` [GIT pull] core/urgent " Linus Torvalds
2022-05-08 18:09   ` Linus Torvalds
2022-05-08 18:36     ` Linus Torvalds [this message]
2022-05-09 15:02       ` Jean-Philippe Brucker
2022-05-10 11:27   ` Link: tag and links to submission and reports (was: Re: [GIT pull] core/urgent for v5.18-rc6) Thorsten Leemhuis
2022-05-11  8:37     ` Borislav Petkov
2022-05-11 13:48       ` Michael Ellerman
2022-05-11 15:50       ` Theodore Ts'o
2022-05-11 15:55         ` Linus Torvalds
2022-05-11 19:35           ` Borislav Petkov
2022-05-12  8:43             ` Thorsten Leemhuis
2022-08-04  9:29               ` Borislav Petkov
2022-05-11 22:37           ` Theodore Ts'o
2022-05-08 18:40 ` [GIT pull] core/urgent for v5.18-rc6 pr-tracker-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHk-=wiEC-Jdx4iTP5NQ0a8stsU+SL0DjbTwhQ+mvPGNBXyMag@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=dave.hansen@intel.com \
    --cc=fenghua.yu@intel.com \
    --cc=jacob.jun.pan@linux.intel.com \
    --cc=jean-philippe@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=zhangfei.gao@foxmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.