Skip to content

Commit 0f25f0e

Browse files
committed
Merge tag 'pull-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull 'struct fd' class updates from Al Viro: "The bulk of struct fd memory safety stuff Making sure that struct fd instances are destroyed in the same scope where they'd been created, getting rid of reassignments and passing them by reference, converting to CLASS(fd{,_pos,_raw}). We are getting very close to having the memory safety of that stuff trivial to verify" * tag 'pull-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: (28 commits) deal with the last remaing boolean uses of fd_file() css_set_fork(): switch to CLASS(fd_raw, ...) memcg_write_event_control(): switch to CLASS(fd) assorted variants of irqfd setup: convert to CLASS(fd) do_pollfd(): convert to CLASS(fd) convert do_select() convert vfs_dedupe_file_range(). convert cifs_ioctl_copychunk() convert media_request_get_by_fd() convert spu_run(2) switch spufs_calls_{get,put}() to CLASS() use convert cachestat(2) convert do_preadv()/do_pwritev() fdget(), more trivial conversions fdget(), trivial conversions privcmd_ioeventfd_assign(): don't open-code eventfd_ctx_fdget() o2hb_region_dev_store(): avoid goto around fdget()/fdput() introduce "fd_pos" class, convert fdget_pos() users to it. fdget_raw() users: switch to CLASS(fd_raw) convert vmsplice() to CLASS(fd) ...
2 parents 23acd17 + 38052c2 commit 0f25f0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+751
-1395
lines changed

arch/alpha/kernel/osf_sys.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
152152
long __user *, basep)
153153
{
154154
int error;
155-
struct fd arg = fdget_pos(fd);
155+
CLASS(fd_pos, arg)(fd);
156156
struct osf_dirent_callback buf = {
157157
.ctx.actor = osf_filldir,
158158
.dirent = dirent,
159159
.basep = basep,
160160
.count = count
161161
};
162162

163-
if (!fd_file(arg))
163+
if (fd_empty(arg))
164164
return -EBADF;
165165

166166
error = iterate_dir(fd_file(arg), &buf.ctx);
@@ -169,7 +169,6 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
169169
if (count != buf.count)
170170
error = count - buf.count;
171171

172-
fdput_pos(arg);
173172
return error;
174173
}
175174

arch/arm/kernel/sys_oabi-compat.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
235235
unsigned long arg)
236236
{
237237
void __user *argp = (void __user *)arg;
238-
struct fd f = fdget_raw(fd);
238+
CLASS(fd_raw, f)(fd);
239239
struct flock64 flock;
240-
long err = -EBADF;
240+
long err;
241241

242-
if (!fd_file(f))
243-
goto out;
242+
if (fd_empty(f))
243+
return -EBADF;
244244

245245
switch (cmd) {
246246
case F_GETLK64:
@@ -271,8 +271,6 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
271271
err = sys_fcntl64(fd, cmd, arg);
272272
break;
273273
}
274-
fdput(f);
275-
out:
276274
return err;
277275
}
278276

arch/powerpc/kvm/book3s_64_vio.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
115115
struct iommu_table_group *table_group;
116116
long i;
117117
struct kvmppc_spapr_tce_iommu_table *stit;
118-
struct fd f;
118+
CLASS(fd, f)(tablefd);
119119

120-
f = fdget(tablefd);
121-
if (!fd_file(f))
120+
if (fd_empty(f))
122121
return -EBADF;
123122

124123
rcu_read_lock();
@@ -130,16 +129,12 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
130129
}
131130
rcu_read_unlock();
132131

133-
if (!found) {
134-
fdput(f);
132+
if (!found)
135133
return -EINVAL;
136-
}
137134

138135
table_group = iommu_group_get_iommudata(grp);
139-
if (WARN_ON(!table_group)) {
140-
fdput(f);
136+
if (WARN_ON(!table_group))
141137
return -EFAULT;
142-
}
143138

144139
for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
145140
struct iommu_table *tbltmp = table_group->tables[i];
@@ -160,10 +155,8 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
160155
break;
161156
}
162157
}
163-
if (!tbl) {
164-
fdput(f);
158+
if (!tbl)
165159
return -EINVAL;
166-
}
167160

168161
rcu_read_lock();
169162
list_for_each_entry_rcu(stit, &stt->iommu_tables, next) {
@@ -174,23 +167,20 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
174167
/* stit is being destroyed */
175168
iommu_tce_table_put(tbl);
176169
rcu_read_unlock();
177-
fdput(f);
178170
return -ENOTTY;
179171
}
180172
/*
181173
* The table is already known to this KVM, we just increased
182174
* its KVM reference counter and can return.
183175
*/
184176
rcu_read_unlock();
185-
fdput(f);
186177
return 0;
187178
}
188179
rcu_read_unlock();
189180

190181
stit = kzalloc(sizeof(*stit), GFP_KERNEL);
191182
if (!stit) {
192183
iommu_tce_table_put(tbl);
193-
fdput(f);
194184
return -ENOMEM;
195185
}
196186

@@ -199,7 +189,6 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
199189

200190
list_add_rcu(&stit->next, &stt->iommu_tables);
201191

202-
fdput(f);
203192
return 0;
204193
}
205194

arch/powerpc/kvm/powerpc.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,31 +1933,28 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
19331933
#endif
19341934
#ifdef CONFIG_KVM_MPIC
19351935
case KVM_CAP_IRQ_MPIC: {
1936-
struct fd f;
1936+
CLASS(fd, f)(cap->args[0]);
19371937
struct kvm_device *dev;
19381938

19391939
r = -EBADF;
1940-
f = fdget(cap->args[0]);
1941-
if (!fd_file(f))
1940+
if (fd_empty(f))
19421941
break;
19431942

19441943
r = -EPERM;
19451944
dev = kvm_device_from_filp(fd_file(f));
19461945
if (dev)
19471946
r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
19481947

1949-
fdput(f);
19501948
break;
19511949
}
19521950
#endif
19531951
#ifdef CONFIG_KVM_XICS
19541952
case KVM_CAP_IRQ_XICS: {
1955-
struct fd f;
1953+
CLASS(fd, f)(cap->args[0]);
19561954
struct kvm_device *dev;
19571955

19581956
r = -EBADF;
1959-
f = fdget(cap->args[0]);
1960-
if (!fd_file(f))
1957+
if (fd_empty(f))
19611958
break;
19621959

19631960
r = -EPERM;
@@ -1968,34 +1965,27 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
19681965
else
19691966
r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
19701967
}
1971-
1972-
fdput(f);
19731968
break;
19741969
}
19751970
#endif /* CONFIG_KVM_XICS */
19761971
#ifdef CONFIG_KVM_XIVE
19771972
case KVM_CAP_PPC_IRQ_XIVE: {
1978-
struct fd f;
1973+
CLASS(fd, f)(cap->args[0]);
19791974
struct kvm_device *dev;
19801975

19811976
r = -EBADF;
1982-
f = fdget(cap->args[0]);
1983-
if (!fd_file(f))
1977+
if (fd_empty(f))
19841978
break;
19851979

19861980
r = -ENXIO;
1987-
if (!xive_enabled()) {
1988-
fdput(f);
1981+
if (!xive_enabled())
19891982
break;
1990-
}
19911983

19921984
r = -EPERM;
19931985
dev = kvm_device_from_filp(fd_file(f));
19941986
if (dev)
19951987
r = kvmppc_xive_native_connect_vcpu(dev, vcpu,
19961988
cap->args[1]);
1997-
1998-
fdput(f);
19991989
break;
20001990
}
20011991
#endif /* CONFIG_KVM_XIVE */

arch/powerpc/platforms/cell/spu_syscalls.c

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ static inline struct spufs_calls *spufs_calls_get(void)
3636

