Merge tag 'vfs-7.4-rc1.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull vfs fixes from Christian Brauner:

 - writeback:

    - Update dirtied_when instead of dirtied_time_when when a wb switch
      puts an inode at the newest end of the target b_dirty list. The
      list was left out of order so move_expired_inodes() could stop
      before older inodes, and the lazytime expiration timestamp was
      reset

    - Clear the old wb's dirty-I/O state and bandwidth contribution when
      a batch of inode switches leaves its dirty lists empty. An empty
      wb stayed scheduled for periodic writeback and inflated
      bdi->tot_write_bandwidth

    - Take wb_switch_rwsem across the inode scan of
      cleanup_offline_cgwb(). The v7.3-rc5 fix that rotates scanned
      inodes let a scan racing a sync()'s queue_io() move lazytime
      inodes past the sync's cutoff, so sync() returned without
      persisting their timestamps

    - Don't kick writeback for an IOCB_DONTCACHE write on an inode that
      has no wb yet. With cgroup writeback an O_DIRECT RWF_DONTCACHE
      write to a block device dereferenced NULL

 - statmount/listmount/listns:

    - Refuse statmount() string requests with a buffer that isn't larger
      than struct statmount

    - Don't wrap around with a U64_MAX cursor in listmount() and
      listns()

    - Don't end listns() at an inactive namespace

    - Only add init_ipc_ns and init_net to the namespace trees if
      CONFIG_IPC_NS and CONFIG_NET_NS are enabled

 - iomap:

    - Only mark blocks uptodate in __iomap_write_begin() after reading
      them and let __iomap_write_end() mark the zeroed blocks uptodate
      once the copy from userspace succeeded

    - Check a chunk against max_integrity_io_size() on its own before
      the bio size check

    - Only enforce block alignment for direct I/O into holes and
      unwritten extents if IOMAP_DIO_FSBLOCK_ALIGNED is set

 - backing-file:

    - Check S_APPEND and S_IMMUTABLE on the backing inode. FUSE
      passthrough opens the backing file with vfs_open() which skips
      may_open(), so anyone who may open the FUSE file for writing could
      overwrite an append-only backing file anywhere or write to an
      immutable one. Open, mmap and writes now check the backing inode
      the way may_open() and do_mmap() do

    - Always call kiocb_end_write() for a queued write

    - Let backing files inherit FMODE_NONOTIFY from the user file. The
      event fds fanotify creates for permission events on a stacked
      filesystem raised FS_OPEN_PERM on the real path from inside the
      listener's read() and deadlocked listeners with marks on both
      layers

 - pidfs:

    - Look the task up before checking for exit information in
      PIDFD_GET_INFO and start over if the task no longer owns the pid
      once all fields have been read. A caller racing release_task() got
      neither PIDFD_INFO_PID nor PIDFD_INFO_EXIT or ESRCH although the
      exit information was available, and a caller racing a sub-thread's
      exec reported that thread's tid as the pid

    - Clear the mask bits of fields that lie beyond the struct size the
      caller passed. The uapi header promises that the mask names no
      such field but a caller passing PIDFD_INFO_SIZE_VER0 got the
      coredump and supported mask bits set for fields that were never
      written

 - aio:

    - Give the aio ring a ->read_folio that fails with EIO so a fault on
      a ring whose file was truncated gets SIGBUS instead of jumping to
      NULL

    - Check nr_events against the ring size before doubling it in
      ioctx_alloc()

 - splice:

    - Only write back an implicit file position in do_splice() when the
      splice helper succeeded. splice doesn't take f_pos_lock so a
      failed splice restored a stale snapshot over a position that a
      concurrent getdents() had moved. On ext2 the next readdir then
      trusted an offset inside a directory entry

    - Retry tee() when the input pipe was emptied or the output pipe
      filled between the prep steps and link_pipe() instead of returning
      0 while the input pipe still has writers, as splice_pipe_to_pipe()
      does

 - ucount:

    - Take the reference that pins a ucounts level on the 0 -> 1
      transition before checking the rlimit

    - Read the parent ucounts before dropping the rlimit count in
      do_dec_rlimit_put_ucounts()

 - inode:

    - Rotate inodes whose page cache couldn't be dropped to the tail of
      the LRU

    - Skip inodes that another LRU walker is isolating

 - setns:

    - Set nsset->flags before anything in prepare_nsset() can fail so
      the error path drops the cred prepared for CLONE_NEWUSER. A
      failing setns() with CLONE_NEWUSER and CLONE_NEWNS leaked it
      together with its user namespace

    - Refuse setns(CLONE_NEWNS) for a task that shares its fs_struct
      again when more namespaces are requested

 - minix:

    - Restore uptodate on the pinned bitmap buffers before redirtying
      them after a failed writeback instead of tripping the warning in
      mark_buffer_dirty(), as ext2 and ext4 do for their superblock
      buffers

    - Reject rmdir() of a directory with a corrupted link count with
      EFSCORRUPTED instead of hitting the drop_nlink() warning

 - userns: Keep the exec securebits and their locks when creating or
   joining a user namespace. set_cred_user_ns() reset all securebits to
   SECUREBITS_DEFAULT so anyone who may create a user namespace could
   shed SECBIT_EXEC_RESTRICT_FILE and SECBIT_EXEC_DENY_INTERACTIVE with
   a single unshare(CLONE_NEWUSER)

 - fs: Honor FOP_UNSIGNED_OFFSET in sys_llseek() and in the pread64(),
   pwrite64(), preadv() and pwritev() wrappers

 - proc/kcore: Advance the vmalloc retry in read_kcore_iter() by the
   last vread_iter() copy instead of the running total

 - eventpoll: Return the error when the wakeup source for EPOLLWAKEUP
   can't be created in EPOLL_CTL_MOD instead of reporting success
   without suspend protection

 - coredump: Mark do_coredump() noinline_for_stack

 - nsfs: Check the trailing bytes of a namespace file handle in bytes
   instead of words

 - netfs: Restore the per-level and per-point decoding of the debug mask

 - exportfs: Release the directory file get_name() opened with
   __fput_sync(). From nfsd's kernel threads the deferred fputs arrived
   faster than delayed_fput retired them and each pinned the directory's
   readdir state, growing unreclaimable slab by about 90 MB per second
   under a parallel tree walk

 - fs: Call ramfs_kill_sb() for a ramfs rootfs. Since rootfs can be
   unmounted as of v7.0 its struct ramfs_fs_info was leaked

 - signal: Check the blocked mask for every entry
   dequeue_synchronous_signal() looks at. The scan could select a
   blocked synchronous signal and deliver it while blocked

 - super: Convert the superblock device table from an rhltable to a
   plain hashtable under a spinlock. This fixes a bug from v7.3 where
   fs_bdev_freeze() froze a btrfs filesystem through a stale member that
   open_ctree() had already released while the freeze was waiting for
   SB_BORN

 - fork: Leave rcu_users at zero until copy_process() can't fail
   anymore. bpf_task_acquire() handed out trusted references to the new
   task from the task_alloc LSM hook and sched_ext's ops.init_task() and
   a failing fork freed the task behind them

 - xattr: Always run cap_convert_nscap() for a security.capability
   write. A zero-length setxattr() skipped it and let an unprivileged
   caller without CAP_SETFCAP or write permission on the file plant an
   empty security.capability xattr that made every execve() of the
   binary fail with EINVAL

 - namei: Skip the S_ISDIR assertion in

 - proc: Refuse to mount on entries below /proc/<pid>/net like fd/,
   fdinfo/ and map_files/. proc_net_d_revalidate() always fails so the
   first lookup by anyone with search permission ran d_invalidate() on
   the dentry and detached every mount on it, locked mounts included

 - libfs: Hold one mount reference for all pins instead of one per pin.
   The pin count is what keeps the mount alive, so the per-pin reference
   only cost a mntget() and a mntput() through
   mntput_no_expire_slowpath() for every debugfs, tracefs, securityfs or
   configfs file that comes and goes

 - iov_iter: Return 0 from iov_iter_alignment() for an empty iterator of
   any type

 - watch_queue: Give info_id its own storage instead of overlaying it
   with rcu_head

 - filelock: Take an active superblock reference while waiting for a
   delegation break. After a lazy umount closing the delegation holder
   could shut down the superblock under the waiter whose final iput()
   then ran shmem_evict_inode() on freed state

 - fs: Skip the copy_file_range() write limit checks when there is no
   data to copy

 - bpf: Allow bpf_d_path() from fentry on filp_close_sync(). Since
   close_range() became synchronous this cycle close(), close_range()
   and the closes on exec and exit no longer go through filp_close().
   fexit stays rejected since the file may be gone by then

 - fs_pin: Dequeue the wait entry unconditionally in pin_kill(). A
   spuriously woken waiter kept its on-stack entry queued, took that as
   proof that the pin was alive and used the freed acct structure when
   umount() of the accounting file raced acct(2)

 - dcache: Don't fail __d_obtain_alias() for an inode without links and
   aliases. The ESTALE check added this cycle broke open_by_handle_at()
   and nfsd file handle decoding on NFS and FUSE where i_nlink is just
   what the server last reported and the handle may well still be valid

 - selftests/ovl: Fix the mountinfo expectations of the detached layer
   test which has failed since it was added

* tag 'vfs-7.4-rc1.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (60 commits)
  fs: don't fail __d_obtain_alias() for unlinked inodes
  ns: don't add init_ipc_ns and init_net without namespace support
  aio: avoid request count overflow in ioctx_alloc()
  selftests/ovl: fix detached layer mountinfo expectations
  bpf: Allow bpf_d_path() from filp_close_sync()
  fs: skip copy_file_range write limits when there is no data
  minix: reject rmdir of a directory with a corrupted link count
  fs_pin: do not trust the pin after a spurious wakeup in pin_kill()
  ucount: read the parent ucounts before dropping the rlimit count
  splice: retry tee() instead of returning 0 when it races
  selftests/pidfd: test that fields beyond the struct size are not reported
  pidfs: do not report fields that do not fit into the caller's struct
  fs: let backing files inherit FMODE_NONOTIFY from the user file
  splice: avoid stale f_pos writeback on error
  backing-file: always end a queued write
  filelock: keep superblock active through delegation wait
  watch_queue: keep info_id separate from rcu_head
  iomap: only follow dio alignment requirement if specified
  iov_iter: fix NULL deref in iov_iter_alignment()
  backing-file: enforce append-only and immutable backing inodes
  ...
