From patchwork Mon Sep 3 14:40:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 10586019 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 9E8D514BD for ; Mon, 3 Sep 2018 14:40:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8BF5329526 for ; Mon, 3 Sep 2018 14:40:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7EB872952E; Mon, 3 Sep 2018 14:40:56 +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 A324C29526 for ; Mon, 3 Sep 2018 14:40:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F34A6B685B; Mon, 3 Sep 2018 10:40:54 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8A2AA6B685C; Mon, 3 Sep 2018 10:40:54 -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 76D8A6B685D; Mon, 3 Sep 2018 10:40:54 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by kanga.kvack.org (Postfix) with ESMTP id 085346B685B for ; Mon, 3 Sep 2018 10:40:54 -0400 (EDT) Received: by mail-lj1-f197.google.com with SMTP id y2-v6so159099lje.11 for ; Mon, 03 Sep 2018 07:40:53 -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=PX12NYVOY4exy0KtbUfbanoHU3j+FwiRicLg4M9GVLM=; b=UKFiLJpVkc5ZmRORFw3g9bArLXZxwpLiH2invk1Fe7ka8zIva7Q/f9UG3cNFamHT4e WWfJkBpvDjk+KNExosuiViMhPiqG+Xb2nVsaVkz0NArKkKQElPlqN06OEi2Ik8/X2aR+ /gEkbOvdzry84zpoMmtBqM3JDtuEu+wLrIn46R9QcMXkHQghAQsL2ULpc16fS6Y07CPF bULFWcFWqYd/dlAipOLW1Q3AM4DO20F86daRU7TE93RO2mgsNtalFQNjPfBBQWCohajJ jjQSySOjBTLzp9ZaBj437F6t/7IoVJsuFg9sov0ZGyFeFmcpVBnPqdDUU3cjCKIa63tX uQLQ== 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: APzg51DD4mcHdIIGn9VDLWNN1uCddHm2jXPk9UvJQbNOtOsj+Y4EZxAs Z24hVyv1AXWl2DgMFMK/iUnvgiYYXiAtOiTNdmmp7zTdBw6hzyAf9Cz9Tlqkp4h4Og+cUiveUw+ lmx+HaWGFC9v191Nll2FAb2Psjn4lJI/HtpS/1VZx459UvEFcgNAC2/7kaMqLXmPHNw== X-Received: by 2002:a19:6308:: with SMTP id x8-v6mr17365581lfb.140.1535985653407; Mon, 03 Sep 2018 07:40:53 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaCRoMWL8f18JtQg+B3+Lg13VsAmGKpK4ZqQ6qun3CzDi5mtFq9830Ly5qciDbZ3cRUouMY X-Received: by 2002:a19:6308:: with SMTP id x8-v6mr17365544lfb.140.1535985652349; Mon, 03 Sep 2018 07:40:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535985652; cv=none; d=google.com; s=arc-20160816; b=oSZeJOtS0+lWskWhwfZ540SP9Scg7Qg8XCS7fp6U35OjjqWw9UqAv/1yI/ub4z5UEn 8d/7UgzXL5cR7e6uT2N7yFhyqVNEOyn7WXUFGB5tmlg6aO483Es9IEldphkOcJPGV3pV RVCoSJSmL5O9AvH4vs48Bi6oKZUhQwav6ah5+ZwcmMJ0/ijkb65udoJicl2pTcI6M816 6e8/85vq1p3x1liR5+xg3pKs1V5vEKyVAq1ouK6H/lpz7nDBr6ew6rgaxkt2TtHrhT0F TpjqKDefEwJP5Um1l+S520NXxjvt7r+2VyTeeUpmGUDA3kWefoakpJYknmfXv4w4PNWW QtKA== 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=PX12NYVOY4exy0KtbUfbanoHU3j+FwiRicLg4M9GVLM=; b=E75jDzMUqP0l0H64Wy9MXfEz5vDGiIBFoJXbi9+GikUEdX6ic3Hn1YKyHjfA1aX70Y N6DZVDm6BMWssWMdt50V0CUP7hEfLUeyBe3uRlQEDlGVSVTSBpojYexQ4kmVyoWvRdSY GZF8k18+r6YtHELz7sNbcgyYfM8/ygqG3wYJyB7Gi+gOL4klFJOIz4CHYIyJbK0oUoNW XFSQNEYs/FMBZ+95IMuy41l+87Jl95g5QpvqbF+izUsyQFnUz7TxL7cmrLnyGF7WSGtg /Bb5JvvXmR7ZyG4x+0K68JG4pxU7MdNl1u0KQEpDnU+1Ld4wv1nTaL1TdknWv5zhZjim DiVg== 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 c19-v6si17218042lfb.84.2018.09.03.07.40.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Sep 2018 07:40:52 -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 E4A3E182E3; Mon, 3 Sep 2018 16:40:51 +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 lrx-OVbL88V7; Mon, 3 Sep 2018 16:40:48 +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 7F4111831E; Mon, 3 Sep 2018 16:40:48 +0200 (CEST) Received: from boulder02.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 62D421A05E; Mon, 3 Sep 2018 16:40:48 +0200 (CEST) Received: from boulder02.se.axis.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5767B1A05C; Mon, 3 Sep 2018 16:40:48 +0200 (CEST) Received: from seth.se.axis.com (unknown [10.0.2.172]) by boulder02.se.axis.com (Postfix) with ESMTP; Mon, 3 Sep 2018 16:40:48 +0200 (CEST) Received: from lnxartpec.se.axis.com (lnxartpec.se.axis.com [10.88.4.9]) by seth.se.axis.com (Postfix) with ESMTP id 4B49725C2; Mon, 3 Sep 2018 16:40:48 +0200 (CEST) Received: by lnxartpec.se.axis.com (Postfix, from userid 10564) id 47AE38096C; Mon, 3 Sep 2018 16:40:48 +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: [PATCH v3] kmemleak: add module param to print warnings to dmesg Date: Mon, 3 Sep 2018 16:40:46 +0200 Message-Id: <20180903144046.21023-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 a module option to print the full object information to dmesg too. It can be enabled with kmemleak.verbose=1 on the kernel command line, or "echo 1 > /sys/module/kmemleak/parameters/verbose": This allows easier integration of kmemleak into test systems: We have automated test infrastructure to test our Linux systems. With this option, running our tests with kmemleak is as simple as enabling kmemleak and passing this command line option; the test infrastructure knows how to save kernel logs, which will now include kmemleak reports. Without this option, the test infrastructure needs to be specifically taught to read out the kmemleak debugfs file. Removing this need for special handling makes kmemleak more similar to other kernel debug options (slab debugging, debug objects, etc). Signed-off-by: Vincent Whitchurch Acked-by: Catalin Marinas --- v3: Expand use case description. Replace config option with module parameter. mm/kmemleak.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 9a085d525bbc..c91d43738596 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -86,6 +86,7 @@ #include #include #include +#include #include #include #include @@ -181,6 +182,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) */ @@ -235,6 +237,9 @@ static int kmemleak_skip_disable; /* If there are leaks that can be reported */ static bool kmemleak_found_leaks; +static bool kmemleak_verbose; +module_param_named(verbose, kmemleak_verbose, bool, 0600); + /* * Early object allocation/freeing logging. Kmemleak is initialized after the * kernel allocator. However, both the kernel allocator and kmemleak may @@ -299,6 +304,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 +338,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 +389,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 +1622,10 @@ static void kmemleak_scan(void) if (unreferenced_object(object) && !(object->flags & OBJECT_REPORTED)) { object->flags |= OBJECT_REPORTED; + + if (kmemleak_verbose) + print_unreferenced(NULL, object); + new_leaks++; } spin_unlock_irqrestore(&object->lock, flags);