Skip to content

Commit ef36b9a

Browse files
committed
Merge tag 'pull-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull vfs fget updates from Al Viro: "fget() to fdget() conversions" * tag 'pull-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: fuse_dev_ioctl(): switch to fdget() cgroup_get_from_fd(): switch to fdget_raw() bpf: switch to fdget_raw() build_mount_idmapped(): switch to fdget() kill the last remaining user of proc_ns_fget() SVM-SEV: convert the rest of fget() uses to fdget() in there convert sgx_set_attribute() to fdget()/fdput() convert setns(2) to fdget()/fdput()
2 parents 61d325d + 4a892c0 commit ef36b9a

10 files changed

Lines changed: 84 additions & 113 deletions

File tree

arch/x86/kernel/cpu/sgx/main.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -892,20 +892,19 @@ static struct miscdevice sgx_dev_provision = {
892892
int sgx_set_attribute(unsigned long *allowed_attributes,
893893
unsigned int attribute_fd)
894894
{
895-
struct file *file;
895+
struct fd f = fdget(attribute_fd);
896896

897-
file = fget(attribute_fd);
898-
if (!file)
897+
if (!f.file)
899898
return -EINVAL;
900899

901-
if (file->f_op != &sgx_provision_fops) {
902-
fput(file);
900+
if (f.file->f_op != &sgx_provision_fops) {
901+
fdput(f);
903902
return -EINVAL;
904903
}
905904

906905
*allowed_attributes |= SGX_ATTR_PROVISIONKEY;
907906

908-
fput(file);
907+
fdput(f);
909908
return 0;
910909
}
911910
EXPORT_SYMBOL_GPL(sgx_set_attribute);

arch/x86/kvm/svm/sev.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,18 +1767,20 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
17671767
{
17681768
struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
17691769
struct kvm_sev_info *src_sev, *cg_cleanup_sev;
1770-
struct file *source_kvm_file;
1770+
struct fd f = fdget(source_fd);
17711771
struct kvm *source_kvm;
17721772
bool charged = false;
17731773
int ret;
17741774

1775-
source_kvm_file = fget(source_fd);
1776-
if (!file_is_kvm(source_kvm_file)) {
1775+
if (!f.file)
1776+
return -EBADF;
1777+
1778+
if (!file_is_kvm(f.file)) {
17771779
ret = -EBADF;
17781780
goto out_fput;
17791781
}
17801782

1781-
source_kvm = source_kvm_file->private_data;
1783+
source_kvm = f.file->private_data;
17821784
ret = sev_lock_two_vms(kvm, source_kvm);
17831785
if (ret)
17841786
goto out_fput;
@@ -1828,8 +1830,7 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
18281830
out_unlock:
18291831
sev_unlock_two_vms(kvm, source_kvm);
18301832
out_fput:
1831-
if (source_kvm_file)
1832-
fput(source_kvm_file);
1833+
fdput(f);
18331834
return ret;
18341835
}
18351836

@@ -2046,18 +2047,20 @@ int sev_mem_enc_unregister_region(struct kvm *kvm,
20462047

20472048
int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
20482049
{
2049-
struct file *source_kvm_file;
2050+
struct fd f = fdget(source_fd);
20502051
struct kvm *source_kvm;
20512052
struct kvm_sev_info *source_sev, *mirror_sev;
20522053
int ret;
20532054

2054-
source_kvm_file = fget(source_fd);
2055-
if (!file_is_kvm(source_kvm_file)) {
2055+
if (!f.file)
2056+
return -EBADF;
2057+
2058+
if (!file_is_kvm(f.file)) {
20562059
ret = -EBADF;
20572060
goto e_source_fput;
20582061
}
20592062

2060-
source_kvm = source_kvm_file->private_data;
2063+
source_kvm = f.file->private_data;
20612064
ret = sev_lock_two_vms(kvm, source_kvm);
20622065
if (ret)
20632066
goto e_source_fput;
@@ -2103,8 +2106,7 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)
21032106
e_unlock:
21042107
sev_unlock_two_vms(kvm, source_kvm);
21052108
e_source_fput:
2106-
if (source_kvm_file)
2107-
fput(source_kvm_file);
2109+
fdput(f);
21082110
return ret;
21092111
}
21102112

fs/fuse/dev.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,30 +2257,31 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
22572257
int res;
22582258
int oldfd;
22592259
struct fuse_dev *fud = NULL;
2260+
struct fd f;
22602261

22612262
switch (cmd) {
22622263
case FUSE_DEV_IOC_CLONE:
2263-
res = -EFAULT;
2264-
if (!get_user(oldfd, (__u32 __user *)arg)) {
2265-
struct file *old = fget(oldfd);
2266-
2267-
res = -EINVAL;
2268-
if (old) {
2269-
/*
2270-
* Check against file->f_op because CUSE
2271-
* uses the same ioctl handler.
2272-
*/
2273-
if (old->f_op == file->f_op)
2274-
fud = fuse_get_dev(old);
2275-
2276-
if (fud) {
2277-
mutex_lock(&fuse_mutex);
2278-
res = fuse_device_clone(fud->fc, file);
2279-
mutex_unlock(&fuse_mutex);
2280-
}
2281-
fput(old);
2282-
}
2264+
if (get_user(oldfd, (__u32 __user *)arg))
2265+
return -EFAULT;
2266+
2267+
f = fdget(oldfd);
2268+
if (!f.file)
2269+
return -EINVAL;
2270+
2271+
/*
2272+
* Check against file->f_op because CUSE
2273+
* uses the same ioctl handler.
2274+
*/
2275+
if (f.file->f_op == file->f_op)
2276+
fud = fuse_get_dev(f.file);
2277+
2278+
res = -EINVAL;
2279+
if (fud) {
2280+
mutex_lock(&fuse_mutex);
2281+
res = fuse_device_clone(fud->fc, file);
2282+
mutex_unlock(&fuse_mutex);
22832283
}
2284+
fdput(f);
22842285
break;
22852286
default:
22862287
res = -ENOTTY;

fs/namespace.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,7 +4194,7 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
41944194
int err = 0;
41954195
struct ns_common *ns;
41964196
struct user_namespace *mnt_userns;
4197-
struct file *file;
4197+
struct fd f;
41984198

41994199
if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
42004200
return 0;
@@ -4210,16 +4210,16 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
42104210
if (attr->userns_fd > INT_MAX)
42114211
return -EINVAL;
42124212

4213-
file = fget(attr->userns_fd);
4214-
if (!file)
4213+
f = fdget(attr->userns_fd);
4214+
if (!f.file)
42154215
return -EBADF;
42164216

4217-
if (!proc_ns_file(file)) {
4217+
if (!proc_ns_file(f.file)) {
42184218
err = -EINVAL;
42194219
goto out_fput;
42204220
}
42214221

4222-
ns = get_proc_ns(file_inode(file));
4222+
ns = get_proc_ns(file_inode(f.file));
42234223
if (ns->ops->type != CLONE_NEWUSER) {
42244224
err = -EINVAL;
42254225
goto out_fput;
@@ -4248,7 +4248,7 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
42484248
kattr->mnt_userns = get_user_ns(mnt_userns);
42494249

42504250
out_fput:
4251-
fput(file);
4251+
fdput(f);
42524252
return err;
42534253
}
42544254

fs/nsfs.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,6 @@ bool proc_ns_file(const struct file *file)
235235
return file->f_op == &ns_file_operations;
236236
}
237237

238-
struct file *proc_ns_fget(int fd)
239-
{
240-
struct file *file;
241-
242-
file = fget(fd);
243-
if (!file)
244-
return ERR_PTR(-EBADF);
245-
246-
if (file->f_op != &ns_file_operations)
247-
goto out_invalid;
248-
249-
return file;
250-
251-
out_invalid:
252-
fput(file);
253-
return ERR_PTR(-EINVAL);
254-
}
255-
256238
/**
257239
* ns_match() - Returns true if current namespace matches dev/ino provided.
258240
* @ns: current namespace

include/linux/proc_ns.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ static inline int ns_alloc_inum(struct ns_common *ns)
7272

7373
#define ns_free_inum(ns) proc_free_inum((ns)->inum)
7474

75-
extern struct file *proc_ns_fget(int fd);
7675
#define get_proc_ns(inode) ((struct ns_common *)(inode)->i_private)
7776
extern int ns_get_path(struct path *path, struct task_struct *task,
7877
const struct proc_ns_operations *ns_ops);

kernel/bpf/bpf_inode_storage.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,33 @@ void bpf_inode_storage_free(struct inode *inode)
8484
static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
8585
{
8686
struct bpf_local_storage_data *sdata;
87-
struct file *f;
88-
int fd;
87+
struct fd f = fdget_raw(*(int *)key);
8988

90-
fd = *(int *)key;
91-
f = fget_raw(fd);
92-
if (!f)
89+
if (!f.file)
9390
return ERR_PTR(-EBADF);
9491

95-
sdata = inode_storage_lookup(f->f_inode, map, true);
96-
fput(f);
92+
sdata = inode_storage_lookup(file_inode(f.file), map, true);
93+
fdput(f);
9794
return sdata ? sdata->data : NULL;
9895
}
9996

10097
static int bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
10198
void *value, u64 map_flags)
10299
{
103100
struct bpf_local_storage_data *sdata;
104-
struct file *f;
105-
int fd;
101+
struct fd f = fdget_raw(*(int *)key);
106102

107-
fd = *(int *)key;
108-
f = fget_raw(fd);
109-
if (!f)
103+
if (!f.file)
110104
return -EBADF;
111-
if (!inode_storage_ptr(f->f_inode)) {
112-
fput(f);
105+
if (!inode_storage_ptr(file_inode(f.file))) {
106+
fdput(f);
113107
return -EBADF;
114108
}
115109

116-
sdata = bpf_local_storage_update(f->f_inode,
110+
sdata = bpf_local_storage_update(file_inode(f.file),
117111
(struct bpf_local_storage_map *)map,
118112
value, map_flags, GFP_ATOMIC);
119-
fput(f);
113+
fdput(f);
120114
return PTR_ERR_OR_ZERO(sdata);
121115
}
122116

@@ -135,16 +129,14 @@ static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
135129

136130
static int bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
137131
{
138-
struct file *f;
139-
int fd, err;
132+
struct fd f = fdget_raw(*(int *)key);
133+
int err;
140134

141-
fd = *(int *)key;
142-
f = fget_raw(fd);
143-
if (!f)
135+
if (!f.file)
144136
return -EBADF;
145137

146-
err = inode_storage_delete(f->f_inode, map);
147-
fput(f);
138+
err = inode_storage_delete(file_inode(f.file), map);
139+
fdput(f);
148140
return err;
149141
}
150142

kernel/cgroup/cgroup.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6856,14 +6856,12 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);
68566856
struct cgroup *cgroup_v1v2_get_from_fd(int fd)
68576857
{
68586858
struct cgroup *cgrp;
6859-
struct file *f;
6860-
6861-
f = fget_raw(fd);
6862-
if (!f)
6859+
struct fd f = fdget_raw(fd);
6860+
if (!f.file)
68636861
return ERR_PTR(-EBADF);
68646862

6865-
cgrp = cgroup_v1v2_get_from_file(f);
6866-
fput(f);
6863+
cgrp = cgroup_v1v2_get_from_file(f.file);
6864+
fdput(f);
68676865
return cgrp;
68686866
}
68696867

kernel/nsproxy.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -545,21 +545,20 @@ static void commit_nsset(struct nsset *nsset)
545545

546546
SYSCALL_DEFINE2(setns, int, fd, int, flags)
547547
{
548-
struct file *file;
548+
struct fd f = fdget(fd);
549549
struct ns_common *ns = NULL;
550550
struct nsset nsset = {};
551551
int err = 0;
552552

553-
file = fget(fd);
554-
if (!file)
553+
if (!f.file)
555554
return -EBADF;
556555

557-
if (proc_ns_file(file)) {
558-
ns = get_proc_ns(file_inode(file));
556+
if (proc_ns_file(f.file)) {
557+
ns = get_proc_ns(file_inode(f.file));
559558
if (flags && (ns->ops->type != flags))
560559
err = -EINVAL;
561560
flags = ns->ops->type;
562-
} else if (!IS_ERR(pidfd_pid(file))) {
561+
} else if (!IS_ERR(pidfd_pid(f.file))) {
563562
err = check_setns_flags(flags);
564563
} else {
565564
err = -EINVAL;
@@ -571,17 +570,17 @@ SYSCALL_DEFINE2(setns, int, fd, int, flags)
571570
if (err)
572571
goto out;
573572

574-
if (proc_ns_file(file))
573+
if (proc_ns_file(f.file))
575574
err = validate_ns(&nsset, ns);
576575
else
577-
err = validate_nsset(&nsset, file->private_data);
576+
err = validate_nsset(&nsset, f.file->private_data);
578577
if (!err) {
579578
commit_nsset(&nsset);
580579
perf_event_namespaces(current);
581580
}
582581
put_nsset(&nsset);
583582
out:
584-
fput(file);
583+
fdput(f);
585584
return err;
586585
}
587586

net/core/net_namespace.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/sched/task.h>
2121
#include <linux/uidgid.h>
2222
#include <linux/cookie.h>
23+
#include <linux/proc_fs.h>
2324

2425
#include <net/sock.h>
2526
#include <net/netlink.h>
@@ -676,21 +677,19 @@ EXPORT_SYMBOL_GPL(get_net_ns);
676677

677678
struct net *get_net_ns_by_fd(int fd)
678679
{
679-
struct file *file;
680-
struct ns_common *ns;
681-
struct net *net;
680+
struct fd f = fdget(fd);
681+
struct net *net = ERR_PTR(-EINVAL);
682682

683-
file = proc_ns_fget(fd);
684-
if (IS_ERR(file))
685-
return ERR_CAST(file);
683+
if (!f.file)
684+
return ERR_PTR(-EBADF);
686685

687-
ns = get_proc_ns(file_inode(file));
688-
if (ns->ops == &netns_operations)
689-
net = get_net(container_of(ns, struct net, ns));
690-
else
691-
net = ERR_PTR(-EINVAL);
686+
if (proc_ns_file(f.file)) {
687+
struct ns_common *ns = get_proc_ns(file_inode(f.file));
688+
if (ns->ops == &netns_operations)
689+
net = get_net(container_of(ns, struct net, ns));
690+
}
691+
fdput(f);
692692

693-
fput(file);
694693
return net;
695694
}
696695
EXPORT_SYMBOL_GPL(get_net_ns_by_fd);

0 commit comments

Comments
 (0)