linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] net: core: Replace kmap() with kmap_local_page()
@ 2022-07-01 10:51 Fabio M. De Francesco
  0 siblings, 0 replies; only message in thread
From: Fabio M. De Francesco @ 2022-07-01 10:51 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, David Ahern, Pavel Begunkov, Wei Wang, Yangbo Lu,
	Richard Palethorpe, Thomas Gleixner, netdev, linux-kernel, bpf
  Cc: Fabio M. De Francesco, Ira Weiny

The use of kmap() is being deprecated in favor of kmap_local_page().

With kmap_local_page(), the mappings are per thread, CPU local and not
globally visible. Taking page faults is allowed. Furthermore, the mappings
can be acquired from any context (including interrupts).

Therefore, use kmap_local_page() in sock.c and datagram.c because these
mappings are per thread, CPU local, and not globally visible.

Actually this is an RFC because I'm not 100% sure that the mappings in
sock.c are not handed over to other contexts. Unfortunately I know very
little about this code. The fact that "page" is kmapped and then kunmapped
before exiting sock_send_page*() is not a guarantee of thread locality.
That "kernel_sendmsg*()" is a bit "suspicious".

Can anyone please confirm whether or not "kaddr" is handed over to other
contexts while the call sites might sleep between kmap() / kunmap()?

Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
---
 net/core/datagram.c | 4 ++--
 net/core/sock.c     | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/net/core/datagram.c b/net/core/datagram.c
index 50f4faeea76c..3a8fa210e1a1 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -438,14 +438,14 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
 		end = start + skb_frag_size(frag);
 		if ((copy = end - offset) > 0) {
 			struct page *page = skb_frag_page(frag);
-			u8 *vaddr = kmap(page);
+			u8 *vaddr = kmap_local_page(page);
 
 			if (copy > len)
 				copy = len;
 			n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
 					vaddr + skb_frag_off(frag) + offset - start,
 					copy, data, to);
-			kunmap(page);
+			kunmap_local(vaddr);
 			offset += n;
 			if (n != copy)
 				goto short_copy;
diff --git a/net/core/sock.c b/net/core/sock.c
index 2ff40dd0a7a6..12dd6ced62cf 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3155,11 +3155,11 @@ ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, siz
 	ssize_t res;
 	struct msghdr msg = {.msg_flags = flags};
 	struct kvec iov;
-	char *kaddr = kmap(page);
+	char *kaddr = kmap_local_page(page);
 	iov.iov_base = kaddr + offset;
 	iov.iov_len = size;
 	res = kernel_sendmsg(sock, &msg, &iov, 1, size);
-	kunmap(page);
+	kunmap_local(kaddr);
 	return res;
 }
 EXPORT_SYMBOL(sock_no_sendpage);
@@ -3170,12 +3170,12 @@ ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
 	ssize_t res;
 	struct msghdr msg = {.msg_flags = flags};
 	struct kvec iov;
-	char *kaddr = kmap(page);
+	char *kaddr = kmap_local_page(page);
 
 	iov.iov_base = kaddr + offset;
 	iov.iov_len = size;
 	res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
-	kunmap(page);
+	kunmap_local(kaddr);
 	return res;
 }
 EXPORT_SYMBOL(sock_no_sendpage_locked);
-- 
2.36.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-01 10:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-01 10:51 [RFC PATCH] net: core: Replace kmap() with kmap_local_page() Fabio M. De Francesco

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).