All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roberto Sassu <roberto.sassu@huaweicloud.com>
To: corbet@lwn.net, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org,
	sdf@google.com, haoluo@google.com, jolsa@kernel.org,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, dsahern@kernel.org, shuah@kernel.org,
	brauner@kernel.org
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org, ebiederm@xmission.com,
	mcgrof@kernel.org, Roberto Sassu <roberto.sassu@huawei.com>
Subject: [PATCH 1/5] usermode_driver: Introduce umd_send_recv() from bpfilter
Date: Fri, 17 Mar 2023 15:52:36 +0100	[thread overview]
Message-ID: <20230317145240.363908-2-roberto.sassu@huaweicloud.com> (raw)
In-Reply-To: <20230317145240.363908-1-roberto.sassu@huaweicloud.com>

From: Roberto Sassu <roberto.sassu@huawei.com>

Move bpfilter_send_req() to usermode_driver.c and rename it to
umd_send_recv(). Make the latter independent from the message format by
passing to it the pointers to the request and reply message buffers and the
respective lengths, and by merely doing read/write operations.

From umd_send_recv(), remove the call to __stop_umh() and returning
reply.status, which is message format-specific. Let the callers of
umd_send_recv(), such as bpfilter_process_sockopt(), call shutdown_umh()
and evaluate the reply. Consequently, remove __stop_umh(), since in
bpfilter_process_sockopt() the CONFIG_INET condition is always true.

In addition to the original bpfilter_send_req() implementation, support
partial reads and writes, so that callers are not limited by the length of
the message to send or receive. Currently, the pipe supports receiving
64 KB at once.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 include/linux/usermode_driver.h |  2 ++
 kernel/usermode_driver.c        | 47 ++++++++++++++++++++++++++++++-
 net/bpfilter/bpfilter_kern.c    | 50 +++++++++------------------------
 3 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/include/linux/usermode_driver.h b/include/linux/usermode_driver.h
index ad970416260..37b8d08cc3d 100644
--- a/include/linux/usermode_driver.h
+++ b/include/linux/usermode_driver.h
@@ -15,5 +15,7 @@ int umd_load_blob(struct umd_info *info, const void *data, size_t len);
 int umd_unload_blob(struct umd_info *info);
 int fork_usermode_driver(struct umd_info *info);
 void umd_cleanup_helper(struct umd_info *info);
+int umd_send_recv(struct umd_info *info, void *in, size_t in_len, void *out,
+		  size_t out_len);
 
 #endif /* __LINUX_USERMODE_DRIVER_H__ */
diff --git a/kernel/usermode_driver.c b/kernel/usermode_driver.c
index 8303f4c7ca7..cdbfaad99d7 100644
--- a/kernel/usermode_driver.c
+++ b/kernel/usermode_driver.c
@@ -188,4 +188,49 @@ int fork_usermode_driver(struct umd_info *info)
 }
 EXPORT_SYMBOL_GPL(fork_usermode_driver);
 
-
+/**
+ * umd_send_recv - send/receive a message through the pipe
+ * @info: user mode driver info
+ * @in: request message
+ * @in_len: size of @in
+ * @out: reply message
+ * @out_len: size of @out
+ *
+ * Send a message to the user space process through the created pipe and read
+ * the reply. Partial reads and writes are supported.
+ *
+ * Return: Zero on success, -EFAULT otherwise.
+ */
+int umd_send_recv(struct umd_info *info, void *in, size_t in_len, void *out,
+		  size_t out_len)
+{
+	loff_t pos, offset;
+	ssize_t n;
+
+	if (!info->tgid)
+		return -EFAULT;
+	pos = 0;
+	offset = 0;
+	while (in_len) {
+		n = kernel_write(info->pipe_to_umh, in + offset, in_len, &pos);
+		if (n <= 0) {
+			pr_err("write fail %zd\n", n);
+			return -EFAULT;
+		}
+		in_len -= n;
+		offset += n;
+	}
+	pos = 0;
+	offset = 0;
+	while (out_len) {
+		n = kernel_read(info->pipe_from_umh, out + offset, out_len, &pos);
+		if (n <= 0) {
+			pr_err("read fail %zd\n", n);
+			return -EFAULT;
+		}
+		out_len -= n;
+		offset += n;
+	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(umd_send_recv);
diff --git a/net/bpfilter/bpfilter_kern.c b/net/bpfilter/bpfilter_kern.c
index 422ec6e7ccf..17d4df5f8fe 100644
--- a/net/bpfilter/bpfilter_kern.c
+++ b/net/bpfilter/bpfilter_kern.c
@@ -25,40 +25,6 @@ static void shutdown_umh(void)
 	}
 }
 
