OSDN Git Service

mm/mmu_notifiers: add a get/put scheme for the registration
authorJason Gunthorpe <jgg@mellanox.com>
Tue, 6 Aug 2019 23:15:40 +0000 (20:15 -0300)
committerJason Gunthorpe <jgg@mellanox.com>
Fri, 16 Aug 2019 15:02:52 +0000 (12:02 -0300)
commit2c7933f53f6bff7656e3324ca1a04e478bdc57c1
treec74caf61c703e404f9d0b3f8df7cc1fe4944ee3c
parent70df291bf81ffda47ff84e6e2da4fbe21f95a861
mm/mmu_notifiers: add a get/put scheme for the registration

Many places in the kernel have a flow where userspace will create some
object and that object will need to connect to the subsystem's
mmu_notifier subscription for the duration of its lifetime.

In this case the subsystem is usually tracking multiple mm_structs and it
is difficult to keep track of what struct mmu_notifier's have been
allocated for what mm's.

Since this has been open coded in a variety of exciting ways, provide core
functionality to do this safely.

This approach uses the struct mmu_notifier_ops * as a key to determine if
the subsystem has a notifier registered on the mm or not. If there is a
registration then the existing notifier struct is returned, otherwise the
ops->alloc_notifiers() is used to create a new per-subsystem notifier for
the mm.

The destroy side incorporates an async call_srcu based destruction which
will avoid bugs in the callers such as commit 6d7c3cde93c1 ("mm/hmm: fix
use after free with struct hmm in the mmu notifiers").

Since we are inside the mmu notifier core locking is fairly simple, the
allocation uses the same approach as for mmu_notifier_mm, the write side
of the mmap_sem makes everything deterministic and we only need to do
hlist_add_head_rcu() under the mm_take_all_locks(). The new users count
and the discoverability in the hlist is fully serialized by the
mmu_notifier_mm->lock.

Link: https://lore.kernel.org/r/20190806231548.25242-4-jgg@ziepe.ca
Co-developed-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Christoph Hellwig <hch@infradead.org>
Reviewed-by: Ralph Campbell <rcampbell@nvidia.com>
Tested-by: Ralph Campbell <rcampbell@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
include/linux/mmu_notifier.h
mm/mmu_notifier.c