Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4299593ybx; Sat, 9 Nov 2019 12:55:08 -0800 (PST) X-Google-Smtp-Source: APXvYqzBMTEIYp0wozjpMsFbp6FRlU6wvsZ+8JUj1MPCM05EeTzTxosdqJ08/s0Prs09xjqWcn4Q X-Received: by 2002:a05:6402:128c:: with SMTP id w12mr17926106edv.178.1573332908181; Sat, 09 Nov 2019 12:55:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573332908; cv=none; d=google.com; s=arc-20160816; b=EQqz/BU1jgRtJJ20Qr0e4zpjfMfoDVXC2olL3Wac+gGRz3pe10tBWVax5DgBIaGL7Y C35Tz6ve9bVv+mf/T0bpCItKVxMi95HRJrbANS8ZBewOUkCt3Jyi9aus1HQNrsEljYK8 /7oEd5OHtM2Vuf8sqmHydRlr+oat+QT/b/cN3CpAz/08xWqqNUZZpKvx4LL5FQ89lTWw O91Ci+CiZ0vW1F0PuE8H+YGYotDqKPEBHZCVwG/sFXQvuArWjzgJUxyW49f1gfdEMIR6 PWS689z9VRo6spdxSb3LYPS1YlKXmCo2nKg9gmAUjVfwuWhxIXJ4QcL77hX4wAyfc/qx rgfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=RX1N01XenRFeYg1AASAbKeIWIhlclqO6HROPEqHdyxw=; b=JKcXzpTdWFzX/RkdWXrovKP/PS+WDgDLgei113+8KxmTih5SVbSDicFunAu+tpRo8m mF/pgMFLXBHnWfhK/bBpR1bqSVmC4sLjQyeog6GLwLVqa/JgmsfcWGUjxRpeYGSstoRv VrUQP/zEalCEq2NsQlnvxBvex0bPpIMM53Er7NHO9uwPJ8nwRSaO9HW5F1atSgzwb96k EJUWCqkGGF6Laf8/TkmcXMegULGDgDhaoPqZk53Zec8qFCGtRxBMfPeGDkPeNR1qgY3/ dwhDky8od3bOFPdM93ieqftjDD0fLn+IH6YZXP7wAe21m4Iw3c4s2JipFPN9jgZ0TaCU MyYA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l19si6313978ejq.261.2019.11.09.12.54.16; Sat, 09 Nov 2019 12:55:08 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726537AbfKIUwa (ORCPT + 99 others); Sat, 9 Nov 2019 15:52:30 -0500 Received: from gentwo.org ([3.19.106.255]:39100 "EHLO gentwo.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726383AbfKIUwa (ORCPT ); Sat, 9 Nov 2019 15:52:30 -0500 Received: by gentwo.org (Postfix, from userid 1002) id 9CEE23EC14; Sat, 9 Nov 2019 20:52:29 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by gentwo.org (Postfix) with ESMTP id 9A0DA3E886; Sat, 9 Nov 2019 20:52:29 +0000 (UTC) Date: Sat, 9 Nov 2019 20:52:29 +0000 (UTC) From: Christopher Lameter X-X-Sender: cl@www.lameter.com To: Yu Zhao cc: Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , "Kirill A . Shutemov" , Tetsuo Handa , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: Re: [PATCH v4 2/2] mm: avoid slub allocation while holding list_lock In-Reply-To: <20191108193958.205102-2-yuzhao@google.com> Message-ID: References: <20190914000743.182739-1-yuzhao@google.com> <20191108193958.205102-1-yuzhao@google.com> <20191108193958.205102-2-yuzhao@google.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 8 Nov 2019, Yu Zhao wrote: > 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. How did this happen? The kmalloc needs to be always done before the list_lock is taken. > 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 *** Ahh. list_slab_objects() in shutdown? There is a much easier fix for this: [FIX] slub: Remove kmalloc under list_lock from list_slab_objects() list_slab_objects() is called when a slab is destroyed and there are objects still left to list the objects in the syslog. This is a pretty rare event. And there it seems we take the list_lock and call kmalloc while holding that lock. Perform the allocation in free_partial() before the list_lock is taken. Fixes: bbd7d57bfe852d9788bae5fb171c7edb4021d8ac ("slub: Potential stack overflow") Signed-off-by: Christoph Lameter Index: linux/mm/slub.c =================================================================== --- linux.orig/mm/slub.c 2019-10-15 13:54:57.032655296 +0000 +++ linux/mm/slub.c 2019-11-09 20:43:52.374187381 +0000 @@ -3690,14 +3690,11 @@ error: } static void list_slab_objects(struct kmem_cache *s, struct page *page, - const char *text) + const char *text, unsigned long *map) { #ifdef CONFIG_SLUB_DEBUG void *addr = page_address(page); void *p; - unsigned long *map = bitmap_zalloc(page->objects, GFP_ATOMIC); - if (!map) - return; slab_err(s, page, text, s->name); slab_lock(page); @@ -3723,6 +3720,10 @@ static void free_partial(struct kmem_cac { LIST_HEAD(discard); struct page *page, *h; + unsigned long *map = bitmap_alloc(oo_objects(s->max), GFP_KERNEL); + + if (!map) + return; BUG_ON(irqs_disabled()); spin_lock_irq(&n->list_lock); @@ -3732,7 +3733,8 @@ static void free_partial(struct kmem_cac list_add(&page->slab_list, &discard); } else { list_slab_objects(s, page, - "Objects remaining in %s on __kmem_cache_shutdown()"); + "Objects remaining in %s on __kmem_cache_shutdown()", + map); } } spin_unlock_irq(&n->list_lock);