2023-12-14 08:11:37

by Fabio M. De Francesco

[permalink] [raw]
Subject: [PATCH] mm/memory: Replace kmap() with kmap_local_page()

From: "Fabio M. De Francesco" <[email protected]>

kmap() has been deprecated in favor of kmap_local_page().

Therefore, replace kmap() with kmap_local_page() in mm/memory.c.

There are two main problems with kmap(): (1) It comes with an overhead as
the mapping space is restricted and protected by a global lock for
synchronization and (2) it also requires global TLB invalidation when the
kmap’s pool wraps and it might block when the mapping space is fully
utilized until a slot becomes available.

With kmap_local_page() the mappings are per thread, CPU local, can take
page-faults, and can be called from any context (including interrupts).
It is faster than kmap() in kernels with HIGHMEM enabled. The tasks can
be preempted and, when they are scheduled to run again, the kernel
virtual addresses are restored and still valid.

Obviously, thread locality implies that the kernel virtual addresses
returned by kmap_local_page() are only valid in the context of the
callers (i.e., they cannot be handed to other threads).

The use of kmap_local_page() in mm/memory.c does not break the
above-mentioned assumption, so it is allowed and preferred.

Cc: Ira Weiny <[email protected]>
Signed-off-by: Fabio M. De Francesco <[email protected]>
---
mm/memory.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 7d9f6b685032..88377a107fbe 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5852,7 +5852,7 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
if (bytes > PAGE_SIZE-offset)
bytes = PAGE_SIZE-offset;

- maddr = kmap(page);
+ maddr = kmap_local_page(page);
if (write) {
copy_to_user_page(vma, page, addr,
maddr + offset, buf, bytes);
@@ -5861,8 +5861,7 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
copy_from_user_page(vma, page, addr,
buf, maddr + offset, bytes);
}
- kunmap(page);
- put_page(page);
+ unmap_and_put_page(page, maddr);
}
len -= bytes;
buf += bytes;
--
2.43.0


2023-12-14 19:48:56

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] mm/memory: Replace kmap() with kmap_local_page()

> From: "Fabio M. De Francesco" <[email protected]>
> ...
> From: "Fabio M. De Francesco" <[email protected]>
>
> ...
>
> Signed-off-by: Fabio M. De Francesco <[email protected]>

We get a complaint from checkpatch (and possibly from Stephen Rothwell)
that the From: and Signed-off-by: email addresses differ.

I tentatively rewrote your explicit From: to @linux.intel.com, which
may have been unwelcome. What can we do here?


2023-12-15 08:41:25

by Fabio M. De Francesco

[permalink] [raw]
Subject: Re: [PATCH] mm/memory: Replace kmap() with kmap_local_page()

On Thursday, 14 December 2023 20:47:47 CET Andrew Morton wrote:
> > From: "Fabio M. De Francesco" <[email protected]>
> > ...
> > From: "Fabio M. De Francesco" <[email protected]>
> >
> > ...
> >
> > Signed-off-by: Fabio M. De Francesco
> > <[email protected]>
> We get a complaint from checkpatch (and possibly from Stephen Rothwell)
> that the From: and Signed-off-by: email addresses differ.
>
> I tentatively rewrote your explicit From: to @linux.intel.com, which
> may have been unwelcome. What can we do here?

I have two email addresses and sometimes some confusion arises while
configuring. Please discard this patch. I'm going to send another to replace
this.

Thanks,

Fabio





2023-12-15 16:24:19

by Fabio M. De Francesco

[permalink] [raw]
Subject: Re: [PATCH] mm/memory: Replace kmap() with kmap_local_page()

On Friday, 15 December 2023 09:40:33 CET Fabio M. De Francesco wrote:
> On Thursday, 14 December 2023 20:47:47 CET Andrew Morton wrote:
> >
> > [skip]
> >
> > I tentatively rewrote your explicit From: to @linux.intel.com, which
> > may have been unwelcome. What can we do here?
>
Sorry, in my last message I asked you to discard this patch and sent another
one but I hadn't yet understood that you rewrote and accepted it. My English
is still not good enough. Thanks for rewriting it. I'm perfectly fine with your
decision to rewrite it to @linux.intel.com.

Fabio