Skip to content

Commit 18b19ab

Browse files
committed
Merge tag 'namespace-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull namespace updates from Christian Brauner: "This contains a larger set of changes around the generic namespace infrastructure of the kernel. Each specific namespace type (net, cgroup, mnt, ...) embedds a struct ns_common which carries the reference count of the namespace and so on. We open-coded and cargo-culted so many quirks for each namespace type that it just wasn't scalable anymore. So given there's a bunch of new changes coming in that area I've started cleaning all of this up. The core change is to make it possible to correctly initialize every namespace uniformly and derive the correct initialization settings from the type of the namespace such as namespace operations, namespace type and so on. This leaves the new ns_common_init() function with a single parameter which is the specific namespace type which derives the correct parameters statically. This also means the compiler will yell as soon as someone does something remotely fishy. The ns_common_init() addition also allows us to remove ns_alloc_inum() and drops any special-casing of the initial network namespace in the network namespace initialization code that Linus complained about. Another part is reworking the reference counting. The reference counting was open-coded and copy-pasted for each namespace type even though they all followed the same rules. This also removes all open accesses to the reference count and makes it private and only uses a very small set of dedicated helpers to manipulate them just like we do for e.g., files. In addition this generalizes the mount namespace iteration infrastructure introduced a few cycles ago. As reminder, the vfs makes it possible to iterate sequentially and bidirectionally through all mount namespaces on the system or all mount namespaces that the caller holds privilege over. This allow userspace to iterate over all mounts in all mount namespaces using the listmount() and statmount() system call. Each mount namespace has a unique identifier for the lifetime of the systems that is exposed to userspace. The network namespace also has a unique identifier working exactly the same way. This extends the concept to all other namespace types. The new nstree type makes it possible to lookup namespaces purely by their identifier and to walk the namespace list sequentially and bidirectionally for all namespace types, allowing userspace to iterate through all namespaces. Looking up namespaces in the namespace tree works completely locklessly. This also means we can move the mount namespace onto the generic infrastructure and remove a bunch of code and members from struct mnt_namespace itself. There's a bunch of stuff coming on top of this in the future but for now this uses the generic namespace tree to extend a concept introduced first for pidfs a few cycles ago. For a while now we have supported pidfs file handles for pidfds. This has proven to be very useful. This extends the concept to cover namespaces as well. It is possible to encode and decode namespace file handles using the common name_to_handle_at() and open_by_handle_at() apis. As with pidfs file handles, namespace file handles are exhaustive, meaning it is not required to actually hold a reference to nsfs in able to decode aka open_by_handle_at() a namespace file handle. Instead the FD_NSFS_ROOT constant can be passed which will let the kernel grab a reference to the root of nsfs internally and thus decode the file handle. Namespaces file descriptors can already be derived from pidfds which means they aren't subject to overmount protection bugs. IOW, it's irrelevant if the caller would not have access to an appropriate /proc/<pid>/ns/ directory as they could always just derive the namespace based on a pidfd already. It has the same advantage as pidfds. It's possible to reliably and for the lifetime of the system refer to a namespace without pinning any resources and to compare them trivially. Permission checking is kept simple. If the caller is located in the namespace the file handle refers to they are able to open it otherwise they must hold privilege over the owning namespace of the relevant namespace. The namespace file handle layout is exposed as uapi and has a stable and extensible format. For now it simply contains the namespace identifier, the namespace type, and the inode number. The stable format means that userspace may construct its own namespace file handles without going through name_to_handle_at() as they are already allowed for pidfs and cgroup file handles" * tag 'namespace-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (65 commits) ns: drop assert ns: move ns type into struct ns_common nstree: make struct ns_tree private ns: add ns_debug() ns: simplify ns_common_init() further cgroup: add missing ns_common include ns: use inode initializer for initial namespaces selftests/namespaces: verify initial namespace inode numbers ns: rename to __ns_ref nsfs: port to ns_ref_*() helpers net: port to ns_ref_*() helpers uts: port to ns_ref_*() helpers ipv4: use check_net() net: use check_net() net-sysfs: use check_net() user: port to ns_ref_*() helpers time: port to ns_ref_*() helpers pid: port to ns_ref_*() helpers ipc: port to ns_ref_*() helpers cgroup: port to ns_ref_*() helpers ...
2 parents 5484a4e + 6e65f4e commit 18b19ab

