From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34D23C3ABBF for ; Wed, 7 May 2025 17:55:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B96196B00A7; Wed, 7 May 2025 13:55:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B42796B00A8; Wed, 7 May 2025 13:55:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0BFE6B00A9; Wed, 7 May 2025 13:55:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7AFF96B00A7 for ; Wed, 7 May 2025 13:55:29 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C5B44140534 for ; Wed, 7 May 2025 17:55:30 +0000 (UTC) X-FDA: 83416864020.15.0938069 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2]) by imf01.hostedemail.com (Postfix) with ESMTP id E467D40005 for ; Wed, 7 May 2025 17:55:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=163.com header.s=s110527 header.b="KTz/pmCc"; dmarc=pass (policy=none) header.from=163.com; spf=pass (imf01.hostedemail.com: domain of 00107082@163.com designates 117.135.210.2 as permitted sender) smtp.mailfrom=00107082@163.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746640529; a=rsa-sha256; cv=none; b=TCas+bFr0VzC7Hv8eUo9DbVidUOgThhZLZwrbtcJyMlg3KqTFxKlmlgtPsqKK5IWJelTDC 7yTVTLxrNirNYxaq6y7WvCp67N7Xn5WMCxwKp08g65hh379hZ1Yy7XCjiKqyJEL5WOaVwb N0JDbbp55u3abiwnTn2C4Sp6MqwnlAw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746640529; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=YIY43qYBD99OWzeg8wKW1sLnzHXeQ++VFJkFM35Tleg=; b=OY7hHUO1wuQG9jRLc4bU6qcfBP5J1xHSGpAJPFqlcQAdnUl+zBh/M5Iffq3zjEKNi2Po+9 Bc5mtIhaQPy6lOTCLaTvM8ALICx/KQiNEViwPVGsMOeoNJGKyFXD3kRcRqrpnhH6y1aNyl VYkc80PUBrIymf7AtLH0vAIXGt2BRLE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=163.com header.s=s110527 header.b="KTz/pmCc"; dmarc=pass (policy=none) header.from=163.com; spf=pass (imf01.hostedemail.com: domain of 00107082@163.com designates 117.135.210.2 as permitted sender) smtp.mailfrom=00107082@163.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=YIY43 qYBD99OWzeg8wKW1sLnzHXeQ++VFJkFM35Tleg=; b=KTz/pmCckYv29InklcU2/ Z5SijtOUfr0x6f17gNenc8TyLENfxnkm0q2f99p3n7EM4jU+2REV+JDmaykAoCjS ZxvW5HGCDrcsoNDO3mOXsj0owRXrT3asm5T88zQMmlXhUQLAsQB8vqcJUpGTdsrW uN0reg2sbpoOBD1WO7eiRQ= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-2 (Coremail) with SMTP id _____wBXkbt1nhtoqgIsFA--.21859S4; Thu, 08 May 2025 01:55:17 +0800 (CST) From: David Wang <00107082@163.com> To: surenb@google.com, kent.overstreet@linux.dev, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, David Wang <00107082@163.com> Subject: [PATCH] alloc_tag: avoid mem alloc and iter reset when reading allocinfo Date: Thu, 8 May 2025 01:55:00 +0800 Message-Id: <20250507175500.204569-1-00107082@163.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wBXkbt1nhtoqgIsFA--.21859S4 X-Coremail-Antispam: 1Uf129KBjvAXoW3Cr4fXF48CF4DWr47AF1kKrg_yoW8Jw15Xo ZrWrWvva97WFnxJF98CF1jqws8XrykC3Z3Arn3Xryavr98K3Z5Zr93Kr15X395JF1UCw18 Wasakr1UGwn8J3s7n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTR7KIEDUUUU X-Originating-IP: [111.35.191.17] X-CM-SenderInfo: qqqrilqqysqiywtou0bp/xtbB0g1GqmgbmJWIvgABs6 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E467D40005 X-Stat-Signature: 7t87imh5dc453r3inkpqgkt9pb5fiqcw X-Rspam-User: X-HE-Tag: 1746640527-100194 X-HE-Meta: U2FsdGVkX1+fmLjYSo9pkjpWo7bnsjpvS1eDGB9KN7AK443UEobc9B6b600/Yp7R5/y++MxoIFLDqNkp+3nYuj+8Gy5Z8qrpAl3wQQnwDgI8F/WMcCHzHgfSb5ayRN1UCHqxJ33OBL9ciNpODHxoyHqNxKOxPP3fnVMRBYD6UvMYdQoJ+VQXER1RMAJiMIDibeRRQ+uwqTIcMQ6xyXCgACAE7ExauS7K5r/kVkQOUoIvyqetgA4TVladl1HWMbiUSvJfIUQ64mHid0HUt68olT7VBDO6xURjWVGqWmt5dXun+WPD9cHFl5fvB7cQFdIvGBIlcYfxrsWslbKhTRHOmUM7d0b5ioGoEGCc69LEZoqh1YdWo2xNwigOUnaugJfH30qL8DZhMvJJoNELe6pDh5VtekUenLX9DlfhtsuH9wvJg4vEOn046RTQAGUvn1uivZxfObr1iYrXwxG7UALaM/Bw8zBBWjQHmtznaJ866vvRX2JWTK0iLnQhijL2EB+2/QjJw4fjuIP1+s+SgOj55JdYbB3qVTJr9cK8+8YRMBHoOJpxVFz5kpKC6lxDot+1mCb0x/hmqRNSD7DNiqIGOT+5k7A251YVrlbyg7NBqvi+Xpz0R3lTClnSMSlgBd5+pNgt9WRM9e/W2hs9TspoYjBU0ZxQF3WYGI5QoZxrp2pKdYkxOYZwaQzaGwdb0qgjt/wdLfT2MxytKUWeL6pusI+84QwGeN1iiOmjIxBnXGHEmaHF5H6MKys3jHI/NCCI3+u5pI84v77ZlM1wvR0BBXMHV8dznm3gQgdtsQQn/hgSue5vV3vLfEh4Fp94cVaRNBlJjL1S/HtmQa6fTTzOvTIDAHT1BeFwKlMXmW37hfuXDmCpr8+by1uWicZ8jxtZNIv/LL/b1skktfva43c+dsO6JaZy/XSz+1wDWmf4KnDeeJop1mOd1LMJaMSgYgrz1YEPT43cx/geXG8vKdy s6QBf6WJ vxsNKN2FFY+rnbU8Zmpe+ibHMyLYoM9nlKcV0P3XJTJTzsSORneYMbKOYHD6A7DxPbpTeZMad6pHXsJcLnDQG9w+LcLKDz0Ck7R9fANkmk+hMo5SJVqcaWvpIE8CNteGuj4ow+9ScTWQpVwdIIor7IDr4h3UPT8akR4EMJYIiXuz6QJZob7SjN49ub1ge1l7JZDBs+vYSS8YuM7r/NvU1rSn5D0nrny3gm+ynZNktZs7jt/Q9+Nz0yyKHAHh0c9A5ClCeR3dZQk8nlyS4VjE0FSfBRVv8oXqKa0RZS69yx7K9TUh0sJSE9JlhGZ31CcKVNxdjZ2PPp2pXY72tN3mv/nmFpfTicJxzo0qetdO+VKSAtvHaIT/n1EE3feVXA62P2XSnQpaq4F8zwJWR1EwrxwxGgqGHHp5wbZp9EqDzi594G1iXpVa0LV3LYA== 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: List-Subscribe: List-Unsubscribe: --- The patch is not complete, just want to get feedbacks whether this worth carrying on. --- When reading /proc/allocinfo, for each read syscall, seq_file would invoke start/stop callbacks. In start callback, a memory is alloced to store iterator and the iterator would restart from beginning to walk to its previous position. Each seq_file read() takes at most 4096 bytes, even read with a larger user space buffer, meaning read out /proc/allocinfo, tens of read syscalls are needed. For example, a 306036 bytes allocinfo files need 76 reads: $ sudo cat /proc/allocinfo | wc 3964 16678 306036 For those n=3964 lines, each read takes about m=3964/76=52 lines, the iter would be rewinding: m steps on 1st read, 2*m steps on 2nd read 3*m steps on 3rd read ... n steps on the last read totally, the iterator would be iterated O(n*n/m) times. (Each read would take more time than previous one.) To use a private data alloced when /proc/allocinfo is opened, the n/m memory alloction could be avoid, and there is no need to restart the iterator from very beginning everytime. So only 1 memory allocation and n steps for iterating are needed. (Only when module changed, the iterator should be invalidated and restart.) Timings before: $ time cat /proc/allocinfo > /dev/null real 0m0.007s user 0m0.000s sys 0m0.007s read-syscalls get slower and slower: read(3, "allocinfo - version: 1.0\n# <"..., 131072) = 4085 <0.000062> ... read(3, " 0 0 drivers/gp"..., 131072) = 4046 <0.000135> read(3, " 0 0 sound/core"..., 131072) = 4021 <0.000150> ... and with the change: $ time cat /proc/allocinfo > /dev/null real 0m0.004s user 0m0.000s sys 0m0.003s 7ms drop to 4ms, more friendly to monitoring tools. Detailed strace stats before the change: $ sudo strace -T -e read cat /proc/allocinfo > /dev/null read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@>\2\0\0\0\0\0"..., 832) = 832 <0.000040> read(3, "allocinfo - version: 1.0\n# <"..., 131072) = 4085 <0.000062> read(3, " 4096 1 arch/x86/k"..., 131072) = 4063 <0.000062> read(3, " 0 0 arch/x86/k"..., 131072) = 4029 <0.000061> read(3, " 64 1 kernel/wor"..., 131072) = 4064 <0.000061> read(3, " 0 0 kernel/sch"..., 131072) = 4041 <0.000051> read(3, " 4992 39 kernel/irq"..., 131072) = 4061 <0.000070> read(3, " 0 0 kernel/fut"..., 131072) = 4060 <0.000071> read(3, " 0 0 kernel/aud"..., 131072) = 4025 <0.000122> read(3, " 1536 3 kernel/tra"..., 131072) = 4014 <0.000064> read(3, " 0 0 kernel/tra"..., 131072) = 4079 <0.000067> read(3, " 0 0 kernel/tra"..., 131072) = 4035 <0.000072> read(3, " 0 0 kernel/bpf"..., 131072) = 4044 <0.000074> read(3, " 0 0 kernel/bpf"..., 131072) = 4061 <0.000063> read(3, " 0 0 ./include/"..., 131072) = 4084 <0.000063> read(3, " 0 0 kernel/eve"..., 131072) = 4084 <0.000071> read(3, " 278528 68 mm/memory."..., 131072) = 4068 <0.000070> read(3, " 0 0 mm/zswap.c"..., 131072) = 4091 <0.000082> read(3, " 0 0 mm/balloon"..., 131072) = 4039 <0.000083> read(3, " 0 0 fs/splice."..., 131072) = 4095 <0.000083> read(3, " 0 0 fs/binfmt_"..., 131072) = 4039 <0.000075> read(3, " 192 3 fs/proc/kc"..., 131072) = 4088 <0.000076> read(3, " 0 0 ipc/mqueue"..., 131072) = 4017 <0.000076> read(3, " 0 0 security/s"..., 131072) = 4075 <0.000072> read(3, " 0 0 security/s"..., 131072) = 4029 <0.000084> read(3, " 0 0 security/t"..., 131072) = 4053 <0.000076> read(3, " 0 0 security/a"..., 131072) = 4054 <0.000078> read(3, " 0 0 crypto/rsa"..., 131072) = 4046 <0.000081> read(3, " 0 0 crypto/asy"..., 131072) = 4079 <0.000081> read(3, " 1728 9 block/blk-"..., 131072) = 4034 <0.000084> read(3, " 0 0 io_uring/r"..., 131072) = 4044 <0.000085> read(3, " 0 0 lib/crypto"..., 131072) = 4056 <0.000363> read(3, " 0 0 lib/pldmfw"..., 131072) = 4079 <0.000091> read(3, " 384 6 drivers/pc"..., 131072) = 4091 <0.000098> read(3, " 0 0 drivers/vi"..., 131072) = 4068 <0.000093> read(3, " 4096 1 drivers/ac"..., 131072) = 4086 <0.000089> read(3, " 11040 230 drivers/ac"..., 131072) = 4001 <0.000090> read(3, " 0 0 drivers/ac"..., 131072) = 4030 <0.000094> read(3, " 0 0 drivers/dm"..., 131072) = 4031 <0.000094> read(3, " 0 0 drivers/xe"..., 131072) = 4085 <0.000097> read(3, " 4224 33 drivers/tt"..., 131072) = 4086 <0.000097> read(3, " 0 0 drivers/io"..., 131072) = 4038 <0.000097> read(3, " 0 0 drivers/io"..., 131072) = 4034 <0.000100> read(3, " 64 2 drivers/ba"..., 131072) = 4094 <0.000110> read(3, " 0 0 drivers/ba"..., 131072) = 4025 <0.000100> read(3, " 0 0 drivers/dm"..., 131072) = 4077 <0.000106> read(3, " 0 0 drivers/i2"..., 131072) = 4049 <0.000103> read(3, " 0 0 drivers/ed"..., 131072) = 4035 <0.000108> read(3, " 0 0 drivers/fi"..., 131072) = 4052 <0.000107> read(3, " 0 0 net/core/s"..., 131072) = 4057 <0.000119> read(3, " 5248 82 net/core/r"..., 131072) = 4050 <0.000110> read(3, " 0 0 net/core/d"..., 131072) = 4047 <0.000111> read(3, " 0 0 net/ethtoo"..., 131072) = 4059 <0.000112> read(3, " 32 1 net/ipv4/r"..., 131072) = 4052 <0.000116> read(3, " 1920 10 net/ipv4/i"..., 131072) = 4040 <0.000116> read(3, " 0 0 net/xfrm/x"..., 131072) = 4071 <0.000118> read(3, " 0 0 net/ipv6/i"..., 131072) = 4053 <0.000120> read(3, " 0 0 net/devlin"..., 131072) = 4064 <0.000120> read(3, " 0 0 lib/decomp"..., 131072) = 3996 <0.000123> read(3, " 0 0 drivers/vi"..., 131072) = 4043 <0.000121> read(3, " 0 0 drivers/us"..., 131072) = 4045 <0.000123> read(3, " 0 0 drivers/us"..., 131072) = 4020 <0.000126> read(3, " 64 1 drivers/sc"..., 131072) = 4043 <0.000128> read(3, " 0 0 drivers/ne"..., 131072) = 4094 <0.000130> read(3, " 0 0 drivers/hi"..., 131072) = 4025 <0.000132> read(3, " 672 6 fs/ext4/mb"..., 131072) = 4071 <0.000132> read(3, " 0 0 fs/autofs/"..., 131072) = 4038 <0.000134> read(3, " 0 0 fs/fuse/fi"..., 131072) = 4093 <0.000134> read(3, " 0 0 drivers/gp"..., 131072) = 4035 <0.000133> read(3, " 0 0 drivers/gp"..., 131072) = 4046 <0.000135> read(3, " 0 0 sound/core"..., 131072) = 4021 <0.000150> read(3, " 0 0 sound/core"..., 131072) = 4000 <0.000134> read(3, " 2048 1 drivers/vi"..., 131072) = 4078 <0.000138> read(3, " 3328 104 drivers/gp"..., 131072) = 4040 <0.000143> read(3, " 0 0 fs/overlay"..., 131072) = 4075 <0.000141> read(3, " 0 0 net/netfil"..., 131072) = 4061 <0.000139> read(3, " 0 0 net/xfrm/x"..., 131072) = 2022 <0.000129> read(3, "", 131072) = 0 <0.000114> And with this change: $ sudo strace -T -e read cat /proc/allocinfo > /dev/null read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@>\2\0\0\0\0\0"..., 832) = 832 <0.000018> read(3, "allocinfo - version: 1.0\n# <"..., 131072) = 4085 <0.000066> read(3, " 4096 1 arch/x86/k"..., 131072) = 4063 <0.000062> read(3, " 0 0 arch/x86/k"..., 131072) = 4029 <0.000062> read(3, " 64 1 kernel/wor"..., 131072) = 4064 <0.000064> read(3, " 0 0 kernel/sch"..., 131072) = 4041 <0.000046> read(3, " 4992 39 kernel/irq"..., 131072) = 4061 <0.000053> read(3, " 0 0 kernel/fut"..., 131072) = 4060 <0.000054> read(3, " 0 0 kernel/aud"..., 131072) = 4025 <0.000053> read(3, " 1536 3 kernel/tra"..., 131072) = 4014 <0.000051> read(3, " 0 0 kernel/tra"..., 131072) = 4079 <0.000050> read(3, " 0 0 kernel/tra"..., 131072) = 4035 <0.000051> read(3, " 0 0 kernel/bpf"..., 131072) = 4044 <0.000053> read(3, " 0 0 kernel/bpf"..., 131072) = 4061 <0.000053> read(3, " 0 0 ./include/"..., 131072) = 4084 <0.000052> read(3, " 0 0 kernel/eve"..., 131072) = 4084 <0.000072> read(3, " 286720 70 mm/memory."..., 131072) = 4068 <0.000071> read(3, " 0 0 mm/zswap.c"..., 131072) = 4091 <0.000056> read(3, " 0 0 mm/balloon"..., 131072) = 4039 <0.000056> read(3, " 0 0 fs/splice."..., 131072) = 4095 <0.000076> read(3, " 0 0 fs/binfmt_"..., 131072) = 4039 <0.000075> read(3, " 192 3 fs/proc/kc"..., 131072) = 4088 <0.000057> read(3, " 0 0 ipc/mqueue"..., 131072) = 4017 <0.000055> read(3, " 0 0 security/s"..., 131072) = 4075 <0.000046> read(3, " 0 0 security/s"..., 131072) = 4029 <0.000045> read(3, " 0 0 security/t"..., 131072) = 4053 <0.000050> read(3, " 0 0 security/a"..., 131072) = 4054 <0.000060> read(3, " 0 0 crypto/rsa"..., 131072) = 4046 <0.000181> read(3, " 0 0 crypto/asy"..., 131072) = 4079 <0.000051> read(3, " 1728 9 block/blk-"..., 131072) = 4034 <0.000070> read(3, " 0 0 io_uring/r"..., 131072) = 4044 <0.000078> read(3, " 0 0 lib/crypto"..., 131072) = 4066 <0.000067> read(3, " 0 0 arch/x86/l"..., 131072) = 4085 <0.000066> read(3, " 7168 7 drivers/pc"..., 131072) = 4090 <0.000066> read(3, " 0 0 drivers/vi"..., 131072) = 4063 <0.000065> read(3, " 1024 1 drivers/ac"..., 131072) = 4094 <0.000067> read(3, " 0 0 drivers/ac"..., 131072) = 4023 <0.000063> read(3, " 2048 2 drivers/ac"..., 131072) = 4020 <0.000066> read(3, " 0 0 drivers/dm"..., 131072) = 4094 <0.000067> read(3, " 0 0 drivers/xe"..., 131072) = 4087 <0.000068> read(3, " 1024 1 drivers/tt"..., 131072) = 4093 <0.000056> read(3, " 0 0 drivers/io"..., 131072) = 4037 <0.000044> read(3, " 0 0 drivers/io"..., 131072) = 4095 <0.000061> read(3, " 0 0 drivers/ba"..., 131072) = 4045 <0.000059> read(3, " 0 0 drivers/ba"..., 131072) = 4011 <0.000057> read(3, " 0 0 drivers/ma"..., 131072) = 4087 <0.000064> read(3, " 0 0 drivers/i2"..., 131072) = 4028 <0.000059> read(3, " 0 0 drivers/ed"..., 131072) = 4048 <0.000080> read(3, " 0 0 drivers/fi"..., 131072) = 4082 <0.000054> read(3, " 0 0 net/core/s"..., 131072) = 4091 <0.000068> read(3, " 0 0 net/core/f"..., 131072) = 4041 <0.000053> read(3, " 20 1 net/sched/"..., 131072) = 4054 <0.000051> read(3, " 0 0 net/ethtoo"..., 131072) = 4045 <0.000051> read(3, " 0 0 net/ipv4/r"..., 131072) = 4040 <0.000051> read(3, " 2048 1 net/ipv4/f"..., 131072) = 4056 <0.000050> read(3, " 0 0 net/xfrm/x"..., 131072) = 4053 <0.000056> read(3, " 3072 6 net/ipv6/i"..., 131072) = 4095 <0.000060> read(3, " 0 0 net/devlin"..., 131072) = 4094 <0.000064> read(3, " 0 0 lib/decomp"..., 131072) = 4023 <0.000063> read(3, " 2048 2 drivers/us"..., 131072) = 4053 <0.000062> read(3, " 0 0 drivers/us"..., 131072) = 4011 <0.000068> read(3, " 0 0 drivers/in"..., 131072) = 4092 <0.000059> read(3, " 64 1 drivers/sc"..., 131072) = 4067 <0.000064> read(3, " 0 0 drivers/ne"..., 131072) = 4059 <0.000067> read(3, " 8 1 drivers/hi"..., 131072) = 4050 <0.000052> read(3, " 256 1 fs/ext4/mb"..., 131072) = 4024 <0.000070> read(3, " 0 0 fs/autofs/"..., 131072) = 4082 <0.000070> read(3, " 0 0 fs/fuse/in"..., 131072) = 4074 <0.000054> read(3, " 0 0 drivers/gp"..., 131072) = 4066 <0.000063> read(3, " 0 0 drivers/gp"..., 131072) = 4091 <0.000052> read(3, " 0 0 drivers/gp"..., 131072) = 4029 <0.000064> read(3, " 0 0 drivers/gp"..., 131072) = 4094 <0.000062> read(3, " 128 2 sound/core"..., 131072) = 4069 <0.000068> read(3, " 64 1 sound/pci/"..., 131072) = 4074 <0.000069> read(3, " 384 4 net/bridge"..., 131072) = 4021 <0.000054> read(3, " 0 0 net/netfil"..., 131072) = 4084 <0.000050> read(3, " 0 0 net/xfrm/x"..., 131072) = 1513 <0.000047> read(3, "", 131072) = 0 <0.000030> Signed-off-by: David Wang <00107082@163.com> --- lib/alloc_tag.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 25ecc1334b67..ec83cf798209 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -45,21 +45,17 @@ struct allocinfo_private { static void *allocinfo_start(struct seq_file *m, loff_t *pos) { struct allocinfo_private *priv; - struct codetag *ct; loff_t node = *pos; - priv = kzalloc(sizeof(*priv), GFP_KERNEL); - m->private = priv; - if (!priv) - return NULL; - - priv->print_header = (node == 0); + priv = (struct allocinfo_private *)m->private; codetag_lock_module_list(alloc_tag_cttype, true); - priv->iter = codetag_get_ct_iter(alloc_tag_cttype); - while ((ct = codetag_next_ct(&priv->iter)) != NULL && node) - node--; - - return ct ? priv : NULL; + if (node == 0) { + priv->print_header = true; + priv->iter = codetag_get_ct_iter(alloc_tag_cttype); + codetag_next_ct(&priv->iter); + } + // TODO: need to check iter is valid, or rewinding it. + return priv->iter.ct ? priv : NULL; } static void *allocinfo_next(struct seq_file *m, void *arg, loff_t *pos) @@ -76,12 +72,7 @@ static void *allocinfo_next(struct seq_file *m, void *arg, loff_t *pos) static void allocinfo_stop(struct seq_file *m, void *arg) { - struct allocinfo_private *priv = (struct allocinfo_private *)m->private; - - if (priv) { - codetag_lock_module_list(alloc_tag_cttype, false); - kfree(priv); - } + codetag_lock_module_list(alloc_tag_cttype, false); } static void print_allocinfo_header(struct seq_buf *buf) @@ -249,7 +240,8 @@ static void __init procfs_init(void) if (!mem_profiling_support) return; - if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) { + if (!proc_create_seq_private(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op, + sizeof(struct allocinfo_private), NULL)) { pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME); shutdown_mem_profiling(false); } -- 2.39.2