From patchwork Mon Jul 8 19:18:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13726980 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 0F970C3DA41 for ; Mon, 8 Jul 2024 19:18:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC0AB6B00A3; Mon, 8 Jul 2024 15:18:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4A596B00A1; Mon, 8 Jul 2024 15:18:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 764BB6B00A2; Mon, 8 Jul 2024 15:18:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 420C66B009D for ; Mon, 8 Jul 2024 15:18:45 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EF770A13F7 for ; Mon, 8 Jul 2024 19:18:44 +0000 (UTC) X-FDA: 82317547368.17.F947CAC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 278531C0002 for ; Mon, 8 Jul 2024 19:18:42 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gfAubh4v; spf=pass (imf18.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720466300; 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:in-reply-to:references:references:dkim-signature; bh=g29uBA4Aihr80hlQRZhk1dGMqRUfvnyJOm71zecTvqA=; b=1w7++avKG73luwdMzt4EV6e3XxsB53gY/Dj/AzxfgkKfR4kF4uJ7qlaiMib9QZBs/TcBhO dSrmhQkjmKCNZMv51CVDO6m+KVkDTRQ2jr4Z4Oe3amLF1rcrDZJbDAzy/fURzo+1TYRHbY 6jS/UYkHi1joDWSt4jw4sz5gel2nHzA= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gfAubh4v; spf=pass (imf18.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720466300; a=rsa-sha256; cv=none; b=dOUl3CCSiW7RN5o+wjWqWWzsFlsPMuRwVdzIRn/aPLcNaEoYBZ19/4hBr+d3H5FeXZqFpW X1JQSseblJfbYKC+hEby8mbl0cTtpVSYYDpxuwIi/QKtJuHsOB1rMTKVixRkmBQtT/vhbx SEOULHd75+X8EWUIv1PW8BtAdfgZVwY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3C02E6110F; Mon, 8 Jul 2024 19:18:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DAEDC4AF17; Mon, 8 Jul 2024 19:18:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720466321; bh=fvRlIwowaF9/LBzfEGVnkRN2BeFqeZ8hAGZLXhTB0Rk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gfAubh4vdFbZlhDJOkduJGAqtq4TRKB8ZZxx7chKka/17RGOfga2LdRJPnigStdgh aqSD1mvdHlQqjsXnKH0lMRKL1O1re3Jhnol8lNR9YLyzZVXbE9NQ5kLZ4X37DQh8F/ KosUIK5QKo63xy9213Pq1FOZ+SC+Qfs3Sy/iuSNU6Z8PopCvsGEpAtAH1WR0a2BLpF lEbEReGdJ2aai8mgey5MSf1XlyKHHsduE7kPsOj72eUSM3y7eHfzeyH9IYY0KFrlIJ rIdfaUlYSw+WMXmtO5GNJ6LQ+NCAygm3NHCFbKChlkh+papvqh+3tTjcKNHL4NfAkr 1o0EbCNw6xKYQ== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Tony Luck , "Guilherme G. Piccoli" , linux-hardening@vger.kernel.org, Jann Horn , Nick Desaulniers , Miguel Ojeda , Marco Elver , Nathan Chancellor , Hao Luo , Przemek Kitszel , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Mark Rutland , Jakub Kicinski , Petr Pavlu , Alexander Lobakin , Tony Ambardar , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC][PATCH 4/4] pstore: Replace classic kmalloc code pattern with typed argument Date: Mon, 8 Jul 2024 12:18:38 -0700 Message-Id: <20240708191840.335463-4-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240708190924.work.846-kees@kernel.org> References: <20240708190924.work.846-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3200; i=kees@kernel.org; h=from:subject; bh=fvRlIwowaF9/LBzfEGVnkRN2BeFqeZ8hAGZLXhTB0Rk=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmjDuOIMJSOfezmI7F7aWAtc3Jd3uJ1pxI/qwE2 X/jjfpapACJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZow7jgAKCRCJcvTf3G3A Ju/pD/9jE2sUjkU0LDQMn2KwGX2yZsbWJ5NO7+g3aGS+vs2V//QT/bnGfFzfd1De6giGp0M2kHm P0ODwqfZv69aT5o0x9p7Hb393ZH1Bbq/3ATW79p6VHI85Blp6qLckXX9HkdcdAMCBsQo2Xuj7Wd IX634YZ1O8v+G8u7LZvqYndpeVOeCa9oPw9S5XCCHQ0xv+ViQ8cip0Bk/ctV/zeEyHX0eWjGtJF 8dcGd2uslfUqdLlyHhJFrHJxQx1MsSOKWygJ5m0lnD25WD5V7jmoRJBW6O05+R1rrRnXDYbSKPm rdnkT0OnLjJQQq9sLSBIlqYDGE4Df9H9H2L+atoDC/NXyB0T7ovyJUomc1xqB8SwuV0MpGF+YYv Z5U8TN5M4I0yojNTfsQXnLl3xNbIKvL2/3YWgUIBgNKiAMF+PW7OhsAOf7DffaT/Rrt2lEPCkRN R3mwvksK9PqYinrloTlRfVGth1VEUmAwk1HJP7AsLvierItqfSxtTMjFQOipLcrZOJM2fZ8KXQH oQS0dWvgNmyT70TKWTtW4mevrcJg7FBIinAI4QUch3X/b8ZrnBTlTVaNwHLjZusj/a/2VU+TjY+ dy6GwLfOgm71EQqo+Pt/X9+FaWC2Q6mNvKmpnxFYR6DvBelFg+qFu3/7iz7wLW4yyXq58WazK1N 26/45pC7RBdRDmQ= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Stat-Signature: dib3dn1xhe83yg9n8kmbaz9itwujuxz3 X-Rspam-User: X-Rspamd-Queue-Id: 278531C0002 X-Rspamd-Server: rspam02 X-HE-Tag: 1720466322-773431 X-HE-Meta: U2FsdGVkX1/rRASAHEM8Rj1nNxeEnwrc9jMA31MYfIJLhj7NEsbkRajFlKYc8J7Mx2aBLMlAgmaV3m5QgaOyLKWC5hno0xw4H0gPXu48HuXJGwEosVPcQaY7E3nawvDsH6+Y+mXbZ1ITqrdblKjRvcRVVoNLdqXI0MvFe3VFciTpgoTJwYVnqY/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xaX62qqCEst+8nXXvVWXx5rJS3k3mCQv7WAdTcHPbDy1wM22rRc/FM5IPFdXnBiRYIWAXIyqj+xPWkpO1QQQzC4/aMG0yIfR0o78y/l3abQMeBVdjCIOvZL4WvBbYc= 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: Using a short Coccinelle script, it is possible to replace the classic kmalloc code patterns with the typed information: @alloc@ type TYPE; TYPE *P; expression GFP; identifier ALLOC =~ "k[mz]alloc"; @@ P = ALLOC( - \(sizeof(*P)\|sizeof(TYPE)\), GFP) + P, GFP) Show this just for kmalloc/kzalloc usage in fs/pstore as an example. Doing this for all allocator calls in the kernel touches much more: 11941 files changed, 22459 insertions(+), 22345 deletions(-) And obviously requires some more wrappers for kv*alloc, devm_*alloc, etc. Signed-off-by: Kees Cook --- Cc: Tony Luck Cc: "Guilherme G. Piccoli" Cc: linux-hardening@vger.kernel.org --- fs/pstore/blk.c | 2 +- fs/pstore/platform.c | 2 +- fs/pstore/ram.c | 3 +-- fs/pstore/ram_core.c | 2 +- fs/pstore/zone.c | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/fs/pstore/blk.c b/fs/pstore/blk.c index de8cf5d75f34..7bb9cacb380f 100644 --- a/fs/pstore/blk.c +++ b/fs/pstore/blk.c @@ -297,7 +297,7 @@ static int __init __best_effort_init(void) return -EINVAL; } - best_effort_dev = kzalloc(sizeof(*best_effort_dev), GFP_KERNEL); + best_effort_dev = kzalloc(best_effort_dev, GFP_KERNEL); if (!best_effort_dev) return -ENOMEM; diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index 03425928d2fb..4e527c3ea530 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -682,7 +682,7 @@ void pstore_get_backend_records(struct pstore_info *psi, struct pstore_record *record; int rc; - record = kzalloc(sizeof(*record), GFP_KERNEL); + record = kzalloc(record, GFP_KERNEL); if (!record) { pr_err("out of memory creating record\n"); break; diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index b1a455f42e93..a0665a98b135 100644 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -228,8 +228,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record) */ struct persistent_ram_zone *tmp_prz, *prz_next; - tmp_prz = kzalloc(sizeof(struct persistent_ram_zone), - GFP_KERNEL); + tmp_prz = kzalloc(tmp_prz, GFP_KERNEL); if (!tmp_prz) return -ENOMEM; prz = tmp_prz; diff --git a/fs/pstore/ram_core.c b/fs/pstore/ram_core.c index f1848cdd6d34..01ddf1be6c3a 100644 --- a/fs/pstore/ram_core.c +++ b/fs/pstore/ram_core.c @@ -588,7 +588,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, struct persistent_ram_zone *prz; int ret = -ENOMEM; - prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL); + prz = kzalloc(prz, GFP_KERNEL); if (!prz) { pr_err("failed to allocate persistent ram zone\n"); goto err; diff --git a/fs/pstore/zone.c b/fs/pstore/zone.c index 694db616663f..8df890bb4db9 100644 --- a/fs/pstore/zone.c +++ b/fs/pstore/zone.c @@ -1165,7 +1165,7 @@ static struct pstore_zone *psz_init_zone(enum pstore_type_id type, return ERR_PTR(-ENOMEM); } - zone = kzalloc(sizeof(struct pstore_zone), GFP_KERNEL); + zone = kzalloc(zone, GFP_KERNEL); if (!zone) return ERR_PTR(-ENOMEM);