Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp4815245img; Tue, 26 Mar 2019 18:02:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqzOO8qDSmVbS4r0+Zm4n2zWSwC1PDsLy7sVl8QKAYydALS4wzmpL4l0fsOTS1xO8gELRWyk X-Received: by 2002:a63:7141:: with SMTP id b1mr4929055pgn.331.1553648556298; Tue, 26 Mar 2019 18:02:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553648556; cv=none; d=google.com; s=arc-20160816; b=J813Bo2yy+qQvzTTwZ0k8kRBqFsX1ktFOQaJd6NIWWgsGJGYgCgp6+nvibUaGyvUpS LjGVAqY7q/cq9oR/actANRczLNXzeiaxzLz5ARE8Qitwxpt+iVD6fpxYBkUDjTTPoyTx VEVZ/fIFvzL72/bEabPjNtORutoP52hyalJE4eU0YZTOa6Ai0CYqpPoCfq7+qCdV4PRD QSlhAA/vH+flzmW2jyb9ZOIgUm/I94vELDXy9CxQQp9vHv97Bhbm9XC3HzrWb4hY75LV 71IQRfBKOneDgahCvtpQh7SeNR1K2KdSceP3GiC6si47ZAnabqN5HoJ+si9T0p3BL0iH UdXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=pZxffqpv2AkoRy7l1MYn8mOEK0pZUkSkYNUnR2f18Ts=; b=KNavL2Ly6kDNUYGeHFYES8cGnTrzWuvS8ygtPtlUP5qm2NdVc7QkA9JXuKR3x1VgcZ dCaBVVTYvo/6tWxd0Gz+ayuY0TGtHeQQqE1i867u2atowmNeTgKjpYu/g1C7/08CdGNX rTB6rIhpvqetLX7swB3DDo+7XEmycLpJPcjw0Eb0GgtNhPxYOn7kCq6QLX27tbREYQWS lbJAHrveWX/ydSFUjE92aZhnKbx9Q7R0RKawHCJz/JjdcOOitBlrGvIPU6N0KO1JdNeZ rth5GzhmeHoqjAFWIrO8V+SbFlQgmMM1hl46iMn9orqN4QB2eBQCjmSmnFTcno1NVyqK dV8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=U7dj3Cr5; 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 p10si8998296plr.137.2019.03.26.18.02.16; Tue, 26 Mar 2019 18:02:36 -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=pass header.i=@lca.pw header.s=google header.b=U7dj3Cr5; 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 S1732292AbfC0BAQ (ORCPT + 99 others); Tue, 26 Mar 2019 21:00:16 -0400 Received: from mail-qt1-f194.google.com ([209.85.160.194]:33283 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732225AbfC0BAP (ORCPT ); Tue, 26 Mar 2019 21:00:15 -0400 Received: by mail-qt1-f194.google.com with SMTP id k14so17013806qtb.0 for ; Tue, 26 Mar 2019 18:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=from:to:cc:subject:date:message-id; bh=pZxffqpv2AkoRy7l1MYn8mOEK0pZUkSkYNUnR2f18Ts=; b=U7dj3Cr53elo9aF+87clxge+HccjN2L7G7EmN+YbZQJEzwn1rZLsOecFkFfmh32iIS 3OolISTsTDjaeGqpIGfYIeTQhUzj2zax/UQ5TfLl8LfYCASUnTnUjxH9AOcHm9t76B5j vfp1SzSqKeAL5zv4eQkDoS1PMTo+KArXgtJFxzQ0OYbQVDaKt364XEJRT44ACDiUMSbS 1kYHA4gEp6RyrERzBVsyG3cBsm4LYQefYZsQldtyjDg0ose1Nm9IlAXLRhGWL1UF8kOk ICcHl8yyGZE+wdygbNGyNee3OEJaE+4g8djjqMl5LXKK4uMp6cA/dNqmwbEgjYvoTsIa DyAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=pZxffqpv2AkoRy7l1MYn8mOEK0pZUkSkYNUnR2f18Ts=; b=qJoBGf/xpF8k2n0aS/cFKv0Of/Mqo1OajI1++TcmLRMoGvtdwnGqgMqpgRq3UO4dIf 41cA6krtEXI3ORzHrJE3KkGSjgOgeJ9zWBtbHv0MxG9DZgdu0uUufB9z0spG30KLhe9E TT2nUqno7gKZwcSmGI86EtXkcJ1sPUMRf1tZzCZv6v+jVJiJWwu2wBVrnSnVpCDfX/Wh /7OAjhRUZk1Xtz4DlQ5QaT57ajjRGeL80uFjFAnNCXJmiTG1owazGaY3SJw0Fy8NS5TH qpGAGIRbBrEzedh3yicIkg2vz6UBc+xuJVbpimddytZvOn7BO4QNn8rc0rbJX7yrq/Kk v+lg== X-Gm-Message-State: APjAAAX2ZszymfyR1RGn4rxJtFOO7/fsMspQOT8hGnJW720WuUNva7nM x76FXP4qrSiSgUZ2oSZBR/p4/Q== X-Received: by 2002:ac8:29e8:: with SMTP id 37mr29212714qtt.153.1553648414651; Tue, 26 Mar 2019 18:00:14 -0700 (PDT) Received: from ovpn-120-94.rdu2.redhat.com (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id b3sm10821266qti.33.2019.03.26.18.00.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Mar 2019 18:00:14 -0700 (PDT) From: Qian Cai To: akpm@linux-foundation.org Cc: catalin.marinas@arm.com, cl@linux.com, mhocko@kernel.org, willy@infradead.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH v4] kmemleak: survive in a low-memory situation Date: Tue, 26 Mar 2019 20:59:48 -0400 Message-Id: <20190327005948.24263-1-cai@lca.pw> X-Mailer: git-send-email 2.17.2 (Apple Git-113) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kmemleak could quickly fail to allocate an object structure and then disable itself below in a low-memory situation. For example, running a mmap() workload triggering swapping and OOM. This is especially problematic for running things like LTP testsuite where one OOM test case would disable the whole kmemleak and render the rest of test cases without kmemleak watching for leaking. Kmemleak allocation could fail even though the tracked memory is succeeded. Hence, it could still try to start a direct reclaim if it is not executed in an atomic context (spinlock, irq-handler etc), or a high-priority allocation in an atomic context as a last-ditch effort. Since kmemleak is a debug feature, it is unlikely to be used in production that memory resources is scarce where direct reclaim or high-priority atomic allocations should not be granted lightly. 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. Another approach is to fail back the original allocation once kmemleak_alloc() failed, but there are too many call sites to deal with which makes it error-prone. kmemleak: Cannot allocate a kmemleak_object structure kmemleak: Kernel memory leak detector disabled kmemleak: Automatic memory scanning thread ended RIP: 0010:__alloc_pages_nodemask+0x242a/0x2ab0 Call Trace: allocate_slab+0x4d9/0x930 new_slab+0x46/0x70 ___slab_alloc+0x5d3/0x9c0 __slab_alloc+0x12/0x20 kmem_cache_alloc+0x30a/0x360 create_object+0x96/0x9a0 kmemleak_alloc+0x71/0xa0 kmem_cache_alloc+0x254/0x360 mempool_alloc_slab+0x3f/0x60 mempool_alloc+0x120/0x329 bio_alloc_bioset+0x1a8/0x510 get_swap_bio+0x107/0x470 __swap_writepage+0xab4/0x1650 swap_writepage+0x86/0xe0 Signed-off-by: Qian Cai --- v4: Update the commit log. Fix a typo in comments per Christ. Consolidate the allocation. v3: Update the commit log. Simplify the code inspired by graph_trace_open() from ftrace. v2: Remove the needless checking for NULL objects in slab_post_alloc_hook() per Catalin. mm/kmemleak.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index a2d894d3de07..7f4545ab1f84 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -580,7 +580,16 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, struct rb_node **link, *rb_parent; unsigned long untagged_ptr; - object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp)); + /* + * The tracked memory was allocated successful, if the kmemleak object + * failed to allocate for some reasons, it ends up with the whole + * kmemleak disabled, so try it harder. + */ + gfp = (in_atomic() || irqs_disabled()) ? + gfp_kmemleak_mask(gfp) | GFP_ATOMIC : + gfp_kmemleak_mask(gfp) | __GFP_DIRECT_RECLAIM; + + object = kmem_cache_alloc(object_cache, gfp); if (!object) { pr_warn("Cannot allocate a kmemleak_object structure\n"); kmemleak_disable(); -- 2.17.2 (Apple Git-113)