Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v2 4/4] scsi: sym53c8xx_2: replace __get_free_pages() with kmalloc()
Date: Sat, 04 Jul 2026 06:22:21 +0000	[thread overview]
Message-ID: <20260704062222.130B61F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260704-b4-scsi-v2-4-7d2d21a810de@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Replacing `__get_free_pages()` with `kmalloc()` breaks the internal custom buddy allocator because `kmalloc()` does not guarantee page-aligned memory, leading to memory leaks, incorrect block coalescing, and potential kernel crashes.
--

commit c528e5943bc5d64b931842bf0abd4f0d0100f91e
Author: Mike Rapoport (Microsoft) <rppt@kernel.org>

scsi: sym53c8xx_2: replace __get_free_pages() with kmalloc()
    
This commit replaces __get_free_pages() and free_pages() with kmalloc() and
kfree() for the internal memory allocator of the sym53c8xx_2 driver. The goal
is to improve the API usage, as kfree() does not require knowing the size of
the freed object.
    
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com

> diff --git a/drivers/scsi/sym53c8xx_2/sym_hipd.h b/drivers/scsi/sym53c8xx_2/sym_hipd.h
> index 9231a28990643..aa365e8ba66fc 100644
> --- a/drivers/scsi/sym53c8xx_2/sym_hipd.h
> +++ b/drivers/scsi/sym53c8xx_2/sym_hipd.h
> @@ -1110,9 +1110,9 @@ sym_build_sge(struct sym_hcb *np, struct sym_tblmove *data, u64 badd, int len)
>   */
>  
>  #define sym_get_mem_cluster()	\
> -	(void *) __get_free_pages(GFP_ATOMIC, SYM_MEM_PAGE_ORDER)
> +	kmalloc(PAGE_SIZE << SYM_MEM_PAGE_ORDER, GFP_ATOMIC)

[Severity: High]
Will this break the custom buddy allocator logic in ___sym_mfree?

The custom buddy allocator relies on XOR arithmetic and bitwise AND to find and
coalesce buddy blocks, which fundamentally requires the base address of the root
block (the memory cluster) to be naturally aligned to its size:

drivers/scsi/sym53c8xx_2/sym_malloc.c:___sym_mfree() {
...
		b = a ^ s;
		q = &h[i];
		while (q->next && q->next != (m_link_p) b) {
			q = q->next;
		}
...
		q->next = q->next->next;
		a = a & b;
...
}

While __get_free_pages guarantees page alignment, kmalloc does not (especially
when KASAN or SLUB debugging is enabled).

If kmalloc returns an unaligned pointer, the XOR buddy calculation computes
incorrect buddy addresses. This prevents blocks from coalescing properly and
causes a memory leak.

Additionally, if blocks from differently offset clusters happen to merge, the
allocator reconstructs a naturally aligned base address that differs from the
original pointer returned by kmalloc. This can lead to calling kfree on an
invalid pointer, resulting in a kernel crash.

>  #define sym_free_mem_cluster(p)	\
> -	free_pages((unsigned long)p, SYM_MEM_PAGE_ORDER)
> +	kfree(p)
>  
>  /*
>   *  Link between free memory chunks of a given size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260704-b4-scsi-v2-0-7d2d21a810de@kernel.org?part=4

      reply	other threads:[~2026-07-04  6:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-04  6:13 [PATCH v2 0/4] scsi: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
2026-07-04  6:13 ` [PATCH v2 1/4] scsi: target: file: use kmalloc() to allocate temporary protection buffer Mike Rapoport (Microsoft)
2026-07-04  6:13 ` [PATCH v2 2/4] scsi: proc: use kmalloc() in proc writers Mike Rapoport (Microsoft)
2026-07-04  6:13 ` [PATCH v2 3/4] scsi: ipr: use kmalloc() to allocate IPR dump buffer memory Mike Rapoport (Microsoft)
2026-07-04  6:28   ` sashiko-bot
2026-07-04  6:13 ` [PATCH v2 4/4] scsi: sym53c8xx_2: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
2026-07-04  6:22   ` sashiko-bot [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=20260704062222.130B61F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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