55 files changed

Lines changed: 3678 additions & 463 deletions

Some content is hidden

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

block/blk-integrity.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
5858
int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
5959
struct logical_block_metadata_cap __user *argp)
6060
{
61-
struct blk_integrity *bi = blk_get_integrity(bdev->bd_disk);
61+
struct blk_integrity *bi;
6262
struct logical_block_metadata_cap meta_cap = {};
6363
size_t usize = _IOC_SIZE(cmd);
6464

65-
if (_IOC_DIR(cmd) != _IOC_DIR(FS_IOC_GETLBMD_CAP) ||
66-
_IOC_TYPE(cmd) != _IOC_TYPE(FS_IOC_GETLBMD_CAP) ||
67-
_IOC_NR(cmd) != _IOC_NR(FS_IOC_GETLBMD_CAP) ||
68-
_IOC_SIZE(cmd) < LBMD_SIZE_VER0)
65+
if (!extensible_ioctl_valid(cmd, FS_IOC_GETLBMD_CAP, LBMD_SIZE_VER0))
6966
return -ENOIOCTLCMD;
7067

68+
bi = blk_get_integrity(bdev->bd_disk);
7169
if (!bi)
7270
goto out;
7371

fs/fhandle.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/personality.h>
1212
#include <linux/uaccess.h>
1313
#include <linux/compat.h>
14+
#include <linux/nsfs.h>
1415
#include "internal.h"
1516
#include "mount.h"
1617

@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
189190
return 0;
190191
}
191192

193+
if (fd == FD_NSFS_ROOT) {
194+
nsfs_get_root(root);
195+
return 0;
196+
}
197+
192198
return -EBADF;
193199
}
194200

fs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
355355
int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
356356
struct iattr *attr);
357357
void pidfs_get_root(struct path *path);
358+
void nsfs_get_root(struct path *path);

fs/mount.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ struct mnt_namespace {
1717
};
1818
struct user_namespace *user_ns;
1919
struct ucounts *ucounts;
20-
u64 seq; /* Sequence number to prevent loops */
21-
union {
22-
wait_queue_head_t poll;
23-
struct rcu_head mnt_ns_rcu;
24-
};
20+
wait_queue_head_t poll;
2521
u64 seq_origin; /* Sequence number of origin mount namespace */
2622
u64 event;
2723
#ifdef CONFIG_FSNOTIFY
@@ -30,8 +26,6 @@ struct mnt_namespace {
3026
#endif
3127
unsigned int nr_mounts; /* # of mounts in the namespace */
3228
unsigned int pending_mounts;
33-
struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
34-
struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */
3529
refcount_t passive; /* number references not pinning @mounts */
3630
} __randomize_layout;
3731

@@ -149,7 +143,7 @@ static inline void detach_mounts(struct dentry *dentry)
149143

150144
static inline void get_mnt_ns(struct mnt_namespace *ns)
151145
{
152-
refcount_inc(&ns->ns.count);
146+
ns_ref_inc(ns);
153147
}
154148

155149
extern seqlock_t mount_lock;
@@ -173,7 +167,7 @@ static inline bool is_local_mountpoint(const struct dentry *dentry)
173167

174168
static inline bool is_anon_ns(struct mnt_namespace *ns)
175169
{
176-
return ns->seq == 0;
170+
return ns->ns.ns_id == 0;
177171
}
178172

179173
static inline bool anon_ns_root(const struct mount *m)

0 commit comments

Comments
 (0)