Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp755496iog; Mon, 13 Jun 2022 12:13:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDC/ssDOymx0E1ewj+HRSQ+PrqZnvZa4DMpn95A3YsvRBdjQMRKYJISV5HL8qa2TbGzNjb X-Received: by 2002:a63:5610:0:b0:3f2:7e19:1697 with SMTP id k16-20020a635610000000b003f27e191697mr951519pgb.74.1655147605213; Mon, 13 Jun 2022 12:13:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655147605; cv=none; d=google.com; s=arc-20160816; b=JBiKhgsVbe55CnJUst9PReXyTOongdxjsuKDnuUJCH/PIgNvB4Bpl+hxD5YUB22iFv YCbIoHX08WWUVJ2X6oIBAhPXdxvB+t9xwuSi0E5rHSCvBQZJ+rjbRMKN8kZNWSmdETcr 8gP86A0a+I2kf9Xdpgw6cS6gstzrS0t3PoYA1UEfgQUfLhsgp6Eejm6TlB3byq8SOXky +3miFAo7MNvNi7rYa43tlctICkvEeXGl0ygXi8L/orF1mX/+sLlzPF1/uCogMUEuoq8P rk8Le2QH84jEA/9M8hg6qsf0UNWhGgvQrD0zw2bcV65wOvMDx3BAaQ0mcGdX4eUYsNfw y67A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=m5WZciF763E0+xCIFmFDSURLec1OZVtfXv6gTUhZ7xc=; b=QCORfkWOYkJO6tKEaNjQeceXdRsrbXe+NaS4ORT2hveuiEiFeYnIol2J7j9tz6FiUH MCW5GUJTpXydqjt1MWA2GOvnZWiTwikqaHO0dwsWijO9vT9PjlA27U8XXRYF8T4Bltul 0MZ2O28lg3aPq2TBr4KlJBJ5H39nZCqgT29dBSGo8EFM9pUjGCoaqVwfWlq3r7EtBkVj MWKgHyXcD8dwjGE2uZ1munEKn4FpKmtiCSnYlkNczCPZCmiUNOXAk++6EAFH4Dh6nqLx oVrYMP6JMT1jpVd3kBIrX90mgfCgXJc24PfXrxN/5k4Muhr646/X9pwK8K6DJoIbFmq8 zZkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@veeam.com header.s=mx2-2022 header.b=KEZcWRF1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=veeam.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g7-20020a655947000000b003fd7477c7f0si9755807pgu.615.2022.06.13.12.13.13; Mon, 13 Jun 2022 12:13:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@veeam.com header.s=mx2-2022 header.b=KEZcWRF1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=veeam.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343577AbiFMS5i (ORCPT + 99 others); Mon, 13 Jun 2022 14:57:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237408AbiFMSxA (ORCPT ); Mon, 13 Jun 2022 14:53:00 -0400 Received: from mx2.veeam.com (mx2.veeam.com [64.129.123.6]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29EE2F45F4; Mon, 13 Jun 2022 08:55:17 -0700 (PDT) Received: from mail.veeam.com (prgmbx01.amust.local [172.24.128.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.veeam.com (Postfix) with ESMTPS id 95DB6431DB; Mon, 13 Jun 2022 11:55:14 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=veeam.com; s=mx2-2022; t=1655135714; bh=m5WZciF763E0+xCIFmFDSURLec1OZVtfXv6gTUhZ7xc=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=KEZcWRF1G+QDkNvQa4Gq7V1cn+XBwcXUTvTg4DapPGa38uy8tGN4hoEtzj0sO+a+6 AhsseO7rfJX8AzoDrzbxK1IucG/Bf2t3rvmkSv8GAEBLqZXLIEFxeoi3U+P/cSAMY5 Demo556Ii1jrHE+iJZ8LBxJeLO2EW/HSCDo/hjI28+MePQwnugtKqNtVUPDbw0EwUu dWL4DNkU0u6Z65tyYjI6lWd8YpRO/N+3l0n1QCv/7fWEqPOJaQFZXS84h7kA8kqpDp uWCVugL6DwwLQgaFUYTwrg5UEGLIJszs8WR5Ud6ek83dxrKhPHrtwRQe0ZlF4p/vBK x/QIqdZ/HKh+A== Received: from prgdevlinuxpatch01.amust.local (172.24.14.5) by prgmbx01.amust.local (172.24.128.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.9; Mon, 13 Jun 2022 17:54:53 +0200 From: Sergei Shtepa To: , , CC: Sergei Shtepa Subject: [PATCH 17/20] block, blksnap: debugging mechanism for monitoring memory consumption Date: Mon, 13 Jun 2022 18:53:10 +0300 Message-ID: <1655135593-1900-18-git-send-email-sergei.shtepa@veeam.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1655135593-1900-1-git-send-email-sergei.shtepa@veeam.com> References: <1655135593-1900-1-git-send-email-sergei.shtepa@veeam.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.24.14.5] X-ClientProxiedBy: prgmbx01.amust.local (172.24.128.102) To prgmbx01.amust.local (172.24.128.102) X-EsetResult: clean, is OK X-EsetId: 37303A29D2A50A536D7766 X-Veeam-MMEX: True X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Of course, the kernel contains mechanisms for detecting memory leaks. There are tools for monitoring memory consumption. However, this mechanism turned out to be very simple and convenient for debugging, and therefore it was decided to leave it in the current version. Signed-off-by: Sergei Shtepa --- drivers/block/blksnap/memory_checker.c | 100 +++++++++++++++++++++++++ drivers/block/blksnap/memory_checker.h | 41 ++++++++++ 2 files changed, 141 insertions(+) create mode 100644 drivers/block/blksnap/memory_checker.c create mode 100644 drivers/block/blksnap/memory_checker.h diff --git a/drivers/block/blksnap/memory_checker.c b/drivers/block/blksnap/memory_checker.c new file mode 100644 index 000000000000..96988ed99f08 --- /dev/null +++ b/drivers/block/blksnap/memory_checker.c @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifdef CONFIG_BLK_SNAP_DEBUG_MEMORY_LEAK +#define pr_fmt(fmt) KBUILD_MODNAME "-memory_checker: " fmt +#include +#include +#include "memory_checker.h" + +char *memory_object_names[] = { + /*alloc_page*/ + "page", + /*kzalloc*/ + "cbt_map", + "chunk", + "blk_snap_snaphot_event", + "diff_area", + "big_buffer", + "diff_io", + "diff_storage", + "storage_bdev", + "storage_block", + "diff_region", + "diff_buffer", + "event", + "snapimage", + "snapshot", + "tracker", + "tracked_device", + /*kcalloc*/ + "blk_snap_cbt_info", + "blk_snap_block_range", + "blk_snap_dev_t", + "tracker_array", + "snapimage_array", + "superblock_array", + "blk_snap_image_info", + /*end*/ +}; + +static_assert( + sizeof(memory_object_names) == (memory_object_count * sizeof(char *)), + "The size of enum memory_object_type is not equal to size of memory_object_names array."); + +static atomic_t memory_counter[memory_object_count]; +static atomic_t memory_counter_max[memory_object_count]; + +void memory_object_inc(enum memory_object_type type) +{ + int value; + + if (unlikely(type >= memory_object_count)) + return; + + value = atomic_inc_return(&memory_counter[type]); + if (value > atomic_read(&memory_counter_max[type])) + atomic_inc(&memory_counter_max[type]); +} + +void memory_object_dec(enum memory_object_type type) +{ + if (unlikely(type >= memory_object_count)) + return; + + atomic_dec(&memory_counter[type]); +} + +int memory_object_print(void) +{ + int inx; + int not_free = 0; + + pr_info("Objects in memory:\n"); + for (inx = 0; inx < memory_object_count; inx++) { + int count = atomic_read(&memory_counter[inx]); + + if (count) { + not_free += count; + pr_info("%s: %d\n", memory_object_names[inx], count); + } + } + if (not_free) + pr_info("Found %d allocated objects\n", not_free); + else + pr_info("All objects have been released\n"); + return not_free; +} + +void memory_object_max_print(void) +{ + int inx; + + pr_info("Maximim objects in memory:\n"); + for (inx = 0; inx < memory_object_count; inx++) { + int count = atomic_read(&memory_counter_max[inx]); + + if (count) + pr_info("%s: %d\n", memory_object_names[inx], count); + } + pr_info(".\n"); +} +#endif diff --git a/drivers/block/blksnap/memory_checker.h b/drivers/block/blksnap/memory_checker.h new file mode 100644 index 000000000000..b19a2584d0ce --- /dev/null +++ b/drivers/block/blksnap/memory_checker.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#pragma once +#ifdef CONFIG_BLK_SNAP_DEBUG_MEMORY_LEAK + +enum memory_object_type { + /*alloc_page*/ + memory_object_page, + /*kzalloc*/ + memory_object_cbt_map, + memory_object_chunk, + memory_object_blk_snap_snapshot_event, + memory_object_diff_area, + memory_object_big_buffer, + memory_object_diff_io, + memory_object_diff_storage, + memory_object_storage_bdev, + memory_object_storage_block, + memory_object_diff_region, + memory_object_diff_buffer, + memory_object_event, + memory_object_snapimage, + memory_object_snapshot, + memory_object_tracker, + memory_object_tracked_device, + /*kcalloc*/ + memory_object_blk_snap_cbt_info, + memory_object_blk_snap_block_range, + memory_object_blk_snap_dev_t, + memory_object_tracker_array, + memory_object_snapimage_array, + memory_object_superblock_array, + memory_object_blk_snap_image_info, + /*end*/ + memory_object_count +}; + +void memory_object_inc(enum memory_object_type type); +void memory_object_dec(enum memory_object_type type); +int memory_object_print(void); +void memory_object_max_print(void); +#endif -- 2.20.1