For backing file operations, users are expected to pass credentials
that will outlive the backing file common operations.
Use the specialized guard statements to override/revert the
credentials.
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
---
fs/backing-file.c | 27 +++++++--------------------
1 file changed, 7 insertions(+), 20 deletions(-)
diff --git a/fs/backing-file.c b/fs/backing-file.c
index 740185198db3..9610d5166736 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -140,7 +140,6 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
struct backing_file_ctx *ctx)
{
struct backing_aio *aio = NULL;
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
@@ -153,7 +152,7 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
!(file->f_mode & FMODE_CAN_ODIRECT))
return -EINVAL;
- old_cred = override_creds(ctx->cred);
+ cred_guard(ctx->cred);
if (is_sync_kiocb(iocb)) {
rwf_t rwf = iocb_to_rw_flags(flags);
@@ -174,8 +173,6 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
backing_aio_cleanup(aio, ret);
}
out:
- revert_creds(old_cred);
-
if (ctx->accessed)
ctx->accessed(ctx->user_file);
@@ -187,7 +184,6 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
struct kiocb *iocb, int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
@@ -210,7 +206,7 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
*/
flags &= ~IOCB_DIO_CALLER_COMP;
- old_cred = override_creds(ctx->cred);
+ cred_guard(ctx->cred);
if (is_sync_kiocb(iocb)) {
rwf_t rwf = iocb_to_rw_flags(flags);
@@ -222,12 +218,12 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
ret = backing_aio_init_wq(iocb);
if (ret)
- goto out;
+ return ret;
ret = -ENOMEM;
aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
if (!aio)
- goto out;
+ return ret;
aio->orig_iocb = iocb;
aio->end_write = ctx->end_write;
@@ -240,9 +236,6 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
if (ret != -EIOCBQUEUED)
backing_aio_cleanup(aio, ret);
}
-out:
- revert_creds(old_cred);
-
return ret;
}
EXPORT_SYMBOL_GPL(backing_file_write_iter);
@@ -252,15 +245,13 @@ ssize_t backing_file_splice_read(struct file *in, loff_t *ppos,
unsigned int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
return -EIO;
- old_cred = override_creds(ctx->cred);
+ cred_guard(ctx->cred);
ret = vfs_splice_read(in, ppos, pipe, len, flags);
- revert_creds(old_cred);
if (ctx->accessed)
ctx->accessed(ctx->user_file);
@@ -274,7 +265,6 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
unsigned int flags,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
ssize_t ret;
if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
@@ -284,11 +274,10 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
if (ret)
return ret;
- old_cred = override_creds(ctx->cred);
+ cred_guard(ctx->cred);
file_start_write(out);
ret = iter_file_splice_write(pipe, out, ppos, len, flags);
file_end_write(out);
- revert_creds(old_cred);
if (ctx->end_write)
ctx->end_write(ctx->user_file);
@@ -300,7 +289,6 @@ EXPORT_SYMBOL_GPL(backing_file_splice_write);
int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
struct backing_file_ctx *ctx)
{
- const struct cred *old_cred;
int ret;
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)) ||
@@ -312,9 +300,8 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
vma_set_file(vma, file);
- old_cred = override_creds(ctx->cred);
+ cred_guard(ctx->cred);
ret = call_mmap(vma->vm_file, vma);
- revert_creds(old_cred);
if (ctx->accessed)
ctx->accessed(ctx->user_file);
--
2.44.0
Hi Vinicius,
kernel test robot noticed the following build warnings:
[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on linus/master v6.9-rc2 next-20240403]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Vinicius-Costa-Gomes/cred-Add-a-light-version-of-override-revert_creds/20240403-101954
base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link: https://lore.kernel.org/r/20240403021808.309900-3-vinicius.gomes%40intel.com
patch subject: [PATCH v1 2/3] fs: Optimize credentials reference count for backing file ops
config: i386-randconfig-061-20240403 (https://download.01.org/0day-ci/archive/20240403/202404032344.SKdrnkhI-lkp@intel.com/config)
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240403/202404032344.SKdrnkhI-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202404032344.SKdrnkhI-lkp@intel.com/
sparse warnings: (new ones prefixed by >>)
fs/backing-file.c: note: in included file (through include/linux/sched/signal.h, include/linux/rcuwait.h, include/linux/percpu-rwsem.h, ...):
>> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@
include/linux/cred.h:182:41: sparse: expected struct cred const *old
include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred
>> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@
include/linux/cred.h:182:41: sparse: expected struct cred const *old
include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred
>> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@
include/linux/cred.h:182:41: sparse: expected struct cred const *old
include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred
>> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@
include/linux/cred.h:182:41: sparse: expected struct cred const *old
include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred
>> include/linux/cred.h:182:41: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct cred const *old @@ got struct cred const [noderef] __rcu *cred @@
include/linux/cred.h:182:41: sparse: expected struct cred const *old
include/linux/cred.h:182:41: sparse: got struct cred const [noderef] __rcu *cred
vim +182 include/linux/cred.h
58319057b78476 Andy Lutomirski 2015-09-04 174
dd60a254548056 Vinicius Costa Gomes 2024-04-02 175 /*
dd60a254548056 Vinicius Costa Gomes 2024-04-02 176 * Override creds without bumping reference count. Caller must ensure
dd60a254548056 Vinicius Costa Gomes 2024-04-02 177 * reference remains valid or has taken reference. Almost always not the
dd60a254548056 Vinicius Costa Gomes 2024-04-02 178 * interface you want. Use override_creds()/revert_creds() instead.
dd60a254548056 Vinicius Costa Gomes 2024-04-02 179 */
dd60a254548056 Vinicius Costa Gomes 2024-04-02 180 static inline const struct cred *override_creds_light(const struct cred *override_cred)
dd60a254548056 Vinicius Costa Gomes 2024-04-02 181 {
dd60a254548056 Vinicius Costa Gomes 2024-04-02 @182 const struct cred *old = current->cred;
dd60a254548056 Vinicius Costa Gomes 2024-04-02 183
dd60a254548056 Vinicius Costa Gomes 2024-04-02 184 rcu_assign_pointer(current->cred, override_cred);
dd60a254548056 Vinicius Costa Gomes 2024-04-02 185 return old;
dd60a254548056 Vinicius Costa Gomes 2024-04-02 186 }
dd60a254548056 Vinicius Costa Gomes 2024-04-02 187
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.