public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: aalbersh@kernel.org
Cc: linux-xfs@vger.kernel.org
Subject: [PATCH v1.1 02/40] libxfs: port various kernel apis from 7.0
Date: Wed, 25 Mar 2026 09:33:33 -0700	[thread overview]
Message-ID: <20260325163333.GY6223@frogsfrogsfrogs> (raw)
In-Reply-To: <177429120775.2266274.16124276962731944084.stgit@frogsfrogsfrogs>

From: Darrick J. Wong <djwong@kernel.org>

Port more kernel APIs from Linux, as of version 7.0.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
v1.1: add rvb, clarify the commit message
---
 include/kmem.h |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/include/kmem.h b/include/kmem.h
index 66f8b1fbea8fdf..2c276e929c4808 100644
--- a/include/kmem.h
+++ b/include/kmem.h
@@ -61,6 +61,56 @@ static inline void *kmalloc(size_t size, gfp_t flags)
 #define kzalloc(size, gfp)	kvmalloc((size), (gfp) | __GFP_ZERO)
 #define kvzalloc(size, gfp)	kzalloc((size), (gfp))
 
+/**
+ * kmalloc_array - allocate memory for an array.
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate (see kmalloc).
+ */
+static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
+{
+	size_t bytes;
+
+	if (unlikely(check_mul_overflow(n, size, &bytes)))
+		return NULL;
+	return kmalloc(bytes, flags);
+}
+#define kcalloc(n, size, gfp)	kmalloc_array((n), (size), (gfp) | __GFP_ZERO)
+
+/**
+ * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
+ * @factor1: first factor
+ * @factor2: second factor
+ *
+ * Returns: calculate @factor1 * @factor2, both promoted to size_t,
+ * with any overflow causing the return value to be SIZE_MAX. The
+ * lvalue must be size_t to avoid implicit type conversion.
+ */
+static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
+{
+	size_t bytes;
+
+	if (check_mul_overflow(factor1, factor2, &bytes))
+		return SIZE_MAX;
+
+	return bytes;
+}
+
+#define __alloc_objs(KMALLOC, GFP, TYPE, COUNT)				\
+({									\
+	const size_t __obj_size = size_mul(sizeof(TYPE), COUNT);	\
+	(TYPE *)KMALLOC(__obj_size, GFP);				\
+})
+
+/* Helper macro to avoid gfp flags if they are the default one */
+#define __default_gfp(a,b,...) b
+#define default_gfp(...) __default_gfp(,##__VA_ARGS__,GFP_KERNEL)
+
+#define kzalloc_obj(P, ...) \
+	__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
+#define kmalloc_obj(VAR_OR_TYPE, ...) \
+	__alloc_objs(kmalloc, default_gfp(__VA_ARGS__), typeof(VAR_OR_TYPE), 1)
+
 static inline void kfree(const void *ptr)
 {
 	free((void *)ptr);

  parent reply	other threads:[~2026-03-25 16:33 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-23 18:39 [PATCHBOMB] xfsprogs: rollup of libxfs-7.0-sync patches Darrick J. Wong
2026-03-23 18:40 ` [PATCHSET 1/2] xfsprogs: various bug fixes for 7.0 Darrick J. Wong
2026-03-23 18:41   ` [PATCH 1/1] xfs_repair: don't fail on INCOMPLETE attrs in leaf blocks Darrick J. Wong
2026-03-23 18:41 ` [PATCHSET 2/2] xfsprogs: new libxfs code from kernel 7.0 Darrick J. Wong
2026-03-23 18:41   ` [PATCH 01/40] libxfs: fix XFS_STATS_DEC Darrick J. Wong
2026-03-24  6:15     ` Christoph Hellwig
2026-03-25 16:32     ` [PATCH v1.1 " Darrick J. Wong
2026-03-23 18:41   ` [PATCH 02/40] libxfs: port various kernel apis from 7.0 Darrick J. Wong
2026-03-24  6:16     ` Christoph Hellwig
2026-03-24 17:18       ` Darrick J. Wong
2026-03-25  5:43         ` Christoph Hellwig
2026-03-25 16:33     ` Darrick J. Wong [this message]
2026-03-23 18:42   ` [PATCH 03/40] libfrog: hoist some utilities from libxfs Darrick J. Wong
2026-03-23 18:42   ` [PATCH 04/40] libfrog: fix missing gettext call in current_fixed_time Darrick J. Wong
2026-03-23 18:42   ` [PATCH 05/40] xfs: start creating infrastructure for health monitoring Darrick J. Wong
2026-03-23 18:42   ` [PATCH 06/40] xfs: create event queuing, formatting, and discovery infrastructure Darrick J. Wong
2026-03-23 18:43   ` [PATCH 07/40] xfs: convey filesystem unmount events to the health monitor Darrick J. Wong
2026-03-23 18:43   ` [PATCH 08/40] xfs: convey metadata health " Darrick J. Wong
2026-03-23 18:43   ` [PATCH 09/40] xfs: convey filesystem shutdown " Darrick J. Wong
2026-03-23 18:43   ` [PATCH 10/40] xfs: convey externally discovered fsdax media errors " Darrick J. Wong
2026-03-23 18:44   ` [PATCH 11/40] xfs: convey file I/O " Darrick J. Wong
2026-03-23 18:44   ` [PATCH 12/40] xfs: check if an open file is on the health monitored fs Darrick J. Wong
2026-03-23 18:44   ` [PATCH 13/40] xfs: add media verification ioctl Darrick J. Wong
2026-03-23 18:44   ` [PATCH 14/40] xfs: move struct xfs_log_iovec to xfs_log_priv.h Darrick J. Wong
2026-03-23 18:45   ` [PATCH 15/40] xfs: directly include xfs_platform.h Darrick J. Wong
2026-03-23 18:45   ` [PATCH 16/40] xfs: remove xfs_attr_leaf_hasname Darrick J. Wong
2026-03-23 18:45   ` [PATCH 17/40] xfs: add missing forward declaration in xfs_zones.h Darrick J. Wong
2026-03-23 18:45   ` [PATCH 18/40] xfs: add a xfs_rtgroup_raw_size helper Darrick J. Wong
2026-03-23 18:46   ` [PATCH 19/40] xfs: split and refactor zone validation Darrick J. Wong
2026-03-23 18:46   ` [PATCH 20/40] xfs: delete attr leaf freemap entries when empty Darrick J. Wong
2026-03-23 18:46   ` [PATCH 21/40] xfs: fix freemap adjustments when adding xattrs to leaf blocks Darrick J. Wong
2026-03-23 18:47   ` [PATCH 22/40] xfs: refactor attr3 leaf table size computation Darrick J. Wong
2026-03-23 18:47   ` [PATCH 23/40] xfs: strengthen attr leaf block freemap checking Darrick J. Wong
2026-03-23 18:47   ` [PATCH 24/40] xfs: reduce xfs_attr_try_sf_addname parameters Darrick J. Wong
2026-03-23 18:47   ` [PATCH 25/40] xfs: speed up parent pointer operations when possible Darrick J. Wong
2026-03-23 18:48   ` [PATCH 26/40] xfs: add a method to replace shortform attrs Darrick J. Wong
2026-03-23 18:48   ` [PATCH 27/40] xfs: fix spacing style issues in xfs_alloc.c Darrick J. Wong
2026-03-23 18:48   ` [PATCH 28/40] xfs: don't validate error tags in the I/O path Darrick J. Wong
2026-03-23 18:48   ` [PATCH 29/40] xfs: add zone reset error injection Darrick J. Wong
2026-03-23 18:49   ` [PATCH 30/40] xfs: give the defer_relog stat a xs_ prefix Darrick J. Wong
2026-03-23 18:49   ` [PATCH 31/40] treewide: Replace kmalloc with kmalloc_obj for non-scalar types Darrick J. Wong
2026-03-23 18:49   ` [PATCH 32/40] Convert 'alloc_obj' family to use the new default GFP_KERNEL argument Darrick J. Wong
2026-03-23 18:49   ` [PATCH 33/40] xfs: Refactoring the nagcount and delta calculation Darrick J. Wong
2026-03-23 18:50   ` [PATCH 34/40] xfs: fix code alignment issues in xfs_ondisk.c Darrick J. Wong
2026-03-23 18:50   ` [PATCH 35/40] xfs: remove metafile inodes from the active inode stat Darrick J. Wong
2026-03-23 18:50   ` [PATCH 36/40] xfs: Add a comment in xfs_log_sb() Darrick J. Wong
2026-03-23 18:50   ` [PATCH 37/40] xfs: remove duplicate static size checks Darrick J. Wong
2026-03-23 18:51   ` [PATCH 38/40] xfs: add static size checks for ioctl UABI Darrick J. Wong
2026-03-23 18:51   ` [PATCH 39/40] xfs: Remove redundant NULL check after __GFP_NOFAIL Darrick J. Wong
2026-03-23 18:51   ` [PATCH 40/40] xfs: fix returned valued from xfs_defer_can_append Darrick J. Wong

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=20260325163333.GY6223@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=aalbersh@kernel.org \
    --cc=linux-xfs@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