[PATCH 5/8] FOLD: don't use in_task() to decide for offloading

Christoph Hellwig
2026-04-09T16:03:41Z

As described in commit c99fab6e80b76, some block drivers might call
into ->bi_end_io from non-preemptible context. Copy and past the
logic from that commit, although having a core helper for it would
be nicer.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
include/linux/bio.h | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/include/linux/bio.h b/include/linux/bio.h
index 45c311e5ff71..72664807c757 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -375,6 +375,16 @@ static inline struct bio *bio_alloc(struct block_device *bdev,

void submit_bio(struct bio *bio);

+/* Offload from atomic contexts to minimize scheduling overhead */
+static inline bool bio_in_atomic(void)
+{
+ if (IS_ENABLED(CONFIG_PREEMPTION) && rcu_preempt_depth())
+ return true;
+ if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
+ return true;
+ return !preemptible();
+}
+
void __bio_complete_in_task(struct bio *bio);

/**
@@ -386,10 +396,11 @@ void __bio_complete_in_task(struct bio *bio);
*/
static inline bool bio_complete_in_task(struct bio *bio)
{
- if (in_task())
- return false;
- __bio_complete_in_task(bio);
- return true;
+ if (bio_in_atomic()) {
+ __bio_complete_in_task(bio);
+ return true;
+ }
+ return false;
}

extern void bio_endio(struct bio *);
--
2.47.3