2016-09-17 22:38:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Facebook
|
|
|
|
* Copyright (C) 2013-2014 Jens Axboe
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-03 16:57:00 +08:00
|
|
|
#include <linux/sched.h>
|
2016-09-17 16:28:25 +08:00
|
|
|
#include <linux/random.h>
|
2016-09-17 22:38:44 +08:00
|
|
|
#include <linux/sbitmap.h>
|
2017-01-26 06:32:13 +08:00
|
|
|
#include <linux/seq_file.h>
|
2016-09-17 22:38:44 +08:00
|
|
|
|
2018-12-12 09:39:41 +08:00
|
|
|
/*
|
|
|
|
* See if we have deferred clears that we can batch move
|
|
|
|
*/
|
|
|
|
static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index)
|
|
|
|
{
|
|
|
|
unsigned long mask, val;
|
|
|
|
unsigned long __maybe_unused flags;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
/* Silence bogus lockdep warning */
|
|
|
|
#if defined(CONFIG_LOCKDEP)
|
|
|
|
local_irq_save(flags);
|
|
|
|
#endif
|
|
|
|
spin_lock(&sb->map[index].swap_lock);
|
|
|
|
|
|
|
|
if (!sb->map[index].cleared)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First get a stable cleared mask, setting the old mask to 0.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
mask = sb->map[index].cleared;
|
|
|
|
} while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now clear the masked bits in our free word
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
val = sb->map[index].word;
|
|
|
|
} while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
out_unlock:
|
|
|
|
spin_unlock(&sb->map[index].swap_lock);
|
|
|
|
#if defined(CONFIG_LOCKDEP)
|
|
|
|
local_irq_restore(flags);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
|
|
|
|
gfp_t flags, int node)
|
|
|
|
{
|
|
|
|
unsigned int bits_per_word;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (shift < 0) {
|
|
|
|
shift = ilog2(BITS_PER_LONG);
|
|
|
|
/*
|
|
|
|
* If the bitmap is small, shrink the number of bits per word so
|
|
|
|
* we spread over a few cachelines, at least. If less than 4
|
|
|
|
* bits, just forget about it, it's not going to work optimally
|
|
|
|
* anyway.
|
|
|
|
*/
|
|
|
|
if (depth >= 4) {
|
|
|
|
while ((4U << shift) > depth)
|
|
|
|
shift--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bits_per_word = 1U << shift;
|
|
|
|
if (bits_per_word > BITS_PER_LONG)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
sb->shift = shift;
|
|
|
|
sb->depth = depth;
|
|
|
|
sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
|
|
|
|
|
|
|
|
if (depth == 0) {
|
|
|
|
sb->map = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
treewide: kzalloc_node() -> kcalloc_node()
The kzalloc_node() function has a 2-factor argument form, kcalloc_node(). This
patch replaces cases of:
kzalloc_node(a * b, gfp, node)
with:
kcalloc_node(a * b, gfp, node)
as well as handling cases of:
kzalloc_node(a * b * c, gfp, node)
with:
kzalloc_node(array3_size(a, b, c), gfp, node)
as it's slightly less ugly than:
kcalloc_node(array_size(a, b), c, gfp, node)
This does, however, attempt to ignore constant size factors like:
kzalloc_node(4 * 1024, gfp, node)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc_node(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc_node(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc_node(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc_node(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc_node
+ kcalloc_node
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc_node(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc_node(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc_node(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc_node(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc_node(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc_node(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc_node(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc_node(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc_node(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc_node(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc_node(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc_node(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc_node(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc_node(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc_node(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc_node(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc_node(C1 * C2 * C3, ...)
|
kzalloc_node(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc_node(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc_node(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc_node(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc_node(sizeof(THING) * C2, ...)
|
kzalloc_node(sizeof(TYPE) * C2, ...)
|
kzalloc_node(C1 * C2 * C3, ...)
|
kzalloc_node(C1 * C2, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc_node
+ kcalloc_node
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:04:20 +08:00
|
|
|
sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
|
2016-09-17 22:38:44 +08:00
|
|
|
if (!sb->map)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
|
|
|
sb->map[i].depth = min(depth, bits_per_word);
|
|
|
|
depth -= sb->map[i].depth;
|
2018-12-01 04:18:06 +08:00
|
|
|
spin_lock_init(&sb->map[i].swap_lock);
|
2016-09-17 22:38:44 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_init_node);
|
|
|
|
|
|
|
|
void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
|
|
|
|
{
|
|
|
|
unsigned int bits_per_word = 1U << sb->shift;
|
|
|
|
unsigned int i;
|
|
|
|
|
2018-12-12 09:39:41 +08:00
|
|
|
for (i = 0; i < sb->map_nr; i++)
|
|
|
|
sbitmap_deferred_clear(sb, i);
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
sb->depth = depth;
|
|
|
|
sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
|
|
|
sb->map[i].depth = min(depth, bits_per_word);
|
|
|
|
depth -= sb->map[i].depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_resize);
|
|
|
|
|
2017-04-14 15:59:58 +08:00
|
|
|
static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
|
|
|
|
unsigned int hint, bool wrap)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
|
|
|
unsigned int orig_hint = hint;
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
while (1) {
|
2017-04-14 15:59:58 +08:00
|
|
|
nr = find_next_zero_bit(word, depth, hint);
|
|
|
|
if (unlikely(nr >= depth)) {
|
2016-09-17 22:38:44 +08:00
|
|
|
/*
|
|
|
|
* We started with an offset, and we didn't reset the
|
|
|
|
* offset to 0 in a failure case, so start from 0 to
|
|
|
|
* exhaust the map.
|
|
|
|
*/
|
|
|
|
if (orig_hint && hint && wrap) {
|
|
|
|
hint = orig_hint = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-28 08:56:43 +08:00
|
|
|
if (!test_and_set_bit_lock(nr, word))
|
2016-09-17 22:38:44 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
hint = nr + 1;
|
2017-04-14 15:59:58 +08:00
|
|
|
if (hint >= depth - 1)
|
2016-09-17 22:38:44 +08:00
|
|
|
hint = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2018-12-01 04:18:06 +08:00
|
|
|
static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
|
|
|
|
unsigned int alloc_hint, bool round_robin)
|
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
do {
|
|
|
|
nr = __sbitmap_get_word(&sb->map[index].word,
|
|
|
|
sb->map[index].depth, alloc_hint,
|
|
|
|
!round_robin);
|
|
|
|
if (nr != -1)
|
|
|
|
break;
|
|
|
|
if (!sbitmap_deferred_clear(sb, index))
|
|
|
|
break;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
|
|
|
|
{
|
|
|
|
unsigned int i, index;
|
|
|
|
int nr = -1;
|
|
|
|
|
|
|
|
index = SB_NR_TO_INDEX(sb, alloc_hint);
|
|
|
|
|
2018-11-30 03:35:16 +08:00
|
|
|
/*
|
|
|
|
* Unless we're doing round robin tag allocation, just use the
|
|
|
|
* alloc_hint to find the right word index. No point in looping
|
|
|
|
* twice in find_next_zero_bit() for that case.
|
|
|
|
*/
|
|
|
|
if (round_robin)
|
|
|
|
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
|
|
|
|
else
|
|
|
|
alloc_hint = 0;
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
2018-12-01 04:18:06 +08:00
|
|
|
nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
|
|
|
|
round_robin);
|
2016-09-17 22:38:44 +08:00
|
|
|
if (nr != -1) {
|
|
|
|
nr += index << sb->shift;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Jump to next index. */
|
2018-11-30 03:35:16 +08:00
|
|
|
alloc_hint = 0;
|
|
|
|
if (++index >= sb->map_nr)
|
2016-09-17 22:38:44 +08:00
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_get);
|
|
|
|
|
2017-04-14 15:59:58 +08:00
|
|
|
int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
|
|
|
|
unsigned long shallow_depth)
|
|
|
|
{
|
|
|
|
unsigned int i, index;
|
|
|
|
int nr = -1;
|
|
|
|
|
|
|
|
index = SB_NR_TO_INDEX(sb, alloc_hint);
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
2018-12-12 09:39:41 +08:00
|
|
|
again:
|
2017-04-14 15:59:58 +08:00
|
|
|
nr = __sbitmap_get_word(&sb->map[index].word,
|
|
|
|
min(sb->map[index].depth, shallow_depth),
|
|
|
|
SB_NR_TO_BIT(sb, alloc_hint), true);
|
|
|
|
if (nr != -1) {
|
|
|
|
nr += index << sb->shift;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:39:41 +08:00
|
|
|
if (sbitmap_deferred_clear(sb, index))
|
|
|
|
goto again;
|
|
|
|
|
2017-04-14 15:59:58 +08:00
|
|
|
/* Jump to next index. */
|
|
|
|
index++;
|
|
|
|
alloc_hint = index << sb->shift;
|
|
|
|
|
|
|
|
if (index >= sb->map_nr) {
|
|
|
|
index = 0;
|
|
|
|
alloc_hint = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
bool sbitmap_any_bit_set(const struct sbitmap *sb)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
2018-12-12 09:39:41 +08:00
|
|
|
if (sb->map[i].word & ~sb->map[i].cleared)
|
2016-09-17 22:38:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
|
|
|
|
|
|
|
|
bool sbitmap_any_bit_clear(const struct sbitmap *sb)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
|
|
|
const struct sbitmap_word *word = &sb->map[i];
|
2018-12-12 09:39:41 +08:00
|
|
|
unsigned long mask = word->word & ~word->cleared;
|
2016-09-17 22:38:44 +08:00
|
|
|
unsigned long ret;
|
|
|
|
|
2018-12-12 09:39:41 +08:00
|
|
|
ret = find_first_zero_bit(&mask, word->depth);
|
2016-09-17 22:38:44 +08:00
|
|
|
if (ret < word->depth)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
|
|
|
|
|
2018-12-01 04:18:06 +08:00
|
|
|
static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
2016-09-19 21:34:08 +08:00
|
|
|
unsigned int i, weight = 0;
|
2016-09-17 22:38:44 +08:00
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
|
|
|
const struct sbitmap_word *word = &sb->map[i];
|
|
|
|
|
2018-12-01 04:18:06 +08:00
|
|
|
if (set)
|
|
|
|
weight += bitmap_weight(&word->word, word->depth);
|
|
|
|
else
|
|
|
|
weight += bitmap_weight(&word->cleared, word->depth);
|
2016-09-17 22:38:44 +08:00
|
|
|
}
|
|
|
|
return weight;
|
|
|
|
}
|
2018-12-01 04:18:06 +08:00
|
|
|
|
|
|
|
static unsigned int sbitmap_weight(const struct sbitmap *sb)
|
|
|
|
{
|
|
|
|
return __sbitmap_weight(sb, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int sbitmap_cleared(const struct sbitmap *sb)
|
|
|
|
{
|
|
|
|
return __sbitmap_weight(sb, false);
|
|
|
|
}
|
2016-09-17 22:38:44 +08:00
|
|
|
|
2017-01-26 06:32:13 +08:00
|
|
|
void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
|
|
|
|
{
|
|
|
|
seq_printf(m, "depth=%u\n", sb->depth);
|
2018-12-01 04:18:06 +08:00
|
|
|
seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb));
|
|
|
|
seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
|
2017-01-26 06:32:13 +08:00
|
|
|
seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
|
|
|
|
seq_printf(m, "map_nr=%u\n", sb->map_nr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_show);
|
|
|
|
|
|
|
|
static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
|
|
|
|
{
|
|
|
|
if ((offset & 0xf) == 0) {
|
|
|
|
if (offset != 0)
|
|
|
|
seq_putc(m, '\n');
|
|
|
|
seq_printf(m, "%08x:", offset);
|
|
|
|
}
|
|
|
|
if ((offset & 0x1) == 0)
|
|
|
|
seq_putc(m, ' ');
|
|
|
|
seq_printf(m, "%02x", byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
|
|
|
|
{
|
|
|
|
u8 byte = 0;
|
|
|
|
unsigned int byte_bits = 0;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sb->map_nr; i++) {
|
|
|
|
unsigned long word = READ_ONCE(sb->map[i].word);
|
|
|
|
unsigned int word_bits = READ_ONCE(sb->map[i].depth);
|
|
|
|
|
|
|
|
while (word_bits > 0) {
|
|
|
|
unsigned int bits = min(8 - byte_bits, word_bits);
|
|
|
|
|
|
|
|
byte |= (word & (BIT(bits) - 1)) << byte_bits;
|
|
|
|
byte_bits += bits;
|
|
|
|
if (byte_bits == 8) {
|
|
|
|
emit_byte(m, offset, byte);
|
|
|
|
byte = 0;
|
|
|
|
byte_bits = 0;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
word >>= bits;
|
|
|
|
word_bits -= bits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (byte_bits) {
|
|
|
|
emit_byte(m, offset, byte);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
if (offset)
|
|
|
|
seq_putc(m, '\n');
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
|
|
|
|
|
2018-05-10 08:16:31 +08:00
|
|
|
static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
|
|
|
|
unsigned int depth)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
|
|
|
unsigned int wake_batch;
|
2018-05-10 08:16:31 +08:00
|
|
|
unsigned int shallow_depth;
|
2016-09-17 22:38:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For each batch, we wake up one queue. We need to make sure that our
|
2018-05-10 08:16:31 +08:00
|
|
|
* 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.
|
2016-09-17 22:38:44 +08:00
|
|
|
*/
|
2018-05-10 08:16:31 +08:00
|
|
|
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);
|
2016-09-17 22:38:44 +08:00
|
|
|
|
|
|
|
return wake_batch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
|
2016-09-17 16:28:24 +08:00
|
|
|
int shift, bool round_robin, gfp_t flags, int node)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2016-09-17 16:28:23 +08:00
|
|
|
sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
|
|
|
|
if (!sbq->alloc_hint) {
|
|
|
|
sbitmap_free(&sbq->sb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-09-17 16:28:25 +08:00
|
|
|
if (depth && !round_robin) {
|
|
|
|
for_each_possible_cpu(i)
|
|
|
|
*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:16:31 +08:00
|
|
|
sbq->min_shallow_depth = UINT_MAX;
|
|
|
|
sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
|
2016-09-17 22:38:44 +08:00
|
|
|
atomic_set(&sbq->wake_index, 0);
|
2018-11-30 08:36:41 +08:00
|
|
|
atomic_set(&sbq->ws_active, 0);
|
2016-09-17 22:38:44 +08:00
|
|
|
|
2016-09-17 16:28:22 +08:00
|
|
|
sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
|
2016-09-17 22:38:44 +08:00
|
|
|
if (!sbq->ws) {
|
2016-09-17 16:28:23 +08:00
|
|
|
free_percpu(sbq->alloc_hint);
|
2016-09-17 22:38:44 +08:00
|
|
|
sbitmap_free(&sbq->sb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
|
|
|
|
init_waitqueue_head(&sbq->ws[i].wait);
|
|
|
|
atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
|
|
|
|
}
|
2016-09-17 16:28:24 +08:00
|
|
|
|
|
|
|
sbq->round_robin = round_robin;
|
2016-09-17 22:38:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
|
|
|
|
|
2018-05-10 08:16:31 +08:00
|
|
|
static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
|
|
|
|
unsigned int depth)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
2018-05-10 08:16:31 +08:00
|
|
|
unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
|
2017-01-19 03:55:22 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sbq->wake_batch != wake_batch) {
|
|
|
|
WRITE_ONCE(sbq->wake_batch, wake_batch);
|
|
|
|
/*
|
2018-05-25 01:00:39 +08:00
|
|
|
* Pairs with the memory barrier in sbitmap_queue_wake_up()
|
|
|
|
* to ensure that the batch size is updated before the wait
|
|
|
|
* counts.
|
2017-01-19 03:55:22 +08:00
|
|
|
*/
|
|
|
|
smp_mb__before_atomic();
|
|
|
|
for (i = 0; i < SBQ_WAIT_QUEUES; i++)
|
|
|
|
atomic_set(&sbq->ws[i].wait_cnt, 1);
|
|
|
|
}
|
2018-05-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
|
|
|
|
{
|
|
|
|
sbitmap_queue_update_wake_batch(sbq, depth);
|
2016-09-17 22:38:44 +08:00
|
|
|
sbitmap_resize(&sbq->sb, depth);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
|
|
|
|
|
2016-09-17 16:28:24 +08:00
|
|
|
int __sbitmap_queue_get(struct sbitmap_queue *sbq)
|
2016-09-17 16:28:23 +08:00
|
|
|
{
|
2016-09-17 16:28:26 +08:00
|
|
|
unsigned int hint, depth;
|
2016-09-17 16:28:23 +08:00
|
|
|
int nr;
|
|
|
|
|
|
|
|
hint = this_cpu_read(*sbq->alloc_hint);
|
2016-09-17 16:28:26 +08:00
|
|
|
depth = READ_ONCE(sbq->sb.depth);
|
|
|
|
if (unlikely(hint >= depth)) {
|
|
|
|
hint = depth ? prandom_u32() % depth : 0;
|
|
|
|
this_cpu_write(*sbq->alloc_hint, hint);
|
|
|
|
}
|
2016-09-17 16:28:24 +08:00
|
|
|
nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
|
2016-09-17 16:28:23 +08:00
|
|
|
|
|
|
|
if (nr == -1) {
|
|
|
|
/* If the map is full, a hint won't do us much good. */
|
|
|
|
this_cpu_write(*sbq->alloc_hint, 0);
|
2016-09-17 16:28:24 +08:00
|
|
|
} else if (nr == hint || unlikely(sbq->round_robin)) {
|
2016-09-17 16:28:23 +08:00
|
|
|
/* Only update the hint if we used it. */
|
|
|
|
hint = nr + 1;
|
2016-09-17 16:28:26 +08:00
|
|
|
if (hint >= depth - 1)
|
2016-09-17 16:28:23 +08:00
|
|
|
hint = 0;
|
|
|
|
this_cpu_write(*sbq->alloc_hint, hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
|
|
|
|
|
2017-04-14 15:59:58 +08:00
|
|
|
int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
|
|
|
|
unsigned int shallow_depth)
|
|
|
|
{
|
|
|
|
unsigned int hint, depth;
|
|
|
|
int nr;
|
|
|
|
|
2018-05-10 08:29:24 +08:00
|
|
|
WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
|
|
|
|
|
2017-04-14 15:59:58 +08:00
|
|
|
hint = this_cpu_read(*sbq->alloc_hint);
|
|
|
|
depth = READ_ONCE(sbq->sb.depth);
|
|
|
|
if (unlikely(hint >= depth)) {
|
|
|
|
hint = depth ? prandom_u32() % depth : 0;
|
|
|
|
this_cpu_write(*sbq->alloc_hint, hint);
|
|
|
|
}
|
|
|
|
nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
|
|
|
|
|
|
|
|
if (nr == -1) {
|
|
|
|
/* If the map is full, a hint won't do us much good. */
|
|
|
|
this_cpu_write(*sbq->alloc_hint, 0);
|
|
|
|
} else if (nr == hint || unlikely(sbq->round_robin)) {
|
|
|
|
/* Only update the hint if we used it. */
|
|
|
|
hint = nr + 1;
|
|
|
|
if (hint >= depth - 1)
|
|
|
|
hint = 0;
|
|
|
|
this_cpu_write(*sbq->alloc_hint, hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
|
|
|
|
|
2018-05-10 08:16:31 +08:00
|
|
|
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);
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
|
|
|
|
{
|
|
|
|
int i, wake_index;
|
|
|
|
|
2018-11-30 08:36:41 +08:00
|
|
|
if (!atomic_read(&sbq->ws_active))
|
|
|
|
return NULL;
|
|
|
|
|
2016-09-17 22:38:44 +08:00
|
|
|
wake_index = atomic_read(&sbq->wake_index);
|
|
|
|
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
|
|
|
|
struct sbq_wait_state *ws = &sbq->ws[wake_index];
|
|
|
|
|
|
|
|
if (waitqueue_active(&ws->wait)) {
|
|
|
|
int o = atomic_read(&sbq->wake_index);
|
|
|
|
|
|
|
|
if (wake_index != o)
|
|
|
|
atomic_cmpxchg(&sbq->wake_index, o, wake_index);
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
wake_index = sbq_index_inc(wake_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
static bool __sbq_wake_up(struct sbitmap_queue *sbq)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
|
|
|
struct sbq_wait_state *ws;
|
2017-01-19 03:55:22 +08:00
|
|
|
unsigned int wake_batch;
|
2016-09-17 22:38:44 +08:00
|
|
|
int wait_cnt;
|
|
|
|
|
|
|
|
ws = sbq_wake_ptr(sbq);
|
|
|
|
if (!ws)
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
return false;
|
2016-09-17 22:38:44 +08:00
|
|
|
|
|
|
|
wait_cnt = atomic_dec_return(&ws->wait_cnt);
|
2017-01-19 03:55:22 +08:00
|
|
|
if (wait_cnt <= 0) {
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
int ret;
|
|
|
|
|
2017-01-19 03:55:22 +08:00
|
|
|
wake_batch = READ_ONCE(sbq->wake_batch);
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
|
2017-01-19 03:55:22 +08:00
|
|
|
/*
|
|
|
|
* Pairs with the memory barrier in sbitmap_queue_resize() to
|
|
|
|
* ensure that we see the batch size update before the wait
|
|
|
|
* count is reset.
|
|
|
|
*/
|
|
|
|
smp_mb__before_atomic();
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
|
2017-01-19 03:55:22 +08:00
|
|
|
/*
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
* For concurrent callers of this, the one that failed the
|
|
|
|
* atomic_cmpxhcg() race should call this function again
|
|
|
|
* to wakeup a new batch on a different 'ws'.
|
2017-01-19 03:55:22 +08:00
|
|
|
*/
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
|
|
|
|
if (ret == wait_cnt) {
|
|
|
|
sbq_index_atomic_inc(&sbq->wake_index);
|
|
|
|
wake_up_nr(&ws->wait, wake_batch);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-09-17 22:38:44 +08:00
|
|
|
}
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:00:39 +08:00
|
|
|
void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
|
sbitmap: fix race in wait batch accounting
If we have multiple callers of sbq_wake_up(), we can end up in a
situation where the wait_cnt will continually go more and more
negative. Consider the case where our wake batch is 1, hence
wait_cnt will start out as 1.
wait_cnt == 1
CPU0 CPU1
atomic_dec_return(), cnt == 0
atomic_dec_return(), cnt == -1
cmpxchg(-1, 0) (succeeds)
[wait_cnt now 0]
cmpxchg(0, 1) (fails)
This ends up with wait_cnt being 0, we'll wakeup immediately
next time. Going through the same loop as above again, and
we'll have wait_cnt -1.
For the case where we have a larger wake batch, the only
difference is that the starting point will be higher. We'll
still end up with continually smaller batch wakeups, which
defeats the purpose of the rolling wakeups.
Always reset the wait_cnt to the batch value. Then it doesn't
matter who wins the race. But ensure that whomever does win
the race is the one that increments the ws index and wakes up
our batch count, loser gets to call __sbq_wake_up() again to
account his wakeups towards the next active wait state index.
Fixes: 6c0ca7ae292a ("sbitmap: fix wakeup hang after sbq resize")
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-05-15 02:17:31 +08:00
|
|
|
{
|
|
|
|
while (__sbq_wake_up(sbq))
|
|
|
|
;
|
2016-09-17 22:38:44 +08:00
|
|
|
}
|
2018-05-25 01:00:39 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
|
2016-09-17 22:38:44 +08:00
|
|
|
|
2016-09-17 16:28:23 +08:00
|
|
|
void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
|
2016-09-17 16:28:24 +08:00
|
|
|
unsigned int cpu)
|
2016-09-17 22:38:44 +08:00
|
|
|
{
|
2018-12-01 04:18:06 +08:00
|
|
|
sbitmap_deferred_clear_bit(&sbq->sb, nr);
|
|
|
|
|
2018-05-25 01:00:39 +08:00
|
|
|
/*
|
|
|
|
* Pairs with the memory barrier in set_current_state() to ensure the
|
|
|
|
* proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
|
|
|
|
* and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
|
|
|
|
* waiter. See the comment on waitqueue_active().
|
|
|
|
*/
|
|
|
|
smp_mb__after_atomic();
|
|
|
|
sbitmap_queue_wake_up(sbq);
|
|
|
|
|
2016-09-18 03:20:54 +08:00
|
|
|
if (likely(!sbq->round_robin && nr < sbq->sb.depth))
|
2016-09-17 16:28:23 +08:00
|
|
|
*per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
|
2016-09-17 22:38:44 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
|
|
|
|
|
|
|
|
void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
|
|
|
|
{
|
|
|
|
int i, wake_index;
|
|
|
|
|
|
|
|
/*
|
2017-01-19 03:55:21 +08:00
|
|
|
* Pairs with the memory barrier in set_current_state() like in
|
2018-05-25 01:00:39 +08:00
|
|
|
* sbitmap_queue_wake_up().
|
2016-09-17 22:38:44 +08:00
|
|
|
*/
|
|
|
|
smp_mb();
|
|
|
|
wake_index = atomic_read(&sbq->wake_index);
|
|
|
|
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
|
|
|
|
struct sbq_wait_state *ws = &sbq->ws[wake_index];
|
|
|
|
|
|
|
|
if (waitqueue_active(&ws->wait))
|
|
|
|
wake_up(&ws->wait);
|
|
|
|
|
|
|
|
wake_index = sbq_index_inc(wake_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
|
2017-01-26 06:32:13 +08:00
|
|
|
|
|
|
|
void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
|
|
|
|
{
|
|
|
|
bool first;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sbitmap_show(&sbq->sb, m);
|
|
|
|
|
|
|
|
seq_puts(m, "alloc_hint={");
|
|
|
|
first = true;
|
|
|
|
for_each_possible_cpu(i) {
|
|
|
|
if (!first)
|
|
|
|
seq_puts(m, ", ");
|
|
|
|
first = false;
|
|
|
|
seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
|
|
|
|
}
|
|
|
|
seq_puts(m, "}\n");
|
|
|
|
|
|
|
|
seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
|
|
|
|
seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
|
2018-11-30 08:36:41 +08:00
|
|
|
seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
|
2017-01-26 06:32:13 +08:00
|
|
|
|
|
|
|
seq_puts(m, "ws={\n");
|
|
|
|
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
|
|
|
|
struct sbq_wait_state *ws = &sbq->ws[i];
|
|
|
|
|
|
|
|
seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
|
|
|
|
atomic_read(&ws->wait_cnt),
|
|
|
|
waitqueue_active(&ws->wait) ? "active" : "inactive");
|
|
|
|
}
|
|
|
|
seq_puts(m, "}\n");
|
|
|
|
|
|
|
|
seq_printf(m, "round_robin=%d\n", sbq->round_robin);
|
2018-05-10 08:16:31 +08:00
|
|
|
seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
|
2017-01-26 06:32:13 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_queue_show);
|
2018-11-30 08:36:41 +08:00
|
|
|
|
2018-12-20 23:49:00 +08:00
|
|
|
void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
|
|
|
|
struct sbq_wait_state *ws,
|
|
|
|
struct sbq_wait *sbq_wait)
|
|
|
|
{
|
|
|
|
if (!sbq_wait->sbq) {
|
|
|
|
sbq_wait->sbq = sbq;
|
|
|
|
atomic_inc(&sbq->ws_active);
|
|
|
|
}
|
|
|
|
add_wait_queue(&ws->wait, &sbq_wait->wait);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
|
|
|
|
|
|
|
|
void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
|
|
|
|
{
|
|
|
|
list_del_init(&sbq_wait->wait.entry);
|
|
|
|
if (sbq_wait->sbq) {
|
|
|
|
atomic_dec(&sbq_wait->sbq->ws_active);
|
|
|
|
sbq_wait->sbq = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
|
|
|
|
|
2018-11-30 08:36:41 +08:00
|
|
|
void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
|
|
|
|
struct sbq_wait_state *ws,
|
|
|
|
struct sbq_wait *sbq_wait, int state)
|
|
|
|
{
|
2018-12-20 23:49:00 +08:00
|
|
|
if (!sbq_wait->sbq) {
|
2018-11-30 08:36:41 +08:00
|
|
|
atomic_inc(&sbq->ws_active);
|
2018-12-20 23:49:00 +08:00
|
|
|
sbq_wait->sbq = sbq;
|
2018-11-30 08:36:41 +08:00
|
|
|
}
|
|
|
|
prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
|
|
|
|
|
|
|
|
void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
|
|
|
|
struct sbq_wait *sbq_wait)
|
|
|
|
{
|
|
|
|
finish_wait(&ws->wait, &sbq_wait->wait);
|
2018-12-20 23:49:00 +08:00
|
|
|
if (sbq_wait->sbq) {
|
2018-11-30 08:36:41 +08:00
|
|
|
atomic_dec(&sbq->ws_active);
|
2018-12-20 23:49:00 +08:00
|
|
|
sbq_wait->sbq = NULL;
|
2018-11-30 08:36:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sbitmap_finish_wait);
|