Linux-bcachefs Archive mirror
 help / color / mirror / Atom feed
From: Zijie Zhao <zzjas98@gmail.com>
To: kent.overstreet@linux.dev, bfoster@redhat.com
Cc: linux-bcachefs@vger.kernel.org, chenyuan0y@gmail.com
Subject: [fs/bcachefs] Question about memory allocation in btree_paths_realloc
Date: Wed, 20 Mar 2024 16:37:02 -0500	[thread overview]
Message-ID: <080de8c1-497c-45aa-a5a0-10dba5319c64@gmail.com> (raw)

Dear BCACHEFS maintainers,

We encountered an unusual usage of kzalloc in btree_paths_realloc 
while performing a static analysis for kernel code.


https://elixir.bootlin.com/linux/latest/source/fs/bcachefs/btree_iter.c#L1523

```
static noinline void btree_paths_realloc(struct btree_trans *trans)
{
	unsigned nr = trans->nr_paths * 2;

	void *p = kzalloc(BITS_TO_LONGS(nr) * sizeof(unsigned long) +
			  sizeof(struct btree_trans_paths) +
			  nr * sizeof(struct btree_path) +
			  nr * sizeof(btree_path_idx_t) + 8 +
			  nr * sizeof(struct btree_insert_entry), GFP_KERNEL|__GFP_NOFAIL);

	unsigned long *paths_allocated = p;
	memcpy(paths_allocated, trans->paths_allocated, 
BITS_TO_LONGS(trans->nr_paths) * sizeof(unsigned long));
	...
}
```

Here kzalloc might return NULL in case of out-of-memory, making 
memcpy(NULL,...) to have undefined behavior. Would a NULL check be 
needed here?

Please let us know if we missed any key information or assumption! We 
appreciate your time!


Best,
Zijie

             reply	other threads:[~2024-03-20 21:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-20 21:37 Zijie Zhao [this message]
2024-03-20 21:48 ` [fs/bcachefs] Question about memory allocation in btree_paths_realloc Kent Overstreet
2024-03-20 21:50   ` Zijie Zhao

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=080de8c1-497c-45aa-a5a0-10dba5319c64@gmail.com \
    --to=zzjas98@gmail.com \
    --cc=bfoster@redhat.com \
    --cc=chenyuan0y@gmail.com \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcachefs@vger.kernel.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).