Skip to contentSkip to conversation

lib/sbitmap: define swap_lock as raw_spinlock_t

PATCH Applied Ming Lei 3 participants 3 messages Sep 19, 2024 – Sep 20, 2024

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 lib/sbitmap: define swap_lock as raw_spinlock_t

When called from sbitmap_queue_get(), sbitmap_deferred_clear() may be run with preempt disabled. In RT kernel, spin_lock() can sleep, then warning of "BUG: sleeping function called from invalid context" can be triggered.

Fix it by replacing it with raw_spin_lock.

Cc Yang Yang <[email protected]>
Fixes 72d04bdcf3f7 ("sbitmap: fix io hung due to race on sbitmap_word::cleared")
---
 include/linux/sbitmap.h | 2 +-
 lib/sbitmap.c           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/sbitmap.h +1 -1
index c09cdcc99471..189140bf11fc 100644
@@ -40,7 +40,7 @@ struct sbitmap_word {
 	/**
 	 * @swap_lock: serializes simultaneous updates of ->word and ->cleared
 	 */
-	spinlock_t swap_lock;
+	raw_spinlock_t swap_lock;
 } ____cacheline_aligned_in_smp;
 
 /**
lib/sbitmap.c +2 -2
index 5e2e93307f0d..d3412984170c 100644
@@ -65,7 +65,7 @@ static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,
 {
 	unsigned long mask, word_mask;
 
-	guard(spinlock_irqsave)(&map->swap_lock);
+	guard(raw_spinlock_irqsave)(&map->swap_lock);
 
 	if (!map->cleared) {
 		if (depth == 0)
@@ -136,7 +136,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
 	}
 
 	for (i = 0; i < sb->map_nr; i++)
-		spin_lock_init(&sb->map[i].swap_lock);
+		raw_spin_lock_init(&sb->map[i].swap_lock);
 
 	return 0;
 }
Signature · 1 line
2.46.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 41 lines · Ming Lei

When called from sbitmap_queue_get(), sbitmap_deferred_clear() may be run with preempt disabled. In RT kernel, spin_lock() can sleep, then warning of "BUG: sleeping function called from invalid context" can be triggered.

Fix it by replacing it with raw_spin_lock.

Cc Yang Yang <[email protected]>
Fixes 72d04bdcf3f7 ("sbitmap: fix io hung due to race on sbitmap_word::cleared")
---
  include/linux/sbitmap.h | 2 +-
  lib/sbitmap.c           | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/sbitmap.h +1 -1
index c09cdcc99471..189140bf11fc 100644
@@ -40,7 +40,7 @@ struct sbitmap_word {
  	/**
  	 * @swap_lock: serializes simultaneous updates of ->word and ->cleared
  	 */
-	spinlock_t swap_lock;
+	raw_spinlock_t swap_lock;
  } ____cacheline_aligned_in_smp;
 
  /**
lib/sbitmap.c +1 -1
index 5e2e93307f0d..d3412984170c 100644
@@ -65,7 +65,7 @@ static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,
  {
  	unsigned long mask, word_mask;
 
-	guard(spinlock_irqsave)(&map->swap_lock);
+	guard(raw_spinlock_irqsave)(&map->swap_lock);
 
  	if (!map->cleared) {
  		if (depth == 0)

lib/sbitmap.c int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, · line 142

Quoted 8 lines
lib/sbitmap.c +1 -1
@@ -136,7 +136,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  	}
 
  	for (i = 0; i < sb->map_nr; i++)
-		spin_lock_init(&sb->map[i].swap_lock);
+		raw_spin_lock_init(&sb->map[i].swap_lock);
 
  	return 0;
  }

Looks good. Feel free to add:

Thanks.

maintainer Applied
#2 lore raw
In reply to Ming Lei
Quoted 8 lines · Ming Lei

When called from sbitmap_queue_get(), sbitmap_deferred_clear() may be run with preempt disabled. In RT kernel, spin_lock() can sleep, then warning of "BUG: sleeping function called from invalid context" can be triggered.

Fix it by replacing it with raw_spin_lock.

[...]

Applied, thanks!

[1/1] lib/sbitmap: define swap_lock as raw_spinlock_t

      commit: 65f666c6203600053478ce8e34a1db269a8701c9

Best regards,

Signature · 1 line
Jens Axboe