History

mm: fix unexpected changes to {failslab|fail_page_alloc}.attr

When we specify __GFP_NOWARN, we only expect that no warnings will be issued for current caller. But in the __should_failslab() and __should_fail_alloc_page(), the local GFP flags alter the global {failslab|fail_page_alloc}.attr, which is persistent and shared by all tasks. This is not what we expected, let's fix it. [akpm@linux-foundation.org: unexport should_fail_ex()] Link: https://lkml.kernel.org/r/20221118100011.2634-1-zhengqi.arch@bytedance.com Fixes: 3f913fc5f974 ("mm: fix missing handler for __GFP_NOWARN") Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> Reported-by: Dmitry Vyukov <dvyukov@google.com> Reviewed-by: Akinobu Mita <akinobu.mita@gmail.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Cc: Akinobu Mita <akinobu.mita@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Qi Zheng committed 3 years ago

Commit ea4452d1 parent de1ccfb4 files · +28 −11Browse files

Unified · Split

4 files
  1. include/linux/fault-inject.h +5 −2
  2. lib/fault-inject.c +8 −5
  3. mm/failslab.c +10 −2
  4. mm/page_alloc.c +5 −2
include/linux/fault-inject.h+5−2
@@ -20,7 +20,6 @@ struct fault_attr { · 19 unmodified lines
20 atomic_t space;20 atomic_t space;
21 unsigned long verbose;21 unsigned long verbose;
22 bool task_filter;22 bool task_filter;
23 bool no_warn;
24 unsigned long stacktrace_depth;23 unsigned long stacktrace_depth;
25 unsigned long require_start;24 unsigned long require_start;
26 unsigned long require_end;25 unsigned long require_end;
@@ -32,6 +31,10 @@ struct fault_attr { · 5 unmodified lines
32 struct dentry *dname;31 struct dentry *dname;
33};32};
34 33
34enum fault_flags {
35 FAULT_NOWARN = 1 << 0,
36};
37
35#define FAULT_ATTR_INITIALIZER { \38#define FAULT_ATTR_INITIALIZER { \
36 .interval = 1, \39 .interval = 1, \
37 .times = ATOMIC_INIT(1), \40 .times = ATOMIC_INIT(1), \
@@ -40,11 +43,11 @@ struct fault_attr { · 2 unmodified lines
40 .ratelimit_state = RATELIMIT_STATE_INIT_DISABLED, \43 .ratelimit_state = RATELIMIT_STATE_INIT_DISABLED, \
41 .verbose = 2, \44 .verbose = 2, \
42 .dname = NULL, \45 .dname = NULL, \
43 .no_warn = false, \
44 }46 }
45 47
46#define DECLARE_FAULT_ATTR(name) struct fault_attr name = FAULT_ATTR_INITIALIZER48#define DECLARE_FAULT_ATTR(name) struct fault_attr name = FAULT_ATTR_INITIALIZER
47int setup_fault_attr(struct fault_attr *attr, char *str);49int setup_fault_attr(struct fault_attr *attr, char *str);
50bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags);
48bool should_fail(struct fault_attr *attr, ssize_t size);51bool should_fail(struct fault_attr *attr, ssize_t size);
49 52
50#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS53#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
lib/fault-inject.c+8−5
@@ -41,9 +41,6 @@ EXPORT_SYMBOL_GPL(setup_fault_attr); · 40 unmodified lines
41 41
42static void fail_dump(struct fault_attr *attr)42static void fail_dump(struct fault_attr *attr)
43{43{
44 if (attr->no_warn)
45 return;
46
47 if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {44 if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {
48 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"45 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
49 "name %pd, interval %lu, probability %lu, "46 "name %pd, interval %lu, probability %lu, "
@@ -103,7 +100,7 @@ static inline bool fail_stacktrace(struct fault_attr *attr) · 53 unmodified lines
103 * http://www.nongnu.org/failmalloc/100 * http://www.nongnu.org/failmalloc/
104 */101 */
105 102
106bool should_fail(struct fault_attr *attr, ssize_t size)103bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags)
107{104{
108 if (in_task()) {105 if (in_task()) {
109 unsigned int fail_nth = READ_ONCE(current->fail_nth);106 unsigned int fail_nth = READ_ONCE(current->fail_nth);
@@ -146,13 +143,19 @@ bool should_fail(struct fault_attr *attr, ssize_t size) · 36 unmodified lines
146 return false;143 return false;
147 144
148fail:145fail:
149 fail_dump(attr);146 if (!(flags & FAULT_NOWARN))
147 fail_dump(attr);
150 148
151 if (atomic_read(&attr->times) != -1)149 if (atomic_read(&attr->times) != -1)
152 atomic_dec_not_zero(&attr->times);150 atomic_dec_not_zero(&attr->times);
153 151
154 return true;152 return true;
155}153}
154
155bool should_fail(struct fault_attr *attr, ssize_t size)
156{
157 return should_fail_ex(attr, size, 0);
158}
156EXPORT_SYMBOL_GPL(should_fail);159EXPORT_SYMBOL_GPL(should_fail);
157 160
158#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS161#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
mm/failslab.c+10−2
@@ -16,6 +16,8 @@ static struct { · 15 unmodified lines
16 16
17bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)17bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
18{18{
19 int flags = 0;
20
19 /* No fault-injection for bootstrap cache */21 /* No fault-injection for bootstrap cache */
20 if (unlikely(s == kmem_cache))22 if (unlikely(s == kmem_cache))
21 return false;23 return false;
@@ -30,10 +32,16 @@ bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags) · 8 unmodified lines
30 if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))32 if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
31 return false;33 return false;
32 34
35 /*
36 * In some cases, it expects to specify __GFP_NOWARN
37 * to avoid printing any information(not just a warning),
38 * thus avoiding deadlocks. See commit 6b9dbedbe349 for
39 * details.
40 */
33 if (gfpflags & __GFP_NOWARN)41 if (gfpflags & __GFP_NOWARN)
34 failslab.attr.no_warn = true;42 flags |= FAULT_NOWARN;
35 43
36 return should_fail(&failslab.attr, s->object_size);44 return should_fail_ex(&failslab.attr, s->object_size, flags);
37}45}
38 46
39static int __init setup_failslab(char *str)47static int __init setup_failslab(char *str)
mm/page_alloc.c+5−2
@@ -3887,6 +3887,8 @@ __setup("fail_page_alloc=", setup_fail_page_alloc); · 3886 unmodified lines
3887 3887
3888static bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)3888static bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
3889{3889{
3890 int flags = 0;
3891
3890 if (order < fail_page_alloc.min_order)3892 if (order < fail_page_alloc.min_order)
3891 return false;3893 return false;
3892 if (gfp_mask & __GFP_NOFAIL)3894 if (gfp_mask & __GFP_NOFAIL)
@@ -3897,10 +3899,11 @@ static bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) · 4 unmodified lines
3897 (gfp_mask & __GFP_DIRECT_RECLAIM))3899 (gfp_mask & __GFP_DIRECT_RECLAIM))
3898 return false;3900 return false;
3899 3901
3902 /* See comment in __should_failslab() */
3900 if (gfp_mask & __GFP_NOWARN)3903 if (gfp_mask & __GFP_NOWARN)
3901 fail_page_alloc.attr.no_warn = true;3904 flags |= FAULT_NOWARN;
3902 3905
3903 return should_fail(&fail_page_alloc.attr, 1 << order);3906 return should_fail_ex(&fail_page_alloc.attr, 1 << order, flags);
3904}3907}
3905 3908
3906#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS3909#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS