bcachefs - a new COW filesystem
Subsystems (28)
Kent Overstreet
maintainercover bcachefs - a new COW filesystem
I'm submitting the bcachefs filesystem for review and inclusion.
Included in this patch series are all the non fs/bcachefs/ patches. The entire tree, based on v6.3, may be found at:
http://evilpiepirate.org/git/bcachefs.git bcachefs-for-upstream
----------------------------------------------------------------
bcachefs overview, status:
Features:
- too many to list
Known bugs:
- too many to list
Status:
- Snapshots have been declared stable; one serious bug report outstanding to look into, most users report it working well.
These are RW btrfs-style snapshots, but with far better scalability and no scalability issues with sparse snapshots due to key level versioning.
- Erasure coding is getting really close; hope to have it ready for users to beat on it by this summer. This is a novel RAID/erasure coding design with no write hole, and no fragmentation of writes (e.g. RAIDZ).
- Tons of scalabality work finished over the past year, users are running it on 100 TB filesystems without complaint, waiting for first 1 PB user; next thing to address re: scalability is fsck/recovery memory usage.
- Test infrastructure! Major project milestone, check out our test dashboard at
https://evilpiepirate.org/~testdashboard/ci?branch=bcachefs
Other project notes:
irc::/irc.oftc.net/bcache is where most activity happens; I'm always there, and most code review happens there - I find the conversational format more productive.
------------------------------------------------
patches in this series:
Christopher James Halse Rogers (1):
stacktrace: Export stack_trace_save_tsk
Daniel Hill (1):
lib: add mean and variance module.
Dave Chinner (3):
vfs: factor out inode hash head calculation
hlist-bl: add hlist_bl_fake()
vfs: inode cache conversion to hash-bl
Kent Overstreet (27):
Compiler Attributes: add __flatten
locking/lockdep: lock_class_is_held()
locking/lockdep: lockdep_set_no_check_recursion()
locking: SIX locks (shared/intent/exclusive)
MAINTAINERS: Add entry for six locks
sched: Add task_struct->faults_disabled_mapping
mm: Bring back vmalloc_exec
fs: factor out d_mark_tmpfile()
block: Add some exports for bcachefs
block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset
block: Bring back zero_fill_bio_iter
block: Rework bio_for_each_segment_all()
block: Rework bio_for_each_folio_all()
block: Don't block on s_umount from __invalidate_super()
bcache: move closures to lib/
MAINTAINERS: Add entry for closures
closures: closure_wait_event()
closures: closure_nr_remaining()
closures: Add a missing include
iov_iter: copy_folio_from_iter_atomic()
MAINTAINERS: Add entry for generic-radix-tree lib/generic-radix-tree.c: Don't overflow in peek() lib/generic-radix-tree.c: Add a missing include lib/generic-radix-tree.c: Add peek_prev() lib/string_helpers: string_get_size() now returns characters wrote
lib: Export errname
MAINTAINERS: Add entry for bcachefs
MAINTAINERS | 39 +
block/bdev.c | 2 +-
block/bio.c | 57 +-
block/blk-core.c | 1 +
block/blk-map.c | 38 +-
block/blk.h | 1 -
block/bounce.c | 12 +-
drivers/md/bcache/Kconfig | 10 +-
drivers/md/bcache/Makefile | 4 +-
drivers/md/bcache/bcache.h | 2 +-
drivers/md/bcache/btree.c | 8 +-
drivers/md/bcache/super.c | 1 -
drivers/md/bcache/util.h | 3 +-
drivers/md/dm-crypt.c | 10 +-
drivers/md/raid1.c | 4 +-
fs/btrfs/disk-io.c | 4 +-
fs/btrfs/extent_io.c | 50 +-
fs/btrfs/raid56.c | 14 +-
fs/crypto/bio.c | 9 +-
fs/dcache.c | 12 +-
fs/erofs/zdata.c | 4 +-
fs/ext4/page-io.c | 8 +-
fs/ext4/readpage.c | 4 +-
fs/f2fs/data.c | 20 +-
fs/gfs2/lops.c | 10 +-
fs/gfs2/meta_io.c | 8 +-
fs/inode.c | 218 +++--
fs/iomap/buffered-io.c | 14 +-
fs/mpage.c | 4 +-
fs/squashfs/block.c | 48 +-
fs/squashfs/lz4_wrapper.c | 17 +-
fs/squashfs/lzo_wrapper.c | 17 +-
fs/squashfs/xz_wrapper.c | 19 +-
fs/squashfs/zlib_wrapper.c | 18 +-
fs/squashfs/zstd_wrapper.c | 19 +-
fs/super.c | 40 +-
fs/verity/verify.c | 9 +-
include/linux/bio.h | 132 +--
include/linux/blkdev.h | 1 +
include/linux/bvec.h | 70 +-
.../md/bcache => include/linux}/closure.h | 46 +-
include/linux/compiler_attributes.h | 5 +
include/linux/dcache.h | 1 +
include/linux/fs.h | 10 +-
include/linux/generic-radix-tree.h | 68 +-
include/linux/list_bl.h | 22 +
include/linux/lockdep.h | 10 +
include/linux/lockdep_types.h | 2 +-
include/linux/mean_and_variance.h | 219 +++++
include/linux/sched.h | 1 +
include/linux/six.h | 210 +++++
include/linux/string_helpers.h | 4 +-
include/linux/uio.h | 2 +
include/linux/vmalloc.h | 1 +
init/init_task.c | 1 +
kernel/Kconfig.locks | 3 +
kernel/locking/Makefile | 1 +
kernel/locking/lockdep.c | 46 ++
kernel/locking/six.c | 779 ++++++++++++++++++
kernel/module/main.c | 4 +-
kernel/stacktrace.c | 2 +
lib/Kconfig | 3 +
lib/Kconfig.debug | 18 +
lib/Makefile | 2 +
{drivers/md/bcache => lib}/closure.c | 36 +-
lib/errname.c | 1 +
lib/generic-radix-tree.c | 76 +-
lib/iov_iter.c | 53 +-
lib/math/Kconfig | 3 +
lib/math/Makefile | 2 +
lib/math/mean_and_variance.c | 136 +++
lib/math/mean_and_variance_test.c | 155 ++++
lib/string_helpers.c | 8 +-
mm/nommu.c | 18 +
mm/vmalloc.c | 21 +
75 files changed, 2485 insertions(+), 445 deletions(-)
rename {drivers/md/bcache => include/linux}/closure.h (93%)
create mode 100644 include/linux/mean_and_variance.h
create mode 100644 include/linux/six.h
create mode 100644 kernel/locking/six.c
rename {drivers/md/bcache => lib}/closure.c (88%)
create mode 100644 lib/math/mean_and_variance.c
create mode 100644 lib/math/mean_and_variance_test.cSignature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 1/32 Compiler Attributes: add __flatten
From: Kent Overstreet <[email protected]>
This makes __attribute__((flatten)) available, which is used by bcachefs.
--- include/linux/compiler_attributes.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/compiler_attributes.h +5 -0
@@ -366,4 +366,9 @@*/ #define __fix_address noinline __noclone +/* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-flatten-function-attribute +*/ +#define __flatten __attribute__((flatten)) +#endif /* __LINUX_COMPILER_ATTRIBUTES_H */
Signature · 1 line
2.40.1
Re: [PATCH 01/32] Compiler Attributes: add __flatten
Quoted 3 lines · Kent Overstreet
This makes __attribute__((flatten)) available, which is used by bcachefs.
We already have it in mainline, so I think it is one less patch you need to care about! :)
Cheers,
Miguel
Kent Overstreet
maintainerRe: [PATCH 01/32] Compiler Attributes: add __flatten
Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Kent Overstreet
This makes __attribute__((flatten)) available, which is used by bcachefs.
We already have it in mainline, so I think it is one less patch you need to care about! :)
Cheers,
Miguel
Wonderful :)
Cheers,
Kent
Kent Overstreet
maintainerPATCH 2/32 locking/lockdep: lock_class_is_held()
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
This patch adds lock_class_is_held(), which can be used to assert that a particular type of lock is not held.
--- include/linux/lockdep.h | 4 ++++ kernel/locking/lockdep.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+)
include/linux/lockdep.h +4 -0
@@ -339,6 +339,8 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);#define lockdep_repin_lock (l,c) lock_repin_lock(&(l)->dep_map, (c))#define lockdep_unpin_lock (l,c) lock_unpin_lock(&(l)->dep_map, (c)) +int lock_class_is_held (struct lock_class_key *key); +#else /* !CONFIG_LOCKDEP */ static inline void lockdep_init_task (struct task_struct *task)
@@ -427,6 +429,8 @@ extern int lockdep_is_held(const void *);#define lockdep_repin_lock (l, c) do { (void)(l); (void)(c); } while (0)#define lockdep_unpin_lock (l, c) do { (void)(l); (void)(c); } while (0) +static inline int lock_class_is_held (struct lock_class_key *key) {return 0 ; } +#endif /* !LOCKDEP */ enum xhlock_context_t {
kernel/locking/lockdep.c +20 -0
@@ -6487,6 +6487,26 @@ void debug_check_no_locks_held(void) }EXPORT_SYMBOL_GPL (debug_check_no_locks_held); +#ifdef CONFIG_LOCKDEP +int lock_class_is_held (struct lock_class_key *key) +{ +struct task_struct *curr = current; +struct held_lock *hlock; + +if (unlikely (!debug_locks)) +return 0 ; + +for (hlock = curr->held_locks; + hlock < curr->held_locks + curr->lockdep_depth; + hlock++) +if (hlock->instance->key == key) +return 1 ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (lock_class_is_held); +#endif +#ifdef __KERNEL__void debug_show_all_locks (void ) {
Signature · 1 line
2.40.1
Peter Zijlstra
maintainerRe: [PATCH 02/32] locking/lockdep: lock_class_is_held()
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This patch adds
lock_class_is_held(), which can be used to assert that a particular type of lock is not held.
How is lock_is_held_type() not sufficient? Which is what's used to implement lockdep_assert_held*().
Kent Overstreet
maintainerRe: [PATCH 02/32] locking/lockdep: lock_class_is_held()
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This patch adds
lock_class_is_held(), which can be used to assert that a particular type of lock is not held.How is
lock_is_held_type()not sufficient? Which is what's used to implement lockdep_assert_held*().
I should've looked at that before - it returns a tristate, so it's closer than I thought, but this is used in contexts where we don't have a lock or lockdep_map to pass and need to pass the lock_class_key instead.
e.g, when initializing a btree_trans, or waiting on btree node IO, we need to assert that no btree node locks are held.
Looking at the code, __lock_is_held() -> match_held_lock() has to care about a bunch of stuff related to subclasses that doesn't seem relevant to lock_class_is_held() - lock_class_is_held() is practically no code in comparison, so I'm inclined to think they should just be separate.
But I'm not the lockdep expert :) Thoughts?
Kent Overstreet
maintainerPATCH 3/32 locking/lockdep: lockdep_set_no_check_recursion()
In reply to Kent OverstreetThis adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
--- include/linux/lockdep.h | 6 ++++++ include/linux/lockdep_types.h | 2 +- kernel/locking/lockdep.c | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-)
include/linux/lockdep.h +6 -0
@@ -665,4 +665,10 @@ lockdep_rcu_suspicious(const char *file, const int line, const char *s) }#endif +#ifdef CONFIG_DEBUG_LOCK_ALLOC +void lockdep_set_no_check_recursion (struct lockdep_map *map); +#else +static inline void lockdep_set_no_check_recursion (struct lockdep_map *map) {} +#endif +#endif /* __LINUX_LOCKDEP_H */
include/linux/lockdep_types.h +1 -1
@@ -128,7 +128,7 @@ struct lock_class {u8 wait_type_inner;u8 wait_type_outer;u8 lock_type; -/* u8 hole; */ +u8 no_check_recursion;#ifdef CONFIG_LOCK_STAT unsigned long contention_point[LOCKSTAT_POINTS ];
kernel/locking/lockdep.c +26 -0
@@ -3024,6 +3024,9 @@ check_deadlock(struct task_struct *curr, struct held_lock *next)if ((next->read ==2 ) && prev->read)continue ; +if (hlock_class (next)->no_check_recursion) +continue ; +/* * We're holding the nest_lock, which serializes this lock's * nesting behaviour.
@@ -3085,6 +3088,10 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev,return 2 ; } +if (hlock_class (prev) ==hlock_class (next) && +hlock_class (prev)->no_check_recursion) +return 2 ; +/* * Prove that the new <prev> -> <next> dependency would not * create a circular dependency in the graph. (We do this by
@@ -6620,3 +6627,22 @@ void lockdep_rcu_suspicious(const char *file, const int line, const char *s)warn_rcu_exit (rcu); }EXPORT_SYMBOL_GPL (lockdep_rcu_suspicious); + +#ifdef CONFIG_DEBUG_LOCK_ALLOC +void lockdep_set_no_check_recursion (struct lockdep_map *lock) +{ +struct lock_class *class = lock->class_cache[0 ]; +unsigned long flags; + +raw_local_irq_save (flags); +lockdep_recursion_inc (); + +if (!class) + class =register_lock_class (lock,0 ,0 ); +if (class) + class->no_check_recursion = true; +lockdep_recursion_finish (); +raw_local_irq_restore (flags); +} +EXPORT_SYMBOL_GPL (lockdep_set_no_check_recursion); +#endif
Signature · 1 line
2.40.1
Peter Zijlstra
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 8 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
Quoted 8 lines
Quoted 5 lines
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Probably in the other branch that I was meaning to re-mail you separately, clearly I hadn't pulled the latest versions back into here... expect that shortly :)
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
Quoted 3 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
Quoted 8 lines
Quoted 5 lines
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Waiman Long
reviewerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 12 lines · Kent Overstreet
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Quoted 8 lines
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Maybe you can use lock_set_novalidate_class() instead.
Cheers,
Longman
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 17 lines · Waiman Long
Quoted 15 lines · Kent Overstreet
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
Quoted 7 lines
Quoted 5 lines
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Maybe you can use
lock_set_novalidate_class()instead.
No, we want that to go away, this is the replacement.
Waiman Long
reviewerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 17 lines · Kent Overstreet
Quoted 16 lines · Waiman Long
Quoted 15 lines · Kent Overstreet
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Maybe you can use
lock_set_novalidate_class()instead.No, we want that to go away, this is the replacement.
OK, you can mention that in the commit log then.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
In reply to Kent OverstreetQuoted 14 lines · Kent Overstreet
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
Quoted 8 lines
Quoted 5 lines
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Quoted 8 lines
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Have you read the ww_mutex code? If not, please do so, it does similar things.
The way it gets around the self-nesting check is by using the nest_lock annotation, the acquire context itself also has a dep_map for this purpose.
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 24 lines · Peter Zijlstra
Quoted 14 lines · Kent Overstreet
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
Quoted 8 lines
Quoted 5 lines
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Quoted 8 lines
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Quoted 6 lines
Have you read the
ww_mutexcode? If not, please do so, it does similar things.The way it gets around the self-nesting check is by using the
nest_lockannotation, the acquire context itself also has adep_mapfor this purpose.
This might work.
I was confused for a good bit when reading tho code to figure out how it works - nest_lock seems to be a pretty bad name, it's really not a lock. acquire_ctx?
Peter Zijlstra
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 9 lines · Kent Overstreet
Quoted 6 lines · Peter Zijlstra
Have you read the
ww_mutexcode? If not, please do so, it does similar things.The way it gets around the self-nesting check is by using the
nest_lockannotation, the acquire context itself also has adep_mapfor this purpose.
Quoted 5 lines
This might work.
I was confused for a good bit when reading tho code to figure out how it works -
nest_lockseems to be a pretty bad name, it's really not a lock.acquire_ctx?
That's just how ww_mutex uses it, the annotation itself comes from mm_take_all_locks() where mm->mmap_lock (the lock formerly known as mmap_sem) is used to serialize multi acquisition of vma locks.
That is, no other code takes multiple vma locks (be it i_mmap_rwsem or anonvma->root->rwsem) in any order. These locks nest inside mmap_lock and therefore by holding mmap_lock you serialize the whole thing and can take them in any order you like.
Perhaps, now, all these many years later another name would've made more sense, but I don't think it's worth the hassle of the tree-wide rename (there's a few other users since).
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
Quoted 20 lines · Peter Zijlstra
Quoted 9 lines · Kent Overstreet
Quoted 6 lines · Peter Zijlstra
Have you read the
ww_mutexcode? If not, please do so, it does similar things.The way it gets around the self-nesting check is by using the
nest_lockannotation, the acquire context itself also has adep_mapfor this purpose.Quoted 5 lines
This might work.
I was confused for a good bit when reading tho code to figure out how it works -
nest_lockseems to be a pretty bad name, it's really not a lock.acquire_ctx?That's just how
ww_mutexuses it, the annotation itself comes frommm_take_all_locks()wheremm->mmap_lock(the lock formerly known asmmap_sem) is used to serialize multi acquisition of vma locks.
Quoted 8 lines
That is, no other code takes multiple vma locks (be it
i_mmap_rwsemoranonvma->root->rwsem) in any order. These locks nest insidemmap_lockand therefore by holdingmmap_lockyou serialize the whole thing and can take them in any order you like.Perhaps, now, all these many years later another name would've made more sense, but I don't think it's worth the hassle of the tree-wide rename (there's a few other users since).
Thanks for the history lesson :)
Kent Overstreet
maintainerRe: [PATCH 03/32] locking/lockdep: lockdep_set_no_check_recursion()
In reply to Peter ZijlstraQuoted 24 lines · Peter Zijlstra
Quoted 14 lines · Kent Overstreet
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Kent Overstreet
This adds a method to tell lockdep not to check lock ordering within a lock class - but to still check lock ordering w.r.t. other lock types.
Quoted 8 lines
Quoted 5 lines
This is for bcachefs, where for btree node locks we have our own deadlock avoidance strategy w.r.t. other btree node locks (cycle detection), but we still want lockdep to check lock ordering w.r.t. other lock types.
ISTR you had a much nicer version of this where you gave a custom order function -- what happend to that?
Quoted 8 lines
Actually, I spoke too soon; this patch and the other series with the comparison function solve different problems.
For bcachefs btree node locks, we don't have a defined lock ordering at all - we do full runtime cycle detection, so we don't want lockdep checking for self deadlock because we're handling that but we _do_ want lockdep checking lock ordering of btree node locks w.r.t. other locks in the system.
Quoted 6 lines
Have you read the
ww_mutexcode? If not, please do so, it does similar things.The way it gets around the self-nesting check is by using the
nest_lockannotation, the acquire context itself also has adep_mapfor this purpose.
So, spent some time plumbing this through the six lock code and seeing how it'd work.
I like it in theory, it's got the right semantics and it would allow for lockdep to check that we're not taking locks with more than one btree_trans in the same thread. Unfortunately, we've got code paths that are meant to be called from contexts that may or may not have a btree_trans - and this is fine right now, because they just use trylock(), but having to plumb nest_lock through is going to make a mess of things.
(The relevant codepaths include shrinker callbacks, where we definitely can not just init a new btree_trans, and also the btree node write path which can be kicked off from all sorts of places).
Can we go with lockdep_set_no_check_recursion() for now? It's a pretty small addition to the lockdep code.
Kent Overstreet
maintainerPATCH 4/32 locking: SIX locks (shared/intent/exclusive)
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
New lock for bcachefs, like read/write locks but with a third state, intent.
Intent locks conflict with each other, but not with read locks; taking a write lock requires first holding an intent lock.
--- include/linux/six.h | 210 +++++++++++ kernel/Kconfig.locks | 3 + kernel/locking/Makefile | 1 + kernel/locking/six.c | 779 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 993 insertions(+) create mode 100644 include/linux/six.h create mode 100644 kernel/locking/six.c
include/linux/six.h new file +210 -0
@@ -0,0 +1,210 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_SIX_H +#define _LINUX_SIX_H + +/* +* Shared/intent/exclusive locks: sleepable read/write locks, much like rw +* semaphores, except with a third intermediate state, intent. Basic operations +* are: +* +* six_lock_read(&foo->lock); +* six_unlock_read(&foo->lock); +* +* six_lock_intent(&foo->lock); +* six_unlock_intent(&foo->lock); +* +* six_lock_write(&foo->lock); +* six_unlock_write(&foo->lock); +* +* Intent locks block other intent locks, but do not block read locks, and you +* must have an intent lock held before taking a write lock, like so: +* +* six_lock_intent(&foo->lock); +* six_lock_write(&foo->lock); +* six_unlock_write(&foo->lock); +* six_unlock_intent(&foo->lock); +* +* Other operations: +* +* six_trylock_read() +* six_trylock_intent() +* six_trylock_write() +* +* six_lock_downgrade(): convert from intent to read +* six_lock_tryupgrade(): attempt to convert from read to intent +* +* Locks also embed a sequence number, which is incremented when the lock is +* locked or unlocked for write. The current sequence number can be grabbed +* while a lock is held from lock->state.seq; then, if you drop the lock you can +* use six_relock_(read|intent_write)(lock, seq) to attempt to retake the lock +* iff it hasn't been locked for write in the meantime. +* +* There are also operations that take the lock type as a parameter, where the +* type is one of SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write: +* +* six_lock_type(lock, type) +* six_unlock_type(lock, type) +* six_relock(lock, type, seq) +* six_trylock_type(lock, type) +* six_trylock_convert(lock, from, to) +* +* A lock may be held multiple types by the same thread (for read or intent, +* not write). However, the six locks code does _not_ implement the actual +* recursive checks itself though - rather, if your code (e.g. btree iterator +* code) knows that the current thread already has a lock held, and for the +* correct type, six_lock_increment() may be used to bump up the counter for +* that type - the only effect is that one more call to unlock will be required +* before the lock is unlocked. +*/ + +#include <linux/lockdep.h> +#include <linux/osq_lock.h> +#include <linux/sched.h> +#include <linux/types.h> + +#define SIX_LOCK_SEPARATE_LOCKFNS + +union six_lock_state { +struct { +atomic64_t counter; + }; + +struct { +u64 v; + }; + +struct { +/* for waitlist_bitnr() */ +unsigned long l; + }; + +struct { +unsigned read_lock:27 ; +unsigned write_locking:1 ; +unsigned intent_lock:1 ; +unsigned waiters:3 ; +/* +* seq works much like in seqlocks: it's incremented every time +* we lock and unlock for write. +* +* If it's odd write lock is held, even unlocked. +* +* Thus readers can unlock, and then lock again later iff it +* hasn't been modified in the meantime. +*/ +u32 seq; + }; +}; + +enum six_lock_type { + SIX_LOCK_read, + SIX_LOCK_intent, + SIX_LOCK_write, +}; + +struct six_lock { +union six_lock_state state; +unsigned intent_lock_recurse; +struct task_struct *owner; +struct optimistic_spin_queue osq; +unsigned __percpu *readers; + +raw_spinlock_t wait_lock; +struct list_head wait_list[2 ]; +#ifdef CONFIG_DEBUG_LOCK_ALLOC +struct lockdep_map dep_map; +#endif +}; + +typedef int (*six_lock_should_sleep_fn )(struct six_lock *lock,void *); + +static __always_inline void __six_lock_init(struct six_lock *lock, +const char *name, +struct lock_class_key *key) +{ +atomic64_set (&lock->state.counter,0 ); +raw_spin_lock_init (&lock->wait_lock); +INIT_LIST_HEAD (&lock->wait_list[SIX_LOCK_read]); +INIT_LIST_HEAD (&lock->wait_list[SIX_LOCK_intent]); +#ifdef CONFIG_DEBUG_LOCK_ALLOC +debug_check_no_locks_freed ((void *) lock,sizeof (*lock)); +lockdep_init_map (&lock->dep_map, name, key,0 ); +#endif +} + +#define six_lock_init (lock) \ +do { \ + static struct lock_class_key __key; \ + \ + __six_lock_init((lock), #lock, &__key); \ +} while (0) + +#define __SIX_VAL (field, _v) (((union six_lock_state) { .field = _v }).v) + +#define __SIX_LOCK (type) \ +bool six_trylock_##type(struct six_lock *); \ +bool six_relock_##type(struct six_lock *, u32); \ +int six_lock_##type(struct six_lock *, six_lock_should_sleep_fn, void *);\ +void six_unlock_##type(struct six_lock *); + +__SIX_LOCK(read) +__SIX_LOCK(intent) +__SIX_LOCK(write) +#undef __SIX_LOCK + +#define SIX_LOCK_DISPATCH (type, fn, ...) \ + switch (type) { \ + case SIX_LOCK_read: \ + return fn##_read(__VA_ARGS__); \ + case SIX_LOCK_intent: \ + return fn##_intent(__VA_ARGS__); \ + case SIX_LOCK_write: \ + return fn##_write(__VA_ARGS__); \ + default: \ + BUG(); \ + } + +static inline bool six_trylock_type (struct six_lock *lock,enum six_lock_type type) +{ +SIX_LOCK_DISPATCH (type, six_trylock, lock); +} + +static inline bool six_relock_type (struct six_lock *lock,enum six_lock_type type, +unsigned seq) +{ +SIX_LOCK_DISPATCH (type, six_relock, lock, seq); +} + +static inline int six_lock_type (struct six_lock *lock,enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +SIX_LOCK_DISPATCH (type, six_lock, lock, should_sleep_fn, p); +} + +static inline void six_unlock_type (struct six_lock *lock,enum six_lock_type type) +{ +SIX_LOCK_DISPATCH (type, six_unlock, lock); +} + +void six_lock_downgrade (struct six_lock *); +bool six_lock_tryupgrade (struct six_lock *); +bool six_trylock_convert (struct six_lock *,enum six_lock_type , +enum six_lock_type ); + +void six_lock_increment (struct six_lock *,enum six_lock_type ); + +void six_lock_wakeup_all (struct six_lock *); + +void six_lock_pcpu_free_rcu (struct six_lock *); +void six_lock_pcpu_free (struct six_lock *); +void six_lock_pcpu_alloc (struct six_lock *); + +struct six_lock_count { +unsigned read; +unsigned intent; +}; + +struct six_lock_count six_lock_counts (struct six_lock *); + +#endif /* _LINUX_SIX_H */
kernel/Kconfig.locks +3 -0
@@ -259,3 +259,6 @@ config ARCH_HAS_MMIOWB config MMIOWB def_bool y if ARCH_HAS_MMIOWB depends on SMP + +config SIXLOCKS + bool
kernel/locking/Makefile +1 -0
@@ -32,3 +32,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_SIXLOCKS) += six.o
kernel/locking/six.c new file +779 -0
779 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
include/linux/six.h line 10
Quoted 13 lines · Kent Overstreet
include/linux/six.h new file +10
-0@@ -0,0 +1,210 @@ +* There are also operations that take the lock type as a parameter, where the +* type is one of SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write: +* +* six_lock_type(lock, type) +* six_unlock_type(lock, type) +* six_relock(lock, type, seq) +* six_trylock_type(lock, type) +* six_trylock_convert(lock, from, to) +* +* A lock may be held multiple types by the same thread (for read or intent,
"multiple times"
include/linux/six.h line 11
Quoted 1 line
include/linux/six.h new file +1
-0+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Kent Overstreet
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 17 lines · Jan Engelhardt
include/linux/six.h line 10
Quoted 13 lines · Kent Overstreet
include/linux/six.h new file +10
-0@@ -0,0 +1,210 @@ +* There are also operations that take the lock type as a parameter, where the +* type is one of SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write: +* +* six_lock_type(lock, type) +* six_unlock_type(lock, type) +* six_relock(lock, type, seq) +* six_trylock_type(lock, type) +* six_trylock_convert(lock, from, to) +* +* A lock may be held multiple types by the same thread (for read or intent, "multiple times"
Thanks, fixed
Quoted 4 lines
include/linux/six.h line 11
Quoted 1 line
include/linux/six.h new file +1
-0+// SPDX-License-Identifier: GPL-2.0 The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Where is that list?
Re: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 7 lines · Kent Overstreet
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Kent Overstreet
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 5 lines · Jan Engelhardt
Quoted 3 lines · Kent Overstreet
Quoted 2 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Do we have anything more official? Quick grep through the source tree says I'm following accepted usage.
Re: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 9 lines · Kent Overstreet
Quoted 8 lines · Jan Engelhardt
Quoted 6 lines · Kent Overstreet
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Do we have anything more official? Quick grep through the source tree says I'm following accepted usage.
Documentation/process/license-rules.rst points to spdx.org for further info.
or LICENSES/preferred/GPL-2.0 contains this:
Valid-License-Identifier: GPL-2.0
Valid-License-Identifier: GPL-2.0-only
Valid-License-Identifier: GPL-2.0+
Valid-License-Identifier: GPL-2.0-or-later
SPDX-URL: https://spdx.org/licenses/GPL-2.0.html
Signature · 1 line
~Randy
Kent Overstreet
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 23 lines · Randy Dunlap
Quoted 9 lines · Kent Overstreet
Quoted 8 lines · Jan Engelhardt
Quoted 6 lines · Kent Overstreet
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Do we have anything more official? Quick grep through the source tree says I'm following accepted usage.
Documentation/process/license-rules.rstpoints to spdx.org for further info.
Quoted 6 lines
or
LICENSES/preferred/GPL-2.0contains this:
Valid-License-Identifier: GPL-2.0
Valid-License-Identifier: GPL-2.0-only
Valid-License-Identifier: GPL-2.0+
Valid-License-Identifier: GPL-2.0-or-later
SPDX-URL: https://spdx.org/licenses/GPL-2.0.html
Thanks, I'll leave it at GPL-2.0 then.
Eric Biggers
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 24 lines · Kent Overstreet
Quoted 23 lines · Randy Dunlap
Quoted 9 lines · Kent Overstreet
Quoted 8 lines · Jan Engelhardt
Quoted 6 lines · Kent Overstreet
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Do we have anything more official? Quick grep through the source tree says I'm following accepted usage.
Documentation/process/license-rules.rstpoints to spdx.org for further info.
Quoted 8 lines
Quoted 6 lines
or
LICENSES/preferred/GPL-2.0contains this:
Valid-License-Identifier: GPL-2.0
Valid-License-Identifier: GPL-2.0-only
Valid-License-Identifier: GPL-2.0+
Valid-License-Identifier: GPL-2.0-or-later
SPDX-URL: https://spdx.org/licenses/GPL-2.0.htmlThanks, I'll leave it at GPL-2.0 then.
https://spdx.org/licenses/GPL-2.0.html says that GPL-2.0 is deprecated. Its replacement is https://spdx.org/licenses/GPL-2.0-only.html. Yes, they mean the same thing, but the new names were introduced to be clearer than the old ones.
- Eric
Kent Overstreet
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 32 lines · Eric Biggers
Quoted 31 lines · Kent Overstreet
Quoted 29 lines · Randy Dunlap
Quoted 9 lines · Kent Overstreet
Quoted 8 lines · Jan Engelhardt
Quoted 6 lines · Kent Overstreet
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+// SPDX-License-Identifier: GPL-2.0
The currently SPDX list only knows "GPL-2.0-only" or "GPL-2.0-or-later", please edit.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Where is that list?
I just went to spdx.org and then chose "License List" from the horizontal top bar menu.
Do we have anything more official? Quick grep through the source tree says I'm following accepted usage.
Documentation/process/license-rules.rstpoints to spdx.org for further info.or
LICENSES/preferred/GPL-2.0contains this:
Valid-License-Identifier: GPL-2.0
Valid-License-Identifier: GPL-2.0-only
Valid-License-Identifier: GPL-2.0+
Valid-License-Identifier: GPL-2.0-or-later
SPDX-URL: https://spdx.org/licenses/GPL-2.0.html
Quoted 5 lines
Quoted 1 line
Thanks, I'll leave it at GPL-2.0 then.
https://spdx.org/licenses/GPL-2.0.html says that GPL-2.0 is deprecated. Its replacement is https://spdx.org/licenses/GPL-2.0-only.html. Yes, they mean the same thing, but the new names were introduced to be clearer than the old ones.
Perhaps updating LICENCES/preferred/GPL-2.0 is in order.
Re: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
In reply to Kent OverstreetQuoted 46 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
New lock for bcachefs, like read/write locks but with a third state, intent.
Intent locks conflict with each other, but not with read locks; taking a write lock requires first holding an intent lock.
Signed-off-by Kent Overstreet <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Will Deacon <[email protected]>Cc Waiman Long <[email protected]>Cc Boqun Feng <[email protected]>--- include/linux/six.h | 210 +++++++++++ kernel/Kconfig.locks | 3 + kernel/locking/Makefile | 1 + kernel/locking/six.c | 779 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 993 insertions(+) create mode 100644 include/linux/six.h create mode 100644 kernel/locking/six.cinclude/linux/six.h new file +17
-0@@ -0,0 +1,210 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_SIX_H +#define _LINUX_SIX_H + +/* +* Shared/intent/exclusive locks: sleepable read/write locks, much like rw +* semaphores, except with a third intermediate state, intent. Basic operations +* are: +* +* six_lock_read(&foo->lock); +* six_unlock_read(&foo->lock); +* +* six_lock_intent(&foo->lock); +* six_unlock_intent(&foo->lock); +* +* six_lock_write(&foo->lock);
include/linux/six.h line 27
Quoted 10 lines
include/linux/six.h new file +10
-0+* six_unlock_write(&foo->lock); +* +* Intent locks block other intent locks, but do not block read locks, and you +* must have an intent lock held before taking a write lock, like so: +* +* six_lock_intent(&foo->lock); +* six_lock_write(&foo->lock); +* six_unlock_write(&foo->lock); +* six_unlock_intent(&foo->lock); +*
So the idea is to create a fundamentally unfair rwsem? One that always prefers readers over writers?
Quoted 5 lines
include/linux/six.h new file +5
-0+* Other operations: +* +* six_trylock_read() +* six_trylock_intent() +* six_trylock_write()
include/linux/six.h line 42
Quoted 10 lines
include/linux/six.h new file +10
-0+* +* six_lock_downgrade(): convert from intent to read +* six_lock_tryupgrade(): attempt to convert from read to intent +* +* Locks also embed a sequence number, which is incremented when the lock is +* locked or unlocked for write. The current sequence number can be grabbed +* while a lock is held from lock->state.seq; then, if you drop the lock you can +* use six_relock_(read|intent_write)(lock, seq) to attempt to retake the lock +* iff it hasn't been locked for write in the meantime. +*
^^^
This is a cool idea.
Quoted 6 lines
include/linux/six.h new file +6
-0+* There are also operations that take the lock type as a parameter, where the +* type is one of SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write: +* +* six_lock_type(lock, type) +* six_unlock_type(lock, type) +* six_relock(lock, type, seq)
include/linux/six.h line 58
Quoted 10 lines
include/linux/six.h new file +10
-0+* six_trylock_type(lock, type) +* six_trylock_convert(lock, from, to) +* +* A lock may be held multiple types by the same thread (for read or intent, +* not write). However, the six locks code does _not_ implement the actual +* recursive checks itself though - rather, if your code (e.g. btree iterator +* code) knows that the current thread already has a lock held, and for the +* correct type, six_lock_increment() may be used to bump up the counter for +* that type - the only effect is that one more call to unlock will be required +* before the lock is unlocked.
Thse semantics are a bit confusing. Once you hold a read or intent lock, you can take it as many times as you like. What happens if I take it in one context and release it in another? Say, across a workqueue job for instance?
Are intent locks "converted" to write locks, or do they stack? For instance, suppose I take the intent lock 3 times and then take a write lock. How many times do I have to call unlock to fully release it (3 or 4)? If I release it just once, do I still hold the write lock or am I back to "intent" state?
include/linux/six.h line 66
Quoted 10 lines
include/linux/six.h new file +8
-0+*/ + +#include <linux/lockdep.h> +#include <linux/osq_lock.h> +#include <linux/sched.h> +#include <linux/types.h> + +#define SIX_LOCK_SEPARATE_LOCKFNS
Some basic info about the underlying design would be nice here. What info is tracked in the union below? When are different members being used? How does the code decide which way to cast this thing? etc.
Quoted 10 lines
include/linux/six.h new file +10
-0+ +union six_lock_state { +struct { +atomic64_t counter; + }; + +struct { +u64 v; + }; +
include/linux/six.h line 86
Quoted 10 lines
include/linux/six.h new file +10
-0+struct { +/* for waitlist_bitnr() */ +unsigned long l; + }; + +struct { +unsigned read_lock:27 ; +unsigned write_locking:1 ; +unsigned intent_lock:1 ; +unsigned waiters:3 ;
Ewww...bitfields. That seems a bit scary in a union. There is no guarantee that the underlying arch will even pack that into a single word, AIUI. It may be safer to do this with masking and shifting instead.
Quoted 406 lines
include/linux/six.h new file +124
-0+/* +* seq works much like in seqlocks: it's incremented every time +* we lock and unlock for write. +* +* If it's odd write lock is held, even unlocked. +* +* Thus readers can unlock, and then lock again later iff it +* hasn't been modified in the meantime. +*/ + u32 seq; + }; +}; + +enum six_lock_type { + SIX_LOCK_read, + SIX_LOCK_intent, + SIX_LOCK_write, +}; + +struct six_lock { +union six_lock_state state; +unsigned intent_lock_recurse; +struct task_struct *owner; +struct optimistic_spin_queue osq; +unsigned __percpu *readers; + +raw_spinlock_t wait_lock; +struct list_head wait_list[2 ]; +#ifdef CONFIG_DEBUG_LOCK_ALLOC +struct lockdep_map dep_map; +#endif +}; + +typedef int (*six_lock_should_sleep_fn )(struct six_lock *lock,void *); + +static __always_inline void __six_lock_init(struct six_lock *lock, +const char *name, +struct lock_class_key *key) +{ +atomic64_set (&lock->state.counter,0 ); +raw_spin_lock_init (&lock->wait_lock); +INIT_LIST_HEAD (&lock->wait_list[SIX_LOCK_read]); +INIT_LIST_HEAD (&lock->wait_list[SIX_LOCK_intent]); +#ifdef CONFIG_DEBUG_LOCK_ALLOC +debug_check_no_locks_freed ((void *) lock,sizeof (*lock)); +lockdep_init_map (&lock->dep_map, name, key,0 ); +#endif +} + +#define six_lock_init (lock) \ +do { \ + static struct lock_class_key __key; \ + \ + __six_lock_init((lock), #lock, &__key); \ +} while (0) + +#define __SIX_VAL (field, _v) (((union six_lock_state) { .field = _v }).v) + +#define __SIX_LOCK (type) \ +bool six_trylock_##type(struct six_lock *); \ +bool six_relock_##type(struct six_lock *, u32); \ +int six_lock_##type(struct six_lock *, six_lock_should_sleep_fn, void *);\ +void six_unlock_##type(struct six_lock *); + +__SIX_LOCK(read) +__SIX_LOCK(intent) +__SIX_LOCK(write) +#undef __SIX_LOCK + +#define SIX_LOCK_DISPATCH (type, fn, ...) \ + switch (type) { \ + case SIX_LOCK_read: \ + return fn##_read(__VA_ARGS__); \ + case SIX_LOCK_intent: \ + return fn##_intent(__VA_ARGS__); \ + case SIX_LOCK_write: \ + return fn##_write(__VA_ARGS__); \ + default: \ + BUG(); \ + } + +static inline bool six_trylock_type (struct six_lock *lock,enum six_lock_type type) +{ +SIX_LOCK_DISPATCH (type, six_trylock, lock); +} + +static inline bool six_relock_type (struct six_lock *lock,enum six_lock_type type, +unsigned seq) +{ +SIX_LOCK_DISPATCH (type, six_relock, lock, seq); +} + +static inline int six_lock_type (struct six_lock *lock,enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +SIX_LOCK_DISPATCH (type, six_lock, lock, should_sleep_fn, p); +} + +static inline void six_unlock_type (struct six_lock *lock,enum six_lock_type type) +{ +SIX_LOCK_DISPATCH (type, six_unlock, lock); +} + +void six_lock_downgrade (struct six_lock *); +bool six_lock_tryupgrade (struct six_lock *); +bool six_trylock_convert (struct six_lock *,enum six_lock_type , +enum six_lock_type ); + +void six_lock_increment (struct six_lock *,enum six_lock_type ); + +void six_lock_wakeup_all (struct six_lock *); + +void six_lock_pcpu_free_rcu (struct six_lock *); +void six_lock_pcpu_free (struct six_lock *); +void six_lock_pcpu_alloc (struct six_lock *); + +struct six_lock_count { +unsigned read; +unsigned intent; +}; + +struct six_lock_count six_lock_counts (struct six_lock *); + +#endif /* _LINUX_SIX_H */ kernel/Kconfig.locks +3
-0@@ -259,3 +259,6 @@ config ARCH_HAS_MMIOWB config MMIOWB def_bool y if ARCH_HAS_MMIOWB depends on SMP + +config SIXLOCKS + boolkernel/locking/Makefile +1
-0@@ -32,3 +32,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_SIXLOCKS) += six.okernel/locking/six.c new file +256
-0@@ -0,0 +1,779 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/export.h> +#include <linux/log2.h> +#include <linux/percpu.h> +#include <linux/preempt.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> +#include <linux/sched/rt.h> +#include <linux/six.h> +#include <linux/slab.h> + +#ifdef DEBUG +#define EBUG_ON (cond) BUG_ON(cond) +#else +#define EBUG_ON (cond) do {} while (0) +#endif + +#define six_acquire (l, t) lock_acquire(l, 0, t, 0, 0, NULL, _RET_IP_) +#define six_release (l) lock_release(l, _RET_IP_) + +struct six_lock_vals { +/* Value we add to the lock in order to take the lock: */ +u64 lock_val; + +/* If the lock has this value (used as a mask), taking the lock fails: */ +u64 lock_fail; + +/* Value we add to the lock in order to release the lock: */ +u64 unlock_val; + +/* Mask that indicates lock is held for this type: */ +u64 held_mask; + +/* Waitlist we wakeup when releasing the lock: */ +enum six_lock_type unlock_wakeup; +}; + +#define __SIX_LOCK_HELD_read __SIX_VAL(read_lock, ~0) +#define __SIX_LOCK_HELD_intent __SIX_VAL(intent_lock, ~0) +#define __SIX_LOCK_HELD_write __SIX_VAL(seq, 1) + +#define LOCK_VALS { \ + [SIX_LOCK_read] = { \ + .lock_val = __SIX_VAL(read_lock, 1), \ + .lock_fail = __SIX_LOCK_HELD_write + __SIX_VAL(write_locking, 1),\ + .unlock_val = -__SIX_VAL(read_lock, 1), \ + .held_mask = __SIX_LOCK_HELD_read, \ + .unlock_wakeup = SIX_LOCK_write, \ + }, \ + [SIX_LOCK_intent] = { \ + .lock_val = __SIX_VAL(intent_lock, 1), \ + .lock_fail = __SIX_LOCK_HELD_intent, \ + .unlock_val = -__SIX_VAL(intent_lock, 1), \ + .held_mask = __SIX_LOCK_HELD_intent, \ + .unlock_wakeup = SIX_LOCK_intent, \ + }, \ + [SIX_LOCK_write] = { \ + .lock_val = __SIX_VAL(seq, 1), \ + .lock_fail = __SIX_LOCK_HELD_read, \ + .unlock_val = __SIX_VAL(seq, 1), \ + .held_mask = __SIX_LOCK_HELD_write, \ + .unlock_wakeup = SIX_LOCK_read, \ + }, \ +} + +static inline void six_set_owner (struct six_lock *lock,enum six_lock_type type, +union six_lock_state old) +{ +if (type != SIX_LOCK_intent) +return ; + +if (!old.intent_lock) { +EBUG_ON (lock->owner); + lock->owner = current; + }else { +EBUG_ON (lock->owner != current); + } +} + +static inline unsigned pcpu_read_count (struct six_lock *lock) +{ +unsigned read_count =0 ; +int cpu; + +for_each_possible_cpu (cpu) + read_count += *per_cpu_ptr (lock->readers, cpu); +return read_count; +} + +struct six_lock_waiter { +struct list_head list; +struct task_struct *task; +}; + +/* This is probably up there with the more evil things I've done */ +#define waitlist_bitnr (id) ilog2((((union six_lock_state) { .waiters = 1 << (id) }).l)) + +static inline void six_lock_wakeup (struct six_lock *lock, +union six_lock_state state, +unsigned waitlist_id) +{ +if (waitlist_id == SIX_LOCK_write) { +if (state.write_locking && !state.read_lock) { +struct task_struct *p =READ_ONCE (lock->owner); +if (p) +wake_up_process (p); + } + }else { +struct list_head *wait_list = &lock->wait_list[waitlist_id]; +struct six_lock_waiter *w, *next; + +if (!(state.waiters & (1 << waitlist_id))) +return ; + +clear_bit (waitlist_bitnr (waitlist_id), + (unsigned long *) &lock->state.v); + +raw_spin_lock (&lock->wait_lock); + +list_for_each_entry_safe (w, next, wait_list, list) { +list_del_init (&w->list); + +if (wake_up_process (w->task) && + waitlist_id != SIX_LOCK_read) { +if (!list_empty (wait_list)) +set_bit (waitlist_bitnr (waitlist_id), + (unsigned long *) &lock->state.v); +break ; + } + } + +raw_spin_unlock (&lock->wait_lock); + } +} + +static __always_inline bool do_six_trylock_type(struct six_lock *lock, +enum six_lock_type type, +bool try) +{ +const struct six_lock_vals l[] =LOCK_VALS ; +union six_lock_state old, new; +bool ret; +u64 v; + +EBUG_ON (type == SIX_LOCK_write && lock->owner != current); +EBUG_ON (type == SIX_LOCK_write && (lock->state.seq &1 )); + +EBUG_ON (type == SIX_LOCK_write && (try != !(lock->state.write_locking))); + +/* +* Percpu reader mode: +* +* The basic idea behind this algorithm is that you can implement a lock +* between two threads without any atomics, just memory barriers: +* +* For two threads you'll need two variables, one variable for "thread a +* has the lock" and another for "thread b has the lock". +* +* To take the lock, a thread sets its variable indicating that it holds +* the lock, then issues a full memory barrier, then reads from the +* other thread's variable to check if the other thread thinks it has +* the lock. If we raced, we backoff and retry/sleep. +*/ + +if (type == SIX_LOCK_read && lock->readers) { +retry: +preempt_disable (); +this_cpu_inc (*lock->readers);/* signal that we own lock */ + +smp_mb (); + + old.v =READ_ONCE (lock->state.v); + ret = !(old.v & l[type].lock_fail); + +this_cpu_sub (*lock->readers, !ret); +preempt_enable (); + +/* +* If we failed because a writer was trying to take the +* lock, issue a wakeup because we might have caused a +* spurious trylock failure: +*/ +if (old.write_locking) { +struct task_struct *p =READ_ONCE (lock->owner); + +if (p) +wake_up_process (p); + } + +/* +* If we failed from the lock path and the waiting bit wasn't +* set, set it: +*/ +if (!try && !ret) { + v = old.v; + +do { + new.v = old.v = v; + +if (!(old.v & l[type].lock_fail)) + goto retry; + +if (new.waiters & (1 << type)) +break ; + + new.waiters |=1 << type; + }while ((v =atomic64_cmpxchg (&lock->state.counter, + old.v, new.v)) != old.v); + } + }else if (type == SIX_LOCK_write && lock->readers) { +if (try) { +atomic64_add (__SIX_VAL (write_locking,1 ), + &lock->state.counter); +smp_mb__after_atomic (); + } + + ret = !pcpu_read_count (lock); + +/* +* On success, we increment lock->seq; also we clear +* write_locking unless we failed from the lock path: +*/ + v =0 ; +if (ret) + v +=__SIX_VAL (seq,1 ); +if (ret || try) + v -=__SIX_VAL (write_locking,1 ); + +if (try && !ret) { + old.v =atomic64_add_return (v, &lock->state.counter); +six_lock_wakeup (lock, old, SIX_LOCK_read); + }else { +atomic64_add (v, &lock->state.counter); + } + }else { + v =READ_ONCE (lock->state.v); +do { + new.v = old.v = v; + +if (!(old.v & l[type].lock_fail)) { + new.v += l[type].lock_val; + +if (type == SIX_LOCK_write) + new.write_locking =0 ; + }else if (!try && type != SIX_LOCK_write && + !(new.waiters & (1 << type))) + new.waiters |=1 << type; +else +break ;/* waiting bit already set */ + }while ((v =atomic64_cmpxchg_acquire (&lock->state.counter, + old.v, new.v)) != old.v); + + ret = !(old.v & l[type].lock_fail); + +EBUG_ON (ret && !(lock->state.v & l[type].held_mask));
kernel/locking/six.c line 266
Quoted 10 lines
kernel/locking/six.c new file +10
-0+ } + +if (ret) +six_set_owner (lock, type, old); + +EBUG_ON (type == SIX_LOCK_write && (try || ret) && (lock->state.write_locking)); + +return ret; +} +
^^^
I'd really like to see some more comments in the code above. It's pretty complex.
Quoted 513 lines
kernel/locking/six.c new file +513
-0+__always_inline __flatten +static bool __six_trylock_type (struct six_lock *lock,enum six_lock_type type) +{ +if (!do_six_trylock_type (lock, type, true)) +return false; + +if (type != SIX_LOCK_write) +six_acquire (&lock->dep_map,1 ); +return true; +} + +__always_inline __flatten +static bool __six_relock_type (struct six_lock *lock,enum six_lock_type type, +unsigned seq) +{ +const struct six_lock_vals l[] =LOCK_VALS ; +union six_lock_state old; +u64 v; + +EBUG_ON (type == SIX_LOCK_write); + +if (type == SIX_LOCK_read && + lock->readers) { +bool ret; + +preempt_disable (); +this_cpu_inc (*lock->readers); + +smp_mb (); + + old.v =READ_ONCE (lock->state.v); + ret = !(old.v & l[type].lock_fail) && old.seq == seq; + +this_cpu_sub (*lock->readers, !ret); +preempt_enable (); + +/* +* Similar to the lock path, we may have caused a spurious write +* lock fail and need to issue a wakeup: +*/ +if (old.write_locking) { +struct task_struct *p =READ_ONCE (lock->owner); + +if (p) +wake_up_process (p); + } + +if (ret) +six_acquire (&lock->dep_map,1 ); + +return ret; + } + + v =READ_ONCE (lock->state.v); +do { + old.v = v; + +if (old.seq != seq || old.v & l[type].lock_fail) +return false; + }while ((v =atomic64_cmpxchg_acquire (&lock->state.counter, + old.v, + old.v + l[type].lock_val)) != old.v); + +six_set_owner (lock, type, old); +if (type != SIX_LOCK_write) +six_acquire (&lock->dep_map,1 ); +return true; +} + +#ifdef CONFIG_LOCK_SPIN_ON_OWNER + +static inline int six_can_spin_on_owner (struct six_lock *lock) +{ +struct task_struct *owner; +int retval =1 ; + +if (need_resched ()) +return 0 ; + +rcu_read_lock (); + owner =READ_ONCE (lock->owner); +if (owner) + retval = owner->on_cpu; +rcu_read_unlock (); +/* +* if lock->owner is not set, the mutex owner may have just acquired +* it and not set the owner yet or the mutex has been released. +*/ +return retval; +} + +static inline bool six_spin_on_owner (struct six_lock *lock, +struct task_struct *owner) +{ +bool ret = true; + +rcu_read_lock (); +while (lock->owner == owner) { +/* +* Ensure we emit the owner->on_cpu, dereference _after_ +* checking lock->owner still matches owner. If that fails, +* owner might point to freed memory. If it still matches, +* the rcu_read_lock() ensures the memory stays valid. +*/ +barrier (); + +if (!owner->on_cpu ||need_resched ()) { + ret = false; +break ; + } + +cpu_relax (); + } +rcu_read_unlock (); + +return ret; +} + +static inline bool six_optimistic_spin (struct six_lock *lock,enum six_lock_type type) +{ +struct task_struct *task = current; + +if (type == SIX_LOCK_write) +return false; + +preempt_disable (); +if (!six_can_spin_on_owner (lock)) + goto fail; + +if (!osq_lock (&lock->osq)) + goto fail; + +while (1 ) { +struct task_struct *owner; + +/* +* If there's an owner, wait for it to either +* release the lock or go to sleep. +*/ + owner =READ_ONCE (lock->owner); +if (owner && !six_spin_on_owner (lock, owner)) +break ; + +if (do_six_trylock_type (lock, type, false)) { +osq_unlock (&lock->osq); +preempt_enable (); +return true; + } + +/* +* When there's no owner, we might have preempted between the +* owner acquiring the lock and setting the owner field. If +* we're an RT task that will live-lock because we won't let +* the owner complete. +*/ +if (!owner && (need_resched () ||rt_task (task))) +break ; + +/* +* The cpu_relax() call is a compiler barrier which forces +* everything in this loop to be re-loaded. We don't need +* memory barriers as we'll eventually observe the right +* values at the cost of a few extra spins. +*/ +cpu_relax (); + } + +osq_unlock (&lock->osq); +fail: +preempt_enable (); + +/* +* If we fell out of the spin path because of need_resched(), +* reschedule now, before we try-lock again. This avoids getting +* scheduled out right after we obtained the lock. +*/ +if (need_resched ()) +schedule (); + +return false; +} + +#else /* CONFIG_LOCK_SPIN_ON_OWNER */ + +static inline bool six_optimistic_spin (struct six_lock *lock,enum six_lock_type type) +{ +return false; +} + +#endif + +noinline +static int __six_lock_type_slowpath(struct six_lock *lock, enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +union six_lock_state old; +struct six_lock_waiter wait; +int ret =0 ; + +if (type == SIX_LOCK_write) { +EBUG_ON (lock->state.write_locking); +atomic64_add (__SIX_VAL (write_locking,1 ), &lock->state.counter); +smp_mb__after_atomic (); + } + + ret = should_sleep_fn ?should_sleep_fn (lock, p) :0 ; +if (ret) + goto out_before_sleep; + +if (six_optimistic_spin (lock, type)) + goto out_before_sleep; + +lock_contended (&lock->dep_map, _RET_IP_); + +INIT_LIST_HEAD (&wait.list); + wait.task = current; + +while (1 ) { +set_current_state (TASK_UNINTERRUPTIBLE ); +if (type == SIX_LOCK_write) +EBUG_ON (lock->owner != current); +else if (list_empty_careful (&wait.list)) { +raw_spin_lock (&lock->wait_lock); +list_add_tail (&wait.list, &lock->wait_list[type]); +raw_spin_unlock (&lock->wait_lock); + } + +if (do_six_trylock_type (lock, type, false)) +break ; + + ret = should_sleep_fn ?should_sleep_fn (lock, p) :0 ; +if (ret) +break ; + +schedule (); + } + +__set_current_state (TASK_RUNNING ); + +if (!list_empty_careful (&wait.list)) { +raw_spin_lock (&lock->wait_lock); +list_del_init (&wait.list); +raw_spin_unlock (&lock->wait_lock); + } +out_before_sleep: +if (ret && type == SIX_LOCK_write) { + old.v =atomic64_sub_return (__SIX_VAL (write_locking,1 ), + &lock->state.counter); +six_lock_wakeup (lock, old, SIX_LOCK_read); + } + +return ret; +} + +__always_inline +static int __six_lock_type(struct six_lock *lock, enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +int ret; + +if (type != SIX_LOCK_write) +six_acquire (&lock->dep_map,0 ); + + ret =do_six_trylock_type (lock, type, true) ?0 + :__six_lock_type_slowpath (lock, type, should_sleep_fn, p); + +if (ret && type != SIX_LOCK_write) +six_release (&lock->dep_map); +if (!ret) +lock_acquired (&lock->dep_map, _RET_IP_); + +return ret; +} + +__always_inline __flatten +static void __six_unlock_type (struct six_lock *lock,enum six_lock_type type) +{ +const struct six_lock_vals l[] =LOCK_VALS ; +union six_lock_state state; + +EBUG_ON (type == SIX_LOCK_write && + !(lock->state.v & __SIX_LOCK_HELD_intent)); + +if (type != SIX_LOCK_write) +six_release (&lock->dep_map); + +if (type == SIX_LOCK_intent) { +EBUG_ON (lock->owner != current); + +if (lock->intent_lock_recurse) { + --lock->intent_lock_recurse; +return ; + } + + lock->owner =NULL ; + } + +if (type == SIX_LOCK_read && + lock->readers) { +smp_mb ();/* unlock barrier */ +this_cpu_dec (*lock->readers); +smp_mb ();/* between unlocking and checking for waiters */ + state.v =READ_ONCE (lock->state.v); + }else { +EBUG_ON (!(lock->state.v & l[type].held_mask)); + state.v =atomic64_add_return_release (l[type].unlock_val, + &lock->state.counter); + } + +six_lock_wakeup (lock, state, l[type].unlock_wakeup); +} + +#define __SIX_LOCK (type) \ +bool six_trylock_##type(struct six_lock *lock) \ +{ \ + return __six_trylock_type(lock, SIX_LOCK_##type); \ +} \ +EXPORT_SYMBOL_GPL(six_trylock_##type); \ + \ +bool six_relock_##type(struct six_lock *lock, u32 seq) \ +{ \ + return __six_relock_type(lock, SIX_LOCK_##type, seq); \ +} \ +EXPORT_SYMBOL_GPL(six_relock_##type); \ + \ +int six_lock_##type(struct six_lock *lock, \ + six_lock_should_sleep_fn should_sleep_fn, void *p) \ +{ \ + return __six_lock_type(lock, SIX_LOCK_##type, should_sleep_fn, p);\ +} \ +EXPORT_SYMBOL_GPL(six_lock_##type); \ + \ +void six_unlock_##type(struct six_lock *lock) \ +{ \ + __six_unlock_type(lock, SIX_LOCK_##type); \ +} \ +EXPORT_SYMBOL_GPL(six_unlock_##type); + +__SIX_LOCK(read) +__SIX_LOCK(intent) +__SIX_LOCK(write) + +#undef __SIX_LOCK + +/* Convert from intent to read: */ +void six_lock_downgrade (struct six_lock *lock) +{ +six_lock_increment (lock, SIX_LOCK_read); +six_unlock_intent (lock); +} +EXPORT_SYMBOL_GPL (six_lock_downgrade); + +bool six_lock_tryupgrade (struct six_lock *lock) +{ +union six_lock_state old, new; +u64 v =READ_ONCE (lock->state.v); + +do { + new.v = old.v = v; + +if (new.intent_lock) +return false; + +if (!lock->readers) { +EBUG_ON (!new.read_lock); + new.read_lock--; + } + + new.intent_lock =1 ; + }while ((v =atomic64_cmpxchg_acquire (&lock->state.counter, + old.v, new.v)) != old.v); + +if (lock->readers) +this_cpu_dec (*lock->readers); + +six_set_owner (lock, SIX_LOCK_intent, old); + +return true; +} +EXPORT_SYMBOL_GPL (six_lock_tryupgrade); + +bool six_trylock_convert (struct six_lock *lock, +enum six_lock_type from, +enum six_lock_type to) +{ +EBUG_ON (to == SIX_LOCK_write || from == SIX_LOCK_write); + +if (to == from) +return true; + +if (to == SIX_LOCK_read) { +six_lock_downgrade (lock); +return true; + }else { +return six_lock_tryupgrade (lock); + } +} +EXPORT_SYMBOL_GPL (six_trylock_convert); + +/* +* Increment read/intent lock count, assuming we already have it read or intent +* locked: +*/ +void six_lock_increment (struct six_lock *lock,enum six_lock_type type) +{ +const struct six_lock_vals l[] =LOCK_VALS ; + +six_acquire (&lock->dep_map,0 ); + +/* XXX: assert already locked, and that we don't overflow: */ + +switch (type) { +case SIX_LOCK_read: +if (lock->readers) { +this_cpu_inc (*lock->readers); + }else { +EBUG_ON (!lock->state.read_lock && + !lock->state.intent_lock); +atomic64_add (l[type].lock_val, &lock->state.counter); + } +break ; +case SIX_LOCK_intent: +EBUG_ON (!lock->state.intent_lock); + lock->intent_lock_recurse++; +break ; +case SIX_LOCK_write: +BUG (); +break ; + } +} +EXPORT_SYMBOL_GPL (six_lock_increment); + +void six_lock_wakeup_all (struct six_lock *lock) +{ +struct six_lock_waiter *w; + +raw_spin_lock (&lock->wait_lock); + +list_for_each_entry (w, &lock->wait_list[0 ], list) +wake_up_process (w->task); +list_for_each_entry (w, &lock->wait_list[1 ], list) +wake_up_process (w->task); + +raw_spin_unlock (&lock->wait_lock); +} +EXPORT_SYMBOL_GPL (six_lock_wakeup_all); + +struct free_pcpu_rcu { +struct rcu_head rcu; +void __percpu *p; +}; + +static void free_pcpu_rcu_fn (struct rcu_head *_rcu) +{ +struct free_pcpu_rcu *rcu = + container_of(_rcu, struct free_pcpu_rcu, rcu); + +free_percpu (rcu->p); +kfree (rcu); +} + +void six_lock_pcpu_free_rcu (struct six_lock *lock) +{ +struct free_pcpu_rcu *rcu =kzalloc (sizeof (*rcu),GFP_KERNEL ); + +if (!rcu) +return ; + + rcu->p = lock->readers; + lock->readers =NULL ; + +call_rcu (&rcu->rcu, free_pcpu_rcu_fn); +} +EXPORT_SYMBOL_GPL (six_lock_pcpu_free_rcu); + +void six_lock_pcpu_free (struct six_lock *lock) +{ +BUG_ON (lock->readers &&pcpu_read_count (lock)); +BUG_ON (lock->state.read_lock); + +free_percpu (lock->readers); + lock->readers =NULL ; +} +EXPORT_SYMBOL_GPL (six_lock_pcpu_free); + +void six_lock_pcpu_alloc (struct six_lock *lock) +{ +#ifdef __KERNEL__ +if (!lock->readers) + lock->readers =alloc_percpu (unsigned); +#endif +} +EXPORT_SYMBOL_GPL (six_lock_pcpu_alloc); + +/* +* Returns lock held counts, for both read and intent +*/ +struct six_lock_count six_lock_counts (struct six_lock *lock) +{ +struct six_lock_count ret = {0 , lock->state.intent_lock }; + +if (!lock->readers) + ret.read += lock->state.read_lock; +else { +int cpu; + +for_each_possible_cpu (cpu) + ret.read += *per_cpu_ptr (lock->readers, cpu); + } + +return ret; +} +EXPORT_SYMBOL_GPL (six_lock_counts);
Signature · 1 line
Jeff Layton <[email protected]>
Kent Overstreet
maintainerRe: [PATCH 04/32] locking: SIX locks (shared/intent/exclusive)
Quoted 2 lines · Jeff Layton
So the idea is to create a fundamentally unfair rwsem? One that always prefers readers over writers?
No, not sure where you're getting that from. It's unfair, but writes are preferred over readers :)
Quoted 17 lines
Quoted 6 lines
+ * Other operations:
+ *+ * six_trylock_read() + * six_trylock_intent() + * six_trylock_write()Quoted 10 lines
+ *
+ *six_lock_downgrade(): convert from intent to read
+ *six_lock_tryupgrade(): attempt to convert from read to intent
+ *
+ * Locks also embed a sequence number, which is incremented when the lock is
+ * locked or unlocked for write. The current sequence number can be grabbed
+ * while a lock is held fromlock->state.seq; then, if you drop the lock you can
+ * use six_relock_(read|intent_write)(lock, seq) to attempt to retake the lock
+ * iff it hasn't been locked for write in the meantime.
+ *
Quoted 2 lines
^^^
This is a cool idea.
It's used heavily in bcachefs so we can drop locks if we might be blocking - and then relock and continue, at the cost of a transaction restart if the relock fails. It's a huge win for tail latency.
Quoted 16 lines
Quoted 7 lines
+ * type is one of
SIX_LOCK_read,SIX_LOCK_intent, orSIX_LOCK_write:
+ *+ * six_lock_type(lock, type) + * six_unlock_type(lock, type) + * six_relock(lock, type, seq) + * six_trylock_type(lock, type) + * six_trylock_convert(lock, from, to)Quoted 8 lines
+ *
+ * A lock may be held multiple types by the same thread (for read or intent,
+ * not write). However, the six locks code does _not_ implement the actual
+ * recursive checks itself though - rather, if your code (e.g. btree iterator
+ * code) knows that the current thread already has a lock held, and for the
+ * correct type,six_lock_increment()may be used to bump up the counter for
+ * that type - the only effect is that one more call to unlock will be required
+ * before the lock is unlocked.
Quoted 4 lines
Thse semantics are a bit confusing. Once you hold a read or intent lock, you can take it as many times as you like. What happens if I take it in one context and release it in another? Say, across a workqueue job for instance?
Not allowed because of lockdep, same as with other locks.
Quoted 5 lines
Are intent locks "converted" to write locks, or do they stack? For instance, suppose I take the intent lock 3 times and then take a write lock. How many times do I have to call unlock to fully release it (3 or 4)? If I release it just once, do I still hold the write lock or am I back to "intent" state?
They stack. You'd call unlock_write() once ad unlock_intent() three times.
Quoted 3 lines
Some basic info about the underlying design would be nice here. What info is tracked in the union below? When are different members being used? How does the code decide which way to cast this thing? etc.
The field names seem pretty descriptive to me.
counter, v are just for READ_ONCE/atomic64 cmpxchg ops.
Quoted 4 lines
Ewww...bitfields. That seems a bit scary in a union. There is no guarantee that the underlying arch will even pack that into a single word, AIUI. It may be safer to do this with masking and shifting instead.
It wouldn't hurt to add a BUILD_BUG_ON() for the size, but I don't find anything "scary" about unions and bitfields :)
And it makes the code more descriptive and readable than masking and shifting.
Quoted 126 lines
Quoted 126 lines
kernel/locking/six.c new file +126
-0+static __always_inline bool do_six_trylock_type(struct six_lock *lock, +enum six_lock_type type, +bool try) +{ +const struct six_lock_vals l[] =LOCK_VALS ; +union six_lock_state old, new; +bool ret; +u64 v; + +EBUG_ON (type == SIX_LOCK_write && lock->owner != current); +EBUG_ON (type == SIX_LOCK_write && (lock->state.seq &1 )); + +EBUG_ON (type == SIX_LOCK_write && (try != !(lock->state.write_locking))); + +/* +* Percpu reader mode: +* +* The basic idea behind this algorithm is that you can implement a lock +* between two threads without any atomics, just memory barriers: +* +* For two threads you'll need two variables, one variable for "thread a +* has the lock" and another for "thread b has the lock". +* +* To take the lock, a thread sets its variable indicating that it holds +* the lock, then issues a full memory barrier, then reads from the +* other thread's variable to check if the other thread thinks it has +* the lock. If we raced, we backoff and retry/sleep. +*/ + +if (type == SIX_LOCK_read && lock->readers) { +retry: +preempt_disable (); +this_cpu_inc (*lock->readers);/* signal that we own lock */ + +smp_mb (); + + old.v =READ_ONCE (lock->state.v); + ret = !(old.v & l[type].lock_fail); + +this_cpu_sub (*lock->readers, !ret); +preempt_enable (); + +/* +* If we failed because a writer was trying to take the +* lock, issue a wakeup because we might have caused a +* spurious trylock failure: +*/ +if (old.write_locking) { +struct task_struct *p =READ_ONCE (lock->owner); + +if (p) +wake_up_process (p); + } + +/* +* If we failed from the lock path and the waiting bit wasn't +* set, set it: +*/ +if (!try && !ret) { + v = old.v; + +do { + new.v = old.v = v; + +if (!(old.v & l[type].lock_fail)) + goto retry; + +if (new.waiters & (1 << type)) +break ; + + new.waiters |=1 << type; + }while ((v =atomic64_cmpxchg (&lock->state.counter, + old.v, new.v)) != old.v); + } + }else if (type == SIX_LOCK_write && lock->readers) { +if (try) { +atomic64_add (__SIX_VAL (write_locking,1 ), + &lock->state.counter); +smp_mb__after_atomic (); + } + + ret = !pcpu_read_count (lock); + +/* +* On success, we increment lock->seq; also we clear +* write_locking unless we failed from the lock path: +*/ + v =0 ; +if (ret) + v +=__SIX_VAL (seq,1 ); +if (ret || try) + v -=__SIX_VAL (write_locking,1 ); + +if (try && !ret) { + old.v =atomic64_add_return (v, &lock->state.counter); +six_lock_wakeup (lock, old, SIX_LOCK_read); + }else { +atomic64_add (v, &lock->state.counter); + } + }else { + v =READ_ONCE (lock->state.v); +do { + new.v = old.v = v; + +if (!(old.v & l[type].lock_fail)) { + new.v += l[type].lock_val; + +if (type == SIX_LOCK_write) + new.write_locking =0 ; + }else if (!try && type != SIX_LOCK_write && + !(new.waiters & (1 << type))) + new.waiters |=1 << type; +else +break ;/* waiting bit already set */ + }while ((v =atomic64_cmpxchg_acquire (&lock->state.counter, + old.v, new.v)) != old.v); + + ret = !(old.v & l[type].lock_fail); + +EBUG_ON (ret && !(lock->state.v & l[type].held_mask)); + } + +if (ret) +six_set_owner (lock, type, old); + +EBUG_ON (type == SIX_LOCK_write && (try || ret) && (lock->state.write_locking));
Quoted 8 lines
kernel/locking/six.c line 266
Quoted 4 lines
kernel/locking/six.c new file +4
-0+ +return ret; +} +^^^
I'd really like to see some more comments in the code above. It's pretty complex.
It's already got more comments than is typical for kernel locking code :)
But if there's specific things you'd like to see clarified, please do point them out.
Kent Overstreet
maintainerPATCH 5/32 MAINTAINERS: Add entry for six locks
In reply to Kent OverstreetSIX locks are a new locking primitive, shared/intent/exclusive, currently used by bcachefs but available for other uses. Mark them as maintained.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -19166,6 +19166,14 @@ S: Maintained W: http://www.winischhofer.at/linuxsisusbvga.shtml F: drivers/usb/misc/sisusbvga/ +SIX LOCKS +M: Kent Overstreet <[email protected]> +L: [email protected] +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/six.h +F: kernel/locking/six.c + SL28 CPLD MFD DRIVER M: Michael Walle <[email protected]> S: Maintained
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 6/32 sched: Add task_struct->faults_disabled_mapping
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
This is used by bcachefs to fix a page cache coherency issue with O_DIRECT writes.
Also relevant: mapping->invalidate_lock, see below.
O_DIRECT writes (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in.
But O_DIRECT writes invoke the page fault handler (via get_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general.
Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to task_struct for this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future.
---------------------------------
The closest current VFS equivalent is mapping->invalidate_lock, which comes from XFS. However, it's not used by direct IO. Instead, direct IO paths shoot down the page cache twice - before starting the IO and at the end, and they're still technically racy w.r.t. page cache coherency.
This is a more complete approach: in the future we might consider replacing mapping->invalidate_lock with the bcachefs code.
--- include/linux/sched.h | 1 + init/init_task.c | 1 + 2 files changed, 2 insertions(+)
include/linux/sched.h +1 -0
@@ -869,6 +869,7 @@ struct task_struct {struct mm_struct *mm;struct mm_struct *active_mm; +struct address_space *faults_disabled_mapping;int exit_state;int exit_code;
init/init_task.c +1 -0
@@ -85,6 +85,7 @@ struct task_struct init_task .nr_cpus_allowed= NR_CPUS, .mm = NULL, .active_mm = &init_mm, + .faults_disabled_mapping = NULL, .restart_block = { .fn = do_no_restart_syscall, },
Signature · 1 line
2.40.1
Jan Kara
reviewerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 18 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is used by bcachefs to fix a page cache coherency issue with
O_DIRECTwrites.Also relevant:
mapping->invalidate_lock, see below.
O_DIRECTwrites (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in.But
O_DIRECTwrites invoke the page fault handler (viaget_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general.
Quoted 5 lines
Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to
task_structfor this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future.
It would be nice to have at least a link to the code that's actually using the field you are adding.
Also I think we were already through this discussion [1] and we ended up agreeing that your scheme actually solves only the AA deadlock but a malicious userspace can easily create AB BA deadlock by running direct IO to file A using mapped file B as a buffer *and* direct IO to file B using mapped file A as a buffer.
[1] https://lore.kernel.org/all/[email protected]
Quoted 9 lines
---------------------------------
The closest current VFS equivalent is
mapping->invalidate_lock, which comes from XFS. However, it's not used by direct IO. Instead, direct IO paths shoot down the page cache twice - before starting the IO and at the end, and they're still technically racy w.r.t. page cache coherency.This is a more complete approach: in the future we might consider replacing
mapping->invalidate_lockwith the bcachefs code.
Yes, and this is because we never provided 100% consistent buffered VS direct IO behavior on the same file exactly because we never found the complexity worth the usefulness...
Honza
Quoted 37 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Jan Kara <[email protected]>Cc Darrick J. Wong <[email protected]>--- include/linux/sched.h | 1 + init/init_task.c | 1 + 2 files changed, 2 insertions(+)include/linux/sched.h +1
-0@@ -869,6 +869,7 @@ struct task_struct {struct mm_struct *mm;struct mm_struct *active_mm; +struct address_space *faults_disabled_mapping;int exit_state;int exit_code;init/init_task.c +1
-0@@ -85,6 +85,7 @@ struct task_struct init_task .nr_cpus_allowed= NR_CPUS, .mm = NULL, .active_mm = &init_mm, + .faults_disabled_mapping = NULL, .restart_block = { .fn = do_no_restart_syscall, },Signature · 1 line
2.40.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Kent Overstreet
maintainerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 19 lines · Jan Kara
Quoted 18 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is used by bcachefs to fix a page cache coherency issue with
O_DIRECTwrites.Also relevant:
mapping->invalidate_lock, see below.
O_DIRECTwrites (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in.But
O_DIRECTwrites invoke the page fault handler (viaget_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general.
Quoted 8 lines
Quoted 5 lines
Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to
task_structfor this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future.It would be nice to have at least a link to the code that's actually using the field you are adding.
Bit of a trick to link to a _later_ patch in the series from a commit message, but...
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n975
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n2454
Quoted 5 lines
Also I think we were already through this discussion [1] and we ended up agreeing that your scheme actually solves only the AA deadlock but a malicious userspace can easily create AB BA deadlock by running direct IO to file A using mapped file B as a buffer *and* direct IO to file B using mapped file A as a buffer.
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
David Howells was also just running into a strange locking situation with iov_iters and recursive gups - I don't recall all the details, but it sounded like this might be a solution for that. David, did you have thoughts on that?
Jan Kara
reviewerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 26 lines · Kent Overstreet
Quoted 25 lines · Jan Kara
Quoted 23 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is used by bcachefs to fix a page cache coherency issue with
O_DIRECTwrites.Also relevant:
mapping->invalidate_lock, see below.
O_DIRECTwrites (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in.But
O_DIRECTwrites invoke the page fault handler (viaget_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general.Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to
task_structfor this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future.
Quoted 8 lines
Quoted 2 lines
It would be nice to have at least a link to the code that's actually using the field you are adding.
Bit of a trick to link to a _later_ patch in the series from a commit message, but...
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n975
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n2454
Thanks and I'm sorry for the delay.
Quoted 8 lines
Quoted 5 lines
Also I think we were already through this discussion [1] and we ended up agreeing that your scheme actually solves only the AA deadlock but a malicious userspace can easily create AB BA deadlock by running direct IO to file A using mapped file B as a buffer *and* direct IO to file B using mapped file A as a buffer.
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks - b01b2d72da25 ("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for the iomap_dio_rw() call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 11 lines · Jan Kara
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
generic/708 is the btrfs version of this.
But I think all of the file systems that have this deadlock are actually fundamentally broken because they have a mess up locking hierarchy where page faults take the same lock that is held over the the direct I/ operation. And the right thing is to fix this. I have work in progress for btrfs, and something similar should apply to gfs2, with the added complication that it probably means a revision to their network protocol.
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 15 lines · Christoph Hellwig
Quoted 11 lines · Jan Kara
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
generic/708 is the btrfs version of this.
Quoted 7 lines
But I think all of the file systems that have this deadlock are actually fundamentally broken because they have a mess up locking hierarchy where page faults take the same lock that is held over the the direct I/ operation. And the right thing is to fix this. I have work in progress for btrfs, and something similar should apply to gfs2, with the added complication that it probably means a revision to their network protocol.
No, this is fundamentally because userspace controls the ordering of locking because the buffer passed to dio can point into any address space. You can't solve this by changing the locking heirarchy.
If you want to be able to have locking around adding things to the pagecache so that things that bypass the pagecache can prevent inconsistencies (and we do, the big one is fcollapse), and if you want dio to be able to use that same locking (because otherwise dio will also cause page cache inconsistency), this is the way to do it.
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 9 lines · Kent Overstreet
No, this is fundamentally because userspace controls the ordering of locking because the buffer passed to dio can point into any address space. You can't solve this by changing the locking heirarchy.
If you want to be able to have locking around adding things to the pagecache so that things that bypass the pagecache can prevent inconsistencies (and we do, the big one is fcollapse), and if you want dio to be able to use that same locking (because otherwise dio will also cause page cache inconsistency), this is the way to do it.
Well, it seems like you are talking about something else than the existing cases in gfs2 and btrfs, that is you want full consistency between direct I/O and buffered I/O. That's something nothing in the kernel has ever provided, so I'd be curious why you think you need it and want different semantics from everyone else?
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Kent Overstreet
No, this is fundamentally because userspace controls the ordering of locking because the buffer passed to dio can point into any address space. You can't solve this by changing the locking heirarchy.
If you want to be able to have locking around adding things to the pagecache so that things that bypass the pagecache can prevent inconsistencies (and we do, the big one is fcollapse), and if you want dio to be able to use that same locking (because otherwise dio will also cause page cache inconsistency), this is the way to do it.
Quoted 5 lines
Well, it seems like you are talking about something else than the existing cases in gfs2 and btrfs, that is you want full consistency between direct I/O and buffered I/O. That's something nothing in the kernel has ever provided, so I'd be curious why you think you need it and want different semantics from everyone else?
Because I like code that is correct.
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 7 lines · Kent Overstreet
Quoted 5 lines
Well, it seems like you are talking about something else than the existing cases in gfs2 and btrfs, that is you want full consistency between direct I/O and buffered I/O. That's something nothing in the kernel has ever provided, so I'd be curious why you think you need it and want different semantics from everyone else?
Because I like code that is correct.
Well, start with explaining your definition of correctness, why everyone else is "not correct", an how you can help fixing this correctness problem in the existing kernel. Thanks for your cooperation!
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Kent Overstreet
Quoted 5 lines
Well, it seems like you are talking about something else than the existing cases in gfs2 and btrfs, that is you want full consistency between direct I/O and buffered I/O. That's something nothing in the kernel has ever provided, so I'd be curious why you think you need it and want different semantics from everyone else?
Quoted 5 lines
Quoted 1 line
Because I like code that is correct.
Well, start with explaining your definition of correctness, why everyone else is "not correct", an how you can help fixing this correctness problem in the existing kernel. Thanks for your cooperation!
A cache that isn't actually consistent is a _bug_. You're being Obsequious. And any time this has come up in previous discussions (including at LSF), that was never up for debate, the only question has been whether it was even possible to practically fix it.
The DIO code recognizes cache incoherency as something to be avoided by shooting down the page cache both at the beginning of the IO _and again at the end_. That's the kind of obvious hackery for a race condition that we would like to avoid.
Regarding the consequences of this kind of bug - stale data exposed to userspace, possibly stale data overwriting a write we acked, and worse any filesystem state that hangs off the page cache being inconsistent with the data on disk.
And look, we've been over all this before, so I don't see what this adds to the discussion.
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 4 lines · Kent Overstreet
A cache that isn't actually consistent is a _bug_. You're being Obsequious. And any time this has come up in previous discussions (including at LSF), that was never up for debate, the only question has been whether it was even possible to practically fix it.
That is not my impression. But again, if you think it is useful, go ahead and seel people on the idea. But please prepare a series that includes the rationale, performance tradeoffs and real live implications for it. And do it on the existing code that people use and not just your shiny new thing.
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 11 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
A cache that isn't actually consistent is a _bug_. You're being Obsequious. And any time this has come up in previous discussions (including at LSF), that was never up for debate, the only question has been whether it was even possible to practically fix it.
That is not my impression. But again, if you think it is useful, go ahead and seel people on the idea. But please prepare a series that includes the rationale, performance tradeoffs and real live implications for it. And do it on the existing code that people use and not just your shiny new thing.
When I'm ready to lift this to the VFS level I will; it should simplify locking overall and it'll be one less thing for people to worry about.
(i.e. the fact that even _readahead_ can pull in pages a dio is invalidating is a really nice footgun if not worked around).
Right now though I've got more than enough on my plate just trying to finally get bcachefs merged, I'm happy to explain what this is for but I'm not ready for additional headaches or projects yet.
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Christoph HellwigQuoted 7 lines · Christoph Hellwig
Quoted 6 lines · Kent Overstreet
Quoted 5 lines
Well, it seems like you are talking about something else than the existing cases in gfs2 and btrfs, that is you want full consistency between direct I/O and buffered I/O. That's something nothing in the kernel has ever provided, so I'd be curious why you think you need it and want different semantics from everyone else?
Quoted 5 lines
Quoted 1 line
Because I like code that is correct.
Well, start with explaining your definition of correctness, why everyone else is "not correct", an how you can help fixing this correctness problem in the existing kernel. Thanks for your cooperation!
BTW, if you wanted a more serious answer, just asking for the commit message to be expanded would be a better way to ask...
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Christoph HellwigAm Di., 23. Mai 2023 um 18:28 Uhr schrieb Christoph Hellwig <[email protected]>:
Quoted 15 lines · Jan Kara
Quoted 11 lines
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
generic/708 is the btrfs version of this.
Quoted 7 lines
But I think all of the file systems that have this deadlock are actually fundamentally broken because they have a mess up locking hierarchy where page faults take the same lock that is held over the the direct I/ operation. And the right thing is to fix this. I have work in progress for btrfs, and something similar should apply to gfs2, with the added complication that it probably means a revision to their network protocol.
We do disable page faults, and there can be deadlocks in page fault handlers while no page faults are allowed.
I'm roughly aware of the locking hierarchy that other filesystems use, and that's something we want to avoid because of two reasons: (1) it would be an incompatible change, and (2) we want to avoid cluster-wide locking operations as much as possible because they are very slow.
These kinds of locking conflicts are so rare in practice that the theoretical inefficiency of having to retry the operation doesn't matter.
Quoted 4 lines
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Please let me know when those btrfs changes are in a presentable shape ...
Thanks,
Andreas
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 27 lines · Andreas Gr?nbacher
Am Di., 23. Mai 2023 um 18:28 Uhr schrieb Christoph Hellwig <[email protected]>:
Quoted 15 lines · Jan Kara
Quoted 11 lines
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
generic/708 is the btrfs version of this.
Quoted 7 lines
But I think all of the file systems that have this deadlock are actually fundamentally broken because they have a mess up locking hierarchy where page faults take the same lock that is held over the the direct I/ operation. And the right thing is to fix this. I have work in progress for btrfs, and something similar should apply to gfs2, with the added complication that it probably means a revision to their network protocol.
We do disable page faults, and there can be deadlocks in page fault handlers while no page faults are allowed.
Quoted 8 lines
I'm roughly aware of the locking hierarchy that other filesystems use, and that's something we want to avoid because of two reasons: (1) it would be an incompatible change, and (2) we want to avoid cluster-wide locking operations as much as possible because they are very slow.
These kinds of locking conflicts are so rare in practice that the theoretical inefficiency of having to retry the operation doesn't matter.
Would you be willing to expand on that? I'm wondering if this would simplify things for gfs2, but you mention locking heirarchy being an incompatible change - how does that work?
Quoted 7 lines
Quoted 5 lines
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Please let me know when those btrfs changes are in a presentable shape ...
I would also be curious to know what btrfs needs and what the approach is there.
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Am Fr., 26. Mai 2023 um 01:20 Uhr schrieb Kent Overstreet <[email protected]>:
Quoted 33 lines · Andreas Gr?nbacher
Quoted 32 lines
Am Di., 23. Mai 2023 um 18:28 Uhr schrieb Christoph Hellwig <[email protected]>:
Quoted 15 lines · Jan Kara
Quoted 11 lines
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
generic/708 is the btrfs version of this.
Quoted 7 lines
But I think all of the file systems that have this deadlock are actually fundamentally broken because they have a mess up locking hierarchy where page faults take the same lock that is held over the the direct I/ operation. And the right thing is to fix this. I have work in progress for btrfs, and something similar should apply to gfs2, with the added complication that it probably means a revision to their network protocol.
We do disable page faults, and there can be deadlocks in page fault handlers while no page faults are allowed.
I'm roughly aware of the locking hierarchy that other filesystems use, and that's something we want to avoid because of two reasons: (1) it would be an incompatible change, and (2) we want to avoid cluster-wide locking operations as much as possible because they are very slow.
Quoted 7 lines
Quoted 3 lines
These kinds of locking conflicts are so rare in practice that the theoretical inefficiency of having to retry the operation doesn't matter.
Would you be willing to expand on that? I'm wondering if this would simplify things for gfs2, but you mention locking heirarchy being an incompatible change - how does that work?
Oh, it's just that gfs2 uses one dlm lock per inode to control access to that inode. In the code, this is called the "inode glock" --- glocks being an abstraction above dlm locks --- but it boils down to dlm locks in the end. An additional layer of locking will only work correctly if all cluster nodes use the new locks consistently, so old cluster nodes will become incompatible. Those kinds of changes are hard.
But the additional lock taking would also hurt performance, forever, and I'd really like to avoid taking that hit.
It may not be obvious to everyone, but allowing page faults during reads and writes (i.e., while holding dlm locks) can lead to distributed deadlocks. We cannot just pretend to be a local filesystem.
Thanks,
Andreas
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Please let me know when those btrfs changes are in a presentable shape ...
I would also be curious to know what btrfs needs and what the approach is there.
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 8 lines · Andreas Gr?nbacher
Oh, it's just that gfs2 uses one dlm lock per inode to control access to that inode. In the code, this is called the "inode glock" --- glocks being an abstraction above dlm locks --- but it boils down to dlm locks in the end. An additional layer of locking will only work correctly if all cluster nodes use the new locks consistently, so old cluster nodes will become incompatible. Those kinds of changes are hard.
Quoted 7 lines
But the additional lock taking would also hurt performance, forever, and I'd really like to avoid taking that hit.
It may not be obvious to everyone, but allowing page faults during reads and writes (i.e., while holding dlm locks) can lead to distributed deadlocks. We cannot just pretend to be a local filesystem.
Ah, gotcha. Same basic issue then, dio -> page fault means you're taking two DLM locks simulateously, so without lock ordering you get deadlock.
So that means promoting this to the VFS won't be be useful to you (because the VFS lock will be a local lock, not your DLM lock).
Do you have trylock() and a defined lock ordering you can check for or glocks (inode number)? Here's the new and expanded commit message, in case my approach is of interest to you:
commit 32858d0fb90b503c8c39e899ad5155e44639d3b5
Author: Kent Overstreet <[email protected]> Date: Wed Oct 16 15:03:50 2019 -0400
sched: Add task_struct->faults_disabled_mapping
There has been a long standing page cache coherence bug with direct IO. This provides part of a mechanism to fix it, currently just used by bcachefs but potentially worth promoting to the VFS.
Direct IO evicts the range of the pagecache being read or written to.
For reads, we need dirty pages to be written to disk, so that the read doesn't return stale data. For writes, we need to evict that range of the pagecache so that it's not stale after the write completes.
However, without a locking mechanism to prevent those pages from being re-added to the pagecache - by a buffered read or page fault - page cache inconsistency is still possible.
This isn't necessarily just an issue for userspace when they're playing games; filesystems may hang arbitrary state off the pagecache, and so page cache inconsistency may cause real filesystem bugs, depending on the filesystem. This is less of an issue for iomap based filesystems, but e.g. buffer heads caches disk block mappings (!) and attaches them to the pagecache, and bcachefs attaches disk reservations to pagecache pages.
This issue has been hard to fix, because
- we need to add a lock (henceforth calld pagecache_add_lock), which would be held for the duration of the direct IO
- page faults add pages to the page cache, thus need to take the same lock
- dio -> gup -> page fault thus can deadlock
And we cannot enforce a lock ordering with this lock, since userspace will be controlling the lock ordering (via the fd and buffer arguments to direct IOs), so we need a different method of deadlock avoidance.
We need to tell the page fault handler that we're already holding a pagecache_add_lock, and since plumbing it through the entire gup() path would be highly impractical this adds a field to task_struct.
Then the full method is:
- in the dio path, when we take first pagecache_add_lock, note the mapping in task_struct
- in the page fault handler, if faults_disabled_mapping is set, we check if it's the same mapping as the one taking a page fault for, and if so return an error.
Then we check lock ordering: if there's a lock ordering violation and trylock fails, we'll have to cycle the locks and return an error that tells the DIO path to retry: faults_disabled_mapping is also used for signalling "locks were dropped, please retry".
Also relevant to this patch: mapping->invalidate_lock. mapping->invalidate_lock provides most of the required semantics - it's used by truncate/fallocate to block pages being added to the pagecache. However, since it's a rwsem, direct IOs would need to take the write side in order to block page cache adds, and would then be exclusive with each other - we'll need a new type of lock to pair with this approach.
Re: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Kent OverstreetQuoted 9 lines · Kent Overstreet
Quoted 6 lines
Quoted 4 lines
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Please let me know when those btrfs changes are in a presentable shape ...
I would also be curious to know what btrfs needs and what the approach is there.
btrfs has the extent locked, where "extent locked" is a somewhat magic range lock that actually includes different lock bits. It does so because it clears the page writeback bit when the data made it to the media, but before the metadata required to find it is commited, and the extent lock prevents it from trying to do a readpage on something that has actually very recently been written back but not fully commited. Once btrfs is changed to only clear the page writeback bit once the write is fully commited like in other file systems this extra level of locking can go away, and there are no more locks in the readpage path that are also taken by the direct I/O code. With that a lot of code in btrfs working around this can go away, including the no fault direct I/O code.
Kent Overstreet
maintainerRe: [Cluster-devel] [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Kent Overstreet
Quoted 6 lines
Quoted 4 lines
I'm absolutely not in favour to add workarounds for thes kind of locking problems to the core kernel. I already feel bad for allowing the small workaround in iomap for btrfs, as just fixing the locking back then would have avoid massive ratholing.
Please let me know when those btrfs changes are in a presentable shape ...
I would also be curious to know what btrfs needs and what the approach is there.
Quoted 12 lines
btrfs has the extent locked, where "extent locked" is a somewhat magic range lock that actually includes different lock bits. It does so because it clears the page writeback bit when the data made it to the media, but before the metadata required to find it is commited, and the extent lock prevents it from trying to do a readpage on something that has actually very recently been written back but not fully commited. Once btrfs is changed to only clear the page writeback bit once the write is fully commited like in other file systems this extra level of locking can go away, and there are no more locks in the readpage path that are also taken by the direct I/O code. With that a lot of code in btrfs working around this can go away, including the no fault direct I/O code.
wow, yeah, that is not how that is supposed to work...
Kent Overstreet
maintainerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Jan KaraQuoted 10 lines
Quoted 2 lines
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.
Oof, that sounds a bit sketchy. What happens if the dio call passes in an address from the same address space? What happens if we race with the pages we faulted in being evicted?
Quoted 3 lines
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
More tests more good.
So if we want to lift this scheme to the VFS layer, we'd start by replacing the lock you added (grepping for it, the name escapes me) with a different type of lock - two_state_shared_lock in my code, it's like a rw lock except writers don't exclude other writers. That way the DIO path can use it without singlethreading writes to a single file.
Jan Kara
reviewerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 11 lines · Kent Overstreet
Quoted 10 lines
Quoted 2 lines
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.
Quoted 2 lines
Oof, that sounds a bit sketchy. What happens if the dio call passes in an address from the same address space?
If we submit direct IO that uses mapped file F at offset O as a buffer for direct IO from file F, offset O, it will currently livelock in an indefinite retry loop. It should rather return error or fall back to buffered IO. But that should be fixable. Andreas?
But if the buffer and direct IO range does not overlap, it will just happily work - iomap_dio_rw() invalidates only the range direct IO is done to.
Quoted 1 line
What happens if we race with the pages we faulted in being evicted?
We fault them in again and retry.
Quoted 11 lines
Quoted 3 lines
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
More tests more good.
So if we want to lift this scheme to the VFS layer, we'd start by replacing the lock you added (grepping for it, the name escapes me) with a different type of lock -
two_state_shared_lockin my code, it's like a rw lock except writers don't exclude other writers. That way the DIO path can use it without singlethreading writes to a single file.
Yes, I've noticed that you are introducing in bcachefs a lock with very similar semantics to mapping->invalidate_lock, just with this special lock type. What I'm kind of worried about with two_state_shared_lock as implemented in bcachefs is the fairness. AFAICS so far if someone is e.g. heavily faulting pages on a file, direct IO to that file can be starved indefinitely. That is IMHO not a good thing and I would not like to use this type of lock in VFS until this problem is resolved. But it should be fixable e.g. by introducing some kind of deadline for a waiter after which it will block acquisitions of the other lock state.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Kent Overstreet
maintainerRe: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
Quoted 8 lines · Jan Kara
If we submit direct IO that uses mapped file F at offset O as a buffer for direct IO from file F, offset O, it will currently livelock in an indefinite retry loop. It should rather return error or fall back to buffered IO. But that should be fixable. Andreas?
But if the buffer and direct IO range does not overlap, it will just happily work -
iomap_dio_rw()invalidates only the range direct IO is done to.
*nod*
readahead triggered from the page fault path is another consideration. No idea how that interacts with the gf2s method; IIRC there's a hack in the page fault path that says somewhere "we may be getting called via gup(), don't invoke readahead".
We could potentially kill that hack if we lifted this to the VFS layer.
Quoted 17 lines
Quoted 2 lines
What happens if we race with the pages we faulted in being evicted?
We fault them in again and retry.
Quoted 11 lines
Quoted 3 lines
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
More tests more good.
So if we want to lift this scheme to the VFS layer, we'd start by replacing the lock you added (grepping for it, the name escapes me) with a different type of lock -
two_state_shared_lockin my code, it's like a rw lock except writers don't exclude other writers. That way the DIO path can use it without singlethreading writes to a single file.
Quoted 9 lines
Yes, I've noticed that you are introducing in bcachefs a lock with very similar semantics to
mapping->invalidate_lock, just with this special lock type. What I'm kind of worried about withtwo_state_shared_lockas implemented in bcachefs is the fairness. AFAICS so far if someone is e.g. heavily faulting pages on a file, direct IO to that file can be starved indefinitely. That is IMHO not a good thing and I would not like to use this type of lock in VFS until this problem is resolved. But it should be fixable e.g. by introducing some kind of deadline for a waiter after which it will block acquisitions of the other lock state.
Yeah, my two_state_shared lock is definitely at the quick and dirty prototype level, the implementation would need work. Lockdep support would be another hard requirement.
The deadline might be a good idea, OTOH it'd want tuning. Maybe something like what rwsem does where we block new read acquirerers if there's a writer waiting would work.
Re: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Jan KaraAm Do., 25. Mai 2023 um 10:56 Uhr schrieb Jan Kara <[email protected]>:
Quoted 12 lines · Kent Overstreet
Quoted 11 lines
Quoted 10 lines
Quoted 2 lines
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.
Quoted 7 lines
Quoted 2 lines
Oof, that sounds a bit sketchy. What happens if the dio call passes in an address from the same address space?
If we submit direct IO that uses mapped file F at offset O as a buffer for direct IO from file F, offset O, it will currently livelock in an indefinite retry loop. It should rather return error or fall back to buffered IO. But that should be fixable. Andreas?
Yes, I guess. Thanks for the heads-up.
Andreas
Quoted 34 lines
But if the buffer and direct IO range does not overlap, it will just happily work -
iomap_dio_rw()invalidates only the range direct IO is done to.Quoted 1 line
What happens if we race with the pages we faulted in being evicted?
We fault them in again and retry.
Quoted 11 lines
Quoted 3 lines
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
More tests more good.
So if we want to lift this scheme to the VFS layer, we'd start by replacing the lock you added (grepping for it, the name escapes me) with a different type of lock -
two_state_shared_lockin my code, it's like a rw lock except writers don't exclude other writers. That way the DIO path can use it without singlethreading writes to a single file.Yes, I've noticed that you are introducing in bcachefs a lock with very similar semantics to
mapping->invalidate_lock, just with this special lock type. What I'm kind of worried about withtwo_state_shared_lockas implemented in bcachefs is the fairness. AFAICS so far if someone is e.g. heavily faulting pages on a file, direct IO to that file can be starved indefinitely. That is IMHO not a good thing and I would not like to use this type of lock in VFS until this problem is resolved. But it should be fixable e.g. by introducing some kind of deadline for a waiter after which it will block acquisitions of the other lock state.Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
Re: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping
In reply to Jan KaraAm Di., 23. Mai 2023 um 15:37 Uhr schrieb Jan Kara <[email protected]>:
Quoted 55 lines · Kent Overstreet
Quoted 26 lines · Jan Kara
Quoted 25 lines · Kent Overstreet
Quoted 23 lines
From: Kent Overstreet <[email protected]>
This is used by bcachefs to fix a page cache coherency issue with
O_DIRECTwrites.Also relevant:
mapping->invalidate_lock, see below.
O_DIRECTwrites (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in.But
O_DIRECTwrites invoke the page fault handler (viaget_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general.Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to
task_structfor this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future.Quoted 8 lines
Quoted 2 lines
It would be nice to have at least a link to the code that's actually using the field you are adding.
Bit of a trick to link to a _later_ patch in the series from a commit message, but...
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n975
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/fs-io.c#n2454Thanks and I'm sorry for the delay.
Quoted 8 lines
Quoted 5 lines
Also I think we were already through this discussion [1] and we ended up agreeing that your scheme actually solves only the AA deadlock but a malicious userspace can easily create AB BA deadlock by running direct IO to file A using mapped file B as a buffer *and* direct IO to file B using mapped file A as a buffer.
No, that's definitely handled (and you can see it in the code I linked), and I wrote a torture test for fstests as well.
I've checked the code and AFAICT it is all indeed handled. BTW, I've now remembered that GFS2 has dealt with the same deadlocks -
b01b2d72da25("gfs2: Fix mmap + page fault deadlocks for direct I/O") - in a different way (by prefaulting pages from the iter before grabbing the problematic lock and then disabling page faults for theiomap_dio_rw()call). I guess we should somehow unify these schemes so that we don't have two mechanisms for avoiding exactly the same deadlock. Adding GFS2 guys to CC.
Quoted 3 lines
Also good that you've written a fstest for this, that is definitely a useful addition, although I suspect GFS2 guys added a test for this not so long ago when testing their stuff. Maybe they have a pointer handy?
Ah yes, that's xfstests commit d3cbdabf ("generic: Test page faults during read and write").
Thanks,
Andreas
Quoted 4 lines
Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
Kent Overstreet
maintainerPATCH 7/32 mm: Bring back vmalloc_exec
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
--- include/linux/vmalloc.h | 1 + kernel/module/main.c | 4 +--- mm/nommu.c | 18 ++++++++++++++++++ mm/vmalloc.c | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-)
include/linux/vmalloc.h +1 -0
@@ -145,6 +145,7 @@ extern void *vzalloc(unsigned long size) __alloc_size(1);extern void *vmalloc_user (unsigned long size)__alloc_size(1) ;extern void *vmalloc_node (unsigned long size,int node)__alloc_size(1) ;extern void *vzalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vmalloc_exec (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;extern void *vmalloc_32 (unsigned long size)__alloc_size(1) ;extern void *vmalloc_32_user (unsigned long size)__alloc_size(1) ;extern void *__vmalloc (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;
kernel/module/main.c +1 -3
@@ -1607,9 +1607,7 @@ static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbgvoid *__weak module_alloc (unsigned long size) { -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); +return vmalloc_exec (size,GFP_KERNEL ); }bool __weak module_init_section (const char *name)
mm/nommu.c +18 -0
@@ -280,6 +280,24 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +*/ + +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc (size, gfp_mask); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +/** * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) * @size: allocation size
mm/vmalloc.c +21 -0
@@ -3401,6 +3401,27 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +#if defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA32 )#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)#elif defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA )
Signature · 1 line
2.40.1
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Small nits -
Would be good to refer to the original patch that removed it, i.e. 7a0e27b2a0ce ("mm: remove vmalloc_exec") something like 'patch ... folded vmalloc_exec() into its one user, however bcachefs requires this as well so revert'.
Would also be good to mention that you are now exporting the function which the original didn't appear to do.
Quoted 6 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Andrew Morton <[email protected]>Cc Uladzislau Rezki <[email protected]>Cc Christoph Hellwig <[email protected]>
Another nit: I'm a vmalloc reviewer so would be good to get cc'd too :) (forgivable mistake as very recent change!)
Quoted 96 lines
--- include/linux/vmalloc.h | 1 + kernel/module/main.c | 4 +--- mm/nommu.c | 18 ++++++++++++++++++ mm/vmalloc.c | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-)include/linux/vmalloc.h +1
-0@@ -145,6 +145,7 @@ extern void *vzalloc(unsigned long size) __alloc_size(1);extern void *vmalloc_user (unsigned long size)__alloc_size(1) ;extern void *vmalloc_node (unsigned long size,int node)__alloc_size(1) ;extern void *vzalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vmalloc_exec (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;extern void *vmalloc_32 (unsigned long size)__alloc_size(1) ;extern void *vmalloc_32_user (unsigned long size)__alloc_size(1) ;extern void *__vmalloc (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;kernel/module/main.c +1
-3@@ -1607,9 +1607,7 @@ static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbgvoid *__weak module_alloc (unsigned long size) { -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); +return vmalloc_exec (size,GFP_KERNEL ); }bool __weak module_init_section (const char *name)mm/nommu.c +18
-0@@ -280,6 +280,24 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +*/ + +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc (size, gfp_mask); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +/** * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) * @size: allocation size mm/vmalloc.c +21
-1@@ -3401,6 +3401,27 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +#if defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA32 )#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)#elif defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA ) --
Quoted 2 lines
2.40.1
Otherwise lgtm, feel free to add:
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 17 lines · Lorenzo Stoakes
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Small nits -
Would be good to refer to the original patch that removed it, i.e.
7a0e27b2a0ce("mm: removevmalloc_exec") something like 'patch ... foldedvmalloc_exec()into its one user, however bcachefs requires this as well so revert'.Would also be good to mention that you are now exporting the function which the original didn't appear to do.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Andrew Morton <[email protected]>Cc Uladzislau Rezki <[email protected]>Cc Christoph Hellwig <[email protected]>Another nit: I'm a vmalloc reviewer so would be good to get cc'd too :) (forgivable mistake as very recent change!)
Thanks - folded your suggestions into the commit message, and added you for the next posting :)
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetQuoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 10 lines · Lorenzo Stoakes
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Quoted 6 lines
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
None that I see.
The background is that bcachefs generates a per btree node unpack function, based on the packed format for that btree node, for unpacking keys within that node. The unpack function is only ~50 bytes, and for locality we want it to be located with the btree node's other in-memory lookup tables so they can be prefetched all at once.
Here's the codegen:
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/bkey.c#n727
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 26 lines · Kent Overstreet
Quoted 10 lines · Lorenzo Stoakes
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Quoted 6 lines
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
None that I see.
The background is that bcachefs generates a per btree node unpack function, based on the packed format for that btree node, for unpacking keys within that node. The unpack function is only ~50 bytes, and for locality we want it to be located with the btree node's other in-memory lookup tables so they can be prefetched all at once.
Quoted 3 lines
Here's the codegen:
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/bkey.c#n727
Well, it's a cool trick, but it's not clear that it actually belongs in production kernel code. What else in the kernel actually does dynamic codegen? Just BPF, I think?
Among other issues, this is entirely architecture-specific, and it may cause interoperability issues with various other features, including security features. Is it really safe to leave a W&X page around, for example?
What seems to be missing is any explanation for what we're actually getting from this extremely unusual solution that cannot be gained any other way. What is unique about bcachefs that it really needs something like this?
- Eric
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 3 lines · Eric Biggers
What seems to be missing is any explanation for what we're actually getting from this extremely unusual solution that cannot be gained any other way. What is unique about bcachefs that it really needs something like this?
Ok, as promised:
Background: all metadata in bcachefs is a structured as key/value pairs, and there's a common key format for all keys.
struct bkey {
/* 3 byte header */
u8 u64s; /* size of k/v in u64s */
u8 format; /* packed/unpacked, needs_whiteout */
u8 type; /* value type */
u8 pad;
/*
* Order of fields below is for little endian, they're in
* reverse order on big endian (and byte swabbed as necessary
* when reading foreign endian metadata)
*
* Since field order matches byte order, the key can be treated
* as one large multi word integer for doing comparisons:
*/
u96 version; /* nonces, send/recv support */
u32 size; /* size of extent keys */
/* Below are the field used for ordering/comparison: */
u32 snapshot;
u64 offset;
u64 inode;
/* Value is stored inline with key */
struct bch_val v;
};sizeof(struct bkey) == 40.
An extent value that has one pointer and no checksum is 8 bytes, with one pointer and one 32 bit checksum 16 bytes, for 56 bytes total (key included).
But for a given btree node, most of the key fields will typically be redundandant. An extents leaf node might have extents for all one inode number or a small range of inode numbers, snapshots may or may not be in use, etc. - clearly some compression is desirable here.
The key observation is that key compression is possible if we have a compression function that preserves order, and an order-preserving compression function is possible if it's allowed to fail. That means we do comparisons on packed keys, which lets us skip _most_ unpack operations, for btree node resorts and for lookups within a node.
Packing works by defining a format with an offset and a bit width for each field, so e.g. if all keys in a btree node have the same inode number the packed format can specify that inode number and then a field width of 0 bits for the inode field.
Continuing the arithmetic from before, a packed extent key will typically be only 8 or 16 bytes, or 24-32 including the val, which means bkey packing cuts our metadata size roughly in half.
(It also makes our key format somewhat self describing and gives us a mechanism by which we could add or extend fields in the future).
-----------------------------------------------------
As mentioned before, since packed bkeys are still multi-word integers we can do some important operations without unpacking, but to iterate over keys, compare packed & unpacked keys in resort, etc. - we'll still need to unpack, so we need this operation to be as fast as possible.
bkey.c __bch2_bkey_unpack_key() is the unspecialized version written in C, that works on any archictecture. It loops over the fields in a bkey_format, pulling them out of the input words and adding back the field offsets. It's got the absolute minimum number of branches - one per field, when deciding to advance to the next input word - but it can't be branchless and it's a whole ton of shifts and bitops.
dynamic codegen lets us produce unpack functions that are fully branchless and _much_ smaller. For any given btree node we'll have a format where multiple fields have 0 field with - i.e. those fields are always constants. That code goes away, and also if the format can be byte aligned we can eliminate shifts and bitopts. Code size for the dynamically compiled unpack functions is roughly 10% that of the unspecialized C version.
I hope that addresses some of the "what is this even for" questions :)
Cheers,
Kent
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Hi Kent,
Quoted 88 lines · Kent Overstreet
Quoted 3 lines · Eric Biggers
What seems to be missing is any explanation for what we're actually getting from this extremely unusual solution that cannot be gained any other way. What is unique about bcachefs that it really needs something like this?
Ok, as promised:
Background: all metadata in bcachefs is a structured as key/value pairs, and there's a common key format for all keys.
struct bkey { /* 3 byte header */ u8 u64s; /* size of k/v in u64s */ u8 format; /* packed/unpacked, needs_whiteout */ u8 type; /* value type */ u8 pad; /* * Order of fields below is for little endian, they're in * reverse order on big endian (and byte swabbed as necessary * when reading foreign endian metadata) * * Since field order matches byte order, the key can be treated * as one large multi word integer for doing comparisons: */ u96 version; /* nonces, send/recv support */ u32 size; /* size of extent keys */ /* Below are the field used for ordering/comparison: */ u32 snapshot; u64 offset; u64 inode; /* Value is stored inline with key */ struct bch_val v; };sizeof(struct bkey) == 40.
An extent value that has one pointer and no checksum is 8 bytes, with one pointer and one 32 bit checksum 16 bytes, for 56 bytes total (key included).
But for a given btree node, most of the key fields will typically be redundandant. An extents leaf node might have extents for all one inode number or a small range of inode numbers, snapshots may or may not be in use, etc. - clearly some compression is desirable here.
The key observation is that key compression is possible if we have a compression function that preserves order, and an order-preserving compression function is possible if it's allowed to fail. That means we do comparisons on packed keys, which lets us skip _most_ unpack operations, for btree node resorts and for lookups within a node.
Packing works by defining a format with an offset and a bit width for each field, so e.g. if all keys in a btree node have the same inode number the packed format can specify that inode number and then a field width of 0 bits for the inode field.
Continuing the arithmetic from before, a packed extent key will typically be only 8 or 16 bytes, or 24-32 including the val, which means bkey packing cuts our metadata size roughly in half.
(It also makes our key format somewhat self describing and gives us a mechanism by which we could add or extend fields in the future).
-----------------------------------------------------
As mentioned before, since packed bkeys are still multi-word integers we can do some important operations without unpacking, but to iterate over keys, compare packed & unpacked keys in resort, etc. - we'll still need to unpack, so we need this operation to be as fast as possible.
bkey.c__bch2_bkey_unpack_key()is the unspecialized version written in C, that works on any archictecture. It loops over the fields in abkey_format, pulling them out of the input words and adding back the field offsets. It's got the absolute minimum number of branches - one per field, when deciding to advance to the next input word - but it can't be branchless and it's a whole ton of shifts and bitops.dynamic codegen lets us produce unpack functions that are fully branchless and _much_ smaller. For any given btree node we'll have a format where multiple fields have 0 field with - i.e. those fields are always constants. That code goes away, and also if the format can be byte aligned we can eliminate shifts and bitopts. Code size for the dynamically compiled unpack functions is roughly 10% that of the unspecialized C version.
Quoted 4 lines
I hope that addresses some of the "what is this even for" questions :)
Cheers,
Kent
I don't think this response addresses all the possibilities for optimizing the C implementation, so I'd like to bring up a few and make sure that you've explored them.
To summarize, you need to decode 6 fields that are each a variable number of bits (not known at compile time), and add an offset (also not known at compile time) to each field.
I don't think the offset is particularly interesting. Adding an offset to each field is very cheap and trivially parallelizable by the CPU.
It's really the bit width that's "interesting", as it must be the serialized decoding of variable-length fields that slows things down a lot.
First, I wanted to mention that decoding of variable-length fields has been extensively studied for decompression algorithms, e.g. for Huffman decoding. And it turns out that it can be done branchlessly. The basic idea is that you have a branchless refill step that looks like the following:
#define REFILL_BITS_BRANCHLESS() \
bitbuf |= get_unaligned_u64(p) << bitsleft; \
p += 7 - ((bitsleft >> 3) & 0x7); \
bitsleft |= 56;That branchlessly ensures that 'bitbuf' contains '56 <= bitsleft <= 63' bits. Then, the needed number of bits can be removed and returned:
#define READ_BITS(n) \
REFILL_BITS_BRANCHLESS(); \
tmp = bitbuf & (((u64)1 << (n)) - 1); \
bitbuf >>= (n); \
bitsleft -= (n); \
tmpIf you're interested, I can give you some references about the above method. But, I really just wanted to mention it for completeness, since I think you'd actually want to go in a slightly different direction, since (a) you have all the field widths available from the beginning, as opposed to being interleaved into the bitstream itself (as is the case in Huffman decoding for example), so you're not limited to serialized decoding of each field, (b) your fields are up to 96 bits, and (c) you've selected a bitstream convention that seems to make it such that your stream *must* be read in aligned units of u64, so I don't think something like REFILL_BITS_BRANCHLESS() could work for you anyway.
What I would suggest instead is preprocessing the list of 6 field lengths to create some information that can be used to extract all 6 fields branchlessly with no dependencies between different fields. (And you clearly *can* add a preprocessing step, as you already have one -- the dynamic code generator.)
So, something like the following:
const struct field_info *info = &format->fields[0];
field0 = (in->u64s[info->word_idx] >> info->shift1) & info->mask;
field0 |= in->u64s[info->word_idx - 1] >> info->shift2;... but with the code for all 6 fields interleaved.
On modern CPUs, I think that would be faster than your current C code.
You could do better by creating variants that are specialized for specific common sets of parameters. During "preprocessing", you would select a variant and set an enum accordingly. During decoding, you would switch on that enum and call the appropriate variant. (This could also be done with a function pointer, of course, but indirect calls are slow these days...)
For example, you mentioned that 8-byte packed keys is a common case. In that case there is only a single u64 to decode from, so you could create a function that just handles that case:
field0 = (word >> info->shift) & info->mask;
You could also create other variants, e.g.:
- 16-byte packed keys (which you mentioned are common)
- Some specific set of fields have zero width so don't need to be extracted (which it sounds like is common, or is it different fields each time?)
- All fields having specific lengths (are there any particularly common cases?)
Have you considered any of these ideas?
- Eric
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 28 lines · Eric Biggers
What I would suggest instead is preprocessing the list of 6 field lengths to create some information that can be used to extract all 6 fields branchlessly with no dependencies between different fields. (And you clearly *can* add a preprocessing step, as you already have one -- the dynamic code generator.)
So, something like the following:
const struct field_info *info = &format->fields[0]; field0 = (in->u64s[info->word_idx] >> info->shift1) & info->mask; field0 |= in->u64s[info->word_idx - 1] >> info->shift2;... but with the code for all 6 fields interleaved.
On modern CPUs, I think that would be faster than your current C code.
You could do better by creating variants that are specialized for specific common sets of parameters. During "preprocessing", you would select a variant and set an enum accordingly. During decoding, you would switch on that enum and call the appropriate variant. (This could also be done with a function pointer, of course, but indirect calls are slow these days...)
For example, you mentioned that 8-byte packed keys is a common case. In that case there is only a single u64 to decode from, so you could create a function that just handles that case:
field0 = (word >> info->shift) & info->mask;
Quoted 8 lines
You could also create other variants, e.g.:
- 16-byte packed keys (which you mentioned are common)
- Some specific set of fields have zero width so don't need to be extracted (which it sounds like is common, or is it different fields each time?)
- All fields having specific lengths (are there any particularly common cases?)Have you considered any of these ideas?
I like that idea.
Gonna hack some code... :)
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Eric BiggersQuoted 13 lines · Eric Biggers
First, I wanted to mention that decoding of variable-length fields has been extensively studied for decompression algorithms, e.g. for Huffman decoding. And it turns out that it can be done branchlessly. The basic idea is that you have a branchless refill step that looks like the following:
#define REFILL_BITS_BRANCHLESS() \ bitbuf |= get_unaligned_u64(p) << bitsleft; \ p += 7 - ((bitsleft >> 3) & 0x7); \ bitsleft |= 56;That branchlessly ensures that 'bitbuf' contains '56 <= bitsleft <= 63' bits. Then, the needed number of bits can be removed and returned:
Quoted 8 lines
#define READ_BITS(n) \ REFILL_BITS_BRANCHLESS(); \ tmp = bitbuf & (((u64)1 << (n)) - 1); \ bitbuf >>= (n); \ bitsleft -= (n); \ tmpIf you're interested, I can give you some references about the above method.
I might be interested in those references, new bit tricks and integer encodings are always fun :)
Quoted 23 lines
But, I really just wanted to mention it for completeness, since I think you'd actually want to go in a slightly different direction, since (a) you have all the field widths available from the beginning, as opposed to being interleaved into the bitstream itself (as is the case in Huffman decoding for example), so you're not limited to serialized decoding of each field, (b) your fields are up to 96 bits, and (c) you've selected a bitstream convention that seems to make it such that your stream *must* be read in aligned units of u64, so I don't think something like
REFILL_BITS_BRANCHLESS()could work for you anyway.What I would suggest instead is preprocessing the list of 6 field lengths to create some information that can be used to extract all 6 fields branchlessly with no dependencies between different fields. (And you clearly *can* add a preprocessing step, as you already have one -- the dynamic code generator.)
So, something like the following:
const struct field_info *info = &format->fields[0]; field0 = (in->u64s[info->word_idx] >> info->shift1) & info->mask; field0 |= in->u64s[info->word_idx - 1] >> info->shift2;... but with the code for all 6 fields interleaved.
Quoted 7 lines
On modern CPUs, I think that would be faster than your current C code.
You could do better by creating variants that are specialized for specific common sets of parameters. During "preprocessing", you would select a variant and set an enum accordingly. During decoding, you would switch on that enum and call the appropriate variant. (This could also be done with a function pointer, of course, but indirect calls are slow these days...)
testing random btree updates:
dynamically generated unpack:rand_insert: 20.0 MiB with 1 threads in 33 sec, 1609 nsec per iter, 607 KiB per sec
old C unpack:rand_insert: 20.0 MiB with 1 threads in 35 sec, 1672 nsec per iter, 584 KiB per sec
the Eric Biggers special:rand_insert: 20.0 MiB with 1 threads in 35 sec, 1676 nsec per iter, 583 KiB per sec
Tested two versions of your approach, one without a shift value, one where we use a shift value to try to avoid unaligned access - second was perhaps 1% faster
so it's not looking good. This benchmark doesn't even hit on unpack_key() quite as much as I thought, so the difference is significant.
fs/bcachefs/bkey.c +72 -19
@@ -7,6 +7,8 @@#include "bset.h" #include "util.h" +#include <asm/unaligned.h> +#undef EBUG_ON#ifdef DEBUG_BKEYS
@@ -19,9 +21,35 @@ const struct bkey_format bch2_bkey_format_current = BKEY_FORMAT_CURRENT;struct bkey_format_processed bch2_bkey_format_postprocess (const struct bkey_format f) { -return (struct bkey_format_processed ) { - .f = f, - }; +struct bkey_format_processed ret = { .f = f, .aligned = true }; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +unsigned offset = f.key_u64s *64 ; +#else +unsigned offset =KEY_PACKED_BITS_START ; +#endif + +for (unsigned i =0 ; i <BKEY_NR_FIELDS ; i++) { +unsigned bits = f.bits_per_field[i]; + +if (bits &7 ) { + ret.aligned = false; +break ; + } + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + offset -= bits; +#endif + + ret.shift[i] =min (offset &63 ,64 - bits); + ret.offset[i] = (offset - ret.shift[i]) /8 ; + ret.mask[i] = bits ? ~0ULL >> (64 - bits) :0 ; + +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + offset += bits; +#endif + } + +return ret; } void bch2_bkey_packed_to_binary_text(struct printbuf *out,
@@ -191,6 +219,19 @@ static u64 get_inc_field(struct unpack_state *state, unsigned field)return v + offset; } +__always_inline +static u64 get_aligned_field(const struct bkey_format_processed *f, +const struct bkey_packed *in, +unsigned field_idx) +{ +u64 v =get_unaligned ((u64 *) (((u8 *) in->_data) + f->offset[field_idx])); + + v >>= f->shift[field_idx]; + v &= f->mask[field_idx]; + +return v +le64_to_cpu (f->f.field_offset[field_idx]); +} +__always_inline static bool set_inc_field(struct pack_state *state, unsigned field, u64 v) {
@@ -269,45 +310,57 @@ bool bch2_bkey_transform(const struct bkey_format *out_f,return true; } -struct bkey __bch2_bkey_unpack_key (const struct bkey_format_processed *format_p, +struct bkey __bch2_bkey_unpack_key (const struct bkey_format_processed *format,const struct bkey_packed *in) { -const struct bkey_format *format = &format_p->f; -struct unpack_state state =unpack_state_init (format, in);struct bkey out; -EBUG_ON (format->nr_fields !=BKEY_NR_FIELDS ); -EBUG_ON (in->u64s < format->key_u64s); +EBUG_ON (format->f.nr_fields !=BKEY_NR_FIELDS ); +EBUG_ON (in->u64s < format->f.key_u64s);EBUG_ON (in->format !=KEY_FORMAT_LOCAL_BTREE ); -EBUG_ON (in->u64s - format->key_u64s + BKEY_U64s >U8_MAX ); +EBUG_ON (in->u64s - format->f.key_u64s + BKEY_U64s >U8_MAX ); - out.u64s = BKEY_U64s + in->u64s - format->key_u64s; + out.u64s = BKEY_U64s + in->u64s - format->f.key_u64s; out.format =KEY_FORMAT_CURRENT ; out.needs_whiteout = in->needs_whiteout; out.type = in->type; out.pad[0 ] =0 ; +if (likely (format->aligned)) { +#define x (id, field) out.field = get_aligned_field(format, in, id); + bkey_fields() +#undef x + }else { +struct unpack_state state =unpack_state_init (&format->f, in); +#define x (id, field) out.field = get_inc_field(&state, id); - bkey_fields() + bkey_fields()#undef x + }return out; } -struct bpos __bkey_unpack_pos (const struct bkey_format_processed *format_p, +struct bpos __bkey_unpack_pos (const struct bkey_format_processed *format,const struct bkey_packed *in) { -const struct bkey_format *format = &format_p->f; -struct unpack_state state =unpack_state_init (format, in);struct bpos out; -EBUG_ON (format->nr_fields !=BKEY_NR_FIELDS ); -EBUG_ON (in->u64s < format->key_u64s); +EBUG_ON (format->f.nr_fields !=BKEY_NR_FIELDS ); +EBUG_ON (in->u64s < format->f.key_u64s);EBUG_ON (in->format !=KEY_FORMAT_LOCAL_BTREE ); - out.inode =get_inc_field (&state,BKEY_FIELD_INODE ); - out.offset =get_inc_field (&state,BKEY_FIELD_OFFSET ); - out.snapshot =get_inc_field (&state,BKEY_FIELD_SNAPSHOT ); +if (likely (format->aligned)) { + out.inode =get_aligned_field (format, in,BKEY_FIELD_INODE ); + out.offset =get_aligned_field (format, in,BKEY_FIELD_OFFSET ); + out.snapshot =get_aligned_field (format, in,BKEY_FIELD_SNAPSHOT ); + }else { +struct unpack_state state =unpack_state_init (&format->f, in); + + out.inode =get_inc_field (&state,BKEY_FIELD_INODE ); + out.offset =get_inc_field (&state,BKEY_FIELD_OFFSET ); + out.snapshot =get_inc_field (&state,BKEY_FIELD_SNAPSHOT ); + }return out; }
fs/bcachefs/btree_types.h +4 -0
@@ -70,6 +70,10 @@ struct btree_bkey_cached_common {struct bkey_format_processed {struct bkey_format f; +bool aligned; +u8 offset[6 ]; +u8 shift[6 ]; +u64 mask[6 ]; };struct btree {
fs/bcachefs/btree_update_interior.h +5 -1
@@ -181,7 +181,11 @@ static inline void btree_node_reset_sib_u64s(struct btree *b)static inline void *btree_data_end (struct bch_fs *c,struct btree *b) { -return (void *) b->data +btree_bytes (c); +/* +* __bch2_bkey_unpack_key() may read up to 8 bytes past the end of the +* input buffer: +*/ +return (void *) b->data +btree_bytes (c) -8 ; } static inline struct bkey_packed *unwritten_whiteouts_start(struct bch_fs *c,
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 21 lines · Kent Overstreet
Quoted 20 lines · Eric Biggers
First, I wanted to mention that decoding of variable-length fields has been extensively studied for decompression algorithms, e.g. for Huffman decoding. And it turns out that it can be done branchlessly. The basic idea is that you have a branchless refill step that looks like the following:
#define REFILL_BITS_BRANCHLESS() \ bitbuf |= get_unaligned_u64(p) << bitsleft; \ p += 7 - ((bitsleft >> 3) & 0x7); \ bitsleft |= 56;That branchlessly ensures that 'bitbuf' contains '56 <= bitsleft <= 63' bits. Then, the needed number of bits can be removed and returned:
#define READ_BITS(n) \ REFILL_BITS_BRANCHLESS(); \ tmp = bitbuf & (((u64)1 << (n)) - 1); \ bitbuf >>= (n); \ bitsleft -= (n); \ tmp
Quoted 4 lines
Quoted 1 line
If you're interested, I can give you some references about the above method.
I might be interested in those references, new bit tricks and integer encodings are always fun :)
There are some good blog posts by Fabian Giese:
* https://fgiesen.wordpress.com/2018/02/19/reading-bits-in-far-too-many-ways-part-1/
* https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
* https://fgiesen.wordpress.com/2018/09/27/reading-bits-in-far-too-many-ways-part-3/
And the examples I gave above are basically what I use in libdeflate:
https://github.com/ebiggers/libdeflate/blob/master/lib/deflate_decompress.c
fs/bcachefs/bkey.c
Quoted 51 lines
Quoted 23 lines
But, I really just wanted to mention it for completeness, since I think you'd actually want to go in a slightly different direction, since (a) you have all the field widths available from the beginning, as opposed to being interleaved into the bitstream itself (as is the case in Huffman decoding for example), so you're not limited to serialized decoding of each field, (b) your fields are up to 96 bits, and (c) you've selected a bitstream convention that seems to make it such that your stream *must* be read in aligned units of u64, so I don't think something like
REFILL_BITS_BRANCHLESS()could work for you anyway.What I would suggest instead is preprocessing the list of 6 field lengths to create some information that can be used to extract all 6 fields branchlessly with no dependencies between different fields. (And you clearly *can* add a preprocessing step, as you already have one -- the dynamic code generator.)
So, something like the following:
const struct field_info *info = &format->fields[0]; field0 = (in->u64s[info->word_idx] >> info->shift1) & info->mask; field0 |= in->u64s[info->word_idx - 1] >> info->shift2;... but with the code for all 6 fields interleaved.
Quoted 7 lines
On modern CPUs, I think that would be faster than your current C code.
You could do better by creating variants that are specialized for specific common sets of parameters. During "preprocessing", you would select a variant and set an enum accordingly. During decoding, you would switch on that enum and call the appropriate variant. (This could also be done with a function pointer, of course, but indirect calls are slow these days...)
testing random btree updates:
dynamically generated unpack:
rand_insert: 20.0 MiB with 1 threads in 33 sec, 1609 nsec per iter, 607 KiB per secold C unpack:
rand_insert: 20.0 MiB with 1 threads in 35 sec, 1672 nsec per iter, 584 KiB per secthe Eric Biggers special:
rand_insert: 20.0 MiB with 1 threads in 35 sec, 1676 nsec per iter, 583 KiB per secTested two versions of your approach, one without a shift value, one where we use a shift value to try to avoid unaligned access - second was perhaps 1% faster
so it's not looking good. This benchmark doesn't even hit on
unpack_key()quite as much as I thought, so the difference is significant.fs/bcachefs/bkey.c +0
-0
I don't know what this patch applies to, so I can't properly review it.
I suggest checking the assembly and making sure it is what is expected.
In general, for this type of thing it's also helpful to put together a userspace micro-benchmark program so that it's very fast to evaluate different options. Building and booting a kernel and doing some I/O benchmark on a bcachefs sounds much more time consuming and less precise.
Quoted 25 lines
fs/bcachefs/bkey.c +7
-7-struct bkey __bch2_bkey_unpack_key (const struct bkey_format_processed *format_p, +struct bkey __bch2_bkey_unpack_key (const struct bkey_format_processed *format,const struct bkey_packed *in) { -const struct bkey_format *format = &format_p->f; -struct unpack_state state =unpack_state_init (format, in);struct bkey out; -EBUG_ON (format->nr_fields !=BKEY_NR_FIELDS ); -EBUG_ON (in->u64s < format->key_u64s); +EBUG_ON (format->f.nr_fields !=BKEY_NR_FIELDS ); +EBUG_ON (in->u64s < format->f.key_u64s);EBUG_ON (in->format !=KEY_FORMAT_LOCAL_BTREE ); -EBUG_ON (in->u64s - format->key_u64s + BKEY_U64s >U8_MAX ); +EBUG_ON (in->u64s - format->f.key_u64s + BKEY_U64s >U8_MAX ); - out.u64s = BKEY_U64s + in->u64s - format->key_u64s; + out.u64s = BKEY_U64s + in->u64s - format->f.key_u64s; out.format =KEY_FORMAT_CURRENT ; out.needs_whiteout = in->needs_whiteout; out.type = in->type; out.pad[0 ] =0 ; +if (likely (format->aligned)) { +#define x (id, field) out.field = get_aligned_field(format, in, id);
fs/bcachefs/bkey.c bool bch2_bkey_transform(const struct bkey_format *out_f, · line 339
Quoted 10 lines
fs/bcachefs/bkey.c +7
-1+ bkey_fields() +#undef x + }else { +struct unpack_state state =unpack_state_init (&format->f, in); +#define x (id, field) out.field = get_inc_field(&state, id); - bkey_fields() + bkey_fields()#undef x + }
It looks like you didn't change the !aligned case. How often is the 'aligned' case taken?
I think it would also help if the generated assembly had the handling of the fields interleaved. To achieve that, it might be necessary to interleave the C code.
- Eric
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 3 lines · Eric Biggers
I think it would also help if the generated assembly had the handling of the fields interleaved. To achieve that, it might be necessary to interleave the C code.
No, that has negligable effect on performance - as expected, for an out of order processor. < 1% improvement.
It doesn't look like this approach is going to work here. Sadly.
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 9 lines · Kent Overstreet
Quoted 3 lines · Eric Biggers
I think it would also help if the generated assembly had the handling of the fields interleaved. To achieve that, it might be necessary to interleave the C code.
No, that has negligable effect on performance - as expected, for an out of order processor. < 1% improvement.
It doesn't look like this approach is going to work here. Sadly.
I'd be glad to take a look at the code you actually tried. It would be helpful if you actually provided it, instead of just this "I tried it, I'm giving up now" sort of thing.
I was also hoping you'd take the time to split this out into a userspace micro-benchmark program that we could quickly try different approaches on.
BTW, even if people are okay with dynamic code generation (which seems unlikely?), you'll still need a C version for architectures that you haven't implemented the dynamic code generation for.
- Eric
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 6 lines · Eric Biggers
Quoted 5 lines · Kent Overstreet
Quoted 3 lines · Eric Biggers
I think it would also help if the generated assembly had the handling of the fields interleaved. To achieve that, it might be necessary to interleave the C code.
Quoted 8 lines
Quoted 4 lines
No, that has negligable effect on performance - as expected, for an out of order processor. < 1% improvement.
It doesn't look like this approach is going to work here. Sadly.
I'd be glad to take a look at the code you actually tried. It would be helpful if you actually provided it, instead of just this "I tried it, I'm giving up now" sort of thing.
https://evilpiepirate.org/git/bcachefs.git/log/?h=bkey_unpack
Quoted 2 lines
I was also hoping you'd take the time to split this out into a userspace micro-benchmark program that we could quickly try different approaches on.
I don't need to, because I already have this:
https://evilpiepirate.org/git/ktest.git/tree/tests/bcachefs/perf.ktest
Quoted 3 lines
BTW, even if people are okay with dynamic code generation (which seems unlikely?), you'll still need a C version for architectures that you haven't implemented the dynamic code generation for.
Excuse me? There already is a C version, and we've been discussing it. Your approach wasn't any faster than the existing C version.
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 16 lines · Kent Overstreet
Quoted 6 lines · Eric Biggers
Quoted 5 lines · Kent Overstreet
Quoted 3 lines · Eric Biggers
I think it would also help if the generated assembly had the handling of the fields interleaved. To achieve that, it might be necessary to interleave the C code.
Quoted 8 lines
Quoted 4 lines
No, that has negligable effect on performance - as expected, for an out of order processor. < 1% improvement.
It doesn't look like this approach is going to work here. Sadly.
I'd be glad to take a look at the code you actually tried. It would be helpful if you actually provided it, instead of just this "I tried it, I'm giving up now" sort of thing.
Quoted 7 lines
https://evilpiepirate.org/git/bcachefs.git/log/?h=bkey_unpack
Quoted 2 lines
I was also hoping you'd take the time to split this out into a userspace micro-benchmark program that we could quickly try different approaches on.
I don't need to, because I already have this:
https://evilpiepirate.org/git/ktest.git/tree/tests/bcachefs/perf.ktest
Sure, given that this is an optimization problem with a very small scope (decoding 6 fields from a bitstream), I was hoping for something easier and faster to iterate on than setting up a full kernel + bcachefs test environment and reverse engineering 500 lines of shell script. But sure, I can look into that when I have a chance.
Quoted 1 line
Your approach wasn't any faster than the existing C version.
Well, it's your implementation of what you thought was "my approach". It doesn't quite match what I had suggested. As I mentioned in my last email, it's also unclear that your new code is ever actually executed, since you made it conditional on all fields being byte-aligned...
- Eric
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 5 lines · Eric Biggers
Sure, given that this is an optimization problem with a very small scope (decoding 6 fields from a bitstream), I was hoping for something easier and faster to iterate on than setting up a full kernel + bcachefs test environment and reverse engineering 500 lines of shell script. But sure, I can look into that when I have a chance.
If you were actually wanting to help, that repository is the tool I use for kernel development and testing - it's got documentation.
It builds a kernel, boots a VM and runs a test in about 15 seconds, no need for lifting that code out to userspace.
Quoted 6 lines
Quoted 1 line
Your approach wasn't any faster than the existing C version.
Well, it's your implementation of what you thought was "my approach". It doesn't quite match what I had suggested. As I mentioned in my last email, it's also unclear that your new code is ever actually executed, since you made it conditional on all fields being byte-aligned...
Eric, I'm not an idiot, that was one of the first things I checked. No unaligned bkey formats were generated in my tests.
The latest iteration of your approach that I looked at compiled to ~250 bytes of code, vs. ~50 bytes for the dynamically generated unpack functions. I'm sure it's possible to shave a bit off with some more work, but looking at the generated code it's clear it's not going to be competitive.
Eric Biggers
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 7 lines · Kent Overstreet
Quoted 5 lines · Eric Biggers
Sure, given that this is an optimization problem with a very small scope (decoding 6 fields from a bitstream), I was hoping for something easier and faster to iterate on than setting up a full kernel + bcachefs test environment and reverse engineering 500 lines of shell script. But sure, I can look into that when I have a chance.
Quoted 6 lines
If you were actually wanting to help, that repository is the tool I use for kernel development and testing - it's got documentation.
It builds a kernel, boots a VM and runs a test in about 15 seconds, no need for lifting that code out to userspace.
FYI, I had a go with your test framework today, but I ran into too many problems to really bother with it. In case you want to improve it, these are the problems I ran into (so far). The command I was trying to run, after having run './root_image create' as root as the README says to do, was 'build-test-kernel run -I ~/src/ktest/tests/bcachefs/perf.ktest':
- Error due to ~/src/ktest/tests/bcachefs/bcachefs-tools not existing. Worked around by cloning the bcachefs-tools repo to this location. (Note, it's not a git submodule, so updating the git submodules didn't help.)
- Error due to "Root image not found". Worked around by recursively chown'ing /var/lib/ktest from root to my user. (Note, the README says to run 'root_image create' as root, which results in root ownership.)
- Error due to "cannot set up guest memory 'pc.ram': Cannot allocate memory". Worked around by editing tests/bcachefs/perf.ktest to change config-mem from 32G to 16G. (I have 32G memory total on this computer.)
- Error due to "failed to open /dev/vfio/10: No such file or directory". Enabling CONFIG_VFIO and CONFIG_VFIO_PCI in my host kernel didn't help. It seems the test is hardcoded to expect PCI passthrough to be set up with a specific device. I'd have expected it to just set up a standard virtual disk.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 5 lines · Eric Biggers
FYI, I had a go with your test framework today, but I ran into too many problems to really bother with it. In case you want to improve it, these are the problems I ran into (so far). The command I was trying to run, after having run './root_image create' as root as the README says to do, was 'build-test-kernel run -I ~/src/ktest/tests/bcachefs/perf.ktest':
Thanks for giving it a shot...
Quoted 3 lines
- Error due to ~/src/ktest/tests/bcachefs/bcachefs-tools not existing. Worked around by cloning the bcachefs-tools repo to this location. (Note, it's not a git submodule, so updating the git submodules didn't help.)
a require-git line was missing, fixed that...
Quoted 3 lines
- Error due to "Root image not found". Worked around by recursively chown'ing
/var/lib/ktestfrom root to my user. (Note, the README says to run 'root_imagecreate' as root, which results in root ownership.)
Not sure about this one - root ownership is supposed to be fine because qemu opens the root image read only, we use qemu's block device in-memory snapshot mode. Was it just not readable by your user?
Quoted 3 lines
- Error due to "cannot set up guest memory 'pc.ram': Cannot allocate memory". Worked around by editing tests/bcachefs/perf.ktest to change config-mem from 32G to 16G. (I have 32G memory total on this computer.)
I think 32G is excessive for the tests that actually need to be in this file, dropping that back to 16G.
Quoted 4 lines
- Error due to "failed to open
/dev/vfio/10: No such file or directory". EnablingCONFIG_VFIOandCONFIG_VFIO_PCIin my host kernel didn't help. It seems the test is hardcoded to expect PCI passthrough to be set up with a specific device. I'd have expected it to just set up a standard virtual disk.
Some of the tests in that file do need a fast device, but the tests we're interested in do not - I'll split that up.
I just pushed fixes for everything except the root_image issue if you want to give it another go.
Cheers,
Kent
RE: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetFrom: Kent Overstreet
Quoted 1 line
Sent: 14 May 2023 06:45
...
Quoted 6 lines
dynamically generated unpack:
rand_insert: 20.0 MiB with 1 threads in 33 sec, 1609 nsec per iter, 607 KiB per secold C unpack:
rand_insert: 20.0 MiB with 1 threads in 35 sec, 1672 nsec per iter, 584 KiB per sec
Quoted 6 lines
the Eric Biggers special:
rand_insert: 20.0 MiB with 1 threads in 35 sec, 1676 nsec per iter, 583 KiB per secTested two versions of your approach, one without a shift value, one where we use a shift value to try to avoid unaligned access - second was perhaps 1% faster
You won't notice any effect of avoiding unaligned accesses on x86. I think then get split into 64bit accesses and again on 64 byte boundaries (that is what I see for uncached access to PCIe). The kernel won't be doing >64bit and the 'out of order' pipeline will tend to cover the others (especially since you get 2 reads/clock).
Quoted 3 lines
so it's not looking good. This benchmark doesn't even hit on
unpack_key()quite as much as I thought, so the difference is significant.
Beware: unless you manage to lock the cpu frequency (which is ~impossible on some cpu) timings in nanoseconds are pretty useless.
You can use the performance counter to get accurate cycle times (provided there isn't a cpu switch in the middle of a micro-benchmark).
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
RE: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetFrom: Kent Overstreet
Quoted 1 line
Sent: 09 May 2023 22:29
...
Quoted 5 lines
The background is that bcachefs generates a per btree node unpack function, based on the packed format for that btree node, for unpacking keys within that node. The unpack function is only ~50 bytes, and for locality we want it to be located with the btree node's other in-memory lookup tables so they can be prefetched all at once.
Loading data into the d-cache isn't going to load code into the i-cache.
Indeed you don't want to be mixing code and data in the same cache line - because it just wastes space in the cache.
Looks to me like you could have a few different unpack functions and pick the correct one based on the packed format. Quite likely the code would be just as fast (if longer) when you allow for parallel execution on modern cpu.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Lorenzo StoakesQuoted 10 lines · Lorenzo Stoakes
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Quoted 6 lines
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
I already trolled Kent with this on IRC, but for the parts of bcachefs that want better assembly code than whatever gcc generates from the C source, could you compile code to BPF and then let the BPF JIT engines turn that into machine code for you?
(also distracted by LSFMM)
--D
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 15 lines · Darrick J. Wong
Quoted 14 lines · Lorenzo Stoakes
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
Quoted 7 lines
Quoted 2 lines
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
I already trolled Kent with this on IRC, but for the parts of bcachefs that want better assembly code than whatever gcc generates from the C source, could you compile code to BPF and then let the BPF JIT engines turn that into machine code for you?
It's an intriguing idea, but it'd be a _lot_ of work and this is old code that's never had a single bug - I'm not in a hurry to rewrite it.
And there would still be the issue that we've still got lots of little unpack functions that go with other tables; we can't just burn a full page per unpack function, that would waste way too much memory, and if we put them together then we're stuck writing a whole nother allocator
- nope, and then we're also mucking with the memory layout of the data structures used in the very hottest paths in the filesystem - I'm very wary of introducing performance regressions there.
I think it'd be much more practical to find some way of making vmalloc_exec() more palatable. What are the exact concerns?
Theodore Ts'o
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 3 lines · Kent Overstreet
I think it'd be much more practical to find some way of making
vmalloc_exec()more palatable. What are the exact concerns?
Having a vmalloc_exec() function (whether it is not exported at all, or exported as a GPL symbol) makes it *much* easier for an exploit writer, since it's a super convenient gadget for use with Returned-oriented-programming[1] to create a writeable, executable space that could then be filled with arbitrary code of the exploit author's arbitrary desire.
[1] https://en.wikipedia.org/wiki/Return-oriented_programming
The other thing I'll note from examining the code generator, is that it appears that bcachefs *only* has support for x86_64. This brings me back to the days of my youth when all the world was a Vax[2]. :-)
10. Thou shalt foreswear, renounce, and abjure the vile heresy which claimeth that ``All the world's a VAX'', and have no commerce with the benighted heathens who cling to this barbarous belief, that the days of thy program may be long even though the days of thy current machine be short.
[ This particular heresy bids fair to be replaced by ``All the world's a Sun'' or ``All the world's a 386'' (this latter being a particularly revolting invention of Satan), but the words apply to all such without limitation. Beware, in particular, of the subtle and terrible ``All the world's a 32-bit machine'', which is almost true today but shall cease to be so before thy resume grows too much longer.]
[2] The Ten Commandments for C Programmers (Annotated Edition)
https://www.lysator.liu.se/c/ten-commandments.html
Seriously, does this mean that bcachefs won't work on Arm systems (arm32 or arm64)? Or Risc V systems? Or S/390's? Or Power architectuers? Or Itanium or PA-RISC systems? (OK, I really don't care all that much about those last two. :-)
When people ask me why file systems are so hard to make enterprise ready, I tell them to recall the general advice given to people to write secure, robust systems: (a) avoid premature optimization, (b) avoid fine-grained, multi-threaded programming, as much as possible, because locking bugs are a b*tch, and (c) avoid unnecessary global state as much as possible.
File systems tend to violate all of these precepts: (a) people chase benchmark optimizations to the exclusion of all else, because people have an unhealthy obsession with Phornix benchmark articles, (b) file systems tend to be inherently multi-threaded, with lots of locks, and
(c) file systems are all about managing global state in the form of files, directories, etc.
However, hiding a miniature architecture-specific compiler inside a file system seems to be a rather blatent example of "premature optimization".
- Ted
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 4 lines · Theodore Ts'o
Seriously, does this mean that bcachefs won't work on Arm systems (arm32 or arm64)? Or Risc V systems? Or S/390's? Or Power architectuers? Or Itanium or PA-RISC systems? (OK, I really don't care all that much about those last two. :-)
No :)
My CI servers are arm64 servers. There's a bch2_bkey_unpack_key() written in C, that works on any architecture. But specializing for a particular format is a not-insignificant performance improvement, so writing an arm64 version has been on my todo list.
Quoted 14 lines
When people ask me why file systems are so hard to make enterprise ready, I tell them to recall the general advice given to people to write secure, robust systems: (a) avoid premature optimization, (b) avoid fine-grained, multi-threaded programming, as much as possible, because locking bugs are a b*tch, and (c) avoid unnecessary global state as much as possible.
File systems tend to violate all of these precepts: (a) people chase benchmark optimizations to the exclusion of all else, because people have an unhealthy obsession with Phornix benchmark articles, (b) file systems tend to be inherently multi-threaded, with lots of locks, and
(c) file systems are all about managing global state in the form of files, directories, etc.
Quoted 3 lines
However, hiding a miniature architecture-specific compiler inside a file system seems to be a rather blatent example of "premature optimization".
Ted, this project is _15_ years old.
I'm getting ready to write a full explanation of what this is for and why it's important, I've just been busy with the conference - and I want to write something good, that provides all the context.
I've also been mulling over fallback options, but I don't see any good ones. The unspecialized, C version of unpack has branches (the absolute minimum, I took my time when I was writing that code too); the specialized versions are branchless and _much_ smaller, and the only way to do that specialization is with some form of dynamic codegen.
But I do owe you all a detailed walkthrough of what this is all about, so you'll get it in the next day or so.
Cheers,
Kent
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Lorenzo StoakesQuoted 10 lines · Lorenzo Stoakes
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Quoted 6 lines
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
Just to be abundantly clear, my original ack was a mistake (I overlooked the _exporting_ of the function being as significant as it is and assumed in an LSF/MM haze that it was simply a refactoring of _already available_ functionality rather than newly providing a means to allocate directly executable kernel memory).
Exporting this is horrible for the numerous reasons expounded on in this thread, we need a different solution.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Le 13/05/2023 à 15:25, Lorenzo Stoakes a écrit :
Quoted 24 lines · Lorenzo Stoakes
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Kent Overstreet
Quoted 4 lines
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
No, we will never add back a way for random code allocating executable memory in kernel space.
Quoted 6 lines
Yeah I think I glossed over this aspect a bit as it looks ostensibly like simply reinstating a helper function because the code is now used in more than one place (at lsf/mm so a little distracted :)
But it being exported is a problem. Perhaps there's another way of acheving the same aim without having to do so?
Just to be abundantly clear, my original ack was a mistake (I overlooked the _exporting_ of the function being as significant as it is and assumed in an LSF/MM haze that it was simply a refactoring of
_alreadyavailable_functionality rather than newly providing a means to allocate directly executable kernel memory).
Quoted 5 lines
Exporting this is horrible for the numerous reasons expounded on in this thread, we need a different solution.
Nacked-by Lorenzo Stoakes <[email protected]>
I addition to that, I still don't understand why you bring back vmalloc_exec() instead of using module_alloc().
As reminded in a previous response, some architectures like powerpc/32s cannot allocate exec memory in vmalloc space. On powerpc this is because exec protection is performed on 256Mbytes segments and vmalloc space is flagged non-exec. Some other architectures have a constraint on distance between kernel core text and other text.
Today you have for instance kprobes in the kernel that need dynamic exec memory. It uses module_alloc() to get it. On some architectures you also have ftrace that gets some exec memory with module_alloc().
So, I still don't understand why you cannot use module_alloc() and need vmalloc_exec() instead.
Thanks
Christophe
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 9 lines · Christophe Leroy
I addition to that, I still don't understand why you bring back
vmalloc_exec()instead of usingmodule_alloc().As reminded in a previous response, some architectures like powerpc/32s cannot allocate exec memory in vmalloc space. On powerpc this is because exec protection is performed on 256Mbytes segments and vmalloc space is flagged non-exec. Some other architectures have a constraint on distance between kernel core text and other text.
Quoted 6 lines
Today you have for instance kprobes in the kernel that need dynamic exec memory. It uses
module_alloc()to get it. On some architectures you also have ftrace that gets some exec memory withmodule_alloc().So, I still don't understand why you cannot use
module_alloc()and needvmalloc_exec()instead.
Because I didn't know about it :)
Looks like that is indeed the appropriate interface (if a bit poorly named), I'll switch to using that, thanks.
It'll still need to be exported, but it looks like the W|X attribute discussion is not really germane here since it's what other in kernel users are using, and there's nothing particularly special about how bcachefs is using it compared to them.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Le 15/05/2023 à 01:43, Kent Overstreet a écrit :
Quoted 19 lines · Christophe Leroy
Quoted 9 lines
I addition to that, I still don't understand why you bring back
vmalloc_exec()instead of usingmodule_alloc().As reminded in a previous response, some architectures like powerpc/32s cannot allocate exec memory in vmalloc space. On powerpc this is because exec protection is performed on 256Mbytes segments and vmalloc space is flagged non-exec. Some other architectures have a constraint on distance between kernel core text and other text.
Quoted 6 lines
Today you have for instance kprobes in the kernel that need dynamic exec memory. It uses
module_alloc()to get it. On some architectures you also have ftrace that gets some exec memory withmodule_alloc().So, I still don't understand why you cannot use
module_alloc()and needvmalloc_exec()instead.Because I didn't know about it :)
Quoted 7 lines
Looks like that is indeed the appropriate interface (if a bit poorly named), I'll switch to using that, thanks.
It'll still need to be exported, but it looks like the W|X attribute discussion is not really germane here since it's what other in kernel users are using, and there's nothing particularly special about how bcachefs is using it compared to them.
The W|X subject is applicable.
If you look into powerpc's module_alloc(), you'll see that when CONFIG_STRICT_MODULE_RWX is selected, module_alloc() allocate PAGE_KERNEL memory. It is then up to the consumer to change it to RO-X.
See for instance in arch/powerpc/kernel/kprobes.c:
void *alloc_insn_page(void)
{
void *page;
page = module_alloc(PAGE_SIZE);
if (!page)
return NULL;
if (strict_module_rwx_enabled())
set_memory_rox((unsigned long)page, 1);
return page;
}Christophe
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 39 lines · Christophe Leroy
Le 15/05/2023 à 01:43, Kent Overstreet a écrit :
Quoted 19 lines · Christophe Leroy
Quoted 9 lines
I addition to that, I still don't understand why you bring back
vmalloc_exec()instead of usingmodule_alloc().As reminded in a previous response, some architectures like powerpc/32s cannot allocate exec memory in vmalloc space. On powerpc this is because exec protection is performed on 256Mbytes segments and vmalloc space is flagged non-exec. Some other architectures have a constraint on distance between kernel core text and other text.
Quoted 6 lines
Today you have for instance kprobes in the kernel that need dynamic exec memory. It uses
module_alloc()to get it. On some architectures you also have ftrace that gets some exec memory withmodule_alloc().So, I still don't understand why you cannot use
module_alloc()and needvmalloc_exec()instead.Because I didn't know about it :)
Quoted 7 lines
Looks like that is indeed the appropriate interface (if a bit poorly named), I'll switch to using that, thanks.
It'll still need to be exported, but it looks like the W|X attribute discussion is not really germane here since it's what other in kernel users are using, and there's nothing particularly special about how bcachefs is using it compared to them.
The W|X subject is applicable.
If you look into powerpc's
module_alloc(), you'll see that whenCONFIG_STRICT_MODULE_RWXis selected,module_alloc()allocatePAGE_KERNELmemory. It is then up to the consumer to change it to RO-X.See for instance in
arch/powerpc/kernel/kprobes.c:void
*alloc_insn_page(void)
Quoted 12 lines
{
void *page;
page = module_alloc(PAGE_SIZE);
if (!page)
return NULL;
if (strict_module_rwx_enabled())
set_memory_rox((unsigned long)page, 1);
return page;
}Yeah.
I'm looking at the bpf code now.
<RANT MODE, YOU ARE WARNED>
Can I just say, for the record - god damn this situation is starting to piss me off? This really nicely encapsulates everything I hate about kernel development processes and culture and the fscking messes that get foisted upon people as a result.
All I'm trying to do is write a fucking filesystem here people, I've got enough on my plate. Dealing with the fallout of a kernel interface going away without a proper replacement was NOT WHAT I FUCKING HAD IN MIND?
5% performance regression without this. That's just not acceptable, I can't produce a filesystem that people will in the end want to use by leaving performance on the table, it's death of a thousand cuts if I take that attitude. Every 1% needs to be accounted for, a 5% performance regression is flat out not going to happen.
And the real icing on this motherfucking turd sandwich of a cake, is that I'm not the first person to have to solve this particular technical problem.
BPF has the code I need.
But, in true kernel fashion, did they recognize that this was a subproblem they could write as a library, both making their code more modular and easier to understand, as well as, oh I don't know, not leaving a giant steaming turd for the next person to come along?
Nope.
I'd be embarassed if I was responsible for this.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetLe 09/05/2023 à 18:56, Kent Overstreet a écrit :
Quoted 90 lines
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Signed-off-by Kent Overstreet <[email protected]>Cc Andrew Morton <[email protected]>Cc Uladzislau Rezki <[email protected]>Cc Christoph Hellwig <[email protected]>--- include/linux/vmalloc.h | 1 + kernel/module/main.c | 4 +--- mm/nommu.c | 18 ++++++++++++++++++ mm/vmalloc.c | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-)include/linux/vmalloc.h +1
-0@@ -145,6 +145,7 @@ extern void *vzalloc(unsigned long size) __alloc_size(1);extern void *vmalloc_user (unsigned long size)__alloc_size(1) ;extern void *vmalloc_node (unsigned long size,int node)__alloc_size(1) ;extern void *vzalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vmalloc_exec (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;extern void *vmalloc_32 (unsigned long size)__alloc_size(1) ;extern void *vmalloc_32_user (unsigned long size)__alloc_size(1) ;extern void *__vmalloc (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;kernel/module/main.c +1
-3@@ -1607,9 +1607,7 @@ static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbgvoid *__weak module_alloc (unsigned long size) { -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); +return vmalloc_exec (size,GFP_KERNEL ); }bool __weak module_init_section (const char *name)mm/nommu.c +18
-0@@ -280,6 +280,24 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +*/ + +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc (size, gfp_mask); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +/** * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) * @size: allocation size mm/vmalloc.c +9
-0@@ -3401,6 +3401,27 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags
mm/vmalloc.c void *vzalloc_node(unsigned long size, int node) · line 3422
Quoted 10 lines
mm/vmalloc.c +10
-0+* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +}
That cannot work. The VMALLOC space is mapped non-exec on powerpc/32. You have to allocate between MODULES_VADDR and MODULES_END if you want something executable so you must use module_alloc() see
https://elixir.bootlin.com/linux/v6.4-rc1/source/arch/powerpc/kernel/module.c#L108
Quoted 5 lines
mm/vmalloc.c +2
-0+EXPORT_SYMBOL_GPL (vmalloc_exec); +#if defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA32 )#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)#elif defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA )
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetQuoted 10 lines · Kent Overstreet
mm/vmalloc.c +10
-0+/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead.
mm/vmalloc.c void *vzalloc_node(unsigned long size, int node) · line 3423
Quoted 10 lines
mm/vmalloc.c +10
-0+* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec);
Uh W+X memory reagions.
The 90s called, they want their shellcode back.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 16 lines · Johannes Thumshirn
Quoted 15 lines · Kent Overstreet
mm/vmalloc.c +15
-0+/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{
Quoted 8 lines
mm/vmalloc.c void *vzalloc_node(unsigned long size, int node) · line 3423
Quoted 5 lines
mm/vmalloc.c +5
-0+return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec);Uh W+X memory reagions.
The 90s called, they want their shellcode back.
Just to clarify: the kernel must never create W+X memory regions. So, no, do not reintroduce vmalloc_exec().
Dynamic code areas need to be constructed in a non-executable memory, then switched to read-only and verified to still be what was expected, and only then made executable.
Signature · 1 line
Kees Cook
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 26 lines · Kees Cook
Quoted 16 lines · Johannes Thumshirn
Quoted 15 lines · Kent Overstreet
mm/vmalloc.c +15
-0+/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{Quoted 8 lines
mm/vmalloc.c void *vzalloc_node(unsigned long size, int node) · line 3423
Quoted 5 lines
mm/vmalloc.c +5
-0+return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec);Uh W+X memory reagions.
The 90s called, they want their shellcode back.
Quoted 6 lines
Just to clarify: the kernel must never create W+X memory regions. So, no, do not reintroduce
vmalloc_exec().Dynamic code areas need to be constructed in a non-executable memory, then switched to read-only and verified to still be what was expected, and only then made executable.
So if we're opening this up to the topic if what an acceptible API would look like - how hard is this requirement?
The reason is that the functions we're constructing are only ~50 bytes, so we don't want to be burning a full page per function (particularly for the 64kb page architectures...)
If we were to build an allocator for sub-page dynamically constructed code, we'd have to flip the whole page to W+X while copying in the new function. But, we could construct it in non executable memory and then hand it off to this new allocator to do the copy, which would also do the page permission flipping.
It seem like this is something BPF might want eventually too, depending on average BPF program size...
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 34 lines · Kent Overstreet
Quoted 26 lines · Kees Cook
Quoted 16 lines · Johannes Thumshirn
Quoted 15 lines · Kent Overstreet
mm/vmalloc.c +15
-0+/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{Quoted 8 lines
mm/vmalloc.c void *vzalloc_node(unsigned long size, int node) · line 3423
Quoted 5 lines
mm/vmalloc.c +5
-0+return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec);Uh W+X memory reagions.
The 90s called, they want their shellcode back.Quoted 6 lines
Just to clarify: the kernel must never create W+X memory regions. So, no, do not reintroduce
vmalloc_exec().Dynamic code areas need to be constructed in a non-executable memory, then switched to read-only and verified to still be what was expected, and only then made executable.
Quoted 6 lines
So if we're opening this up to the topic if what an acceptible API would look like - how hard is this requirement?
The reason is that the functions we're constructing are only ~50 bytes, so we don't want to be burning a full page per function (particularly for the 64kb page architectures...)
For something that small, why not use the text_poke API?
Signature · 1 line
Kees Cook
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?
This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
external api will be:
void *jit_alloc(void *buf, size_t len, gfp_t gfp);
void jit_free(void *buf);
void jit_update(void *buf, void *new_code, size_t len); /* update an existing allocation */
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 11 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
Quoted 7 lines
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
How does that avoid the problem of being able to construct an arbitrary gadget that somebody else will then execute? IOW, what bpf has done seems like it's working around & undoing the security improvements.
I suppose it's an improvement that only the executable address is passed back to the caller, and not the writable address.
Quoted 6 lines
external api will be:
void *jit_alloc(void *buf,
size_tlen,gfp_tgfp);
voidjit_free(void *buf);
voidjit_update(void *buf, void *new_code, size_t len); /* update an existing allocation */
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 20 lines · Matthew Wilcox
Quoted 11 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
Quoted 7 lines
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
Quoted 6 lines
How does that avoid the problem of being able to construct an arbitrary gadget that somebody else will then execute? IOW, what bpf has done seems like it's working around & undoing the security improvements.
I suppose it's an improvement that only the executable address is passed back to the caller, and not the writable address.
That's my thinking; grepping around finds several uses of module_alloc() that are all doing different variations on the page permissions dance. Let's just do it once and do it right...
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Matthew WilcoxQuoted 20 lines · Matthew Wilcox
Quoted 11 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
Quoted 7 lines
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
Quoted 6 lines
How does that avoid the problem of being able to construct an arbitrary gadget that somebody else will then execute? IOW, what bpf has done seems like it's working around & undoing the security improvements.
I suppose it's an improvement that only the executable address is passed back to the caller, and not the writable address.
Ok, here's what I came up with. Have not tested all corner cases, still need to write docs - but I think this gives us a nicer interface than what bpf/kprobes/etc. have been doing, and it does the sub-page sized allocations I need.
With an additional tweak to module_alloc() (not done in this patch yet) we avoid ever mapping in pages both writeable and executable:
-->--
From 6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976 Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocator
This provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak module_alloc()), and it also supports sub-page sized allocations.
include/linux/jitalloc.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_JITALLOC_H +#define _LINUX_JITALLOC_H + +void jit_update (void *buf,void *new_buf,size_t len); +void jit_free (void *buf); +void *jit_alloc (void *buf,size_t len); + +#endif /* _LINUX_JITALLOC_H */
mm/Kconfig +3 -0
@@ -1202,6 +1202,9 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config JITALLOC + bool + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_JITALLOC) += jitalloc.o
mm/jitalloc.c new file +187 -0
187 lines not on this page · raw message
Mike Rapoport
reviewerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 43 lines · Kent Overstreet
Quoted 20 lines · Matthew Wilcox
Quoted 11 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
Quoted 7 lines
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
Quoted 6 lines
How does that avoid the problem of being able to construct an arbitrary gadget that somebody else will then execute? IOW, what bpf has done seems like it's working around & undoing the security improvements.
I suppose it's an improvement that only the executable address is passed back to the caller, and not the writable address.
Ok, here's what I came up with. Have not tested all corner cases, still need to write docs - but I think this gives us a nicer interface than what bpf/kprobes/etc. have been doing, and it does the sub-page sized allocations I need.
With an additional tweak to
module_alloc()(not done in this patch yet) we avoid ever mapping in pages both writeable and executable:-->--
From
6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocator
Quoted 6 lines
This provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak
module_alloc()), and it also supports sub-page sized allocations.
This looks like yet another workaround for that module_alloc() was not designed to handle permission changes. Rather than create more and more wrappers for module_alloc() we need to have core API for code allocation, apparently on top of vmalloc, and then use that API for modules, bpf, tracing and whatnot.
There was quite lengthy discussion about how to handle code allocations here:
https://lore.kernel.org/linux-mm/[email protected]/
and Song is already working on improvements for module_alloc(), e.g. see commit ac3b43283923 ("module: replace module_layout with module_memory")
Another thing, the code below will not even compile on !x86.
Quoted 234 lines
Signed-off-by Kent Overstreet <[email protected]>include/linux/jitalloc.h new file +9
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_JITALLOC_H +#define _LINUX_JITALLOC_H + +void jit_update (void *buf,void *new_buf,size_t len); +void jit_free (void *buf); +void *jit_alloc (void *buf,size_t len); + +#endif /* _LINUX_JITALLOC_H */ mm/Kconfig +3
-0@@ -1202,6 +1202,9 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config JITALLOC + bool + source "mm/damon/Kconfig" endmenumm/Makefile +1
-0@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_JITALLOC) += jitalloc.omm/jitalloc.c new file +187
-0@@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/gfp.h> +#include <linux/highmem.h> +#include <linux/jitalloc.h> +#include <linux/mm.h> +#include <linux/moduleloader.h> +#include <linux/mutex.h> +#include <linux/set_memory.h> +#include <linux/vmalloc.h> + +#include <asm/text-patching.h> + +static DEFINE_MUTEX (jit_alloc_lock); + +struct jit_cache { +unsigned obj_size_bits; +unsigned objs_per_slab; +struct list_head partial; +}; + +#define JITALLOC_MIN_SIZE 16 +#define NR_JIT_CACHES ilog2(PAGE_SIZE / JITALLOC_MIN_SIZE) + +static struct jit_cache jit_caches[NR_JIT_CACHES ]; + +struct jit_slab { +unsigned long __page_flags; + +struct jit_cache *cache; + void *executably_mapped;; +unsigned long *objs_allocated;/* bitmap of free objects */ +struct list_head list; +}; + +#define folio_jit_slab (folio) (_Generic((folio), \ + const struct folio *: (const struct jit_slab *)(folio), \ + struct folio *: (struct jit_slab *)(folio))) + +#define jit_slab_folio (s) (_Generic((s), \ + const struct jit_slab *: (const struct folio *)s, \ + struct jit_slab *: (struct folio *)s)) + +static struct jit_slab *jit_slab_alloc (struct jit_cache *cache) +{ +void *executably_mapped =module_alloc (PAGE_SIZE ); +struct page *page; +struct folio *folio; +struct jit_slab *slab; +unsigned long *objs_allocated; + +if (!executably_mapped) +return NULL ; + + objs_allocated =kcalloc (BITS_TO_LONGS (cache->objs_per_slab),sizeof (unsigned long ),GFP_KERNEL ); +if (!objs_allocated ) { +vfree (executably_mapped); +return NULL ; + } + +set_vm_flush_reset_perms (executably_mapped); +set_memory_rox ((unsigned long ) executably_mapped,1 ); + + page =vmalloc_to_page (executably_mapped); + folio =page_folio (page); + +__folio_set_slab (folio); + slab =folio_jit_slab (folio); + slab->cache = cache; + slab->executably_mapped = executably_mapped; + slab->objs_allocated = objs_allocated; +INIT_LIST_HEAD (&slab->list); + +return slab; +} + +static void *jit_cache_alloc (void *buf,size_t len,struct jit_cache *cache) +{ +struct jit_slab *s = + list_first_entry_or_null(&cache->partial, struct jit_slab, list) ?: +jit_slab_alloc (cache); +unsigned obj_idx, nr_allocated; + +if (!s) +return NULL ; + + obj_idx =find_first_zero_bit (s->objs_allocated, cache->objs_per_slab); + +BUG_ON (obj_idx >= cache->objs_per_slab); +__set_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab); + +if (nr_allocated == s->cache->objs_per_slab) { +list_del_init (&s->list); + }else if (nr_allocated ==1 ) { +list_del (&s->list); +list_add (&s->list, &s->cache->partial); + } + +return s->executably_mapped + (obj_idx << cache->obj_size_bits); +} + +void jit_update (void *buf,void *new_buf,size_t len) +{ +text_poke_copy (buf, new_buf, len); +} +EXPORT_SYMBOL_GPL (jit_update); + +void jit_free (void *buf) +{ +struct page *page; +struct folio *folio; +struct jit_slab *s; +unsigned obj_idx, nr_allocated; +size_t offset; + +if (!buf) +return ; + + page =vmalloc_to_page (buf); + folio =page_folio (page); + offset =offset_in_folio (folio, buf); + +if (!folio_test_slab (folio)) { +vfree (buf); +return ; + } + + s =folio_jit_slab (folio); + +mutex_lock (&jit_alloc_lock); + obj_idx = offset >> s->cache->obj_size_bits; + +__clear_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab); + +if (nr_allocated ==0 ) { +list_del (&s->list); +kfree (s->objs_allocated); +folio_put (folio); + }else if (nr_allocated +1 == s->cache->objs_per_slab) { +list_del (&s->list); +list_add (&s->list, &s->cache->partial); + } + +mutex_unlock (&jit_alloc_lock); +} +EXPORT_SYMBOL_GPL (jit_free); + +void *jit_alloc (void *buf,size_t len) +{ +unsigned jit_cache_idx =ilog2 (roundup_pow_of_two (len) /16 ); +void *p; + +if (jit_cache_idx <NR_JIT_CACHES ) { +mutex_lock (&jit_alloc_lock); + p =jit_cache_alloc (buf, len, &jit_caches[jit_cache_idx]); +mutex_unlock (&jit_alloc_lock); + }else { + p =module_alloc (len); +if (p) { +set_vm_flush_reset_perms (p); +set_memory_rox ((unsigned long ) p,DIV_ROUND_UP (len,PAGE_SIZE )); + } + } + +if (p && buf) +jit_update (p, buf, len); + +return p; +} +EXPORT_SYMBOL_GPL (jit_alloc); + +static int __init jit_alloc_init (void ) +{ +for (unsigned i =0 ; i <ARRAY_SIZE (jit_caches); i++) { + jit_caches[i].obj_size_bits =ilog2 (JITALLOC_MIN_SIZE ) + i; + jit_caches[i].objs_per_slab =PAGE_SIZE >> jit_caches[i].obj_size_bits; + +INIT_LIST_HEAD (&jit_caches[i].partial); + } + +return 0 ; +} +core_initcall (jit_alloc_init);
Signature · 2 lines
Sincerely yours, Mike.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 57 lines · Mike Rapoport
Quoted 43 lines · Kent Overstreet
Quoted 20 lines · Matthew Wilcox
Quoted 11 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
Quoted 7 lines
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
Quoted 6 lines
How does that avoid the problem of being able to construct an arbitrary gadget that somebody else will then execute? IOW, what bpf has done seems like it's working around & undoing the security improvements.
I suppose it's an improvement that only the executable address is passed back to the caller, and not the writable address.
Ok, here's what I came up with. Have not tested all corner cases, still need to write docs - but I think this gives us a nicer interface than what bpf/kprobes/etc. have been doing, and it does the sub-page sized allocations I need.
With an additional tweak to
module_alloc()(not done in this patch yet) we avoid ever mapping in pages both writeable and executable:-->--
From
6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocatorQuoted 6 lines
This provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak
module_alloc()), and it also supports sub-page sized allocations.This looks like yet another workaround for that
module_alloc()was not designed to handle permission changes. Rather than create more and more wrappers formodule_alloc()we need to have core API for code allocation, apparently on top of vmalloc, and then use that API for modules, bpf, tracing and whatnot.
Quoted 4 lines
There was quite lengthy discussion about how to handle code allocations here:
Thanks for the link!
Added Song to the CC.
Song, I'm looking at your code now - switching to hugepages is great, but I wonder if we might be able to combine our two approaches - with the slab allocator I did, do we have to bother with VMAs at all? And then it gets us sub-page sized allocations.
Quoted 4 lines
and Song is already working on improvements for
module_alloc(), e.g. see commitac3b43283923("module: replacemodule_layoutwithmodule_memory")Another thing, the code below will not even compile on !x86.
Due to text_poke(), which I see is abstracted better in that patchset.
I'm very curious why text_poke() does tlb flushing at all; it seems like flush_icache_range() is actually what's needed?
text_poke() also only touching up to two pages, without that being documented, is also a footgun...
And I'm really curious why text_poke() is needed at all. Seems like we could just use kmap_local() to create a temporary writeable mapping, except in my testing that got me a RO mapping. Odd.
Mike Rapoport
reviewerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 4 lines · Kent Overstreet
On Wed, May 17, 2023 at 05:04:27PM +0300, Mike Rapoport wrote:
And I'm really curious why
text_poke()is needed at all. Seems like we could just usekmap_local()to create a temporary writeable mapping,
On 64 bit kmap_local_page() is aliased to page_address() and does not map anything. text_poke() is needed to actually create a temporary writable mapping without touching page tables in vmalloc and/or direct map.
Signature · 2 lines
Sincerely yours, Mike.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 9 lines · Mike Rapoport
Quoted 4 lines · Kent Overstreet
On Wed, May 17, 2023 at 05:04:27PM +0300, Mike Rapoport wrote:
And I'm really curious why
text_poke()is needed at all. Seems like we could just usekmap_local()to create a temporary writeable mapping,On 64 bit
kmap_local_page()is aliased topage_address()and does not map anything.text_poke()is needed to actually create a temporary writable mapping without touching page tables in vmalloc and/or direct map.
Duh - thanks!
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetQuoted 6 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
Dynamically generating code is a giant can of worms.
Kees touched on a basic security thing: a linear address mapped W+X is a big no-no. And that's just scratching the surface -- ideally we would have a strong protocol for generating code: the code is generated in some extra-secure context, then it's made immutable and double-checked, then it becomes live. (And we would offer this to userspace, some day.) Just having a different address for the W and X aliases is pretty weak.
(When x86 modifies itself at boot or for static keys, it changes out the page tables temporarily.)
And even beyond security, we have correctness. x86 is a fairly forgiving architecture. If you go back in time about 20 years, modify
some code *at the same linear address at which you intend to execute it*, and jump to it, it works. It may even work if you do it through
an alias (the manual is vague). But it's not 20 years ago, and you have
multiple cores. This does *not* work with multiple CPUs -- you need to serialize on the CPU executing the modified code. On all the but the very newest CPUs, you need to kludge up the serialization, and that's sloooooooooooooow. Very new CPUs have the SERIALIZE instruction, which
is merely sloooooow.
(The manual is terrible. It's clear that a way to do this without serializing must exist, because that's what happens when code is paged in from a user program.)
And remember that x86 is the forgiving architecture. Other architectures have their own rules that may involve all kinds of terrifying cache management. IIRC ARM (32-bit) is really quite nasty in this regard. I've seen some references suggesting that RISC-V has a broken design of its cache management and this is a real mess.
x86 low level stuff on Linux gets away with it because the implementation is conservative and very slow, but it's very rarely invoked.
eBPF gets away with it in ways that probably no one really likes, but also no one expects eBPF to load programs particularly quickly.
You are proposing doing this when a btree node is loaded. You could spend 20 *thousand* cycles, on *each CPU*, the first time you access that node, not to mention the extra branch to decide whether you need to spend those 20k cycles. Or you could use IPIs.
Or you could just not do this. I think you should just remove all this dynamic codegen stuff, at least for now.
Quoted 13 lines
I'm working up a new allocator - a (very simple) slab allocator where you pass a buffer, and it gives you a copy of that buffer mapped executable, but not writeable.
It looks like we'll be able to convert bpf, kprobes, and ftrace trampolines to it; it'll consolidate a fair amount of code (particularly in bpf), and they won't have to burn a full page per allocation anymore.
bpf has a neat trick where it maps the same page in two different locations, one is the executable location and the other is the writeable location - I'm stealing that.
Quoted 5 lines
external api will be:
void *jit_alloc(void *buf,
size_tlen,gfp_tgfp);
voidjit_free(void *buf);
voidjit_update(void *buf, void *new_code, size_t len); /* update an existing allocation */
Based on the above, I regret to inform you that jit_update() will either need to sync all cores via IPI or all cores will need to check whether a sync is needed and do it themselves.
That IPI could be, I dunno, 500k cycles? 1M cycles? Depends on what cores are asleep at the time. (I have some old Sandy Bridge machines where, if you tick all the boxes wrong, you might spend tens of milliseconds doing this due to power savings gone wrong.) Or are you planning to implement a fancy mostly-lockless thing to track which cores actually need the IPI so you can avoid waking up sleeping cores?
Sorry to be a party pooper.
--Andy
P.S. I have given some thought to how to make a JIT API that was actually (somewhat) performant. It's nontrivial, and it would involve having at least phone calls and possibly actual meetings with people who understand the microarchitecture of various CPUs to get all the details hammered out and documented properly.
I don't think it would be efficient for teeny little functions like bcachefs wants, but maybe? That would be even more complex and messy.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 8 lines · Andy Lutomirski
Quoted 6 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
Quoted 7 lines
Dynamically generating code is a giant can of worms.
Kees touched on a basic security thing: a linear address mapped W+X is a big no-no. And that's just scratching the surface -- ideally we would have a strong protocol for generating code: the code is generated in some extra-secure context, then it's made immutable and double-checked, then it becomes live.
"Double checking" arbitrary code is is fantasy. You can't "prove the security" of arbitrary code post compilation.
Rice's theorem states that any nontrivial property of a program is either a direct consequence of the syntax, or is undecidable. It's why programs in statically typed languages are easier to reason about, and it's also why the borrow checker in Rust is a syntactic construct.
You just have to be able to trust the code that generates the code. Just like you have to be able to trust any other code that lives in kernel space.
This is far safer and easier to reason about than what BPF is doing because we're not compiling arbitrary code, the actual codegen part is 200 loc and the input is just a single table.
Quoted 4 lines
(When x86 modifies itself at boot or for static keys, it changes out the page tables temporarily.)
Quoted 10 lines
And even beyond security, we have correctness. x86 is a fairly forgiving architecture. If you go back in time about 20 years, modify some code *at the same linear address at which you intend to execute it*, and jump to it, it works. It may even work if you do it through an alias (the manual is vague). But it's not 20 years ago, and you have multiple cores. This does *not* work with multiple CPUs -- you need to serialize on the CPU executing the modified code. On all the but the very newest CPUs, you need to kludge up the serialization, and that's sloooooooooooooow. Very new CPUs have the SERIALIZE instruction, which is merely sloooooow.
If what you were saying was true, it would be an issue any time we mapped in new executable code for userspace - minor page faults would be stupidly slow.
This code has been running on thousands of machines for years, and the only issues that have come up have been due to the recent introduction of indirect branch tracking. x86 doesn't have such broken caches, and architectures that do have utterly broken caches (because that's what you're describing: you're describing caches that _are not coherent across cores_) are not high on my list of things I care about.
Also, SERIALIZE is a spectre thing. Not relevant here.
Quoted 3 lines
Based on the above, I regret to inform you that
jit_update()will either need to sync all cores via IPI or all cores will need to check whether a sync is needed and do it themselves.
text_poke() doesn't even send IPIs.
I think you've been misled about some things :)
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 17 lines · Kent Overstreet
Quoted 8 lines · Andy Lutomirski
Quoted 6 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
Quoted 7 lines
Dynamically generating code is a giant can of worms.
Kees touched on a basic security thing: a linear address mapped W+X is a big no-no. And that's just scratching the surface -- ideally we would have a strong protocol for generating code: the code is generated in some extra-secure context, then it's made immutable and double-checked, then it becomes live.
Quoted 7 lines
"Double checking" arbitrary code is is fantasy. You can't "prove the security" of arbitrary code post compilation.
Rice's theorem states that any nontrivial property of a program is either a direct consequence of the syntax, or is undecidable. It's why programs in statically typed languages are easier to reason about, and it's also why the borrow checker in Rust is a syntactic construct.
If you want security in some theoretical sense, sure, you're probably right. But that doesn't stop people from double-checking executable code to quite good effect. For example:
(I have no personal experience with this, but I know people who do. It's obviously not perfect, but I think it provides meaningful benefits.)
I'm not saying Linux should do this internally, but it might not be a terrible idea some day.
Quoted 8 lines
You just have to be able to trust the code that generates the code. Just like you have to be able to trust any other code that lives in kernel space.
This is far safer and easier to reason about than what BPF is doing because we're not compiling arbitrary code, the actual codegen part is 200 loc and the input is just a single table.
Great, then propose a model where the codegen operates in an extra-safe protected context. Or pre-generate the most common variants, have them pull their constants from memory instead of immediates, and use that.
Quoted 16 lines
Quoted 5 lines
(When x86 modifies itself at boot or for static keys, it changes out the page tables temporarily.)
Quoted 10 lines
And even beyond security, we have correctness. x86 is a fairly forgiving architecture. If you go back in time about 20 years, modify some code *at the same linear address at which you intend to execute it*, and jump to it, it works. It may even work if you do it through an alias (the manual is vague). But it's not 20 years ago, and you have multiple cores. This does *not* work with multiple CPUs -- you need to serialize on the CPU executing the modified code. On all the but the very newest CPUs, you need to kludge up the serialization, and that's sloooooooooooooow. Very new CPUs have the SERIALIZE instruction, which is merely sloooooow.
Quoted 3 lines
If what you were saying was true, it would be an issue any time we mapped in new executable code for userspace - minor page faults would be stupidly slow.
I literally mentioned this in the email.
I don't know _precisely_ what's going on, but I assume it's that it's impossible (assuming the kernel gets TLB invalidation right) for a CPU to have anything buffered for a linear address that is unmapped, so when it gets mapped, the CPU can't have anything stale in its buffers. (By buffers, I mean any sort of instruction or decoded instruction cache.)
Having *this* conversation is what I was talking about in regard to possible fancy future optimization.
Quoted 7 lines
This code has been running on thousands of machines for years, and the only issues that have come up have been due to the recent introduction of indirect branch tracking. x86 doesn't have such broken caches, and architectures that do have utterly broken caches (because that's what you're describing: you're describing caches that
_arenot coherent acrosscores_) are not high on my list of things I care about.
I care. And a bunch of people who haven't gotten their filesystem corrupted because of a missed serialization.
Quoted 2 lines
Also, SERIALIZE is a spectre thing. Not relevant here.
Nope, try again. SERIALIZE "serializes" in the rather vague sense in the Intel SDM. I don't think it's terribly useful for Spectre.
(Yes, I know what I'm talking about.)
Quoted 6 lines
Quoted 4 lines
Based on the above, I regret to inform you that
jit_update()will either need to sync all cores via IPI or all cores will need to check whether a sync is needed and do it themselves.
text_poke()doesn't even send IPIs.
text_poke() and the associated machinery is unbelievably complicated.
Also, arch/x86/kernel/alternative.c contains:
void text_poke_sync(void)
{
on_each_cpu(do_sync_core, NULL, 1);
}The magic in text_poke() was developed over the course of years, and Intel architects were involved.
(And I think some text_poke() stuff uses RCU, which is another way to sync without IPI. I doubt the performance characteristics are appropriate for bcachefs, but I could be wrong.)
Quoted 2 lines
I think you've been misled about some things :)
I wish.
I like bcachefs. I really don't want to have to put on my maintainer hat here, and I do indeed generally stay in the background. (And I haven't had nearly as much time for this kind of the work in the last couple years as I'd like, sigh.) But I personally have a fairly strict opinion that, if someone (including myself!) wants to merge something that plays clever games that may cause x86 architecture code (especially mm code) to do things it shouldn't in corner cases, even if no one has directly observed that corner case or even knows how to get it to misbehave, then they had better have a very convincing argument that it's safe. No one likes debugging bugs when something that should be coherent becomes incoherent.
So, if you really really want self-modifying code in bcachefs, its correctness needs to be very, very well argued, and it needs to be maintainable. Otherwise I will NAK it. Sorry.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 26 lines · Andy Lutomirski
Quoted 17 lines · Kent Overstreet
Quoted 8 lines · Andy Lutomirski
Quoted 6 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
Quoted 7 lines
Dynamically generating code is a giant can of worms.
Kees touched on a basic security thing: a linear address mapped W+X is a big no-no. And that's just scratching the surface -- ideally we would have a strong protocol for generating code: the code is generated in some extra-secure context, then it's made immutable and double-checked, then it becomes live.
Quoted 7 lines
"Double checking" arbitrary code is is fantasy. You can't "prove the security" of arbitrary code post compilation.
Rice's theorem states that any nontrivial property of a program is either a direct consequence of the syntax, or is undecidable. It's why programs in statically typed languages are easier to reason about, and it's also why the borrow checker in Rust is a syntactic construct.
Quoted 7 lines
If you want security in some theoretical sense, sure, you're probably right. But that doesn't stop people from double-checking executable code to quite good effect. For example:
(I have no personal experience with this, but I know people who do. It's obviously not perfect, but I think it provides meaningful benefits.)
I'm not saying Linux should do this internally, but it might not be a terrible idea some day.
So you want to pull a virus scanner into the kernel.
Quoted 4 lines
Quoted 4 lines
You just have to be able to trust the code that generates the code. Just like you have to be able to trust any other code that lives in kernel space.
Quoted 8 lines
Quoted 3 lines
This is far safer and easier to reason about than what BPF is doing because we're not compiling arbitrary code, the actual codegen part is 200 loc and the input is just a single table.
Great, then propose a model where the codegen operates in an extra-safe protected context. Or pre-generate the most common variants, have them pull their constants from memory instead of immediates, and use that.
I'll do no such nonsense.
Quoted 5 lines
Quoted 3 lines
If what you were saying was true, it would be an issue any time we mapped in new executable code for userspace - minor page faults would be stupidly slow.
I literally mentioned this in the email.
No, you didn't. Feel free to link or cite if you think otherwise.
Quoted 23 lines
I don't know _precisely_ what's going on, but I assume it's that it's impossible (assuming the kernel gets TLB invalidation right) for a CPU to have anything buffered for a linear address that is unmapped, so when it gets mapped, the CPU can't have anything stale in its buffers. (By buffers, I mean any sort of instruction or decoded instruction cache.)
Having *this* conversation is what I was talking about in regard to possible fancy future optimization.
Quoted 7 lines
This code has been running on thousands of machines for years, and the only issues that have come up have been due to the recent introduction of indirect branch tracking. x86 doesn't have such broken caches, and architectures that do have utterly broken caches (because that's what you're describing: you're describing caches that
_arenot coherent acrosscores_) are not high on my list of things I care about.I care. And a bunch of people who haven't gotten their filesystem corrupted because of a missed serialization.
Quoted 2 lines
Also, SERIALIZE is a spectre thing. Not relevant here.
Nope, try again. SERIALIZE "serializes" in the rather vague sense in the Intel SDM. I don't think it's terribly useful for Spectre.
(Yes, I know what I'm talking about.)
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Based on the above, I regret to inform you that
jit_update()will either need to sync all cores via IPI or all cores will need to check whether a sync is needed and do it themselves.
text_poke()doesn't even send IPIs.
text_poke()and the associated machinery is unbelievably complicated.
It's not that bad.
The only reference to IPIs in text_poke() is the comment that indicates that flush_tlb_mm_range() may sometimes do IPIs, but explicitly indicates that it does _not_ do IPIs the way text_poke() is using it.
Quoted 6 lines
Also,
arch/x86/kernel/alternative.ccontains:void
text_poke_sync(void){ on_each_cpu(do_sync_core, NULL, 1); }
...which is for modifying code that is currently being executed, not the text_poke() or text_poke_copy() paths.
Quoted 7 lines
The magic in
text_poke()was developed over the course of years, and Intel architects were involved.(And I think some
text_poke()stuff uses RCU, which is another way to sync without IPI. I doubt the performance characteristics are appropriate for bcachefs, but I could be wrong.)
No, it doesn't use RCU.
Quoted 3 lines
Quoted 1 line
I think you've been misled about some things :)
I wish.
Given your comments on text_poke(), I think you are. You're confusing synchronization requirements for _self modifying_ code with the synchronization requirements for writing new code to memory, and then executing it.
And given that bcachefs is not doing anything new here - we're doing a more limited form of what BPF is already doing - I don't think this is even the appropriate place for this discussion. There is a new executable memory allocator being developed and posted, which is expected to wrap text_poke() in an arch-independent way so that allocations can share pages, and so that we can remove the need to have pages mapped both writeable and executable.
If you've got knowledge you wish to share on how to get cache coherency right, I think that might be a more appropriate thread.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 9 lines · Kent Overstreet
Quoted 6 lines · Andy Lutomirski
On Sat, Jun 17, 2023, at 8:34 AM, Kent Overstreet wrote:
Great, then propose a model where the codegen operates in an extra-safe protected context. Or pre-generate the most common variants, have them pull their constants from memory instead of immediates, and use that.
I'll do no such nonsense.
You can avoid generating code beyond what gcc generates at all, or you can pre-generate code but not on an ongoing basis at runtime, or you can generate code at runtime correctly. I don't think there are many other options.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
If what you were saying was true, it would be an issue any time we mapped in new executable code for userspace - minor page faults would be stupidly slow.
I literally mentioned this in the email.
No, you didn't. Feel free to link or cite if you think otherwise.
"It's clear that a way to do this without
serializing must exist, because that's what happens when code is paged in from a user program."
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
text_poke()doesn't even send IPIs.
text_poke()and the associated machinery is unbelievably complicated.It's not that bad.
This is a useless discussion.
Quoted 16 lines
The only reference to IPIs in
text_poke()is the comment that indicates thatflush_tlb_mm_range()may sometimes do IPIs, but explicitly indicates that it does _not_ do IPIs the waytext_poke()is using it.Quoted 6 lines
Also,
arch/x86/kernel/alternative.ccontains:void
text_poke_sync(void){ on_each_cpu(do_sync_core, NULL, 1); }...which is for modifying code that is currently being executed, not the
text_poke()ortext_poke_copy()paths.Quoted 1 line
Quoted 8 lines
Quoted 6 lines
The magic in
text_poke()was developed over the course of years, and Intel architects were involved.(And I think some
text_poke()stuff uses RCU, which is another way to sync without IPI. I doubt the performance characteristics are appropriate for bcachefs, but I could be wrong.)No, it doesn't use RCU.
It literally says in alternative.c:
* Not safe against concurrent execution; useful for JITs to dump
* new code blocks into unused regions of RX memory. Can be used in
* conjunction with synchronize_rcu_tasks() to wait for existing
* execution to quiesce after having made sure no existing functions
* pointers are live.
I don't know whether any callers actually do this. I didn't look.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
I think you've been misled about some things :)
I wish.
Given your comments on
text_poke(), I think you are. You're confusing synchronization requirements for_selfmodifying_code with the synchronization requirements for writing new code to memory, and then executing it.
No, you are misunderstanding the difference.
Version A:
User mmap()s an executable file (DSO, whatever). At first, there is either no PTE or a not-present PTE. At some point, in response to a page fault or just the kernel prefetching, the kernel fills in the backing page and then creates the PTE. From the CPU's perspective, the virtual address atomically transitions from having nothing there to having the final code there. It works (despite the manual having nothing to say about this case). It's also completely unavoidable.
Version B:
Kernel vmallocs some space *and populates the pagetables*. There is backing storage, that is executable (or it's a non-NX system, although those are quite rare these days).
Because the CPU hates you, it speculatively executes that code. (Maybe you're under attack. Maybe you're just unlucky. Doesn't matter.) It populates the instruction cache, remembers the decoded instructions, etc. It does all the things that make the manual say scary things about serialization. It notices that the speculative execution was wrong and backs it out, but nothing is invalidated.
Now you write code into there. Either you do this from a different CPU or you do it at a different linear address, so the magic hardware that invalidates for you does not trigger.
Now you jump into that code, and you tell yourself that it was new code because it was all zeros before and you never intentionally executed it. But the CPU could not care less what you think, and you lose.
Quoted 8 lines
And given that bcachefs is not doing anything new here - we're doing a more limited form of what BPF is already doing - I don't think this is even the appropriate place for this discussion. There is a new executable memory allocator being developed and posted, which is expected to wrap
text_poke()in an arch-independent way so that allocations can share pages, and so that we can remove the need to have pages mapped both writeable and executable.
I don't really care what BPF is doing, and BPF may well have the same problem.
But if I understood what bcachefs is doing, it's creating code vastly more frequently than BPF, in response to entirely unprivileged operations from usermode. It's a whole different amount of exposure.
Quoted 3 lines
If you've got knowledge you wish to share on how to get cache coherency right, I think that might be a more appropriate thread.
I'll look.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetQuoted 11 lines · Kent Overstreet
Quoted 10 lines · Andy Lutomirski
Quoted 6 lines · Kent Overstreet
Quoted 1 line · Kees Cook
For something that small, why not use the
text_pokeAPI?This looks like it's meant for patching existing kernel text, which isn't what I want - I'm generating new functions on the fly, one per btree node.
Dynamically generating code is a giant can of worms.
Quoted 8 lines
Quoted 5 lines
Kees touched on a basic security thing: a linear address mapped W+X is a big no-no. And that's just scratching the surface -- ideally we would have a strong protocol for generating code: the code is generated in some extra-secure context, then it's made immutable and double-checked, then it becomes live.
"Double checking" arbitrary code is is fantasy. You can't "prove the security" of arbitrary code post compilation.
I think there's a misunderstanding here about the threat model I'm interested in protecting against for JITs. While making sure the VM of a JIT is safe in itself, that's separate from what I'm concerned about.
The threat model is about flaws _elsewhere_ in the kernel that can leverage the JIT machinery to convert a "write anything anywhere anytime" exploit primitive into an "execute anything" primitive. Arguments can be made to say "a write anything flaw means the total collapse of the security model so there's no point defending against it", but both that type of flaw and the slippery slope argument don't stand up well to real-world situations.
The kinds of flaws we've seen are frequently limited in scope (write 1 byte, write only NULs, write only in a specific range, etc), but when chained together, the weakest link is what ultimately compromises the kernel. As such, "W^X" is a basic building block of the kernel's self-defense methods, because it is such a potent target for a write->execute attack upgrades.
Since a JIT constructs something that will become executable, it needs to defend itself against stray writes from other threads. Since Linux doesn't (really) use per-CPU page tables, the workspace for a JIT can be targeted by something that isn't the JIT. To deal with this, JITs need to use 3 phases: a writing pass (into W memory), then switch it to RO and perform a verification pass (construct it again, but compare results to the RO version), and finally switch it executable. Or, it can use writes to memory that only the local CPU can perform (i.e. text_poke(), which uses a different set of page tables with different permissions).
Without basic W^X, it becomes extremely difficult to build further defenses (e.g. protecting page tables themselves, etc) since WX will remain the easiest target.
-Kees
Signature · 1 line
Kees Cook
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 11 lines · Kees Cook
I think there's a misunderstanding here about the threat model I'm interested in protecting against for JITs. While making sure the VM of a JIT is safe in itself, that's separate from what I'm concerned about.
The threat model is about flaws _elsewhere_ in the kernel that can leverage the JIT machinery to convert a "write anything anywhere anytime" exploit primitive into an "execute anything" primitive. Arguments can be made to say "a write anything flaw means the total collapse of the security model so there's no point defending against it", but both that type of flaw and the slippery slope argument don't stand up well to real-world situations.
Hey Kees, thanks for the explanation - I don't think this is a concern for what bcachefs is doing, since we're not doing a full jit. The unpack functions we generate only write to the 40 bytes pointed to by rsi; not terribly useful as an execute anything primitive :)
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Kent OverstreetQuoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Much like Kees and Andy, I have concerns with adding new code generators to the kernel. Even ignoring the actual code generation, there are a bunch of subtle ordering/maintenance/synchronization concerns across architectures, and we already have a fair amount of pain with the existing cases.
Can you share more detail on how you want to use this?
From a quick scan of your gitweb for the bcachefs-for-upstream branch I couldn't spot the relevant patches.
Thanks,
Mark.
Quoted 105 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Andrew Morton <[email protected]>Cc Uladzislau Rezki <[email protected]>Cc Christoph Hellwig <[email protected]>--- include/linux/vmalloc.h | 1 + kernel/module/main.c | 4 +--- mm/nommu.c | 18 ++++++++++++++++++ mm/vmalloc.c | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-)include/linux/vmalloc.h +1
-0@@ -145,6 +145,7 @@ extern void *vzalloc(unsigned long size) __alloc_size(1);extern void *vmalloc_user (unsigned long size)__alloc_size(1) ;extern void *vmalloc_node (unsigned long size,int node)__alloc_size(1) ;extern void *vzalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vmalloc_exec (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;extern void *vmalloc_32 (unsigned long size)__alloc_size(1) ;extern void *vmalloc_32_user (unsigned long size)__alloc_size(1) ;extern void *__vmalloc (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;kernel/module/main.c +1
-3@@ -1607,9 +1607,7 @@ static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbgvoid *__weak module_alloc (unsigned long size) { -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL ,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , -NUMA_NO_NODE ,__builtin_return_address (0 )); +return vmalloc_exec (size,GFP_KERNEL ); }bool __weak module_init_section (const char *name)mm/nommu.c +18
-0@@ -280,6 +280,24 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +*/ + +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc (size, gfp_mask); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +/** * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) * @size: allocation size mm/vmalloc.c +21
-0@@ -3401,6 +3401,27 @@ void *vzalloc_node(unsigned long size, int node) }EXPORT_SYMBOL (vzalloc_node); +/** +* vmalloc_exec - allocate virtually contiguous, executable memory +* @size: allocation size +* +* Kernel-internal function to allocate enough pages to cover @size +* the page level allocator and map them into contiguous and +* executable kernel virtual space. +* +* For tight control over page level allocator and protection flags +* use __vmalloc() instead. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vmalloc_exec (unsigned long size,gfp_t gfp_mask) +{ +return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , + gfp_mask,PAGE_KERNEL_EXEC ,VM_FLUSH_RESET_PERMS , +NUMA_NO_NODE ,__builtin_return_address (0 )); +} +EXPORT_SYMBOL_GPL (vmalloc_exec); +#if defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA32 )#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)#elif defined(CONFIG_64BIT ) && defined(CONFIG_ZONE_DMA )Signature · 1 line
2.40.1
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 10 lines · Mark Rutland
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Much like Kees and Andy, I have concerns with adding new code generators to the kernel. Even ignoring the actual code generation, there are a bunch of subtle ordering/maintenance/synchronization concerns across architectures, and we already have a fair amount of pain with the existing cases.
Look, jits are just not that unusual. I'm not going to be responding to vague concerns that don't have any actual engineering rational.
Quoted 4 lines
Can you share more detail on how you want to use this?
From a quick scan of your gitweb for the bcachefs-for-upstream branch I couldn't spot the relevant patches.
I've already written extensively in this thread.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 7 lines · Kent Overstreet
Quoted 6 lines · Mark Rutland
Quoted 4 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
This is needed for bcachefs, which dynamically generates per-btree node unpack functions.
Quoted 7 lines
Quoted 4 lines
Much like Kees and Andy, I have concerns with adding new code generators to the kernel. Even ignoring the actual code generation, there are a bunch of subtle ordering/maintenance/synchronization concerns across architectures, and we already have a fair amount of pain with the existing cases.
Look, jits are just not that unusual. I'm not going to be responding to vague concerns that don't have any actual engineering rational.
Sorry, but I do have an engineering rationale here: I want to make sure that this actually works, on architectures that I care about, and will be maintanable long-term.
We've had a bunch of problems with other JITs ranging from JIT-local "we got the encoding wrong" to major kernel infrastructure changes like tasks RCU rude synchronization. I'm trying to figure out whether any of those are likely to apply and/or whether we should be refactoring other infrastructure for use here (e.g. the factoring the acutal instruction generation from arch code, or perhaps reusing eBPF so this can be arch-neutral).
I appreciate that's not clear from my initial mail, but please don't jump straight to assuming I'm adversarial here.
Quoted 6 lines
Quoted 4 lines
Can you share more detail on how you want to use this?
From a quick scan of your gitweb for the bcachefs-for-upstream branch I couldn't spot the relevant patches.
I've already written extensively in this thread.
Sorry, I hadn't seen that.
For the benefit of others, the codegen is at:
https://lore.kernel.org/lkml/ZFq7JhrhyrMTNfd%[email protected]/
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/bkey.c#n727
... and the rationale is at:
https://lore.kernel.org/lkml/[email protected]/
One thing I note mmediately is that HAVE_BCACHEFS_COMPILED_UNPACK seems to be x86-only. If this is important, that'll need some rework to either be arch-neutral or allow for arch-specific implementations.
Thanks,
Mark.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 11 lines · Mark Rutland
Sorry, but I do have an engineering rationale here: I want to make sure that this actually works, on architectures that I care about, and will be maintanable long-term.
We've had a bunch of problems with other JITs ranging from JIT-local "we got the encoding wrong" to major kernel infrastructure changes like tasks RCU rude synchronization. I'm trying to figure out whether any of those are likely to apply and/or whether we should be refactoring other infrastructure for use here (e.g. the factoring the acutal instruction generation from arch code, or perhaps reusing eBPF so this can be arch-neutral).
Quoted 2 lines
I appreciate that's not clear from my initial mail, but please don't jump straight to assuming I'm adversarial here.
I know you're not trying to be adversarial, but vague negative feedback _is_ hostile, because productive technical discussions can't happen without specifics and you're putting all the onus on the other person to make that happen.
When you're raising an issue, try be specific - don't make people dig. If you're unable to be specific, perhaps you're not the right person to be raising the issue.
I'm of course happy to answer questions that haven't already been asked.
This code is pretty simple as JITs go. With the existing, vmalloc_exec() based code, there aren't any fancy secondary mappings going on, so no crazy cache coherency games, and no crazy syncronization issues to worry
about: the jit functions are protected by the per-btree-node locks.
vmalloc_exec() isn't being upstreamed however, since people don't want WX mappings.
The infrastructure changes we need (and not just for bcachefs) are
- better executable memory allocation API, with support for sub-page
allocations: this is already being worked on, the prototype slab allocator I posted is probably going to be the basis for part of this
- an arch indepenendent version of text_poke(): we don't want user code to be flipping page permissions to update text, text_poke() is the proper API but it's x86 only. No one has volunteered for this yet.
Re-using eBPF for bcachefs's unpack functions is not outside the realm of possibility, but BPF is a heavy, complex dependency - it's not something I'll be looking at unless the BPF people are volunteering to refactor their stuff to provide a suitable API.
Quoted 3 lines
One thing I note mmediately is that
HAVE_BCACHEFS_COMPILED_UNPACKseems to be x86-only. If this is important, that'll need some rework to either be arch-neutral or allow for arch-specific implementations.
Correct. Arm will happen at some point, but it's not an immediate priority.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 12 lines · Kent Overstreet
Quoted 11 lines · Mark Rutland
Sorry, but I do have an engineering rationale here: I want to make sure that this actually works, on architectures that I care about, and will be maintanable long-term.
We've had a bunch of problems with other JITs ranging from JIT-local "we got the encoding wrong" to major kernel infrastructure changes like tasks RCU rude synchronization. I'm trying to figure out whether any of those are likely to apply and/or whether we should be refactoring other infrastructure for use here (e.g. the factoring the acutal instruction generation from arch code, or perhaps reusing eBPF so this can be arch-neutral).
Quoted 7 lines
Quoted 2 lines
I appreciate that's not clear from my initial mail, but please don't jump straight to assuming I'm adversarial here.
I know you're not trying to be adversarial, but vague negative feedback _is_ hostile, because productive technical discussions can't happen without specifics and you're putting all the onus on the other person to make that happen.
I'm sorry, but this isn't how correct code gets written, and this isn't how at least x86 maintenance operates.
Code is either correct, and comes with an explanation as to how it is correct, or it doesn't go in. Saying that something is like BPF is not an explanation as to how it's correct. Saying that someone has not come up with the chain of events that causes a mere violation of architecture rules to actual incorrect execution is not an explanation as to how something is correct.
So, without intending any particular hostility:
<puts on maintainer hat>
bcachefs's x86 JIT is:
<takes off maintainer hat>
This makes me sad, because I like bcachefs. But you can get it merged without worrying about my NAK by removing the x86 part.
Quoted 15 lines
When you're raising an issue, try be specific - don't make people dig. If you're unable to be specific, perhaps you're not the right person to be raising the issue.
I'm of course happy to answer questions that haven't already been asked.
This code is pretty simple as JITs go. With the existing,
vmalloc_exec()based code, there aren't any fancy secondary mappings going on, so no crazy cache coherency games, and no crazy syncronization issues to worry
about: the jit functions are protected by the per-btree-node locks.
vmalloc_exec()isn't being upstreamed however, since people don't want WX mappings.
Quoted 9 lines
The infrastructure changes we need (and not just for bcachefs) are
- better executable memory allocation API, with support for sub-page
allocations: this is already being worked on, the prototype slab allocator I posted is probably going to be the basis for part of this- an arch indepenendent version of
text_poke(): we don't want user code to be flipping page permissions to update text,text_poke()is the proper API but it's x86 only. No one has volunteered for this yet.
text_poke() by itself is *not* the proper API, as discussed. It doesn't serialize adequately, even on x86. We have text_poke_sync() for that.
--Andy
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 6 lines · Andy Lutomirski
Code is either correct, and comes with an explanation as to how it is correct, or it doesn't go in. Saying that something is like BPF is not an explanation as to how it's correct. Saying that someone has not come up with the chain of events that causes a mere violation of architecture rules to actual incorrect execution is not an explanation as to how something is correct.
No, I'm saying your concerns are baseless and too vague to address.
Quoted 3 lines
text_poke()by itself is *not* the proper API, as discussed. It doesn't serialize adequately, even on x86. We havetext_poke_sync()for that.
Andy, I replied explaining the difference between text_poke() and text_poke_sync(). It's clear you have no idea what you're talking about, so I'm not going to be wasting my time on further communications with you.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 9 lines · Kent Overstreet
Quoted 6 lines · Andy Lutomirski
Code is either correct, and comes with an explanation as to how it is correct, or it doesn't go in. Saying that something is like BPF is not an explanation as to how it's correct. Saying that someone has not come up with the chain of events that causes a mere violation of architecture rules to actual incorrect execution is not an explanation as to how something is correct.
No, I'm saying your concerns are baseless and too vague to address.
If you don't address them, the NAK will stand forever, or at least until a different group of people take over x86 maintainership. That's fine with me.
I'm generally pretty happy about working with people to get their Linux code right. But no one is obligated to listen to me.
Quoted 9 lines
Quoted 4 lines
text_poke()by itself is *not* the proper API, as discussed. It doesn't serialize adequately, even on x86. We havetext_poke_sync()for that.Andy, I replied explaining the difference between
text_poke()andtext_poke_sync(). It's clear you have no idea what you're talking about, so I'm not going to be wasting my time on further communications with you.
No problem. Then your x86 code will not be merged upstream.
Best of luck with the actual filesystem parts!
--Andy
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 5 lines
Quoted 2 lines
No, I'm saying your concerns are baseless and too vague to address.
If you don't address them, the NAK will stand forever, or at least until a different group of people take over x86 maintainership. That's fine with me.
I've got a specific concern: I don't see vmalloc_exec() used in this series anywhere. I also don't see any of the actual assembly that's being generated, or the glue code that's calling into the generated assembly.
I grepped around a bit in your git trees, but I also couldn't find it in there. Any chance you could help a guy out and point us to some of the specifics of this new, tiny JIT?
Quoted 4 lines
Quoted 4 lines
Andy, I replied explaining the difference between
text_poke()andtext_poke_sync(). It's clear you have no idea what you're talking about, so I'm not going to be wasting my time on further communications with you.
One more specific concern: This comment made me very uncomfortable and it read to me very much like a personal attack, something which is contrary to our code of conduct.
Kent Overstreet
maintainerRe: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 6 lines · Dave Hansen
Quoted 5 lines
Quoted 2 lines
No, I'm saying your concerns are baseless and too vague to address.
If you don't address them, the NAK will stand forever, or at least until a different group of people take over x86 maintainership. That's fine with me.
Quoted 8 lines
I've got a specific concern: I don't see
vmalloc_exec()used in this series anywhere. I also don't see any of the actual assembly that's being generated, or the glue code that's calling into the generated assembly.I grepped around a bit in your git trees, but I also couldn't find it in there. Any chance you could help a guy out and point us to some of the specifics of this new, tiny JIT?
vmalloc_exec() has already been dropped from the patchset - I'll switch to the new jit allocator when that's available and doing sub-page allocations.
I can however point you at the code that generates the unpack functions:
https://evilpiepirate.org/git/bcachefs.git/tree/fs/bcachefs/bkey.c#n727
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Andy, I replied explaining the difference between
text_poke()andtext_poke_sync(). It's clear you have no idea what you're talking about, so I'm not going to be wasting my time on further communications with you.One more specific concern: This comment made me very uncomfortable and it read to me very much like a personal attack, something which is contrary to our code of conduct.
It's not; I prefer to be direct than passive aggressive, and if I have to bow out of a discussion that isn't going anywhere I feel I owe an explanation of _why_. Too much conflict avoidance means things don't get resolved.
And Andy and I are talking on IRC now, so things are proceeding in a better direction.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
In reply to Dave HansenHi all-
Quoted 6 lines · Dave Hansen
Quoted 5 lines
Quoted 2 lines
No, I'm saying your concerns are baseless and too vague to address.
If you don't address them, the NAK will stand forever, or at least until a different group of people take over x86 maintainership. That's fine with me.
Quoted 9 lines
I've got a specific concern: I don't see
vmalloc_exec()used in this series anywhere. I also don't see any of the actual assembly that's being generated, or the glue code that's calling into the generated assembly.I grepped around a bit in your git trees, but I also couldn't find it in there. Any chance you could help a guy out and point us to some of the specifics of this new, tiny JIT?
So I had a nice discussion with Kent on IRC, and, for the benefit of everyone else reading along, I *think* the JITted code can be replaced by a table-driven approach like this:
typedef unsigned int u32;
typedef unsigned long u64;
struct uncompressed
{
u32 a;
u32 b;
u64 c;
u64 d;
u64 e;
u64 f;
};struct bitblock
{
u64 source;
u64 target;
u64 mask;
int shift;
};// out needs to be zeroed first
void unpack(struct uncompressed *out, const u64 *in, const struct bitblock *blocks, int nblocks)
{
u64 *out_as_words = (u64*)out;
for (int i = 0; i < nblocks; i++) {
const struct bitblock *b;
out_as_words[b->target] |= (in[b->source] & b->mask) << b->shift;
}
}void apply_offsets(struct uncompressed *out, const struct uncompressed *offsets)
{
out->a += offsets->a;
out->b += offsets->b;
out->c += offsets->c;
out->d += offsets->d;
out->e += offsets->e;
out->f += offsets->f;
}Which generates nice code: https://godbolt.org/z/3fEq37hf5
It would need spectre protection in two places, I think, because it's almost most certainly a great gadget if the attacker can speculatively control the 'blocks' table. This could be mitigated (I think) by hardcoding nblocks as 12 and by masking b->target.
In contrast, the JIT approach needs a retpoline on each call, which could be more expensive than my entire function :) I haven't benchmarked them lately.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 66 lines · Andy Lutomirski
Hi all-
Quoted 6 lines · Dave Hansen
Quoted 5 lines
Quoted 2 lines
No, I'm saying your concerns are baseless and too vague to address.
If you don't address them, the NAK will stand forever, or at least until a different group of people take over x86 maintainership. That's fine with me.
Quoted 9 lines
I've got a specific concern: I don't see
vmalloc_exec()used in this series anywhere. I also don't see any of the actual assembly that's being generated, or the glue code that's calling into the generated assembly.I grepped around a bit in your git trees, but I also couldn't find it in there. Any chance you could help a guy out and point us to some of the specifics of this new, tiny JIT?
So I had a nice discussion with Kent on IRC, and, for the benefit of everyone else reading along, I *think* the JITted code can be replaced by a table-driven approach like this:
typedef unsigned int u32;
typedef unsigned long u64;struct uncompressed
{ u32 a; u32 b; u64 c; u64 d; u64 e; u64 f; };struct bitblock
{ u64 source; u64 target; u64 mask; int shift; };// out needs to be zeroed first
voidunpack(struct uncompressed *out, const u64 *in, const struct bitblock *blocks, int nblocks){ u64 *out_as_words = (u64*)out; for (int i = 0; i < nblocks; i++) { const struct bitblock *b; out_as_words[b->target] |= (in[b->source] & b->mask) << b->shift; } }void
apply_offsets(struct uncompressed *out, const struct uncompressed *offsets){ out->a += offsets->a; out->b += offsets->b; out->c += offsets->c; out->d += offsets->d; out->e += offsets->e; out->f += offsets->f; }
Quoted 1 line
Which generates nice code: https://godbolt.org/z/3fEq37hf5
Thinking about this a bit more, I think the only real performance issue with my code is that it does 12 read-xor-write operations in memory, which all depend on each other in horrible ways.
If it's reversed so the stores are all in order, then this issue would go away.
typedef unsigned int u32;
typedef unsigned long u64;
struct uncompressed
{
u32 a;
u32 b;
u64 c;
u64 d;
u64 e;
u64 f;
};struct field_piece {
int source;
int shift;
u64 mask;
};struct field_pieces {
struct field_piece pieces[2];
u64 offset;
};u64 unpack_one(const u64 *in, const struct field_pieces *pieces)
{
const struct field_piece *p = pieces->pieces;
return (((in[p[0].source] & p[0].mask) << p[0].shift) |
((in[p[1].source] & p[1].mask) << p[1].shift)) +
pieces->offset;
}struct encoding {
struct field_pieces a, b, c, d, e, f;
};void unpack(struct uncompressed *out, const u64 *in, const struct encoding *encoding)
{
out->a = unpack_one(in, &encoding->a);
out->b = unpack_one(in, &encoding->b);
out->c = unpack_one(in, &encoding->c);
out->d = unpack_one(in, &encoding->d);
out->e = unpack_one(in, &encoding->e);
out->f = unpack_one(in, &encoding->f);
}https://godbolt.org/z/srsfcGK4j
Could be faster. Probably worth testing.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 25 lines · Andy Lutomirski
Quoted 23 lines
// out needs to be zeroed first
voidunpack(struct uncompressed *out, const u64 *in, const struct bitblock *blocks, int nblocks){ u64 *out_as_words = (u64*)out; for (int i = 0; i < nblocks; i++) { const struct bitblock *b; out_as_words[b->target] |= (in[b->source] & b->mask) << b->shift; } }void
apply_offsets(struct uncompressed *out, const struct uncompressed *offsets){ out->a += offsets->a; out->b += offsets->b; out->c += offsets->c; out->d += offsets->d; out->e += offsets->e; out->f += offsets->f; }
Quoted 3 lines
Quoted 1 line
Which generates nice code: https://godbolt.org/z/3fEq37hf5
Thinking about this a bit more, I think the only real performance issue with my code is that it does 12 read-xor-write operations in memory, which all depend on each other in horrible ways.
If you compare the generated code, just notice that you forgot to initialize b in unpack() in this version.
I presume you wanted it to say "b = &blocks[i]”.
Re: [PATCH 07/32] mm: Bring back vmalloc_exec
Quoted 24 lines · Nadav Amit
Quoted 24 lines · Andy Lutomirski
Quoted 23 lines
// out needs to be zeroed first
voidunpack(struct uncompressed *out, const u64 *in, const struct bitblock *blocks, int nblocks){ u64 *out_as_words = (u64*)out; for (int i = 0; i < nblocks; i++) { const struct bitblock *b; out_as_words[b->target] |= (in[b->source] & b->mask) << b->shift; } }void
apply_offsets(struct uncompressed *out, const struct uncompressed *offsets){ out->a += offsets->a; out->b += offsets->b; out->c += offsets->c; out->d += offsets->d; out->e += offsets->e; out->f += offsets->f; }
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Which generates nice code: https://godbolt.org/z/3fEq37hf5
Thinking about this a bit more, I think the only real performance issue with my code is that it does 12 read-xor-write operations in memory, which all depend on each other in horrible ways.
If you compare the generated code, just notice that you forgot to initialize b in
unpack()in this version.I presume you wanted it to say "b = &blocks[i]”.
Indeed. I also didn't notice that -Wall wasn't set. Oops.
Kent Overstreet
maintainerPATCH 8/32 fs: factor out d_mark_tmpfile()
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
New helper for bcachefs - bcachefs doesn't want the inode_dec_link_count() call that d_tmpfile does, it handles i_nlink on its own atomically with other btree updates
--- fs/dcache.c | 12 ++++++++++-- include/linux/dcache.h | 1 + 2 files changed, 11 insertions(+), 2 deletions(-)
fs/dcache.c +10 -2
@@ -3249,11 +3249,10 @@ void d_genocide(struct dentry *parent)EXPORT_SYMBOL (d_genocide); -void d_tmpfile (struct file *file,struct inode *inode) +void d_mark_tmpfile (struct file *file,struct inode *inode) {struct dentry *dentry = file->f_path.dentry; -inode_dec_link_count (inode);BUG_ON (dentry->d_name.name != dentry->d_iname || !hlist_unhashed (&dentry->d_u.d_alias) || !d_unlinked (dentry));
@@ -3263,6 +3262,15 @@ void d_tmpfile(struct file *file, struct inode *inode) (unsigned long long)inode->i_ino);spin_unlock (&dentry->d_lock);spin_unlock (&dentry->d_parent->d_lock); +} +EXPORT_SYMBOL (d_mark_tmpfile); + +void d_tmpfile (struct file *file,struct inode *inode) +{ +struct dentry *dentry = file->f_path.dentry; + +inode_dec_link_count (inode); +d_mark_tmpfile (file, inode);d_instantiate (dentry, inode); }EXPORT_SYMBOL (d_tmpfile);
include/linux/dcache.h +1 -0
@@ -251,6 +251,7 @@ extern struct dentry * d_make_root(struct inode *);/* <clickety>-<click> the ramfs-type tree */ extern void d_genocide (struct dentry *); +extern void d_mark_tmpfile (struct file *,struct inode *);extern void d_tmpfile (struct file *,struct inode *);extern struct dentry *d_find_alias (struct inode *);
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 9/32 block: Add some exports for bcachefs
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
- bio_set_pages_dirty(), bio_check_pages_dirty() - dio path
- blk_status_to_str() - error messages
- bio_add_folio() - this should definitely be exported for everyone, it's the modern version of bio_add_page()
--- block/bio.c | 3 +++ block/blk-core.c | 1 + block/blk.h | 1 - include/linux/blkdev.h | 1 + 4 files changed, 5 insertions(+), 1 deletion(-)
block/bio.c +3 -0
@@ -1159,6 +1159,7 @@ bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,return false;return bio_add_page (bio, &folio->page, len, off) >0 ; } +EXPORT_SYMBOL (bio_add_folio);void __bio_release_pages (struct bio *bio,bool mark_dirty) {
@@ -1480,6 +1481,7 @@ void bio_set_pages_dirty(struct bio *bio)set_page_dirty_lock (bvec->bv_page); } } +EXPORT_SYMBOL_GPL (bio_set_pages_dirty);/* * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
@@ -1539,6 +1541,7 @@ void bio_check_pages_dirty(struct bio *bio)spin_unlock_irqrestore (&bio_dirty_lock, flags);schedule_work (&bio_dirty_work); } +EXPORT_SYMBOL_GPL (bio_check_pages_dirty);static inline bool bio_remaining_done (struct bio *bio) {
block/blk-core.c +1 -0
@@ -205,6 +205,7 @@ const char *blk_status_to_str(blk_status_t status)return "<null>" ;return blk_errors[idx].name; } +EXPORT_SYMBOL_GPL (blk_status_to_str);/** * blk_sync_queue - cancel any pending callbacks on a queue
block/blk.h +0 -1
@@ -259,7 +259,6 @@ static inline void blk_integrity_del(struct gendisk *disk)unsigned long blk_rq_timeout (unsigned long timeout);void blk_add_timer (struct request *req); -const char *blk_status_to_str (blk_status_t status);bool blk_attempt_plug_merge (struct request_queue *q,struct bio *bio,unsigned int nr_segs);
include/linux/blkdev.h +1 -0
@@ -867,6 +867,7 @@ extern const char *blk_op_str(enum req_op op);int blk_status_to_errno (blk_status_t status);blk_status_t errno_to_blk_status (int errno); +const char *blk_status_to_str (blk_status_t status);/* only poll the hardware once, don't continue until a completion was found */ #define BLK_POLL_ONESHOT (1 << 0)
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 10/32 block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset
In reply to Kent Overstreetbio_iov_iter_get_pages() trims the IO based on the block size of the block device the IO will be issued to.
However, bcachefs is a multi device filesystem; when we're creating the bio we don't yet know which block device the bio will be submitted to - we have to handle the alignment checks elsewhere.
Thus this is needed to avoid a null ptr deref.
--- block/bio.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
block/bio.c +6 -4
@@ -1245,7 +1245,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)struct page **pages = (struct page **)bv;ssize_t size, left;unsigned len, i =0 ; -size_t offset, trim; +size_t offset;int ret =0 ;/*
@@ -1274,10 +1274,12 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) nr_pages =DIV_ROUND_UP (offset + size,PAGE_SIZE ); - trim = size & (bdev_logical_block_size (bio->bi_bdev) -1 ); -iov_iter_revert (iter, trim); +if (bio->bi_bdev) { +size_t trim = size & (bdev_logical_block_size (bio->bi_bdev) -1 ); +iov_iter_revert (iter, trim); + size -= trim; + } - size -= trim;if (unlikely (!size)) { ret = -EFAULT ; goto out;
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 11/32 block: Bring back zero_fill_bio_iter
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
This reverts the commit that deleted it; it's used by bcachefs.
--- block/bio.c | 6 +++--- include/linux/bio.h | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-)
block/bio.c +3 -3
@@ -606,15 +606,15 @@ struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask) }EXPORT_SYMBOL (bio_kmalloc); -void zero_fill_bio (struct bio *bio) +void zero_fill_bio_iter (struct bio *bio,struct bvec_iter start) {struct bio_vec bv;struct bvec_iter iter; -bio_for_each_segment (bv, bio, iter) +__bio_for_each_segment (bv, bio, iter, start)memzero_bvec (&bv); } -EXPORT_SYMBOL (zero_fill_bio); +EXPORT_SYMBOL (zero_fill_bio_iter);/** * bio_truncate - truncate the bio to small size of @new_size
include/linux/bio.h +6 -1
@@ -484,7 +484,12 @@ extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,extern void bio_copy_data (struct bio *dst,struct bio *src);extern void bio_free_pages (struct bio *bio);void guard_bio_eod (struct bio *bio); -void zero_fill_bio (struct bio *bio); +void zero_fill_bio_iter (struct bio *bio,struct bvec_iter iter); + +static inline void zero_fill_bio (struct bio *bio) +{ +zero_fill_bio_iter (bio, bio->bi_iter); +}static inline void bio_release_pages (struct bio *bio,bool mark_dirty) {
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 12/32 block: Rework bio_for_each_segment_all()
In reply to Kent OverstreetThis patch reworks bio_for_each_segment_all() to be more inline with how the other bio iterators work:
- bio_iter_all_peek() now returns a synthesized bio_vec; we don't stash one in the iterator and pass a pointer to it - bad. This way makes it clearer what's a constructed value vs. a reference to something pre-existing, and it also will help with cleaning up and consolidating code with bio_for_each_folio_all().
- We now provide bio_for_each_segment_all_continue(), for squashfs:
this makes their code clearer.
--- block/bio.c | 38 ++++++++++++------------ block/blk-map.c | 38 ++++++++++++------------ block/bounce.c | 12 ++++---- drivers/md/bcache/btree.c | 8 ++--- drivers/md/dm-crypt.c | 10 +++---- drivers/md/raid1.c | 4 +-- fs/btrfs/disk-io.c | 4 +-- fs/btrfs/extent_io.c | 50 +++++++++++++++---------------- fs/btrfs/raid56.c | 14 ++++----- fs/erofs/zdata.c | 4 +-- fs/ext4/page-io.c | 8 ++--- fs/ext4/readpage.c | 4 +-- fs/f2fs/data.c | 20 ++++++------- fs/gfs2/lops.c | 10 +++---- fs/gfs2/meta_io.c | 8 ++--- fs/mpage.c | 4 +-- fs/squashfs/block.c | 48 +++++++++++++++++------------- fs/squashfs/lz4_wrapper.c | 17 ++++++----- fs/squashfs/lzo_wrapper.c | 17 ++++++----- fs/squashfs/xz_wrapper.c | 19 ++++++------ fs/squashfs/zlib_wrapper.c | 18 ++++++----- fs/squashfs/zstd_wrapper.c | 19 ++++++------ include/linux/bio.h | 34 ++++++++++++++++----- include/linux/bvec.h | 61 ++++++++++++++++++++++---------------- 24 files changed, 256 insertions(+), 213 deletions(-)
block/bio.c +19 -19
@@ -1163,13 +1163,13 @@ EXPORT_SYMBOL(bio_add_folio);void __bio_release_pages (struct bio *bio,bool mark_dirty) { -struct bvec_iter_all iter_all; -struct bio_vec *bvec; +struct bvec_iter_all iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) { -if (mark_dirty && !PageCompound (bvec->bv_page)) -set_page_dirty_lock (bvec->bv_page); -put_page (bvec->bv_page); +bio_for_each_segment_all (bvec, bio, iter) { +if (mark_dirty && !PageCompound (bvec.bv_page)) +set_page_dirty_lock (bvec.bv_page); +put_page (bvec.bv_page); } }EXPORT_SYMBOL_GPL (__bio_release_pages);
@@ -1436,11 +1436,11 @@ EXPORT_SYMBOL(bio_copy_data);void bio_free_pages (struct bio *bio) { -struct bio_vec *bvec; -struct bvec_iter_all iter_all; +struct bvec_iter_all iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) -__free_page (bvec->bv_page); +bio_for_each_segment_all (bvec, bio, iter) +__free_page (bvec.bv_page); }EXPORT_SYMBOL (bio_free_pages);
@@ -1475,12 +1475,12 @@ EXPORT_SYMBOL(bio_free_pages);*/ void bio_set_pages_dirty (struct bio *bio) { -struct bio_vec *bvec; -struct bvec_iter_all iter_all; +struct bvec_iter_all iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) { -if (!PageCompound (bvec->bv_page)) -set_page_dirty_lock (bvec->bv_page); +bio_for_each_segment_all (bvec, bio, iter) { +if (!PageCompound (bvec.bv_page)) +set_page_dirty_lock (bvec.bv_page); } }EXPORT_SYMBOL_GPL (bio_set_pages_dirty);
@@ -1524,12 +1524,12 @@ static void bio_dirty_fn(struct work_struct *work)void bio_check_pages_dirty (struct bio *bio) { -struct bio_vec *bvec; +struct bvec_iter_all iter; +struct bio_vec bvec;unsigned long flags; -struct bvec_iter_all iter_all; -bio_for_each_segment_all (bvec, bio, iter_all) { -if (!PageDirty (bvec->bv_page) && !PageCompound (bvec->bv_page)) +bio_for_each_segment_all (bvec, bio, iter) { +if (!PageDirty (bvec.bv_page) && !PageCompound (bvec.bv_page)) goto defer; }
block/blk-map.c +19 -19
@@ -46,21 +46,21 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,*/ static int bio_copy_from_iter (struct bio *bio,struct iov_iter *iter) { -struct bio_vec *bvec; -struct bvec_iter_all iter_all; +struct bvec_iter_all bv_iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) { +bio_for_each_segment_all (bvec, bio, bv_iter) {ssize_t ret; - ret =copy_page_from_iter (bvec->bv_page, - bvec->bv_offset, - bvec->bv_len, + ret =copy_page_from_iter (bvec.bv_page, + bvec.bv_offset, + bvec.bv_len, iter);if (!iov_iter_count (iter))break ; -if (ret < bvec->bv_len) +if (ret < bvec.bv_len)return -EFAULT ; }
@@ -77,21 +77,21 @@ static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)*/ static int bio_copy_to_iter (struct bio *bio,struct iov_iter iter) { -struct bio_vec *bvec; -struct bvec_iter_all iter_all; +struct bvec_iter_all bv_iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) { +bio_for_each_segment_all (bvec, bio, bv_iter) {ssize_t ret; - ret =copy_page_to_iter (bvec->bv_page, - bvec->bv_offset, - bvec->bv_len, + ret =copy_page_to_iter (bvec.bv_page, + bvec.bv_offset, + bvec.bv_len, &iter);if (!iov_iter_count (&iter))break ; -if (ret < bvec->bv_len) +if (ret < bvec.bv_len)return -EFAULT ; }
@@ -442,12 +442,12 @@ static void bio_copy_kern_endio(struct bio *bio)static void bio_copy_kern_endio_read (struct bio *bio) {char *p = bio->bi_private; -struct bio_vec *bvec; -struct bvec_iter_all iter_all; +struct bvec_iter_all iter; +struct bio_vec bvec; -bio_for_each_segment_all (bvec, bio, iter_all) { -memcpy_from_bvec (p, bvec); - p += bvec->bv_len; +bio_for_each_segment_all (bvec, bio, iter) { +memcpy_from_bvec (p, &bvec); + p += bvec.bv_len; }bio_copy_kern_endio (bio);
block/bounce.c +6 -6
@@ -102,18 +102,18 @@ static void copy_to_high_bio_irq(struct bio *to, struct bio *from)static void bounce_end_io (struct bio *bio) {struct bio *bio_orig = bio->bi_private; -struct bio_vec *bvec, orig_vec; +struct bio_vec bvec, orig_vec;struct bvec_iter orig_iter = bio_orig->bi_iter; -struct bvec_iter_all iter_all; +struct bvec_iter_all iter;/* * free up bounce indirect pages used */ -bio_for_each_segment_all (bvec, bio, iter_all) { +bio_for_each_segment_all (bvec, bio, iter) { orig_vec =bio_iter_iovec (bio_orig, orig_iter); -if (bvec->bv_page != orig_vec.bv_page) { -dec_zone_page_state (bvec->bv_page,NR_BOUNCE ); -mempool_free (bvec->bv_page, &page_pool); +if (bvec.bv_page != orig_vec.bv_page) { +dec_zone_page_state (bvec.bv_page,NR_BOUNCE ); +mempool_free (bvec.bv_page, &page_pool); }bio_advance_iter (bio_orig, &orig_iter, orig_vec.bv_len); }
drivers/md/bcache/btree.c +4 -4
@@ -373,12 +373,12 @@ static void do_btree_node_write(struct btree *b) bset_sector_offset(&b->keys, i));if (!bch_bio_alloc_pages (b->bio, __GFP_NOWARN|GFP_NOWAIT )) { -struct bio_vec *bv; +struct bio_vec bv;void *addr = (void *) ((unsigned long ) i & ~(PAGE_SIZE -1 )); -struct bvec_iter_all iter_all; +struct bvec_iter_all iter; -bio_for_each_segment_all (bv, b->bio, iter_all) { -memcpy (page_address (bv->bv_page), addr,PAGE_SIZE ); +bio_for_each_segment_all (bv, b->bio, iter) { +memcpy (page_address (bv.bv_page), addr,PAGE_SIZE ); addr +=PAGE_SIZE ; }
drivers/md/dm-crypt.c +5 -5
@@ -1713,12 +1713,12 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned int size)static void crypt_free_buffer_pages (struct crypt_config *cc,struct bio *clone) { -struct bio_vec *bv; -struct bvec_iter_all iter_all; +struct bvec_iter_all iter; +struct bio_vec bv; -bio_for_each_segment_all (bv, clone, iter_all) { -BUG_ON (!bv->bv_page); -mempool_free (bv->bv_page, &cc->page_pool); +bio_for_each_segment_all (bv, clone, iter) { +BUG_ON (!bv.bv_page); +mempool_free (bv.bv_page, &cc->page_pool); } }
drivers/md/raid1.c +2 -2
@@ -2188,7 +2188,7 @@ static void process_checks(struct r1bio *r1_bio)blk_status_t status = sbio->bi_status;struct page **ppages =get_resync_pages (pbio)->pages;struct page **spages =get_resync_pages (sbio)->pages; -struct bio_vec *bi; +struct bio_vec bi;int page_len[RESYNC_PAGES ] = {0 };struct bvec_iter_all iter_all;
@@ -2198,7 +2198,7 @@ static void process_checks(struct r1bio *r1_bio) sbio->bi_status =0 ;bio_for_each_segment_all (bi, sbio, iter_all) - page_len[j++] = bi->bv_len; + page_len[j++] = bi.bv_len;if (!status) {for (j = vcnt; j-- ; ) {
fs/btrfs/disk-io.c +2 -2
@@ -3804,12 +3804,12 @@ ALLOW_ERROR_INJECTION(open_ctree, ERRNO);static void btrfs_end_super_write (struct bio *bio) {struct btrfs_device *device = bio->bi_private; -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;struct page *page;bio_for_each_segment_all (bvec, bio, iter_all) { - page = bvec->bv_page; + page = bvec.bv_page;if (bio->bi_status) { btrfs_warn_rl_in_rcu(device->fs_info,
fs/btrfs/extent_io.c +25 -25
125 lines not on this page · raw message
fs/btrfs/raid56.c +7 -7
@@ -1388,7 +1388,7 @@ static struct sector_ptr *find_stripe_sector(struct btrfs_raid_bio *rbio,static void set_bio_pages_uptodate (struct btrfs_raid_bio *rbio,struct bio *bio) {const u32 sectorsize = rbio->bioc->fs_info->sectorsize; -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;ASSERT (!bio_flagged (bio,BIO_CLONED ));
@@ -1397,9 +1397,9 @@ static void set_bio_pages_uptodate(struct btrfs_raid_bio *rbio, struct bio *bio)struct sector_ptr *sector;int pgoff; -for (pgoff = bvec->bv_offset; pgoff - bvec->bv_offset < bvec->bv_len; +for (pgoff = bvec.bv_offset; pgoff - bvec.bv_offset < bvec.bv_len; pgoff += sectorsize) { - sector =find_stripe_sector (rbio, bvec->bv_page, pgoff); + sector =find_stripe_sector (rbio, bvec.bv_page, pgoff);ASSERT (sector);if (sector) sector->uptodate =1 ;
@@ -1453,7 +1453,7 @@ static void verify_bio_data_sectors(struct btrfs_raid_bio *rbio, {struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;int total_sector_nr =get_bio_sector_nr (rbio, bio); -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;/* No data csum for the whole stripe, no need to verify. */
@@ -1467,8 +1467,8 @@ static void verify_bio_data_sectors(struct btrfs_raid_bio *rbio,bio_for_each_segment_all (bvec, bio, iter_all) {int bv_offset; -for (bv_offset = bvec->bv_offset; - bv_offset < bvec->bv_offset + bvec->bv_len; +for (bv_offset = bvec.bv_offset; + bv_offset < bvec.bv_offset + bvec.bv_len; bv_offset += fs_info->sectorsize, total_sector_nr++) {u8 csum_buf[BTRFS_CSUM_SIZE ]; u8 *expected_csum = rbio->csum_buf +
@@ -1479,7 +1479,7 @@ static void verify_bio_data_sectors(struct btrfs_raid_bio *rbio,if (!test_bit (total_sector_nr, rbio->csum_bitmap))continue ; - ret =btrfs_check_sector_csum (fs_info, bvec->bv_page, + ret =btrfs_check_sector_csum (fs_info, bvec.bv_page, bv_offset, csum_buf, expected_csum);if (ret <0 )set_bit (total_sector_nr, rbio->error_bitmap);
fs/erofs/zdata.c +2 -2
@@ -1651,11 +1651,11 @@ static void z_erofs_decompressqueue_endio(struct bio *bio) {struct z_erofs_decompressqueue *q = bio->bi_private;blk_status_t err = bio->bi_status; -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;bio_for_each_segment_all (bvec, bio, iter_all) { -struct page *page = bvec->bv_page; +struct page *page = bvec.bv_page;DBG_BUGON (PageUptodate (page));DBG_BUGON (z_erofs_page_is_invalidated (page));
fs/ext4/page-io.c +4 -4
@@ -99,15 +99,15 @@ static void buffer_io_error(struct buffer_head *bh)static void ext4_finish_bio (struct bio *bio) { -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;bio_for_each_segment_all (bvec, bio, iter_all) { -struct page *page = bvec->bv_page; +struct page *page = bvec.bv_page;struct page *bounce_page =NULL ;struct buffer_head *bh, *head; -unsigned bio_start = bvec->bv_offset; -unsigned bio_end = bio_start + bvec->bv_len; +unsigned bio_start = bvec.bv_offset; +unsigned bio_end = bio_start + bvec.bv_len;unsigned under_io =0 ;unsigned long flags;
fs/ext4/readpage.c +2 -2
@@ -69,11 +69,11 @@ struct bio_post_read_ctx {static void __read_end_io (struct bio *bio) {struct page *page; -struct bio_vec *bv; +struct bio_vec bv;struct bvec_iter_all iter_all;bio_for_each_segment_all (bv, bio, iter_all) { - page = bv->bv_page; + page = bv.bv_page;if (bio->bi_status)ClearPageUptodate (page);
fs/f2fs/data.c +10 -10
@@ -139,12 +139,12 @@ struct bio_post_read_ctx {*/ static void f2fs_finish_read_bio (struct bio *bio,bool in_task) { -struct bio_vec *bv; +struct bio_vec bv;struct bvec_iter_all iter_all;struct bio_post_read_ctx *ctx = bio->bi_private;bio_for_each_segment_all (bv, bio, iter_all) { -struct page *page = bv->bv_page; +struct page *page = bv.bv_page;if (f2fs_is_compressed_page (page)) {if (ctx && !ctx->decompression_attempted)
@@ -189,11 +189,11 @@ static void f2fs_verify_bio(struct work_struct *work)* as those were handled separately by f2fs_end_read_compressed_page(). */ if (may_have_compressed_pages) { -struct bio_vec *bv; +struct bio_vec bv;struct bvec_iter_all iter_all;bio_for_each_segment_all (bv, bio, iter_all) { -struct page *page = bv->bv_page; +struct page *page = bv.bv_page;if (!f2fs_is_compressed_page (page) && !fsverity_verify_page (page)) {
@@ -241,13 +241,13 @@ static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)static void f2fs_handle_step_decompress (struct bio_post_read_ctx *ctx,bool in_task) { -struct bio_vec *bv; +struct bio_vec bv;struct bvec_iter_all iter_all;bool all_compressed = true;block_t blkaddr = ctx->fs_blkaddr;bio_for_each_segment_all (bv, ctx->bio, iter_all) { -struct page *page = bv->bv_page; +struct page *page = bv.bv_page;if (f2fs_is_compressed_page (page)) f2fs_end_read_compressed_page(page, false, blkaddr,
@@ -327,7 +327,7 @@ static void f2fs_read_end_io(struct bio *bio)static void f2fs_write_end_io (struct bio *bio) {struct f2fs_sb_info *sbi; -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;iostat_update_and_unbind_ctx (bio);
@@ -337,7 +337,7 @@ static void f2fs_write_end_io(struct bio *bio) bio->bi_status =BLK_STS_IOERR ;bio_for_each_segment_all (bvec, bio, iter_all) { -struct page *page = bvec->bv_page; +struct page *page = bvec.bv_page;enum count_type type =WB_DATA_TYPE (page);if (page_private_dummy (page)) {
@@ -583,7 +583,7 @@ static void __submit_merged_bio(struct f2fs_bio_info *io)static bool __has_merged_page (struct bio *bio,struct inode *inode,struct page *page,nid_t ino) { -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;if (!bio)
@@ -593,7 +593,7 @@ static bool __has_merged_page(struct bio *bio, struct inode *inode,return true;bio_for_each_segment_all (bvec, bio, iter_all) { -struct page *target = bvec->bv_page; +struct page *target = bvec.bv_page;if (fscrypt_is_bounce_page (target)) { target =fscrypt_pagecache_page (target);
fs/gfs2/lops.c +5 -5
@@ -202,7 +202,7 @@ static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp,static void gfs2_end_log_write (struct bio *bio) {struct gfs2_sbd *sdp = bio->bi_private; -struct bio_vec *bvec; +struct bio_vec bvec;struct page *page;struct bvec_iter_all iter_all;
@@ -217,9 +217,9 @@ static void gfs2_end_log_write(struct bio *bio) }bio_for_each_segment_all (bvec, bio, iter_all) { - page = bvec->bv_page; + page = bvec.bv_page;if (page_has_buffers (page)) -gfs2_end_log_write_bh (sdp, bvec, bio->bi_status); +gfs2_end_log_write_bh (sdp, &bvec, bio->bi_status);else mempool_free (page, gfs2_page_pool); }
@@ -395,11 +395,11 @@ static void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)static void gfs2_end_log_read (struct bio *bio) {struct page *page; -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;bio_for_each_segment_all (bvec, bio, iter_all) { - page = bvec->bv_page; + page = bvec.bv_page;if (bio->bi_status) {int err =blk_status_to_errno (bio->bi_status);
fs/gfs2/meta_io.c +4 -4
@@ -193,15 +193,15 @@ struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)static void gfs2_meta_read_endio (struct bio *bio) { -struct bio_vec *bvec; +struct bio_vec bvec;struct bvec_iter_all iter_all;bio_for_each_segment_all (bvec, bio, iter_all) { -struct page *page = bvec->bv_page; +struct page *page = bvec.bv_page;struct buffer_head *bh =page_buffers (page); -unsigned int len = bvec->bv_len; +unsigned int len = bvec.bv_len; -while (bh_offset (bh) < bvec->bv_offset) +while (bh_offset (bh) < bvec.bv_offset) bh = bh->b_this_page; do { struct buffer_head *next = bh->b_this_page;
fs/mpage.c +2 -2
@@ -45,11 +45,11 @@*/ static void mpage_end_io (struct bio *bio) { -struct bio_vec *bv; +struct bio_vec bv;struct bvec_iter_all iter_all;bio_for_each_segment_all (bv, bio, iter_all) { -struct page *page = bv->bv_page; +struct page *page = bv.bv_page;page_endio (page,bio_op (bio),blk_status_to_errno (bio->bi_status)); }
fs/squashfs/block.c +27 -21
97 lines not on this page · raw message
fs/squashfs/lz4_wrapper.c +10 -7
@@ -92,20 +92,23 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, struct bio *bio, int offset, int length, struct squashfs_page_actor *output) { - struct bvec_iter_all iter_all = {}; -struct bio_vec *bvec =bvec_init_iter_all (&iter_all); + struct bvec_iter_all iter; +struct bio_vec bvec;struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int bytes = length, res; -while (bio_next_segment (bio, &iter_all)) { -int avail =min (bytes, ((int )bvec->bv_len) - offset); +bvec_iter_all_init (&iter); +bio_iter_all_advance (bio, &iter, offset); - data =bvec_virt (bvec); -memcpy (buff, data + offset, avail); +bio_for_each_segment_all_continue (bvec, bio, iter) { +unsigned avail =min_t (unsigned, bytes, bvec.bv_len); + +memcpy (buff,bvec_virt (&bvec), avail); buff += avail; bytes -= avail; - offset =0 ; +if (!bytes) +break ; } res = LZ4_decompress_safe(stream->input, stream->output,
fs/squashfs/lzo_wrapper.c +10 -7
@@ -66,21 +66,24 @@ static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm, struct bio *bio, int offset, int length, struct squashfs_page_actor *output) { - struct bvec_iter_all iter_all = {}; -struct bio_vec *bvec =bvec_init_iter_all (&iter_all); + struct bvec_iter_all iter; +struct bio_vec bvec;struct squashfs_lzo *stream = strm;void *buff = stream->input, *data;int bytes = length, res;size_t out_len = output->length; -while (bio_next_segment (bio, &iter_all)) { -int avail =min (bytes, ((int )bvec->bv_len) - offset); +bvec_iter_all_init (&iter); +bio_iter_all_advance (bio, &iter, offset); - data =bvec_virt (bvec); -memcpy (buff, data + offset, avail); +bio_for_each_segment_all_continue (bvec, bio, iter) { +unsigned avail =min_t (unsigned, bytes, bvec.bv_len); + +memcpy (buff,bvec_virt (&bvec), avail); buff += avail; bytes -= avail; - offset =0 ; +if (!bytes) +break ; } res = lzo1x_decompress_safe(stream->input, (size_t)length,
fs/squashfs/xz_wrapper.c +10 -9
@@ -120,8 +120,7 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, struct bio *bio, int offset, int length, struct squashfs_page_actor *output) { - struct bvec_iter_all iter_all = {}; -struct bio_vec *bvec =bvec_init_iter_all (&iter_all); + struct bvec_iter_all iter;int total =0 , error =0 ;struct squashfs_xz *stream = strm;
@@ -136,26 +135,28 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, goto finish; } +bvec_iter_all_init (&iter); +bio_iter_all_advance (bio, &iter, offset); +for (;;) {enum xz_ret xz_err;if (stream->buf.in_pos == stream->buf.in_size) { -const void *data; -int avail; +struct bio_vec bvec =bio_iter_all_peek (bio, &iter); +unsigned avail =min_t (unsigned, length, bvec.bv_len); -if (!bio_next_segment (bio, &iter_all)) { +if (iter.idx >= bio->bi_vcnt) {/* XZ_STREAM_END must be reached. */ error = -EIO ;break ; } - avail =min (length, ((int )bvec->bv_len) - offset); - data =bvec_virt (bvec); length -= avail; - stream->buf.in = data + offset; + stream->buf.in =bvec_virt (&bvec); stream->buf.in_size = avail; stream->buf.in_pos =0 ; - offset =0 ; + +bio_iter_all_advance (bio, &iter, avail); }if (stream->buf.out_pos == stream->buf.out_size) {
fs/squashfs/zlib_wrapper.c +10 -8
@@ -53,8 +53,7 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, struct bio *bio, int offset, int length, struct squashfs_page_actor *output) { - struct bvec_iter_all iter_all = {}; -struct bio_vec *bvec =bvec_init_iter_all (&iter_all); + struct bvec_iter_all iter;int zlib_init =0 , error =0 ;z_stream *stream = strm;
@@ -67,25 +66,28 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, goto finish; } +bvec_iter_all_init (&iter); +bio_iter_all_advance (bio, &iter, offset); +for (;;) {int zlib_err;if (stream->avail_in ==0 ) { -const void *data; +struct bio_vec bvec =bio_iter_all_peek (bio, &iter);int avail; -if (!bio_next_segment (bio, &iter_all)) { +if (iter.idx >= bio->bi_vcnt) {/* Z_STREAM_END must be reached. */ error = -EIO ;break ; } - avail =min (length, ((int )bvec->bv_len) - offset); - data =bvec_virt (bvec); + avail =min_t (unsigned, length, bvec.bv_len); length -= avail; - stream->next_in = data + offset; + stream->next_in =bvec_virt (&bvec); stream->avail_in = avail; - offset =0 ; + +bio_iter_all_advance (bio, &iter, avail); }if (stream->avail_out ==0 ) {
fs/squashfs/zstd_wrapper.c +10 -9
@@ -68,8 +68,7 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,int error =0 ;zstd_in_buffer in_buf = {NULL ,0 ,0 };zstd_out_buffer out_buf = {NULL ,0 ,0 }; -struct bvec_iter_all iter_all = {}; -struct bio_vec *bvec =bvec_init_iter_all (&iter_all); +struct bvec_iter_all iter; stream =zstd_init_dstream (wksp->window_size, wksp->mem, wksp->mem_size);
@@ -85,25 +84,27 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, goto finish; } +bvec_iter_all_init (&iter); +bio_iter_all_advance (bio, &iter, offset); +for (;;) {size_t zstd_err;if (in_buf.pos == in_buf.size) { -const void *data; -int avail; +struct bio_vec bvec =bio_iter_all_peek (bio, &iter); +unsigned avail =min_t (unsigned, length, bvec.bv_len); -if (!bio_next_segment (bio, &iter_all)) { +if (iter.idx >= bio->bi_vcnt) { error = -EIO ;break ; } - avail =min (length, ((int )bvec->bv_len) - offset); - data =bvec_virt (bvec); length -= avail; - in_buf.src = data + offset; + in_buf.src =bvec_virt (&bvec); in_buf.size = avail; in_buf.pos =0 ; - offset =0 ; + +bio_iter_all_advance (bio, &iter, avail); }if (out_buf.pos == out_buf.size) {
include/linux/bio.h +26 -8
@@ -78,22 +78,40 @@ static inline void *bio_data(struct bio *bio)return NULL ; } -static inline bool bio_next_segment (const struct bio *bio, -struct bvec_iter_all *iter) +static inline struct bio_vec bio_iter_all_peek (const struct bio *bio, +struct bvec_iter_all *iter) { -if (iter->idx >= bio->bi_vcnt) -return false; +if (WARN_ON (iter->idx >= bio->bi_vcnt)) +return (struct bio_vec ) {NULL }; -bvec_advance (&bio->bi_io_vec[iter->idx], iter); -return true; +return bvec_iter_all_peek (bio->bi_io_vec, iter); +} + +static inline void bio_iter_all_advance (const struct bio *bio, +struct bvec_iter_all *iter, +unsigned bytes) +{ +bvec_iter_all_advance (bio->bi_io_vec, iter, bytes); + +WARN_ON (iter->idx > bio->bi_vcnt || + (iter->idx == bio->bi_vcnt && iter->done)); } +#define bio_for_each_segment_all_continue (bvl, bio, iter) \ + for (; \ + iter.idx < bio->bi_vcnt && \ + ((bvl = bio_iter_all_peek(bio, &iter)), true); \ + bio_iter_all_advance((bio), &iter, bvl.bv_len)) +/* * drivers should _never_ use the all version - the bio may have been split * before it got to the driver and the driver won't own all of it */ -#define bio_for_each_segment_all (bvl, bio, iter) \ - for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); ) +#define bio_for_each_segment_all (bvl, bio, iter) \ + for (bvec_iter_all_init(&iter); \ + iter.idx < (bio)->bi_vcnt && \ + ((bvl = bio_iter_all_peek((bio), &iter)), true); \ + bio_iter_all_advance((bio), &iter, bvl.bv_len))static inline void bio_advance_iter (const struct bio *bio,struct bvec_iter *iter,unsigned int bytes)
include/linux/bvec.h +35 -26
87 lines not on this page · raw message
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 13/32 block: Rework bio_for_each_folio_all()
In reply to Kent OverstreetThis reimplements bio_for_each_folio_all() on top of the newly-reworked bvec_iter_all, and since it's now trivial we also provide bio_for_each_folio.
--- fs/crypto/bio.c | 9 +++-- fs/iomap/buffered-io.c | 14 ++++--- fs/verity/verify.c | 9 +++-- include/linux/bio.h | 91 +++++++++++++++++++++--------------------- include/linux/bvec.h | 15 +++++-- 5 files changed, 75 insertions(+), 63 deletions(-)
fs/crypto/bio.c +5 -4
@@ -30,11 +30,12 @@*/ bool fscrypt_decrypt_bio (struct bio *bio) { -struct folio_iter fi; +struct bvec_iter_all iter; +struct folio_vec fv; -bio_for_each_folio_all (fi, bio) { -int err =fscrypt_decrypt_pagecache_blocks (fi.folio, fi.length, - fi.offset); +bio_for_each_folio_all (fv, bio, iter) { +int err =fscrypt_decrypt_pagecache_blocks (fv.fv_folio, fv.fv_len, + fv.fv_offset);if (err) { bio->bi_status =errno_to_blk_status (err);
fs/iomap/buffered-io.c +8 -6
@@ -187,10 +187,11 @@ static void iomap_finish_folio_read(struct folio *folio, size_t offset,static void iomap_read_end_io (struct bio *bio) {int error =blk_status_to_errno (bio->bi_status); -struct folio_iter fi; +struct bvec_iter_all iter; +struct folio_vec fv; -bio_for_each_folio_all (fi, bio) -iomap_finish_folio_read (fi.folio, fi.offset, fi.length, error); +bio_for_each_folio_all (fv, bio, iter) +iomap_finish_folio_read (fv.fv_folio, fv.fv_offset, fv.fv_len, error);bio_put (bio); }
@@ -1328,7 +1329,8 @@ iomap_finish_ioend(struct iomap_ioend *ioend, int error)u32 folio_count =0 ;for (bio = &ioend->io_inline_bio; bio; bio = next) { -struct folio_iter fi; +struct bvec_iter_all iter; +struct folio_vec fv;/* * For the last bio, bi_private points to the ioend, so we
@@ -1340,8 +1342,8 @@ iomap_finish_ioend(struct iomap_ioend *ioend, int error) next = bio->bi_private;/* walk all folios in bio, ending page IO on them */ -bio_for_each_folio_all (fi, bio) { -iomap_finish_folio_write (inode, fi.folio, fi.length, +bio_for_each_folio_all (fv, bio, iter) { +iomap_finish_folio_write (inode, fv.fv_folio, fv.fv_len, error); folio_count++; }
fs/verity/verify.c +5 -4
@@ -340,7 +340,8 @@ void fsverity_verify_bio(struct bio *bio)struct inode *inode =bio_first_page_all (bio)->mapping->host;struct fsverity_info *vi = inode->i_verity_info;struct ahash_request *req; -struct folio_iter fi; +struct bvec_iter_all iter; +struct folio_vec fv;unsigned long max_ra_pages =0 ;/* This allocation never fails, since it's mempool-backed. */
@@ -359,9 +360,9 @@ void fsverity_verify_bio(struct bio *bio) max_ra_pages = bio->bi_iter.bi_size >> (PAGE_SHIFT +2 ); } -bio_for_each_folio_all (fi, bio) { -if (!verify_data_blocks (inode, vi, req, fi.folio, fi.length, - fi.offset, max_ra_pages)) { +bio_for_each_folio_all (fv, bio, iter) { +if (!verify_data_blocks (inode, vi, req, fv.fv_folio, fv.fv_len, + fv.fv_offset, max_ra_pages)) { bio->bi_status =BLK_STS_IOERR ;break ; }
include/linux/bio.h +45 -46
110 lines not on this page · raw message
include/linux/bvec.h +12 -3
@@ -205,18 +205,27 @@ static inline void bvec_iter_all_init(struct bvec_iter_all *iter_all) iter_all->idx =0 ; } -static inline struct bio_vec bvec_iter_all_peek (const struct bio_vec *bvec, -struct bvec_iter_all *iter) +static inline struct bio_vec __bvec_iter_all_peek (const struct bio_vec *bvec, +const struct bvec_iter_all *iter) {struct bio_vec bv = bvec[iter->idx]; +BUG_ON (iter->done >= bv.bv_len); + bv.bv_offset += iter->done; bv.bv_len -= iter->done; bv.bv_page += bv.bv_offset >>PAGE_SHIFT ; bv.bv_offset &= ~PAGE_MASK ; - bv.bv_len =min_t (unsigned,PAGE_SIZE - bv.bv_offset, bv.bv_len); +return bv; +} + +static inline struct bio_vec bvec_iter_all_peek (const struct bio_vec *bvec, +const struct bvec_iter_all *iter) +{ +struct bio_vec bv =__bvec_iter_all_peek (bvec, iter); + bv.bv_len =min_t (unsigned,PAGE_SIZE - bv.bv_offset, bv.bv_len);return bv; }
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 14/32 block: Don't block on s_umount from __invalidate_super()
In reply to Kent Overstreet__invalidate_super() is used to flush any filesystem mounted on a device, generally on some sort of media change event.
However, when unmounting a filesystem and closing the underlying block devices, we can deadlock if the block driver then calls __invalidate_device() (e.g. because the block device goes away when it is no longer in use).
This happens with bcachefs on top of loopback, and can be triggered by fstests generic/042:
put_super
-> blkdev_put
-> lo_release
-> disk_force_media_change
-> __invalidate_device
-> get_superThis isn't inherently specific to bcachefs - it hasn't shown up with other filesystems before because most other filesystems use the sget() mechanism for opening/closing block devices (and enforcing exclusion), however sget() has its own downsides and weird/sketchy behaviour w.r.t. block device open lifetime - if that ever gets fixed more code will run into this issue.
The __invalidate_device() call here is really a best effort "I just yanked the device for a mounted filesystem, please try not to lose my data" - if it's ever actually needed the user has already done something crazy, and we probably shouldn't make things worse by deadlocking. Switching to a trylock seems in keeping with what the code is trying to do.
If we ever get revoke() at the block layer, perhaps we would look at rearchitecting to use that instead.
--- block/bdev.c | 2 +- fs/super.c | 40 +++++++++++++++++++++++++++++++--------- include/linux/fs.h | 1 + 3 files changed, 33 insertions(+), 10 deletions(-)
block/bdev.c +1 -1
@@ -922,7 +922,7 @@ EXPORT_SYMBOL(lookup_bdev);int __invalidate_device (struct block_device *bdev,bool kill_dirty) { -struct super_block *sb =get_super (bdev); +struct super_block *sb =try_get_super (bdev);int res =0 ;if (sb) {
fs/super.c +31 -9
@@ -791,14 +791,7 @@ void iterate_supers_type(struct file_system_type *type,EXPORT_SYMBOL (iterate_supers_type); -/** -* get_super - get the superblock of a device -* @bdev: device to get the superblock for -* -* Scans the superblock list and finds the superblock of the file system -* mounted on the device given. %NULL is returned if no match is found. -*/ -struct super_block *get_super (struct block_device *bdev) +static struct super_block *__get_super (struct block_device *bdev,bool try) {struct super_block *sb;
@@ -813,7 +806,12 @@ struct super_block *get_super(struct block_device *bdev)if (sb->s_bdev == bdev) { sb->s_count++;spin_unlock (&sb_lock); -down_read (&sb->s_umount); + +if (!try) +down_read (&sb->s_umount); +else if (!down_read_trylock (&sb->s_umount)) +return NULL ; +/* still alive? */ if (sb->s_root && (sb->s_flags &SB_BORN ))return sb;
@@ -828,6 +826,30 @@ struct super_block *get_super(struct block_device *bdev)return NULL ; } +/** +* get_super - get the superblock of a device +* @bdev: device to get the superblock for +* +* Scans the superblock list and finds the superblock of the file system +* mounted on the device given. %NULL is returned if no match is found. +*/ +struct super_block *get_super (struct block_device *bdev) +{ +return __get_super (bdev, false); +} + +/** +* try_get_super - get the superblock of a device, using trylock on sb->s_umount +* @bdev: device to get the superblock for +* +* Scans the superblock list and finds the superblock of the file system +* mounted on the device given. %NULL is returned if no match is found. +*/ +struct super_block *try_get_super (struct block_device *bdev) +{ +return __get_super (bdev, true); +} +/** * get_active_super - get an active reference to the superblock of a device * @bdev: device to get the superblock for
include/linux/fs.h +1 -0
@@ -2878,6 +2878,7 @@ extern struct file_system_type *get_filesystem(struct file_system_type *fs);extern void put_filesystem (struct file_system_type *fs);extern struct file_system_type *get_fs_type (const char *name);extern struct super_block *get_super (struct block_device *); +extern struct super_block *try_get_super (struct block_device *);extern struct super_block *get_active_super (struct block_device *bdev);extern void drop_super (struct super_block *sb);extern void drop_super_exclusive (struct super_block *sb);
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 15/32 bcache: move closures to lib/
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
Prep work for bcachefs - being a fork of bcache it also uses closures
---
drivers/md/bcache/Kconfig | 10 +-----
drivers/md/bcache/Makefile | 4 +--
drivers/md/bcache/bcache.h | 2 +-
drivers/md/bcache/super.c | 1 -
drivers/md/bcache/util.h | 3 +-
.../md/bcache => include/linux}/closure.h | 17 +++++----
lib/Kconfig | 3 ++
lib/Kconfig.debug | 9 +++++
lib/Makefile | 2 ++
{drivers/md/bcache => lib}/closure.c | 35 +++++++++----------
10 files changed, 43 insertions(+), 43 deletions(-)
rename {drivers/md/bcache => include/linux}/closure.h (97%)
rename {drivers/md/bcache => lib}/closure.c (88%)drivers/md/bcache/Kconfig +1 -9
@@ -4,6 +4,7 @@ config BCACHE tristate "Block device as cache" select BLOCK_HOLDER_DEPRECATED if SYSFS select CRC64 + select CLOSURES help Allows a block device to be used as cache for other devices; uses a btree for indexing and the layout is optimized for SSDs.
@@ -19,15 +20,6 @@ config BCACHE_DEBUG Enables extra debugging tools, allows expensive runtime checks to be turned on. -config BCACHE_CLOSURES_DEBUG - bool "Debug closures" - depends on BCACHE - select DEBUG_FS - help - Keeps all active closures in a linked list and provides a debugfs - interface to list them, which makes it possible to see asynchronous - operations that get stuck. - config BCACHE_ASYNC_REGISTRATION bool "Asynchronous device registration" depends on BCACHE
drivers/md/bcache/Makefile +2 -2
@@ -2,6 +2,6 @@ obj-$(CONFIG_BCACHE) += bcache.o -bcache-y := alloc.o bset.o btree.o closure.o debug.o extents.o\ - io.o journal.o movinggc.o request.o stats.o super.o sysfs.o trace.o\ +bcache-y := alloc.o bset.o btree.o debug.o extents.o io.o\ + journal.o movinggc.o request.o stats.o super.o sysfs.o trace.o\ util.o writeback.o features.o
drivers/md/bcache/bcache.h +1 -1
@@ -179,6 +179,7 @@#define pr_fmt (fmt) "bcache: %s() " fmt, __func__#include <linux/bio.h> +#include <linux/closure.h> #include <linux/kobject.h> #include <linux/list.h> #include <linux/mutex.h>
@@ -192,7 +193,6 @@#include "bcache_ondisk.h" #include "bset.h" #include "util.h" -#include "closure.h" struct bucket {atomic_t pin;
drivers/md/bcache/super.c +0 -1
@@ -2912,7 +2912,6 @@ static int __init bcache_init(void) goto err;bch_debug_init (); -closure_debug_init (); bcache_is_reboot = false;
drivers/md/bcache/util.h +1 -2
@@ -4,6 +4,7 @@#define _BCACHE_UTIL_H#include <linux/blkdev.h> +#include <linux/closure.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/sched/clock.h>
@@ -13,8 +14,6 @@#include <linux/workqueue.h> #include <linux/crc64.h> -#include "closure.h" -struct closure ;#ifdef CONFIG_BCACHE_DEBUG
include/linux/closure.h renamed from drivers/md/bcache/closure.h +8 -9
@@ -155,7 +155,7 @@ struct closure {atomic_t remaining; -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES #define CLOSURE_MAGIC_DEAD 0xc054dead#define CLOSURE_MAGIC_ALIVE 0xc054a11e
@@ -184,15 +184,13 @@ static inline void closure_sync(struct closure *cl)__closure_sync (cl); } -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES -void closure_debug_init (void );void closure_debug_create (struct closure *cl);void closure_debug_destroy (struct closure *cl);#else -static inline void closure_debug_init (void ) {}static inline void closure_debug_create (struct closure *cl) {}static inline void closure_debug_destroy (struct closure *cl) {}
@@ -200,21 +198,21 @@ static inline void closure_debug_destroy(struct closure *cl) {}static inline void closure_set_ip (struct closure *cl) { -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES cl->ip = _THIS_IP_;#endif }static inline void closure_set_ret_ip (struct closure *cl) { -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES cl->ip = _RET_IP_;#endif }static inline void closure_set_waiting (struct closure *cl,unsigned long f) { -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES cl->waiting_on = f;#endif }
@@ -243,6 +241,7 @@ static inline void closure_queue(struct closure *cl)*/ BUILD_BUG_ON (offsetof(struct closure , fn) != offsetof(struct work_struct , func)); +if (wq) {INIT_WORK (&cl->work, cl->work.func);BUG_ON (!queue_work (wq, &cl->work));
@@ -255,7 +254,7 @@ static inline void closure_queue(struct closure *cl)*/ static inline void closure_get (struct closure *cl) { -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES BUG_ON ((atomic_inc_return (&cl->remaining) &CLOSURE_REMAINING_MASK ) <=1 );#else
@@ -271,7 +270,7 @@ static inline void closure_get(struct closure *cl)*/ static inline void closure_init (struct closure *cl,struct closure *parent) { -memset (cl,0 ,sizeof (struct closure )); + cl->fn =NULL ; cl->parent = parent;if (parent)closure_get (parent);
lib/Kconfig +3 -0
@@ -504,6 +504,9 @@ config ASSOCIATIVE_ARRAY for more information. +config CLOSURES + bool + config HAS_IOMEM bool depends on !NO_IOMEM
lib/Kconfig.debug +9 -0
@@ -1618,6 +1618,15 @@ config DEBUG_NOTIFIERS This is a relatively cheap check but if you care about maximum performance, say N. +config DEBUG_CLOSURES + bool "Debug closures (bcache async widgits)" + depends on CLOSURES + select DEBUG_FS + help + Keeps all active closures in a linked list and provides a debugfs + interface to list them, which makes it possible to see asynchronous + operations that get stuck. + config BUG_ON_DATA_CORRUPTION bool "Trigger a BUG when data corruption is detected" select DEBUG_LIST
lib/Makefile +2 -0
@@ -245,6 +245,8 @@ obj-$(CONFIG_ATOMIC64_SELFTEST) += atomic64_test.o obj-$(CONFIG_CPU_RMAP) += cpu_rmap.o +obj-$(CONFIG_CLOSURES) += closure.o + obj-$(CONFIG_DQL) += dynamic_queue_limits.o obj-$(CONFIG_GLOB) += glob.o
lib/closure.c renamed from drivers/md/bcache/closure.c +16 -19
@@ -6,13 +6,12 @@* Copyright 2012 Google, Inc. */ +#include <linux/closure.h> #include <linux/debugfs.h> -#include <linux/module.h> +#include <linux/export.h> #include <linux/seq_file.h> #include <linux/sched/debug.h> -#include "closure.h" -static inline void closure_put_after_sub (struct closure *cl,int flags) {int r = flags &CLOSURE_REMAINING_MASK ;
@@ -45,6 +44,7 @@ void closure_sub(struct closure *cl, int v) {closure_put_after_sub (cl,atomic_sub_return (v, &cl->remaining)); } +EXPORT_SYMBOL (closure_sub);/* * closure_put - decrement a closure's refcount
@@ -53,6 +53,7 @@ void closure_put(struct closure *cl) {closure_put_after_sub (cl,atomic_dec_return (&cl->remaining)); } +EXPORT_SYMBOL (closure_put);/* * closure_wake_up - wake up all closures on a wait list, without memory barrier
@@ -74,6 +75,7 @@ void __closure_wake_up(struct closure_waitlist *wait_list)closure_sub (cl,CLOSURE_WAITING +1 ); } } +EXPORT_SYMBOL (__closure_wake_up);/** * closure_wait - add a closure to a waitlist
@@ -93,6 +95,7 @@ bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)return true; } +EXPORT_SYMBOL (closure_wait);struct closure_syncer {struct task_struct *task;
@@ -127,8 +130,9 @@ void __sched __closure_sync(struct closure *cl)__set_current_state (TASK_RUNNING ); } +EXPORT_SYMBOL (__closure_sync); -#ifdef CONFIG_BCACHE_CLOSURES_DEBUG +#ifdef CONFIG_DEBUG_CLOSURES static LIST_HEAD (closure_list);static DEFINE_SPINLOCK (closure_list_lock);
@@ -144,6 +148,7 @@ void closure_debug_create(struct closure *cl)list_add (&cl->all, &closure_list);spin_unlock_irqrestore (&closure_list_lock, flags); } +EXPORT_SYMBOL (closure_debug_create);void closure_debug_destroy (struct closure *cl) {
@@ -156,8 +161,7 @@ void closure_debug_destroy(struct closure *cl)list_del (&cl->all);spin_unlock_irqrestore (&closure_list_lock, flags); } - -static struct dentry *closure_debug; +EXPORT_SYMBOL (closure_debug_destroy);static int debug_show (struct seq_file *f,void *data) {
@@ -181,7 +185,7 @@ static int debug_show(struct seq_file *f, void *data)seq_printf (f," W %pS\n" , (void *) cl->waiting_on); -seq_printf (f,"\n" ); +seq_puts (f,"\n" ); }spin_unlock_irq (&closure_list_lock);
@@ -190,18 +194,11 @@ static int debug_show(struct seq_file *f, void *data)DEFINE_SHOW_ATTRIBUTE (debug); -void __init closure_debug_init (void ) +static int __init closure_debug_init (void ) { -if (!IS_ERR_OR_NULL (bcache_debug)) -/* -* it is unnecessary to check return value of -* debugfs_create_file(), we should not care -* about this. -*/ - closure_debug =debugfs_create_file ( -"closures" ,0400 , bcache_debug,NULL , &debug_fops); +debugfs_create_file ("closures" ,0400 ,NULL ,NULL , &debug_fops); +return 0 ; } -#endif +late_initcall(closure_debug_init) -MODULE_AUTHOR ("Kent Overstreet <[email protected]>" ); -MODULE_LICENSE ("GPL" ); +#endif
Signature · 1 line
2.40.1
Re: [PATCH 15/32] bcache: move closures to lib/
Quoted 6 lines · Kent Overstreet
lib/Kconfig.debug +0
-0@@ -1618,6 +1618,15 @@ config DEBUG_NOTIFIERS This is a relatively cheap check but if you care about maximum
lib/Kconfig.debug config DEBUG_NOTIFIERS · line 1628
Quoted 10 lines
lib/Kconfig.debug +8
-0performance, say N. +config DEBUG_CLOSURES + bool "Debug closures (bcache async widgits)" + depends on CLOSURES + select DEBUG_FS + help + Keeps all active closures in a linked list and provides a debugfs + interface to list them, which makes it possible to see asynchronous + operations that get stuck.
According to coding-style.rst, the help text (3 lines above) should be indented with 2 additional spaces.
Quoted 4 lines
lib/Kconfig.debug +4
-0+ help + Keeps all active closures in a linked list and provides a debugfs + interface to list them, which makes it possible to see asynchronous + operations that get stuck.
Signature · 1 line
~Randy
Kent Overstreet
maintainerPATCH 16/32 MAINTAINERS: Add entry for closures
In reply to Kent Overstreetclosures, from bcache, are async widgets with a variety of uses. bcachefs also uses them, so they're being moved to lib/; mark them as maintained.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -5044,6 +5044,14 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core F: Documentation/devicetree/bindings/timer/ F: drivers/clocksource/ +CLOSURES: +M: Kent Overstreet <[email protected]> +L: [email protected] +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/closure.h +F: lib/closure.c + CMPC ACPI DRIVER M: Thadeu Lima de Souza Cascardo <[email protected]> M: Daniel Oliveira Nascimento <[email protected]>
Signature · 1 line
2.40.1
Re: [PATCH 16/32] MAINTAINERS: Add entry for closures
Quoted 10 lines
2023年5月10日 00:56,Kent Overstreet <[email protected]> 写道:
closures, from bcache, are async widgets with a variety of uses. bcachefs also uses them, so they're being moved to
lib/; mark them as maintained.Signed-off-by Kent Overstreet <[email protected]>Cc Coly Li <[email protected]>
Thanks.
Coly Li
Quoted 26 lines
---
MAINTAINERS| 8 ++++++++1 file changed, 8 insertions(+)MAINTAINERS +0
-0@@ -5044,6 +5044,14 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/coreF:
Documentation/devicetree/bindings/timer/
F:drivers/clocksource/MAINTAINERS +8
-0+CLOSURES: +M: Kent Overstreet <[email protected]> +L: [email protected] +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/closure.h +F: lib/closure.c +CMPC ACPI DRIVER
M: Thadeu Lima de Souza Cascardo <[email protected]>
M: Daniel Oliveira Nascimento <[email protected]>Signature · 1 line
2.40.1
Re: [PATCH 16/32] MAINTAINERS: Add entry for closures
In reply to Kent OverstreetQuoted 15 lines · Kent Overstreet
closures, from bcache, are async widgets with a variety of uses. bcachefs also uses them, so they're being moved to
lib/; mark them as maintained.Signed-off-by Kent Overstreet <[email protected]>Cc Coly Li <[email protected]>--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)MAINTAINERS +0
-0
MAINTAINERS T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core · line 5047
Quoted 4 lines
MAINTAINERS +1
-0@@ -5044,6 +5044,14 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/core F: Documentation/devicetree/bindings/timer/ F: drivers/clocksource/ +CLOSURES:
No colon at the end of the line.
Quoted 10 lines
MAINTAINERS +7
-0+M: Kent Overstreet <[email protected]> +L: [email protected] +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/closure.h +F: lib/closure.c + CMPC ACPI DRIVER M: Thadeu Lima de Souza Cascardo <[email protected]> M: Daniel Oliveira Nascimento <[email protected]>
Signature · 1 line
~Randy
Kent Overstreet
maintainerPATCH 17/32 closures: closure_wait_event()
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
Like wait_event() - except, because it uses closures and closure waitlists it doesn't have the restriction on modifying task state inside the condition check, like wait_event() does.
--- include/linux/closure.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/linux/closure.h +22 -0
@@ -374,4 +374,26 @@ static inline void closure_call(struct closure *cl, closure_fn fn, continue_at_nobarrier(cl, fn, wq); } +#define __closure_wait_event (waitlist, _cond) \ +do { \ + struct closure cl; \ + \ + closure_init_stack(&cl); \ + \ + while (1) { \ + closure_wait(waitlist, &cl); \ + if (_cond) \ + break; \ + closure_sync(&cl); \ + } \ + closure_wake_up(waitlist); \ + closure_sync(&cl); \ +} while (0) + +#define closure_wait_event (waitlist, _cond) \ +do { \ + if (!(_cond)) \ + __closure_wait_event(waitlist, _cond); \ +} while (0) +#endif /* _LINUX_CLOSURE_H */
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 18/32 closures: closure_nr_remaining()
In reply to Kent OverstreetFactor out a new helper, which returns the number of events outstanding.
--- include/linux/closure.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/closure.h +6 -1
@@ -172,6 +172,11 @@ void __closure_wake_up(struct closure_waitlist *list);bool closure_wait (struct closure_waitlist *list,struct closure *cl);void __closure_sync (struct closure *cl); +static inline unsigned closure_nr_remaining (struct closure *cl) +{ +return atomic_read (&cl->remaining) &CLOSURE_REMAINING_MASK ; +} +/** * closure_sync - sleep until a closure a closure has nothing left to wait on *
@@ -180,7 +185,7 @@ void __closure_sync(struct closure *cl);*/ static inline void closure_sync (struct closure *cl) { -if ((atomic_read (&cl->remaining) &CLOSURE_REMAINING_MASK ) !=1 ) +if (closure_nr_remaining (cl) !=1 )__closure_sync (cl); }
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 19/32 closures: Add a missing include
In reply to Kent OverstreetFixes building in userspace.
--- lib/closure.c | 1 + 1 file changed, 1 insertion(+)
lib/closure.c +1 -0
@@ -9,6 +9,7 @@#include <linux/closure.h> #include <linux/debugfs.h> #include <linux/export.h> +#include <linux/rcupdate.h> #include <linux/seq_file.h> #include <linux/sched/debug.h>
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 20/32 vfs: factor out inode hash head calculation
In reply to Kent OverstreetFrom: Dave Chinner <[email protected]>
In preparation for changing the inode hash table implementation.
--- fs/inode.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-)
fs/inode.c +25 -19
@@ -60,6 +60,22 @@ static unsigned int i_hash_shift __read_mostly;static struct hlist_head *inode_hashtable__read_mostly ;static __cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_hash_lock ); +static unsigned long hash (struct super_block *sb,unsigned long hashval) +{ +unsigned long tmp; + + tmp = (hashval * (unsigned long )sb) ^ (GOLDEN_RATIO_PRIME + hashval) / +L1_CACHE_BYTES ; + tmp = tmp ^ ((tmp ^GOLDEN_RATIO_PRIME ) >> i_hash_shift); +return tmp & i_hash_mask; +} + +static inline struct hlist_head *i_hash_head (struct super_block *sb, +unsigned int hashval) +{ +return inode_hashtable +hash (sb, hashval); +} +/* * Empty aops. Can be used for the cases where the user does not * define any of the address_space operations.
@@ -506,16 +522,6 @@ static inline void inode_sb_list_del(struct inode *inode) } } -static unsigned long hash (struct super_block *sb,unsigned long hashval) -{ -unsigned long tmp; - - tmp = (hashval * (unsigned long )sb) ^ (GOLDEN_RATIO_PRIME + hashval) / -L1_CACHE_BYTES ; - tmp = tmp ^ ((tmp ^GOLDEN_RATIO_PRIME ) >> i_hash_shift); -return tmp & i_hash_mask; -} -/** * __insert_inode_hash - hash an inode * @inode: unhashed inode
@@ -1163,7 +1169,7 @@ struct inode *inode_insert5(struct inode *inode, unsigned long hashval,int (*test)(struct inode *,void *), int (*set)(struct inode *, void *), void *data) { - struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); + struct hlist_head *head = i_hash_head(inode->i_sb, hashval);struct inode *old; again:
@@ -1267,7 +1273,7 @@ EXPORT_SYMBOL(iget5_locked);*/ struct inode *iget_locked (struct super_block *sb,unsigned long ino) { -struct hlist_head *head = inode_hashtable +hash (sb, ino); +struct hlist_head *head =i_hash_head (sb, ino);struct inode *inode; again:spin_lock (&inode_hash_lock);
@@ -1335,7 +1341,7 @@ EXPORT_SYMBOL(iget_locked);*/ static int test_inode_iunique (struct super_block *sb,unsigned long ino) { -struct hlist_head *b = inode_hashtable +hash (sb, ino); +struct hlist_head *b =i_hash_head (sb, ino);struct inode *inode;hlist_for_each_entry_rcu (inode, b, i_hash) {
@@ -1422,7 +1428,7 @@ EXPORT_SYMBOL(igrab);struct inode *ilookup5_nowait (struct super_block *sb,unsigned long hashval,int (*test)(struct inode *,void *),void *data) { -struct hlist_head *head = inode_hashtable +hash (sb, hashval); +struct hlist_head *head =i_hash_head (sb, hashval);struct inode *inode;spin_lock (&inode_hash_lock);
@@ -1477,7 +1483,7 @@ EXPORT_SYMBOL(ilookup5);*/ struct inode *ilookup (struct super_block *sb,unsigned long ino) { -struct hlist_head *head = inode_hashtable +hash (sb, ino); +struct hlist_head *head =i_hash_head (sb, ino);struct inode *inode; again:spin_lock (&inode_hash_lock);
@@ -1526,7 +1532,7 @@ struct inode *find_inode_nowait(struct super_block *sb, void *), void *data) { -struct hlist_head *head = inode_hashtable +hash (sb, hashval); +struct hlist_head *head =i_hash_head (sb, hashval);struct inode *inode, *ret_inode =NULL ;int mval;
@@ -1571,7 +1577,7 @@ EXPORT_SYMBOL(find_inode_nowait);struct inode *find_inode_rcu (struct super_block *sb,unsigned long hashval,int (*test)(struct inode *,void *),void *data) { -struct hlist_head *head = inode_hashtable +hash (sb, hashval); +struct hlist_head *head =i_hash_head (sb, hashval);struct inode *inode; RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
@@ -1609,7 +1615,7 @@ EXPORT_SYMBOL(find_inode_rcu);struct inode *find_inode_by_ino_rcu (struct super_block *sb,unsigned long ino) { -struct hlist_head *head = inode_hashtable +hash (sb, ino); +struct hlist_head *head =i_hash_head (sb, ino);struct inode *inode; RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
@@ -1629,7 +1635,7 @@ int insert_inode_locked(struct inode *inode) {struct super_block *sb = inode->i_sb;ino_t ino = inode->i_ino; -struct hlist_head *head = inode_hashtable +hash (sb, ino); +struct hlist_head *head =i_hash_head (sb, ino);while (1 ) {struct inode *old =NULL ;
Signature · 1 line
2.40.1
Re: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 3 lines · Kent Overstreet
In preparation for changing the inode hash table implementation.
This is interesting completely independent of bcachefs so we should give it some testing.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash
[20/32] vfs: factor out inode hash head calculation
https://git.kernel.org/vfs/vfs/c/b54a4516146d
Re: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 16 lines · Christian Brauner
Quoted 3 lines · Kent Overstreet
In preparation for changing the inode hash table implementation.
This is interesting completely independent of bcachefs so we should give it some testing.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
Quoted 8 lines
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash[20/32] vfs: factor out inode hash head calculation
https://git.kernel.org/vfs/vfs/c/b54a4516146d
Hi Christian - I suspect you should pull the latest version of these patches from:
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git vfs-scale
The commit messages are more recent and complete, and I've been testing the branch in all my test kernels since 6.4-rc1 without issues.
There's also the dlist-lock stuff for avoiding s_inode_list_lock contention in that branch. Once the global hash lock is removed, the s_inode_list_lock is the only global lock in the inode instantiation and reclaim paths. It nests inside the hash locks, so all the contention is currently taken on the hash locks - remove the global hash locks and we just contend on the next global cache line and the workload doesn't go any faster.
i.e. to see the full benefit of the inode hash lock contention reduction, we also need the sb->s_inode_list_lock contention to be fixed....
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 8 lines · Dave Chinner
Hi Christian - I suspect you should pull the latest version of these patches from:
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.gitvfs-scaleThe commit messages are more recent and complete, and I've been testing the branch in all my test kernels since 6.4-rc1 without issues.
Can you please send the series to linux-fsdevel for review?
Re: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Dave Chinner
Hi Christian - I suspect you should pull the latest version of these patches from:
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.gitvfs-scaleThe commit messages are more recent and complete, and I've been testing the branch in all my test kernels since 6.4-rc1 without issues.
Can you please send the series to linux-fsdevel for review?
When it gets back to the top of my priority pile. Last time I sent it there was zero interest in reviewing it from fs/vfs developers but it attracted lots of obnoxious shouting from some RTPREEMPT people about using bit locks. If there's interest in getting it merged, then I can add it to my backlog of stuff to do...
As it is, I'm buried layers deep right now, so I really have no bandwidth to deal with this in the foreseeable future. The code is there, it works just fine, if you want to push it through the process of getting it merged, you're more than welcome to do so.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Christian Brauner
maintainerRe: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 11 lines · Dave Chinner
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Dave Chinner
Hi Christian - I suspect you should pull the latest version of these patches from:
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.gitvfs-scaleThe commit messages are more recent and complete, and I've been testing the branch in all my test kernels since 6.4-rc1 without issues.
Quoted 6 lines
Quoted 1 line
Can you please send the series to linux-fsdevel for review?
When it gets back to the top of my priority pile. Last time I sent it there was zero interest in reviewing it from fs/vfs developers but it attracted lots of obnoxious shouting from some RTPREEMPT people about using bit locks. If there's interest in getting it
I think there is given that it seems to have nice perf gains.
Quoted 6 lines
merged, then I can add it to my backlog of stuff to do...
As it is, I'm buried layers deep right now, so I really have no bandwidth to deal with this in the foreseeable future. The code is there, it works just fine, if you want to push it through the process of getting it merged, you're more than welcome to do so.
I'm here to help get more review done and pick stuff up. I won't be able to do it without additional reviewers such as Christoph helping of course as this isn't a one-man show.
Let's see if we can get this reviewed. If you have the bandwith to send it to fsdevel that'd be great. If it takes you a while to get back to it then that's fine too.
Kent Overstreet
maintainerRe: (subset) [PATCH 20/32] vfs: factor out inode hash head calculation
Quoted 7 lines · Christian Brauner
I'm here to help get more review done and pick stuff up. I won't be able to do it without additional reviewers such as Christoph helping of course as this isn't a one-man show.
Let's see if we can get this reviewed. If you have the bandwith to send it to fsdevel that'd be great. If it takes you a while to get back to it then that's fine too.
These patches really should have my reviewed-by on them already, I stared at them quite a bit (and fs/inode.c in general) awhile back.
(I was attempting to convert fs/inode.c to an rhashtable up until I realized the inode lifetime rules are completely insane, so when I saw Dave's much simpler approach I was _more_ than happy to not have to contemplate that mess anymore...)
Kent Overstreet
maintainerPATCH 21/32 hlist-bl: add hlist_bl_fake()
In reply to Kent OverstreetFrom: Dave Chinner <[email protected]>
in preparation for switching the VFS inode cache over the hlist_bl lists, we nee dto be able to fake a list node that looks like it is hased for correct operation of filesystems that don't directly use the VFS indoe cache.
--- include/linux/list_bl.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/linux/list_bl.h +22 -0
@@ -143,6 +143,28 @@ static inline void hlist_bl_del_init(struct hlist_bl_node *n) } } +/** +* hlist_bl_add_fake - create a fake list consisting of a single headless node +* @n: Node to make a fake list out of +* +* This makes @n appear to be its own predecessor on a headless hlist. +* The point of this is to allow things like hlist_bl_del() to work correctly +* in cases where there is no list. +*/ +static inline void hlist_bl_add_fake (struct hlist_bl_node *n) +{ + n->pprev = &n->next; +} + +/** +* hlist_fake: Is this node a fake hlist_bl? +* @h: Node to check for being a self-referential fake hlist. +*/ +static inline bool hlist_bl_fake (struct hlist_bl_node *n) +{ +return n->pprev == &n->next; +} +static inline void hlist_bl_lock (struct hlist_bl_head *b) {bit_spin_lock (0 , (unsigned long *)b);
Signature · 1 line
2.40.1
Re: [PATCH 21/32] hlist-bl: add hlist_bl_fake()
Quoted 3 lines · Kent Overstreet
From: Dave Chinner <[email protected]>
in preparation for switching the VFS inode cache over the
hlist_bl
In
Quoted 1 line
lists, we nee dto be able to fake a list node that looks like it is
need to
Quoted 1 line
hased for correct operation of filesystems that don't directly use
hashed
Quoted 1 line
the VFS indoe cache.
inode cache hash index.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: (subset) [PATCH 21/32] hlist-bl: add hlist_bl_fake()
In reply to Kent OverstreetQuoted 6 lines · Kent Overstreet
in preparation for switching the VFS inode cache over the
hlist_bllists, we nee dto be able to fake a list node that looks like it is hased for correct operation of filesystems that don't directly use the VFS indoe cache.
This is interesting completely independent of bcachefs so we should give it some testing.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash
[21/32] hlist-bl: add hlist_bl_fake()
https://git.kernel.org/vfs/vfs/c/0ef99590b01f
Kent Overstreet
maintainerPATCH 22/32 vfs: inode cache conversion to hash-bl
In reply to Kent OverstreetFrom: Dave Chinner <[email protected]>
Because scalability of the global inode_hash_lock really, really sucks.
32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k] ext4_create
- 52.09% ext4_create
- 41.03% __ext4_new_inode
- 29.92% insert_inode_locked
- 25.35% _raw_spin_lock
- do_raw_spin_lock
- 24.97% __pv_queued_spin_lock_slowpath
- 72.33% 0.02% [kernel] [k] do_filp_open
- 72.31% do_filp_open
- 72.28% path_openat
- 57.03% bch2_create
- 56.46% __bch2_create
- 40.43% inode_insert5
- 36.07% _raw_spin_lock
- do_raw_spin_lock
35.86% __pv_queued_spin_lock_slowpath
4.02% find_inode
Convert the inode hash table to a RCU-aware hash-bl table just like the dentry cache. Note that we need to store a pointer to the hlist_bl_head the inode has been added to in the inode so that when it comes to unhash the inode we know what list to lock. We need to do this because the hash value that is used to hash the inode is generated from the inode itself - filesystems can provide this themselves so we have to either store the hash or the head pointer in the inode to be able to find the right list head for removal...
Same workload after:
--- fs/inode.c | 200 ++++++++++++++++++++++++++++----------------- include/linux/fs.h | 9 +- 2 files changed, 132 insertions(+), 77 deletions(-)
fs/inode.c +127 -73
@@ -57,8 +57,7 @@static unsigned int i_hash_mask__read_mostly ;static unsigned int i_hash_shift__read_mostly ; -static struct hlist_head *inode_hashtable__read_mostly ; -static __cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_hash_lock ); +static struct hlist_bl_head *inode_hashtable__read_mostly ;static unsigned long hash (struct super_block *sb,unsigned long hashval) {
@@ -70,7 +69,7 @@ static unsigned long hash(struct super_block *sb, unsigned long hashval)return tmp & i_hash_mask; } -static inline struct hlist_head *i_hash_head (struct super_block *sb, +static inline struct hlist_bl_head *i_hash_head (struct super_block *sb,unsigned int hashval) {return inode_hashtable +hash (sb, hashval);
@@ -433,7 +432,7 @@ EXPORT_SYMBOL(address_space_init_once);void inode_init_once (struct inode *inode) {memset (inode,0 ,sizeof (*inode)); -INIT_HLIST_NODE (&inode->i_hash); +INIT_HLIST_BL_NODE (&inode->i_hash);INIT_LIST_HEAD (&inode->i_devices);INIT_LIST_HEAD (&inode->i_io_list);INIT_LIST_HEAD (&inode->i_wb_list);
@@ -522,6 +521,17 @@ static inline void inode_sb_list_del(struct inode *inode) } } +/* +* Ensure that we store the hash head in the inode when we insert the inode into +* the hlist_bl_head... +*/ +static inline void +__insert_inode_hash_head (struct inode *inode,struct hlist_bl_head *b) +{ +hlist_bl_add_head_rcu (&inode->i_hash, b); + inode->i_hash_head = b; +} +/** * __insert_inode_hash - hash an inode * @inode: unhashed inode
@@ -532,13 +542,13 @@ static inline void inode_sb_list_del(struct inode *inode)*/ void __insert_inode_hash (struct inode *inode,unsigned long hashval) { -struct hlist_head *b = inode_hashtable +hash (inode->i_sb, hashval); +struct hlist_bl_head *b =i_hash_head (inode->i_sb, hashval); -spin_lock (&inode_hash_lock); +hlist_bl_lock (b);spin_lock (&inode->i_lock); -hlist_add_head_rcu (&inode->i_hash, b); +__insert_inode_hash_head (inode, b);spin_unlock (&inode->i_lock); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b); }EXPORT_SYMBOL (__insert_inode_hash);
@@ -550,11 +560,44 @@ EXPORT_SYMBOL(__insert_inode_hash);*/ void __remove_inode_hash (struct inode *inode) { -spin_lock (&inode_hash_lock); -spin_lock (&inode->i_lock); -hlist_del_init_rcu (&inode->i_hash); -spin_unlock (&inode->i_lock); -spin_unlock (&inode_hash_lock); +struct hlist_bl_head *b = inode->i_hash_head; + +/* +* There are some callers that come through here without synchronisation +* and potentially with multiple references to the inode. Hence we have +* to handle the case that we might race with a remove and insert to a +* different list. Coda, in particular, seems to have a userspace API +* that can directly trigger "unhash/rehash to different list" behaviour +* without any serialisation at all. +* +* Hence we have to handle the situation where the inode->i_hash_head +* might point to a different list than what we expect, indicating that +* we raced with another unhash and potentially a new insertion. This +* means we have to retest the head once we have everything locked up +* and loop again if it doesn't match. +*/ +while (b) { +hlist_bl_lock (b); +spin_lock (&inode->i_lock); +if (b != inode->i_hash_head) { +hlist_bl_unlock (b); + b = inode->i_hash_head; +spin_unlock (&inode->i_lock); +continue ; + } +/* +* Need to set the pprev pointer to NULL after list removal so +* that both RCU traversals and hlist_bl_unhashed() work +* correctly at this point. +*/ +hlist_bl_del_rcu (&inode->i_hash); + inode->i_hash.pprev =NULL ; + inode->i_hash_head =NULL ; +spin_unlock (&inode->i_lock); +hlist_bl_unlock (b); +break ; + } + }EXPORT_SYMBOL (__remove_inode_hash);
@@ -904,26 +947,28 @@ long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)return freed; } -static void __wait_on_freeing_inode (struct inode *inode); +static void __wait_on_freeing_inode (struct hlist_bl_head *b, +struct inode *inode);/* * Called with the inode lock held. */ static struct inode *find_inode (struct super_block *sb, -struct hlist_head *head, +struct hlist_bl_head *b,int (*test)(struct inode *,void *),void *data) { +struct hlist_bl_node *node;struct inode *inode =NULL ; repeat: -hlist_for_each_entry (inode, head, i_hash) { +hlist_bl_for_each_entry (inode, node, b, i_hash) {if (inode->i_sb != sb)continue ;if (!test (inode, data))continue ;spin_lock (&inode->i_lock);if (inode->i_state & (I_FREEING |I_WILL_FREE )) { -__wait_on_freeing_inode (inode); +__wait_on_freeing_inode (b, inode); goto repeat; }if (unlikely (inode->i_state &I_CREATING )) {
@@ -942,19 +987,20 @@ static struct inode *find_inode(struct super_block *sb,* iget_locked for details. */ static struct inode *find_inode_fast (struct super_block *sb, -struct hlist_head *head,unsigned long ino) +struct hlist_bl_head *b,unsigned long ino) { +struct hlist_bl_node *node;struct inode *inode =NULL ; repeat: -hlist_for_each_entry (inode, head, i_hash) { +hlist_bl_for_each_entry (inode, node, b, i_hash) {if (inode->i_ino != ino)continue ;if (inode->i_sb != sb)continue ;spin_lock (&inode->i_lock);if (inode->i_state & (I_FREEING |I_WILL_FREE )) { -__wait_on_freeing_inode (inode); +__wait_on_freeing_inode (b, inode); goto repeat; }if (unlikely (inode->i_state &I_CREATING )) {
@@ -1162,25 +1208,25 @@ EXPORT_SYMBOL(unlock_two_nondirectories);* return it locked, hashed, and with the I_NEW flag set. The file system gets * to fill it in before unlocking it via unlock_new_inode(). * -* Note both @test and @set are called with the inode_hash_lock held, so can't -* sleep. +* Note both @test and @set are called with the inode hash chain lock held, +* so can't sleep. */ struct inode *inode_insert5 (struct inode *inode,unsigned long hashval,int (*test)(struct inode *,void *),int (*set)(struct inode *,void *),void *data) { -struct hlist_head *head =i_hash_head (inode->i_sb, hashval); +struct hlist_bl_head *b =i_hash_head (inode->i_sb, hashval);struct inode *old; again: -spin_lock (&inode_hash_lock); - old =find_inode (inode->i_sb, head, test, data); +hlist_bl_lock (b); + old =find_inode (inode->i_sb, b, test, data);if (unlikely (old)) {/* * Uhhuh, somebody else created the same inode under us. * Use the old inode instead of the preallocated one. */ -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);if (IS_ERR (old))return NULL ;wait_on_inode (old);
@@ -1202,7 +1248,7 @@ struct inode *inode_insert5(struct inode *inode, unsigned long hashval,*/ spin_lock (&inode->i_lock); inode->i_state |=I_NEW ; -hlist_add_head_rcu (&inode->i_hash, head); +__insert_inode_hash_head (inode, b);spin_unlock (&inode->i_lock);/*
@@ -1212,7 +1258,7 @@ struct inode *inode_insert5(struct inode *inode, unsigned long hashval,if (list_empty (&inode->i_sb_list))inode_sb_list_add (inode); unlock: -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);return inode; }
@@ -1273,12 +1319,12 @@ EXPORT_SYMBOL(iget5_locked);*/ struct inode *iget_locked (struct super_block *sb,unsigned long ino) { -struct hlist_head *head =i_hash_head (sb, ino); +struct hlist_bl_head *b =i_hash_head (sb, ino);struct inode *inode; again: -spin_lock (&inode_hash_lock); - inode =find_inode_fast (sb, head, ino); -spin_unlock (&inode_hash_lock); +hlist_bl_lock (b); + inode =find_inode_fast (sb, b, ino); +hlist_bl_unlock (b);if (inode) {if (IS_ERR (inode))return NULL ;
@@ -1294,17 +1340,17 @@ struct inode *iget_locked(struct super_block *sb, unsigned long ino)if (inode) {struct inode *old; -spin_lock (&inode_hash_lock); +hlist_bl_lock (b);/* We released the lock, so.. */ - old =find_inode_fast (sb, head, ino); + old =find_inode_fast (sb, b, ino);if (!old) { inode->i_ino = ino;spin_lock (&inode->i_lock); inode->i_state =I_NEW ; -hlist_add_head_rcu (&inode->i_hash, head); +__insert_inode_hash_head (inode, b);spin_unlock (&inode->i_lock);inode_sb_list_add (inode); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);/* Return the locked inode with I_NEW set, the * caller is responsible for filling in the contents
@@ -1317,7 +1363,7 @@ struct inode *iget_locked(struct super_block *sb, unsigned long ino)* us. Use the old inode instead of the one we just * allocated. */ -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);destroy_inode (inode);if (IS_ERR (old))return NULL ;
@@ -1341,10 +1387,11 @@ EXPORT_SYMBOL(iget_locked);*/ static int test_inode_iunique (struct super_block *sb,unsigned long ino) { -struct hlist_head *b =i_hash_head (sb, ino); +struct hlist_bl_head *b =i_hash_head (sb, ino); +struct hlist_bl_node *node;struct inode *inode; -hlist_for_each_entry_rcu (inode, b, i_hash) { +hlist_bl_for_each_entry_rcu (inode, node, b, i_hash) {if (inode->i_ino == ino && inode->i_sb == sb)return 0 ; }
@@ -1428,12 +1475,12 @@ EXPORT_SYMBOL(igrab);struct inode *ilookup5_nowait (struct super_block *sb,unsigned long hashval,int (*test)(struct inode *,void *),void *data) { -struct hlist_head *head =i_hash_head (sb, hashval); +struct hlist_bl_head *b =i_hash_head (sb, hashval);struct inode *inode; -spin_lock (&inode_hash_lock); - inode =find_inode (sb, head, test, data); -spin_unlock (&inode_hash_lock); +hlist_bl_lock (b); + inode =find_inode (sb, b, test, data); +hlist_bl_unlock (b);return IS_ERR (inode) ?NULL : inode; }
@@ -1483,12 +1530,12 @@ EXPORT_SYMBOL(ilookup5);*/ struct inode *ilookup (struct super_block *sb,unsigned long ino) { -struct hlist_head *head =i_hash_head (sb, ino); +struct hlist_bl_head *b =i_hash_head (sb, ino);struct inode *inode; again: -spin_lock (&inode_hash_lock); - inode =find_inode_fast (sb, head, ino); -spin_unlock (&inode_hash_lock); +hlist_bl_lock (b); + inode =find_inode_fast (sb, b, ino); +hlist_bl_unlock (b);if (inode) {if (IS_ERR (inode))
@@ -1532,12 +1579,13 @@ struct inode *find_inode_nowait(struct super_block *sb, void *), void *data) { -struct hlist_head *head =i_hash_head (sb, hashval); +struct hlist_bl_head *b =i_hash_head (sb, hashval); +struct hlist_bl_node *node;struct inode *inode, *ret_inode =NULL ;int mval; -spin_lock (&inode_hash_lock); -hlist_for_each_entry (inode, head, i_hash) { +hlist_bl_lock (b); +hlist_bl_for_each_entry (inode, node, b, i_hash) {if (inode->i_sb != sb)continue ; mval =match (inode, hashval, data);
@@ -1548,7 +1596,7 @@ struct inode *find_inode_nowait(struct super_block *sb, goto out; } out: -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);return ret_inode; }EXPORT_SYMBOL (find_inode_nowait);
@@ -1577,13 +1625,14 @@ EXPORT_SYMBOL(find_inode_nowait);struct inode *find_inode_rcu (struct super_block *sb,unsigned long hashval,int (*test)(struct inode *,void *),void *data) { -struct hlist_head *head =i_hash_head (sb, hashval); +struct hlist_bl_head *b =i_hash_head (sb, hashval); +struct hlist_bl_node *node;struct inode *inode;RCU_LOCKDEP_WARN (!rcu_read_lock_held (),"suspicious find_inode_rcu() usage" ); -hlist_for_each_entry_rcu (inode, head, i_hash) { +hlist_bl_for_each_entry_rcu (inode, node, b, i_hash) {if (inode->i_sb == sb && !(READ_ONCE (inode->i_state) & (I_FREEING |I_WILL_FREE )) &&test (inode, data))
@@ -1615,13 +1664,14 @@ EXPORT_SYMBOL(find_inode_rcu);struct inode *find_inode_by_ino_rcu (struct super_block *sb,unsigned long ino) { -struct hlist_head *head =i_hash_head (sb, ino); +struct hlist_bl_head *b =i_hash_head (sb, ino); +struct hlist_bl_node *node;struct inode *inode;RCU_LOCKDEP_WARN (!rcu_read_lock_held (),"suspicious find_inode_by_ino_rcu() usage" ); -hlist_for_each_entry_rcu (inode, head, i_hash) { +hlist_bl_for_each_entry_rcu (inode, node, b, i_hash) {if (inode->i_ino == ino && inode->i_sb == sb && !(READ_ONCE (inode->i_state) & (I_FREEING |I_WILL_FREE )))
@@ -1635,39 +1685,42 @@ int insert_inode_locked(struct inode *inode) {struct super_block *sb = inode->i_sb;ino_t ino = inode->i_ino; -struct hlist_head *head =i_hash_head (sb, ino); +struct hlist_bl_head *b =i_hash_head (sb, ino);while (1 ) { -struct inode *old =NULL ; -spin_lock (&inode_hash_lock); -hlist_for_each_entry (old, head, i_hash) { -if (old->i_ino != ino) +struct hlist_bl_node *node; +struct inode *old =NULL , *t; + +hlist_bl_lock (b); +hlist_bl_for_each_entry (t, node, b, i_hash) { +if (t->i_ino != ino)continue ; -if (old->i_sb != sb) +if (t->i_sb != sb)continue ; -spin_lock (&old->i_lock); -if (old->i_state & (I_FREEING |I_WILL_FREE )) { -spin_unlock (&old->i_lock); +spin_lock (&t->i_lock); +if (t->i_state & (I_FREEING |I_WILL_FREE )) { +spin_unlock (&t->i_lock);continue ; } + old = t;break ; }if (likely (!old)) {spin_lock (&inode->i_lock); inode->i_state |=I_NEW |I_CREATING ; -hlist_add_head_rcu (&inode->i_hash, head); +__insert_inode_hash_head (inode, b);spin_unlock (&inode->i_lock); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);return 0 ; }if (unlikely (old->i_state &I_CREATING )) {spin_unlock (&old->i_lock); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);return -EBUSY ; }__iget (old);spin_unlock (&old->i_lock); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);wait_on_inode (old);if (unlikely (!inode_unhashed (old))) {iput (old);
@@ -2192,17 +2245,18 @@ EXPORT_SYMBOL(inode_needs_sync);* wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list * will DTRT. */ -static void __wait_on_freeing_inode (struct inode *inode) +static void __wait_on_freeing_inode (struct hlist_bl_head *b, +struct inode *inode) {wait_queue_head_t *wq;DEFINE_WAIT_BIT (wait, &inode->i_state, __I_NEW); wq =bit_waitqueue (&inode->i_state, __I_NEW);prepare_to_wait (wq, &wait.wq_entry,TASK_UNINTERRUPTIBLE );spin_unlock (&inode->i_lock); -spin_unlock (&inode_hash_lock); +hlist_bl_unlock (b);schedule ();finish_wait (wq, &wait.wq_entry); -spin_lock (&inode_hash_lock); +hlist_bl_lock (b); }static __initdata unsigned long ihash_entries;
@@ -2228,7 +2282,7 @@ void __init inode_init_early(void) inode_hashtable = alloc_large_system_hash("Inode-cache", - sizeof(struct hlist_head), + sizeof(struct hlist_bl_head), ihash_entries, 14, HASH_EARLY | HASH_ZERO,
@@ -2254,7 +2308,7 @@ void __init inode_init(void) inode_hashtable = alloc_large_system_hash("Inode-cache", - sizeof(struct hlist_head), + sizeof(struct hlist_bl_head), ihash_entries, 14, HASH_ZERO,
include/linux/fs.h +5 -4
@@ -647,7 +647,8 @@ struct inode {unsigned long dirtied_when;/* jiffies of first dirtying */ unsigned long dirtied_time_when; -struct hlist_node i_hash; +struct hlist_bl_node i_hash; +struct hlist_bl_head *i_hash_head;struct list_head i_io_list;/* backing dev IO list */ #ifdef CONFIG_CGROUP_WRITEBACK struct bdi_writeback *i_wb;/* the associated cgroup wb */
@@ -713,7 +714,7 @@ static inline unsigned int i_blocksize(const struct inode *node)static inline int inode_unhashed (struct inode *inode) { -return hlist_unhashed (&inode->i_hash); +return hlist_bl_unhashed (&inode->i_hash); }/*
@@ -724,7 +725,7 @@ static inline int inode_unhashed(struct inode *inode)*/ static inline void inode_fake_hash (struct inode *inode) { -hlist_add_fake (&inode->i_hash); +hlist_bl_add_fake (&inode->i_hash); }/*
@@ -2695,7 +2696,7 @@ static inline void insert_inode_hash(struct inode *inode)extern void __remove_inode_hash (struct inode *);static inline void remove_inode_hash (struct inode *inode) { -if (!inode_unhashed (inode) && !hlist_fake (&inode->i_hash)) +if (!inode_unhashed (inode) && !hlist_bl_fake (&inode->i_hash))__remove_inode_hash (inode); }
Signature · 1 line
2.40.1
Re: [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 36 lines · Kent Overstreet
From: Dave Chinner <[email protected]>
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
(quoted diff) +0
-2- 52.13% 0.04% [kernel] [k] ext4_create - 52.09% ext4_create - 41.03% __ext4_new_inode - 29.92% insert_inode_locked - 25.35% _raw_spin_lock - do_raw_spin_lock - 24.97% __pv_queued_spin_lock_slowpath - 72.33% 0.02% [kernel] [k] do_filp_open - 72.31% do_filp_open - 72.28% path_openat - 57.03% bch2_create - 56.46% __bch2_create - 40.43% inode_insert5 - 36.07% _raw_spin_lock - do_raw_spin_lock 35.86% __pv_queued_spin_lock_slowpath 4.02% find_inodeConvert the inode hash table to a RCU-aware hash-bl table just like the dentry cache. Note that we need to store a pointer to the
hlist_bl_headthe inode has been added to in the inode so that when it comes to unhash the inode we know what list to lock. We need to do this because the hash value that is used to hash the inode is generated from the inode itself - filesystems can provide this themselves so we have to either store the hash or the head pointer in the inode to be able to find the right list head for removal...
Quoted 7 lines
Same workload after:
Signed-off-by Dave Chinner <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Kent Overstreet <[email protected]>
I have been maintaining this patchset uptodate in my own local trees and the code in this patch looks the same. The commit message above, however, has been mangled. The full commit message should be:
vfs: inode cache conversion to hash-bl
Because scalability of the global inode_hash_lock really, really sucks and prevents me from doing scalability characterisation and analysis of bcachefs algorithms.
Profiles of a 32-way concurrent create of 51.2m inodes with fsmark on a couple of different filesystems on a 5.10 kernel:
- 52.13% 0.04% [kernel] [k] ext4_create
- 52.09% ext4_create
- 41.03% __ext4_new_inode
- 29.92% insert_inode_locked
- 25.35% _raw_spin_lock
- do_raw_spin_lock
- 24.97% __pv_queued_spin_lock_slowpath
- 72.33% 0.02% [kernel] [k] do_filp_open
- 72.31% do_filp_open
- 72.28% path_openat
- 57.03% bch2_create
- 56.46% __bch2_create
- 40.43% inode_insert5
- 36.07% _raw_spin_lock
- do_raw_spin_lock
35.86% __pv_queued_spin_lock_slowpath
4.02% find_inode
btrfs was tested but it is limited by internal lock contention at
Quoted 1 line
=2 threads on this workload, so never hammers the inode cache lock
hard enough for this change to matter to it's performance.
However, both bcachefs and ext4 demonstrate poor scaling at >=8 threads on concurrent lookup or create workloads.
Hence convert the inode hash table to a RCU-aware hash-bl table just like the dentry cache. Note that we need to store a pointer to the hlist_bl_head the inode has been added to in the inode so that when it comes to unhash the inode we know what list to lock. We need to do this because, unlike the dentry cache, the hash value that is used to hash the inode is not generated from the inode itself. i.e. filesystems can provide this themselves so we have to either store the hashval or the hlist head pointer in the inode to be able to find the right list head for removal...
Concurrent create with variying thread count (files/s):
ext4 bcachefs
threads vanilla patched vanilla patched 2 117k 112k 80k 85k 4 185k 190k 133k 145k 8 303k 346k 185k 255k 16 389k 465k 190k 420k 32 360k 437k 142k 481k
CPU usage for both bcachefs and ext4 at 16 and 32 threads has been halved on the patched kernel, while performance has increased marginally on ext4 and massively on bcachefs. Internal filesystem algorithms now limit performance on these workloads, not the global inode_hash_lock.
Profile of the workloads on the patched kernels:
- 35.94% 0.07% [kernel] [k] ext4_create
- 35.87% ext4_create
- 20.45% __ext4_new_inode
...
3.36% insert_inode_locked
- 78.43% do_filp_open
- 78.36% path_openat
- 53.95% bch2_create
- 47.99% __bch2_create
....
- 7.57% inode_insert5
6.94% find_inode
Spinlock contention is largely gone from the inode hash operations and the filesystems are limited by contention in their internal algorithms.
---
Other than that, the diffstat is the same and I don't see any obvious differences in the code comapred to what I've been running locally.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Christian Brauner
maintainerRe: [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 131 lines · Dave Chinner
Quoted 36 lines · Kent Overstreet
From: Dave Chinner <[email protected]>
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
(quoted diff) +0
-2- 52.13% 0.04% [kernel] [k] ext4_create - 52.09% ext4_create - 41.03% __ext4_new_inode - 29.92% insert_inode_locked - 25.35% _raw_spin_lock - do_raw_spin_lock - 24.97% __pv_queued_spin_lock_slowpath - 72.33% 0.02% [kernel] [k] do_filp_open - 72.31% do_filp_open - 72.28% path_openat - 57.03% bch2_create - 56.46% __bch2_create - 40.43% inode_insert5 - 36.07% _raw_spin_lock - do_raw_spin_lock 35.86% __pv_queued_spin_lock_slowpath 4.02% find_inodeConvert the inode hash table to a RCU-aware hash-bl table just like the dentry cache. Note that we need to store a pointer to the
hlist_bl_headthe inode has been added to in the inode so that when it comes to unhash the inode we know what list to lock. We need to do this because the hash value that is used to hash the inode is generated from the inode itself - filesystems can provide this themselves so we have to either store the hash or the head pointer in the inode to be able to find the right list head for removal...Quoted 7 lines
Same workload after:
Signed-off-by Dave Chinner <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Kent Overstreet <[email protected]>I have been maintaining this patchset uptodate in my own local trees and the code in this patch looks the same. The commit message above, however, has been mangled. The full commit message should be:
vfs: inode cache conversion to hash-bl
Because scalability of the global
inode_hash_lockreally, really sucks and prevents me from doing scalability characterisation and analysis of bcachefs algorithms.Profiles of a 32-way concurrent create of 51.2m inodes with fsmark on a couple of different filesystems on a 5.10 kernel:
(quoted diff) +0
-2- 52.13% 0.04% [kernel] [k] ext4_create - 52.09% ext4_create - 41.03% __ext4_new_inode - 29.92% insert_inode_locked - 25.35% _raw_spin_lock - do_raw_spin_lock - 24.97% __pv_queued_spin_lock_slowpath - 72.33% 0.02% [kernel] [k] do_filp_open - 72.31% do_filp_open - 72.28% path_openat - 57.03% bch2_create - 56.46% __bch2_create - 40.43% inode_insert5 - 36.07% _raw_spin_lock - do_raw_spin_lock 35.86% __pv_queued_spin_lock_slowpath 4.02% find_inodebtrfs was tested but it is limited by internal lock contention at
Quoted 1 line
=2 threads on this workload, so never hammers the inode cache lock
hard enough for this change to matter to it's performance.
However, both bcachefs and ext4 demonstrate poor scaling at >=8 threads on concurrent lookup or create workloads.
Hence convert the inode hash table to a RCU-aware hash-bl table just like the dentry cache. Note that we need to store a pointer to the
hlist_bl_headthe inode has been added to in the inode so that when it comes to unhash the inode we know what list to lock. We need to do this because, unlike the dentry cache, the hash value that is used to hash the inode is not generated from the inode itself. i.e. filesystems can provide this themselves so we have to either store the hashval or the hlist head pointer in the inode to be able to find the right list head for removal...Concurrent create with variying thread count (files/s):
ext4 bcachefs
threads vanilla patched vanilla patched 2 117k 112k 80k 85k 4 185k 190k 133k 145k 8 303k 346k 185k 255k 16 389k 465k 190k 420k 32 360k 437k 142k 481kCPU usage for both bcachefs and ext4 at 16 and 32 threads has been halved on the patched kernel, while performance has increased marginally on ext4 and massively on bcachefs. Internal filesystem algorithms now limit performance on these workloads, not the global
inode_hash_lock.Profile of the workloads on the patched kernels:
- 35.94% 0.07% [kernel] [k]
ext4_create
- 35.87%ext4_create
- 20.45%__ext4_new_inode
...3.36% insert_inode_locked- 78.43%
do_filp_open
- 78.36%path_openat
- 53.95%bch2_create
- 47.99%__bch2_create
....
- 7.57%inode_insert5
6.94%find_inodeSpinlock contention is largely gone from the inode hash operations and the filesystems are limited by contention in their internal algorithms.
Quoted 5 lines
Signed-off-by Dave Chinner <[email protected]>---
Other than that, the diffstat is the same and I don't see any obvious differences in the code comapred to what I've been running locally.
There's a bit of a backlog before I get around to looking at this but it'd be great if we'd have a few reviewers for this change.
Kent Overstreet
maintainerRe: [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 3 lines · Christian Brauner
On Wed, May 10, 2023 at 02:45:57PM +1000, Dave Chinner wrote:
There's a bit of a backlog before I get around to looking at this but it'd be great if we'd have a few reviewers for this change.
It is well tested - it's been in the bcachefs tree for ages with zero issues. I'm pulling it out of the bcachefs-prerequisites series though since Dave's still got it in his tree, he's got a newer version with better commit messages.
It's a significant performance boost on metadata heavy workloads for any non-XFS filesystem, we should definitely get it in.
Re: [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 5 lines · Kent Overstreet
Quoted 3 lines · Christian Brauner
On Wed, May 10, 2023 at 02:45:57PM +1000, Dave Chinner wrote:
There's a bit of a backlog before I get around to looking at this but it'd be great if we'd have a few reviewers for this change.
Quoted 7 lines
It is well tested - it's been in the bcachefs tree for ages with zero issues. I'm pulling it out of the bcachefs-prerequisites series though since Dave's still got it in his tree, he's got a newer version with better commit messages.
It's a significant performance boost on metadata heavy workloads for any non-XFS filesystem, we should definitely get it in.
I've got an up to date vfs-scale tree here (6.4-rc1) but I have not been able to test it effectively right now because my local performance test server is broken. I'll do what I can on the old small machine that I have to validate it when I get time, but that might be a few weeks away....
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git vfs-scale
As it is, the inode hash-bl changes have zero impact on XFS because it has it's own highly scalable lockless, sharded inode cache. So unless I'm explicitly testing ext4 or btrfs scalability (rare) it's not getting a lot of scalability exercise. It is being used by the root filesytsems on all those test VMs, but that's about it...
That said, my vfs-scale tree also has Waiman Long's old dlist code (per cpu linked list) which converts the sb inode list and removes the global lock there. This does make a huge impact for XFS - the current code limits inode cache cycling to about 600,000 inodes/sec on >=16p machines. With dlists, however:
| 5.17.0 on a XFS filesystem with 50 million inodes in it on a 32p | machine with a 1.6MIOPS/6.5GB/s block device. | | Fully concurrent full filesystem bulkstat: | | wall time sys time IOPS BW rate | unpatched: 1m56.035s 56m12.234s 8k 200MB/s 0.4M/s | patched: 0m15.710s 3m45.164s 70k 1.9GB/s 3.4M/s | | Unpatched flat kernel profile: | | 81.97% [kernel] [k] __pv_queued_spin_lock_slowpath | 1.84% [kernel] [k] do_raw_spin_lock | 1.33% [kernel] [k] __raw_callee_save___pv_queued_spin_unlock | 0.50% [kernel] [k] memset_erms | 0.42% [kernel] [k] do_raw_spin_unlock | 0.42% [kernel] [k] xfs_perag_get | 0.40% [kernel] [k] xfs_buf_find | 0.39% [kernel] [k] __raw_spin_lock_init | | Patched flat kernel profile: | | 10.90% [kernel] [k] do_raw_spin_lock | 7.21% [kernel] [k] __raw_callee_save___pv_queued_spin_unlock | 3.16% [kernel] [k] xfs_buf_find | 3.06% [kernel] [k] rcu_segcblist_enqueue | 2.73% [kernel] [k] memset_erms | 2.31% [kernel] [k] __pv_queued_spin_lock_slowpath | 2.15% [kernel] [k] __raw_spin_lock_init | 2.15% [kernel] [k] do_raw_spin_unlock | 2.12% [kernel] [k] xfs_perag_get | 1.93% [kernel] [k] xfs_btree_lookup
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Christian Brauner
maintainerRe: [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 20 lines · Dave Chinner
Quoted 5 lines · Kent Overstreet
Quoted 3 lines · Christian Brauner
On Wed, May 10, 2023 at 02:45:57PM +1000, Dave Chinner wrote:
There's a bit of a backlog before I get around to looking at this but it'd be great if we'd have a few reviewers for this change.Quoted 7 lines
It is well tested - it's been in the bcachefs tree for ages with zero issues. I'm pulling it out of the bcachefs-prerequisites series though since Dave's still got it in his tree, he's got a newer version with better commit messages.
It's a significant performance boost on metadata heavy workloads for any non-XFS filesystem, we should definitely get it in.
I've got an up to date vfs-scale tree here (6.4-rc1) but I have not been able to test it effectively right now because my local performance test server is broken. I'll do what I can on the old small machine that I have to validate it when I get time, but that might be a few weeks away....
Quoted 7 lines
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.gitvfs-scaleAs it is, the inode hash-bl changes have zero impact on XFS because it has it's own highly scalable lockless, sharded inode cache. So unless I'm explicitly testing ext4 or btrfs scalability (rare) it's not getting a lot of scalability exercise. It is being used by the root filesytsems on all those test VMs, but that's about it...
I think there's a bunch of perf tests being run on -next. So we can stuff it into a vfs.unstable.* branch and see what -next thinks of this performance wise.
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
In reply to Kent OverstreetQuoted 14 lines · Kent Overstreet
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k]
ext4_create
- 52.09%ext4_create
- 41.03%__ext4_new_inode
- 29.92%insert_inode_locked
- 25.35%_raw_spin_lock
-do_raw_spin_lock
- 24.97%__pv_queued_spin_lock_slowpath
Quoted 1 line
[...]
This is interesting completely independent of bcachefs so we should give it some testing.
I updated a few places that had outdated comments.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash
[22/32] vfs: inode cache conversion to hash-bl
https://git.kernel.org/vfs/vfs/c/e3e92d47e6b1
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 30 lines · Christian Brauner
Quoted 14 lines · Kent Overstreet
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k]
ext4_create
- 52.09%ext4_create
- 41.03%__ext4_new_inode
- 29.92%insert_inode_locked
- 25.35%_raw_spin_lock
-do_raw_spin_lock
- 24.97%__pv_queued_spin_lock_slowpathQuoted 1 line
[...]
This is interesting completely independent of bcachefs so we should give it some testing.
I updated a few places that had outdated comments.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
Quoted 8 lines
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash[22/32] vfs: inode cache conversion to hash-bl
https://git.kernel.org/vfs/vfs/c/e3e92d47e6b1
What, if anything, is blocking this? It is over 5 months now, I don't see it in master nor -next.
To be clear there is no urgency as far as I'm concerned, but I did run into something which is primarily bottlenecked by inode hash lock and looks like the above should sort it out.
Looks like the patch was simply forgotten.
tl;dr can this land in -next please
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 43 lines · Mateusz Guzik
Quoted 30 lines · Christian Brauner
Quoted 14 lines · Kent Overstreet
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k]
ext4_create
- 52.09%ext4_create
- 41.03%__ext4_new_inode
- 29.92%insert_inode_locked
- 25.35%_raw_spin_lock
-do_raw_spin_lock
- 24.97%__pv_queued_spin_lock_slowpathQuoted 1 line
[...]
This is interesting completely independent of bcachefs so we should give it some testing.
I updated a few places that had outdated comments.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
Quoted 8 lines
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash[22/32] vfs: inode cache conversion to hash-bl
https://git.kernel.org/vfs/vfs/c/e3e92d47e6b1What, if anything, is blocking this? It is over 5 months now, I don't see it in master nor -next.
Quoted 7 lines
To be clear there is no urgency as far as I'm concerned, but I did run into something which is primarily bottlenecked by inode hash lock and looks like the above should sort it out.
Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files, so 20 mln inodes in total). Box had 24 cores and 24G RAM.
Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 total
NetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long time.
So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
All systems were mostly busy contending on locks and in particular Linux was almost exclusively busy waiting on inode hash lock.
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 35 lines · Mateusz Guzik
Quoted 34 lines · Mateusz Guzik
Quoted 33 lines · Christian Brauner
Quoted 14 lines · Kent Overstreet
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k]
ext4_create
- 52.09%ext4_create
- 41.03%__ext4_new_inode
- 29.92%insert_inode_locked
- 25.35%_raw_spin_lock
-do_raw_spin_lock
- 24.97%__pv_queued_spin_lock_slowpathQuoted 1 line
[...]
This is interesting completely independent of bcachefs so we should give it some testing.
I updated a few places that had outdated comments.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree. Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash[22/32] vfs: inode cache conversion to hash-bl
https://git.kernel.org/vfs/vfs/c/e3e92d47e6b1What, if anything, is blocking this? It is over 5 months now, I don't see it in master nor -next.
Not having a test machine that can validate my current vfs-scale patchset for 4 of the 5 months makes it hard to measure and demonstrate the efficacy of the changes on a current kernel....
Quoted 27 lines
Quoted 7 lines
To be clear there is no urgency as far as I'm concerned, but I did run into something which is primarily bottlenecked by inode hash lock and looks like the above should sort it out.
Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files, so 20 mln inodes in total). Box had 24 cores and 24G RAM.
Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 totalNetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long time.
Quoted 8 lines
So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
All systems were mostly busy contending on locks and in particular Linux was almost exclusively busy waiting on inode hash lock.
Did you bother to test the patch, or are you just complaining that nobody has already done the work for you?
Because if you tested the patch, you'd have realised that by itself it does nothing to improve performance of the concurrent find+stat workload. The lock contention simply moves to the sb_inode_list_lock instead.
Patches to get rid of the sb_inode_list_lock contention were written by smarter people than me long before I wrote the hash-bl patches. However, the problem the dlist stuff was written to address (sync iterating all inodes causing sb inode list lock contention when we have a hundred million cached inodes in memory) was better fixed by changing the sync algorithm not to iterate all cached inodes just to find the handful of dirty ones it needed to sync.
IOWs, those sb_inode_list_lock changes haven't been included for the same reason as the hash-bl patches: outside micro-benchmarks, these locks just don't show up in profiles on production machines. Hence there's no urgency to "fix" these lock contention problems despite the ease with which micro-benchmarks can reproduce it...
I've kept the patches current for years, even though there hasn't been a pressing need for them. The last "vfs-scale" version I did some validation on is here:
https://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git/log/?h=vfs-scale
5.17 was the last kernel I did any serious validation and measurement against, and that all needs to be repeated before proposing it for inclusion because lots of stuff has changed since I last did some serious multi-filesystem a/b testing of this code....
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 47 lines · Dave Chinner
Quoted 46 lines · Mateusz Guzik
Quoted 45 lines · Mateusz Guzik
Quoted 43 lines · Christian Brauner
Quoted 14 lines · Kent Overstreet
Because scalability of the global
inode_hash_lockreally, really sucks.32-way concurrent create on a couple of different filesystems before:
- 52.13% 0.04% [kernel] [k]
ext4_create
- 52.09%ext4_create
- 41.03%__ext4_new_inode
- 29.92%insert_inode_locked
- 25.35%_raw_spin_lock
-do_raw_spin_lock
- 24.97%__pv_queued_spin_lock_slowpathQuoted 1 line
[...]
This is interesting completely independent of bcachefs so we should give
it some testing.I updated a few places that had outdated comments.
---
Applied to the vfs.unstable.inode-hash branch of the vfs/vfs.git tree.
Patches in the vfs.unstable.inode-hash branch should appear in linux-next soon.Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.unstable.inode-hash[22/32] vfs: inode cache conversion to hash-bl
https://git.kernel.org/vfs/vfs/c/e3e92d47e6b1
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
What, if anything, is blocking this? It is over 5 months now, I don't see it in master nor -next.
Not having a test machine that can validate my current vfs-scale patchset for 4 of the 5 months makes it hard to measure and demonstrate the efficacy of the changes on a current kernel....
Ok, see below.
Quoted 30 lines
Quoted 30 lines
Quoted 7 lines
To be clear there is no urgency as far as I'm concerned, but I did run into something which is primarily bottlenecked by inode hash lock and looks like the above should sort it out.
Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files, so 20 mln inodes in total). Box had 24 cores and 24G RAM.
Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 totalNetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long time.So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
Quoted 9 lines
Quoted 5 lines
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
All systems were mostly busy contending on locks and in particular Linux was almost exclusively busy waiting on inode hash lock.
Did you bother to test the patch, or are you just complaining that nobody has already done the work for you?
Why are you giving me attitude?
I ran a test, found the major bottleneck and it turned out there is a patch which takes care of it, but its inclusion is stalled without further communication. So I asked about it.
Quoted 5 lines
Because if you tested the patch, you'd have realised that by itself it does nothing to improve performance of the concurrent find+stat workload. The lock contention simply moves to the
sb_inode_list_lockinstead.
Is that something you benched? While it may be there is no change, going from one bottleneck to another does not automatically mean there are no gains in performance.
For example, this thing on FreeBSD used to take over one minute (just like on Linux right now), vast majority of which was spent on multicore issues. I massaged it down to ~18 seconds, despite it still being mostly bottlenecked on locks.
So I benched the hashbl change and it provides a marked improvement:
stock: 7.60s user 1306.90s system 1863% cpu 1:10.55 total patched: 6.34s user 453.87s system 1312% cpu 35.052 total
But indeed as expected it is still bottlenecked on locks.
Quoted 7 lines
IOWs, those
sb_inode_list_lockchanges haven't been included for the same reason as the hash-bl patches: outside micro-benchmarks, these locks just don't show up in profiles on production machines. Hence there's no urgency to "fix" these lock contention problems despite the ease with which micro-benchmarks can reproduce it...
The above is not a made-up microbenchmark though.
I got someone running FreeBSD whose workload mostly consists of stating tens of millions of files in parallel and which was suffering a lot from perf standpoint -- flamegraphs show that contending on locks due to memory reclamation induced by stat calls is almost everything that was going on at the time. Said workload probably should not do that to begin with (instead have a db with everything it normally stats for?), but here we are.
That is to say, while I would not be in position to test Linux in the above workload, the problem (high inode turnover in memory) is very much real.
All that said, if a real deployment which runs into the problem is needed to justify the change, then I can't help (wrong system).
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
It was bugging me that find_inode_fast is at the top of the profile (modulo the locking routine).
Internals don't look too bad (it skips collisions without taking locks), so I started wondering if hashing is any good.
I re-ran the scan of 20 mln and started counting visited inodes for each call, got this:
[0, 1) 58266 | | [1, 2) 385228 |@@@ | [2, 3) 1252480 |@@@@@@@@@@ | [3, 4) 2710082 |@@@@@@@@@@@@@@@@@@@@@@@ | [4, 5) 4385945 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [5, 6) 5662628 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [6, 7) 6074390 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [7, 8) 5575381 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [8, 9) 4475706 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [9, 10) 3183676 |@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [10, 11) 2041743 |@@@@@@@@@@@@@@@@@ | [11, 12) 1189850 |@@@@@@@@@@ | [12, 13) 637683 |@@@@@ | [13, 14) 313830 |@@ | [14, 15) 143277 |@ | [15, 16) 61501 | | [16, 17) 25116 | | [17, 18) 9693 | | [18, 19) 3435 | | [19, 20) 1120 | | [20, 21) 385 | | [21, 22) 99 | | [22, 23) 45 | | [23, 24) 15 | | [24, 25) 2 | | [25, 26) 2 | | [26, 27) 2 | |
I compared this to literally just taking the ino & i_hash_mask as the value, got this:
[0, 1) 119800 | | [1, 2) 508063 |@@@ | [2, 3) 1576390 |@@@@@@@@@@@ | [3, 4) 2763163 |@@@@@@@@@@@@@@@@@@@ | [4, 5) 3696348 |@@@@@@@@@@@@@@@@@@@@@@@@@@ | [5, 6) 5975274 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [6, 7) 7253615 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [7, 8) 6563736 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [8, 9) 5012728 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [9, 10) 3495208 |@@@@@@@@@@@@@@@@@@@@@@@@@ | [10, 11) 1606659 |@@@@@@@@@@@ | [11, 12) 459458 |@@@ | [12, 13) 3940 | | [13, 14) 21 | | [14, 15) 6 | | [15, 16) 2 | |
That is to say distribution is much better, in particular the longest streak is 15 (as opposed to 26).
While I'm not saying just taking ino is any good, I am saying there is room for improvement here as far as one mount point is concerned.
Side note is that sb could have a randomly generated seed instead of its address, which should help distributing this better.
So tl;dr hash distribution leaves some room for improvement and *maybe* I'll prod it some time next month. One can also grow the table of course, but that's for later.
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
In reply to Mateusz GuzikQuoted 35 lines · Mateusz Guzik
Quoted 34 lines · Dave Chinner
Quoted 33 lines · Mateusz Guzik
Quoted 7 lines
To be clear there is no urgency as far as I'm concerned, but I did run into something which is primarily bottlenecked by inode hash lock and looks like the above should sort it out.
Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files, so 20 mln inodes in total). Box had 24 cores and 24G RAM.
Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 totalNetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long time.So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
All systems were mostly busy contending on locks and in particular Linux was almost exclusively busy waiting on inode hash lock.
Did you bother to test the patch, or are you just complaining that nobody has already done the work for you?
Why are you giving me attitude?
Look in the mirror, mate.
Starting off with a derogatory statement like:
"In case you can't be arsed, ..."
is a really good way to start a fight.
I don't think anyone working on this stuff couldn't be bothered to get their lazy arses off their couches to get it merged. Though you may not have intended it that way, that's exactly what "can't be arsed" means.
I have not asked for this code to be merged because I'm not ready to ask for it to be merged. I'm trying to be careful and cautious about changing core kernel code that every linux installation out there uses because I care about this code being robust and stable. That's the exact opposite of "can't be arsed"....
Further, you have asked for code that is not ready to be merged to be merged without reviewing it or even testing it to see if it solved your reported problem. This is pretty basic stuff - it you want it merged, then *you also need to put effort into getting it merged* regardless of who wrote the code. TANSTAAFL.
But you've done neither - you've just made demands and thrown hypocritical shade implying busy people working on complex code are lazy arses.
Perhaps you should consider your words more carefully in future?
Quoted 9 lines
Quoted 5 lines
Because if you tested the patch, you'd have realised that by itself it does nothing to improve performance of the concurrent find+stat workload. The lock contention simply moves to the
sb_inode_list_lockinstead.Is that something you benched? While it may be there is no change, going from one bottleneck to another does not automatically mean there are no gains in performance.
Of course I have. I wouldn't have said anything if this wasn't a subject I have specific knowledge and expertise in. As I've already said, I've been running this specific "will it scale" find+stat micro-benchmark for well over a decade. For example:
https://lore.kernel.org/linux-xfs/20130603074452.GZ29466@dastard/
That's dated June 2013, and the workload is:
"8-way 50 million zero-length file create, 8-way find+stat of all the files, 8-unlink of all the files:"
Yeah, this workload only scaled to a bit over 4 CPUs a decade ago, hence I only tested to 8-way....
Quoted 10 lines
For example, this thing on FreeBSD used to take over one minute (just like on Linux right now), vast majority of which was spent on multicore issues. I massaged it down to ~18 seconds, despite it still being mostly bottlenecked on locks.
So I benched the hashbl change and it provides a marked improvement:
stock: 7.60s user 1306.90s system 1863% cpu 1:10.55 total patched: 6.34s user 453.87s system 1312% cpu 35.052 totalBut indeed as expected it is still bottlenecked on locks.
That's better than I expected, but then again I haven't looked at this code in detail since around 5.17 and lots has changed since then. What filesystem was this? What kernel? What locks is it bottlenecked on now? Did you test the vfs-scale branch I pointed you at, or just the hash-bl patches?
Quoted 8 lines
Quoted 6 lines
IOWs, those
sb_inode_list_lockchanges haven't been included for the same reason as the hash-bl patches: outside micro-benchmarks, these locks just don't show up in profiles on production machines. Hence there's no urgency to "fix" these lock contention problems despite the ease with which micro-benchmarks can reproduce it...The above is not a made-up microbenchmark though.
I didn't say anything about it being "made up".
There's typically a huge difference in behaviour between the microbenchmark which immediately discards retrieved data and has no memory footprint to speak of versus an application that comparing the retrieved data with an in-memory index of inodes held in a memory constrained environment to determine if anything has changed and then doing different work if they have changed.
IOWs, while microbenchmarks can easily produce contention, it's much less obvious that applications doing substantial userspace work between similar data retrieval operations will experience similar kernel level contention problems.
What is lacking here is real world evidence showing this is a production level problem that needs to be solved immediately....
Quoted 5 lines
I got someone running FreeBSD whose workload mostly consists of stating tens of millions of files in parallel and which was suffering a lot from perf standpoint -- flamegraphs show that contending on locks due to memory reclamation induced by stat calls is almost everything that was going on at the time.
.... and "one person's workload on FreeBSD" is not significant evidence there's a Linux kernel problem that needs to be solved immediately.
Quoted 3 lines
Said workload probably should not do that to begin with (instead have a db with everything it normally stats for?), but here we are.
As you state, the right fix for the application is to avoid scanning tens of millions of inodes repeatedly. We have functionality in linux like fanotify to watch and report changes to individual files in a huge filesystem, so even if this was running on Linux the push-back would be to use fanotify and avoid repeatedly poll the entire filesystem to find individual file changes.
Quoted 3 lines
That is to say, while I would not be in position to test Linux in the above workload, the problem (high inode turnover in memory) is very much real.
Yup, XFS currently bottlenecks at about 800,000 inodes/s being streamed through memory my old 32p test machine - it's largely the sb->s_inode_list_lock that is the limitation. The vfs-scale branch I've pointed to brings that up to about 1.5 million inodes/s before the next set of limits are hit - the system is CPU bound due to the aggregate memory demand of ~10GB/s being allocated and freed by the mm subsystem (not lock contention). Hence further improvements are all about improving per-lookup operation CPU and memory efficiency..
Quoted 2 lines
All that said, if a real deployment which runs into the problem is needed to justify the change, then I can't help (wrong system).
Well, that's kind of the point, though - if users and customers are not reporting that they have production workloads where 800,000 inodes/s throughput through the inode cache is the performance limiting factor, then why risk destabilising core code by changing it?
Yes, we can go faster (as the vfs-scale branch shows), but if applications aren't limited by the existing code, why risk breaking every linux installation out there by pushing something that isn't 100% baked? Nobody wins if the new code is faster for a few but has bugs that many people hit, so if there's no urgency to change the code I won't hurry to push the change. I've carried this code for years, a few months here or there isn't going to change anything material....
If you think that's wrong or want it faster than I might address it, then by all means you can take the vfs-scale branch and do the validation work needed to get it pushed it upstream sooner.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 75 lines · Dave Chinner
Quoted 40 lines · Mateusz Guzik
Quoted 39 lines · Dave Chinner
Quoted 38 lines · Mateusz Guzik
Quoted 9 lines
To be clear there is no urgency as far as I'm concerned, but I did run
into something which is primarily bottlenecked by inode hash lock and
looks like the above should sort it out.Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat
on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files,
so 20 mln inodes in total). Box had 24 cores and 24G RAM.Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 totalNetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long
time.So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
All systems were mostly busy contending on locks and in particular Linux
was almost exclusively busy waiting on inode hash lock.Did you bother to test the patch, or are you just complaining that nobody has already done the work for you?
Why are you giving me attitude?
Look in the mirror, mate.
Starting off with a derogatory statement like:
"In case you can't be arsed, ..."
is a really good way to start a fight.
I don't think anyone working on this stuff couldn't be bothered to get their lazy arses off their couches to get it merged. Though you may not have intended it that way, that's exactly what "can't be arsed" means.
I have not asked for this code to be merged because I'm not ready to ask for it to be merged. I'm trying to be careful and cautious about changing core kernel code that every linux installation out there uses because I care about this code being robust and stable. That's the exact opposite of "can't be arsed"....
Further, you have asked for code that is not ready to be merged to be merged without reviewing it or even testing it to see if it solved your reported problem. This is pretty basic stuff - it you want it merged, then *you also need to put effort into getting it merged* regardless of who wrote the code. TANSTAAFL.
Quoted 4 lines
But you've done neither - you've just made demands and thrown hypocritical shade implying busy people working on complex code are lazy arses.
So I took few days to take a look at this with a fresh eye and I see where the major disconnect is coming from, albeit still don't see how it came to be nor why it persists.
To my understanding your understanding is that I demand you carry the hash bl patch over the finish line and I'm rude about it as well.
That is not my position here though.
For starters my opening e-mail was to Christian, not you. You are CC'ed as the patch author. It is responding to an e-mail which claimed the patch would land in -next, which to my poking around did not happen (and I checked it's not in master either). Since there was no other traffic about it that I could find, I figured it was probably forgotten. You may also notice the e-mail explicitly states:
1. I have a case which runs into inode hash being a problem
2. *there is no urgency*, I'm just asking what's up with the patch not getting anywhere.
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
If you know about Illumos, it is mostly slow and any serious performance work stopped there when Oracle closed the codebase over a decade ago. Or to put it differently, one has to be doing something really bad to not be faster today. And there was this bad -- the inode hash. I found it amusing and decided to share in addition to asking about the patch.
So no Dave, I'm not claiming the patch is not in because anyone is lazy.
Whether the patch is ready for reviews and whatnot is your call to make as the author.
To repeat from my previous e-mail I note the lock causes real problems in a real-world setting, it's not just microbenchmarks, but I'm in no position to test it against the actual workload (only the part I carved out into a benchmark, where it does help -- gets rid of the nasty back-to-back lock acquire, first to search for the inode and then to insert a new one).
If your assessment is that more testing is needed, that makes sense and is again your call to make. I repeat again I can't help with this bit though. And if you don't think the effort is justified at the moment (or there are other things with higher priority), so be it.
It may be I'll stick around in general and if so it may be I'm going to run into you again.
With this in mind:
Quoted 2 lines
Perhaps you should consider your words more carefully in future?
On that front perhaps you could refrain from assuming someone is trying to call you names or whatnot. But more importantly if you consider an e-mail to be rude, you can call it out instead of escalating or responding in what you consider to be the same tone.
All that said I'm bailing from this patchset.
Cheers,
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 100 lines · Mateusz Guzik
Quoted 75 lines · Dave Chinner
Quoted 40 lines · Mateusz Guzik
Quoted 39 lines · Dave Chinner
Quoted 38 lines · Mateusz Guzik
Quoted 9 lines
To be clear there is no urgency as far as I'm concerned, but I did run
into something which is primarily bottlenecked by inode hash lock and
looks like the above should sort it out.Looks like the patch was simply forgotten.
tl;dr can this land in -next please
In case you can't be arsed, here is something funny which may convince you to expedite. ;)
I did some benching by running 20 processes in parallel, each doing stat
on a tree of 1 million files (one tree per proc, 1000 dirs x 1000 files,
so 20 mln inodes in total). Box had 24 cores and 24G RAM.Best times:
Linux: 7.60s user 1306.90s system 1863% cpu 1:10.55 total FreeBSD: 3.49s user 345.12s system 1983% cpu 17.573 total OpenBSD: 5.01s user 6463.66s system 2000% cpu 5:23.42 total DragonflyBSD: 11.73s user 1316.76s system 1023% cpu 2:09.78 total OmniosCE: 9.17s user 516.53s system 1550% cpu 33.905 totalNetBSD failed to complete the run, OOM-killing workers:
http://mail-index.netbsd.org/tech-kern/2023/10/19/msg029242.html
OpenBSD is shafted by a big kernel lock, so no surprise it takes a long
time.So what I find funny is that Linux needed more time than OmniosCE (an Illumos variant, fork of Solaris).
It also needed more time than FreeBSD, which is not necessarily funny but not that great either.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
All systems were mostly busy contending on locks and in particular Linux
was almost exclusively busy waiting on inode hash lock.Did you bother to test the patch, or are you just complaining that nobody has already done the work for you?
Why are you giving me attitude?
Look in the mirror, mate.
Starting off with a derogatory statement like:
"In case you can't be arsed, ..."
is a really good way to start a fight.
I don't think anyone working on this stuff couldn't be bothered to get their lazy arses off their couches to get it merged. Though you may not have intended it that way, that's exactly what "can't be arsed" means.
I have not asked for this code to be merged because I'm not ready to ask for it to be merged. I'm trying to be careful and cautious about changing core kernel code that every linux installation out there uses because I care about this code being robust and stable. That's the exact opposite of "can't be arsed"....
Further, you have asked for code that is not ready to be merged to be merged without reviewing it or even testing it to see if it solved your reported problem. This is pretty basic stuff - it you want it merged, then *you also need to put effort into getting it merged* regardless of who wrote the code. TANSTAAFL.
Quoted 4 lines
But you've done neither - you've just made demands and thrown hypocritical shade implying busy people working on complex code are lazy arses.
So I took few days to take a look at this with a fresh eye and I see where the major disconnect is coming from, albeit still don't see how it came to be nor why it persists.
To my understanding your understanding is that I demand you carry the hash bl patch over the finish line and I'm rude about it as well.
That is not my position here though.
For starters my opening e-mail was to Christian, not you. You are CC'ed as the patch author. It is responding to an e-mail which claimed the patch would land in -next, which to my poking around did not happen (and I checked it's not in master either). Since there was no other traffic about it that I could find, I figured it was probably forgotten. You may also notice the e-mail explicitly states:
1. I have a case which runs into inode hash being a problem
2. *there is no urgency*, I'm just asking what's up with the patch not getting anywhere.
Quoted 2 lines
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
I thought that was a very rude way to address Christian.
Notice how he hasn't even given you a response?
"Hello, this patch improves performance for me on _______ workload. What needs to be done to get this ready for merging? I'd like to take on that work."
--D
Quoted 42 lines
If you know about Illumos, it is mostly slow and any serious performance work stopped there when Oracle closed the codebase over a decade ago. Or to put it differently, one has to be doing something really bad to not be faster today. And there was this bad -- the inode hash. I found it amusing and decided to share in addition to asking about the patch.
So no Dave, I'm not claiming the patch is not in because anyone is lazy.
Whether the patch is ready for reviews and whatnot is your call to make as the author.
To repeat from my previous e-mail I note the lock causes real problems in a real-world setting, it's not just microbenchmarks, but I'm in no position to test it against the actual workload (only the part I carved out into a benchmark, where it does help -- gets rid of the nasty back-to-back lock acquire, first to search for the inode and then to insert a new one).
If your assessment is that more testing is needed, that makes sense and is again your call to make. I repeat again I can't help with this bit though. And if you don't think the effort is justified at the moment (or there are other things with higher priority), so be it.
It may be I'll stick around in general and if so it may be I'm going to run into you again.
With this in mind:Quoted 2 lines
Perhaps you should consider your words more carefully in future?
On that front perhaps you could refrain from assuming someone is trying to call you names or whatnot. But more importantly if you consider an e-mail to be rude, you can call it out instead of escalating or responding in what you consider to be the same tone.
All that said I'm bailing from this patchset.
Cheers,
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Christian Brauner
maintainerRe: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
In reply to Mateusz GuzikQuoted 2 lines
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
Fyi, I can't be arsed to be talked to like that.
Quoted 2 lines
Whether the patch is ready for reviews and whatnot is your call to make as the author.
This is basically why that patch never staid in -next. Dave said this patch is meaningless without his other patchs and I had no reason to doubt that claim nor currently the cycles to benchmark and disprove it.
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 5 lines · Christian Brauner
Quoted 2 lines
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
Fyi, I can't be arsed to be talked to like that.
Maybe there is a language or cultural barrier at play here and the above comes off to you as inflammatory.
I assumed the tone here is rather informal. For example here is an excerpt from your response to me in another thread:
Quoted 1 line
You're all driving me nuts. ;)
I assumed this was not a serious statement and the "being arsed" thing was written by me in the same spirit. I find it surprising there is a strong reaction to it, but I can only explain why.
All that said, I have some patches I intend to submit in the foreseeable future. I am going to make sure to stick to more professional tone.
Quoted 7 lines
Quoted 2 lines
Whether the patch is ready for reviews and whatnot is your call to make as the author.
This is basically why that patch never staid in -next. Dave said this patch is meaningless without his other patchs and I had no reason to doubt that claim nor currently the cycles to benchmark and disprove it.
That makes sense, thanks.
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Kent Overstreet
maintainerRe: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
In reply to Christian BraunerQuoted 11 lines · Christian Brauner
Quoted 2 lines
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
Fyi, I can't be arsed to be talked to like that.
Quoted 2 lines
Whether the patch is ready for reviews and whatnot is your call to make as the author.
This is basically why that patch never staid in -next. Dave said this patch is meaningless without his other patchs and I had no reason to doubt that claim nor currently the cycles to benchmark and disprove it.
It was a big benefit to bcachefs performance, and I've had it in my tree for quite some time. Was there any other holdup?
Re: (subset) [PATCH 22/32] vfs: inode cache conversion to hash-bl
Quoted 9 lines · Kent Overstreet
Quoted 8 lines · Christian Brauner
Quoted 2 lines
The follow up including a statement about "being arsed" once more was to Christian, not you and was rather "tongue in cheek".
Fyi, I can't be arsed to be talked to like that.
Quoted 2 lines
Whether the patch is ready for reviews and whatnot is your call to make as the author.
Quoted 6 lines
Quoted 3 lines
This is basically why that patch never staid in -next. Dave said this patch is meaningless without his other patchs and I had no reason to doubt that claim nor currently the cycles to benchmark and disprove it.
It was a big benefit to bcachefs performance, and I've had it in my tree for quite some time. Was there any other holdup?
Plenty.
- A lack of recent validation against ext4, btrfs and other filesystems.
- the loss of lockdep coverage by moving to bit locks
- it breaks CONFIG_PREEMPT_RT=y because we nest other spinlocks inside the inode_hash_lock and we can't do that if we convert the inode hash to bit locks because RT makes spinlocks sleeping locks.
- There's been additions for lockless RCU inode hash lookups from AFS and ext4 in weird, uncommon corner cases and I have no idea how to validate they still work correctly with hash-bl. I suspect they should just go away with hash-bl, but....
There's more, but these are the big ones.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Kent Overstreet
maintainerPATCH 23/32 iov_iter: copy_folio_from_iter_atomic()
In reply to Kent OverstreetAdd a foliated version of copy_page_from_iter_atomic()
--- include/linux/uio.h | 2 ++ lib/iov_iter.c | 53 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 10 deletions(-)
include/linux/uio.h +2 -0
@@ -154,6 +154,8 @@ static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)size_t copy_page_from_iter_atomic (struct page *page,unsigned offset,size_t bytes,struct iov_iter *i); +size_t copy_folio_from_iter_atomic (struct folio *folio,size_t offset, +size_t bytes,struct iov_iter *i);void iov_iter_advance (struct iov_iter *i,size_t bytes);void iov_iter_revert (struct iov_iter *i,size_t bytes);size_t fault_in_iov_iter_readable (const struct iov_iter *i,size_t bytes);
lib/iov_iter.c +43 -10
69 lines not on this page · raw message
Signature · 1 line
2.40.1
kernel test robot
build botRe: [PATCH 23/32] iov_iter: copy_folio_from_iter_atomic()
kernel test robot noticed the following build warnings:
Hi Kent,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/locking/core]
[cannot apply to axboe-block/for-next akpm-mm/mm-everything kdave/for-next linus/master v6.4-rc1 next-20230509] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Kent-Overstreet/Compiler-Attributes-add-__flatten/20230510-010302 base: tip/locking/core patch link: https://lore.kernel.org/r/20230509165657.1735798-24-kent.overstreet%40linux.dev patch subject: [PATCH 23/32] iov_iter: copy_folio_from_iter_atomic() config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20230510/[email protected]/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/0e5d4229f5e7671dabba56ea36583b1ca20a9a18
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Kent-Overstreet/Compiler-Attributes-add-__flatten/20230510-010302 git checkout 0e5d4229f5e7671dabba56ea36583b1ca20a9a18
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> lib/iov_iter.c:839:16: warning: comparison of distinct pointer types ('typeof (bytes) *' (aka 'unsigned int *') and 'typeof (((1UL) << 12) - (offset & (~(((1UL) << 12) - 1)))) *' (aka 'unsigned long *')) [-Wcompare-distinct-pointer-types]
unsigned b = min(bytes, PAGE_SIZE - (offset & PAGE_MASK));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/minmax.h:67:19: note: expanded from macro 'min'
#define min(x, y) __careful_cmp(x, y, <)
^~~~~~~~~~~~~~~~~~~~~~
include/linux/minmax.h:36:24: note: expanded from macro '__careful_cmp'
__builtin_choose_expr(__safe_cmp(x, y), \
^~~~~~~~~~~~~~~~
include/linux/minmax.h:26:4: note: expanded from macro '__safe_cmp'
(__typecheck(x, y) && __no_side_effects(x, y))
^~~~~~~~~~~~~~~~~
include/linux/minmax.h:20:28: note: expanded from macro '__typecheck'
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~1 warning generated.
vim +839 lib/iov_iter.c
825
826 size_t copy_folio_from_iter_atomic(struct folio *folio, size_t offset,
827 size_t bytes, struct iov_iter *i)
828 {
829 size_t ret = 0;
830
831 if (WARN_ON(offset + bytes > folio_size(folio)))
832 return 0;
833 if (WARN_ON_ONCE(!i->data_source))
834 return 0;
835
836 #ifdef CONFIG_HIGHMEM
837 while (bytes) {
838 struct page *page = folio_page(folio, offset >> PAGE_SHIFT);
> 839 unsigned b = min(bytes, PAGE_SIZE - (offset & PAGE_MASK));
840 unsigned r = __copy_page_from_iter_atomic(page, offset, b, i);
841
842 offset += r;
843 bytes -= r;
844 ret += r;
845
846 if (r != b)
847 break;
848 }
849 #else
850 ret = __copy_page_from_iter_atomic(&folio->page, offset, bytes, i);
851 #endif
852
853 return ret;
854 }
855 EXPORT_SYMBOL(copy_folio_from_iter_atomic);
856
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
kernel test robot
build botRe: [PATCH 23/32] iov_iter: copy_folio_from_iter_atomic()
In reply to Kent Overstreetkernel test robot noticed the following build warnings:
Hi Kent,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/locking/core]
[cannot apply to axboe-block/for-next akpm-mm/mm-everything kdave/for-next linus/master v6.4-rc1 next-20230510] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Kent-Overstreet/Compiler-Attributes-add-__flatten/20230510-010302 base: tip/locking/core patch link: https://lore.kernel.org/r/20230509165657.1735798-24-kent.overstreet%40linux.dev patch subject: [PATCH 23/32] iov_iter: copy_folio_from_iter_atomic() config: powerpc-randconfig-s042-20230509 (https://download.01.org/0day-ci/archive/20230511/[email protected]/config) compiler: powerpc-linux-gcc (GCC) 12.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/0e5d4229f5e7671dabba56ea36583b1ca20a9a18
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Kent-Overstreet/Compiler-Attributes-add-__flatten/20230510-010302
git checkout 0e5d4229f5e7671dabba56ea36583b1ca20a9a18
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=powerpc olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=powerpc SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> lib/iov_iter.c:839:30: sparse: sparse: incompatible types in comparison expression (different type sizes): >> lib/iov_iter.c:839:30: sparse: unsigned int * >> lib/iov_iter.c:839:30: sparse: unsigned long *
vim +839 lib/iov_iter.c
825
826 size_t copy_folio_from_iter_atomic(struct folio *folio, size_t offset,
827 size_t bytes, struct iov_iter *i)
828 {
829 size_t ret = 0;
830
831 if (WARN_ON(offset + bytes > folio_size(folio)))
832 return 0;
833 if (WARN_ON_ONCE(!i->data_source))
834 return 0;
835
836 #ifdef CONFIG_HIGHMEM
837 while (bytes) {
838 struct page *page = folio_page(folio, offset >> PAGE_SHIFT);
> 839 unsigned b = min(bytes, PAGE_SIZE - (offset & PAGE_MASK));
840 unsigned r = __copy_page_from_iter_atomic(page, offset, b, i);
841
842 offset += r;
843 bytes -= r;
844 ret += r;
845
846 if (r != b)
847 break;
848 }
849 #else
850 ret = __copy_page_from_iter_atomic(&folio->page, offset, bytes, i);
851 #endif
852
853 return ret;
854 }
855 EXPORT_SYMBOL(copy_folio_from_iter_atomic);
856
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Kent Overstreet
maintainerPATCH 24/32 MAINTAINERS: Add entry for generic-radix-tree
In reply to Kent Overstreetlib/generic-radix-tree.c is a simple radix tree that supports storing arbitrary types. Add a maintainers entry for it.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -8615,6 +8615,13 @@ F: Documentation/devicetree/bindings/power/power?domain* F: drivers/base/power/domain*.c F: include/linux/pm_domain.h +GENERIC RADIX TREE: +M: Kent Overstreet <[email protected]> +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/generic-radix-tree.h +F: lib/generic-radix-tree.c + GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER M: Eugen Hristev <[email protected]> L: [email protected]
Signature · 1 line
2.40.1
Re: [PATCH 24/32] MAINTAINERS: Add entry for generic-radix-tree
Quoted 13 lines · Kent Overstreet
lib/generic-radix-tree.cis a simple radix tree that supports storing arbitrary types. Add a maintainers entry for it.Signed-off-by Kent Overstreet <[email protected]>--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)MAINTAINERS +0
-0
MAINTAINERS F: Documentation/devicetree/bindings/power/power?domain* · line 8618
Quoted 4 lines
MAINTAINERS +1
-0@@ -8615,6 +8615,13 @@ F: Documentation/devicetree/bindings/power/power?domain* F: drivers/base/power/domain*.c F: include/linux/pm_domain.h +GENERIC RADIX TREE:
No colon at the end of the line.
Quoted 9 lines
MAINTAINERS +6
-0+M: Kent Overstreet <[email protected]> +S: Supported +C: irc://irc.oftc.net/bcache +F: include/linux/generic-radix-tree.h +F: lib/generic-radix-tree.c + GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER M: Eugen Hristev <[email protected]> L: [email protected]
Signature · 1 line
~Randy
Kent Overstreet
maintainerPATCH 25/32 lib/generic-radix-tree.c: Don't overflow in peek()
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
When we started spreading new inode numbers throughout most of the 64 bit inode space, that triggered some corner case bugs, in particular some integer overflows related to the radix tree code. Oops.
--- include/linux/generic-radix-tree.h | 6 ++++++ lib/generic-radix-tree.c | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-)
include/linux/generic-radix-tree.h +6 -0
@@ -184,6 +184,12 @@ void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);static inline void __genradix_iter_advance (struct genradix_iter *iter,size_t obj_size) { +if (iter->offset + obj_size < iter->offset) { + iter->offset =SIZE_MAX ; + iter->pos =SIZE_MAX ; +return ; + } + iter->offset += obj_size; if (!is_power_of_2(obj_size) &&
lib/generic-radix-tree.c +14 -3
@@ -166,6 +166,10 @@ void *__genradix_iter_peek(struct genradix_iter *iter,struct genradix_root *r;struct genradix_node *n;unsigned level, i; + +if (iter->offset ==SIZE_MAX ) +return NULL ; + restart: r =READ_ONCE (radix->root);if (!r)
@@ -184,10 +188,17 @@ void *__genradix_iter_peek(struct genradix_iter *iter, (GENRADIX_ARY -1 );while (!n->children[i]) { +size_t objs_per_ptr =genradix_depth_size (level); + +if (iter->offset + objs_per_ptr < iter->offset) { + iter->offset =SIZE_MAX ; + iter->pos =SIZE_MAX ; +return NULL ; + } + i++; - iter->offset =round_down (iter->offset + -genradix_depth_size (level), -genradix_depth_size (level)); + iter->offset =round_down (iter->offset + objs_per_ptr, + objs_per_ptr); iter->pos = (iter->offset >>PAGE_SHIFT ) * objs_per_page;if (i ==GENRADIX_ARY )
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 26/32 lib/generic-radix-tree.c: Add a missing include
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
We now need linux/limits.h for SIZE_MAX.
--- include/linux/generic-radix-tree.h | 1 + 1 file changed, 1 insertion(+)
include/linux/generic-radix-tree.h +1 -0
@@ -38,6 +38,7 @@#include <asm/page.h> #include <linux/bug.h> +#include <linux/limits.h> #include <linux/log2.h> #include <linux/math.h> #include <linux/types.h>
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 27/32 lib/generic-radix-tree.c: Add peek_prev()
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
This patch adds genradix_peek_prev(), genradix_iter_rewind(), and genradix_for_each_reverse(), for iterating backwards over a generic radix tree.
--- include/linux/generic-radix-tree.h | 61 +++++++++++++++++++++++++++++- lib/generic-radix-tree.c | 59 +++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-)
include/linux/generic-radix-tree.h +60 -1
@@ -117,6 +117,11 @@ static inline size_t __idx_to_offset(size_t idx, size_t obj_size)#define __genradix_cast (_radix) (typeof((_radix)->type[0]) *)#define __genradix_obj_size (_radix) sizeof((_radix)->type[0]) +#define __genradix_objs_per_page (_radix) \ + (PAGE_SIZE / sizeof((_radix)->type[0])) +#define __genradix_page_remainder (_radix) \ + (PAGE_SIZE % sizeof((_radix)->type[0])) +#define __genradix_idx_to_offset (_radix, _idx) \ __idx_to_offset(_idx, __genradix_obj_size(_radix))
@@ -180,7 +185,25 @@ void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);#define genradix_iter_peek (_iter, _radix) \ (__genradix_cast(_radix) \ __genradix_iter_peek(_iter, &(_radix)->tree, \ - PAGE_SIZE / __genradix_obj_size(_radix))) + __genradix_objs_per_page(_radix))) + +void *__genradix_iter_peek_prev (struct genradix_iter *,struct __genradix *, +size_t ,size_t ); + +/** +* genradix_iter_peek - get first entry at or below iterator's current +* position +* @_iter: a genradix_iter +* @_radix: genradix being iterated over +* +* If no more entries exist at or below @_iter's current position, returns NULL +*/ +#define genradix_iter_peek_prev (_iter, _radix) \ + (__genradix_cast(_radix) \ + __genradix_iter_peek_prev(_iter, &(_radix)->tree, \ + __genradix_objs_per_page(_radix), \ + __genradix_obj_size(_radix) + \ + __genradix_page_remainder(_radix)))static inline void __genradix_iter_advance (struct genradix_iter *iter,size_t obj_size)
@@ -203,6 +226,25 @@ static inline void __genradix_iter_advance(struct genradix_iter *iter,#define genradix_iter_advance (_iter, _radix) \ __genradix_iter_advance(_iter, __genradix_obj_size(_radix)) +static inline void __genradix_iter_rewind (struct genradix_iter *iter, +size_t obj_size) +{ +if (iter->offset ==0 || + iter->offset ==SIZE_MAX ) { + iter->offset =SIZE_MAX ; +return ; + } + +if ((iter->offset & (PAGE_SIZE -1 )) ==0 ) + iter->offset -=PAGE_SIZE % obj_size; + + iter->offset -= obj_size; + iter->pos--; +} + +#define genradix_iter_rewind (_iter, _radix) \ + __genradix_iter_rewind(_iter, __genradix_obj_size(_radix)) +#define genradix_for_each_from (_radix, _iter, _p, _start) \ for (_iter = genradix_iter_init(_radix, _start); \ (_p = genradix_iter_peek(&_iter, _radix)) != NULL; \
@@ -220,6 +262,23 @@ static inline void __genradix_iter_advance(struct genradix_iter *iter,#define genradix_for_each (_radix, _iter, _p) \ genradix_for_each_from(_radix, _iter, _p, 0) +#define genradix_last_pos (_radix) \ + (SIZE_MAX / PAGE_SIZE * __genradix_objs_per_page(_radix) - 1) + +/** +* genradix_for_each_reverse - iterate over entry in a genradix, reverse order +* @_radix: genradix to iterate over +* @_iter: a genradix_iter to track current position +* @_p: pointer to genradix entry type +* +* On every iteration, @_p will point to the current entry, and @_iter.pos +* will be the current entry's index. +*/ +#define genradix_for_each_reverse (_radix, _iter, _p) \ + for (_iter = genradix_iter_init(_radix, genradix_last_pos(_radix));\ + (_p = genradix_iter_peek_prev(&_iter, _radix)) != NULL;\ + genradix_iter_rewind(&_iter, _radix)) +int __genradix_prealloc (struct __genradix *,size_t ,gfp_t );/**
lib/generic-radix-tree.c +59 -0
69 lines not on this page · raw message
Signature · 1 line
2.40.1
Kent Overstreet
maintainerPATCH 28/32 stacktrace: Export stack_trace_save_tsk
In reply to Kent OverstreetFrom: Christopher James Halse Rogers <[email protected]>
The bcachefs module wants it, and there doesn't seem to be any reason it shouldn't be exported like the other functions.
--- kernel/stacktrace.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/stacktrace.c +2 -0
@@ -151,6 +151,7 @@ unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,put_task_stack (tsk);return c.len; } +EXPORT_SYMBOL_GPL (stack_trace_save_tsk);/** * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
@@ -301,6 +302,7 @@ unsigned int stack_trace_save_tsk(struct task_struct *task,save_stack_trace_tsk (task, &trace);return trace.nr_entries; } +EXPORT_SYMBOL_GPL (stack_trace_save_tsk);/** * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
Signature · 1 line
2.40.1
Re: [PATCH 28/32] stacktrace: Export stack_trace_save_tsk
Quoted 4 lines · Kent Overstreet
From: Christopher James Halse Rogers <[email protected]>
The bcachefs module wants it, and there doesn't seem to be any reason it shouldn't be exported like the other functions.
What is the bcachefs module using this for?
Is that just for debug purposes? Assuming so, mentioning that in the commit message would be helpful.
Thanks,
Mark.
Quoted 30 lines
Signed-off-by Christopher James Halse Rogers <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- kernel/stacktrace.c | 2 ++ 1 file changed, 2 insertions(+)kernel/stacktrace.c +2
-0@@ -151,6 +151,7 @@ unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,put_task_stack (tsk);return c.len; } +EXPORT_SYMBOL_GPL (stack_trace_save_tsk);/** * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array @@ -301,6 +302,7 @@ unsigned int stack_trace_save_tsk(struct task_struct *task,save_stack_trace_tsk (task, &trace);return trace.nr_entries; } +EXPORT_SYMBOL_GPL (stack_trace_save_tsk);/** * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array Signature · 1 line
2.40.1
Kent Overstreet
maintainerRe: [PATCH 28/32] stacktrace: Export stack_trace_save_tsk
Quoted 10 lines · Mark Rutland
Quoted 4 lines · Kent Overstreet
From: Christopher James Halse Rogers <[email protected]>
The bcachefs module wants it, and there doesn't seem to be any reason it shouldn't be exported like the other functions.
What is the bcachefs module using this for?
Is that just for debug purposes? Assuming so, mentioning that in the commit message would be helpful.
Yes, the output ends up in debugfs.
Kent Overstreet
maintainerPATCH 29/32 lib/string_helpers: string_get_size() now returns characters wrote
In reply to Kent OverstreetFrom: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like snprintf(); this changes string_get_size() to return the the return value of snprintf().
--- include/linux/string_helpers.h | 4 ++-- lib/string_helpers.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/string_helpers.h +2 -2
@@ -23,8 +23,8 @@ enum string_size_units { STRING_UNITS_2, /* use binary powers of 2^10 */ }; -void string_get_size (u64 size,u64 blk_size,enum string_size_units units, -char *buf,int len); +int string_get_size (u64 size,u64 blk_size,enum string_size_units units, +char *buf,int len);int parse_int_array_user (const char __user *from,size_t count,int **array);
lib/string_helpers.c +4 -4
@@ -32,8 +32,8 @@* at least 9 bytes and will always be zero terminated. * */ -void string_get_size (u64 size,u64 blk_size,const enum string_size_units units, -char *buf,int len) +int string_get_size (u64 size,u64 blk_size,const enum string_size_units units, +char *buf,int len) {static const char *const units_10[] = {"B" ,"kB" ,"MB" ,"GB" ,"TB" ,"PB" ,"EB" ,"ZB" ,"YB"
@@ -126,8 +126,8 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,else unit = units_str[units][i]; -snprintf (buf, len,"%u%s %s" , (u32 )size, - tmp, unit); +return snprintf (buf, len,"%u%s %s" , (u32 )size, + tmp, unit); }EXPORT_SYMBOL (string_get_size);
Signature · 1 line
2.40.1
Re: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote
Quoted 5 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like
snprintf(); this changesstring_get_size()to return the the return value ofsnprintf().
Unfortunately, snprintf doesn't return characters written, it return what it TRIED to write, and can cause a lot of problems[1]. This patch would be fine with me if the snprintf was also replaced by scnprintf, which will return the actual string length copied (or 0) *not* including the trailing %NUL.
lib/string_helpers.c void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, · line 130
Quoted 9 lines
[...]
lib/string_helpers.c +2
-2@@ -126,8 +126,8 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,else unit = units_str[units][i]; -snprintf (buf, len,"%u%s %s" , (u32 )size, - tmp, unit); +return snprintf (buf, len,"%u%s %s" , (u32 )size, + tmp, unit);
-Kees
[1] https://github.com/KSPP/linux/issues/105
Signature · 1 line
Kees Cook
Kent Overstreet
maintainerRe: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote
Quoted 7 lines · Kees Cook
Quoted 5 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like
snprintf(); this changesstring_get_size()to return the the return value ofsnprintf().
Quoted 5 lines
Unfortunately, snprintf doesn't return characters written, it return what it TRIED to write, and can cause a lot of problems[1]. This patch would be fine with me if the snprintf was also replaced by scnprintf, which will return the actual string length copied (or 0) *not* including the trailing %NUL.
...All of which would be solved if we were converting code away from raw char * buffers to a proper string building type.
Which I tried to address when I tried to push printbufs upstream, but that turned into a giant exercise in frustration in dealing with maintainers.
Re: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote
Quoted 14 lines · Kent Overstreet
Quoted 7 lines · Kees Cook
Quoted 5 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like
snprintf(); this changesstring_get_size()to return the the return value ofsnprintf().Quoted 5 lines
Unfortunately, snprintf doesn't return characters written, it return what it TRIED to write, and can cause a lot of problems[1]. This patch would be fine with me if the snprintf was also replaced by scnprintf, which will return the actual string length copied (or 0) *not* including the trailing %NUL.
Quoted 6 lines
...All of which would be solved if we were converting code away from raw char * buffers to a proper string building type.
Which I tried to address when I tried to push printbufs upstream, but that turned into a giant exercise in frustration in dealing with maintainers.
Heh, yeah, I've been trying to aim people at using seq_buf instead of a long series of snprintf/strlcat/etc calls. Where can I look at how you wired this up to seq_buf/printbuf? I had trouble finding it when I looked before. I'd really like to find a way to do it without leaving around foot-guns for future callers of string_get_size(). :)
I found the printbuf series:
https://lore.kernel.org/lkml/[email protected]/
It seems there are some nice improvements in there. It'd be really nice if seq_buf could just grow those changes. Adding a static version of seq_buf_init to be used like you have PRINTBUF_EXTERN would be nice (or even a statically sized initializer). And much of the conversions is just changing types and functions. If we can leave all that alone, things become MUCH easier to review, etc, etc. I'd *love* to see an incremental improvement for seq_buf, especially the heap-allocation part.
Signature · 1 line
Kees Cook
Kent Overstreet
maintainerRe: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote
Quoted 6 lines · Kees Cook
Heh, yeah, I've been trying to aim people at using
seq_bufinstead of a long series of snprintf/strlcat/etc calls. Where can I look at how you wired this up to seq_buf/printbuf? I had trouble finding it when I looked before. I'd really like to find a way to do it without leaving around foot-guns for future callers ofstring_get_size(). :)
Quoted 10 lines
I found the printbuf series:
https://lore.kernel.org/lkml/[email protected]/
It seems there are some nice improvements in there. It'd be really nice ifseq_bufcould just grow those changes. Adding a static version ofseq_buf_initto be used like you havePRINTBUF_EXTERNwould be nice (or even a statically sized initializer). And much of the conversions is just changing types and functions. If we can leave all that alone, things become MUCH easier to review, etc, etc. I'd *love* to see an incremental improvement forseq_buf, especially the heap-allocation part.
Well, I raised that with Steve way back when I was starting on the conversions of existing code, and I couldn't get any communication out him regarding making those changes to seq_buf.
So, I'd _love_ to resurrect that patch series and get it in after the bcachefs merger, but don't expect me to go back and redo everything :) the amount of code in existing seq_buf users is fairly small compared to bcachef's printbuf usage, and what that patch series does in the rest of the kernel anyways.
I'd rather save that energy for ditching the seq_file interface and making that just use a printbuf - clean up that bit of API fragmentation.
Kent Overstreet
maintainerRe: [PATCH 29/32] lib/string_helpers: string_get_size() now returns characters wrote
In reply to Kees CookQuoted 7 lines · Kees Cook
Quoted 5 lines · Kent Overstreet
From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like
snprintf(); this changesstring_get_size()to return the the return value ofsnprintf().
Quoted 5 lines
Unfortunately, snprintf doesn't return characters written, it return what it TRIED to write, and can cause a lot of problems[1]. This patch would be fine with me if the snprintf was also replaced by scnprintf, which will return the actual string length copied (or 0) *not* including the trailing %NUL.
Anyways, I can't use scnprintf here, printbufs/seq_buf both need the number of characters that would have been written, but I'll update the comment.
Kent Overstreet
maintainerPATCH 30/32 lib: Export errname
In reply to Kent OverstreetThe bcachefs module now wants this and it seems sensible.
--- lib/errname.c | 1 + 1 file changed, 1 insertion(+)
lib/errname.c +1 -0
@@ -228,3 +228,4 @@ const char *errname(int err)return err >0 ? name +1 : name; } +EXPORT_SYMBOL (errname);
Signature · 1 line
2.40.1