Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-gpio@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-rdma@vger.kernel.org, kvm@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org,
	netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: [PATCH 04/12] bpf: use __anon_inode_getfd
Date: Fri,  8 May 2020 17:36:26 +0200	[thread overview]
Message-ID: <20200508153634.249933-5-hch@lst.de> (raw)
In-Reply-To: <20200508153634.249933-1-hch@lst.de>

Use __anon_inode_getfd instead of opencoding the logic using
get_unused_fd_flags + anon_inode_getfile.  Also switch the
bpf_link_new_file calling conventions to match __anon_inode_getfd.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 include/linux/bpf.h  |  2 +-
 kernel/bpf/cgroup.c  |  6 +++---
 kernel/bpf/syscall.c | 31 +++++++++----------------------
 3 files changed, 13 insertions(+), 26 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index fd2b2322412d7..539649fe8b885 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1103,7 +1103,7 @@ void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
 void bpf_link_inc(struct bpf_link *link);
 void bpf_link_put(struct bpf_link *link);
 int bpf_link_new_fd(struct bpf_link *link);
-struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
+int bpf_link_new_file(struct bpf_link *link, struct file **link_file);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index cb305e71e7deb..8605287adcd9e 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -836,10 +836,10 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	link->cgroup = cgrp;
 	link->type = attr->link_create.attach_type;
 
-	link_file = bpf_link_new_file(&link->link, &link_fd);
-	if (IS_ERR(link_file)) {
+	link_fd = bpf_link_new_file(&link->link, &link_file);
+	if (link_fd < 0) {
 		kfree(link);
-		err = PTR_ERR(link_file);
+		err = link_fd;
 		goto out_put_cgroup;
 	}
 
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 64783da342020..cb2364e17423c 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2307,23 +2307,10 @@ int bpf_link_new_fd(struct bpf_link *link)
  * complicated and expensive operations and should be delayed until all the fd
  * reservation and anon_inode creation succeeds.
  */
-struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd)
+int bpf_link_new_file(struct bpf_link *link, struct file **file)
 {
-	struct file *file;
-	int fd;
-
-	fd = get_unused_fd_flags(O_CLOEXEC);
-	if (fd < 0)
-		return ERR_PTR(fd);
-
-	file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
-	if (IS_ERR(file)) {
-		put_unused_fd(fd);
-		return file;
-	}
-
-	*reserved_fd = fd;
-	return file;
+	return __anon_inode_getfd("bpf_link", &bpf_link_fops, link, O_CLOEXEC,
+			file);
 }
 
 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
@@ -2406,10 +2393,10 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog)
 	}
 	bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
 
-	link_file = bpf_link_new_file(&link->link, &link_fd);
-	if (IS_ERR(link_file)) {
+	link_fd = bpf_link_new_file(&link->link, &link_file);
+	if (link_fd < 0) {
 		kfree(link);
-		err = PTR_ERR(link_file);
+		err = link_fd;
 		goto out_put_prog;
 	}
 
@@ -2520,10 +2507,10 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
 	bpf_link_init(&link->link, &bpf_raw_tp_lops, prog);
 	link->btp = btp;
 
-	link_file = bpf_link_new_file(&link->link, &link_fd);
-	if (IS_ERR(link_file)) {
+	link_fd = bpf_link_new_file(&link->link, &link_file);
+	if (link_fd < 0) {
 		kfree(link);
-		err = PTR_ERR(link_file);
+		err = link_fd;
 		goto out_put_btp;
 	}
 
-- 
2.26.2


  parent reply	other threads:[~2020-05-08 15:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-08 15:36 Add a __anon_inode_getfd helper Christoph Hellwig
2020-05-08 15:36 ` [PATCH 01/12] fd: add a new " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 02/12] kvm: use __anon_inode_getfd Christoph Hellwig
2020-05-08 15:36 ` [PATCH 03/12] pidfd: " Christoph Hellwig
2020-05-08 15:36 ` Christoph Hellwig [this message]
2020-05-08 17:32   ` [PATCH 04/12] bpf: " Andrii Nakryiko
2020-05-08 15:36 ` [PATCH 05/12] io_uring: " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 06/12] eventpoll: " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 07/12] eventfd: " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 08/12] vfio: " Christoph Hellwig
2020-05-08 15:55   ` Alex Williamson
2020-05-08 15:36 ` [PATCH 09/12] rdma: " Christoph Hellwig
2020-05-08 19:52   ` Jason Gunthorpe
2020-05-08 15:36 ` [PATCH 10/12] drm_syncobj: " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 11/12] gpiolib: " Christoph Hellwig
2020-05-08 15:36 ` [PATCH 12/12] vtpm_proxy: " Christoph Hellwig

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=20200508153634.249933-5-hch@lst.de \
    --to=hch@lst.de \
    --cc=bpf@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=io-uring@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --subject='Re: [PATCH 04/12] bpf: use __anon_inode_getfd' \
    /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

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).