Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp4422818img; Tue, 26 Mar 2019 09:06:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZvzbB44ieELC3TAhPWuAYCyBhJ5yHjlLELIj+1S95kux9uIpOOwds7/f4aTi6edyTqqQp X-Received: by 2002:a65:414a:: with SMTP id x10mr15482069pgp.237.1553616414160; Tue, 26 Mar 2019 09:06:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553616414; cv=none; d=google.com; s=arc-20160816; b=zgAzrDCWllUEYDK9dt8Gzgz37+IswdTFprYBXs2RjDOTsSzeoE9iM5nUUm/sP69Bqu DBLwcrj9TOT6mbVgyH+/bBRqSvn8klB2eNeeBJEKCBMhK0/oIC4YfbmXpCwArb2+DOy5 W1jTrN/0OyDe4strwGLqf4HRqM+FNDePRAzT/1BAHNT788SmulBddjpwZa6dGzMkQRbZ s3XqZviU8vdOHwo4im6R3qDOGl1VfZY9kB+ws2OUpBz3I1UnMmBRZGcm3pwEIhcUYHNG yQQZwdJrSvACma+l9wfrCK0/lZ29RgmvHKK/sHddoxplXChstAXE7oSQX1fS94uoTiEW SMlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Yz2RvGFb8xZomssZ7mtRK2dOFU1jnfJaOyAOB+cKBIQ=; b=q8I3AwGfHTENyEoLp44fV7WbrRYWHa2McUCHMe35VETJ8IRuJnW10J0xq/r7jXcmFR NMTxEQ8l7DqvXF4E1T+9t/m0nHSsZWYb4x65iLofONg0REtvt9YJZtGYzSRN1L0birF+ XoZ4SUlh1Lm/rRKIDcdXLCFTFSej0rRCATT06ni1lkscmyEs2P2e6NYdn1dAwHqPBiz0 FWnbnvmt4V0evpladKJBOYNUtdiMDQNJxuIqElp2n9k9wudCCSnemidSHvjMndWiE7cH J0XTCdfiJxj6+lZipX+nhE3m9s7WXp0cy8/e0K+dG5ZkccXhXRC19ZsxuoMS7VyJDDx+ 7KDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=VkC7m7yz; 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 g126si15126622pgc.75.2019.03.26.09.06.37; Tue, 26 Mar 2019 09:06:54 -0700 (PDT) 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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=VkC7m7yz; 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 S1731531AbfCZQFo (ORCPT + 99 others); Tue, 26 Mar 2019 12:05:44 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:43490 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726258AbfCZQFo (ORCPT ); Tue, 26 Mar 2019 12:05:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Yz2RvGFb8xZomssZ7mtRK2dOFU1jnfJaOyAOB+cKBIQ=; b=VkC7m7yzRINQMwiNCcM2hrk5f +Ct1T7URpjIcWrbr0ScsN9kuCOsNZsyRTRwVJnZ0QY/ic6Ti3DY3001lLmKJbVaQ9VHyPCbcZlZjL ydVroQFgbIoPel1WyeZe4zc4W/A+zhSPdufqv6Iqh71FPVqEibq+mmDk9aeGpY7LWT4/IXdXHxEF0 Ki7LZAq/eLVWcWuIrfPd2VYpRPTbE21WP+fudi4qLn3cJJ1fDpoEjImy7GIwSn4N5tGVz9LeXmdwf j4dP/8QAmKnXBG9qfRUXJVWa9INHMetFst4pP9jlJRY35GdXDHwNvdQyVLUU/S1YiiDTATVoyKPSw QsagImLOQ==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1h8oa0-00042L-Ms; Tue, 26 Mar 2019 16:05:36 +0000 Date: Tue, 26 Mar 2019 09:05:36 -0700 From: Matthew Wilcox To: Qian Cai Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, mhocko@kernel.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] kmemleaak: survive in a low-memory situation Message-ID: <20190326160536.GO10344@bombadil.infradead.org> References: <20190326154338.20594-1-cai@lca.pw> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190326154338.20594-1-cai@lca.pw> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 26, 2019 at 11:43:38AM -0400, Qian Cai wrote: > Unless there is a brave soul to reimplement the kmemleak to embed it's > metadata into the tracked memory itself in a foreseeable future, this > provides a good balance between enabling kmemleak in a low-memory > situation and not introducing too much hackiness into the existing > code for now. I don't understand kmemleak. Kirill pointed me at this a few days ago: https://gist.github.com/kiryl/3225e235fea390aa2e49bf625bbe83ec It's caused by the XArray allocating memory using GFP_NOWAIT | __GFP_NOWARN. kmemleak then decides it needs to allocate memory to track this memory. So it calls kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp)); #define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \ __GFP_NORETRY | __GFP_NOMEMALLOC | \ __GFP_NOWARN | __GFP_NOFAIL) then the page allocator gets to see GFP_NOFAIL | GFP_NOWAIT and gets angry. But I don't understand why kmemleak needs to mess with the GFP flags at all. Just allocate using the same flags as the caller, and fail the original allocation if the kmemleak allocation fails. Like this: +++ b/mm/slab.h @@ -435,12 +435,22 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, for (i = 0; i < size; i++) { p[i] = kasan_slab_alloc(s, p[i], flags); /* As p[i] might get tagged, call kmemleak hook after KASAN. */ - kmemleak_alloc_recursive(p[i], s->object_size, 1, - s->flags, flags); + if (kmemleak_alloc_recursive(p[i], s->object_size, 1, + s->flags, flags)) + goto fail; } if (memcg_kmem_enabled()) memcg_kmem_put_cache(s); + return; + +fail: + while (i > 0) { + kasan_blah(...); + kmemleak_blah(); + i--; + } + free_blah(p); + *p = NULL; } #ifndef CONFIG_SLOB and if we had something like this, we wouldn't need kmemleak to have this self-disabling or must-succeed property.