diff mbox series

[for-next,v8,3/6] RDMA/rxe: Add page invalidation support

Message ID 20241009015903.801987-4-matsuda-daisuke@fujitsu.com (mailing list archive)
State New
Headers show
Series On-Demand Paging on SoftRoCE | expand

Commit Message

Daisuke Matsuda (Fujitsu) Oct. 9, 2024, 1:59 a.m. UTC
On page invalidation, an MMU notifier callback is invoked to unmap DMA
addresses and update the driver page table(umem_odp->dma_list). It also
sets the corresponding entries in MR xarray to NULL to prevent any access.
The callback is registered when an ODP-enabled MR is created.

Signed-off-by: Daisuke Matsuda <matsuda-daisuke@fujitsu.com>
---
 drivers/infiniband/sw/rxe/Makefile  |  2 +
 drivers/infiniband/sw/rxe/rxe_odp.c | 57 +++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 drivers/infiniband/sw/rxe/rxe_odp.c

Comments

Zhu Yanjun Oct. 13, 2024, 6:15 a.m. UTC | #1
在 2024/10/9 9:59, Daisuke Matsuda 写道:
> On page invalidation, an MMU notifier callback is invoked to unmap DMA
> addresses and update the driver page table(umem_odp->dma_list). It also
> sets the corresponding entries in MR xarray to NULL to prevent any access.
> The callback is registered when an ODP-enabled MR is created.
> 
> Signed-off-by: Daisuke Matsuda <matsuda-daisuke@fujitsu.com>
> ---
>   drivers/infiniband/sw/rxe/Makefile  |  2 +
>   drivers/infiniband/sw/rxe/rxe_odp.c | 57 +++++++++++++++++++++++++++++
>   2 files changed, 59 insertions(+)
>   create mode 100644 drivers/infiniband/sw/rxe/rxe_odp.c
> 
> diff --git a/drivers/infiniband/sw/rxe/Makefile b/drivers/infiniband/sw/rxe/Makefile
> index 5395a581f4bb..93134f1d1d0c 100644
> --- a/drivers/infiniband/sw/rxe/Makefile
> +++ b/drivers/infiniband/sw/rxe/Makefile
> @@ -23,3 +23,5 @@ rdma_rxe-y := \
>   	rxe_task.o \
>   	rxe_net.o \
>   	rxe_hw_counters.o
> +
> +rdma_rxe-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += rxe_odp.o
> diff --git a/drivers/infiniband/sw/rxe/rxe_odp.c b/drivers/infiniband/sw/rxe/rxe_odp.c
> new file mode 100644
> index 000000000000..ea55b79be0c6
> --- /dev/null
> +++ b/drivers/infiniband/sw/rxe/rxe_odp.c
> @@ -0,0 +1,57 @@
> +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
> +/*
> + * Copyright (c) 2022-2023 Fujitsu Ltd. All rights reserved.
> + */
> +
> +#include <linux/hmm.h>
> +
> +#include <rdma/ib_umem_odp.h>
> +
> +#include "rxe.h"
> +
> +static void rxe_mr_unset_xarray(struct rxe_mr *mr, unsigned long start,
> +				unsigned long end)
> +{
> +	unsigned long upper = rxe_mr_iova_to_index(mr, end - 1);
> +	unsigned long lower = rxe_mr_iova_to_index(mr, start);
> +	void *entry;
> +
> +	XA_STATE(xas, &mr->page_list, lower);
> +
> +	/* make elements in xarray NULL */
> +	xas_lock(&xas);
> +	xas_for_each(&xas, entry, upper)
> +		xas_store(&xas, NULL);
> +	xas_unlock(&xas);
> +}
> +
> +static bool rxe_ib_invalidate_range(struct mmu_interval_notifier *mni,
> +				    const struct mmu_notifier_range *range,
> +				    unsigned long cur_seq)
> +{
> +	struct ib_umem_odp *umem_odp =
> +		container_of(mni, struct ib_umem_odp, notifier);
> +	struct rxe_mr *mr = umem_odp->private;
> +	unsigned long start, end;
> +
> +	if (!mmu_notifier_range_blockable(range))
> +		return false;
> +
> +	mutex_lock(&umem_odp->umem_mutex);

guard(mutex)(&umem_odp->umem_mutex);

It seems that the above is more popular.

Zhu Yanjun
> +	mmu_interval_set_seq(mni, cur_seq);
> +
> +	start = max_t(u64, ib_umem_start(umem_odp), range->start);
> +	end = min_t(u64, ib_umem_end(umem_odp), range->end);
> +
> +	rxe_mr_unset_xarray(mr, start, end);
> +
> +	/* update umem_odp->dma_list */
> +	ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
> +
> +	mutex_unlock(&umem_odp->umem_mutex);
> +	return true;
> +}
> +
> +const struct mmu_interval_notifier_ops rxe_mn_ops = {
> +	.invalidate = rxe_ib_invalidate_range,
> +};
Daisuke Matsuda (Fujitsu) Oct. 28, 2024, 7:25 a.m. UTC | #2
On Sun, Oct 13, 2024 3:16 PM Zhu Yanjun wrote:
> 在 2024/10/9 9:59, Daisuke Matsuda 写道:
> > On page invalidation, an MMU notifier callback is invoked to unmap DMA
> > addresses and update the driver page table(umem_odp->dma_list). It also
> > sets the corresponding entries in MR xarray to NULL to prevent any access.
> > The callback is registered when an ODP-enabled MR is created.
> >
> > Signed-off-by: Daisuke Matsuda <matsuda-daisuke@fujitsu.com>
> > ---
> >   drivers/infiniband/sw/rxe/Makefile  |  2 +
> >   drivers/infiniband/sw/rxe/rxe_odp.c | 57 +++++++++++++++++++++++++++++
> >   2 files changed, 59 insertions(+)
> >   create mode 100644 drivers/infiniband/sw/rxe/rxe_odp.c
> >
> > diff --git a/drivers/infiniband/sw/rxe/Makefile b/drivers/infiniband/sw/rxe/Makefile
> > index 5395a581f4bb..93134f1d1d0c 100644
> > --- a/drivers/infiniband/sw/rxe/Makefile
> > +++ b/drivers/infiniband/sw/rxe/Makefile
> > @@ -23,3 +23,5 @@ rdma_rxe-y := \
> >   	rxe_task.o \
> >   	rxe_net.o \
> >   	rxe_hw_counters.o
> > +
> > +rdma_rxe-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += rxe_odp.o
> > diff --git a/drivers/infiniband/sw/rxe/rxe_odp.c b/drivers/infiniband/sw/rxe/rxe_odp.c
> > new file mode 100644
> > index 000000000000..ea55b79be0c6
> > --- /dev/null
> > +++ b/drivers/infiniband/sw/rxe/rxe_odp.c
> > @@ -0,0 +1,57 @@
> > +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
> > +/*
> > + * Copyright (c) 2022-2023 Fujitsu Ltd. All rights reserved.
> > + */
> > +
> > +#include <linux/hmm.h>
> > +
> > +#include <rdma/ib_umem_odp.h>
> > +
> > +#include "rxe.h"
> > +
> > +static void rxe_mr_unset_xarray(struct rxe_mr *mr, unsigned long start,
> > +				unsigned long end)
> > +{
> > +	unsigned long upper = rxe_mr_iova_to_index(mr, end - 1);
> > +	unsigned long lower = rxe_mr_iova_to_index(mr, start);
> > +	void *entry;
> > +
> > +	XA_STATE(xas, &mr->page_list, lower);
> > +
> > +	/* make elements in xarray NULL */
> > +	xas_lock(&xas);
> > +	xas_for_each(&xas, entry, upper)
> > +		xas_store(&xas, NULL);
> > +	xas_unlock(&xas);
> > +}
> > +
> > +static bool rxe_ib_invalidate_range(struct mmu_interval_notifier *mni,
> > +				    const struct mmu_notifier_range *range,
> > +				    unsigned long cur_seq)
> > +{
> > +	struct ib_umem_odp *umem_odp =
> > +		container_of(mni, struct ib_umem_odp, notifier);
> > +	struct rxe_mr *mr = umem_odp->private;
> > +	unsigned long start, end;
> > +
> > +	if (!mmu_notifier_range_blockable(range))
> > +		return false;
> > +
> > +	mutex_lock(&umem_odp->umem_mutex);
> 
> guard(mutex)(&umem_odp->umem_mutex);
> 
> It seems that the above is more popular.

Thanks for the comment.

I have no objection to your suggestion since the increasing number of
kernel components use "guard(mutex)" syntax these days, but I would rather
suggest making the change to the whole infiniband subsystem at once because
there are multiple mutex lock/unlock pairs to be converted.

Regards,
Daisuke Matsuda

> 
> Zhu Yanjun
> > +	mmu_interval_set_seq(mni, cur_seq);
> > +
> > +	start = max_t(u64, ib_umem_start(umem_odp), range->start);
> > +	end = min_t(u64, ib_umem_end(umem_odp), range->end);
> > +
> > +	rxe_mr_unset_xarray(mr, start, end);
> > +
> > +	/* update umem_odp->dma_list */
> > +	ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
> > +
> > +	mutex_unlock(&umem_odp->umem_mutex);
> > +	return true;
> > +}
> > +
> > +const struct mmu_interval_notifier_ops rxe_mn_ops = {
> > +	.invalidate = rxe_ib_invalidate_range,
> > +};
Zhu Yanjun Oct. 28, 2024, 8:26 p.m. UTC | #3
在 2024/10/28 8:25, Daisuke Matsuda (Fujitsu) 写道:
> On Sun, Oct 13, 2024 3:16 PM Zhu Yanjun wrote:
>> 在 2024/10/9 9:59, Daisuke Matsuda 写道:
>>> On page invalidation, an MMU notifier callback is invoked to unmap DMA
>>> addresses and update the driver page table(umem_odp->dma_list). It also
>>> sets the corresponding entries in MR xarray to NULL to prevent any access.
>>> The callback is registered when an ODP-enabled MR is created.
>>>
>>> Signed-off-by: Daisuke Matsuda <matsuda-daisuke@fujitsu.com>
>>> ---
>>>    drivers/infiniband/sw/rxe/Makefile  |  2 +
>>>    drivers/infiniband/sw/rxe/rxe_odp.c | 57 +++++++++++++++++++++++++++++
>>>    2 files changed, 59 insertions(+)
>>>    create mode 100644 drivers/infiniband/sw/rxe/rxe_odp.c
>>>
>>> diff --git a/drivers/infiniband/sw/rxe/Makefile b/drivers/infiniband/sw/rxe/Makefile
>>> index 5395a581f4bb..93134f1d1d0c 100644
>>> --- a/drivers/infiniband/sw/rxe/Makefile
>>> +++ b/drivers/infiniband/sw/rxe/Makefile
>>> @@ -23,3 +23,5 @@ rdma_rxe-y := \
>>>    	rxe_task.o \
>>>    	rxe_net.o \
>>>    	rxe_hw_counters.o
>>> +
>>> +rdma_rxe-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += rxe_odp.o
>>> diff --git a/drivers/infiniband/sw/rxe/rxe_odp.c b/drivers/infiniband/sw/rxe/rxe_odp.c
>>> new file mode 100644
>>> index 000000000000..ea55b79be0c6
>>> --- /dev/null
>>> +++ b/drivers/infiniband/sw/rxe/rxe_odp.c
>>> @@ -0,0 +1,57 @@
>>> +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
>>> +/*
>>> + * Copyright (c) 2022-2023 Fujitsu Ltd. All rights reserved.
>>> + */
>>> +
>>> +#include <linux/hmm.h>
>>> +
>>> +#include <rdma/ib_umem_odp.h>
>>> +
>>> +#include "rxe.h"
>>> +
>>> +static void rxe_mr_unset_xarray(struct rxe_mr *mr, unsigned long start,
>>> +				unsigned long end)
>>> +{
>>> +	unsigned long upper = rxe_mr_iova_to_index(mr, end - 1);
>>> +	unsigned long lower = rxe_mr_iova_to_index(mr, start);
>>> +	void *entry;
>>> +
>>> +	XA_STATE(xas, &mr->page_list, lower);
>>> +
>>> +	/* make elements in xarray NULL */
>>> +	xas_lock(&xas);
>>> +	xas_for_each(&xas, entry, upper)
>>> +		xas_store(&xas, NULL);
>>> +	xas_unlock(&xas);
>>> +}
>>> +
>>> +static bool rxe_ib_invalidate_range(struct mmu_interval_notifier *mni,
>>> +				    const struct mmu_notifier_range *range,
>>> +				    unsigned long cur_seq)
>>> +{
>>> +	struct ib_umem_odp *umem_odp =
>>> +		container_of(mni, struct ib_umem_odp, notifier);
>>> +	struct rxe_mr *mr = umem_odp->private;
>>> +	unsigned long start, end;
>>> +
>>> +	if (!mmu_notifier_range_blockable(range))
>>> +		return false;
>>> +
>>> +	mutex_lock(&umem_odp->umem_mutex);
>>
>> guard(mutex)(&umem_odp->umem_mutex);
>>
>> It seems that the above is more popular.
> 
> Thanks for the comment.
> 
> I have no objection to your suggestion since the increasing number of
> kernel components use "guard(mutex)" syntax these days, but I would rather
> suggest making the change to the whole infiniband subsystem at once because
> there are multiple mutex lock/unlock pairs to be converted.

If you want to make the such changes to the whole infiniband subsystem, 
I am fine with it.

The "guard(mutex)" is used in the following patch.

https://patchwork.kernel.org/project/linux-rdma/patch/20241009210048.4122518-1-bvanassche@acm.org/

Zhu Yanjun

> 
> Regards,
> Daisuke Matsuda
> 
>>
>> Zhu Yanjun
>>> +	mmu_interval_set_seq(mni, cur_seq);
>>> +
>>> +	start = max_t(u64, ib_umem_start(umem_odp), range->start);
>>> +	end = min_t(u64, ib_umem_end(umem_odp), range->end);
>>> +
>>> +	rxe_mr_unset_xarray(mr, start, end);
>>> +
>>> +	/* update umem_odp->dma_list */
>>> +	ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
>>> +
>>> +	mutex_unlock(&umem_odp->umem_mutex);
>>> +	return true;
>>> +}
>>> +
>>> +const struct mmu_interval_notifier_ops rxe_mn_ops = {
>>> +	.invalidate = rxe_ib_invalidate_range,
>>> +};
>
diff mbox series

