Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp2948811ybx; Fri, 8 Nov 2019 11:43:25 -0800 (PST) X-Google-Smtp-Source: APXvYqwSCleLpk2jQvGH9GT2ns3srHJ3BxITMW/mWq2gM/h1SZ84wepNcInLEow2kOddbUXQkOS5 X-Received: by 2002:a17:906:f0c9:: with SMTP id dk9mr10592707ejb.56.1573242205069; Fri, 08 Nov 2019 11:43:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573242205; cv=none; d=google.com; s=arc-20160816; b=Xf27Ber5if7j5R1r0WqtgQAMzKZd1MZPLA1SEhXPa9eOJfnlKPd/omK5q0kqN5ODV8 KnQ9ArYTPOXhP+PWxOSAMmSlDYziKfFfooHYQJazIkgTcT5Mo9RzO/P+9W9+DGrtKcIL MIs9oFHo6DkAgWT76dU3UoBsfl213XEnxGuMZsQSkyqDJQKeZLHl+Hav9za0cS5nkpBD dgzuVWR6+EL7OiOCZ0L9vFaxNBxlCkvKRjfG8/+71SpXcK/0T24IUWZPiA/rgi9Uyla0 FgXzdkYlGaxtTZcYQOUywlKRrjO/4G9GMvMygG+P390lLTjcaImZXTNu7P+eQi0Urhi7 cnPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=VRnTto8KPjkvow/svAK/HtQEwlurCDazmk8BqxCWuF0=; b=FrfK6AERsublak5yAqYlsuZwCCMw17XITPsB6T/83O0IYPKrlpPJQsIojxy0jYjV27 8yxv4SbgZva1clbvexPWS0nIvhngpTWCtHaVnrMQA8TTu9sllv+deTMgnKP2wM8bX3Q8 JYdMV/5RRGMp0YRbG+8ncQu+4bmhC/7RQjdDavlhrwJUXyJXiaTr6gsWdMEy12Rpw/ff Ptuv2oJCoRwvXk3FOBuE1DX9Jcl6glz07LS0lfE/DyXMMmLiwp8kN4blhrdBz3l2dJmN HSf0cafS9LM7KiHN4MgsP38IM5iORbrKjc/wLQXlpIyvQEjtO7mN5MTCDjaOntxmCC/7 4IHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EHl7l4Lq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o10si6055120edj.316.2019.11.08.11.43.01; Fri, 08 Nov 2019 11:43:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EHl7l4Lq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389080AbfKHTkJ (ORCPT + 99 others); Fri, 8 Nov 2019 14:40:09 -0500 Received: from mail-qk1-f202.google.com ([209.85.222.202]:41624 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387400AbfKHTkJ (ORCPT ); Fri, 8 Nov 2019 14:40:09 -0500 Received: by mail-qk1-f202.google.com with SMTP id c77so7877791qkb.8 for ; Fri, 08 Nov 2019 11:40:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=VRnTto8KPjkvow/svAK/HtQEwlurCDazmk8BqxCWuF0=; b=EHl7l4Lq9VH5X9AXjHCKaa610RoACsxtY8fTjtJX9iB1ikgAE0nREMQkQiiw14GCYt 2HGKyGBYX2GtjA6lp3kbDtgb/DJ+UjZYIbXfePN4uXvOe5yRY9XgX+8sbNBeGk4Nk2QQ TsjeANtIdrQ07dS4cdVmUy+a6OrPgyPCuNvQsKDKFE9UMWjAqvbAf5LFq1p2LFh5Xedd t5r02cabu+ELUsWFmmqVBAJ9lcyXf5wVssRbYlwwxTOWrmOEvfi32l+us16YucMiSFbZ hniikBfjH+KFVn/5YuzWIjGE2UMolwoyMXbsRXbzUrEr6Ux724ZsX6kr0PADPOMPPzbV miJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VRnTto8KPjkvow/svAK/HtQEwlurCDazmk8BqxCWuF0=; b=cBZfQMAIcVwsBLiaPWWA0Rn59QFpFCZaKUO3Sf1Gaczmw/G+Bc6CwUmBkGxWcLE6c4 kAgRjIFa9OIkSSQPRJ5OpUB9DC+KctIzQ588nN9hHJDMgt5y0krGYC0EbY3VGdbXO6R/ j0hTPMsF2KfroC6HDH+9QJWj6zExXdg2e6XRSCmtbeQGNxKWMvrBKKUTqhHAFSmxQabz 0IfEQIkBlHwbR5E7FffiYCjnAUzKGcYUMvqNsCWjaHnmK+JKKM9zSf8Vw965f7WXuA4/ NiczokynijwI4z9Vp3DDxcP+Nq3Z3bbftb9RGPMQL733mkp9jGWZ0fnSqn6sGJDB8v6O WfPA== X-Gm-Message-State: APjAAAUS0xtLuzOHWU3HatTPbnvOGRmN7vcc+VKiMgs075z0IT/Sa5W6 tl09zdBJn72FrYX4BBTr3JX197Paomg= X-Received: by 2002:ac8:384f:: with SMTP id r15mr12476756qtb.155.1573242008100; Fri, 08 Nov 2019 11:40:08 -0800 (PST) Date: Fri, 8 Nov 2019 12:39:58 -0700 In-Reply-To: <20191108193958.205102-1-yuzhao@google.com> Message-Id: <20191108193958.205102-2-yuzhao@google.com> Mime-Version: 1.0 References: <20190914000743.182739-1-yuzhao@google.com> <20191108193958.205102-1-yuzhao@google.com> X-Mailer: git-send-email 2.24.0.rc1.363.gb1bccd3e3d-goog Subject: [PATCH v4 2/2] mm: avoid slub allocation while holding list_lock From: Yu Zhao To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , "Kirill A . Shutemov" , Tetsuo Handa Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , "Kirill A . Shutemov" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If we are already under list_lock, don't call kmalloc(). Otherwise we will run into deadlock because kmalloc() also tries to grab the same lock. Fixing the problem by using a static bitmap instead. WARNING: possible recursive locking detected -------------------------------------------- mount-encrypted/4921 is trying to acquire lock: (&(&n->list_lock)->rlock){-.-.}, at: ___slab_alloc+0x104/0x437 but task is already holding lock: (&(&n->list_lock)->rlock){-.-.}, at: __kmem_cache_shutdown+0x81/0x3cb other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&(&n->list_lock)->rlock); lock(&(&n->list_lock)->rlock); *** DEADLOCK *** Acked-by: Kirill A. Shutemov Signed-off-by: Yu Zhao --- mm/slub.c | 88 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 6930c3febad7..7a4ec3c4b4d9 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -441,19 +441,38 @@ static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page, } #ifdef CONFIG_SLUB_DEBUG +static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)]; +static DEFINE_SPINLOCK(object_map_lock); + /* * Determine a map of object in use on a page. * * Node listlock must be held to guarantee that the page does * not vanish from under us. */ -static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map) +static unsigned long *get_map(struct kmem_cache *s, struct page *page) { void *p; void *addr = page_address(page); + VM_BUG_ON(!irqs_disabled()); + + spin_lock(&object_map_lock); + + bitmap_zero(object_map, page->objects); + for (p = page->freelist; p; p = get_freepointer(s, p)) - set_bit(slab_index(p, s, addr), map); + set_bit(slab_index(p, s, addr), object_map); + + return object_map; +} + +static void put_map(unsigned long *map) +{ + VM_BUG_ON(map != object_map); + lockdep_assert_held(&object_map_lock); + + spin_unlock(&object_map_lock); } static inline unsigned int size_from_object(struct kmem_cache *s) @@ -3695,13 +3714,12 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page, #ifdef CONFIG_SLUB_DEBUG void *addr = page_address(page); void *p; - unsigned long *map = bitmap_zalloc(page->objects, GFP_ATOMIC); - if (!map) - return; + unsigned long *map; + slab_err(s, page, text, s->name); slab_lock(page); - get_map(s, page, map); + map = get_map(s, page); for_each_object(p, s, addr, page->objects) { if (!test_bit(slab_index(p, s, addr), map)) { @@ -3709,8 +3727,9 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page, print_tracking(s, p); } } + put_map(map); + slab_unlock(page); - bitmap_free(map); #endif } @@ -4404,19 +4423,19 @@ static int count_total(struct page *page) #endif #ifdef CONFIG_SLUB_DEBUG -static void validate_slab(struct kmem_cache *s, struct page *page, - unsigned long *map) +static void validate_slab(struct kmem_cache *s, struct page *page) { void *p; void *addr = page_address(page); + unsigned long *map; + + slab_lock(page); if (!check_slab(s, page) || !on_freelist(s, page, NULL)) - return; + goto unlock; /* Now we know that a valid freelist exists */ - bitmap_zero(map, page->objects); - - get_map(s, page, map); + map = get_map(s, page); for_each_object(p, s, addr, page->objects) { u8 val = test_bit(slab_index(p, s, addr), map) ? SLUB_RED_INACTIVE : SLUB_RED_ACTIVE; @@ -4424,18 +4443,13 @@ static void validate_slab(struct kmem_cache *s, struct page *page, if (!check_object(s, page, p, val)) break; } -} - -static void validate_slab_slab(struct kmem_cache *s, struct page *page, - unsigned long *map) -{ - slab_lock(page); - validate_slab(s, page, map); + put_map(map); +unlock: slab_unlock(page); } static int validate_slab_node(struct kmem_cache *s, - struct kmem_cache_node *n, unsigned long *map) + struct kmem_cache_node *n) { unsigned long count = 0; struct page *page; @@ -4444,7 +4458,7 @@ static int validate_slab_node(struct kmem_cache *s, spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) { - validate_slab_slab(s, page, map); + validate_slab(s, page); count++; } if (count != n->nr_partial) @@ -4455,7 +4469,7 @@ static int validate_slab_node(struct kmem_cache *s, goto out; list_for_each_entry(page, &n->full, slab_list) { - validate_slab_slab(s, page, map); + validate_slab(s, page); count++; } if (count != atomic_long_read(&n->nr_slabs)) @@ -4472,15 +4486,11 @@ static long validate_slab_cache(struct kmem_cache *s) int node; unsigned long count = 0; struct kmem_cache_node *n; - unsigned long *map = bitmap_alloc(oo_objects(s->max), GFP_KERNEL); - - if (!map) - return -ENOMEM; flush_all(s); for_each_kmem_cache_node(s, node, n) - count += validate_slab_node(s, n, map); - bitmap_free(map); + count += validate_slab_node(s, n); + return count; } /* @@ -4610,18 +4620,17 @@ static int add_location(struct loc_track *t, struct kmem_cache *s, } static void process_slab(struct loc_track *t, struct kmem_cache *s, - struct page *page, enum track_item alloc, - unsigned long *map) + struct page *page, enum track_item alloc) { void *addr = page_address(page); void *p; + unsigned long *map; - bitmap_zero(map, page->objects); - get_map(s, page, map); - + map = get_map(s, page); for_each_object(p, s, addr, page->objects) if (!test_bit(slab_index(p, s, addr), map)) add_location(t, s, get_track(s, p, alloc)); + put_map(map); } static int list_locations(struct kmem_cache *s, char *buf, @@ -4632,11 +4641,9 @@ static int list_locations(struct kmem_cache *s, char *buf, struct loc_track t = { 0, 0, NULL }; int node; struct kmem_cache_node *n; - unsigned long *map = bitmap_alloc(oo_objects(s->max), GFP_KERNEL); - if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location), - GFP_KERNEL)) { - bitmap_free(map); + if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location), + GFP_KERNEL)) { return sprintf(buf, "Out of memory\n"); } /* Push back cpu slabs */ @@ -4651,9 +4658,9 @@ static int list_locations(struct kmem_cache *s, char *buf, spin_lock_irqsave(&n->list_lock, flags); list_for_each_entry(page, &n->partial, slab_list) - process_slab(&t, s, page, alloc, map); + process_slab(&t, s, page, alloc); list_for_each_entry(page, &n->full, slab_list) - process_slab(&t, s, page, alloc, map); + process_slab(&t, s, page, alloc); spin_unlock_irqrestore(&n->list_lock, flags); } @@ -4702,7 +4709,6 @@ static int list_locations(struct kmem_cache *s, char *buf, } free_loc_track(&t); - bitmap_free(map); if (!t.count) len += sprintf(buf, "No data\n"); return len; -- 2.24.0.rc1.363.gb1bccd3e3d-goog