Skip to contentSkip to conversation

bcachefs prereqs patch series

PATCH In review Kent Overstreet 7 participants 47 messages Jul 12, 2023 – Oct 18, 2023

Subsystems (10)
lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 1/20 sched: Add task_struct->faults_disabled_mapping

In reply to Kent Overstreet

From: Kent Overstreet <[email protected]>

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.

Cc Jan Kara <[email protected]>
Cc Darrick J. Wong <[email protected]>
Cc Andreas Grünbacher <[email protected]>
---
 include/linux/sched.h | 1 +
 init/init_task.c      | 1 +
 2 files changed, 2 insertions(+)
include/linux/sched.h +1 -0
index eed5d65b8d..bc7b61305c 100644
@@ -871,6 +871,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
index ff6c4b9bfe..f703116e05 100644
@@ -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

Kent Overstreet

author maintainer
#0 lore raw

cover bcachefs prereqs patch series

Here's the latest iteration of the bcachefs prereqs patch series, now hoping to go in for 6.6.

https://lore.kernel.org/linux-bcachefs/[email protected]/T/

This is now slimmed down as much has I can. I've droped the lockdep patches, at the cost of dropping lockdep support for btree node locks. Six locks and mean_and_variance are now in fs/bcachefs/. My copy_folio_from_iter_atomic() patch has been dropped in favor of the iov_iter patch from Matthew, which he said he'd likely send for -rc4, so will already be in by the time of the actual bcachefs pull request.

Christopher James Halse Rogers (1):
stacktrace: Export stack_trace_save_tsk

Kent Overstreet (18):
sched: Add task_struct->faults_disabled_mapping
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: Don't block on s_umount from __invalidate_super() lib/string_helpers: string_get_size() now returns characters wrote
lib: Export errname
locking/osq: Export osq_(lock|unlock)
bcache: move closures to lib/
MAINTAINERS: Add entry for closures
closures: closure_wait_event()
closures: closure_nr_remaining()
closures: Add a missing include
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()

Matthew Wilcox (Oracle) (1):
iov_iter: Handle compound highmem pages in
copy_page_from_iter_atomic()

 MAINTAINERS                                   | 15 ++++
 block/bdev.c                                  |  2 +-
 block/bio.c                                   | 18 +++--
 block/blk-core.c                              |  1 +
 block/blk.h                                   |  1 -
 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 +-
 fs/dcache.c                                   | 12 ++-
 fs/super.c                                    | 40 +++++++---
 include/linux/bio.h                           |  7 +-
 include/linux/blkdev.h                        |  1 +
 .../md/bcache => include/linux}/closure.h     | 46 ++++++++---
 include/linux/dcache.h                        |  1 +
 include/linux/fs.h                            |  1 +
 include/linux/generic-radix-tree.h            | 68 ++++++++++++++++-
 include/linux/sched.h                         |  1 +
 include/linux/string_helpers.h                |  4 +-
 init/init_task.c                              |  1 +
 kernel/locking/osq_lock.c                     |  2 +
 kernel/stacktrace.c                           |  2 +
 lib/Kconfig                                   |  3 +
 lib/Kconfig.debug                             |  9 +++
 lib/Makefile                                  |  2 +
 {drivers/md/bcache => lib}/closure.c          | 36 +++++----
 lib/errname.c                                 |  1 +
 lib/generic-radix-tree.c                      | 76 ++++++++++++++++++-
 lib/iov_iter.c                                | 43 +++++++----
 lib/string_helpers.c                          | 10 ++-
 31 files changed, 333 insertions(+), 90 deletions(-)
 rename {drivers/md/bcache => include/linux}/closure.h (93%)
 rename {drivers/md/bcache => lib}/closure.c (88%)
Signature · 1 line
2.40.1
author maintainer
#2 lore raw

PATCH 2/20 fs: factor out d_mark_tmpfile()

From: 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

Cc Alexander Viro <[email protected]>
Cc Christian Brauner <[email protected]>
---
 fs/dcache.c            | 12 ++++++++++--
 include/linux/dcache.h |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)
fs/dcache.c +10 -2
index 52e6d5fdab..dbdafa2617 100644
@@ -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
index 6b351e009f..3da2f0545d 100644
@@ -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
author maintainer
#3 lore raw

PATCH 3/20 iov_iter: Handle compound highmem pages in copy_page_from_iter_atomic()

In reply to Kent Overstreet

From: "Matthew Wilcox (Oracle)" <[email protected]>

copy_page_from_iter_atomic() already handles !highmem compound pages correctly, but if we are passed a highmem compound page, each base page needs to be mapped & unmapped individually.

---
 lib/iov_iter.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)
lib/iov_iter.c +28 -15
index 960223ed91..f9c4bba272 100644
@@ -857,24 +857,37 @@ size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
 }
 EXPORT_SYMBOL(iov_iter_zero);
 
-size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes,
-				  struct iov_iter *i)
+size_t copy_page_from_iter_atomic(struct page *page, unsigned offset,
+		size_t bytes, struct iov_iter *i)
 {
-	char *kaddr = kmap_atomic(page), *p = kaddr + offset;
-	if (!page_copy_sane(page, offset, bytes)) {
-		kunmap_atomic(kaddr);
+	size_t n, copied = 0;
+
+	if (!page_copy_sane(page, offset, bytes))
 		return 0;
-	}
-	if (WARN_ON_ONCE(!i->data_source)) {
-		kunmap_atomic(kaddr);
+	if (WARN_ON_ONCE(!i->data_source))
 		return 0;
-	}
-	iterate_and_advance(i, bytes, base, len, off,
-		copyin(p + off, base, len),
-		memcpy_from_iter(i, p + off, base, len)
-	)
-	kunmap_atomic(kaddr);
-	return bytes;
+
+	do {
+		char *p;
+
+		n = bytes - copied;
+		if (PageHighMem(page)) {
+			page += offset / PAGE_SIZE;
+			offset %= PAGE_SIZE;
+			n = min_t(size_t, n, PAGE_SIZE - offset);
+		}
+
+		p = kmap_atomic(page) + offset;
+		iterate_and_advance(i, n, base, len, off,
+			copyin(p + off, base, len),
+			memcpy_from_iter(i, p + off, base, len)
+		)
+		kunmap_atomic(p);
+		copied += n;
+		offset += n;
+	} while (PageHighMem(page) && copied != bytes && n > 0);
+
+	return copied;
 }
 EXPORT_SYMBOL(copy_page_from_iter_atomic);
 
Signature · 1 line
2.40.1
author maintainer
#4 lore raw

PATCH 4/20 block: Add some exports for bcachefs

In reply to Kent Overstreet

From: 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            | 2 ++
 block/blk-core.c       | 1 +
 block/blk.h            | 1 -
 include/linux/blkdev.h | 1 +
 4 files changed, 4 insertions(+), 1 deletion(-)
block/bio.c +2 -0
index 043944fd46..1e75840d17 100644
@@ -1481,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.
@@ -1540,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
index 1da77e7d62..b7b0237c36 100644
@@ -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
index 45547bcf11..f20f9ca03e 100644
@@ -251,7 +251,6 @@ static inline void bio_integrity_free(struct bio *bio)
 
 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
index c0ffe203a6..7a32dc98e1 100644
@@ -854,6 +854,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
author maintainer
#10 lore raw

PATCH 5/20 block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Kent Overstreet

bio_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
index 1e75840d17..e74a04ea14 100644
@@ -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
author maintainer
#17 lore raw

PATCH 6/20 block: Bring back zero_fill_bio_iter

In reply to Kent Overstreet

From: Kent Overstreet <[email protected]>

This reverts 6f822e1b5d9dda3d20e87365de138046e3baa03a - this helper is used by bcachefs.

---
 block/bio.c         | 6 +++---
 include/linux/bio.h | 7 ++++++-
 2 files changed, 9 insertions(+), 4 deletions(-)
block/bio.c +3 -3
index e74a04ea14..70b5c987bc 100644
@@ -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
index b3e7529ff5..f2620f8d18 100644
@@ -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
author maintainer
#22 lore raw

PATCH 7/20 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_super

This 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.

Cc Christian Brauner <[email protected]>
Cc Jens Axboe <[email protected]>
---
 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
index 21c63bfef3..a4d7e8732c 100644
@@ -934,7 +934,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
index 04bc62ab7d..a2decce02f 100644
@@ -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
index 133f0640fb..bd5105bc92 100644
@@ -2897,6 +2897,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
author maintainer
#23 lore raw

PATCH 8/20 stacktrace: Export stack_trace_save_tsk

In reply to 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.

---
 kernel/stacktrace.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/stacktrace.c +2 -0
index 9ed5ce9894..4f65824879 100644
@@ -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
author maintainer
#24 lore raw

PATCH 9/20 lib/string_helpers: string_get_size() now returns characters wrote

In reply to 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 changes string_get_size() to return the the return value of snprintf().

---
 include/linux/string_helpers.h |  4 ++--
 lib/string_helpers.c           | 10 ++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
include/linux/string_helpers.h +2 -2
index fae6beaaa2..44148f8feb 100644
@@ -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 +6 -4
index 230020a2e0..b9a34eb386 100644
@@ -31,9 +31,11 @@
  * giving the size in the required units.  @buf should have room for
  * at least 9 bytes and will always be zero terminated.
  *
+ * Return value: number of characters of output that would have been written
+ * (which may be greater than len, if output was truncated).
  */
-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 +128,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
author maintainer
#39 lore raw

PATCH 15/20 closures: closure_nr_remaining()

In reply to Kent Overstreet

Factor 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
index 36b4a83f9b..722a586bb2 100644
@@ -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
author maintainer
#27 lore raw

PATCH 11/20 locking/osq: Export osq_(lock|unlock)

In reply to Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
 kernel/locking/osq_lock.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +2 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 
 	return false;
 }
+EXPORT_SYMBOL_GPL(osq_lock);
 
 void osq_unlock(struct optimistic_spin_queue *lock)
 {
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
 	if (next)
 		WRITE_ONCE(next->locked, 1);
 }
+EXPORT_SYMBOL_GPL(osq_unlock);
Signature · 1 line
2.40.1
author maintainer
#34 lore raw

PATCH 12/20 bcache: move closures to lib/

In reply to Kent Overstreet