This commit is contained in:
Linus Torvalds
2026-10-30 09:25:01 -10:00
65 changed files with 1469 additions and 419 deletions
@@ -99,6 +99,10 @@ This secure bit may be set by user session managers, service managers,
container runtimes, sandboxer tools... Except for test environments, the
related ``SECBIT_EXEC_DENY_INTERACTIVE_LOCKED`` bit should also be set.
Both secure bits and their locks are inherited across :manpage:`fork(2)` and
:manpage:`execve(2)`, and they are kept when a task creates or joins a user
namespace.
Here is the expected behavior for a script interpreter according to combination
of any exec securebits:
+1 -1
View File
@@ -1283,7 +1283,7 @@ int lookup_bdev(const char *pathname, dev_t *dev)
* registers major 0. Fake block device inodes (e.g. fuse with
* rootmode=S_IFBLK) can expose i_rdev == 0, and letting that
* propagate would confuse superblock lookup and trigger warnings
* in the device-to-superblock table (super_dev_register).
* in the device-to-superblock table (sget_fc).
*/
error = -ENODEV;
if (!inode->i_rdev)
+13 -3
View File
@@ -528,7 +528,18 @@ out:
#define aio_migrate_folio NULL
#endif
/*
* filemap_fault() reads a folio that it did not find in the page cache. All
* folios of a live ring are there, so the ring was truncated under the fault.
*/
static int aio_read_folio(struct file *file, struct folio *folio)
{
folio_unlock(folio);
return -EIO;
}
static const struct address_space_operations aio_ctx_aops = {
.read_folio = aio_read_folio,
.dirty_folio = noop_dirty_folio,
.migrate_folio = aio_migrate_folio,
};
@@ -805,13 +816,12 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
* counters at a time, so make sure that isn't 0:
*/
nr_events = max(nr_events, num_possible_cpus() * 4);
nr_events *= 2;
/* Prevent overflows */
if (nr_events > (0x10000000U / sizeof(struct io_event))) {
if (nr_events > (0x8000000U / sizeof(struct io_event))) {
pr_debug("ENOMEM: nr_events too high\n");
return ERR_PTR(-EINVAL);
}
nr_events *= 2;
if (!nr_events || (unsigned long)max_reqs > aio_max_nr)
return ERR_PTR(-EAGAIN);
+43 -1
View File
@@ -36,13 +36,36 @@ struct file *backing_file_open(const struct file *user_file, int flags,
const struct cred *cred)
{
const struct path *user_path = file_user_path(user_file);
struct inode *real_inode = d_inode(real_path->dentry);
struct file *f;
int error;
/*
* may_open() checked the inode of the user file, which need not carry
* S_APPEND and S_IMMUTABLE of the backing inode.
*/
if ((flags & O_ACCMODE) != O_RDONLY) {
if (IS_IMMUTABLE(real_inode))
return ERR_PTR(-EPERM);
if (IS_APPEND(real_inode) && !(flags & O_APPEND))
return ERR_PTR(-EPERM);
}
f = alloc_empty_backing_file(flags, cred, user_file);
if (IS_ERR(f))
return f;
/*
* A user file that must not generate fsnotify events (e.g. the fd
* that fanotify creates for a permission event with
* dentry_open_nonotify()) must not generate them through its backing
* file either. Otherwise a listener watching the backing filesystem
* gets a nested permission event from its own read(2), which only
* another reader of the same group can answer.
*/
if (FMODE_FSNOTIFY_NONE(user_file->f_mode))
file_set_fsnotify_mode(f, FMODE_NONOTIFY);
path_get(user_path);
backing_file_set_user_path(f, user_path);
error = vfs_open(real_path, f);
@@ -84,6 +107,7 @@ struct backing_aio {
refcount_t ref;
struct kiocb *orig_iocb;
/* used for aio completion */
bool is_write;
void (*end_write)(struct kiocb *iocb, ssize_t);
struct work_struct work;
long res;
@@ -124,7 +148,8 @@ static void backing_aio_rw_complete(struct kiocb *iocb, long res)
struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
struct kiocb *orig_iocb = aio->orig_iocb;
if (iocb->ki_flags & IOCB_WRITE)
/* Pairs with kiocb_start_write() in vfs_iocb_iter_write() */
if (aio->is_write)
kiocb_end_write(iocb);
backing_aio_cleanup(aio, res);
@@ -239,6 +264,7 @@ static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
return -ENOMEM;
aio->orig_iocb = iocb;
aio->is_write = true;
aio->end_write = end_write;
kiocb_clone(&aio->iocb, iocb, get_file(file));
aio->iocb.ki_flags = flags;
@@ -263,6 +289,14 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
if (!iov_iter_count(iter))
return 0;
/*
* F_SETFL and RWF_NOAPPEND can take append mode away from the user
* file. They checked its inode, not the backing one.
*/
if ((file->f_flags & O_APPEND) && !(flags & IOCB_APPEND) &&
IS_APPEND(file_inode(file)))
return -EPERM;
ret = file_remove_privs(iocb->ki_filp);
if (ret)
return ret;
@@ -309,6 +343,10 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
if (!out->f_op->splice_write)
return -EINVAL;
/* splice refuses a user file in append mode, so F_SETFL cleared it */
if ((out->f_flags & O_APPEND) && IS_APPEND(file_inode(out)))
return -EPERM;
ret = file_remove_privs(iocb->ki_filp);
if (ret)
return ret;
@@ -338,6 +376,10 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
if (!can_mmap_file(file))
return -ENODEV;
/* do_mmap() checked the inode of the user file */
if ((vma->vm_flags & VM_SHARED) && IS_APPEND(file_inode(file)))
return -EACCES;
vma_set_file(vma, file);
scoped_with_creds(ctx->cred) {
+4 -2
View File
@@ -1198,8 +1198,10 @@ static inline bool coredump_skip(const struct coredump_params *cprm,
return false;
}
static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
size_t **argv, int *argc, const struct linux_binfmt *binfmt)
static noinline_for_stack void do_coredump(struct core_name *cn,
struct coredump_params *cprm,
size_t **argv, int *argc,
const struct linux_binfmt *binfmt)
{
trace_coredump(cprm->siginfo->si_signo);
+5 -14
View File
@@ -452,10 +452,9 @@ static void dentry_free(struct dentry *dentry)
}
/*
* If inode is unlinked and doesn't have any aliases (i.e., all fds pointing to
* it are closed), it is pretty much dead. Except that file handle lookup could
* still revive it which causes issues to fsnotify. So once inode reaches this
* state we make sure to block creating any new aliases.
* When inode is unlinked and doesn't have any aliases (i.e., all fds pointing
* to it are closed) we report FS_DELETE_SELF to fsnotify and cleanup all inode
* marks.
*/
static bool inode_notify_dead(struct inode *inode)
{
@@ -2312,12 +2311,7 @@ static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
sb = inode->i_sb;
spin_lock(&inode->i_lock);
if (!inode_notify_dead(inode))
res = __d_find_any_alias(inode); /* existing alias? */
else
res = ERR_PTR(-ESTALE);
spin_unlock(&inode->i_lock);
res = d_find_any_alias(inode); /* existing alias? */
if (res)
goto out;
@@ -2329,10 +2323,7 @@ static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
security_d_instantiate(new, inode);
spin_lock(&inode->i_lock);
if (!inode_notify_dead(inode))
res = __d_find_any_alias(inode); /* recheck under lock */
else
res = ERR_PTR(-ESTALE);
res = __d_find_any_alias(inode); /* recheck under lock */
if (likely(!res)) { /* still no alias, attach a disconnected dentry */
unsigned add_flags = d_flags_for_inode(inode);
+13 -5
View File
@@ -1982,11 +1982,22 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi,
{
int pwake = 0;
poll_table pt;
int error;
lockdep_assert_irqs_enabled();
init_poll_funcptr(&pt, NULL);
/*
* Create the wakeup source before committing the new event mask.
* Leave the old mask and data unchanged if allocation fails.
*/
if ((event->events & EPOLLWAKEUP) && !ep_has_wakeup_source(epi)) {
error = ep_create_wakeup_source(epi);
if (error)
return error;
}
/*
* Set the new event interest mask before calling f_op->poll();
* otherwise we might miss an event that happens between the
@@ -1994,12 +2005,9 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi,
*/
epi->event.events = event->events; /* need barrier below */
epi->event.data = event->data; /* protected by mtx */
if (epi->event.events & EPOLLWAKEUP) {
if (!ep_has_wakeup_source(epi))
ep_create_wakeup_source(epi);
} else if (ep_has_wakeup_source(epi)) {
if (!(epi->event.events & EPOLLWAKEUP) &&
ep_has_wakeup_source(epi))
ep_destroy_wakeup_source(epi);
}
/*
* The following barrier has two effects:
+5 -1
View File
@@ -336,7 +336,11 @@ static int get_name(const struct path *path, char *name, struct dentry *child)
}
out_close:
fput(file);
/*
* @file is the read-only directory opened above, so __fput()
* cannot block on anything the caller holds.
*/
__fput_sync(file);
out:
return error;
}
+14 -4
View File
@@ -468,12 +468,12 @@ static bool inode_do_switch_wbs(struct inode *inode,
if (inode_state_read(inode) & I_DIRTY_ALL) {
/*
* We need to keep b_dirty list sorted by
* dirtied_time_when. However properly sorting the
* dirtied_when. However properly sorting the
* inode in the list gets too expensive when switching
* many inodes. So just attach inode at the end of the
* dirty list and clobber the dirtied_time_when.
* dirty list and clobber the dirtied_when.
*/
inode->dirtied_time_when = jiffies;
inode->dirtied_when = jiffies;
inode_io_list_move_locked(inode, new_wb,
&new_wb->b_dirty);
} else {
@@ -566,6 +566,7 @@ relock:
}
}
wb_io_lists_depopulated(old_wb);
spin_unlock(&new_wb->list_lock);
spin_unlock(&old_wb->list_lock);
@@ -789,6 +790,13 @@ bool cleanup_offline_cgwb(struct bdi_writeback *wb)
new_wb = &wb->bdi->wb; /* wb_get() is noop for bdi's wb */
nr = 0;
/*
* The scan moves walked inodes to the newest end of b_dirty_time,
* past ones it didn't reach. If one of those was dirtied after a
* sync's cutoff, the sync's queue_io() stops there and misses the
* moved ones. Exclude syncs as process_inode_switch_wbs() does.
*/
down_read(&wb->bdi->wb_switch_rwsem);
spin_lock(&wb->list_lock);
/*
* In addition to the inodes that have completed writeback, also switch
@@ -803,6 +811,7 @@ bool cleanup_offline_cgwb(struct bdi_writeback *wb)
restart = isw_prepare_wbs_switch(new_wb, isw, &wb->b_dirty_time,
&nr);
spin_unlock(&wb->list_lock);
up_read(&wb->bdi->wb_switch_rwsem);
/* no attached inodes? bail out */
if (nr == 0) {
@@ -2570,8 +2579,9 @@ void filemap_dontcache_kick_writeback(struct address_space *mapping)
struct wb_lock_cookie cookie = {};
bool need_wakeup = false;
/* An inode which was never dirtied has no wb and nothing to flush. */
wb = unlocked_inode_to_wb_begin(inode, &cookie);
if (wb_has_dirty_io(wb) &&
if (wb && wb_has_dirty_io(wb) &&
!test_bit(WB_start_dontcache, &wb->state) &&
!test_and_set_bit(WB_start_dontcache, &wb->state)) {
wb_get(wb);
+14 -1
View File
@@ -28,6 +28,19 @@ void pin_insert(struct fs_pin *pin, struct vfsmount *m)
spin_unlock(&pin_lock);
}
/*
* Unlike autoremove_wake_function(), dequeue even if the waiter was already
* running: pin_kill() trusts @p only while its entry is still queued.
*/
static int pin_kill_wake_function(struct wait_queue_entry *wait,
unsigned int mode, int sync, void *key)
{
int ret = default_wake_function(wait, mode, sync, key);
list_del_init_careful(&wait->entry);
return ret;
}
void pin_kill(struct fs_pin *p)
{
wait_queue_entry_t wait;
@@ -36,7 +49,7 @@ void pin_kill(struct fs_pin *p)
rcu_read_unlock();
return;
}
init_wait(&wait);
init_wait_func(&wait, pin_kill_wake_function);
spin_lock_irq(&p->wait.lock);
if (likely(!p->done)) {
p->done = -1;
+13 -2
View File
@@ -593,12 +593,11 @@ static void inode_pin_lru_isolating(struct inode *inode)
static void inode_unpin_lru_isolating(struct inode *inode)
{
spin_lock(&inode->i_lock);
lockdep_assert_held(&inode->i_lock);
WARN_ON(!(inode_state_read(inode) & I_LRU_ISOLATING));
inode_state_clear(inode, I_LRU_ISOLATING);
/* Called with inode->i_lock which ensures memory ordering. */
inode_wake_up_bit(inode, __I_LRU_ISOLATING);
spin_unlock(&inode->i_lock);
}
static void inode_wait_for_lru_isolating(struct inode *inode)
@@ -942,6 +941,12 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
if (!spin_trylock(&inode->i_lock))
return LRU_SKIP;
/* Another walker is dropping its page cache, leave it to that one */
if (inode_state_read(inode) & I_LRU_ISOLATING) {
spin_unlock(&inode->i_lock);
return LRU_SKIP;
}
/*
* Inodes can get referenced, redirtied, or repopulated while
* they're already on the LRU, and this can make them
@@ -981,7 +986,13 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
else
__count_vm_events(PGINODESTEAL, reap);
mm_account_reclaimed_pages(reap);
spin_lock(&inode->i_lock);
/* The walk restarts here: don't retry leftovers right away */
if (!mapping_empty(&inode->i_data))
inode_state_set(inode, I_REFERENCED);
inode_unpin_lru_isolating(inode);
spin_unlock(&inode->i_lock);
return LRU_RETRY;
}
-1
View File
@@ -137,7 +137,6 @@ extern int reconfigure_super(struct fs_context *);
extern bool super_trylock_shared(struct super_block *sb);
struct super_block *user_get_super(dev_t, bool excl);
void put_super(struct super_block *sb);
void __init super_dev_init(void);
extern bool mount_capable(struct fs_context *);
/*
+1 -1
View File
@@ -109,7 +109,7 @@ int iomap_bio_read_folio_range(const struct iomap_iter *iter,
if (!bio ||
bio_end_sector(bio) != iomap_sector(&iter->iomap, iter->pos) ||
bio->bi_iter.bi_size > iomap_max_bio_size(&iter->iomap) - plen ||
!iomap_bio_has_room(&iter->iomap, bio, plen) ||
!bio_add_folio(bio, folio, plen, offset_in_folio(folio, iter->pos)))
iomap_read_alloc_bio(iter, ctx, plen);
return 0;
+2 -1
View File
@@ -920,6 +920,7 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
if (iomap_block_needs_zeroing(iter, block_start)) {
if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
return -EIO;
/* uptodate once copied, see __iomap_write_end() */
folio_zero_segments(folio, poff, from, to, poff + plen);
} else {
const struct iomap *iomap = iomap_iter_srcmap(iter);
@@ -943,8 +944,8 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
if (iomap->flags & IOMAP_F_ZERO_TAIL)
folio_zero_segment(folio, to, poff + plen);
iomap_set_range_uptodate(folio, poff, plen);
}
iomap_set_range_uptodate(folio, poff, plen);
} while ((block_start += plen) < block_end);
return 0;
+16 -10
View File
@@ -590,18 +590,24 @@ out:
static int iomap_dio_hole_iter(struct iomap_iter *iter, struct iomap_dio *dio)
{
loff_t copied = iov_iter_zero(iomap_length(iter), dio->submit.iter);
unsigned int bs = i_blocksize(iter->inode);
loff_t aligned_copied = round_down(copied, bs);
/*
* If fs block size is larger than page size, page fault failure
* can cause @copied to be page aligned but not fs block aligned.
*/
iov_iter_revert(dio->submit.iter, copied - aligned_copied);
dio->size += aligned_copied;
if (!aligned_copied)
if (dio->flags & IOMAP_DIO_FSBLOCK_ALIGNED) {
loff_t aligned_copied = round_down(copied,
i_blocksize(iter->inode));
/*
* For fs passing IOMAP_DIO_FSBLOCK_ALIGNED flag, if fs block
* size is larger than page size, page fault failure
* can cause @copied to be page aligned but not fs block
* aligned.
*/
iov_iter_revert(dio->submit.iter, copied - aligned_copied);
copied = aligned_copied;
}
dio->size += copied;
if (!copied)
return -EFAULT;
return iomap_iter_advance(iter, aligned_copied);
return iomap_iter_advance(iter, copied);
}
static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio)
+9
View File
@@ -17,6 +17,15 @@ static inline size_t iomap_max_bio_size(const struct iomap *iomap)
return BIO_MAX_SIZE;
}
/* Can @len more bytes go into @bio? */
static inline bool iomap_bio_has_room(const struct iomap *iomap,
const struct bio *bio, size_t len)
{
size_t max_size = iomap_max_bio_size(iomap);
return len <= max_size && bio->bi_iter.bi_size <= max_size - len;
}
u32 iomap_finish_ioend_buffered_read(struct iomap_ioend *ioend);
u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend);
+1 -2
View File
@@ -133,8 +133,7 @@ static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos,
{
struct iomap_ioend *ioend = wpc->wb_ctx;
if (ioend->io_bio.bi_iter.bi_size >
iomap_max_bio_size(&wpc->iomap) - map_len)
if (!iomap_bio_has_room(&wpc->iomap, &ioend->io_bio, map_len))
return false;
if (ioend_flags & IOMAP_IOEND_BOUNDARY)
return false;
+7 -5
View File
@@ -1103,10 +1103,11 @@ int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *c
if (IS_ERR(mnt))
return PTR_ERR(mnt);
spin_lock(&pin_fs_lock);
if (!*mount)
if (!*mount) {
*mount = mnt;
mnt = NULL;
}
}
mntget(*mount);
++*count;
spin_unlock(&pin_fs_lock);
mntput(mnt);
@@ -1116,11 +1117,12 @@ EXPORT_SYMBOL(simple_pin_fs);
void simple_release_fs(struct vfsmount **mount, int *count)
{
struct vfsmount *mnt;
struct vfsmount *mnt = NULL;
spin_lock(&pin_fs_lock);
mnt = *mount;
if (!--*count)
if (!--*count) {
mnt = *mount;
*mount = NULL;
}
spin_unlock(&pin_fs_lock);
mntput(mnt);
}
+22 -4
View File
@@ -39,6 +39,24 @@ static __u32 count_free(struct buffer_head *map[], unsigned blocksize, __u32 num
return sum;
}
/*
* The bitmap buffers stay pinned for the lifetime of the mount and their
* in-memory contents are authoritative. A failed writeback clears
* BH_Uptodate on them, so restore it before redirtying the buffer instead
* of tripping the warning in mark_buffer_dirty(). The buffer lock keeps
* this from racing with a write that is still in flight.
*/
static void minix_mark_bitmap_dirty(struct buffer_head *bh)
{
lock_buffer(bh);
if (!buffer_uptodate(bh)) {
clear_buffer_write_io_error(bh);
set_buffer_uptodate(bh);
}
mark_buffer_dirty(bh);
unlock_buffer(bh);
}
void minix_free_block(struct inode *inode, unsigned long block)
{
struct super_block *sb = inode->i_sb;
@@ -64,7 +82,7 @@ void minix_free_block(struct inode *inode, unsigned long block)
printk("minix_free_block (%s:%lu): bit already cleared\n",
sb->s_id, block);
spin_unlock(&bitmap_lock);
mark_buffer_dirty(bh);
minix_mark_bitmap_dirty(bh);
return;
}
@@ -83,7 +101,7 @@ int minix_new_block(struct inode * inode)
if (j < bits_per_zone) {
minix_set_bit(j, bh->b_data);
spin_unlock(&bitmap_lock);
mark_buffer_dirty(bh);
minix_mark_bitmap_dirty(bh);
j += i * bits_per_zone + sbi->s_firstdatazone-1;
if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
break;
@@ -207,7 +225,7 @@ void minix_free_inode(struct inode * inode)
if (!minix_test_and_clear_bit(bit, bh->b_data))
printk("minix_free_inode: bit %lu already cleared\n", bit);
spin_unlock(&bitmap_lock);
mark_buffer_dirty(bh);
minix_mark_bitmap_dirty(bh);
}
struct inode *minix_new_inode(const struct inode *dir, umode_t mode)
@@ -243,7 +261,7 @@ struct inode *minix_new_inode(const struct inode *dir, umode_t mode)
return ERR_PTR(-ENOSPC);
}
spin_unlock(&bitmap_lock);
mark_buffer_dirty(bh);
minix_mark_bitmap_dirty(bh);
j += i * bits_per_zone;
if (!j || j > sbi->s_ninodes) {
iput(inode);
+4
View File
@@ -172,6 +172,10 @@ static int minix_rmdir(struct inode * dir, struct dentry *dentry)
minix_error_inode(dir, "inode has corrupted nlink");
goto out;
}
if (inode->i_nlink < 2) {
minix_error_inode(inode, "inode has corrupted nlink");
goto out;
}
err = -ENOTEMPTY;
if (!minix_empty_dir(inode))
+8 -2
View File
@@ -683,8 +683,14 @@ EXPORT_SYMBOL(inode_permission);
static __always_inline int lookup_inode_permission_may_exec(const struct mnt_idmap *idmap,
struct inode *inode, int mask)
{
/* Lookup already checked this to return -ENOTDIR */
VFS_BUG_ON_INODE(!S_ISDIR(inode->i_mode), inode);
/*
* Lookup already checked this to return -ENOTDIR. In rcu-walk that
* check looks at ->d_flags after ->d_seq had been validated, so it can
* see a dentry which went negative and then became a directory while
* nd->inode is still the old non-directory. Any such walk fails the
* next ->d_seq check and gets redone in ref-walk.
*/
VFS_BUG_ON_INODE(!S_ISDIR(inode->i_mode) && !(mask & MAY_NOT_BLOCK), inode);
VFS_BUG_ON((mask & ~MAY_NOT_BLOCK) != 0);
mask |= MAY_EXEC;
+5 -4
View File
@@ -6067,7 +6067,8 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
ks->bufsize = bufsize;
if (ks->mask & STATMOUNT_STRING_REQ) {
if (bufsize == sizeof(ks->sm))
/* Even the reserved empty string needs room past the fixed part. */
if (bufsize <= sizeof(ks->sm))
return -EOVERFLOW;
ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
@@ -6261,6 +6262,8 @@ static ssize_t do_listmount(struct klistmount *kls, bool reverse)
} else {
if (reverse)
first = mnt_find_id_at_reverse(ns, last_mnt_id - 1);
else if (last_mnt_id == U64_MAX)
first = NULL; /* nothing comes after the largest id */
else
first = mnt_find_id_at(ns, last_mnt_id + 1);
}
@@ -6492,8 +6495,6 @@ void __init mnt_init(void)
HASH_ZERO,
&mp_hash_shift, &mp_hash_mask, 0, 0);
super_dev_init();
kernfs_init();
err = sysfs_init();
@@ -6726,7 +6727,7 @@ static int mntns_install(struct nsset *nsset, struct ns_common *ns)
if (is_anon_ns(mnt_ns))
return -EINVAL;
if (fs->users != 1)
if (current->fs->users != 1)
return -EINVAL;
get_mnt_ns(mnt_ns);
+29 -3
View File
@@ -517,6 +517,32 @@ void fscache_create_volume(struct fscache_volume *volume, bool wait);
#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
#define FSCACHE_DEBUG_CACHE 0
#define FSCACHE_DEBUG_COOKIE 1
#define FSCACHE_DEBUG_OBJECT 2
#define FSCACHE_DEBUG_OPERATION 3
#define FSCACHE_POINT_ENTER 1
#define FSCACHE_POINT_LEAVE 2
#define FSCACHE_POINT_DEBUG 4
#ifndef FSCACHE_DEBUG_LEVEL
#define FSCACHE_DEBUG_LEVEL CACHE
#endif
/*
* Determine whether a particular optional debugging point should be logged.
* Three levels of indirection are needed to expand FSCACHE_DEBUG_LEVEL before
* joining it with its prefix.
*/
#define ____do_kdebug(LEVEL, POINT) \
unlikely(netfs_debug & \
(FSCACHE_POINT_##POINT << (FSCACHE_DEBUG_ ## LEVEL * 3)))
#define ___do_kdebug(LEVEL, POINT) \
____do_kdebug(LEVEL, POINT)
#define __do_kdebug(POINT) \
___do_kdebug(FSCACHE_DEBUG_LEVEL, POINT)
#ifdef __KDEBUG
#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
@@ -525,19 +551,19 @@ void fscache_create_volume(struct fscache_volume *volume, bool wait);
#elif defined(CONFIG_NETFS_DEBUG)
#define _enter(FMT, ...) \
do { \
if (netfs_debug) \
if (__do_kdebug(ENTER)) \
kenter(FMT, ##__VA_ARGS__); \
} while (0)
#define _leave(FMT, ...) \
do { \
if (netfs_debug) \
if (__do_kdebug(LEAVE)) \
kleave(FMT, ##__VA_ARGS__); \
} while (0)
#define _debug(FMT, ...) \
do { \
if (netfs_debug) \
if (__do_kdebug(DEBUG)) \
kdebug(FMT, ##__VA_ARGS__); \
} while (0)
+2 -2
View File
@@ -529,8 +529,8 @@ static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
/* Check that any trailing bytes are zero. */
if ((fh_len > NSFS_FID_SIZE_U32_LATEST) &&
memchr_inv((void *)fid + NSFS_FID_SIZE_U32_LATEST, 0,
fh_len - NSFS_FID_SIZE_U32_LATEST))
memchr_inv((void *)fid + NSFS_FILE_HANDLE_SIZE_LATEST, 0,
(fh_len - NSFS_FID_SIZE_U32_LATEST) * sizeof(u32)))
return NULL;
switch (fh_type) {
+45 -6
View File
@@ -362,6 +362,12 @@ static __u32 pidfs_coredump_mask(enum task_dumpable dumpable)
PIDFD_INFO_COREDUMP_SIGNAL | \
PIDFD_INFO_COREDUMP_CODE)
static bool pidfs_task_owns_pid(struct task_struct *task, struct pid *pid)
{
guard(rcu)();
return pid_task(pid, PIDTYPE_PID) == task;
}
static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
{
struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
@@ -392,6 +398,16 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
return -EREMOTE;
attr = READ_ONCE(pid->attr);
again:
/*
* Look for the task first. pidfs_exit() records the exit information
* before the task is unhashed, so if the task is gone the information
* is there.
*/
task = get_pid_task(pid, PIDTYPE_PID);
if (!task)
smp_rmb(); /* pairs with smp_mb__after_atomic() in pidfs_exit() */
if (mask & PIDFD_INFO_EXIT) {
if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask)) {
smp_rmb();
@@ -414,7 +430,6 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
}
}
task = get_pid_task(pid, PIDTYPE_PID);
if (!task) {
/*
* If the task has already been reaped, only exit
@@ -468,16 +483,27 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
* task might return as soon as the copy_to_user finishes, but that's ok
* and userspace expects that might happen and can act accordingly, so
* this is just best-effort. What we can do however is checking that all
* the fields are set correctly, or return ESRCH to avoid providing
* incomplete information. */
* the fields are set correctly, to avoid providing incomplete
* information.
*/
kinfo.ppid = task_ppid_vnr(task);
kinfo.tgid = task_tgid_vnr(task);
kinfo.pid = task_pid_vnr(task);
kinfo.pid = pid_vnr(pid);
kinfo.mask |= PIDFD_INFO_PID;
if (kinfo.pid == 0 || kinfo.tgid == 0)
return -ESRCH;
/*
* The task was reaped, or lost @pid to a sub-thread that called exec,
* while we were looking at it. Start over: there is exit information
* now, or a new task. Don't go by ->thread_pid, exchange_tids()
* updates it only after it has moved @pid.
*/
if (!pidfs_task_owns_pid(task, pid) || kinfo.tgid == 0) {
put_task_struct(task);
task = NULL;
memset(&kinfo, 0, sizeof(kinfo));
goto again;
}
copy_out:
if (mask & PIDFD_INFO_SUPPORTED_MASK) {
@@ -485,6 +511,14 @@ copy_out:
kinfo.supported_mask = PIDFD_INFO_SUPPORTED;
}
/* Only report fields that fit into the caller's struct. */
if (usize < PIDFD_INFO_SIZE_VER1)
kinfo.mask &= ~(PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL);
if (usize < PIDFD_INFO_SIZE_VER2)
kinfo.mask &= ~PIDFD_INFO_COREDUMP_CODE;
if (usize < PIDFD_INFO_SIZE_VER3)
kinfo.mask &= ~PIDFD_INFO_SUPPORTED_MASK;
/* Are there bits in the return mask not present in PIDFD_INFO_SUPPORTED? */
WARN_ON_ONCE(~PIDFD_INFO_SUPPORTED & kinfo.mask);
/*
@@ -790,6 +824,11 @@ void pidfs_exit(struct task_struct *tsk)
/* Ensure that PIDFD_GET_INFO sees either all or nothing. */
smp_wmb();
set_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask);
/*
* Whoever sees that the task is unhashed must see the bit, see
* pidfd_info(). Taking tasklist_lock does not order the two stores.
*/
smp_mb__after_atomic();
}
#ifdef CONFIG_COREDUMP
+3 -2
View File
@@ -256,9 +256,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
inode = proc_get_inode(dir->i_sb, de);
if (!inode)
return ERR_PTR(-ENOMEM);
/* never reused and so never a mountpoint for long */
if (de->flags & PROC_ENTRY_FORCE_LOOKUP)
return d_splice_alias_ops(inode, dentry,
&proc_net_dentry_ops);
return proc_splice_unmountable(inode, dentry,
&proc_net_dentry_ops);
return d_splice_alias_ops(inode, dentry,
&proc_misc_dentry_ops);
}
+5 -3
View File
@@ -531,12 +531,14 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
* again until we are done.
*/
while (true) {
read += vread_iter(iter, src, left);
size_t n = vread_iter(iter, src, left);
read += n;
if (read == tsz)
break;
src += read;
left -= read;
src += n;
left -= n;
if (fault_in_iov_iter_writeable(iter, left)) {
ret = -EFAULT;
+24 -22
View File
@@ -441,7 +441,7 @@ SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
whence);
retval = (int)offset;
if (offset >= 0) {
if (offset >= 0 || (unsigned_offsets(fd_file(f)) && offset < -MAX_ERRNO)) {
retval = -EFAULT;
if (!copy_to_user(result, &offset, sizeof(offset)))
retval = 0;
@@ -753,13 +753,13 @@ SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
loff_t pos)
{
if (pos < 0)
return -EINVAL;
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;
if (pos < 0 && !unsigned_offsets(fd_file(f)))
return -EINVAL;
if (fd_file(f)->f_mode & FMODE_PREAD)
return vfs_read(fd_file(f), buf, count, &pos);
@@ -783,13 +783,13 @@ COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
size_t count, loff_t pos)
{
if (pos < 0)
return -EINVAL;
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;
if (pos < 0 && !unsigned_offsets(fd_file(f)))
return -EINVAL;
if (fd_file(f)->f_mode & FMODE_PWRITE)
return vfs_write(fd_file(f), buf, count, &pos);
@@ -1123,14 +1123,14 @@ static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
{
ssize_t ret = -EBADF;
if (pos < 0)
return -EINVAL;
CLASS(fd, f)(fd);
if (!fd_empty(f)) {
ret = -ESPIPE;
if (fd_file(f)->f_mode & FMODE_PREAD)
ret = vfs_readv(fd_file(f), vec, vlen, &pos, flags);
ret = -EINVAL;
if (pos >= 0 || unsigned_offsets(fd_file(f))) {
ret = -ESPIPE;
if (fd_file(f)->f_mode & FMODE_PREAD)
ret = vfs_readv(fd_file(f), vec, vlen, &pos, flags);
}
}
if (ret > 0)
@@ -1144,14 +1144,14 @@ static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
{
ssize_t ret = -EBADF;
if (pos < 0)
return -EINVAL;
CLASS(fd, f)(fd);
if (!fd_empty(f)) {
ret = -ESPIPE;
if (fd_file(f)->f_mode & FMODE_PWRITE)
ret = vfs_writev(fd_file(f), vec, vlen, &pos, flags);
ret = -EINVAL;
if (pos >= 0 || unsigned_offsets(fd_file(f))) {
ret = -ESPIPE;
if (fd_file(f)->f_mode & FMODE_PWRITE)
ret = vfs_writev(fd_file(f), vec, vlen, &pos, flags);
}
}
if (ret > 0)
@@ -1531,9 +1531,11 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
else
count = min(count, size_in - (uint64_t)pos_in);
ret = generic_write_check_limits(file_out, pos_out, &count);
if (ret)
return ret;
if (count) {
ret = generic_write_check_limits(file_out, pos_out, &count);
if (ret)
return ret;
}
/* Don't allow overlapped copying within the same file. */
if (inode_in == inode_out &&
+37 -18
View File
@@ -1377,10 +1377,10 @@ ssize_t do_splice(struct file *in, loff_t *off_in, struct file *out,
ret = do_splice_from(ipipe, out, &offset, len, flags);
file_end_write(out);
if (!off_out)
out->f_pos = offset;
else
if (off_out)
*off_out = offset;
else if (ret >= 0)
out->f_pos = offset;
} else if (opipe) {
if (off_out)
return -ESPIPE;
@@ -1401,10 +1401,10 @@ ssize_t do_splice(struct file *in, loff_t *off_in, struct file *out,
ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
if (!off_in)
in->f_pos = offset;
else
if (off_in)
*off_in = offset;
else if (ret >= 0)
in->f_pos = offset;
} else {
ret = -EINVAL;
}
@@ -1884,6 +1884,15 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
unsigned int i_tail, o_tail;
ssize_t ret = 0;
retry:
ret = ipipe_prep(ipipe, flags);
if (ret)
return ret;
ret = opipe_prep(opipe, flags);
if (ret)
return ret;
/*
* Potential ABBA deadlock, work around it by ordering lock
* grabbing by pipe info address. Otherwise two different processes
@@ -1905,13 +1914,32 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe,
i_head = ipipe->head;
o_tail = opipe->tail;
if (pipe_empty(i_head, i_tail) && !ipipe->writers)
break;
/*
* If we have iterated all input buffers or run out of
* output room, break.
*/
if (pipe_empty(i_head, i_tail) ||
pipe_full(o_head, o_tail, opipe->max_usage))
break;
pipe_full(o_head, o_tail, opipe->max_usage)) {
if (ret)
break;
if (flags & SPLICE_F_NONBLOCK) {
ret = -EAGAIN;
break;
}
/*
* We raced with another reader/writer and haven't
* linked any buffers. A zero return value means EOF,
* so retry instead.
*/
pipe_unlock(ipipe);
pipe_unlock(opipe);
goto retry;
}
ibuf = pipe_buf(ipipe, i_tail);
obuf = pipe_buf(opipe, o_head);
@@ -1982,16 +2010,7 @@ ssize_t do_tee(struct file *in, struct file *out, size_t len,
if ((in->f_flags | out->f_flags) & O_NONBLOCK)
flags |= SPLICE_F_NONBLOCK;
/*
* Keep going, unless we encounter an error. The ipipe/opipe
* ordering doesn't really matter.
*/
ret = ipipe_prep(ipipe, flags);
if (!ret) {
ret = opipe_prep(opipe, flags);
if (!ret)
ret = link_pipe(ipipe, opipe, len, flags);
}
ret = link_pipe(ipipe, opipe, len, flags);
}
if (ret > 0) {
+142 -204
View File
@@ -25,7 +25,7 @@
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/memcontrol.h>
#include <linux/rhashtable.h>
#include <linux/hashtable.h>
#include <linux/mount.h>
#include <linux/security.h>
#include <linux/writeback.h> /* for the emergency remount stuff */
@@ -274,8 +274,6 @@ static unsigned long super_cache_count(struct shrinker *shrink,
return total_objects;
}
static struct super_dev *super_dev_alloc(dev_t dev, struct super_block *sb);
static void destroy_super_work(struct work_struct *work)
{
struct super_block *s = container_of(work, struct super_block,
@@ -283,8 +281,6 @@ static void destroy_super_work(struct work_struct *work)
fsnotify_sb_free(s);
security_sb_free(s);
put_user_ns(s->s_user_ns);
/* Only an unregistered entry is still owned by the superblock. */
kfree(s->s_super_dev);
kfree(s->s_subtype);
for (int i = 0; i < SB_FREEZE_LEVELS; i++)
percpu_free_rwsem(&s->s_writers.rw_sem[i]);
@@ -398,9 +394,6 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags,
goto fail;
if (list_lru_init_memcg(&s->s_inode_lru, s->s_shrink))
goto fail;
s->s_super_dev = super_dev_alloc(0, s);
if (!s->s_super_dev)
goto fail;
s->s_min_writeback_pages = MIN_WRITEBACK_PAGES;
return s;
@@ -435,110 +428,92 @@ void put_super(struct super_block *s)
}
}
struct super_dev {
dev_t sd_dev;
struct super_block *sd_sb;
refcount_t sd_ref;
struct rhlist_head sd_node;
struct rcu_head sd_rcu;
};
/*
* Superblocks hashed by the devices they claim: s_dev from sget_fc() to
* kill_super_notify() and every block device opened through
* fs_bdev_file_open_by_{dev,path}(). Walks keep their place with a cursor, an
* entry without a superblock.
*/
#define SUPER_DEV_HASH_BITS 8
static DEFINE_HASHTABLE(super_dev_hash, SUPER_DEV_HASH_BITS);
static DEFINE_SPINLOCK(super_dev_lock);
static struct rhltable super_dev_table;
static const struct rhashtable_params super_dev_params = {
.key_len = sizeof(dev_t),
.key_offset = offsetof(struct super_dev, sd_dev),
.head_offset = offsetof(struct super_dev, sd_node),
};
static struct super_dev *super_dev_alloc(dev_t dev, struct super_block *sb)
static void super_dev_add(struct super_dev *sd, struct super_block *sb, dev_t dev)
{
struct super_dev *fsd;
lockdep_assert_held(&super_dev_lock);
fsd = kzalloc_obj(*fsd);
if (!fsd)
return NULL;
fsd->sd_dev = dev;
fsd->sd_sb = sb;
refcount_set(&fsd->sd_ref, 1);
return fsd;
sd->sd_sb = sb;
sd->sd_dev = dev;
sd->sd_claims = 1;
refcount_inc(&sb->s_passive);
hash_add(super_dev_hash, &sd->sd_hash, dev);
}
static void super_dev_put(struct super_dev *fsd)
/* Drop a claim, the last one unhashes the entry. */
static void super_dev_put(struct super_dev *sd)
{
/* Unlink only once unpinned, so a cursor never resumes from a removed node. */
if (fsd && refcount_dec_and_test(&fsd->sd_ref)) {
rhltable_remove(&super_dev_table, &fsd->sd_node, super_dev_params);
put_super(fsd->sd_sb);
kfree_rcu(fsd, sd_rcu);
struct super_block *sb = sd->sd_sb;
scoped_guard(spinlock, &super_dev_lock) {
if (--sd->sd_claims)
return;
hash_del(&sd->sd_hash);
}
if (sd != &sb->s_super_dev)
kfree(sd);
put_super(sb);
}
/*
* Return the superblock of the next claim behind @cursor with a passive
* reference and put @prev. The mount of a superblock that isn't born yet may
* still drop its claim, so wait for it to be born or to die and look again.
*/
static struct super_block *super_dev_next(struct super_dev *cursor,
struct super_block *prev)
{
struct super_block *sb;
struct super_dev *sd;
if (prev)
put_super(prev);
for (;;) {
scoped_guard(spinlock, &super_dev_lock) {
if (!hash_hashed(&cursor->sd_hash))
hash_add(super_dev_hash, &cursor->sd_hash, cursor->sd_dev);
sd = cursor;
hlist_for_each_entry_continue(sd, sd_hash) {
if (sd->sd_sb && sd->sd_dev == cursor->sd_dev)
break;
}
if (!sd) {
hash_del(&cursor->sd_hash);
return NULL;
}
sb = sd->sd_sb;
refcount_inc(&sb->s_passive);
if (super_flags(sb, SB_BORN | SB_DYING)) {
hash_del(&cursor->sd_hash);
hlist_add_behind(&cursor->sd_hash, &sd->sd_hash);
return sb;
}
}
wait_var_event(&sb->s_flags, super_flags(sb, SB_BORN | SB_DYING));
put_super(sb);
}
}
void __init super_dev_init(void)
/* Unhash the cursor of a walk that stops early. */
static void super_dev_walk_end(struct super_dev *cursor)
{
if (rhltable_init(&super_dev_table, &super_dev_params))
panic("VFS: Cannot initialise super_dev_table\n");
guard(spinlock)(&super_dev_lock);
hash_del(&cursor->sd_hash);
}
static int super_dev_insert(struct super_dev *fsd)
{
int err;
err = rhltable_insert(&super_dev_table, &fsd->sd_node, super_dev_params);
if (!err)
refcount_inc(&fsd->sd_sb->s_passive);
return err;
}
/* Register @sb under @sb->s_dev as the final fallible act of a set callback. */
static int super_dev_register(struct super_block *sb)
{
struct super_dev *fsd = sb->s_super_dev;
int err;
lockdep_assert_held(&sb_lock);
VFS_WARN_ON_ONCE(!sb->s_dev);
VFS_WARN_ON_ONCE(!fsd || fsd->sd_dev);
fsd->sd_dev = sb->s_dev;
err = super_dev_insert(fsd);
if (err)
fsd->sd_dev = 0;
return err;
}
static struct super_dev *super_dev_get(struct rhlist_head *pos)
{
struct super_dev *sb_dev;
for (; pos; pos = rcu_dereference_all(pos->next)) {
sb_dev = container_of(pos, struct super_dev, sd_node);
if (refcount_inc_not_zero(&sb_dev->sd_ref))
return sb_dev;
}
return NULL;
}
static struct super_dev *super_dev_first(dev_t dev)
{
struct super_dev *sb_dev;
rcu_read_lock();
sb_dev = super_dev_get(rhltable_lookup(&super_dev_table, &dev, super_dev_params));
rcu_read_unlock();
return sb_dev;
}
static struct super_dev *super_dev_next(struct super_dev *prev)
{
struct super_dev *sb_dev;
rcu_read_lock();
sb_dev = super_dev_get(rcu_dereference_all(prev->sd_node.next));
rcu_read_unlock();
super_dev_put(prev);
return sb_dev;
}
#define for_each_dev_super(sb, cursor) \
for (sb = super_dev_next(cursor, NULL); sb; sb = super_dev_next(cursor, sb))
static void kill_super_notify(struct super_block *sb)
{
@@ -548,11 +523,8 @@ static void kill_super_notify(struct super_block *sb)
if (sb->s_flags & SB_DEAD)
return;
/* Drop sget_fc()'s claim; a never-registered entry stays with the sb. */
if (sb->s_super_dev->sd_dev) {
super_dev_put(sb->s_super_dev);
sb->s_super_dev = NULL;
}
/* Drop sget_fc()'s claim on s_dev. */
super_dev_put(&sb->s_super_dev);
/*
* Let concurrent mounts know that this thing is really dead.
@@ -880,13 +852,12 @@ retry:
s->s_fs_info = fc->s_fs_info;
err = set(s, fc);
if (err) {
VFS_WARN_ON_ONCE(s->s_super_dev->sd_dev);
s->s_fs_info = NULL;
spin_unlock(&sb_lock);
destroy_unused_super(s);
return ERR_PTR(err);
}
VFS_WARN_ON_ONCE(!s->s_super_dev->sd_dev);
VFS_WARN_ON_ONCE(!s->s_dev);
fc->s_fs_info = NULL;
s->s_type = fc->fs_type;
s->s_iflags |= fc->s_iflags;
@@ -899,6 +870,8 @@ retry:
list_add_tail(&s->s_list, &super_blocks);
hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
spin_unlock(&sb_lock);
scoped_guard(spinlock, &super_dev_lock)
super_dev_add(&s->s_super_dev, s, s->s_dev);
get_filesystem(s->s_type);
shrinker_register(s->s_shrink);
return s;
@@ -1042,18 +1015,14 @@ EXPORT_SYMBOL(iterate_supers_type);
struct super_block *user_get_super(dev_t dev, bool excl)
{
struct super_dev *sb_dev;
struct super_dev cursor = { .sd_dev = dev };
struct super_block *sb;
for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
struct super_block *sb = sb_dev->sd_sb;
if (!super_lock(sb, excl))
continue;
/* The pinned entry holds a passive reference, take our own. */
refcount_inc(&sb->s_passive);
super_dev_put(sb_dev);
return sb;
for_each_dev_super(sb, &cursor) {
if (super_lock(sb, excl)) {
super_dev_walk_end(&cursor);
return sb;
}
}
return NULL;
}
@@ -1336,16 +1305,7 @@ EXPORT_SYMBOL(free_anon_bdev);
int set_anon_super(struct super_block *s, void *data)
{
int error;
error = get_anon_bdev(&s->s_dev);
if (error)
return error;
error = super_dev_register(s);
if (error)
free_anon_bdev(s->s_dev);
return error;
return get_anon_bdev(&s->s_dev);
}
EXPORT_SYMBOL(set_anon_super);
@@ -1442,7 +1402,7 @@ EXPORT_SYMBOL(get_tree_keyed);
static int set_bdev_super(struct super_block *s, void *data)
{
s->s_dev = *(dev_t *)data;
return super_dev_register(s);
return 0;
}
static int super_s_dev_set(struct super_block *s, struct fs_context *fc)
@@ -1502,14 +1462,12 @@ static int fs_super_thaw(struct super_block *sb)
static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
{
struct super_dev *sb_dev;
dev_t dev = bdev->bd_dev;
struct super_dev cursor = { .sd_dev = bdev->bd_dev };
struct super_block *sb;
mutex_unlock(&bdev->bd_holder_lock);
for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
struct super_block *sb = sb_dev->sd_sb;
for_each_dev_super(sb, &cursor) {
if (!super_lock_shared(sb))
continue;
if (sb->s_root && (sb->s_flags & SB_ACTIVE)) {
@@ -1529,14 +1487,12 @@ static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
static void fs_bdev_sync(struct block_device *bdev)
{
struct super_dev *sb_dev;
dev_t dev = bdev->bd_dev;
struct super_dev cursor = { .sd_dev = bdev->bd_dev };
struct super_block *sb;
mutex_unlock(&bdev->bd_holder_lock);
for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
struct super_block *sb = sb_dev->sd_sb;
for_each_dev_super(sb, &cursor) {
if (!super_lock_shared(sb))
continue;
if (sb->s_root && (sb->s_flags & SB_ACTIVE))
@@ -1559,8 +1515,8 @@ static void fs_bdev_sync(struct block_device *bdev)
*/
static int fs_bdev_freeze(struct block_device *bdev)
{
dev_t dev = bdev->bd_dev;
struct super_dev *sb_dev;
struct super_dev cursor = { .sd_dev = bdev->bd_dev };
struct super_block *sb;
unsigned int count = 0;
int error = 0, err;
@@ -1568,13 +1524,13 @@ static int fs_bdev_freeze(struct block_device *bdev)
mutex_unlock(&bdev->bd_holder_lock);
for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
if (!get_active_super(sb_dev->sd_sb))
for_each_dev_super(sb, &cursor) {
if (!get_active_super(sb))
continue;
err = fs_super_freeze(sb_dev->sd_sb);
err = fs_super_freeze(sb);
if (err && !error)
error = err;
deactivate_super(sb_dev->sd_sb);
deactivate_super(sb);
count++;
}
@@ -1604,8 +1560,8 @@ static int fs_bdev_freeze(struct block_device *bdev)
*/
static int fs_bdev_thaw(struct block_device *bdev)
{
dev_t dev = bdev->bd_dev;
struct super_dev *sb_dev;
struct super_dev cursor = { .sd_dev = bdev->bd_dev };
struct super_block *sb;
unsigned int count = 0;
int error = 0, err;
@@ -1613,13 +1569,13 @@ static int fs_bdev_thaw(struct block_device *bdev)
mutex_unlock(&bdev->bd_holder_lock);
for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
if (!get_active_super(sb_dev->sd_sb))
for_each_dev_super(sb, &cursor) {
if (!get_active_super(sb))
continue;
err = fs_super_thaw(sb_dev->sd_sb);
err = fs_super_thaw(sb);
if (err && !error)
error = err;
deactivate_super(sb_dev->sd_sb);
deactivate_super(sb);
count++;
}
@@ -1636,55 +1592,39 @@ static const struct blk_holder_ops fs_holder_ops = {
.thaw = fs_bdev_thaw,
};
static struct super_dev *super_dev_lookup(dev_t dev, struct super_block *sb)
static struct super_dev *super_dev_find(dev_t dev, const struct super_block *sb)
{
struct super_dev *it;
struct rhlist_head *list, *pos;
struct super_dev *sd;
RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious super_dev_lookup() usage");
VFS_WARN_ON_ONCE(!dev);
VFS_WARN_ON_ONCE(!sb);
lockdep_assert_held(&super_dev_lock);
list = rhltable_lookup(&super_dev_table, &dev, super_dev_params);
rhl_for_each_entry_rcu(it, pos, list, sd_node) {
if (it->sd_sb == sb)
return it;
hash_for_each_possible(super_dev_hash, sd, sd_hash, dev) {
if (sd->sd_dev == dev && sd->sd_sb == sb)
return sd;
}
return NULL;
}
static int fs_bdev_register(struct file *bdev_file, struct super_block *sb)
static int fs_bdev_register(struct block_device *bdev, struct super_block *sb)
{
struct super_dev *sb_dev __free(kfree) = NULL;
dev_t dev = file_bdev(bdev_file)->bd_dev;
int err;
struct super_dev *new __free(kfree) = NULL;
struct super_dev *sd;
scoped_guard(rcu) {
sb_dev = super_dev_lookup(dev, sb);
if (sb_dev && refcount_inc_not_zero(&sb_dev->sd_ref)) {
retain_and_null_ptr(sb_dev);
return 0;
}
new = kzalloc_obj(*new);
guard(spinlock)(&super_dev_lock);
sd = super_dev_find(bdev->bd_dev, sb);
if (sd) {
sd->sd_claims++;
return 0;
}
sb_dev = super_dev_alloc(dev, sb);
if (!sb_dev)
/* bdev_freeze() raises the count before its walk takes the lock. */
if (atomic_read(&bdev->bd_fsfreeze_count) > 0)
return -EBUSY;
if (!new)
return -ENOMEM;
err = super_dev_insert(sb_dev);
if (err)
return err;
/* Publish the entry before reading the count; pairs with bdev_freeze(). */
smp_mb();
if (atomic_read(&file_bdev(bdev_file)->bd_fsfreeze_count) > 0) {
err = -EBUSY;
super_dev_put(sb_dev);
}
retain_and_null_ptr(sb_dev);
return err;
super_dev_add(no_free_ptr(new), sb, bdev->bd_dev);
return 0;
}
/**
@@ -1711,7 +1651,7 @@ struct file *fs_bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
if (IS_ERR(bdev_file))
return bdev_file;
err = fs_bdev_register(bdev_file, sb);
err = fs_bdev_register(file_bdev(bdev_file), sb);
if (err) {
bdev_fput(bdev_file);
return ERR_PTR(err);
@@ -1745,7 +1685,7 @@ struct file *fs_bdev_file_open_by_path(const char *path, blk_mode_t mode,
if (IS_ERR(bdev_file))
return bdev_file;
err = fs_bdev_register(bdev_file, sb);
err = fs_bdev_register(file_bdev(bdev_file), sb);
if (err) {
bdev_fput(bdev_file);
return ERR_PTR(err);
@@ -1760,21 +1700,20 @@ EXPORT_SYMBOL_GPL(fs_bdev_file_open_by_path);
* @sb: superblock the device was claimed for
*
* The inverse of fs_bdev_register(): drop one claim on the {dev, @sb} entry
* (the last claim unregisters it; a pinning cursor defers the actual unlink)
* without closing the device. A caller that must act on the still-open device
* between unregistering and closing - e.g. re-allow freezing one denied for a
* membership change - pairs this with bdev_fput(). fs_bdev_file_release() is
* the common unregister-and-close.
* (the last claim unregisters it) without closing the device. A caller that
* must act on the still-open device between unregistering and closing - e.g.
* re-allow freezing one denied for a membership change - pairs this with
* bdev_fput(). fs_bdev_file_release() is the common unregister-and-close.
*/
void fs_bdev_unregister(struct file *bdev_file, struct super_block *sb)
{
dev_t dev = file_bdev(bdev_file)->bd_dev;
struct super_dev *sb_dev;
struct super_dev *sd;
rcu_read_lock();
sb_dev = super_dev_lookup(dev, sb);
rcu_read_unlock();
super_dev_put(sb_dev);
/* The claim we're dropping keeps the entry hashed. */
scoped_guard(spinlock, &super_dev_lock)
sd = super_dev_find(file_bdev(bdev_file)->bd_dev, sb);
if (sd)
super_dev_put(sd);
}
EXPORT_SYMBOL_GPL(fs_bdev_unregister);
@@ -1817,8 +1756,7 @@ int setup_bdev_super(struct super_block *sb, int sb_flags,
return -EACCES;
}
/* The sget_fc() entry is already published; pairs with bdev_freeze(). */
smp_mb();
/* sget_fc() hashed @sb, super_dev_lock orders that against bdev_freeze(). */
if (atomic_read(&bdev->bd_fsfreeze_count) > 0) {
if (fc)
warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
+1 -1
View File
@@ -332,7 +332,7 @@ vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
const void *orig_value = value;
int error;
if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
if (strcmp(name, XATTR_NAME_CAPS) == 0) {
error = cap_convert_nscap(idmap, dentry, &value, size);
if (error < 0)
return error;
+9 -4
View File
@@ -530,8 +530,8 @@ static inline bool is_delegated(struct delegated_inode *di)
*
* Break delegations in a non-blocking fashion. If there are
* outstanding delegations and @di is set, then an extra reference
* will be taken on @inode and @di->di_inode will be populated so
* that it may be waited upon.
* will be taken on @inode and its superblock, and @di->di_inode will
* be populated so that it may be waited upon.
*
* Returns 0 if there is no need to wait or an error. If -EWOULDBLOCK
* is returned, then @di will be populated (if non-NULL).
@@ -545,16 +545,21 @@ static inline int try_break_deleg(struct inode *inode, unsigned int flags,
if (ret == -EWOULDBLOCK && di) {
di->di_inode = inode;
ihold(inode);
/* The caller may drop its mount before break_deleg_wait(). */
atomic_inc(&inode->i_sb->s_active);
}
return ret;
}
static inline int break_deleg_wait(struct delegated_inode *di)
{
struct inode *inode = di->di_inode;
struct super_block *sb = inode->i_sb;
int ret;
ret = break_deleg(di->di_inode, 0);
iput(di->di_inode);
ret = break_deleg(inode, 0);
iput(inode);
deactivate_super(sb);
di->di_inode = NULL;
return ret;
}
+3 -1
View File
@@ -815,7 +815,9 @@ struct inode {
/* 32-bit hole */
struct rw_semaphore i_rwsem;
unsigned long dirtied_when; /* jiffies of first dirtying */
/* Timestamp for writeback list ordering and age checks (jiffies). */
unsigned long dirtied_when;
/* Start time for deciding when lazytime updates need writeback (jiffies). */
unsigned long dirtied_time_when;
struct hlist_node i_hash;
+9 -2
View File
@@ -30,7 +30,6 @@ struct mount;
struct mtd_info;
struct quotactl_ops;
struct shrinker;
struct super_dev;
struct unicode_map;
struct user_namespace;
struct workqueue_struct;
@@ -132,10 +131,18 @@ struct super_operations {
void (*report_error)(const struct fserror_event *event);
};
/* A superblock's claim on a device, see fs/super.c. */
struct super_dev {
struct hlist_node sd_hash;
struct super_block *sd_sb;
dev_t sd_dev;
unsigned int sd_claims;
};
struct super_block {
struct list_head s_list; /* Keep this first */
dev_t s_dev; /* search index; _not_ kdev_t */
struct super_dev *s_super_dev; /* sget_fc()'s device table claim */
struct super_dev s_super_dev; /* claim on s_dev */
unsigned char s_blocksize_bits;
unsigned long s_blocksize;
loff_t s_maxbytes; /* Max file size */
+2 -1
View File
@@ -693,7 +693,8 @@ struct iomap_dio_ops {
#define IOMAP_DIO_PARTIAL (1 << 2)
/*
* Ensure each bio is aligned to fs block size.
* Ensure every operation, including bio and hole zeroing, is aligned to fs
* block size.
*
* For filesystems which need to calculate/verify the checksum of each fs
* block. Otherwise they may not be able to handle unaligned bios.
+2 -4
View File
@@ -52,10 +52,7 @@ struct watch_queue {
* Representation of a watch on an object.
*/
struct watch {
union {
struct rcu_head rcu;
u32 info_id; /* ID to be OR'd in to info field */
};
struct rcu_head rcu;
struct watch_queue __rcu *queue; /* Queue to post events to */
struct hlist_node queue_node; /* Link in queue->watches */
struct watch_list __rcu *watch_list;
@@ -64,6 +61,7 @@ struct watch {
void *private; /* Private data for the watched object */
u64 id; /* Internal identifier */
struct kref usage; /* Object usage count */
u32 info_id; /* ID to be OR'd in to info field */
};
/*
+9 -1
View File
@@ -503,10 +503,18 @@ static int rootfs_init_fs_context(struct fs_context *fc)
return ramfs_init_fs_context(fc);
}
static void rootfs_kill_sb(struct super_block *sb)
{
if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
kill_anon_super(sb);
else
ramfs_kill_sb(sb);
}
struct file_system_type rootfs_fs_type = {
.name = "rootfs",
.init_fs_context = rootfs_init_fs_context,
.kill_sb = kill_anon_super,
.kill_sb = rootfs_kill_sb,
};
void __init init_rootfs(void)
+7
View File
@@ -255,3 +255,10 @@ const struct proc_ns_operations ipcns_operations = {
.install = ipcns_install,
.owner = ipcns_owner,
};
static int __init ipc_ns_tree_init(void)
{
ns_tree_add(&init_ipc_ns);
return 0;
}
pure_initcall(ipc_ns_tree_init);
-2
View File
@@ -45,7 +45,6 @@
#include <linux/mount.h>
#include <linux/ipc_namespace.h>
#include <linux/rhashtable.h>
#include <linux/nstree.h>
#include <linux/uaccess.h>
@@ -149,7 +148,6 @@ void shm_exit_ns(struct ipc_namespace *ns)
static int __init ipc_ns_init(void)
{
shm_init_ns(&init_ipc_ns);
ns_tree_add(&init_ipc_ns);
return 0;
}
+9 -3
View File
@@ -971,10 +971,10 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
dup_user_cpus_ptr(tsk, orig, node);
/*
* One for the user space visible state that goes away when reaped.
* One for the scheduler.
* Nobody can keep the task while copy_process() may still fail, which
* frees it no matter what.
*/
refcount_set(&tsk->rcu_users, 2);
refcount_set(&tsk->rcu_users, 0);
/* One for the rcu users */
refcount_set(&tsk->usage, 1);
#ifdef CONFIG_BLK_DEV_IO_TRACE
@@ -2509,6 +2509,12 @@ __latent_entropy struct task_struct *copy_process(
/* No more failure paths after this point. */
/*
* One for the user space visible state that goes away when reaped.
* One for the scheduler.
*/
refcount_set(&p->rcu_users, 2);
/*
* Copy seccomp details explicitly here, in case they were changed
* before holding sighand lock.
+1 -1
View File
@@ -355,6 +355,7 @@ static int prepare_nsset(unsigned flags, struct nsset *nsset)
{
struct task_struct *me = current;
nsset->flags = flags;
nsset->nsproxy = create_new_namespaces(0, me, current_user_ns(), me->fs);
if (IS_ERR(nsset->nsproxy))
return PTR_ERR(nsset->nsproxy);
@@ -375,7 +376,6 @@ static int prepare_nsset(unsigned flags, struct nsset *nsset)
goto out;
}
nsset->flags = flags;
return 0;
out:
+37 -43
View File
@@ -408,15 +408,12 @@ struct klistns {
u32 ns_type;
struct user_namespace *user_ns;
bool userns_capable;
struct ns_common *first_ns;
};
static void __free_klistns_free(const struct klistns *kls)
{
if (kls->user_ns_id != LISTNS_CURRENT_USER)
put_user_ns(kls->user_ns);
if (kls->first_ns && kls->first_ns->ops)
kls->first_ns->ops->put(kls->first_ns);
}
#define NS_ALL (PID_NS | USER_NS | MNT_NS | UTS_NS | IPC_NS | NET_NS | CGROUP_NS | TIME_NS)
@@ -459,10 +456,13 @@ static inline int prepare_klistns(struct klistns *kls, struct ns_id_req *kreq,
}
/*
* Lookup a namespace owned by owner with id >= ns_id.
* Returns the namespace with the smallest id that is >= ns_id.
* Lookup a namespace owned by owner with id > ns_id.
* Returns the namespace with the smallest id that is > ns_id.
*
* No reference is taken. The caller must hold rcu_read_lock() and may only
* use the result as the starting point of an RCU list walk.
*/
static struct ns_common *lookup_ns_owner_at(u64 ns_id, struct ns_common *owner)
static struct ns_common *lookup_ns_owner_after(u64 ns_id, struct ns_common *owner)
{
struct ns_common *ret = NULL;
struct rb_node *node;
@@ -476,18 +476,14 @@ static struct ns_common *lookup_ns_owner_at(u64 ns_id, struct ns_common *owner)
struct ns_common *ns;
ns = node_to_ns_owner(node);
if (ns_id <= ns->ns_id) {
if (ns_id < ns->ns_id) {
ret = ns;
if (ns_id == ns->ns_id)
break;
node = node->rb_left;
} else {
node = node->rb_right;
}
}
if (ret)
ret = ns_get_unless_inactive(ret);
return ret;
}
@@ -560,25 +556,27 @@ static ssize_t do_listns_userns(struct klistns *kls)
return -EINVAL;
kls->user_ns = to_user_ns(ns);
/*
* Use the rbtree to find the first namespace we care about and
* then use it's list entry to iterate from there.
*/
if (kls->last_ns_id) {
kls->first_ns = lookup_ns_owner_at(kls->last_ns_id + 1, ns);
if (!kls->first_ns)
return -ENOENT;
first_ns = kls->first_ns;
}
ret = 0;
head = &to_ns_common(kls->user_ns)->ns_owner_root.ns_list_head;
kls->userns_capable = may_see_all_namespaces();
rcu_read_lock();
if (!first_ns)
/*
* Use the rbtree to find the first namespace we care about and
* then use it's list entry to iterate from there. That namespace
* is filtered like any other, so the walk doesn't end early just
* because it is inactive or already dying.
*/
if (kls->last_ns_id) {
first_ns = lookup_ns_owner_after(kls->last_ns_id, ns);
if (!first_ns) {
rcu_read_unlock();
return -ENOENT;
}
} else {
first_ns = list_entry_rcu(head->next, typeof(*first_ns), ns_owner_node.ns_list_entry);
}
ns = first_ns;
list_for_each_entry_from_rcu(ns, head, ns_owner_node.ns_list_entry) {
@@ -613,10 +611,13 @@ static ssize_t do_listns_userns(struct klistns *kls)
}
/*
* Lookup a namespace with id >= ns_id in either the unified tree or a type-specific tree.
* Returns the namespace with the smallest id that is >= ns_id.
* Lookup a namespace with id > ns_id in either the unified tree or a type-specific tree.
* Returns the namespace with the smallest id that is > ns_id.
*
* No reference is taken. The caller must hold rcu_read_lock() and may only
* use the result as the starting point of an RCU list walk.
*/
static struct ns_common *lookup_ns_id_at(u64 ns_id, int ns_type)
static struct ns_common *lookup_ns_id_after(u64 ns_id, int ns_type)
{
struct ns_common *ret = NULL;
struct ns_tree_root *ns_tree = NULL;
@@ -643,21 +644,14 @@ static struct ns_common *lookup_ns_id_at(u64 ns_id, int ns_type)
else
ns = node_to_ns_unified(node);
if (ns_id <= ns->ns_id) {
if (ns_type)
ret = node_to_ns(node);
else
ret = node_to_ns_unified(node);
if (ns_id == ns->ns_id)
break;
if (ns_id < ns->ns_id) {
ret = ns;
node = node->rb_left;
} else {
node = node->rb_right;
}
}
if (ret)
ret = ns_get_unless_inactive(ret);
return ret;
}
@@ -707,13 +701,6 @@ static ssize_t do_listns(struct klistns *kls)
return -EINVAL;
}
if (kls->last_ns_id) {
kls->first_ns = lookup_ns_id_at(kls->last_ns_id + 1, ns_type);
if (!kls->first_ns)
return -ENOENT;
first_ns = kls->first_ns;
}
ret = 0;
if (ns_tree)
head = &ns_tree->ns_list_head;
@@ -722,8 +709,15 @@ static ssize_t do_listns(struct klistns *kls)
rcu_read_lock();
if (!first_ns)
if (kls->last_ns_id) {
first_ns = lookup_ns_id_after(kls->last_ns_id, ns_type);
if (!first_ns) {
rcu_read_unlock();
return -ENOENT;
}
} else {
first_ns = first_ns_common(head, ns_tree);
}
for (ns = first_ns; !ns_common_is_head(ns, head, ns_tree) && nr_ns_ids;
ns = next_ns_common(ns, ns_tree)) {
+3 -2
View File
@@ -684,12 +684,13 @@ static int dequeue_synchronous_signal(kernel_siginfo_t *info)
return 0;
/*
* Return the first synchronous signal in the queue.
* Return the first unblocked synchronous signal in the queue.
*/
list_for_each_entry(q, &pending->list, list) {
/* Synchronous signals have a positive si_code */
if ((q->info.si_code > SI_USER) &&
(sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
(sigmask(q->info.si_signo) & SYNCHRONOUS_MASK) &&
!sigismember(&tsk->blocked, q->info.si_signo)) {
sync = q;
goto next;
}
+13 -2
View File
@@ -974,6 +974,10 @@ BTF_ID(func, vfs_getattr)
BTF_ID(func, filp_close)
BTF_SET_END(btf_allowlist_d_path)
BTF_SET_START(btf_allowlist_d_path_fentry)
BTF_ID(func, filp_close_sync)
BTF_SET_END(btf_allowlist_d_path_fentry)
static bool bpf_d_path_allowed(const struct bpf_prog *prog)
{
if (prog->type == BPF_PROG_TYPE_TRACING &&
@@ -983,8 +987,15 @@ static bool bpf_d_path_allowed(const struct bpf_prog *prog)
if (prog->type == BPF_PROG_TYPE_LSM)
return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
return btf_id_set_contains(&btf_allowlist_d_path,
prog->aux->attach_btf_id);
if (btf_id_set_contains(&btf_allowlist_d_path,
prog->aux->attach_btf_id))
return true;
if (btf_id_set_contains(&btf_allowlist_d_path_fentry,
prog->aux->attach_btf_id))
return prog->expected_attach_type == BPF_TRACE_FENTRY;
return false;
}
BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
+17 -11
View File
@@ -285,9 +285,12 @@ static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts,
{
struct ucounts *iter, *next;
for (iter = ucounts; iter != last; iter = next) {
long dec = atomic_long_sub_return(1, &iter->rlimit[type]);
WARN_ON_ONCE(dec < 0);
long dec;
/* Our count pins @iter; once it is dropped @iter may be freed. */
next = iter->ns->ucounts;
dec = atomic_long_sub_return(1, &iter->rlimit[type]);
WARN_ON_ONCE(dec < 0);
if (dec == 0)
put_ucounts(iter);
}
@@ -308,23 +311,26 @@ long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
for (iter = ucounts; iter; iter = iter->ns->ucounts) {
long new = atomic_long_add_return(1, &iter->rlimit[type]);
if (new < 0 || new > max)
/*
* Take the extra reference on the 0 -> 1 transition before the
* limit check: a rejected walk must not leave a concurrent one
* holding the surviving increment with no reference.
*/
if (new == 1 && !get_ucounts(iter))
goto dec_unwind;
if (new < 0 || new > max) {
/* undo iter too, releasing the reference taken above */
do_dec_rlimit_put_ucounts(ucounts, iter->ns->ucounts, type);
return 0;
}
if (iter == ucounts)
ret = new;
if (!override_rlimit)
max = get_userns_rlimit_max(iter->ns, type);
/*
* Grab an extra ucount reference for the caller when
* the rlimit count was previously 0.
*/
if (new != 1)
continue;
if (!get_ucounts(iter))
goto dec_unwind;
}
return ret;
dec_unwind:
/* get_ucounts() failed for iter: undo iter without a put */
dec = atomic_long_sub_return(1, &iter->rlimit[type]);
WARN_ON_ONCE(dec < 0);
do_dec_rlimit_put_ucounts(ucounts, iter, type);
+4 -1
View File
@@ -46,8 +46,11 @@ static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
{
/* Start with the same capabilities as init but useless for doing
* anything as the capabilities are bound to the new user namespace.
* The exec securebits grant nothing, they are restrictions the task
* placed on itself. Keep them and their locks.
*/
cred->securebits = SECUREBITS_DEFAULT;
cred->securebits = SECUREBITS_DEFAULT |
(cred->securebits & (SECURE_ALL_UNPRIVILEGED | SECURE_ALL_UNPRIVILEGED << 1));
cred->cap_inheritable = CAP_EMPTY_SET;
cred->cap_permitted = CAP_FULL_SET;
cred->cap_effective = CAP_FULL_SET;
+1 -1
View File
@@ -212,7 +212,7 @@ void __post_watch_notification(struct watch_list *wlist,
if (watch->id != id)
continue;
n->info &= ~WATCH_INFO_ID;
n->info |= watch->info_id;
n->info |= watch->info_id & WATCH_INFO_ID;
wqueue = rcu_dereference(watch->queue);
wf = rcu_dereference(wqueue->filter);
+4
View File
@@ -864,6 +864,10 @@ unsigned long iov_iter_alignment(const struct iov_iter *i)
return 0;
}
/* An empty iterator may have no segment array at all. */
if (!i->count)
return 0;
/* iovec and kvec have identical layouts */
if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
return iov_iter_alignment_iovec(i);
+3 -1
View File
@@ -447,7 +447,9 @@ static __net_init int setup_net(struct net *net)
down_write(&net_rwsem);
list_add_tail_rcu(&net->list, &net_namespace_list);
up_write(&net_rwsem);
ns_tree_add_raw(net);
/* Without CONFIG_NET_NS init_net isn't exposed as a namespace. */
if (IS_ENABLED(CONFIG_NET_NS))
ns_tree_add_raw(net);
out:
return error;
+1
View File
@@ -40,6 +40,7 @@ TARGETS += filesystems/eventfd
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
TARGETS += filesystems/freeze
TARGETS += filesystems/openat2
TARGETS += filesystems/open_tree_ns
TARGETS += filesystems/overlayfs
@@ -109,8 +109,9 @@ static int trigger_fstat_events(pid_t pid)
fstat(indicatorfd, &fileStat);
out_close:
/* sys_close no longer triggers filp_close, but we can
* call sys_close_range instead which still does
/*
* filp_close_sync() may be inlined into close(2), so use
* close_range(2), which calls it directly.
*/
syscall_close(pipefd[0]);
syscall_close(pipefd[1]);
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
if (CHECK(!bss->called_close,
"close",
"trampoline for filp_close was not called\n"))
"trampoline for filp_close_sync was not called\n"))
goto cleanup;
for (int i = 0; i < MAX_FILES; i++) {
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
return 0;
}
SEC("fentry/filp_close")
SEC("fentry/filp_close_sync")
int BPF_PROG(prog_close, struct file *file, void *id)
{
pid_t pid = bpf_get_current_pid_tgid() >> 32;
@@ -45,4 +45,25 @@ __naked void d_path_reject(void)
: __clobber_all);
}
SEC("fexit/filp_close_sync")
__description("d_path fexit reject")
__failure __msg("helper call is not allowed in probe")
__naked void d_path_fexit_reject(void)
{
asm volatile (" \
r1 = *(u64 *)(r1 + 0); \
r2 = r10; \
r2 += -8; \
r6 = 0; \
*(u64*)(r2 + 0) = r6; \
r3 = 8 ll; \
call %[bpf_d_path]; \
r0 = 0; \
exit; \
"
:
: __imm(bpf_d_path)
: __clobber_all);
}
char _license[] SEC("license") = "GPL";
+83
View File
@@ -10,11 +10,13 @@
* Author: Mickaël Salaün <mic@digikod.net>
*/
#define _GNU_SOURCE
#include <asm-generic/unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/prctl.h>
#include <linux/securebits.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/capability.h>
@@ -24,6 +26,7 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#include <unistd.h>
/* Defines AT_EXECVE_CHECK without type conflicts. */
@@ -460,4 +463,84 @@ TEST_F(secbits, restrict_locked_unset)
CHILD(EXPECT_EQ(EPERM, test_secbits_set(0)));
}
static const unsigned int exec_bits = SECBIT_EXEC_RESTRICT_FILE |
SECBIT_EXEC_DENY_INTERACTIVE;
static const unsigned int exec_locks = SECBIT_EXEC_RESTRICT_FILE_LOCKED |
SECBIT_EXEC_DENY_INTERACTIVE_LOCKED;
/*
* Checks that the exec securebits in @exec survive creating and joining a user
* namespace, and that in there they can be changed exactly when they are not
* locked.
*/
static void test_userns(struct __test_metadata *const _metadata,
const unsigned int exec)
{
const int error = (exec & exec_locks) ? EPERM : 0;
int ready[2], done[2], userns_fd, status;
char path[64], c = 0;
pid_t pid;
/* A user namespace owned by us to join later. */
ASSERT_EQ(0, pipe(ready));
ASSERT_EQ(0, pipe(done));
pid = fork();
ASSERT_LE(0, pid);
if (pid == 0) {
close(ready[0]);
close(done[1]);
if (unshare(CLONE_NEWUSER))
_exit(1);
if (write(ready[1], &c, 1) != 1)
_exit(1);
if (read(done[0], &c, 1) < 0)
_exit(1);
_exit(0);
}
close(ready[1]);
close(done[0]);
if (read(ready[0], &c, 1) != 1) {
waitpid(pid, &status, 0);
SKIP(return, "Cannot create a user namespace");
}
snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
userns_fd = open(path, O_RDONLY | O_CLOEXEC);
ASSERT_LE(0, userns_fd);
EXPECT_EQ(0, test_secbits_set(prctl(PR_GET_SECUREBITS) | exec));
/* Creating a user namespace keeps them... */
CHILD(EXPECT_EQ(0, unshare(CLONE_NEWUSER));
EXPECT_EQ(exec, prctl(PR_GET_SECUREBITS));
EXPECT_EQ(error, test_secbits_set(exec ^ exec_bits));
EXPECT_EQ(error, test_secbits_set(0)));
/* ...and so does joining one. */
CHILD(EXPECT_EQ(0, setns(userns_fd, CLONE_NEWUSER));
EXPECT_EQ(exec, prctl(PR_GET_SECUREBITS));
EXPECT_EQ(error, test_secbits_set(exec ^ exec_bits));
EXPECT_EQ(error, test_secbits_set(0)));
close(userns_fd);
close(done[1]);
EXPECT_EQ(pid, waitpid(pid, &status, 0));
close(ready[0]);
}
TEST_F(secbits, userns_locked)
{
test_userns(_metadata, exec_bits | exec_locks);
}
TEST_F(secbits, userns_unlocked)
{
test_userns(_metadata, exec_bits);
}
TEST_F(secbits, userns_lock_only)
{
test_userns(_metadata, exec_locks);
}
TEST_HARNESS_MAIN
@@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
shared_bdev_test
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
LOCAL_HDRS += loop_util.h
TEST_GEN_PROGS := shared_bdev_test
include ../../lib.mk
@@ -0,0 +1,4 @@
CONFIG_BLK_DEV_DM=y
CONFIG_BLK_DEV_LOOP=y
CONFIG_EROFS_FS=y
CONFIG_MD=y
@@ -0,0 +1,64 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __SELFTESTS_FILESYSTEMS_FREEZE_LOOP_UTIL_H
#define __SELFTESTS_FILESYSTEMS_FREEZE_LOOP_UTIL_H
#include <errno.h>
#include <fcntl.h>
#include <linux/loop.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
/* How often to retry when somebody else binds the free device first. */
#define LOOP_BIND_TRIES 64
/* Bind a free loop device to @fd; its number or -errno. */
static inline int loop_bind(int fd)
{
for (int i = 0; i < LOOP_BIND_TRIES; i++) {
char dev[32];
int cfd, lfd, n, err;
cfd = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
if (cfd < 0)
return -errno;
n = ioctl(cfd, LOOP_CTL_GET_FREE);
err = errno;
close(cfd);
if (n < 0)
return -err;
snprintf(dev, sizeof(dev), "/dev/loop%d", n);
lfd = open(dev, O_RDWR | O_CLOEXEC);
if (lfd < 0)
return -errno;
err = ioctl(lfd, LOOP_SET_FD, fd) ? errno : 0;
close(lfd);
if (!err)
return n;
if (err != EBUSY)
return -err;
}
return -EBUSY;
}
/* Did loop_bind() fail because there are no loop device nodes or no driver? */
static inline bool loop_missing(int ret)
{
return ret == -ENOENT || ret == -ENODEV;
}
static inline void loop_clear(int n)
{
char dev[32];
int fd;
snprintf(dev, sizeof(dev), "/dev/loop%d", n);
fd = open(dev, O_RDWR | O_CLOEXEC);
if (fd < 0)
return;
ioctl(fd, LOOP_CLR_FD, 0);
close(fd);
}
#endif /* __SELFTESTS_FILESYSTEMS_FREEZE_LOOP_UTIL_H */
@@ -0,0 +1,479 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Freezing a block device that several superblocks share.
*
* erofs claims its blob devices with the filesystem type as the holder, so
* several erofs superblocks can share a blob. Merge two images from a common
* layer and a layer of their own, put the common layer on a device-mapper
* device and mount both. A suspend of that device has to freeze each of the
* two superblocks exactly once and the resume has to thaw both, also while
* mounts of one of them come and go. erofs is read-only, so the freezes and
* thaws don't write to the suspended device.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <linux/dm-ioctl.h>
#include <linux/fs.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#include <unistd.h>
#include "../../kselftest_harness.h"
#include "loop_util.h"
#define NR_CHURN 100
enum { M12, M13, L1, L2, L3, NR_IMGS };
static const char *const img_names[NR_IMGS] = { "m12", "m13", "l1", "l2", "l3" };
FIXTURE(shared_bdev) {
char dir[64];
char a[96];
char b[96];
char dm[96];
char name[32];
char src_a[32];
char src_b[32];
char opts_a[256];
char opts_b[256];
int loop[NR_IMGS];
int fd_a;
int fd_b;
dev_t dmdev;
bool dm_created;
bool suspended;
bool mounted_a;
bool mounted_b;
};
static int dm_ioctl(unsigned long cmd, const char *name, uint32_t flags, dev_t *dev)
{
struct dm_ioctl dmi = {
.version = { DM_VERSION_MAJOR, 0, 0 },
.data_size = sizeof(dmi),
.data_start = sizeof(dmi),
.flags = flags,
};
int fd, ret = 0;
strncpy(dmi.name, name, sizeof(dmi.name) - 1);
fd = open("/dev/mapper/control", O_RDWR | O_CLOEXEC);
if (fd < 0)
return -errno;
if (ioctl(fd, cmd, &dmi))
ret = -errno;
else if (dev)
*dev = makedev((dmi.dev >> 8) & 0xfff,
(dmi.dev & 0xff) | ((dmi.dev >> 12) & 0xfff00));
close(fd);
return ret;
}
/* Create @name as a linear mapping of all of @backing and make it live. */
static int dm_create_linear(const char *name, dev_t backing, uint64_t sectors,
dev_t *dev)
{
struct {
struct dm_ioctl dmi;
struct dm_target_spec spec;
char params[64];
} __attribute__((aligned(8))) t = {};
int fd, ret;
ret = dm_ioctl(DM_DEV_CREATE, name, 0, NULL);
if (ret)
return ret;
t.dmi.version[0] = DM_VERSION_MAJOR;
t.dmi.data_size = sizeof(t);
t.dmi.data_start = sizeof(t.dmi);
t.dmi.target_count = 1;
strncpy(t.dmi.name, name, sizeof(t.dmi.name) - 1);
t.spec.length = sectors;
strcpy(t.spec.target_type, "linear");
snprintf(t.params, sizeof(t.params), "%u:%u 0", major(backing), minor(backing));
fd = open("/dev/mapper/control", O_RDWR | O_CLOEXEC);
if (fd >= 0) {
ret = ioctl(fd, DM_TABLE_LOAD, &t) ? -errno : 0;
close(fd);
} else {
ret = -errno;
}
/* Resuming makes the loaded table live. */
if (!ret)
ret = dm_ioctl(DM_DEV_SUSPEND, name, 0, dev);
if (ret)
dm_ioctl(DM_DEV_REMOVE, name, 0, NULL);
return ret;
}
static int suspend(FIXTURE_DATA(shared_bdev) *self)
{
int ret = dm_ioctl(DM_DEV_SUSPEND, self->name, DM_SUSPEND_FLAG, NULL);
if (!ret)
self->suspended = true;
return ret;
}
static int resume(FIXTURE_DATA(shared_bdev) *self)
{
int ret = dm_ioctl(DM_DEV_SUSPEND, self->name, 0, NULL);
if (!ret)
self->suspended = false;
return ret;
}
/* Drop one freeze with FITHAW; EINVAL if the superblock isn't frozen. */
static bool fithaw(int fd)
{
return !ioctl(fd, FITHAW, 0);
}
static int write_file(const char *path, const char *val)
{
ssize_t len = strlen(val);
int fd, ret = 0;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0)
return -1;
if (write(fd, val, len) != len)
ret = -1;
close(fd);
return ret;
}
static int check_file(const char *path, const char *val)
{
char buf[32] = {};
int fd;
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -1;
if (read(fd, buf, sizeof(buf) - 1) < 0)
buf[0] = '\0';
close(fd);
return strcmp(buf, val) ? -1 : 0;
}
static int rm_one(const char *path, const struct stat *st, int type, struct FTW *ftw)
{
return remove(path);
}
static int mount_b(FIXTURE_DATA(shared_bdev) *self)
{
int ret = mount(self->src_b, self->b, "erofs", MS_RDONLY, self->opts_b);
if (!ret)
self->mounted_b = true;
return ret;
}
static void shared_bdev_cleanup(FIXTURE_DATA(shared_bdev) *self)
{
if (self->suspended)
resume(self);
if (self->fd_a >= 0)
close(self->fd_a);
if (self->fd_b >= 0)
close(self->fd_b);
if (self->mounted_a)
umount2(self->a, MNT_DETACH);
umount2(self->b, MNT_DETACH);
if (self->dm_created)
dm_ioctl(DM_DEV_REMOVE, self->name, 0, NULL);
for (int i = 0; i < NR_IMGS; i++) {
if (self->loop[i] >= 0)
loop_clear(self->loop[i]);
}
nftw(self->dir, rm_one, 16, FTW_DEPTH | FTW_PHYS);
}
/* Two layers of their own, a common one, and two images merged from them. */
static int make_images(const char *dir)
{
static const char *const files[][2] = {
{ "src-l1/d1/f1", "one\n" },
{ "src-l2/f2", "two\n" },
{ "src-l3/f3", "three\n" },
};
char path[160], cmd[512];
for (int i = 0; i < 3; i++) {
snprintf(path, sizeof(path), "%s/src-l%d", dir, i + 1);
if (mkdir(path, 0755))
return -1;
}
snprintf(path, sizeof(path), "%s/src-l1/d1", dir);
if (mkdir(path, 0755))
return -1;
for (int i = 0; i < 3; i++) {
snprintf(path, sizeof(path), "%s/%s", dir, files[i][0]);
if (write_file(path, files[i][1]))
return -1;
}
/* Chunk-based layers can be referenced by the merged images. */
snprintf(cmd, sizeof(cmd),
"cd %s && (for l in l1 l2 l3; do "
"mkfs.erofs --chunksize=4096 -Enoinline_data $l src-$l || exit 1; done && "
"mkfs.erofs m12 l1 l2 && mkfs.erofs m13 l1 l3) >/dev/null 2>&1", dir);
return system(cmd) ? -1 : 0;
}
FIXTURE_SETUP(shared_bdev)
{
struct stat img, lst, st;
char path[160], dev[32];
int fd;
for (int i = 0; i < NR_IMGS; i++)
self->loop[i] = -1;
self->fd_a = -1;
self->fd_b = -1;
self->dm_created = false;
self->suspended = false;
self->mounted_a = false;
self->mounted_b = false;
if (geteuid() != 0)
SKIP(return, "test requires CAP_SYS_ADMIN");
if (access("/dev/loop-control", R_OK | W_OK))
SKIP(return, "test requires loop devices");
if (access("/dev/mapper/control", R_OK | W_OK))
SKIP(return, "test requires device-mapper");
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
snprintf(self->dir, sizeof(self->dir), "/tmp/shared_bdev.XXXXXX");
ASSERT_NE(mkdtemp(self->dir), NULL);
snprintf(self->a, sizeof(self->a), "%s/a", self->dir);
snprintf(self->b, sizeof(self->b), "%s/b", self->dir);
snprintf(self->name, sizeof(self->name), "shared_bdev_%d", getpid());
/* The harness doesn't run the teardown when the setup fails. */
ASSERT_EQ(mkdir(self->a, 0700), 0)
shared_bdev_cleanup(self);
ASSERT_EQ(mkdir(self->b, 0700), 0)
shared_bdev_cleanup(self);
if (make_images(self->dir)) {
shared_bdev_cleanup(self);
SKIP(return, "mkfs.erofs failed, is missing or can't merge images");
}
for (int i = 0; i < NR_IMGS; i++) {
snprintf(path, sizeof(path), "%s/%s", self->dir, img_names[i]);
fd = open(path, O_RDWR | O_CLOEXEC);
ASSERT_GE(fd, 0)
shared_bdev_cleanup(self);
self->loop[i] = loop_bind(fd);
if (i == L1) {
ASSERT_EQ(fstat(fd, &img), 0)
shared_bdev_cleanup(self);
}
close(fd);
ASSERT_GE(self->loop[i], 0)
shared_bdev_cleanup(self);
}
/* The common layer goes on a device-mapper device. */
snprintf(dev, sizeof(dev), "/dev/loop%d", self->loop[L1]);
ASSERT_EQ(stat(dev, &lst), 0)
shared_bdev_cleanup(self);
ASSERT_EQ(dm_create_linear(self->name, lst.st_rdev, img.st_size >> 9, &self->dmdev), 0)
shared_bdev_cleanup(self);
self->dm_created = true;
snprintf(self->dm, sizeof(self->dm), "/dev/dm-%u", minor(self->dmdev));
if (stat(self->dm, &st) || st.st_rdev != self->dmdev) {
snprintf(self->dm, sizeof(self->dm), "%s/dm", self->dir);
ASSERT_EQ(mknod(self->dm, S_IFBLK | 0600, self->dmdev), 0)
shared_bdev_cleanup(self);
}
snprintf(self->src_a, sizeof(self->src_a), "/dev/loop%d", self->loop[M12]);
snprintf(self->src_b, sizeof(self->src_b), "/dev/loop%d", self->loop[M13]);
snprintf(self->opts_a, sizeof(self->opts_a), "device=%s,device=/dev/loop%d",
self->dm, self->loop[L2]);
snprintf(self->opts_b, sizeof(self->opts_b), "device=%s,device=/dev/loop%d",
self->dm, self->loop[L3]);
if (mount(self->src_a, self->a, "erofs", MS_RDONLY, self->opts_a)) {
if (errno == ENODEV) {
shared_bdev_cleanup(self);
SKIP(return, "kernel lacks erofs");
}
ASSERT_EQ(errno, 0)
shared_bdev_cleanup(self);
}
self->mounted_a = true;
ASSERT_EQ(mount_b(self), 0)
shared_bdev_cleanup(self);
/* Both read through the shared blob. */
snprintf(path, sizeof(path), "%s/d1/f1", self->a);
ASSERT_EQ(check_file(path, "one\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/f2", self->a);
ASSERT_EQ(check_file(path, "two\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/d1/f1", self->b);
ASSERT_EQ(check_file(path, "one\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/f3", self->b);
ASSERT_EQ(check_file(path, "three\n"), 0)
shared_bdev_cleanup(self);
self->fd_a = open(self->a, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
ASSERT_GE(self->fd_a, 0)
shared_bdev_cleanup(self);
self->fd_b = open(self->b, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
ASSERT_GE(self->fd_b, 0)
shared_bdev_cleanup(self);
}
FIXTURE_TEARDOWN(shared_bdev)
{
shared_bdev_cleanup(self);
}
TEST_F(shared_bdev, suspend_freezes_each_once)
{
ASSERT_EQ(suspend(self), 0);
/* One FITHAW drops each freeze, a second one finds nothing frozen. */
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_TRUE(fithaw(self->fd_b));
ASSERT_FALSE(fithaw(self->fd_b));
ASSERT_EQ(errno, EINVAL);
/* The resume finds nothing left to thaw and still thaws the device. */
ASSERT_EQ(resume(self), 0);
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
}
TEST_F(shared_bdev, resume_thaws_all)
{
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_FALSE(fithaw(self->fd_b));
ASSERT_EQ(errno, EINVAL);
}
TEST_F(shared_bdev, unmounted_sharer)
{
close(self->fd_b);
self->fd_b = -1;
ASSERT_EQ(umount2(self->b, 0), 0);
self->mounted_b = false;
/* Only the remaining superblock is frozen, once. */
ASSERT_EQ(suspend(self), 0);
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_EQ(resume(self), 0);
/* The blob can be claimed again. */
ASSERT_EQ(mount_b(self), 0);
}
TEST_F(shared_bdev, flushbuf)
{
int fd = open(self->dm, O_RDONLY | O_CLOEXEC);
ASSERT_GE(fd, 0);
/* BLKFLSBUF syncs every superblock that uses the device. */
ASSERT_EQ(ioctl(fd, BLKFLSBUF, 0), 0);
close(fd);
}
#ifdef __NR_ustat
/* struct ustat is not exported through UAPI, mirror include/linux/types.h. */
struct ustat_buf {
int f_tfree;
unsigned long f_tinode;
char f_fname[6];
char f_fpack[6];
char pad[64];
};
TEST_F(shared_bdev, ustat)
{
struct ustat_buf buf;
/* ustat() stops at the first superblock it can lock... */
ASSERT_EQ(syscall(__NR_ustat, (unsigned int)self->dmdev, &buf), 0);
/* ...and the walks after it still reach both. */
ASSERT_EQ(suspend(self), 0);
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_TRUE(fithaw(self->fd_b));
ASSERT_EQ(resume(self), 0);
}
#endif
TEST_F(shared_bdev, churn)
{
int status;
pid_t pid;
close(self->fd_b);
self->fd_b = -1;
ASSERT_EQ(umount2(self->b, 0), 0);
self->mounted_b = false;
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* A mount while the device is frozen is refused with EBUSY. */
for (int i = 0; i < NR_CHURN; i++) {
if (!mount(self->src_b, self->b, "erofs", MS_RDONLY, self->opts_b)) {
if (umount2(self->b, 0))
_exit(1);
} else if (errno != EBUSY) {
_exit(2);
}
}
_exit(0);
}
for (int i = 0; i < NR_CHURN; i++) {
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
}
ASSERT_EQ(waitpid(pid, &status, 0), pid);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
/* Nothing is left frozen and the blob can be claimed again. */
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_EQ(mount_b(self), 0);
}
TEST_HARNESS_MAIN
@@ -686,23 +686,23 @@ TEST_F(set_layers_via_fds, set_layers_via_detached_mount_fds)
while (getline(&line, &len, f_mountinfo) != -1) {
char *haystack = line;
if (strstr(haystack, "workdir=/tmp/w"))
if (strstr(haystack, "workdir=/work"))
layers_found[0] = true;
if (strstr(haystack, "upperdir=/tmp/u"))
if (strstr(haystack, "upperdir=/upper"))
layers_found[1] = true;
if (strstr(haystack, "lowerdir+=/tmp/l1"))
if (strstr(haystack, "lowerdir+=/"))
layers_found[2] = true;
if (strstr(haystack, "lowerdir+=/tmp/l2"))
if (strstr(haystack, "lowerdir+=/"))
layers_found[3] = true;
if (strstr(haystack, "lowerdir+=/tmp/l3"))
if (strstr(haystack, "lowerdir+=/"))
layers_found[4] = true;
if (strstr(haystack, "lowerdir+=/tmp/l4"))
if (strstr(haystack, "lowerdir+=/"))
layers_found[5] = true;
if (strstr(haystack, "datadir+=/tmp/d1"))
if (strstr(haystack, "datadir+=/"))
layers_found[6] = true;
if (strstr(haystack, "datadir+=/tmp/d2"))
if (strstr(haystack, "datadir+=/"))
layers_found[7] = true;
if (strstr(haystack, "datadir+=/tmp/d3"))
if (strstr(haystack, "datadir+=/"))
layers_found[8] = true;
}
free(line);
@@ -63,4 +63,15 @@ TEST(listmount_backward)
}
}
/* A cursor at the largest possible mount id must not wrap around. */
TEST(listmount_cursor_max)
{
uint64_t list[LISTMNT_BUFFER];
ssize_t nr_mounts;
nr_mounts = listmount(LSMT_ROOT, 0, UINT64_MAX, list,
LISTMNT_BUFFER, 0);
ASSERT_EQ(nr_mounts, 0);
}
TEST_HARNESS_MAIN
@@ -7,6 +7,7 @@
#include <sched.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statfs.h>
@@ -578,6 +579,63 @@ out:
free(sm);
}
/*
* A string request needs a buffer that is larger than the fixed part of
* struct statmount. That's true even if the string turns out to be empty
* since the empty string itself is placed past the fixed part.
*/
static void test_statmount_string_short_buffer(void)
{
const struct {
uint64_t mask;
const char *name;
} reqs[] = {
{ STATMOUNT_FS_TYPE, "fs type" },
{ STATMOUNT_FS_SUBTYPE, "fs subtype" },
{ STATMOUNT_MNT_UIDMAP, "mount uidmap" },
};
const size_t sizes[] = { 0, 1, 100, sizeof(struct statmount) - 1,
sizeof(struct statmount) };
long page = sysconf(_SC_PAGESIZE);
unsigned char *map;
size_t i, j;
int ret;
map = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (map == MAP_FAILED) {
ksft_test_result_fail("mmap: %s\n", strerror(errno));
return;
}
/* Anything written past the end of the buffer faults. */
if (mprotect(map + page, page, PROT_NONE)) {
ksft_test_result_fail("mprotect: %s\n", strerror(errno));
goto out;
}
for (i = 0; i < ARRAY_SIZE(reqs); i++) {
for (j = 0; j < ARRAY_SIZE(sizes); j++) {
/* The buffer ends right at the guard page. */
struct statmount *sm = (void *)(map + page - sizes[j]);
errno = 0;
ret = statmount(root_id, 0, 0, reqs[i].mask, sm,
sizes[j], 0);
if (ret != -1 || errno != EOVERFLOW) {
ksft_test_result_fail("statmount %s with %zu byte buffer: %s\n",
reqs[i].name, sizes[j],
ret ? strerror(errno) : "success");
goto out;
}
}
}
ksft_test_result_pass("statmount string short buffer\n");
out:
munmap(map, 2 * page);
}
static void test_listmount_tree(void)
{
ssize_t res;
@@ -869,7 +927,7 @@ int main(void)
setup_namespace();
ksft_set_plan(17);
ksft_set_plan(18);
test_listmount_empty_root();
test_statmount_zero_mask();
test_statmount_mnt_basic();
@@ -884,6 +942,7 @@ int main(void)
test_statmount_string(all_mask, str_off(mnt_root), "mount root & all");
test_statmount_string(all_mask, str_off(mnt_point), "mount point & all");
test_statmount_string(all_mask, str_off(fs_type), "fs type & all");
test_statmount_string_short_buffer();
test_listmount_tree();
test_statmount_by_fd_unmounted();
@@ -676,4 +676,38 @@ TEST(listns_error_cases)
}
}
/*
* A cursor at the largest possible namespace id must not wrap around and
* restart the listing from the beginning.
*/
TEST(listns_cursor_max)
{
struct ns_id_req req = {
.size = sizeof(req),
.ns_id = ULLONG_MAX,
};
__u64 ns_ids[10];
ssize_t ret;
/* unified tree */
ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
if (ret < 0 && errno == ENOSYS)
SKIP(return, "listns() not supported");
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, ENOENT);
/* type-specific tree */
req.ns_type = CLONE_NEWUSER;
ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, ENOENT);
/* owner tree */
req.ns_type = 0;
req.user_ns_id = LISTNS_CURRENT_USER;
ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, ENOENT);
}
TEST_HARNESS_MAIN
+16
View File
@@ -160,6 +160,22 @@
#define PIDFD_INFO_COREDUMP_CODE (1UL << 7)
#endif
#ifndef PIDFD_INFO_SIZE_VER0
#define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */
#endif
#ifndef PIDFD_INFO_SIZE_VER1
#define PIDFD_INFO_SIZE_VER1 72 /* sizeof second published struct */
#endif
#ifndef PIDFD_INFO_SIZE_VER2
#define PIDFD_INFO_SIZE_VER2 80 /* sizeof third published struct */
#endif
#ifndef PIDFD_INFO_SIZE_VER3
#define PIDFD_INFO_SIZE_VER3 88 /* sizeof fourth published struct */
#endif
#ifndef PIDFD_COREDUMPED
#define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */
#endif
@@ -764,4 +764,52 @@ TEST(supported_mask_with_other_fields)
close(pidfd);
}
/*
* Test: fields beyond the caller's struct size are not reported
*
* Request the coredump and supported_mask fields with a struct of the
* first published size. None of those fields fit, so the returned mask
* must not name them and the memory behind the struct must be untouched.
*/
TEST(info_size_ver0_mask)
{
struct pidfd_info info = {
.mask = PIDFD_INFO_COREDUMP | PIDFD_INFO_SUPPORTED_MASK,
.coredump_mask = 0xdeadbeef,
.coredump_signal = 0xdeadbeef,
.coredump_code = 0xdeadbeef,
.supported_mask = 0xdeadbeefdeadbeefULL,
};
unsigned int cmd = _IOC(_IOC_READ | _IOC_WRITE, PIDFS_IOCTL_MAGIC, 11,
PIDFD_INFO_SIZE_VER0);
int pidfd;
pid_t pid;
pid = create_child(&pidfd, 0);
ASSERT_GE(pid, 0);
if (pid == 0)
pause();
ASSERT_EQ(ioctl(pidfd, cmd, &info), 0);
/* The fields that fit are reported as usual. */
ASSERT_TRUE(!!(info.mask & PIDFD_INFO_PID));
ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
/* The fields that do not fit are neither reported nor written. */
ASSERT_FALSE(!!(info.mask & (PIDFD_INFO_COREDUMP |
PIDFD_INFO_COREDUMP_SIGNAL |
PIDFD_INFO_COREDUMP_CODE |
PIDFD_INFO_SUPPORTED_MASK)));
ASSERT_EQ(info.coredump_mask, 0xdeadbeef);
ASSERT_EQ(info.coredump_signal, 0xdeadbeef);
ASSERT_EQ(info.coredump_code, 0xdeadbeef);
ASSERT_EQ(info.supported_mask, 0xdeadbeefdeadbeefULL);
sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0);
sys_waitid(P_PIDFD, pidfd, NULL, WEXITED);
close(pidfd);
}
TEST_HARNESS_MAIN