Patch

diff --git a/drivers/infiniband/sw/rxe/Makefile b/drivers/infiniband/sw/rxe/Makefile
index 5395a581f4bb..93134f1d1d0c 100644
--- a/drivers/infiniband/sw/rxe/Makefile
+++ b/drivers/infiniband/sw/rxe/Makefile
@@ -23,3 +23,5 @@  rdma_rxe-y := \
 	rxe_task.o \
 	rxe_net.o \
 	rxe_hw_counters.o
+
+rdma_rxe-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += rxe_odp.o
diff --git a/drivers/infiniband/sw/rxe/rxe_odp.c b/drivers/infiniband/sw/rxe/rxe_odp.c
new file mode 100644
index 000000000000..ea55b79be0c6
--- /dev/null
+++ b/drivers/infiniband/sw/rxe/rxe_odp.c
@@ -0,0 +1,57 @@ 
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright (c) 2022-2023 Fujitsu Ltd. All rights reserved.
+ */
+
+#include <linux/hmm.h>
+
+#include <rdma/ib_umem_odp.h>
+
+#include "rxe.h"
+
+static void rxe_mr_unset_xarray(struct rxe_mr *mr, unsigned long start,
+				unsigned long end)
+{
+	unsigned long upper = rxe_mr_iova_to_index(mr, end - 1);
+	unsigned long lower = rxe_mr_iova_to_index(mr, start);
+	void *entry;
+
+	XA_STATE(xas, &mr->page_list, lower);
+
+	/* make elements in xarray NULL */
+	xas_lock(&xas);
+	xas_for_each(&xas, entry, upper)
+		xas_store(&xas, NULL);
+	xas_unlock(&xas);
+}
+
+static bool rxe_ib_invalidate_range(struct mmu_interval_notifier *mni,
+				    const struct mmu_notifier_range *range,
+				    unsigned long cur_seq)
+{
+	struct ib_umem_odp *umem_odp =
+		container_of(mni, struct ib_umem_odp, notifier);
+	struct rxe_mr *mr = umem_odp->private;
+	unsigned long start, end;
+
+	if (!mmu_notifier_range_blockable(range))
+		return false;
+
+	mutex_lock(&umem_odp->umem_mutex);
+	mmu_interval_set_seq(mni, cur_seq);
+
+	start = max_t(u64, ib_umem_start(umem_odp), range->start);
+	end = min_t(u64, ib_umem_end(umem_odp), range->end);
+
+	rxe_mr_unset_xarray(mr, start, end);
+
+	/* update umem_odp->dma_list */
+	ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
+
+	mutex_unlock(&umem_odp->umem_mutex);
+	return true;
+}
+
+const struct mmu_interval_notifier_ops rxe_mn_ops = {
+	.invalidate = rxe_ib_invalidate_range,
+};