From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Nathan Chancellor <nathan@kernel.org>
Cc: Naveen N Rao <naveen@kernel.org>,
Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
"David S. Miller" <davem@davemloft.net>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
Jinjie Ruan <ruanjinjie@huawei.com>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org, patches@lists.linux.dev
Subject: Re: [PATCH 2/2] kprobes: Use struct_size() in __get_insn_slot()
Date: Thu, 31 Oct 2024 10:58:55 +0900 [thread overview]
Message-ID: <20241031105855.6d88db454041f3cbd9723aab@kernel.org> (raw)
In-Reply-To: <20241030-kprobes-fix-counted-by-annotation-v1-2-8f266001fad0@kernel.org>
On Wed, 30 Oct 2024 09:14:49 -0700
Nathan Chancellor <nathan@kernel.org> wrote:
> __get_insn_slot() allocates 'struct kprobe_insn_page' using a custom
> structure size calculation macro, KPROBE_INSN_PAGE_SIZE. Replace
> KPROBE_INSN_PAGE_SIZE with the struct_size() macro, which is the
> preferred way to calculate the size of flexible structures in the kernel
> because it handles overflow and makes it easier to change and audit how
> flexible structures are allocated across the entire tree.
>
But I like this patch. I'll pick this.
Thank you!
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> ---
> kernel/kprobes.c | 6 +-----
> 1 file changed, 1 insertion(+), 5 deletions(-)
>
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 2cf4628bc97ce2ae18547b513cd75b6350e9cc9c..d452e784b31fa69042229ce0f5ffff9d8b671e92 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -95,10 +95,6 @@ struct kprobe_insn_page {
> char slot_used[] __counted_by(nused);
> };
>
> -#define KPROBE_INSN_PAGE_SIZE(slots) \
> - (offsetof(struct kprobe_insn_page, slot_used) + \
> - (sizeof(char) * (slots)))
> -
> static int slots_per_page(struct kprobe_insn_cache *c)
> {
> return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
> @@ -177,7 +173,7 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
> goto retry;
>
> /* All out of space. Need to allocate a new page. */
> - kip = kmalloc(KPROBE_INSN_PAGE_SIZE(num_slots), GFP_KERNEL);
> + kip = kmalloc(struct_size(kip, slot_used, num_slots), GFP_KERNEL);
> if (!kip)
> goto out;
>
>
> --
> 2.47.0
>
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
prev parent reply other threads:[~2024-10-31 1:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-30 16:14 [PATCH 0/2] kprobes: Adjustments for __counted_by addition Nathan Chancellor
2024-10-30 16:14 ` [PATCH 1/2] kprobes: Fix __get_insn_slot() after __counted_by annotation Nathan Chancellor
2024-10-31 1:58 ` Masami Hiramatsu
2024-10-31 3:37 ` Nathan Chancellor
2024-11-01 1:53 ` Masami Hiramatsu
2024-10-30 16:14 ` [PATCH 2/2] kprobes: Use struct_size() in __get_insn_slot() Nathan Chancellor
2024-10-31 1:58 ` Masami Hiramatsu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241031105855.6d88db454041f3cbd9723aab@kernel.org \
--to=mhiramat@kernel.org \
--cc=anil.s.keshavamurthy@intel.com \
--cc=davem@davemloft.net \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=nathan@kernel.org \
--cc=naveen@kernel.org \
--cc=patches@lists.linux.dev \
--cc=ruanjinjie@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox