Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

file: add FD_{ADD,PREPARE}()

I've been playing with this to allow for moderately flexible usage of
the get_unused_fd_flags() + create file + fd_install() pattern that's
used quite extensively.

How callers allocate files is really heterogenous so it's not really
convenient to fold them into a single class. It's possibe to split them
into subclasses like for anon inodes. I think that's not necessarily
nice as well.

My take is to add two primites:
(1) FD_ADD() the simple cases a file is installed:

fd = FD_ADD(O_CLOEXEC, open_file(some, args)));
if (fd >= 0)
kvm_get_kvm(vcpu->kvm);
return fd;

(2) FD_PREPARE() that captures all the cases where access to fd or file
or additional work before publishing the fd is needed:

FD_PREPARE(fdf, open_flag, file_open_handle(&path, open_flag));
if (fdf.err)
return fdf.err;

if (copy_to_user(/* something something */))
return -EFAULT;

return fd_publish(fdf);

I've converted all of the easy cases over to it and it gets rid of an
aweful lot of convoluted cleanup logic.

It's centered around struct fd_prepare. FD_PREPARE() encapsulates all of
allocation and cleanup logic and must be followed by a call to
fd_publish() which associates the fd with the file and installs it into
the callers fdtable. If fd_publish() isn't called both are deallocated.

It mandates a specific order namely that first we allocate the fd and
then instantiate the file. But that shouldn't be a problem nearly
everyone I've converted uses this exact pattern anyway.

There's a bunch of additional cases where it would be easy to convert
them to this pattern. For example, the whole sync file stuff in dma
currently retains the containing structure of the file instead of the
file itself even though it's only used to allocate files. Changing that
would make it fall into the FD_PREPARE() pattern easily. I've not done
that work yet.

There's room for extending this in a way that wed'd have subclasses for
some particularly often use patterns but as I said I'm not even sure
that's worth it.

Link: https://patch.msgid.link/20251123-work-fd-prepare-v4-1-b6efa1706cfd@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>

+133
+7
include/linux/cleanup.h
··· 261 261 * CLASS(name, var)(args...): 262 262 * declare the variable @var as an instance of the named class 263 263 * 264 + * CLASS_INIT(name, var, init_expr): 265 + * declare the variable @var as an instance of the named class with 266 + * custom initialization expression. 267 + * 264 268 * Ex. 265 269 * 266 270 * DEFINE_CLASS(fdget, struct fd, fdput(_T), fdget(fd), int fd) ··· 293 289 #define CLASS(_name, var) \ 294 290 class_##_name##_t var __cleanup(class_##_name##_destructor) = \ 295 291 class_##_name##_constructor 292 + 293 + #define CLASS_INIT(_name, _var, _init_expr) \ 294 + class_##_name##_t _var __cleanup(class_##_name##_destructor) = (_init_expr) 296 295 297 296 #define scoped_class(_name, var, args) \ 298 297 for (CLASS(_name, var)(args); \
+126
include/linux/file.h
··· 127 127 128 128 extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max; 129 129 130 + /* 131 + * fd_prepare: Combined fd + file allocation cleanup class. 132 + * @err: Error code to indicate if allocation succeeded. 133 + * @__fd: Allocated fd (may not be accessed directly) 134 + * @__file: Allocated struct file pointer (may not be accessed directly) 135 + * 136 + * Allocates an fd and a file together. On error paths, automatically cleans 137 + * up whichever resource was successfully allocated. Allows flexible file 138 + * allocation with different functions per usage. 139 + * 140 + * Do not use directly. 141 + */ 142 + struct fd_prepare { 143 + s32 err; 144 + s32 __fd; /* do not access directly */ 145 + struct file *__file; /* do not access directly */ 146 + }; 147 + 148 + /* Typedef for fd_prepare cleanup guards. */ 149 + typedef struct fd_prepare class_fd_prepare_t; 150 + 151 + /* 152 + * Accessors for fd_prepare class members. 153 + * _Generic() is used for zero-cost type safety. 154 + */ 155 + #define fd_prepare_fd(_fdf) \ 156 + (_Generic((_fdf), struct fd_prepare: (_fdf).__fd)) 157 + 158 + #define fd_prepare_file(_fdf) \ 159 + (_Generic((_fdf), struct fd_prepare: (_fdf).__file)) 160 + 161 + /* Do not use directly. */ 162 + static inline void class_fd_prepare_destructor(const struct fd_prepare *fdf) 163 + { 164 + if (unlikely(fdf->err)) { 165 + if (likely(fdf->__fd >= 0)) 166 + put_unused_fd(fdf->__fd); 167 + if (unlikely(!IS_ERR_OR_NULL(fdf->__file))) 168 + fput(fdf->__file); 169 + } 170 + } 171 + 172 + /* Do not use directly. */ 173 + static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf) 174 + { 175 + if (unlikely(fdf->err)) 176 + return fdf->err; 177 + if (unlikely(fdf->__fd < 0)) 178 + return fdf->__fd; 179 + if (unlikely(IS_ERR(fdf->__file))) 180 + return PTR_ERR(fdf->__file); 181 + if (unlikely(!fdf->__file)) 182 + return -ENOMEM; 183 + return 0; 184 + } 185 + 186 + /* 187 + * __FD_PREPARE_INIT - Helper to initialize fd_prepare class. 188 + * @_fd_flags: flags for get_unused_fd_flags() 189 + * @_file_owned: expression that returns struct file * 190 + * 191 + * Returns a struct fd_prepare with fd, file, and err set. 192 + * If fd allocation fails, fd will be negative and err will be set. If 193 + * fd succeeds but file_init_expr fails, file will be ERR_PTR and err 194 + * will be set. The err field is the single source of truth for error 195 + * checking. 196 + */ 197 + #define __FD_PREPARE_INIT(_fd_flags, _file_owned) \ 198 + ({ \ 199 + struct fd_prepare fdf = { \ 200 + .__fd = get_unused_fd_flags((_fd_flags)), \ 201 + }; \ 202 + if (likely(fdf.__fd >= 0)) \ 203 + fdf.__file = (_file_owned); \ 204 + fdf.err = ACQUIRE_ERR(fd_prepare, &fdf); \ 205 + fdf; \ 206 + }) 207 + 208 + /* 209 + * FD_PREPARE - Macro to declare and initialize an fd_prepare variable. 210 + * 211 + * Declares and initializes an fd_prepare variable with automatic 212 + * cleanup. No separate scope required - cleanup happens when variable 213 + * goes out of scope. 214 + * 215 + * @_fdf: name of struct fd_prepare variable to define 216 + * @_fd_flags: flags for get_unused_fd_flags() 217 + * @_file_owned: struct file to take ownership of (can be expression) 218 + */ 219 + #define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ 220 + CLASS_INIT(fd_prepare, _fdf, __FD_PREPARE_INIT(_fd_flags, _file_owned)) 221 + 222 + /* 223 + * fd_publish - Publish prepared fd and file to the fd table. 224 + * @_fdf: struct fd_prepare variable 225 + */ 226 + #define fd_publish(_fdf) \ 227 + ({ \ 228 + struct fd_prepare *fdp = &(_fdf); \ 229 + VFS_WARN_ON_ONCE(fdp->err); \ 230 + VFS_WARN_ON_ONCE(fdp->__fd < 0); \ 231 + VFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp->__file)); \ 232 + fd_install(fdp->__fd, fdp->__file); \ 233 + fdp->__fd; \ 234 + }) 235 + 236 + /* Do not use directly. */ 237 + #define __FD_ADD(_fdf, _fd_flags, _file_owned) \ 238 + ({ \ 239 + FD_PREPARE(_fdf, _fd_flags, _file_owned); \ 240 + s32 ret = _fdf.err; \ 241 + if (likely(!ret)) \ 242 + ret = fd_publish(_fdf); \ 243 + ret; \ 244 + }) 245 + 246 + /* 247 + * FD_ADD - Allocate and install an fd and file in one step. 248 + * @_fd_flags: flags for get_unused_fd_flags() 249 + * @_file_owned: struct file to take ownership of 250 + * 251 + * Returns the allocated fd number, or negative error code on failure. 252 + */ 253 + #define FD_ADD(_fd_flags, _file_owned) \ 254 + __FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned) 255 + 130 256 #endif /* __LINUX_FILE_H */