* @round_robin: Allocate bits in strict round-robin order.
         */
        bool round_robin;
+
+       /**
+        * @min_shallow_depth: The minimum shallow depth which may be passed to
+        * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
+        */
+       unsigned int min_shallow_depth;
 };
 
 /**
  * @shallow_depth: The maximum number of bits to allocate from a single word.
  * See sbitmap_get_shallow().
  *
+ * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
+ * initializing @sbq.
+ *
  * Return: Non-negative allocated bit number if successful, -1 otherwise.
  */
 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  * @shallow_depth: The maximum number of bits to allocate from a single word.
  * See sbitmap_get_shallow().
  *
+ * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
+ * initializing @sbq.
+ *
  * Return: Non-negative allocated bit number if successful, -1 otherwise.
  */
 static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
        return nr;
 }
 
+/**
+ * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the
+ * minimum shallow depth that will be used.
+ * @sbq: Bitmap queue in question.
+ * @min_shallow_depth: The minimum shallow depth that will be passed to
+ * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
+ *
+ * sbitmap_queue_clear() batches wakeups as an optimization. The batch size
+ * depends on the depth of the bitmap. Since the shallow allocation functions
+ * effectively operate with a different depth, the shallow depth must be taken
+ * into account when calculating the batch size. This function must be called
+ * with the minimum shallow depth that will be used. Failure to do so can result
+ * in missed wakeups.
+ */
+void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
+                                    unsigned int min_shallow_depth);
+
 /**
  * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
  * &struct sbitmap_queue.
 
 }
 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
 
-static unsigned int sbq_calc_wake_batch(unsigned int depth)
+static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
+                                       unsigned int depth)
 {
        unsigned int wake_batch;
+       unsigned int shallow_depth;
 
        /*
         * For each batch, we wake up one queue. We need to make sure that our
-        * batch size is small enough that the full depth of the bitmap is
-        * enough to wake up all of the queues.
+        * batch size is small enough that the full depth of the bitmap,
+        * potentially limited by a shallow depth, is enough to wake up all of
+        * the queues.
+        *
+        * Each full word of the bitmap has bits_per_word bits, and there might
+        * be a partial word. There are depth / bits_per_word full words and
+        * depth % bits_per_word bits left over. In bitwise arithmetic:
+        *
+        * bits_per_word = 1 << shift
+        * depth / bits_per_word = depth >> shift
+        * depth % bits_per_word = depth & ((1 << shift) - 1)
+        *
+        * Each word can be limited to sbq->min_shallow_depth bits.
         */
-       wake_batch = SBQ_WAKE_BATCH;
-       if (wake_batch > depth / SBQ_WAIT_QUEUES)
-               wake_batch = max(1U, depth / SBQ_WAIT_QUEUES);
+       shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
+       depth = ((depth >> sbq->sb.shift) * shallow_depth +
+                min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
+       wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
+                            SBQ_WAKE_BATCH);
 
        return wake_batch;
 }
                        *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
        }
 
-       sbq->wake_batch = sbq_calc_wake_batch(depth);
+       sbq->min_shallow_depth = UINT_MAX;
+       sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
        atomic_set(&sbq->wake_index, 0);
 
        sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
 }
 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
 
-void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
+static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
+                                           unsigned int depth)
 {
-       unsigned int wake_batch = sbq_calc_wake_batch(depth);
+       unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
        int i;
 
        if (sbq->wake_batch != wake_batch) {
                for (i = 0; i < SBQ_WAIT_QUEUES; i++)
                        atomic_set(&sbq->ws[i].wait_cnt, 1);
        }
+}
+
+void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
+{
+       sbitmap_queue_update_wake_batch(sbq, depth);
        sbitmap_resize(&sbq->sb, depth);
 }
 EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
 }
 EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
 
+void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
+                                    unsigned int min_shallow_depth)
+{
+       sbq->min_shallow_depth = min_shallow_depth;
+       sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
+}
+EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
+
 static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
 {
        int i, wake_index;
        seq_puts(m, "}\n");
 
        seq_printf(m, "round_robin=%d\n", sbq->round_robin);
+       seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
 }
 EXPORT_SYMBOL_GPL(sbitmap_queue_show);