From patchwork Mon Aug 27 08:38:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 10576633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 275A9174A for ; Mon, 27 Aug 2018 08:38:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12DED296BF for ; Mon, 27 Aug 2018 08:38:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05EA2296C7; Mon, 27 Aug 2018 08:38:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E605296BF for ; Mon, 27 Aug 2018 08:38:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2CD126B3F7E; Mon, 27 Aug 2018 04:38:34 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 251C96B3F7F; Mon, 27 Aug 2018 04:38:34 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CD726B3F97; Mon, 27 Aug 2018 04:38:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by kanga.kvack.org (Postfix) with ESMTP id 87E486B3F7E for ; Mon, 27 Aug 2018 04:38:33 -0400 (EDT) Received: by mail-lf1-f71.google.com with SMTP id x78-v6so3415576lfi.18 for ; Mon, 27 Aug 2018 01:38:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id; bh=s1HTWhbvHPwaXEz/j7BxOfCxqQgxLTCXDRYHikGbi5Q=; b=ahfTE1pXeQNWOyYjwyeCqDPUtRkDCIWyiyOCaDJidkt0YdNZ+2ui+FLEJVTVw131gH AGhc0SsdGh60L9IrZMAAHICd0xBQgRs2ESoBX+cu5t+A30R/dX2IRsXLJyZh1Vglf4ri GddTjMJV1vQLl7rUv9RWqgeqn0j8oElvrLZar8ryP1LzQPkOtOkqf6ftbEMcy1p09f+7 fjvT5ftWcG8mXtivaeix2FJ8RN8FpUy07HNHqNJEEd5MvEYQQ27bELPGxT6I+T7aWSE5 WPO6v7VjpQElf9d1O9w6sM9uSWuewkGbeFP3EUsEl2Fu6a7ml/u/VEYJHr8tfleMe+Fq uHAA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com X-Gm-Message-State: APzg51CMgKi+S7U4TqgAPmycAEyCYvkD0rRE5OUPgsTYlLvyosSgR1xE JAG4OV/6VUIOgqQhtTUn+BC4JyWmT+4y+WgOaojhjB1nFpST5XB/Uldc9znHfsnlkGUuaVlfa3V LHJdbfGcG+bHmppGK/YgFsH30K9AcTkYP/geiP8xM4Px5t+lew1p6reyzdvtxknqJ0g== X-Received: by 2002:a19:c38b:: with SMTP id t133-v6mr8053243lff.7.1535359112865; Mon, 27 Aug 2018 01:38:32 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbB9CaSlZ0XUjLHfMnG5uank3yjfCrMlNPpvsdx4OqEBUPDG9Q0Ky29f8gj5SWFzQzNXAIU X-Received: by 2002:a19:c38b:: with SMTP id t133-v6mr8053195lff.7.1535359111719; Mon, 27 Aug 2018 01:38:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535359111; cv=none; d=google.com; s=arc-20160816; b=XPjWHLYbzOr0aIK3CRXMeOZgV1iqUoZXYyxX6prUj2uVupVfaD/+bFn/2LnPZmdqKI p31c9dN4RUhLIgnpI8XPGMM1MSwdBi3Haq1+J6Y3+xrrgRbganDKu76P5zPlYkrcRsNG ljOa/aYSrIMLSxZpYSf9wZzczdZYdrSLJ7dA8HmEINxXJmA4Ktz1oHlbGtdotzZLymvC egyhLN/ei5k3nZfVqjrTJWHe3ruccD1e8Rs3uII5EpV41LmGwwuHH8S1PPzucOpXlfKH HZIxXYyb9AA3aaD0XOiscSzApkXcBqORBBlD5tgYWMG8KYyYyqVwjITI2/D+Ef9tYPv/ sTfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=s1HTWhbvHPwaXEz/j7BxOfCxqQgxLTCXDRYHikGbi5Q=; b=T1gCcO14x7CcGacwZZcy9a07b6Rljw9izVSM0jovBeh9/dNvjtZZhpq8Jox7Sz/Mob s/tU5xEnmhBe2qzBjCxwQvObV6XPo0eG/jsI3DukbhBiUemNFTrAJeEtrf0/Z5ShhtPz 4+3JaLds7i1g27kBYZlfXbItYnwbUSco8Es7UwCCI9n81TkX4/2ZihuzGVxIcsh6Ltzr ZB7s2TuF/QTfvRFqum25SJFnwiHJlBgZybzmw1wZ9XqtSZpCPg2lHlLsQ7QI+yt9PImz 1UYGXZpn/N+2PEl4l8VIgxkFlXwwB/ezEWgl6Ttz5sel7cxTik1yfu6Ngl+yvRr7g0YO gqGw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com Received: from bastet.se.axis.com (bastet.se.axis.com. [195.60.68.11]) by mx.google.com with ESMTPS id o26-v6si5823684ljg.228.2018.08.27.01.38.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Aug 2018 01:38:31 -0700 (PDT) Received-SPF: pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) client-ip=195.60.68.11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of vincent.whitchurch@axis.com designates 195.60.68.11 as permitted sender) smtp.mailfrom=vincent.whitchurch@axis.com Received: from localhost (localhost [127.0.0.1]) by bastet.se.axis.com (Postfix) with ESMTP id 11FA7180D1; Mon, 27 Aug 2018 10:38:31 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at bastet.se.axis.com Received: from bastet.se.axis.com ([IPv6:::ffff:127.0.0.1]) by localhost (bastet.se.axis.com [::ffff:127.0.0.1]) (amavisd-new, port 10024) with LMTP id hZAeEO5OK2jC; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: from boulder02.se.axis.com (boulder02.se.axis.com [10.0.8.16]) by bastet.se.axis.com (Postfix) with ESMTPS id CAA8D180FF; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: from boulder02.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7573B1A064; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: from boulder02.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 520E91A062; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: from thoth.se.axis.com (unknown [10.0.2.173]) by boulder02.se.axis.com (Postfix) with ESMTP; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: from lnxartpec.se.axis.com (lnxartpec.se.axis.com [10.88.4.9]) by thoth.se.axis.com (Postfix) with ESMTP id 453D41D31; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) Received: by lnxartpec.se.axis.com (Postfix, from userid 10564) id 3FFCC8016F; Mon, 27 Aug 2018 10:38:29 +0200 (CEST) From: Vincent Whitchurch To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vincent Whitchurch Subject: [PATCHv2] kmemleak: Add option to print warnings to dmesg Date: Mon, 27 Aug 2018 10:38:21 +0200 Message-Id: <20180827083821.7706-1-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.11.0 X-TM-AS-GCONF: 00 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Currently, kmemleak only prints the number of suspected leaks to dmesg but requires the user to read a debugfs file to get the actual stack traces of the objects' allocation points. Add an option to print the full object information to dmesg too. This allows easier integration of kmemleak into automated test systems since those kind of systems presumably already save kernel logs. Signed-off-by: Vincent Whitchurch --- v2: Print hex dump too. lib/Kconfig.debug | 9 +++++++++ mm/kmemleak.c | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ab1b599202bc..9a3fc905b8bd 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -593,6 +593,15 @@ config DEBUG_KMEMLEAK_DEFAULT_OFF Say Y here to disable kmemleak by default. It can then be enabled on the command line via kmemleak=on. +config DEBUG_KMEMLEAK_WARN + bool "Print kmemleak object warnings to log buffer" + depends on DEBUG_KMEMLEAK + help + Say Y here to make kmemleak print information about unreferenced + objects (including stacktraces) as warnings to the kernel log buffer. + Otherwise this information is only available by reading the kmemleak + debugfs file. + config DEBUG_STACK_USAGE bool "Stack utilization instrumentation" depends on DEBUG_KERNEL && !IA64 diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 9a085d525bbc..22662715a3dc 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -181,6 +181,7 @@ struct kmemleak_object { /* flag set to not scan the object */ #define OBJECT_NO_SCAN (1 << 2) +#define HEX_PREFIX " " /* number of bytes to print per line; must be 16 or 32 */ #define HEX_ROW_SIZE 16 /* number of bytes to print at a time (1, 2, 4, 8) */ @@ -299,6 +300,25 @@ static void kmemleak_disable(void); kmemleak_disable(); \ } while (0) +#define warn_or_seq_printf(seq, fmt, ...) do { \ + if (seq) \ + seq_printf(seq, fmt, ##__VA_ARGS__); \ + else \ + pr_warn(fmt, ##__VA_ARGS__); \ +} while (0) + +static void warn_or_seq_hex_dump(struct seq_file *seq, int prefix_type, + int rowsize, int groupsize, const void *buf, + size_t len, bool ascii) +{ + if (seq) + seq_hex_dump(seq, HEX_PREFIX, prefix_type, rowsize, groupsize, + buf, len, ascii); + else + print_hex_dump(KERN_WARNING, pr_fmt(HEX_PREFIX), prefix_type, + rowsize, groupsize, buf, len, ascii); +} + /* * Printing of the objects hex dump to the seq file. The number of lines to be * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The @@ -314,10 +334,10 @@ static void hex_dump_object(struct seq_file *seq, /* limit the number of lines to HEX_MAX_LINES */ len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE); - seq_printf(seq, " hex dump (first %zu bytes):\n", len); + warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len); kasan_disable_current(); - seq_hex_dump(seq, " ", DUMP_PREFIX_NONE, HEX_ROW_SIZE, - HEX_GROUP_SIZE, ptr, len, HEX_ASCII); + warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE, + HEX_GROUP_SIZE, ptr, len, HEX_ASCII); kasan_enable_current(); } @@ -365,17 +385,17 @@ static void print_unreferenced(struct seq_file *seq, int i; unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies); - seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n", + warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n", object->pointer, object->size); - seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n", + warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n", object->comm, object->pid, object->jiffies, msecs_age / 1000, msecs_age % 1000); hex_dump_object(seq, object); - seq_printf(seq, " backtrace:\n"); + warn_or_seq_printf(seq, " backtrace:\n"); for (i = 0; i < object->trace_len; i++) { void *ptr = (void *)object->trace[i]; - seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); + warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); } } @@ -1598,6 +1618,9 @@ static void kmemleak_scan(void) if (unreferenced_object(object) && !(object->flags & OBJECT_REPORTED)) { object->flags |= OBJECT_REPORTED; +#ifdef CONFIG_DEBUG_KMEMLEAK_WARN + print_unreferenced(NULL, object); +#endif new_leaks++; } spin_unlock_irqrestore(&object->lock, flags);