3737
static inline void spufs_calls_put(struct spufs_calls *calls)
3838
{
39+
if (!calls)
40+
return;
41+
3942
BUG_ON(calls != spufs_calls);
4043

4144
/* we don't need to rcu this, as we hold a reference to the module */
@@ -53,82 +56,55 @@ static inline void spufs_calls_put(struct spufs_calls *calls) { }
5356

5457
#endif /* CONFIG_SPU_FS_MODULE */
5558

59+
DEFINE_CLASS(spufs_calls, struct spufs_calls *, spufs_calls_put(_T), spufs_calls_get(), void)
60+
5661
SYSCALL_DEFINE4(spu_create, const char __user *, name, unsigned int, flags,
5762
umode_t, mode, int, neighbor_fd)
5863
{
59-
long ret;
60-
struct spufs_calls *calls;
61-
62-
calls = spufs_calls_get();
64+
CLASS(spufs_calls, calls)();
6365
if (!calls)
6466
return -ENOSYS;
6567

6668
if (flags & SPU_CREATE_AFFINITY_SPU) {
67-
struct fd neighbor = fdget(neighbor_fd);
68-
ret = -EBADF;
69-
if (fd_file(neighbor)) {
70-
ret = calls->create_thread(name, flags, mode, fd_file(neighbor));
71-
fdput(neighbor);
72-
}
73-
} else
74-
ret = calls->create_thread(name, flags, mode, NULL);
75-
76-
spufs_calls_put(calls);
77-
return ret;
69+
CLASS(fd, neighbor)(neighbor_fd);
70+
if (fd_empty(neighbor))
71+
return -EBADF;
72+
return calls->create_thread(name, flags, mode, fd_file(neighbor));
73+
} else {
74+
return calls->create_thread(name, flags, mode, NULL);
75+
}
7876
}
7977

8078
SYSCALL_DEFINE3(spu_run,int, fd, __u32 __user *, unpc, __u32 __user *, ustatus)
8179
{
82-
long ret;
83-
struct fd arg;
84-
struct spufs_calls *calls;
85-
86-
calls = spufs_calls_get();
80+
CLASS(spufs_calls, calls)();
8781
if (!calls)
8882
return -ENOSYS;
8983

90-
ret = -EBADF;
91-
arg = fdget(fd);
92-
if (fd_file(arg)) {
93-
ret = calls->spu_run(fd_file(arg), unpc, ustatus);
94-
fdput(arg);
95-
}
84+
CLASS(fd, arg)(fd);
85+
if (fd_empty(arg))
86+
return -EBADF;
9687

97-
spufs_calls_put(calls);
98-
return ret;
88+
return calls->spu_run(fd_file(arg), unpc, ustatus);
9989
}
10090

10191
#ifdef CONFIG_COREDUMP
10292
int elf_coredump_extra_notes_size(void)
10393
{
104-
struct spufs_calls *calls;
105-
int ret;
106-
107-
calls = spufs_calls_get();
94+
CLASS(spufs_calls, calls)();
10895
if (!calls)
10996
return 0;
11097

111-
ret = calls->coredump_extra_notes_size();
112-
113-
spufs_calls_put(calls);
114-
115-
return ret;
98+
return calls->coredump_extra_notes_size();
11699
}
117100

118101
int elf_coredump_extra_notes_write(struct coredump_params *cprm)
119102
{
120-
struct spufs_calls *calls;
121-
int ret;
122-
123-
calls = spufs_calls_get();
103+
CLASS(spufs_calls, calls)();
124104
if (!calls)
125105
return 0;
126106

127-
ret = calls->coredump_extra_notes_write(cprm);
128-
129-
spufs_calls_put(calls);
130-
131-
return ret;
107+
return calls->coredump_extra_notes_write(cprm);
132108
}
133109
#endif
134110

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -901,19 +901,15 @@ static struct miscdevice sgx_dev_provision = {
901901
int sgx_set_attribute(unsigned long *allowed_attributes,
902902
unsigned int attribute_fd)
903903
{
904-
struct fd f = fdget(attribute_fd);
904+
CLASS(fd, f)(attribute_fd);
905905

906-
if (!fd_file(f))
906+
if (fd_empty(f))
907907
return -EINVAL;
908908

909-
if (fd_file(f)->f_op != &sgx_provision_fops) {
910-
fdput(f);
909+
if (fd_file(f)->f_op != &sgx_provision_fops)
911910
return -EINVAL;
912-
}
913911

914912
*allowed_attributes |= SGX_ATTR_PROVISIONKEY;
915-
916-
fdput(f);
917913
return 0;
918914
}
919915
EXPORT_SYMBOL_GPL(sgx_set_attribute);

0 commit comments

Comments
 (0)