Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp4403317img; Tue, 26 Mar 2019 08:45:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqxul4hwRviZU6aNhpYD5xxahqKA+ruDNCy/z95IxkmiSQfWvf6Qw/nrNVRECcXLG5r59srM X-Received: by 2002:aa7:814e:: with SMTP id d14mr6694413pfn.101.1553615099921; Tue, 26 Mar 2019 08:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553615099; cv=none; d=google.com; s=arc-20160816; b=sYIZal8rJWxRWLaHnNahe4sCYOCKFuatwx7gUuoi2e7GFZVUliaoYoIKVyZ5J5VpTj JEBJB3qGtw4CRcv35TG1C3iTDdGlGqUxuM9d2aoOuL7/0ehwaYEuZARBOFFC9Eb5OO5Y vpdrzpYygw+G1T5SxDgkRlRAXd7BfWCCADfthdjs703FNdtAoqaReg9EWB5ub79vqa5S eANyb00aRdG5Cgm3+DYT2ETTPshMt21HxB0IIlcOUBn1vhZDSUxjhTiCp77pZCS95w04 4C4EKqwmBMmAWD+3pB88a7aEzaB3DARlVD0E9402iSNA3xvc2LHAjoseueUATI4fyJUy ksqA== 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=xPwO+VfBDlBl+yGZj+3rGMuEeXiQlCjRxp+hpH3Hq0A=; b=EDXTkDn3g2jiu+BHETkHOHudvWKY298fFXYEeT1RB7M77BZDoVb5ebOnW+PtnzTn2j HWBbdsUajech6PGvSnv92NE0UKHgNCmS6rAAWasd+Exi8mke5o1QCPXAVsqFaaUjbVNq by6RYJDK63lR+yZT9K/KGinMz8oTzAWdUtcXW4pcfCjbLaoJD+qPyk5IrT540hW2wxJn o8dgJFFgYIprBMldMUJYMVRusjB3G+MI7aGAUfbzNSpfmE4TS33xbgGElB61BOfHdAQm n2qUFOl2XU1RLUujO0mdCTgvK+iL2foHimYCov0uGimLB7cmf2IYyePAT7IVi25Jd/0R Zs0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=UnRUcSI4; 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 t7si15861478pgp.196.2019.03.26.08.44.44; Tue, 26 Mar 2019 08:44:59 -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=UnRUcSI4; 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 S1732190AbfCZPnz (ORCPT + 99 others); Tue, 26 Mar 2019 11:43:55 -0400 Received: from mail-qt1-f194.google.com ([209.85.160.194]:41983 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732088AbfCZPnx (ORCPT ); Tue, 26 Mar 2019 11:43:53 -0400 Received: by mail-qt1-f194.google.com with SMTP id w30so15090913qta.8 for ; Tue, 26 Mar 2019 08:43:52 -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=xPwO+VfBDlBl+yGZj+3rGMuEeXiQlCjRxp+hpH3Hq0A=; b=UnRUcSI4TZIx2r78mB5JigJyEu6np6Ud0wXDTFslg5/OmfzMRXniVE4SYCONHcDC8y tMMLUKfmTGK3huEJJ36MrE7QswyLaZU+qXbNMvpmrjMu8S0Nupb/xdxL0+NRuMJL9CAG vFgxFzh/865BZvaIKY2fmmfEIQY/oLpQMueoAcMThpgZJiCAZtgZs8Vh7yF5dwOqvABO 0g57MnHGvalTW98+FNHl9MpnGuv1g2kkB9VOAW5f59Pitfdnep4R+ZtywZNVtdgDtC/n XILDCURc37mtovGtcED9cavYh91iZ1DcRowoKJ6YAUpwMUNi/zRI6Od0qBIKw2QZjxlX 5dGA== 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=xPwO+VfBDlBl+yGZj+3rGMuEeXiQlCjRxp+hpH3Hq0A=; b=MGJntlj7iae+hhneeiD1ONQFAgBEh2lgXntlSoYmwOJpTdJLqhKi1MLUGihyS7RXzd 508iAHuYg4Ukgt0BLsDeK3T2MmPZ4WMBhW2BOco9I/kEpcyUEDh7BgGqJQhz5KqGBjxZ 1N9zYXQHt2KoUdRbny4vLSOUYTozbqkVdpabPLFVXwM+Ia3HD6rdRxNUcYnOfzogwR9n Gq+b7P72nOQNrLkiVUFlcCIKwXq7Ba64z4bqbHuYCVfjFNOncY6RajLIUfdcdJW1C+pR r10gQJst+MmaSYDf0wkA2VeNaeodosHi1ntlhKLq3D6h9PDsd63g1oj9n5ZOs8V/5DDE W4vw== X-Gm-Message-State: APjAAAX7UDcXJ0lQriC5XzzVPFqotBnuWtM3DSzPC5C9DBDQoEjeC0xc 2P8Qim8KM6E7eYJF25CqoFr19g== X-Received: by 2002:ac8:2ed4:: with SMTP id i20mr25830138qta.52.1553615032240; Tue, 26 Mar 2019 08:43:52 -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 j93sm7528547qtd.82.2019.03.26.08.43.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Mar 2019 08:43:51 -0700 (PDT) From: Qian Cai To: akpm@linux-foundation.org Cc: 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, Qian Cai Subject: [PATCH v3] kmemleaak: survive in a low-memory situation Date: Tue, 26 Mar 2019 11:43:38 -0400 Message-Id: <20190326154338.20594-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 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. Signed-off-by: Qian Cai --- 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, 11 insertions(+) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index a2d894d3de07..239927166894 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -581,6 +581,17 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, unsigned long untagged_ptr; object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp)); + if (!object) { + /* + * 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 let it success at all cost. + */ + gfp = (in_atomic() || irqs_disabled()) ? 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)