From: 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
index 529c9d04e9..b2d10063d3 100644
@@ -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
index 5b87e59676..054e8a33a7 100644
@@ -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
index aebb7ef10e..c8b4914ad8 100644
@@ -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
index 7e9d19fd21..35c701d542 100644
@@ -2911,7 +2911,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
index 6f3cb7c921..f61ab1bada 100644
@@ -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
similarity index 97%
rename from drivers/md/bcache/closure.h
rename to include/linux/closure.h
index c88cdc4ae4..0ec9e7bc8d 100644
@@ -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
index 5c2da561c5..f78bc8b425 100644
@@ -505,6 +505,9 @@ config ASSOCIATIVE_ARRAY
 
 	  for more information.
 
+config CLOSURES
+	bool
+
 config HAS_IOMEM
 	bool
 	depends on !NO_IOMEM
lib/Kconfig.debug +9 -0
index ce51d4dc68..3ee25d5dae 100644
@@ -1637,6 +1637,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
index 876fcdeae3..7798910135 100644
@@ -248,6 +248,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
similarity index 88%
rename from drivers/md/bcache/closure.c
rename to lib/closure.c
index d8d9394a6b..b38ded00b9 100644
@@ -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
author maintainer
#25 lore raw

PATCH 10/20 lib: Export errname

In reply to Kent Overstreet

errname() returns the name of an errcode; this functionality is otherwise only available for error pointers via %pE - bcachefs uses this for better error messages.

---
 lib/errname.c | 1 +
 1 file changed, 1 insertion(+)
lib/errname.c +1 -0
index 67739b174a..dd1b998552 100644
@@ -228,3 +228,4 @@ const char *errname(int err)
 
 	return err > 0 ? name + 1 : name;
 }
+EXPORT_SYMBOL(errname);
Signature · 1 line
2.40.1
author maintainer
#37 lore raw

PATCH 13/20 MAINTAINERS: Add entry for closures

In reply to 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.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index 35e1959464..314b55ecd1 100644
@@ -5064,6 +5064,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
author maintainer
#40 lore raw

PATCH 16/20 closures: Add a missing include

In reply to Kent Overstreet

Fixes building in userspace.

---
 lib/closure.c | 1 +
 1 file changed, 1 insertion(+)
lib/closure.c +1 -0
index b38ded00b9..0855e698ce 100644
@@ -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
author maintainer
#38 lore raw

PATCH 14/20 closures: closure_wait_event()

In reply to Kent Overstreet

From: 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
index 0ec9e7bc8d..36b4a83f9b 100644
@@ -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
author maintainer
#42 lore raw

PATCH 18/20 lib/generic-radix-tree.c: Don't overflow in peek()

In reply to Kent Overstreet

From: 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
index 107613f7d7..63080822dc 100644
@@ -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
index f25eb111c0..7dfa88282b 100644
@@ -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
author maintainer
#41 lore raw

PATCH 17/20 MAINTAINERS: Add entry for generic-radix-tree

In reply to Kent Overstreet

lib/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
index 314b55ecd1..c3fd29247b 100644
@@ -8670,6 +8670,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
author maintainer
#43 lore raw

PATCH 19/20 lib/generic-radix-tree.c: Add a missing include

In reply to Kent Overstreet

From: 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
index 63080822dc..f6cd0f909d 100644
@@ -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
author maintainer
#46 lore raw

PATCH 20/20 lib/generic-radix-tree.c: Add peek_prev()

In reply to Kent Overstreet

From: 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
index f6cd0f909d..c74b737699 100644
@@ -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
Reviewed-by on v1 12/20
#35 lore raw

Re: [PATCH 12/20] bcache: move closures to lib/

In reply to Kent Overstreet

Hi,

LGTM.
I have a couple of small nits below...

Quoted 27 lines · Kent Overstreet

From: 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%)
lib/Kconfig.debug +0 -0
index ce51d4dc68..3ee25d5dae 100644

lib/Kconfig.debug config DEBUG_NOTIFIERS · line 1641

Quoted 5 lines
lib/Kconfig.debug +2 -0
@@ -1637,6 +1637,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)"

widgets

lib/Kconfig.debug config DEBUG_NOTIFIERS · line 1647

Quoted 6 lines
lib/Kconfig.debug +6 -0
+	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.

Indent those 3 help text lines with 2 additional spaces, please, as documented and as is done in (most of) the rest of this file.

With those fixed:

thanks.

Signature · 1 line
~Randy
author maintainer
#36 lore raw

Re: [PATCH 12/20] bcache: move closures to lib/

Quoted 11 lines · Randy Dunlap

lib/Kconfig.debug config DEBUG_NOTIFIERS · line 1647

Quoted 7 lines

From: Kent Overstreet <[email protected]>

lib/Kconfig.debug +6 -0
+	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.

Indent those 3 help text lines with 2 additional spaces, please, as documented and as is done in (most of) the rest of this file.

Quoted 3 lines

With those fixed:

Ack

#26 lore raw

Re: [PATCH 10/20] lib: Export errname

In reply to Kent Overstreet
Quoted 3 lines · Kent Overstreet

errname() returns the name of an errcode; this functionality is otherwise only available for error pointers via %pE - bcachefs uses this for better error messages.

Interesting that this exists! It seems you meant %pe, not %pE, though.

- Eric

#5 lore raw

Re: [PATCH 04/20] block: Add some exports for bcachefs

In reply to Kent Overstreet
Quoted 3 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

- bio_set_pages_dirty(), bio_check_pages_dirty() - dio path

Why? We've so far have been able to get away without file systems reinventing their own DIO path. I'd really like to keep it that way, so if you think the iomap dio code should be improved please explain why. And please also cycle the fsdevel list in.