-static void __stop_umh(void)
-{
-	if (IS_ENABLED(CONFIG_INET))
-		shutdown_umh();
-}
-
-static int bpfilter_send_req(struct mbox_request *req)
-{
-	struct mbox_reply reply;
-	loff_t pos = 0;
-	ssize_t n;
-
-	if (!bpfilter_ops.info.tgid)
-		return -EFAULT;
-	pos = 0;
-	n = kernel_write(bpfilter_ops.info.pipe_to_umh, req, sizeof(*req),
-			   &pos);
-	if (n != sizeof(*req)) {
-		pr_err("write fail %zd\n", n);
-		goto stop;
-	}
-	pos = 0;
-	n = kernel_read(bpfilter_ops.info.pipe_from_umh, &reply, sizeof(reply),
-			&pos);
-	if (n != sizeof(reply)) {
-		pr_err("read fail %zd\n", n);
-		goto stop;
-	}
-	return reply.status;
-stop:
-	__stop_umh();
-	return -EFAULT;
-}
-
 static int bpfilter_process_sockopt(struct sock *sk, int optname,
 				    sockptr_t optval, unsigned int optlen,
 				    bool is_set)
@@ -70,16 +36,27 @@ static int bpfilter_process_sockopt(struct sock *sk, int optname,
 		.addr		= (uintptr_t)optval.user,
 		.len		= optlen,
 	};
+	struct mbox_reply reply;
+	int err;
+
 	if (sockptr_is_kernel(optval)) {
 		pr_err("kernel access not supported\n");
 		return -EFAULT;
 	}
-	return bpfilter_send_req(&req);
+	err = umd_send_recv(&bpfilter_ops.info, &req, sizeof(req), &reply,
+			    sizeof(reply));
+	if (err) {
+		shutdown_umh();
+		return err;
+	}
+
+	return reply.status;
 }
 
 static int start_umh(void)
 {
 	struct mbox_request req = { .pid = current->pid };
+	struct mbox_reply reply;
 	int err;
 
 	/* fork usermode process */
@@ -89,7 +66,8 @@ static int start_umh(void)
 	pr_info("Loaded bpfilter_umh pid %d\n", pid_nr(bpfilter_ops.info.tgid));
 
 	/* health check that usermode process started correctly */
-	if (bpfilter_send_req(&req) != 0) {
+	if (umd_send_recv(&bpfilter_ops.info, &req, sizeof(req), &reply,
+			  sizeof(reply)) != 0 || reply.status != 0) {
 		shutdown_umh();
 		return -EFAULT;
 	}
-- 
2.25.1


  reply	other threads:[~2023-03-17 14:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-17 14:52 [PATCH 0/5] usermode_driver: Add management library and API Roberto Sassu
2023-03-17 14:52 ` Roberto Sassu [this message]
2023-03-17 14:52 ` [PATCH 2/5] usermode_driver_mgmt: Introduce management of user mode drivers Roberto Sassu
2023-03-17 14:52 ` [PATCH 3/5] bpfilter: Port to user mode driver management API Roberto Sassu
2023-03-17 14:52 ` [PATCH 4/5] selftests/umd_mgmt: Add selftests for UMD management library Roberto Sassu
2023-03-17 14:52 ` [PATCH 5/5] doc: Add documentation for the User Mode Driver " Roberto Sassu
2023-03-22 12:34   ` Bagas Sanjaya
2023-03-22  2:23 ` [PATCH 0/5] usermode_driver: Add management library and API Alexei Starovoitov
2023-03-22 12:07   ` Roberto Sassu
2023-03-22 22:27     ` Alexei Starovoitov
2023-03-23 13:36       ` Roberto Sassu
2023-03-25  2:54         ` Alexei Starovoitov
2023-03-27 11:27           ` Roberto Sassu

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=20230317145240.363908-2-roberto.sassu@huaweicloud.com \
    --to=roberto.sassu@huaweicloud.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=edumazet@google.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mcgrof@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=roberto.sassu@huawei.com \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yhs@fb.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.