Quoted 3 lines

- 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()

These look ok to me to go in when the actual user shows up.

#11 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Kent Overstreet
Quoted 6 lines · Kent Overstreet

bio_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.

So, we've been trying really hard to always make sure to pass a bdev to anything that allocates a bio, mostly due due the fact that we actually derive information like the blk-cgroup associations from it.

The whole blk-cgroup stuff is actually a problem for non-trivial multi-device setups. XFS gets away fine because each file just sits on either the main or RT device and no user I/O goes to the log device, and btrfs papers over it in a weird way by always associating with the last added device, which is in many ways gross and wrong, but at least satisfies the assumptions made in blk-cgroup.

How do you plan to deal with this? Because I really don't want folks just to go ahead and ignore the issues, we need to actually sort this out.

#18 lore raw

Re: [PATCH 06/20] block: Bring back zero_fill_bio_iter

In reply to Kent Overstreet
Quoted 5 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

This reverts 6f822e1b5d9dda3d20e87365de138046e3baa03a - this helper is used by bcachefs.

Quoted 7 lines
---
 block/bio.c         | 6 +++---
 include/linux/bio.h | 7 ++++++-
 2 files changed, 9 insertions(+), 4 deletions(-)

I really don't see any point in offering this in the block layer. By the lack of any other caller it obviously isn't such a generic and always used helper, but more importantly it literally is three lines of code to implement it.

author maintainer
#12 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Christoph Hellwig
Quoted 19 lines · Christoph Hellwig
Quoted 6 lines · Kent Overstreet

bio_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.

So, we've been trying really hard to always make sure to pass a bdev to anything that allocates a bio, mostly due due the fact that we actually derive information like the blk-cgroup associations from it.

The whole blk-cgroup stuff is actually a problem for non-trivial multi-device setups. XFS gets away fine because each file just sits on either the main or RT device and no user I/O goes to the log device, and btrfs papers over it in a weird way by always associating with the last added device, which is in many ways gross and wrong, but at least satisfies the assumptions made in blk-cgroup.

Quoted 3 lines

How do you plan to deal with this? Because I really don't want folks just to go ahead and ignore the issues, we need to actually sort this out.

Doing the blk-cgroup association at bio alloc time sounds broken to me, because of stacking block devices - why was the association not done at generic_make_request() time?

author maintainer
#19 lore raw

Re: [PATCH 06/20] block: Bring back zero_fill_bio_iter

In reply to Christoph Hellwig
Quoted 14 lines · Christoph Hellwig
Quoted 5 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

This reverts 6f822e1b5d9dda3d20e87365de138046e3baa03a - this helper is used by bcachefs.

Quoted 7 lines
---
 block/bio.c         | 6 +++---
 include/linux/bio.h | 7 ++++++-
 2 files changed, 9 insertions(+), 4 deletions(-)
Quoted 4 lines

I really don't see any point in offering this in the block layer. By the lack of any other caller it obviously isn't such a generic and always used helper, but more importantly it literally is three lines of code to implement it.

And yet, we've had a subtle bug introduced in that code that took quite awhile to be fixed - I'm not pro code duplication in general and I don't think this is a good place to start.

#9 lore raw

Re: [PATCH 04/20] block: Add some exports for bcachefs

In reply to Kent Overstreet
Quoted 2 lines · Kent Overstreet

- bio_add_folio() - this should definitely be exported for everyone, it's the modern version of bio_add_page()

Looks like this one got added in cd57b77197a4, so it just needs to be dropped from the changelog.

author maintainer
#6 lore raw

Re: [PATCH 04/20] block: Add some exports for bcachefs

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

- bio_set_pages_dirty(), bio_check_pages_dirty() - dio path

Why? We've so far have been able to get away without file systems reinventing their own DIO path. I'd really like to keep it that way, so if you think the iomap dio code should be improved please explain why. And please also cycle the fsdevel list in.

It's been discussed at length why bcachefs doesn't use iomap.

In short, iomap is heavily callback based, the bcachefs io paths are not - we pass around data structures instead. I discussed this with people when iomap was first being written, but iomap ended up being a much more conservative approach, more in line with the old buffer heads code where the generic code calls into the filesystem to obtain mappings.

I'm gradually convincing people of the merits of the bcachefs approach - in particular reducing indirect function calls is getting more attention these days.

#44 lore raw

Re: [PATCH 19/20] lib/generic-radix-tree.c: Add a missing include

In reply to Kent Overstreet
Quoted 3 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

We now need linux/limits.h for SIZE_MAX.

I think this should be squashed into the previous commit

Quoted 21 lines
---
 include/linux/generic-radix-tree.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/generic-radix-tree.h +1 -0
index 63080822dc..f6cd0f909d 100644
@@ -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
author maintainer
#45 lore raw

Re: [PATCH 19/20] lib/generic-radix-tree.c: Add a missing include

Quoted 6 lines · Matthew Wilcox
Quoted 3 lines · Kent Overstreet

From: Kent Overstreet <[email protected]>

We now need linux/limits.h for SIZE_MAX.

I think this should be squashed into the previous commit

No objection...

#7 lore raw

Re: [PATCH 04/20] block: Add some exports for bcachefs

In reply to Kent Overstreet
Quoted 10 lines · Kent Overstreet

In short, iomap is heavily callback based, the bcachefs io paths are not - we pass around data structures instead. I discussed this with people when iomap was first being written, but iomap ended up being a much more conservative approach, more in line with the old buffer heads code where the generic code calls into the filesystem to obtain mappings.

I'm gradually convincing people of the merits of the bcachefs approach - in particular reducing indirect function calls is getting more attention these days.

FYI, Matthew has had patches that convert iomap to be an iterator, and I've massage the first half of them and actuall got them in before. I'd much rather finish off that work (even if only for direct I/O initially) than adding another direct I/O code. But even with out that we should be able to easily pass more private data, in fact btrfs makes pretty heavy use of that.

#20 lore raw

Re: [PATCH 06/20] block: Bring back zero_fill_bio_iter

In reply to Kent Overstreet
Quoted 3 lines · Kent Overstreet

And yet, we've had a subtle bug introduced in that code that took quite awhile to be fixed - I'm not pro code duplication in general and I don't think this is a good place to start.

I'm not sure arguing for adding a helper your can triviall implement yourself really helps to streamline your upstreaming process.

#13 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Kent Overstreet
Quoted 3 lines · Kent Overstreet

Doing the blk-cgroup association at bio alloc time sounds broken to me, because of stacking block devices - why was the association not done at generic_make_request() time?

Because blk-cgroup not only works at the lowest level in the stack, but also for stackable block devices. It's not a design decision I particularly agree with, but it's been there forever.

So we need to assign it when creating the bio (we used to do it at submission time, but the way it was done was horribly ineffcient, that's why I moved it to allocation time), and then when hitting a stacked device it get reassinged (which still is horribly inefficient).

author maintainer
#8 lore raw

Re: [PATCH 04/20] block: Add some exports for bcachefs

In reply to Christoph Hellwig
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Kent Overstreet

In short, iomap is heavily callback based, the bcachefs io paths are not - we pass around data structures instead. I discussed this with people when iomap was first being written, but iomap ended up being a much more conservative approach, more in line with the old buffer heads code where the generic code calls into the filesystem to obtain mappings.

I'm gradually convincing people of the merits of the bcachefs approach - in particular reducing indirect function calls is getting more attention these days.

Quoted 6 lines

FYI, Matthew has had patches that convert iomap to be an iterator, and I've massage the first half of them and actuall got them in before. I'd much rather finish off that work (even if only for direct I/O initially) than adding another direct I/O code. But even with out that we should be able to easily pass more private data, in fact btrfs makes pretty heavy use of that.

That's wonderful, but getting iomap up to the level of what bcachefs needs is still going to be a pretty big project and it's not going to be my highest priority.

bcachefs also hangs more state off of the pagecache, in bcachefs's equivvalent of iomap_page - we store reservations for dirty data there and a few other things, which means the buffered IO paths don't have to walk any other data structures.

I think that's another idea you guys will want to steal, but a higher priority for me is getting a proper FUSE port done - and making bcachefs more tightly weddded to VFS library code is not likely to make that process any easier.

Once a proper fuse port is done and we know what that looks like will be a better time for some consolidation.

author maintainer
#14 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Kent Overstreet

Doing the blk-cgroup association at bio alloc time sounds broken to me, because of stacking block devices - why was the association not done at generic_make_request() time?

Because blk-cgroup not only works at the lowest level in the stack, but also for stackable block devices. It's not a design decision I particularly agree with, but it's been there forever.

You're setting the association only to the highest block device in the stack - how on earth is it supposed to work with anything lower?

And looking at bio_associate_blkg(), this code looks completely broken. It's checking bio->bi_blkg, but that's just been set to NULL in bio_init().

And this is your code, so I think you need to go over this again.

Anyways, bio_associate_blkg() is also called by bio_set_dev(), which means passing the block device to bio_init() was a completely pointless change. bcachefs uses bio_set_dev(), so everything is fine.

author maintainer
#21 lore raw

Re: [PATCH 06/20] block: Bring back zero_fill_bio_iter

In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Kent Overstreet

And yet, we've had a subtle bug introduced in that code that took quite awhile to be fixed - I'm not pro code duplication in general and I don't think this is a good place to start.

I'm not sure arguing for adding a helper your can triviall implement yourself really helps to streamline your upstreaming process.

I gave you my engineering reasons, you're the one who's arguing.

And to make everything perfectly clear: this is code that I originally wrote, and then you started changing without CCing me - your patch that deleted zero_fill_bio_iter() never should've gone in.

#15 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

In reply to Kent Overstreet
Quoted 6 lines · Kent Overstreet
Quoted 3 lines

Because blk-cgroup not only works at the lowest level in the stack, but also for stackable block devices. It's not a design decision I particularly agree with, but it's been there forever.

You're setting the association only to the highest block device in the stack - how on earth is it supposed to work with anything lower?

Hey, ask the cgroup folks as they come up with it. I'm not going to defend the logic here.

Quoted 3 lines

And looking at bio_associate_blkg(), this code looks completely broken. It's checking bio->bi_blkg, but that's just been set to NULL in bio_init().

It's checking bi_blkg because it can also be called from bio_set_dev.

Quoted 1 line

And this is your code, so I think you need to go over this again.

It's "my code" in the sene of that I did one big round of unwinding the even bigger mess that was there. There is another few rounds needed for the code to vaguely make sense.

author maintainer
#16 lore raw

Re: [PATCH 05/20] block: Allow bio_iov_iter_get_pages() with bio->bi_bdev unset

Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Kent Overstreet
Quoted 3 lines

Because blk-cgroup not only works at the lowest level in the stack, but also for stackable block devices. It's not a design decision I particularly agree with, but it's been there forever.

You're setting the association only to the highest block device in the stack - how on earth is it supposed to work with anything lower?

Quoted 8 lines

Hey, ask the cgroup folks as they come up with it. I'm not going to defend the logic here.

Quoted 3 lines

And looking at bio_associate_blkg(), this code looks completely broken. It's checking bio->bi_blkg, but that's just been set to NULL in bio_init().

It's checking bi_blkg because it can also be called from bio_set_dev.

So bio_set_dev() has subtly different behaviour than passing the block device to bio_init()?

That's just broken.

Quoted 6 lines
Quoted 2 lines

And this is your code, so I think you need to go over this again.

It's "my code" in the sene of that I did one big round of unwinding the even bigger mess that was there. There is another few rounds needed for the code to vaguely make sense.

Well, I'll watch for those patches then...

reviewer
#28 lore raw

Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

In reply to Kent Overstreet
Quoted 24 lines · Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
  kernel/locking/osq_lock.c | 2 ++
  1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +1 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 
  	return false;
  }
+EXPORT_SYMBOL_GPL(osq_lock);
 
  void osq_unlock(struct optimistic_spin_queue *lock)
  {

kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234

Quoted 4 lines
kernel/locking/osq_lock.c +1 -0
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
  	if (next)
  		WRITE_ONCE(next->locked, 1);
  }
+EXPORT_SYMBOL_GPL(osq_unlock);

Have you considered extending the current rw_semaphore to support a SIX lock semantics? There are a number of instances in the kernel that a up_read() is followed by a down_write(). Basically, the code try to upgrade the lock from read to write. I have been thinking about adding a upgrade_read() API to do that. However, the concern that I had was that another writer may come in and make modification before the reader can be upgraded to have exclusive write access and will make the task to repeat what has been done in the read lock part. By adding a read with intent to upgrade to write, we can have that guarantee.

With that said, I would prefer to keep osq_{lock/unlock} for internal use by some higher level locking primitives - mutex, rwsem and rt_mutex.

Cheers,
Longman

author maintainer
#29 lore raw

Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

Quoted 29 lines · Waiman Long
Quoted 22 lines · Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
  kernel/locking/osq_lock.c | 2 ++
  1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +1 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
  	return false;
  }
+EXPORT_SYMBOL_GPL(osq_lock);
  void osq_unlock(struct optimistic_spin_queue *lock)
  {

kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234

Quoted 4 lines
kernel/locking/osq_lock.c +1 -0
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
  	if (next)
  		WRITE_ONCE(next->locked, 1);
  }
+EXPORT_SYMBOL_GPL(osq_unlock);

Have you considered extending the current rw_semaphore to support a SIX lock

Quoted 8 lines

semantics? There are a number of instances in the kernel that a up_read() is followed by a down_write(). Basically, the code try to upgrade the lock from read to write. I have been thinking about adding a upgrade_read() API to do that. However, the concern that I had was that another writer may come in and make modification before the reader can be upgraded to have exclusive write access and will make the task to repeat what has been done in the read lock part. By adding a read with intent to upgrade to write, we can have that guarantee.

It's been discussed, Linus had the same thought.

But it'd be a massive change to the rw semaphore code; this "read with intent" really is a third lock state which needs all the same lock/trylock/unlock paths, and with the way rw semaphore has separate entry points for read and write it'd be a _ton_ of new code. It really touches everything - waitlist handling included.

And six locks have several other features that bcachefs needs, and other users may also end up wanting, that rw semaphores don't have; the two main features being a percpu read lock mode and support for an external cycle detector (which requires exposing lock waitlists, with some guarantees about how those waitlists are used).

Quoted 2 lines

With that said, I would prefer to keep osq_{lock/unlock} for internal use by some higher level locking primitives - mutex, rwsem and rt_mutex.

Yeah, I'm aware, but it seems like exposing osq_(lock|unlock) is the most palatable solution for now. Long term, I'd like to get six locks promoted to kernel/locking.

reviewer
#30 lore raw

Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

Quoted 37 lines · Kent Overstreet
Quoted 28 lines · Waiman Long
Quoted 22 lines · Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
   kernel/locking/osq_lock.c | 2 ++
   1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +1 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
   	return false;
   }
+EXPORT_SYMBOL_GPL(osq_lock);
   void osq_unlock(struct optimistic_spin_queue *lock)
   {

kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234

Quoted 4 lines
kernel/locking/osq_lock.c +1 -0
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
   	if (next)
   		WRITE_ONCE(next->locked, 1);
   }
+EXPORT_SYMBOL_GPL(osq_unlock);

Have you considered extending the current rw_semaphore to support a SIX lock

Quoted 8 lines

semantics? There are a number of instances in the kernel that a up_read() is followed by a down_write(). Basically, the code try to upgrade the lock from read to write. I have been thinking about adding a upgrade_read() API to do that. However, the concern that I had was that another writer may come in and make modification before the reader can be upgraded to have exclusive write access and will make the task to repeat what has been done in the read lock part. By adding a read with intent to upgrade to write, we can have that guarantee.

Quoted 7 lines

It's been discussed, Linus had the same thought.

But it'd be a massive change to the rw semaphore code; this "read with intent" really is a third lock state which needs all the same lock/trylock/unlock paths, and with the way rw semaphore has separate entry points for read and write it'd be a _ton_ of new code. It really touches everything - waitlist handling included.

Yes, it is a major change, but I had done that before and it is certainly doable. There are spare bits in the low byte of rwsem->count that can be used as an intent bit. We also need to add a new rwsem_wake_type for that for waitlist handling.

Quoted 6 lines

And six locks have several other features that bcachefs needs, and other users may also end up wanting, that rw semaphores don't have; the two main features being a percpu read lock mode and support for an external cycle detector (which requires exposing lock waitlists, with some guarantees about how those waitlists are used).

Can you provide more information about those features?

Quoted 6 lines
Quoted 3 lines

With that said, I would prefer to keep osq_{lock/unlock} for internal use by some higher level locking primitives - mutex, rwsem and rt_mutex.

Yeah, I'm aware, but it seems like exposing osq_(lock|unlock) is the most palatable solution for now. Long term, I'd like to get six locks promoted to kernel/locking.

Your SIX overlaps with rwsem in term of features. So we will have to somehow merge them instead of having 2 APIs with somewhat similar functionality.

Cheers,
Longman

Quoted 1 line
author maintainer
#31 lore raw

Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

Quoted 62 lines · Waiman Long
Quoted 37 lines · Kent Overstreet
Quoted 28 lines · Waiman Long
Quoted 22 lines · Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
   kernel/locking/osq_lock.c | 2 ++
   1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +1 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
   	return false;
   }
+EXPORT_SYMBOL_GPL(osq_lock);
   void osq_unlock(struct optimistic_spin_queue *lock)
   {

kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234

Quoted 4 lines
kernel/locking/osq_lock.c +1 -0
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
   	if (next)
   		WRITE_ONCE(next->locked, 1);
   }
+EXPORT_SYMBOL_GPL(osq_unlock);

Have you considered extending the current rw_semaphore to support a SIX lock

Quoted 8 lines

semantics? There are a number of instances in the kernel that a up_read() is followed by a down_write(). Basically, the code try to upgrade the lock from read to write. I have been thinking about adding a upgrade_read() API to do that. However, the concern that I had was that another writer may come in and make modification before the reader can be upgraded to have exclusive write access and will make the task to repeat what has been done in the read lock part. By adding a read with intent to upgrade to write, we can have that guarantee.

Quoted 7 lines

It's been discussed, Linus had the same thought.

But it'd be a massive change to the rw semaphore code; this "read with intent" really is a third lock state which needs all the same lock/trylock/unlock paths, and with the way rw semaphore has separate entry points for read and write it'd be a _ton_ of new code. It really touches everything - waitlist handling included.

Yes, it is a major change, but I had done that before and it is certainly doable. There are spare bits in the low byte of rwsem->count that can be used as an intent bit. We also need to add a new rwsem_wake_type for that for waitlist handling.

Quoted 6 lines

And six locks have several other features that bcachefs needs, and other users may also end up wanting, that rw semaphores don't have; the two main features being a percpu read lock mode and support for an external cycle detector (which requires exposing lock waitlists, with some guarantees about how those waitlists are used).

Can you provide more information about those features?

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

With that said, I would prefer to keep osq_{lock/unlock} for internal use by some higher level locking primitives - mutex, rwsem and rt_mutex.

Yeah, I'm aware, but it seems like exposing osq_(lock|unlock) is the most palatable solution for now. Long term, I'd like to get six locks promoted to kernel/locking.

Your SIX overlaps with rwsem in term of features. So we will have to somehow merge them instead of having 2 APIs with somewhat similar functionality.

Waiman, if you think you can add all the features of six locks to rwsem, knock yourself out - but right now this is a vaporware idea for you, not something I can seriously entertain. I'm looking to merge bcachefs next cycle, not sit around and bikeshed for the next six months.

If you start making a serious effort on adding those features to rwsem I'll start walking you through everything six locks has, but right now this is a major digression on a patch that just exports two symbols.

#32 lore raw

[NAK] Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

Quoted 69 lines · Kent Overstreet
Quoted 68 lines · Waiman Long
Quoted 37 lines · Kent Overstreet
Quoted 28 lines · Waiman Long
Quoted 22 lines · Kent Overstreet

These are used by bcachefs's six locks.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Boqun Feng <[email protected]>
---
   kernel/locking/osq_lock.c | 2 ++
   1 file changed, 2 insertions(+)
kernel/locking/osq_lock.c +1 -0
index d5610ad52b..b752ec5cc6 100644
@@ -203,6 +203,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
   	return false;
   }
+EXPORT_SYMBOL_GPL(osq_lock);
   void osq_unlock(struct optimistic_spin_queue *lock)
   {

kernel/locking/osq_lock.c void osq_unlock(struct optimistic_spin_queue *lock) · line 234

Quoted 4 lines
kernel/locking/osq_lock.c +1 -0
@@ -230,3 +231,4 @@ void osq_unlock(struct optimistic_spin_queue *lock)
   	if (next)
   		WRITE_ONCE(next->locked, 1);
   }
+EXPORT_SYMBOL_GPL(osq_unlock);

Have you considered extending the current rw_semaphore to support a SIX lock

Quoted 8 lines

semantics? There are a number of instances in the kernel that a up_read() is followed by a down_write(). Basically, the code try to upgrade the lock from read to write. I have been thinking about adding a upgrade_read() API to do that. However, the concern that I had was that another writer may come in and make modification before the reader can be upgraded to have exclusive write access and will make the task to repeat what has been done in the read lock part. By adding a read with intent to upgrade to write, we can have that guarantee.

Quoted 7 lines

It's been discussed, Linus had the same thought.

But it'd be a massive change to the rw semaphore code; this "read with intent" really is a third lock state which needs all the same lock/trylock/unlock paths, and with the way rw semaphore has separate entry points for read and write it'd be a _ton_ of new code. It really touches everything - waitlist handling included.

Yes, it is a major change, but I had done that before and it is certainly doable. There are spare bits in the low byte of rwsem->count that can be used as an intent bit. We also need to add a new rwsem_wake_type for that for waitlist handling.

Quoted 6 lines

And six locks have several other features that bcachefs needs, and other users may also end up wanting, that rw semaphores don't have; the two main features being a percpu read lock mode and support for an external cycle detector (which requires exposing lock waitlists, with some guarantees about how those waitlists are used).

Can you provide more information about those features?

Quoted 6 lines
Quoted 3 lines

With that said, I would prefer to keep osq_{lock/unlock} for internal use by some higher level locking primitives - mutex, rwsem and rt_mutex.

Yeah, I'm aware, but it seems like exposing osq_(lock|unlock) is the most palatable solution for now. Long term, I'd like to get six locks promoted to kernel/locking.

Quoted 7 lines
Quoted 2 lines

Your SIX overlaps with rwsem in term of features. So we will have to somehow merge them instead of having 2 APIs with somewhat similar functionality.

Waiman, if you think you can add all the features of six locks to rwsem, knock yourself out - but right now this is a vaporware idea for you, not something I can seriously entertain. I'm looking to merge bcachefs next cycle, not sit around and bikeshed for the next six months.

That's an entirely inappropriate response to valid review feedback.

Not having two overlapping locking facilities is not 'bikeshedding' at all ...

Quoted 3 lines

If you start making a serious effort on adding those features to rwsem I'll start walking you through everything six locks has, but right now this is a major digression on a patch that just exports two symbols.

In Linux the burden of work is on people submitting new code, not on reviewers. The rule is that you should not reinvent the wheel in new features - extend existing locking facilities please.

Waiman gave you some pointers as to how to extend rwsems.

Meanwhile, NAK on the export of osq_(lock|unlock):

Ie. NAK on this commit in linux-next:

    97da2065b7cb ("locking/osq: Export osq_(lock|unlock)")

This is an internal function of Linux locking subsystem we would not like to expose or export.

This commit was applied without an Ack or Reviewed-by by a locking subsystem maintainer or reviewer (which Waiman Long is).

Thanks,

Ingo

author maintainer
#33 lore raw

Re: [NAK] Re: [PATCH 11/20] locking/osq: Export osq_(lock|unlock)

Quoted 8 lines · Ingo Molnar
Quoted 4 lines

Waiman, if you think you can add all the features of six locks to rwsem, knock yourself out - but right now this is a vaporware idea for you, not something I can seriously entertain. I'm looking to merge bcachefs next cycle, not sit around and bikeshed for the next six months.

That's an entirely inappropriate response to valid review feedback.

Not having two overlapping locking facilities is not 'bikeshedding' at all ...

Well, there was already a long off-list discussion about adding six lock features to rwsem.

Basically, it looks to me like a total redesign of rwsem in order to do it correctly, and I don't think that would fly. The rwsem code has separate entrypoints for every lock state, and adding a third lock state would at a minimum add a lot of new - nearly duplicate - code.

There's also features and optimizations in six locks that rwsem doesn't have, and it's not clear to me that it would be appropriate to add them to rwsem - each of them would need real discussion. The big ones are:

- percpu reader mode, used for locks for interior nodes and subvolume keys in bcachefs
- exposing of waitlist entries (and this requires nontrivial guarantees to do correctly!), so that bcachefs can do cycle detection deadlock avoidance on top.

In short, this would _not_ be a small project, and I think the saner approach if we really did want to condense down to a single locking implementation would be to replace rwsem with six locks. But before even contemplating that we'd want to see six locks getting wider usage and testing first.

Hence why we're at leaving six locks in fs/bcachefs/ for now.

Quoted 11 lines
Quoted 3 lines

If you start making a serious effort on adding those features to rwsem I'll start walking you through everything six locks has, but right now this is a major digression on a patch that just exports two symbols.

In Linux the burden of work is on people submitting new code, not on reviewers. The rule is that you should not reinvent the wheel in new features - extend existing locking facilities please.

Waiman gave you some pointers as to how to extend rwsems.

Meanwhile, NAK on the export of osq_(lock|unlock):

Perhaps we could get some justification for why you want osq locks to be private?

My initial pull request had six locks in kernel/locking/, specifically to keep osq locks private, as requested by locking people (some years back). But since Linus shot that down, I need an alternative.

If you're really dead set against exporting osq locks (and again, why?), my only alternative will be to either take optimistic spinning out of six locks, or implement optimistic spinning another way (which is something I was already looking at before; the way lock handoff works in six locks now makes that an attractive idea anyways, but of course the devil is in the details with locking code).