Skip to contentSkip to conversation

Remove/convert more kmem_* wrappers

PATCH In review Carlos Maiolino 3 participants 19 messages Nov 20, 2019 – May 14, 2020 XFS filesystem

lore
Share & tools
lore
#0 lore raw

cover Remove/convert more kmem_* wrappers

Hi,

in this new series, we remove most of the remaining kmem_* wrappers.

All of the wrappers being removed in this series can be directly replaced by generic kernel kmalloc()/kzalloc() interface.

Only interface kept is kmem_alloc() but has been converted into a local helper.

This series should be applied on top of my previous series aiming to clean up our memory allocation interface.

Darrick, I believe this is slightly different from what you suggested previously, about converting kmem_* interfaces to use GFP flags directly. At least I read that as keeping current kmem_* interface, and getting rid of KM_* flags now.

But, I believe these patches does not change any allocation logic, and after the series we are left with fewer users of KM_* flags users to get rid of, which IMHO will be easier. And also I already had the patches mostly done :)

Let me know if this is ok for you.

Carlos Maiolino (5):
xfs: remove kmem_zone_zalloc()
xfs: Remove kmem_zone_alloc() wrapper
xfs: remove kmem_zalloc() wrapper
xfs: Remove kmem_realloc
xfs: Convert kmem_alloc() users

 fs/xfs/kmem.c                      | 51 +++---------------------------
 fs/xfs/kmem.h                      | 16 ----------
 fs/xfs/libxfs/xfs_alloc.c          |  3 +-
 fs/xfs/libxfs/xfs_alloc_btree.c    |  2 +-
 fs/xfs/libxfs/xfs_attr_leaf.c      |  9 +++---
 fs/xfs/libxfs/xfs_bmap.c           |  8 +++--
 fs/xfs/libxfs/xfs_bmap_btree.c     |  2 +-
 fs/xfs/libxfs/xfs_da_btree.c       | 16 +++++-----
 fs/xfs/libxfs/xfs_defer.c          |  4 +--
 fs/xfs/libxfs/xfs_dir2.c           | 29 ++++++++---------
 fs/xfs/libxfs/xfs_dir2_block.c     |  2 +-
 fs/xfs/libxfs/xfs_dir2_sf.c        |  8 ++---
 fs/xfs/libxfs/xfs_ialloc_btree.c   |  2 +-
 fs/xfs/libxfs/xfs_iext_tree.c      | 14 +++++---
 fs/xfs/libxfs/xfs_inode_fork.c     | 26 ++++++++-------
 fs/xfs/libxfs/xfs_refcount.c       |  9 +++---
 fs/xfs/libxfs/xfs_refcount_btree.c |  2 +-
 fs/xfs/libxfs/xfs_rmap.c           |  2 +-
 fs/xfs/libxfs/xfs_rmap_btree.c     |  2 +-
 fs/xfs/scrub/agheader.c            |  4 +--
 fs/xfs/scrub/bitmap.c              |  7 ++--
 fs/xfs/scrub/btree.c               |  4 +--
 fs/xfs/scrub/fscounters.c          |  3 +-
 fs/xfs/scrub/refcount.c            |  4 +--
 fs/xfs/xfs_attr_inactive.c         |  2 +-
 fs/xfs/xfs_attr_list.c             |  2 +-
 fs/xfs/xfs_bmap_item.c             |  4 +--
 fs/xfs/xfs_buf.c                   | 13 ++++----
 fs/xfs/xfs_buf_item.c              |  6 ++--
 fs/xfs/xfs_dquot.c                 |  2 +-
 fs/xfs/xfs_dquot_item.c            |  3 +-
 fs/xfs/xfs_error.c                 |  4 +--
 fs/xfs/xfs_extent_busy.c           |  3 +-
 fs/xfs/xfs_extfree_item.c          | 12 ++++---
 fs/xfs/xfs_filestream.c            |  2 +-
 fs/xfs/xfs_icache.c                |  2 +-
 fs/xfs/xfs_icreate_item.c          |  2 +-
 fs/xfs/xfs_inode.c                 |  4 +--
 fs/xfs/xfs_inode_item.c            |  3 +-
 fs/xfs/xfs_itable.c                |  8 ++---
 fs/xfs/xfs_iwalk.c                 |  5 +--
 fs/xfs/xfs_log.c                   | 12 ++++---
 fs/xfs/xfs_log_cil.c               |  8 ++---
 fs/xfs/xfs_log_priv.h              |  2 +-
 fs/xfs/xfs_log_recover.c           | 21 ++++++------
 fs/xfs/xfs_mount.c                 |  7 ++--
 fs/xfs/xfs_mru_cache.c             |  5 +--
 fs/xfs/xfs_qm.c                    |  6 ++--
 fs/xfs/xfs_refcount_item.c         |  9 +++---
 fs/xfs/xfs_rmap_item.c             |  8 +++--
 fs/xfs/xfs_rtalloc.c               |  2 +-
 fs/xfs/xfs_super.c                 |  2 +-
 fs/xfs/xfs_trace.h                 |  1 -
 fs/xfs/xfs_trans.c                 |  4 +--
 fs/xfs/xfs_trans_ail.c             |  3 +-
 fs/xfs/xfs_trans_dquot.c           |  3 +-
 56 files changed, 185 insertions(+), 214 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH v2 1/5 xfs: remove kmem_zone_zalloc()

Use kmem_cache_zalloc() directly.

We can use __GFP_NOFAIL to loop infinitely on a memory allocation request, which also has an implied congestion_wait()

---
V2:
- shortens a >80 characters line left on V1
- rephrase commit log

 fs/xfs/kmem.h                      | 6 ------
 fs/xfs/libxfs/xfs_alloc_btree.c    | 2 +-
 fs/xfs/libxfs/xfs_bmap.c           | 3 ++-
 fs/xfs/libxfs/xfs_bmap_btree.c     | 2 +-
 fs/xfs/libxfs/xfs_da_btree.c       | 2 +-
 fs/xfs/libxfs/xfs_ialloc_btree.c   | 2 +-
 fs/xfs/libxfs/xfs_inode_fork.c     | 6 +++---
 fs/xfs/libxfs/xfs_refcount_btree.c | 2 +-
 fs/xfs/libxfs/xfs_rmap_btree.c     | 2 +-
 fs/xfs/xfs_bmap_item.c             | 4 ++--
 fs/xfs/xfs_buf.c                   | 2 +-
 fs/xfs/xfs_buf_item.c              | 2 +-
 fs/xfs/xfs_dquot.c                 | 2 +-
 fs/xfs/xfs_extfree_item.c          | 6 ++++--
 fs/xfs/xfs_icreate_item.c          | 2 +-
 fs/xfs/xfs_inode_item.c            | 3 ++-
 fs/xfs/xfs_log.c                   | 7 ++++---
 fs/xfs/xfs_log_cil.c               | 2 +-
 fs/xfs/xfs_log_priv.h              | 2 +-
 fs/xfs/xfs_refcount_item.c         | 5 +++--
 fs/xfs/xfs_rmap_item.c             | 5 +++--
 fs/xfs/xfs_trans.c                 | 4 ++--
 fs/xfs/xfs_trans_dquot.c           | 3 ++-
 23 files changed, 39 insertions(+), 37 deletions(-)
fs/xfs/kmem.h +0 -6
index ccdc72519339..3242de676808 100644
@@ -78,12 +78,6 @@ kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
 
 extern void *kmem_zone_alloc(kmem_zone_t *, xfs_km_flags_t);
 
-static inline void *
-kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
-{
-	return kmem_zone_alloc(zone, flags | KM_ZERO);
-}
-
 static inline struct page *
 kmem_to_page(void *addr)
 {
fs/xfs/libxfs/xfs_alloc_btree.c +1 -1
index 279694d73e4e..0867c1fad11b 100644
@@ -487,7 +487,7 @@ xfs_allocbt_init_cursor(
 
 	ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
 
-	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
+	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
 
 	cur->bc_tp = tp;
 	cur->bc_mp = mp;
fs/xfs/libxfs/xfs_bmap.c +2 -1
index b7cc2f9eae7b..9fbdca183465 100644
@@ -1104,7 +1104,8 @@ xfs_bmap_add_attrfork(
 	if (error)
 		goto trans_cancel;
 	ASSERT(ip->i_afp == NULL);
-	ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
+	ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone,
+				      GFP_KERNEL | __GFP_NOFAIL);
 	ip->i_afp->if_flags = XFS_IFEXTENTS;
 	logflags = 0;
 	switch (ip->i_d.di_format) {
fs/xfs/libxfs/xfs_bmap_btree.c +1 -1
index ffe608d2a2d9..77fe4ae671e5 100644
@@ -552,7 +552,7 @@ xfs_bmbt_init_cursor(
 	struct xfs_btree_cur	*cur;
 	ASSERT(whichfork != XFS_COW_FORK);
 
-	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
+	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
 
 	cur->bc_tp = tp;
 	cur->bc_mp = mp;
fs/xfs/libxfs/xfs_da_btree.c +1 -1
index 7ae82d91f776..10a96e64b2ec 100644
@@ -81,7 +81,7 @@ kmem_zone_t *xfs_da_state_zone;	/* anchor for state struct zone */
 xfs_da_state_t *
 xfs_da_state_alloc(void)
 {
-	return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
+	return kmem_cache_zalloc(xfs_da_state_zone, GFP_NOFS | __GFP_NOFAIL);
 }
 
 /*
fs/xfs/libxfs/xfs_ialloc_btree.c +1 -1
index b82992f795aa..5366a874b076 100644
@@ -413,7 +413,7 @@ xfs_inobt_init_cursor(
 	struct xfs_agi		*agi = XFS_BUF_TO_AGI(agbp);
 	struct xfs_btree_cur	*cur;
 
-	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
+	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
 
 	cur->bc_tp = tp;
 	cur->bc_mp = mp;
fs/xfs/libxfs/xfs_inode_fork.c +3 -3
index 296677958212..ceb322c7105e 100644
@@ -98,7 +98,7 @@ xfs_iformat_fork(
 		return 0;
 
 	ASSERT(ip->i_afp == NULL);
-	ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_NOFS);
+	ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone, GFP_NOFS | __GFP_NOFAIL);
 
 	switch (dip->di_aformat) {
 	case XFS_DINODE_FMT_LOCAL:
@@ -688,8 +688,8 @@ xfs_ifork_init_cow(
 	if (ip->i_cowfp)
 		return;
 
-	ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
-				       KM_NOFS);
+	ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
+				       GFP_NOFS | __GFP_NOFAIL);
 	ip->i_cowfp->if_flags = XFS_IFEXTENTS;
 	ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
 	ip->i_cnextents = 0;
fs/xfs/libxfs/xfs_refcount_btree.c +1 -1
index 38529dbacd55..bb86988780ea 100644
@@ -325,7 +325,7 @@ xfs_refcountbt_init_cursor(
 
 	ASSERT(agno != NULLAGNUMBER);
 	ASSERT(agno < mp->m_sb.sb_agcount);
-	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
+	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
 
 	cur->bc_tp = tp;
 	cur->bc_mp = mp;
fs/xfs/libxfs/xfs_rmap_btree.c +1 -1
index fc78efa52c94..8d84dd98e8d3 100644
@@ -461,7 +461,7 @@ xfs_rmapbt_init_cursor(
 	struct xfs_agf		*agf = XFS_BUF_TO_AGF(agbp);
 	struct xfs_btree_cur	*cur;
 
-	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
+	cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
 	cur->bc_tp = tp;
 	cur->bc_mp = mp;
 	/* Overlapping btree; 2 keys per pointer. */
fs/xfs/xfs_bmap_item.c +2 -2
index a89e10519f05..cda582d4bde0 100644
@@ -141,7 +141,7 @@ xfs_bui_init(
 {
 	struct xfs_bui_log_item		*buip;
 
-	buip = kmem_zone_zalloc(xfs_bui_zone, 0);
+	buip = kmem_cache_zalloc(xfs_bui_zone, GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
 	buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
@@ -218,7 +218,7 @@ xfs_trans_get_bud(
 {
 	struct xfs_bud_log_item		*budp;
 
-	budp = kmem_zone_zalloc(xfs_bud_zone, 0);
+	budp = kmem_cache_zalloc(xfs_bud_zone, GFP_KERNEL | __GFP_NOFAIL);
 	xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
 			  &xfs_bud_item_ops);
 	budp->bud_buip = buip;
fs/xfs/xfs_buf.c +1 -1
index 80ef2fc8bb77..0d2c41c6639d 100644
@@ -209,7 +209,7 @@ _xfs_buf_alloc(
 	int			error;
 	int			i;
 
-	bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
+	bp = kmem_cache_zalloc(xfs_buf_zone, GFP_NOFS | __GFP_NOFAIL);
 	if (unlikely(!bp))
 		return NULL;
 
fs/xfs/xfs_buf_item.c +1 -1
index 8dc3330f1797..dc39b2d1b351 100644
@@ -747,7 +747,7 @@ xfs_buf_item_init(
 		return 0;
 	}
 
-	bip = kmem_zone_zalloc(xfs_buf_item_zone, 0);
+	bip = kmem_cache_zalloc(xfs_buf_item_zone, GFP_KERNEL | __GFP_NOFAIL);
 	xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
 	bip->bli_buf = bp;
 
fs/xfs/xfs_dquot.c +1 -1
index a073281c3bd7..13c013918cd7 100644
@@ -440,7 +440,7 @@ xfs_dquot_alloc(
 {
 	struct xfs_dquot	*dqp;
 
-	dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
+	dqp = kmem_cache_zalloc(xfs_qm_dqzone, GFP_KERNEL | __GFP_NOFAIL);
 
 	dqp->dq_flags = type;
 	dqp->q_core.d_id = cpu_to_be32(id);
fs/xfs/xfs_extfree_item.c +4 -2
index 29b3a90aee91..c3b8804aa396 100644
@@ -165,7 +165,8 @@ xfs_efi_init(
 			((nextents - 1) * sizeof(xfs_extent_t)));
 		efip = kmem_zalloc(size, 0);
 	} else {
-		efip = kmem_zone_zalloc(xfs_efi_zone, 0);
+		efip = kmem_cache_zalloc(xfs_efi_zone,
+					 GFP_KERNEL | __GFP_NOFAIL);
 	}
 
 	xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
@@ -336,7 +337,8 @@ xfs_trans_get_efd(
 				(nextents - 1) * sizeof(struct xfs_extent),
 				0);
 	} else {
-		efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
+		efdp = kmem_cache_zalloc(xfs_efd_zone,
+					 GFP_KERNEL | __GFP_NOFAIL);
 	}
 
 	xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
fs/xfs/xfs_icreate_item.c +1 -1
index 490fee22b878..85bbf9dbe095 100644
@@ -89,7 +89,7 @@ xfs_icreate_log(
 {
 	struct xfs_icreate_item	*icp;
 
-	icp = kmem_zone_zalloc(xfs_icreate_zone, 0);
+	icp = kmem_cache_zalloc(xfs_icreate_zone, GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(tp->t_mountp, &icp->ic_item, XFS_LI_ICREATE,
 			  &xfs_icreate_item_ops);
fs/xfs/xfs_inode_item.c +2 -1
index c8461a5515f1..6f8a7226d41d 100644
@@ -652,7 +652,8 @@ xfs_inode_item_init(
 	struct xfs_inode_log_item *iip;
 
 	ASSERT(ip->i_itemp == NULL);
-	iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, 0);
+	iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_zone,
+					      GFP_KERNEL | __GFP_NOFAIL);
 
 	iip->ili_inode = ip;
 	xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
fs/xfs/xfs_log.c +4 -3
index e8349b0d7c51..9ed7869d879f 100644
@@ -454,7 +454,8 @@ xfs_log_reserve(
 	XFS_STATS_INC(mp, xs_try_logspace);
 
 	ASSERT(*ticp == NULL);
-	tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent, 0);
+	tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent,
+				GFP_KERNEL | __GFP_NOFAIL);
 	*ticp = tic;
 
 	xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
@@ -3587,12 +3588,12 @@ xlog_ticket_alloc(
 	int			cnt,
 	char			client,
 	bool			permanent,
-	xfs_km_flags_t		alloc_flags)
+	gfp_t			alloc_flags)
 {
 	struct xlog_ticket	*tic;
 	int			unit_res;
 
-	tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
+	tic = kmem_cache_zalloc(xfs_log_ticket_zone, alloc_flags);
 	if (!tic)
 		return NULL;
 
fs/xfs/xfs_log_cil.c +1 -1
index 23d70836a2b7..9953f2f040ab 100644
@@ -38,7 +38,7 @@ xlog_cil_ticket_alloc(
 	struct xlog_ticket *tic;
 
 	tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
-				KM_NOFS);
+				GFP_NOFS | __GFP_NOFAIL);
 
 	/*
 	 * set the current reservation to zero so we know to steal the basic
fs/xfs/xfs_log_priv.h +1 -1
index c47aa2ca6dc7..54c95fee9dc4 100644
@@ -427,7 +427,7 @@ xlog_ticket_alloc(
 	int		count,
 	char		client,
 	bool		permanent,
-	xfs_km_flags_t	alloc_flags);
+	gfp_t		alloc_flags);
 
 
 static inline void
fs/xfs/xfs_refcount_item.c +3 -2
index 0ac598f55339..76b39f2a0260 100644
@@ -146,7 +146,8 @@ xfs_cui_init(
 		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
 				0);
 	else
-		cuip = kmem_zone_zalloc(xfs_cui_zone, 0);
+		cuip = kmem_cache_zalloc(xfs_cui_zone,
+					 GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
 	cuip->cui_format.cui_nextents = nextents;
@@ -223,7 +224,7 @@ xfs_trans_get_cud(
 {
 	struct xfs_cud_log_item		*cudp;
 
-	cudp = kmem_zone_zalloc(xfs_cud_zone, 0);
+	cudp = kmem_cache_zalloc(xfs_cud_zone, GFP_KERNEL | __GFP_NOFAIL);
 	xfs_log_item_init(tp->t_mountp, &cudp->cud_item, XFS_LI_CUD,
 			  &xfs_cud_item_ops);
 	cudp->cud_cuip = cuip;
fs/xfs/xfs_rmap_item.c +3 -2
index a0a02d862ddd..6aeb6745d007 100644
@@ -144,7 +144,8 @@ xfs_rui_init(
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
 		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
 	else
-		ruip = kmem_zone_zalloc(xfs_rui_zone, 0);
+		ruip = kmem_cache_zalloc(xfs_rui_zone,
+					 GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
 	ruip->rui_format.rui_nextents = nextents;
@@ -246,7 +247,7 @@ xfs_trans_get_rud(
 {
 	struct xfs_rud_log_item		*rudp;
 
-	rudp = kmem_zone_zalloc(xfs_rud_zone, 0);
+	rudp = kmem_cache_zalloc(xfs_rud_zone, GFP_KERNEL | __GFP_NOFAIL);
 	xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
 			  &xfs_rud_item_ops);
 	rudp->rud_ruip = ruip;
fs/xfs/xfs_trans.c +2 -2
index 3b208f9a865c..29f34492d5f4 100644
@@ -90,7 +90,7 @@ xfs_trans_dup(
 
 	trace_xfs_trans_dup(tp, _RET_IP_);
 
-	ntp = kmem_zone_zalloc(xfs_trans_zone, 0);
+	ntp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL);
 
 	/*
 	 * Initialize the new transaction structure.
@@ -263,7 +263,7 @@ xfs_trans_alloc(
 	 * GFP_NOFS allocation context so that we avoid lockdep false positives
 	 * by doing GFP_KERNEL allocations inside sb_start_intwrite().
 	 */
-	tp = kmem_zone_zalloc(xfs_trans_zone, 0);
+	tp = kmem_cache_zalloc(xfs_trans_zone, GFP_KERNEL | __GFP_NOFAIL);
 	if (!(flags & XFS_TRANS_NO_WRITECOUNT))
 		sb_start_intwrite(mp->m_super);
 
fs/xfs/xfs_trans_dquot.c +2 -1
index ff1c326826d3..69e8f6d049aa 100644
@@ -863,7 +863,8 @@ STATIC void
 xfs_trans_alloc_dqinfo(
 	xfs_trans_t	*tp)
 {
-	tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, 0);
+	tp->t_dqinfo = kmem_cache_zalloc(xfs_qm_dqtrxzone,
+					 GFP_KERNEL | __GFP_NOFAIL);
 }
 
 void
Signature · 1 line
2.23.0
#2 lore raw

PATCH v2 2/5 xfs: Remove kmem_zone_alloc() wrapper

In reply to Carlos Maiolino

__GFP_NOFAIL can be used for an infinite retry + congestion_wait, so we can use kmem_cache_alloc() directly.

---
V2:
- Rephrase commit log to explain why it's ok to remove the retry loop from kmem_zone_alloc().

 fs/xfs/kmem.c             | 21 ---------------------
 fs/xfs/kmem.h             |  2 --
 fs/xfs/libxfs/xfs_alloc.c |  3 ++-
 fs/xfs/libxfs/xfs_bmap.c  |  3 ++-
 fs/xfs/xfs_icache.c       |  2 +-
 fs/xfs/xfs_trace.h        |  1 -
 6 files changed, 5 insertions(+), 27 deletions(-)
fs/xfs/kmem.c +0 -21
index 1da94237a8cf..2644fdaa0549 100644
@@ -115,24 +115,3 @@ kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
 		congestion_wait(BLK_RW_ASYNC, HZ/50);
 	} while (1);
 }
-
-void *
-kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
-{
-	int	retries = 0;
-	gfp_t	lflags = kmem_flags_convert(flags);
-	void	*ptr;
-
-	trace_kmem_zone_alloc(kmem_cache_size(zone), flags, _RET_IP_);
-	do {
-		ptr = kmem_cache_alloc(zone, lflags);
-		if (ptr || (flags & KM_MAYFAIL))
-			return ptr;
-		if (!(++retries % 100))
-			xfs_err(NULL,
-		"%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
-				current->comm, current->pid,
-				__func__, lflags);
-		congestion_wait(BLK_RW_ASYNC, HZ/50);
-	} while (1);
-}
fs/xfs/kmem.h +0 -2
index 3242de676808..7e4ad73771ce 100644
@@ -76,8 +76,6 @@ kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
 #define kmem_zone	kmem_cache
 #define kmem_zone_t	struct kmem_cache
 
-extern void *kmem_zone_alloc(kmem_zone_t *, xfs_km_flags_t);
-
 static inline struct page *
 kmem_to_page(void *addr)
 {
fs/xfs/libxfs/xfs_alloc.c +2 -1
index 675613c7bacb..42cae87bdd2d 100644
@@ -2351,7 +2351,8 @@ xfs_defer_agfl_block(
 	ASSERT(xfs_bmap_free_item_zone != NULL);
 	ASSERT(oinfo != NULL);
 
-	new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
+	new = kmem_cache_alloc(xfs_bmap_free_item_zone,
+			       GFP_KERNEL | __GFP_NOFAIL);
 	new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
 	new->xefi_blockcount = 1;
 	new->xefi_oinfo = *oinfo;
fs/xfs/libxfs/xfs_bmap.c +2 -1
index 9fbdca183465..37596e49b92e 100644
@@ -554,7 +554,8 @@ __xfs_bmap_add_free(
 #endif
 	ASSERT(xfs_bmap_free_item_zone != NULL);
 
-	new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
+	new = kmem_cache_alloc(xfs_bmap_free_item_zone,
+			       GFP_KERNEL | __GFP_NOFAIL);
 	new->xefi_startblock = bno;
 	new->xefi_blockcount = (xfs_extlen_t)len;
 	if (oinfo)
fs/xfs/xfs_icache.c +1 -1
index 950e8a51ec66..985f48e3795f 100644
@@ -40,7 +40,7 @@ xfs_inode_alloc(
 	 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
 	 * code up to do this anyway.
 	 */
-	ip = kmem_zone_alloc(xfs_inode_zone, 0);
+	ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL);
 	if (!ip)
 		return NULL;
 	if (inode_init_always(mp->m_super, VFS_I(ip))) {
fs/xfs/xfs_trace.h +0 -1
index c13bb3655e48..192f499ccd7e 100644
@@ -3571,7 +3571,6 @@ DEFINE_KMEM_EVENT(kmem_alloc);
 DEFINE_KMEM_EVENT(kmem_alloc_io);
 DEFINE_KMEM_EVENT(kmem_alloc_large);
 DEFINE_KMEM_EVENT(kmem_realloc);
-DEFINE_KMEM_EVENT(kmem_zone_alloc);
 
 #endif /* _TRACE_XFS_H */
 
Signature · 1 line
2.23.0
Reviewed-by on v2 2/5
#3 lore raw

Re: [PATCH V2 2/5] xfs: Remove kmem_zone_alloc() wrapper

Quoted 4 lines · Carlos Maiolino

__GFP_NOFAIL can be used for an infinite retry + congestion_wait, so we can use kmem_cache_alloc() directly.

Looks ok,

--D

Quoted 111 lines

---
V2:
- Rephrase commit log to explain why it's ok to remove the retry loop from kmem_zone_alloc().

 fs/xfs/kmem.c             | 21 ---------------------
 fs/xfs/kmem.h             |  2 --
 fs/xfs/libxfs/xfs_alloc.c |  3 ++-
 fs/xfs/libxfs/xfs_bmap.c  |  3 ++-
 fs/xfs/xfs_icache.c       |  2 +-
 fs/xfs/xfs_trace.h        |  1 -
 6 files changed, 5 insertions(+), 27 deletions(-)
fs/xfs/kmem.c +0 -21
index 1da94237a8cf..2644fdaa0549 100644
@@ -115,24 +115,3 @@ kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
 		congestion_wait(BLK_RW_ASYNC, HZ/50);
 	} while (1);
 }
-
-void *
-kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
-{
-	int	retries = 0;
-	gfp_t	lflags = kmem_flags_convert(flags);
-	void	*ptr;
-
-	trace_kmem_zone_alloc(kmem_cache_size(zone), flags, _RET_IP_);
-	do {
-		ptr = kmem_cache_alloc(zone, lflags);
-		if (ptr || (flags & KM_MAYFAIL))
-			return ptr;
-		if (!(++retries % 100))
-			xfs_err(NULL,
-		"%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
-				current->comm, current->pid,
-				__func__, lflags);
-		congestion_wait(BLK_RW_ASYNC, HZ/50);
-	} while (1);
-}
fs/xfs/kmem.h +0 -2
index 3242de676808..7e4ad73771ce 100644
@@ -76,8 +76,6 @@ kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
 #define kmem_zone	kmem_cache
 #define kmem_zone_t	struct kmem_cache
 
-extern void *kmem_zone_alloc(kmem_zone_t *, xfs_km_flags_t);
-
 static inline struct page *
 kmem_to_page(void *addr)
 {
fs/xfs/libxfs/xfs_alloc.c +2 -1
index 675613c7bacb..42cae87bdd2d 100644
@@ -2351,7 +2351,8 @@ xfs_defer_agfl_block(
 	ASSERT(xfs_bmap_free_item_zone != NULL);
 	ASSERT(oinfo != NULL);
 
-	new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
+	new = kmem_cache_alloc(xfs_bmap_free_item_zone,
+			       GFP_KERNEL | __GFP_NOFAIL);
 	new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
 	new->xefi_blockcount = 1;
 	new->xefi_oinfo = *oinfo;
fs/xfs/libxfs/xfs_bmap.c +2 -1
index 9fbdca183465..37596e49b92e 100644
@@ -554,7 +554,8 @@ __xfs_bmap_add_free(
 #endif
 	ASSERT(xfs_bmap_free_item_zone != NULL);
 
-	new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
+	new = kmem_cache_alloc(xfs_bmap_free_item_zone,
+			       GFP_KERNEL | __GFP_NOFAIL);
 	new->xefi_startblock = bno;
 	new->xefi_blockcount = (xfs_extlen_t)len;
 	if (oinfo)
fs/xfs/xfs_icache.c +1 -1
index 950e8a51ec66..985f48e3795f 100644
@@ -40,7 +40,7 @@ xfs_inode_alloc(
 	 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
 	 * code up to do this anyway.
 	 */
-	ip = kmem_zone_alloc(xfs_inode_zone, 0);
+	ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL);
 	if (!ip)
 		return NULL;
 	if (inode_init_always(mp->m_super, VFS_I(ip))) {
fs/xfs/xfs_trace.h +0 -1
index c13bb3655e48..192f499ccd7e 100644
@@ -3571,7 +3571,6 @@ DEFINE_KMEM_EVENT(kmem_alloc);
 DEFINE_KMEM_EVENT(kmem_alloc_io);
 DEFINE_KMEM_EVENT(kmem_alloc_large);
 DEFINE_KMEM_EVENT(kmem_realloc);
-DEFINE_KMEM_EVENT(kmem_zone_alloc);
 
 #endif /* _TRACE_XFS_H */
 
Signature · 1 line
2.23.0
#4 lore raw

PATCH 3/5 xfs: remove kmem_zalloc() wrapper

In reply to Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

---
V2:
- Fix comment on xfs_dir_lookup()

 fs/xfs/kmem.h                 |  6 ------
 fs/xfs/libxfs/xfs_attr_leaf.c |  3 ++-
 fs/xfs/libxfs/xfs_da_btree.c  | 10 ++++------
 fs/xfs/libxfs/xfs_dir2.c      | 27 +++++++++++++--------------
 fs/xfs/libxfs/xfs_iext_tree.c | 12 ++++++++----
 fs/xfs/scrub/agheader.c       |  4 ++--
 fs/xfs/scrub/fscounters.c     |  3 ++-
 fs/xfs/xfs_buf.c              |  6 +++---
 fs/xfs/xfs_buf_item.c         |  4 ++--
 fs/xfs/xfs_dquot_item.c       |  3 ++-
 fs/xfs/xfs_error.c            |  4 ++--
 fs/xfs/xfs_extent_busy.c      |  3 ++-
 fs/xfs/xfs_extfree_item.c     |  6 +++---
 fs/xfs/xfs_inode.c            |  2 +-
 fs/xfs/xfs_itable.c           |  8 ++++----
 fs/xfs/xfs_iwalk.c            |  3 ++-
 fs/xfs/xfs_log.c              |  5 +++--
 fs/xfs/xfs_log_cil.c          |  6 +++---
 fs/xfs/xfs_log_recover.c      | 12 ++++++------
 fs/xfs/xfs_mount.c            |  3 ++-
 fs/xfs/xfs_mru_cache.c        |  5 +++--
 fs/xfs/xfs_qm.c               |  3 ++-
 fs/xfs/xfs_refcount_item.c    |  4 ++--
 fs/xfs/xfs_rmap_item.c        |  3 ++-
 fs/xfs/xfs_trans_ail.c        |  3 ++-
 25 files changed, 77 insertions(+), 71 deletions(-)
fs/xfs/kmem.h +0 -6
index 7e4ad73771ce..b9ee67fa747b 100644
@@ -57,12 +57,6 @@ extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
 extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t);
 
-static inline void *
-kmem_zalloc(size_t size, xfs_km_flags_t flags)
-{
-	return kmem_alloc(size, flags | KM_ZERO);
-}
-
 static inline void *
 kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
 {
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);
 
 		/*
 		 * Copy the header into the temp leaf so that all the stuff
fs/xfs/libxfs/xfs_da_btree.c +4 -6
index 10a96e64b2ec..29c25d1b3b76 100644
@@ -2534,10 +2534,8 @@ xfs_buf_map_from_irec(
 	ASSERT(nirecs >= 1);
 
 	if (nirecs > 1) {
-		map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
-				  KM_NOFS);
-		if (!map)
-			return -ENOMEM;
+		map = kzalloc(nirecs * sizeof(struct xfs_buf_map),
+			      GFP_NOFS | __GFP_NOFAIL);
 		*mapp = map;
 	}
 
@@ -2593,8 +2591,8 @@ xfs_dabuf_map(
 		 * Optimize the one-block case.
 		 */
 		if (nfsb != 1)
-			irecs = kmem_zalloc(sizeof(irec) * nfsb,
-					    KM_NOFS);
+			irecs = kzalloc(sizeof(irec) * nfsb,
+					GFP_NOFS | __GFP_NOFAIL);
 
 		nirecs = nfsb;
 		error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
fs/xfs/libxfs/xfs_dir2.c +13 -14
index efd7cec65259..c2deda036271 100644
@@ -104,10 +104,10 @@ xfs_da_mount(
 	ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
 	ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
 
-	mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
-				    KM_MAYFAIL);
-	mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
-				     KM_MAYFAIL);
+	mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry),
+				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry),
+				 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!mp->m_dir_geo || !mp->m_attr_geo) {
 		kfree(mp->m_dir_geo);
 		kfree(mp->m_attr_geo);
@@ -234,7 +234,7 @@ xfs_dir_init(
 	if (error)
 		return error;
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -271,7 +271,7 @@ xfs_dir_createname(
 		XFS_STATS_INC(dp->i_mount, xs_dir_create);
 	}
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -363,14 +363,13 @@ xfs_dir_lookup(
 	XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
 
 	/*
-	 * We need to use KM_NOFS here so that lockdep will not throw false
+	 * We need to use GFP_NOFS here so that lockdep will not throw false
 	 * positive deadlock warnings on a non-transactional lookup path. It is
-	 * safe to recurse into inode recalim in that case, but lockdep can't
-	 * easily be taught about it. Hence KM_NOFS avoids having to add more
-	 * lockdep Doing this avoids having to add a bunch of lockdep class
-	 * annotations into the reclaim path for the ilock.
+	 * safe to recurse into inode reclaim in that case, but lockdep can't
+	 * easily be taught about it. Hence GFP_NOFS avoids having to add more
+	 * lockdep class annotations into the reclaim path for the ilock.
 	 */
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	args->geo = dp->i_mount->m_dir_geo;
 	args->name = name->name;
 	args->namelen = name->len;
@@ -439,7 +438,7 @@ xfs_dir_removename(
 	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 	XFS_STATS_INC(dp->i_mount, xs_dir_remove);
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -500,7 +499,7 @@ xfs_dir_replace(
 	if (rval)
 		return rval;
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
fs/xfs/libxfs/xfs_iext_tree.c +8 -4
index a7ba30cd81da..e75e7c021187 100644
@@ -398,7 +398,8 @@ static void
 xfs_iext_grow(
 	struct xfs_ifork	*ifp)
 {
-	struct xfs_iext_node	*node = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_node	*node = kzalloc(NODE_SIZE,
+						GFP_NOFS | __GFP_NOFAIL);
 	int			i;
 
 	if (ifp->if_height == 1) {
@@ -454,7 +455,8 @@ xfs_iext_split_node(
 	int			*nr_entries)
 {
 	struct xfs_iext_node	*node = *nodep;
-	struct xfs_iext_node	*new = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_node	*new = kzalloc(NODE_SIZE,
+					       GFP_NOFS | __GFP_NOFAIL);
 	const int		nr_move = KEYS_PER_NODE / 2;
 	int			nr_keep = nr_move + (KEYS_PER_NODE & 1);
 	int			i = 0;
@@ -542,7 +544,8 @@ xfs_iext_split_leaf(
 	int			*nr_entries)
 {
 	struct xfs_iext_leaf	*leaf = cur->leaf;
-	struct xfs_iext_leaf	*new = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_leaf	*new = kzalloc(NODE_SIZE,
+					       GFP_NOFS | __GFP_NOFAIL);
 	const int		nr_move = RECS_PER_LEAF / 2;
 	int			nr_keep = nr_move + (RECS_PER_LEAF & 1);
 	int			i;
@@ -583,7 +586,8 @@ xfs_iext_alloc_root(
 {
 	ASSERT(ifp->if_bytes == 0);
 
-	ifp->if_u1.if_root = kmem_zalloc(sizeof(struct xfs_iext_rec), KM_NOFS);
+	ifp->if_u1.if_root = kzalloc(sizeof(struct xfs_iext_rec),
+				     GFP_NOFS | __GFP_NOFAIL);
 	ifp->if_height = 1;
 
 	/* now that we have a node step into it */
fs/xfs/scrub/agheader.c +2 -2
index 6f7126f6d25c..5533c0d38333 100644
@@ -720,8 +720,8 @@ xchk_agfl(
 	memset(&sai, 0, sizeof(sai));
 	sai.sc = sc;
 	sai.sz_entries = agflcount;
-	sai.entries = kmem_zalloc(sizeof(xfs_agblock_t) * agflcount,
-			KM_MAYFAIL);
+	sai.entries = kzalloc(sizeof(xfs_agblock_t) * agflcount,
+			      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!sai.entries) {
 		error = -ENOMEM;
 		goto out;
fs/xfs/scrub/fscounters.c +2 -1
index 7251c66a82c9..bb036c5a6f21 100644
@@ -125,7 +125,8 @@ xchk_setup_fscounters(
 	struct xchk_fscounters	*fsc;
 	int			error;
 
-	sc->buf = kmem_zalloc(sizeof(struct xchk_fscounters), 0);
+	sc->buf = kzalloc(sizeof(struct xchk_fscounters),
+			  GFP_KERNEL | __GFP_NOFAIL);
 	if (!sc->buf)
 		return -ENOMEM;
 	fsc = sc->buf;
fs/xfs/xfs_buf.c +3 -3
index 0d2c41c6639d..c70122fbc2a8 100644
@@ -178,8 +178,8 @@ xfs_buf_get_maps(
 		return 0;
 	}
 
-	bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
-				KM_NOFS);
+	bp->b_maps = kzalloc(map_count * sizeof(struct xfs_buf_map),
+			     GFP_NOFS | __GFP_NOFAIL);
 	if (!bp->b_maps)
 		return -ENOMEM;
 	return 0;
@@ -1749,7 +1749,7 @@ xfs_alloc_buftarg(
 {
 	xfs_buftarg_t		*btp;
 
-	btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
+	btp = kzalloc(sizeof(*btp), GFP_NOFS | __GFP_NOFAIL);
 
 	btp->bt_mount = mp;
 	btp->bt_dev =  bdev->bd_dev;
fs/xfs/xfs_buf_item.c +2 -2
index dc39b2d1b351..4fea8e5e70fb 100644
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(mp, &qf->qql_item, XFS_LI_QUOTAOFF, start ?
 			&xfs_qm_qoffend_logitem_ops : &xfs_qm_qoff_logitem_ops);
fs/xfs/xfs_error.c +2 -2
index 4c0883380d7c..0e0ef2e15e2e 100644
@@ -212,8 +212,8 @@ int
 xfs_errortag_init(
 	struct xfs_mount	*mp)
 {
-	mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
-			KM_MAYFAIL);
+	mp->m_errortag = kzalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
+				 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!mp->m_errortag)
 		return -ENOMEM;
 
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +3 -3
index c3b8804aa396..872312029957 100644
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_inode.c +1 -1
index e1121ed7cbb5..297b2a73f285 100644
@@ -2024,7 +2024,7 @@ xfs_iunlink_add_backref(
 	if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
 		return 0;
 
-	iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
+	iu = kzalloc(sizeof(*iu), GFP_NOFS | __GFP_NOFAIL);
 	iu->iu_agino = prev_agino;
 	iu->iu_next_unlinked = this_agino;
 
fs/xfs/xfs_itable.c +4 -4
index 36bf47f11117..ec5d590469fc 100644
@@ -168,8 +168,8 @@ xfs_bulkstat_one(
 
 	ASSERT(breq->icount == 1);
 
-	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
-			KM_MAYFAIL);
+	bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
+			 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bc.buf)
 		return -ENOMEM;
 
@@ -242,8 +242,8 @@ xfs_bulkstat(
 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
 		return 0;
 
-	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
-			KM_MAYFAIL);
+	bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
+			 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bc.buf)
 		return -ENOMEM;
 
fs/xfs/xfs_iwalk.c +2 -1
index 67e98f9023d2..e6006423e140 100644
@@ -616,7 +616,8 @@ xfs_iwalk_threaded(
 		if (xfs_pwork_ctl_want_abort(&pctl))
 			break;
 
-		iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
+		iwag = kzalloc(sizeof(struct xfs_iwalk_ag),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		iwag->mp = mp;
 		iwag->iwalk_fn = iwalk_fn;
 		iwag->data = data;
fs/xfs/xfs_log.c +3 -2
index 9ed7869d879f..152c87865241 100644
@@ -1412,7 +1412,7 @@ xlog_alloc_log(
 	int			error = -ENOMEM;
 	uint			log2_size = 0;
 
-	log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
+	log = kzalloc(sizeof(struct xlog), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!log) {
 		xfs_warn(mp, "Log allocation failed: No memory!");
 		goto out;
@@ -1482,7 +1482,8 @@ xlog_alloc_log(
 		size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
 				sizeof(struct bio_vec);
 
-		iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
+		iclog = kzalloc(sizeof(*iclog) + bvec_size,
+				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!iclog)
 			goto out_free_iclog;
 
fs/xfs/xfs_log_cil.c +3 -3
index 9953f2f040ab..de5c892c72b0 100644
@@ -660,7 +660,7 @@ xlog_cil_push(
 	if (!cil)
 		return 0;
 
-	new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_NOFS);
+	new_ctx = kzalloc(sizeof(*new_ctx), GFP_NOFS | __GFP_NOFAIL);
 	new_ctx->ticket = xlog_cil_ticket_alloc(log);
 
 	down_write(&cil->xc_ctx_lock);
@@ -1179,11 +1179,11 @@ xlog_cil_init(
 	struct xfs_cil	*cil;
 	struct xfs_cil_ctx *ctx;
 
-	cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
+	cil = kzalloc(sizeof(*cil), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!cil)
 		return -ENOMEM;
 
-	ctx = kmem_zalloc(sizeof(*ctx), KM_MAYFAIL);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!ctx) {
 		kfree(cil);
 		return -ENOMEM;
fs/xfs/xfs_log_recover.c +6 -6
index 4167e1326f62..cb02deb5dedc 100644
@@ -4171,7 +4171,7 @@ xlog_recover_add_item(
 {
 	xlog_recover_item_t	*item;
 
-	item = kmem_zalloc(sizeof(xlog_recover_item_t), 0);
+	item = kzalloc(sizeof(xlog_recover_item_t), GFP_KERNEL | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&item->ri_list);
 	list_add_tail(&item->ri_list, head);
 }
@@ -4298,8 +4298,8 @@ xlog_recover_add_to_trans(
 
 		item->ri_total = in_f->ilf_size;
 		item->ri_buf =
-			kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
-				    0);
+			kzalloc(item->ri_total * sizeof(xfs_log_iovec_t),
+				GFP_KERNEL | __GFP_NOFAIL);
 	}
 
 	if (item->ri_total <= item->ri_cnt) {
@@ -4442,7 +4442,7 @@ xlog_recover_ophdr_to_trans(
 	 * This is a new transaction so allocate a new recovery container to
 	 * hold the recovery ops that will follow.
 	 */
-	trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
+	trans = kzalloc(sizeof(struct xlog_recover), GFP_KERNEL | __GFP_NOFAIL);
 	trans->r_log_tid = tid;
 	trans->r_lsn = be64_to_cpu(rhead->h_lsn);
 	INIT_LIST_HEAD(&trans->r_itemq);
@@ -5561,9 +5561,9 @@ xlog_do_log_recovery(
 	 * First do a pass to find all of the cancelled buf log items.
 	 * Store them in the buf_cancel_table for use in the second pass.
 	 */
-	log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
+	log->l_buf_cancel_table = kzalloc(XLOG_BC_TABLE_SIZE *
 						 sizeof(struct list_head),
-						 0);
+						 GFP_KERNEL | __GFP_NOFAIL);
 	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
 		INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
 
fs/xfs/xfs_mount.c +2 -1
index 53ddb058b11a..c352567c8ef5 100644
@@ -194,7 +194,8 @@ xfs_initialize_perag(
 			continue;
 		}
 
-		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
+		pag = kzalloc(sizeof(*pag),
+			      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!pag)
 			goto out_unwind_new_pags;
 		pag->pag_agno = index;
fs/xfs/xfs_mru_cache.c +3 -2
index 6ef0a71d7681..f24014759c57 100644
@@ -333,12 +333,13 @@ xfs_mru_cache_create(
 	if (!(grp_time = msecs_to_jiffies(lifetime_ms) / grp_count))
 		return -EINVAL;
 
-	if (!(mru = kmem_zalloc(sizeof(*mru), 0)))
+	if (!(mru = kzalloc(sizeof(*mru), GFP_KERNEL | __GFP_NOFAIL)))
 		return -ENOMEM;
 
 	/* An extra list is needed to avoid reaping up to a grp_time early. */
 	mru->grp_count = grp_count + 1;
-	mru->lists = kmem_zalloc(mru->grp_count * sizeof(*mru->lists), 0);
+	mru->lists = kzalloc(mru->grp_count * sizeof(*mru->lists),
+			     GFP_KERNEL | __GFP_NOFAIL);
 
 	if (!mru->lists) {
 		err = -ENOMEM;
fs/xfs/xfs_qm.c +2 -1
index 06c92dc61a03..a2664afa10c3 100644
@@ -643,7 +643,8 @@ xfs_qm_init_quotainfo(
 
 	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
 
-	qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), 0);
+	qinf = mp->m_quotainfo = kzalloc(sizeof(xfs_quotainfo_t),
+					 GFP_KERNEL | __GFP_NOFAIL);
 
 	error = list_lru_init(&qinf->qi_lru);
 	if (error)
fs/xfs/xfs_refcount_item.c +2 -2
index 76b39f2a0260..7ec70a5f1cb0 100644
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_rmap_item.c +2 -1
index 6aeb6745d007..82d822885996 100644
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_trans_ail.c +2 -1
index 589918d11041..2e3515df1bb5 100644
@@ -824,7 +824,8 @@ xfs_trans_ail_init(
 {
 	struct xfs_ail	*ailp;
 
-	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
+	ailp = kzalloc(sizeof(struct xfs_ail),
+		       GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!ailp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
Reviewed-by on v1 3/5
#5 lore raw

Re: [PATCH 3/5] xfs: remove kmem_zalloc() wrapper

Quoted 8 lines · Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

Looks ok,

--D

Quoted 587 lines

---
V2:
- Fix comment on xfs_dir_lookup()

 fs/xfs/kmem.h                 |  6 ------
 fs/xfs/libxfs/xfs_attr_leaf.c |  3 ++-
 fs/xfs/libxfs/xfs_da_btree.c  | 10 ++++------
 fs/xfs/libxfs/xfs_dir2.c      | 27 +++++++++++++--------------
 fs/xfs/libxfs/xfs_iext_tree.c | 12 ++++++++----
 fs/xfs/scrub/agheader.c       |  4 ++--
 fs/xfs/scrub/fscounters.c     |  3 ++-
 fs/xfs/xfs_buf.c              |  6 +++---
 fs/xfs/xfs_buf_item.c         |  4 ++--
 fs/xfs/xfs_dquot_item.c       |  3 ++-
 fs/xfs/xfs_error.c            |  4 ++--
 fs/xfs/xfs_extent_busy.c      |  3 ++-
 fs/xfs/xfs_extfree_item.c     |  6 +++---
 fs/xfs/xfs_inode.c            |  2 +-
 fs/xfs/xfs_itable.c           |  8 ++++----
 fs/xfs/xfs_iwalk.c            |  3 ++-
 fs/xfs/xfs_log.c              |  5 +++--
 fs/xfs/xfs_log_cil.c          |  6 +++---
 fs/xfs/xfs_log_recover.c      | 12 ++++++------
 fs/xfs/xfs_mount.c            |  3 ++-
 fs/xfs/xfs_mru_cache.c        |  5 +++--
 fs/xfs/xfs_qm.c               |  3 ++-
 fs/xfs/xfs_refcount_item.c    |  4 ++--
 fs/xfs/xfs_rmap_item.c        |  3 ++-
 fs/xfs/xfs_trans_ail.c        |  3 ++-
 25 files changed, 77 insertions(+), 71 deletions(-)
fs/xfs/kmem.h +0 -6
index 7e4ad73771ce..b9ee67fa747b 100644
@@ -57,12 +57,6 @@ extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
 extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t);
 
-static inline void *
-kmem_zalloc(size_t size, xfs_km_flags_t flags)
-{
-	return kmem_alloc(size, flags | KM_ZERO);
-}
-
 static inline void *
 kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
 {
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);
 
 		/*
 		 * Copy the header into the temp leaf so that all the stuff
fs/xfs/libxfs/xfs_da_btree.c +4 -6
index 10a96e64b2ec..29c25d1b3b76 100644
@@ -2534,10 +2534,8 @@ xfs_buf_map_from_irec(
 	ASSERT(nirecs >= 1);
 
 	if (nirecs > 1) {
-		map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
-				  KM_NOFS);
-		if (!map)
-			return -ENOMEM;
+		map = kzalloc(nirecs * sizeof(struct xfs_buf_map),
+			      GFP_NOFS | __GFP_NOFAIL);
 		*mapp = map;
 	}
 
@@ -2593,8 +2591,8 @@ xfs_dabuf_map(
 		 * Optimize the one-block case.
 		 */
 		if (nfsb != 1)
-			irecs = kmem_zalloc(sizeof(irec) * nfsb,
-					    KM_NOFS);
+			irecs = kzalloc(sizeof(irec) * nfsb,
+					GFP_NOFS | __GFP_NOFAIL);
 
 		nirecs = nfsb;
 		error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
fs/xfs/libxfs/xfs_dir2.c +13 -14
index efd7cec65259..c2deda036271 100644
@@ -104,10 +104,10 @@ xfs_da_mount(
 	ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
 	ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
 
-	mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
-				    KM_MAYFAIL);
-	mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
-				     KM_MAYFAIL);
+	mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry),
+				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry),
+				 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!mp->m_dir_geo || !mp->m_attr_geo) {
 		kfree(mp->m_dir_geo);
 		kfree(mp->m_attr_geo);
@@ -234,7 +234,7 @@ xfs_dir_init(
 	if (error)
 		return error;
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -271,7 +271,7 @@ xfs_dir_createname(
 		XFS_STATS_INC(dp->i_mount, xs_dir_create);
 	}
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -363,14 +363,13 @@ xfs_dir_lookup(
 	XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
 
 	/*
-	 * We need to use KM_NOFS here so that lockdep will not throw false
+	 * We need to use GFP_NOFS here so that lockdep will not throw false
 	 * positive deadlock warnings on a non-transactional lookup path. It is
-	 * safe to recurse into inode recalim in that case, but lockdep can't
-	 * easily be taught about it. Hence KM_NOFS avoids having to add more
-	 * lockdep Doing this avoids having to add a bunch of lockdep class
-	 * annotations into the reclaim path for the ilock.
+	 * safe to recurse into inode reclaim in that case, but lockdep can't
+	 * easily be taught about it. Hence GFP_NOFS avoids having to add more
+	 * lockdep class annotations into the reclaim path for the ilock.
 	 */
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	args->geo = dp->i_mount->m_dir_geo;
 	args->name = name->name;
 	args->namelen = name->len;
@@ -439,7 +438,7 @@ xfs_dir_removename(
 	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 	XFS_STATS_INC(dp->i_mount, xs_dir_remove);
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
@@ -500,7 +499,7 @@ xfs_dir_replace(
 	if (rval)
 		return rval;
 
-	args = kmem_zalloc(sizeof(*args), KM_NOFS);
+	args = kzalloc(sizeof(*args), GFP_NOFS | __GFP_NOFAIL);
 	if (!args)
 		return -ENOMEM;
 
fs/xfs/libxfs/xfs_iext_tree.c +8 -4
index a7ba30cd81da..e75e7c021187 100644
@@ -398,7 +398,8 @@ static void
 xfs_iext_grow(
 	struct xfs_ifork	*ifp)
 {
-	struct xfs_iext_node	*node = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_node	*node = kzalloc(NODE_SIZE,
+						GFP_NOFS | __GFP_NOFAIL);
 	int			i;
 
 	if (ifp->if_height == 1) {
@@ -454,7 +455,8 @@ xfs_iext_split_node(
 	int			*nr_entries)
 {
 	struct xfs_iext_node	*node = *nodep;
-	struct xfs_iext_node	*new = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_node	*new = kzalloc(NODE_SIZE,
+					       GFP_NOFS | __GFP_NOFAIL);
 	const int		nr_move = KEYS_PER_NODE / 2;
 	int			nr_keep = nr_move + (KEYS_PER_NODE & 1);
 	int			i = 0;
@@ -542,7 +544,8 @@ xfs_iext_split_leaf(
 	int			*nr_entries)
 {
 	struct xfs_iext_leaf	*leaf = cur->leaf;
-	struct xfs_iext_leaf	*new = kmem_zalloc(NODE_SIZE, KM_NOFS);
+	struct xfs_iext_leaf	*new = kzalloc(NODE_SIZE,
+					       GFP_NOFS | __GFP_NOFAIL);
 	const int		nr_move = RECS_PER_LEAF / 2;
 	int			nr_keep = nr_move + (RECS_PER_LEAF & 1);
 	int			i;
@@ -583,7 +586,8 @@ xfs_iext_alloc_root(
 {
 	ASSERT(ifp->if_bytes == 0);
 
-	ifp->if_u1.if_root = kmem_zalloc(sizeof(struct xfs_iext_rec), KM_NOFS);
+	ifp->if_u1.if_root = kzalloc(sizeof(struct xfs_iext_rec),
+				     GFP_NOFS | __GFP_NOFAIL);
 	ifp->if_height = 1;
 
 	/* now that we have a node step into it */
fs/xfs/scrub/agheader.c +2 -2
index 6f7126f6d25c..5533c0d38333 100644
@@ -720,8 +720,8 @@ xchk_agfl(
 	memset(&sai, 0, sizeof(sai));
 	sai.sc = sc;
 	sai.sz_entries = agflcount;
-	sai.entries = kmem_zalloc(sizeof(xfs_agblock_t) * agflcount,
-			KM_MAYFAIL);
+	sai.entries = kzalloc(sizeof(xfs_agblock_t) * agflcount,
+			      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!sai.entries) {
 		error = -ENOMEM;
 		goto out;
fs/xfs/scrub/fscounters.c +2 -1
index 7251c66a82c9..bb036c5a6f21 100644
@@ -125,7 +125,8 @@ xchk_setup_fscounters(
 	struct xchk_fscounters	*fsc;
 	int			error;
 
-	sc->buf = kmem_zalloc(sizeof(struct xchk_fscounters), 0);
+	sc->buf = kzalloc(sizeof(struct xchk_fscounters),
+			  GFP_KERNEL | __GFP_NOFAIL);
 	if (!sc->buf)
 		return -ENOMEM;
 	fsc = sc->buf;
fs/xfs/xfs_buf.c +3 -3
index 0d2c41c6639d..c70122fbc2a8 100644
@@ -178,8 +178,8 @@ xfs_buf_get_maps(
 		return 0;
 	}
 
-	bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
-				KM_NOFS);
+	bp->b_maps = kzalloc(map_count * sizeof(struct xfs_buf_map),
+			     GFP_NOFS | __GFP_NOFAIL);
 	if (!bp->b_maps)
 		return -ENOMEM;
 	return 0;
@@ -1749,7 +1749,7 @@ xfs_alloc_buftarg(
 {
 	xfs_buftarg_t		*btp;
 
-	btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
+	btp = kzalloc(sizeof(*btp), GFP_NOFS | __GFP_NOFAIL);
 
 	btp->bt_mount = mp;
 	btp->bt_dev =  bdev->bd_dev;
fs/xfs/xfs_buf_item.c +2 -2
index dc39b2d1b351..4fea8e5e70fb 100644
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);
 
 	xfs_log_item_init(mp, &qf->qql_item, XFS_LI_QUOTAOFF, start ?
 			&xfs_qm_qoffend_logitem_ops : &xfs_qm_qoff_logitem_ops);
fs/xfs/xfs_error.c +2 -2
index 4c0883380d7c..0e0ef2e15e2e 100644
@@ -212,8 +212,8 @@ int
 xfs_errortag_init(
 	struct xfs_mount	*mp)
 {
-	mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
-			KM_MAYFAIL);
+	mp->m_errortag = kzalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
+				 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!mp->m_errortag)
 		return -ENOMEM;
 
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +3 -3
index c3b8804aa396..872312029957 100644
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_inode.c +1 -1
index e1121ed7cbb5..297b2a73f285 100644
@@ -2024,7 +2024,7 @@ xfs_iunlink_add_backref(
 	if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
 		return 0;
 
-	iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
+	iu = kzalloc(sizeof(*iu), GFP_NOFS | __GFP_NOFAIL);
 	iu->iu_agino = prev_agino;
 	iu->iu_next_unlinked = this_agino;
 
fs/xfs/xfs_itable.c +4 -4
index 36bf47f11117..ec5d590469fc 100644
@@ -168,8 +168,8 @@ xfs_bulkstat_one(
 
 	ASSERT(breq->icount == 1);
 
-	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
-			KM_MAYFAIL);
+	bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
+			 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bc.buf)
 		return -ENOMEM;
 
@@ -242,8 +242,8 @@ xfs_bulkstat(
 	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
 		return 0;
 
-	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
-			KM_MAYFAIL);
+	bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
+			 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bc.buf)
 		return -ENOMEM;
 
fs/xfs/xfs_iwalk.c +2 -1
index 67e98f9023d2..e6006423e140 100644
@@ -616,7 +616,8 @@ xfs_iwalk_threaded(
 		if (xfs_pwork_ctl_want_abort(&pctl))
 			break;
 
-		iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
+		iwag = kzalloc(sizeof(struct xfs_iwalk_ag),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		iwag->mp = mp;
 		iwag->iwalk_fn = iwalk_fn;
 		iwag->data = data;
fs/xfs/xfs_log.c +3 -2
index 9ed7869d879f..152c87865241 100644
@@ -1412,7 +1412,7 @@ xlog_alloc_log(
 	int			error = -ENOMEM;
 	uint			log2_size = 0;
 
-	log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
+	log = kzalloc(sizeof(struct xlog), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!log) {
 		xfs_warn(mp, "Log allocation failed: No memory!");
 		goto out;
@@ -1482,7 +1482,8 @@ xlog_alloc_log(
 		size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
 				sizeof(struct bio_vec);
 
-		iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
+		iclog = kzalloc(sizeof(*iclog) + bvec_size,
+				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!iclog)
 			goto out_free_iclog;
 
fs/xfs/xfs_log_cil.c +3 -3
index 9953f2f040ab..de5c892c72b0 100644
@@ -660,7 +660,7 @@ xlog_cil_push(
 	if (!cil)
 		return 0;
 
-	new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_NOFS);
+	new_ctx = kzalloc(sizeof(*new_ctx), GFP_NOFS | __GFP_NOFAIL);
 	new_ctx->ticket = xlog_cil_ticket_alloc(log);
 
 	down_write(&cil->xc_ctx_lock);
@@ -1179,11 +1179,11 @@ xlog_cil_init(
 	struct xfs_cil	*cil;
 	struct xfs_cil_ctx *ctx;
 
-	cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
+	cil = kzalloc(sizeof(*cil), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!cil)
 		return -ENOMEM;
 
-	ctx = kmem_zalloc(sizeof(*ctx), KM_MAYFAIL);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!ctx) {
 		kfree(cil);
 		return -ENOMEM;
fs/xfs/xfs_log_recover.c +6 -6
index 4167e1326f62..cb02deb5dedc 100644
@@ -4171,7 +4171,7 @@ xlog_recover_add_item(
 {
 	xlog_recover_item_t	*item;
 
-	item = kmem_zalloc(sizeof(xlog_recover_item_t), 0);
+	item = kzalloc(sizeof(xlog_recover_item_t), GFP_KERNEL | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&item->ri_list);
 	list_add_tail(&item->ri_list, head);
 }
@@ -4298,8 +4298,8 @@ xlog_recover_add_to_trans(
 
 		item->ri_total = in_f->ilf_size;
 		item->ri_buf =
-			kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
-				    0);
+			kzalloc(item->ri_total * sizeof(xfs_log_iovec_t),
+				GFP_KERNEL | __GFP_NOFAIL);
 	}
 
 	if (item->ri_total <= item->ri_cnt) {
@@ -4442,7 +4442,7 @@ xlog_recover_ophdr_to_trans(
 	 * This is a new transaction so allocate a new recovery container to
 	 * hold the recovery ops that will follow.
 	 */
-	trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
+	trans = kzalloc(sizeof(struct xlog_recover), GFP_KERNEL | __GFP_NOFAIL);
 	trans->r_log_tid = tid;
 	trans->r_lsn = be64_to_cpu(rhead->h_lsn);
 	INIT_LIST_HEAD(&trans->r_itemq);
@@ -5561,9 +5561,9 @@ xlog_do_log_recovery(
 	 * First do a pass to find all of the cancelled buf log items.
 	 * Store them in the buf_cancel_table for use in the second pass.
 	 */
-	log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
+	log->l_buf_cancel_table = kzalloc(XLOG_BC_TABLE_SIZE *
 						 sizeof(struct list_head),
-						 0);
+						 GFP_KERNEL | __GFP_NOFAIL);
 	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
 		INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
 
fs/xfs/xfs_mount.c +2 -1
index 53ddb058b11a..c352567c8ef5 100644
@@ -194,7 +194,8 @@ xfs_initialize_perag(
 			continue;
 		}
 
-		pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
+		pag = kzalloc(sizeof(*pag),
+			      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!pag)
 			goto out_unwind_new_pags;
 		pag->pag_agno = index;
fs/xfs/xfs_mru_cache.c +3 -2
index 6ef0a71d7681..f24014759c57 100644
@@ -333,12 +333,13 @@ xfs_mru_cache_create(
 	if (!(grp_time = msecs_to_jiffies(lifetime_ms) / grp_count))
 		return -EINVAL;
 
-	if (!(mru = kmem_zalloc(sizeof(*mru), 0)))
+	if (!(mru = kzalloc(sizeof(*mru), GFP_KERNEL | __GFP_NOFAIL)))
 		return -ENOMEM;
 
 	/* An extra list is needed to avoid reaping up to a grp_time early. */
 	mru->grp_count = grp_count + 1;
-	mru->lists = kmem_zalloc(mru->grp_count * sizeof(*mru->lists), 0);
+	mru->lists = kzalloc(mru->grp_count * sizeof(*mru->lists),
+			     GFP_KERNEL | __GFP_NOFAIL);
 
 	if (!mru->lists) {
 		err = -ENOMEM;
fs/xfs/xfs_qm.c +2 -1
index 06c92dc61a03..a2664afa10c3 100644
@@ -643,7 +643,8 @@ xfs_qm_init_quotainfo(
 
 	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
 
-	qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), 0);
+	qinf = mp->m_quotainfo = kzalloc(sizeof(xfs_quotainfo_t),
+					 GFP_KERNEL | __GFP_NOFAIL);
 
 	error = list_lru_init(&qinf->qi_lru);
 	if (error)
fs/xfs/xfs_refcount_item.c +2 -2
index 76b39f2a0260..7ec70a5f1cb0 100644
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_rmap_item.c +2 -1
index 6aeb6745d007..82d822885996 100644
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);
fs/xfs/xfs_trans_ail.c +2 -1
index 589918d11041..2e3515df1bb5 100644
@@ -824,7 +824,8 @@ xfs_trans_ail_init(
 {
 	struct xfs_ail	*ailp;
 
-	ailp = kmem_zalloc(sizeof(struct xfs_ail), KM_MAYFAIL);
+	ailp = kzalloc(sizeof(struct xfs_ail),
+		       GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!ailp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
#6 lore raw

Re: [PATCH 3/5] xfs: remove kmem_zalloc() wrapper

In reply to Carlos Maiolino
Quoted 8 lines · Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

It looks good as a 1:1 translation, but I've noticed a few places we actually have the context wrong and have been saved by the fact tehy are called in transaction context (hence GFP_NOFS is enforced by task flags).

This can be fixed in a separate patch, I've noted the ones I think need changing below.

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr3_leaf_unbalance( · line 2257

Quoted 11 lines
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

Quoted 5 lines
fs/xfs/xfs_buf_item.c +0 -0
index dc39b2d1b351..4fea8e5e70fb 100644

fs/xfs/xfs_buf_item.c xfs_buf_item_get_format( · line 708

Quoted 10 lines
fs/xfs/xfs_buf_item.c +2 -2
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;

In a transaction, GFP_NOFS.

fs/xfs/xfs_dquot_item.c xfs_qm_qoff_logitem_init( · line 351

Quoted 11 lines
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

fs/xfs/xfs_extent_busy.c xfs_extent_busy_insert( · line 37

Quoted 11 lines
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);

transaction, GFP_NOFS.

Quoted 8 lines
fs/xfs/xfs_extent_busy.c +0 -0
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +0 -0
index c3b8804aa396..872312029957 100644

fs/xfs/xfs_extfree_item.c xfs_efi_init( · line 169

Quoted 8 lines
fs/xfs/xfs_extfree_item.c +1 -1
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both of these GFP_NOFS.

fs/xfs/xfs_extfree_item.c xfs_trans_get_efd( · line 341

Quoted 12 lines
fs/xfs/xfs_extfree_item.c +2 -2
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Same here.

Hmmm. I guess I better go look at the kmem_cache_[z]alloc() patches, too.

Quoted 5 lines
fs/xfs/xfs_refcount_item.c +0 -0
index 76b39f2a0260..7ec70a5f1cb0 100644

fs/xfs/xfs_refcount_item.c xfs_cui_init( · line 150

Quoted 10 lines
fs/xfs/xfs_refcount_item.c +2 -2
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Quoted 5 lines
fs/xfs/xfs_rmap_item.c +0 -0
index 6aeb6745d007..82d822885996 100644

fs/xfs/xfs_rmap_item.c xfs_rui_init( · line 149

Quoted 9 lines
fs/xfs/xfs_rmap_item.c +2 -1
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#7 lore raw

Re: [PATCH 3/5] xfs: remove kmem_zalloc() wrapper

Quoted 26 lines · Dave Chinner
Quoted 8 lines · Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

It looks good as a 1:1 translation, but I've noticed a few places we actually have the context wrong and have been saved by the fact tehy are called in transaction context (hence GFP_NOFS is enforced by task flags).

This can be fixed in a separate patch, I've noted the ones I think need changing below.

Quoted 8 lines
fs/xfs/libxfs/xfs_attr_leaf.c +0 -0
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
Quoted 5 lines

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr3_leaf_unbalance( · line 2257

Quoted 3 lines
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

As we're discussing on IRC, this is probably correct, but let's do a straight KM_ -> GFP_ conversion here, warts and all; and then do a separate series to sort out incorrect flag usage.

--D

Quoted 124 lines
Quoted 5 lines
fs/xfs/xfs_buf_item.c +0 -0
index dc39b2d1b351..4fea8e5e70fb 100644

fs/xfs/xfs_buf_item.c xfs_buf_item_get_format( · line 708

Quoted 10 lines
fs/xfs/xfs_buf_item.c +2 -2
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;

In a transaction, GFP_NOFS.

fs/xfs/xfs_dquot_item.c xfs_qm_qoff_logitem_init( · line 351

Quoted 11 lines
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

fs/xfs/xfs_extent_busy.c xfs_extent_busy_insert( · line 37

Quoted 11 lines
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);

transaction, GFP_NOFS.

Quoted 8 lines
fs/xfs/xfs_extent_busy.c +0 -0
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +0 -0
index c3b8804aa396..872312029957 100644

fs/xfs/xfs_extfree_item.c xfs_efi_init( · line 169

Quoted 8 lines
fs/xfs/xfs_extfree_item.c +1 -1
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both of these GFP_NOFS.

fs/xfs/xfs_extfree_item.c xfs_trans_get_efd( · line 341

Quoted 12 lines
fs/xfs/xfs_extfree_item.c +2 -2
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Same here.

Hmmm. I guess I better go look at the kmem_cache_[z]alloc() patches, too.

Quoted 5 lines
fs/xfs/xfs_refcount_item.c +0 -0
index 76b39f2a0260..7ec70a5f1cb0 100644

fs/xfs/xfs_refcount_item.c xfs_cui_init( · line 150

Quoted 10 lines
fs/xfs/xfs_refcount_item.c +2 -2
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Quoted 5 lines
fs/xfs/xfs_rmap_item.c +0 -0
index 6aeb6745d007..82d822885996 100644

fs/xfs/xfs_rmap_item.c xfs_rui_init( · line 149

Quoted 9 lines
fs/xfs/xfs_rmap_item.c +2 -1
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#8 lore raw

Re: [PATCH 3/5] xfs: remove kmem_zalloc() wrapper

Quoted 33 lines · Darrick J. Wong
Quoted 26 lines · Dave Chinner
Quoted 8 lines · Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

It looks good as a 1:1 translation, but I've noticed a few places we actually have the context wrong and have been saved by the fact tehy are called in transaction context (hence GFP_NOFS is enforced by task flags).

This can be fixed in a separate patch, I've noted the ones I think need changing below.

Quoted 8 lines
fs/xfs/libxfs/xfs_attr_leaf.c +0 -0
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
Quoted 5 lines

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr3_leaf_unbalance( · line 2257

Quoted 3 lines
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

Quoted 3 lines

As we're discussing on IRC, this is probably correct, but let's do a straight KM_ -> GFP_ conversion here, warts and all; and then do a separate series to sort out incorrect flag usage.

Sure, thanks guys, I can focus on that then after this series. Do you guys prefer me to 'finish' this series (i.e. removing KM_* flags), or fixing the wrong contexts before the next patches for KM_* -> GFP_* stuff?

Cheers.

Quoted 128 lines

--D

Quoted 124 lines
Quoted 5 lines
fs/xfs/xfs_buf_item.c +0 -0
index dc39b2d1b351..4fea8e5e70fb 100644

fs/xfs/xfs_buf_item.c xfs_buf_item_get_format( · line 708

Quoted 10 lines
fs/xfs/xfs_buf_item.c +2 -2
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;

In a transaction, GFP_NOFS.

fs/xfs/xfs_dquot_item.c xfs_qm_qoff_logitem_init( · line 351

Quoted 11 lines
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

fs/xfs/xfs_extent_busy.c xfs_extent_busy_insert( · line 37

Quoted 11 lines
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);

transaction, GFP_NOFS.

Quoted 8 lines
fs/xfs/xfs_extent_busy.c +0 -0
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +0 -0
index c3b8804aa396..872312029957 100644

fs/xfs/xfs_extfree_item.c xfs_efi_init( · line 169

Quoted 8 lines
fs/xfs/xfs_extfree_item.c +1 -1
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both of these GFP_NOFS.

fs/xfs/xfs_extfree_item.c xfs_trans_get_efd( · line 341

Quoted 12 lines
fs/xfs/xfs_extfree_item.c +2 -2
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Same here.

Hmmm. I guess I better go look at the kmem_cache_[z]alloc() patches, too.

Quoted 5 lines
fs/xfs/xfs_refcount_item.c +0 -0
index 76b39f2a0260..7ec70a5f1cb0 100644

fs/xfs/xfs_refcount_item.c xfs_cui_init( · line 150

Quoted 10 lines
fs/xfs/xfs_refcount_item.c +2 -2
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Quoted 5 lines
fs/xfs/xfs_rmap_item.c +0 -0
index 6aeb6745d007..82d822885996 100644

fs/xfs/xfs_rmap_item.c xfs_rui_init( · line 149

Quoted 9 lines
fs/xfs/xfs_rmap_item.c +2 -1
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
Signature · 1 line
Carlos
#9 lore raw

Re: [PATCH 3/5] xfs: remove kmem_zalloc() wrapper

Quoted 34 lines · Carlos Maiolino
Quoted 33 lines · Darrick J. Wong
Quoted 31 lines · Dave Chinner
Quoted 8 lines · Carlos Maiolino

Use kzalloc() directly

Special attention goes to function xfs_buf_map_from_irec(). Giving the fact we are not allowed to fail there, I removed the 'if (!map)' conditional from there, I'd just like somebody to double check if it's fine as I believe it is

It looks good as a 1:1 translation, but I've noticed a few places we actually have the context wrong and have been saved by the fact tehy are called in transaction context (hence GFP_NOFS is enforced by task flags).

This can be fixed in a separate patch, I've noted the ones I think need changing below.

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr3_leaf_unbalance( · line 2257

Quoted 11 lines
fs/xfs/libxfs/xfs_attr_leaf.c +2 -1
index 795b9b21b64d..67de68584224 100644
@@ -2253,7 +2253,8 @@ xfs_attr3_leaf_unbalance(
 		struct xfs_attr_leafblock *tmp_leaf;
 		struct xfs_attr3_icleaf_hdr tmphdr;
 
-		tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
+		tmp_leaf = kzalloc(state->args->geo->blksize,
+				   GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

Quoted 7 lines
Quoted 3 lines

As we're discussing on IRC, this is probably correct, but let's do a straight KM_ -> GFP_ conversion here, warts and all; and then do a separate series to sort out incorrect flag usage.

Sure, thanks guys, I can focus on that then after this series. Do you guys prefer me to 'finish' this series (i.e. removing KM_* flags), or fixing the wrong contexts before the next patches for KM_* -> GFP_* stuff?

Dunno, but it's all 5.6 material at this point, so you could possibly do both. I'm guessing that continuing the KM_ removal will be easier to review and therefore should go first.

--D

Quoted 134 lines

Cheers.

Quoted 128 lines

--D

Quoted 124 lines
Quoted 5 lines
fs/xfs/xfs_buf_item.c +0 -0
index dc39b2d1b351..4fea8e5e70fb 100644

fs/xfs/xfs_buf_item.c xfs_buf_item_get_format( · line 708

Quoted 10 lines
fs/xfs/xfs_buf_item.c +2 -2
@@ -701,8 +701,8 @@ xfs_buf_item_get_format(
 		return 0;
 	}
 
-	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
-				0);
+	bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
+				   GFP_KERNEL | __GFP_NOFAIL);
 	if (!bip->bli_formats)
 		return -ENOMEM;
 	return 0;

In a transaction, GFP_NOFS.

fs/xfs/xfs_dquot_item.c xfs_qm_qoff_logitem_init( · line 351

Quoted 11 lines
fs/xfs/xfs_dquot_item.c +2 -1
index 1b5e68ccef60..91bd47e8b832 100644
@@ -347,7 +347,8 @@ xfs_qm_qoff_logitem_init(
 {
 	struct xfs_qoff_logitem	*qf;
 
-	qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
+	qf = kzalloc(sizeof(struct xfs_qoff_logitem),
+		     GFP_KERNEL | __GFP_NOFAIL);

In a transaction, GFP_NOFS.

fs/xfs/xfs_extent_busy.c xfs_extent_busy_insert( · line 37

Quoted 11 lines
fs/xfs/xfs_extent_busy.c +2 -1
index 9f0b99c7b34a..0ce50b47fc28 100644
@@ -33,7 +33,8 @@ xfs_extent_busy_insert(
 	struct rb_node		**rbp;
 	struct rb_node		*parent = NULL;
 
-	new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
+	new = kzalloc(sizeof(struct xfs_extent_busy),
+		      GFP_KERNEL | __GFP_NOFAIL);

transaction, GFP_NOFS.

Quoted 8 lines
fs/xfs/xfs_extent_busy.c +0 -0
 	new->agno = agno;
 	new->bno = bno;
 	new->length = len;
fs/xfs/xfs_extfree_item.c +0 -0
index c3b8804aa396..872312029957 100644

fs/xfs/xfs_extfree_item.c xfs_efi_init( · line 169

Quoted 8 lines
fs/xfs/xfs_extfree_item.c +1 -1
@@ -163,7 +163,7 @@ xfs_efi_init(
 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
 		size = (uint)(sizeof(xfs_efi_log_item_t) +
 			((nextents - 1) * sizeof(xfs_extent_t)));
-		efip = kmem_zalloc(size, 0);
+		efip = kzalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efip = kmem_cache_zalloc(xfs_efi_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both of these GFP_NOFS.

fs/xfs/xfs_extfree_item.c xfs_trans_get_efd( · line 341

Quoted 12 lines
fs/xfs/xfs_extfree_item.c +2 -2
@@ -333,9 +333,9 @@ xfs_trans_get_efd(
 	ASSERT(nextents > 0);
 
 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
-		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
+		efdp = kzalloc(sizeof(struct xfs_efd_log_item) +
 				(nextents - 1) * sizeof(struct xfs_extent),
-				0);
+				GFP_KERNEL | __GFP_NOFAIL);
 	} else {
 		efdp = kmem_cache_zalloc(xfs_efd_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Same here.

Hmmm. I guess I better go look at the kmem_cache_[z]alloc() patches, too.

Quoted 5 lines
fs/xfs/xfs_refcount_item.c +0 -0
index 76b39f2a0260..7ec70a5f1cb0 100644

fs/xfs/xfs_refcount_item.c xfs_cui_init( · line 150

Quoted 10 lines
fs/xfs/xfs_refcount_item.c +2 -2
@@ -143,8 +143,8 @@ xfs_cui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
-		cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
-				0);
+		cuip = kzalloc(xfs_cui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		cuip = kmem_cache_zalloc(xfs_cui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Quoted 5 lines
fs/xfs/xfs_rmap_item.c +0 -0
index 6aeb6745d007..82d822885996 100644

fs/xfs/xfs_rmap_item.c xfs_rui_init( · line 149

Quoted 9 lines
fs/xfs/xfs_rmap_item.c +2 -1
@@ -142,7 +142,8 @@ xfs_rui_init(
 
 	ASSERT(nextents > 0);
 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
-		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
+		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
+			       GFP_KERNEL | __GFP_NOFAIL);
 	else
 		ruip = kmem_cache_zalloc(xfs_rui_zone,
 					 GFP_KERNEL | __GFP_NOFAIL);

Both GFP_NOFS.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
Signature · 1 line
Carlos
#10 lore raw

PATCH 4/5 xfs: Remove kmem_realloc

In reply to Carlos Maiolino

We can use krealloc() with __GFP_NOFAIL directly

---
V2:
- Fix small conflict in kmem.h due removal of kmem_free()
- Small comment update on xfs_iroot_realloc()

 fs/xfs/kmem.c                  | 22 ----------------------
 fs/xfs/kmem.h                  |  1 -
 fs/xfs/libxfs/xfs_iext_tree.c  |  2 +-
 fs/xfs/libxfs/xfs_inode_fork.c | 10 +++++-----
 fs/xfs/xfs_log_recover.c       |  2 +-
 fs/xfs/xfs_mount.c             |  4 ++--
 6 files changed, 9 insertions(+), 32 deletions(-)
fs/xfs/kmem.c +0 -22
index 2644fdaa0549..6e10e565632c 100644
@@ -93,25 +93,3 @@ kmem_alloc_large(size_t size, xfs_km_flags_t flags)
 		return ptr;
 	return __kmem_vmalloc(size, flags);
 }
-
-void *
-kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
-{
-	int	retries = 0;
-	gfp_t	lflags = kmem_flags_convert(flags);
-	void	*ptr;
-
-	trace_kmem_realloc(newsize, flags, _RET_IP_);
-
-	do {
-		ptr = krealloc(old, newsize, lflags);
-		if (ptr || (flags & KM_MAYFAIL))
-			return ptr;
-		if (!(++retries % 100))
-			xfs_err(NULL,
-	"%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
-				current->comm, current->pid,
-				newsize, __func__, lflags);
-		congestion_wait(BLK_RW_ASYNC, HZ/50);
-	} while (1);
-}
fs/xfs/kmem.h +0 -1
index b9ee67fa747b..a18c27c99721 100644
@@ -55,7 +55,6 @@ kmem_flags_convert(xfs_km_flags_t flags)
 extern void *kmem_alloc(size_t, xfs_km_flags_t);
 extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
-extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t);
 
 static inline void *
 kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
fs/xfs/libxfs/xfs_iext_tree.c +1 -1
index e75e7c021187..78c9f6c7a36a 100644
@@ -607,7 +607,7 @@ xfs_iext_realloc_root(
 	if (new_size / sizeof(struct xfs_iext_rec) == RECS_PER_LEAF)
 		new_size = NODE_SIZE;
 
-	new = kmem_realloc(ifp->if_u1.if_root, new_size, KM_NOFS);
+	new = krealloc(ifp->if_u1.if_root, new_size, GFP_NOFS | __GFP_NOFAIL);
 	memset(new + ifp->if_bytes, 0, new_size - ifp->if_bytes);
 	ifp->if_u1.if_root = new;
 	cur->leaf = new;
fs/xfs/libxfs/xfs_inode_fork.c +5 -5
index ceb322c7105e..82799dddf97d 100644
@@ -380,15 +380,15 @@ xfs_iroot_realloc(
 
 		/*
 		 * If there is already an existing if_broot, then we need
-		 * to realloc() it and shift the pointers to their new
+		 * to krealloc() it and shift the pointers to their new
 		 * location.  The records don't change location because
 		 * they are kept butted up against the btree block header.
 		 */
 		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
 		new_max = cur_max + rec_diff;
 		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
-		ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
-				KM_NOFS);
+		ifp->if_broot = krealloc(ifp->if_broot, new_size,
+				GFP_NOFS | __GFP_NOFAIL);
 		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 						     ifp->if_broot_bytes);
 		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
@@ -497,8 +497,8 @@ xfs_idata_realloc(
 	 * in size so that it can be logged and stay on word boundaries.
 	 * We enforce that here.
 	 */
-	ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
-			roundup(new_size, 4), KM_NOFS);
+	ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
+				      GFP_NOFS | __GFP_NOFAIL);
 	ifp->if_bytes = new_size;
 }
 
fs/xfs/xfs_log_recover.c +1 -1
index cb02deb5dedc..5423171e0b7d 100644
@@ -4211,7 +4211,7 @@ xlog_recover_add_to_cont_trans(
 	old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
 	old_len = item->ri_buf[item->ri_cnt-1].i_len;
 
-	ptr = kmem_realloc(old_ptr, len + old_len, 0);
+	ptr = krealloc(old_ptr, len + old_len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(&ptr[old_len], dp, len);
 	item->ri_buf[item->ri_cnt-1].i_len += len;
 	item->ri_buf[item->ri_cnt-1].i_addr = ptr;
fs/xfs/xfs_mount.c +2 -2
index c352567c8ef5..12a1cdf8e292 100644
@@ -80,9 +80,9 @@ xfs_uuid_mount(
 	}
 
 	if (hole < 0) {
-		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
+		xfs_uuid_table = krealloc(xfs_uuid_table,
 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
-			0);
+			GFP_KERNEL | __GFP_NOFAIL);
 		hole = xfs_uuid_table_size++;
 	}
 	xfs_uuid_table[hole] = *uuid;
Signature · 1 line
2.23.0
Reviewed-by on v1 4/5
#11 lore raw

Re: [PATCH 4/5] xfs: Remove kmem_realloc

Quoted 3 lines · Carlos Maiolino

We can use krealloc() with __GFP_NOFAIL directly

Looks ok,

--D

Quoted 134 lines

---
V2:
- Fix small conflict in kmem.h due removal of kmem_free()
- Small comment update on xfs_iroot_realloc()

 fs/xfs/kmem.c                  | 22 ----------------------
 fs/xfs/kmem.h                  |  1 -
 fs/xfs/libxfs/xfs_iext_tree.c  |  2 +-
 fs/xfs/libxfs/xfs_inode_fork.c | 10 +++++-----
 fs/xfs/xfs_log_recover.c       |  2 +-
 fs/xfs/xfs_mount.c             |  4 ++--
 6 files changed, 9 insertions(+), 32 deletions(-)
fs/xfs/kmem.c +0 -22
index 2644fdaa0549..6e10e565632c 100644
@@ -93,25 +93,3 @@ kmem_alloc_large(size_t size, xfs_km_flags_t flags)
 		return ptr;
 	return __kmem_vmalloc(size, flags);
 }
-
-void *
-kmem_realloc(const void *old, size_t newsize, xfs_km_flags_t flags)
-{
-	int	retries = 0;
-	gfp_t	lflags = kmem_flags_convert(flags);
-	void	*ptr;
-
-	trace_kmem_realloc(newsize, flags, _RET_IP_);
-
-	do {
-		ptr = krealloc(old, newsize, lflags);
-		if (ptr || (flags & KM_MAYFAIL))
-			return ptr;
-		if (!(++retries % 100))
-			xfs_err(NULL,
-	"%s(%u) possible memory allocation deadlock size %zu in %s (mode:0x%x)",
-				current->comm, current->pid,
-				newsize, __func__, lflags);
-		congestion_wait(BLK_RW_ASYNC, HZ/50);
-	} while (1);
-}
fs/xfs/kmem.h +0 -1
index b9ee67fa747b..a18c27c99721 100644
@@ -55,7 +55,6 @@ kmem_flags_convert(xfs_km_flags_t flags)
 extern void *kmem_alloc(size_t, xfs_km_flags_t);
 extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
-extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t);
 
 static inline void *
 kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
fs/xfs/libxfs/xfs_iext_tree.c +1 -1
index e75e7c021187..78c9f6c7a36a 100644
@@ -607,7 +607,7 @@ xfs_iext_realloc_root(
 	if (new_size / sizeof(struct xfs_iext_rec) == RECS_PER_LEAF)
 		new_size = NODE_SIZE;
 
-	new = kmem_realloc(ifp->if_u1.if_root, new_size, KM_NOFS);
+	new = krealloc(ifp->if_u1.if_root, new_size, GFP_NOFS | __GFP_NOFAIL);
 	memset(new + ifp->if_bytes, 0, new_size - ifp->if_bytes);
 	ifp->if_u1.if_root = new;
 	cur->leaf = new;
fs/xfs/libxfs/xfs_inode_fork.c +5 -5
index ceb322c7105e..82799dddf97d 100644
@@ -380,15 +380,15 @@ xfs_iroot_realloc(
 
 		/*
 		 * If there is already an existing if_broot, then we need
-		 * to realloc() it and shift the pointers to their new
+		 * to krealloc() it and shift the pointers to their new
 		 * location.  The records don't change location because
 		 * they are kept butted up against the btree block header.
 		 */
 		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
 		new_max = cur_max + rec_diff;
 		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
-		ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
-				KM_NOFS);
+		ifp->if_broot = krealloc(ifp->if_broot, new_size,
+				GFP_NOFS | __GFP_NOFAIL);
 		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 						     ifp->if_broot_bytes);
 		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
@@ -497,8 +497,8 @@ xfs_idata_realloc(
 	 * in size so that it can be logged and stay on word boundaries.
 	 * We enforce that here.
 	 */
-	ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
-			roundup(new_size, 4), KM_NOFS);
+	ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
+				      GFP_NOFS | __GFP_NOFAIL);
 	ifp->if_bytes = new_size;
 }
 
fs/xfs/xfs_log_recover.c +1 -1
index cb02deb5dedc..5423171e0b7d 100644
@@ -4211,7 +4211,7 @@ xlog_recover_add_to_cont_trans(
 	old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
 	old_len = item->ri_buf[item->ri_cnt-1].i_len;
 
-	ptr = kmem_realloc(old_ptr, len + old_len, 0);
+	ptr = krealloc(old_ptr, len + old_len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(&ptr[old_len], dp, len);
 	item->ri_buf[item->ri_cnt-1].i_len += len;
 	item->ri_buf[item->ri_cnt-1].i_addr = ptr;
fs/xfs/xfs_mount.c +2 -2
index c352567c8ef5..12a1cdf8e292 100644
@@ -80,9 +80,9 @@ xfs_uuid_mount(
 	}
 
 	if (hole < 0) {
-		xfs_uuid_table = kmem_realloc(xfs_uuid_table,
+		xfs_uuid_table = krealloc(xfs_uuid_table,
 			(xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
-			0);
+			GFP_KERNEL | __GFP_NOFAIL);
 		hole = xfs_uuid_table_size++;
 	}
 	xfs_uuid_table[hole] = *uuid;
Signature · 1 line
2.23.0
#12 lore raw

PATCH 5/5 xfs: Convert kmem_alloc() users

In reply to Carlos Maiolino

Use kmalloc() directly.

There is no logic change on kmem_alloc() since it's being removed soon, but for now, kmem_alloc_io() and kmem_alloc_large() still have use for kmem_alloc() due their fallback to vmalloc() and also the alignment check, so we can't completely remove it here.
But, there is no need to export kmem_alloc() to the whole XFS driver anymore, so, convert kmem_alloc() into a static, local function __kmem_alloc().

---
V2:
- Rephrase commit log

 fs/xfs/kmem.c                  |  8 ++++----
 fs/xfs/kmem.h                  |  1 -
 fs/xfs/libxfs/xfs_attr_leaf.c  |  6 +++---
 fs/xfs/libxfs/xfs_bmap.c       |  2 +-
 fs/xfs/libxfs/xfs_da_btree.c   |  4 +++-
 fs/xfs/libxfs/xfs_defer.c      |  4 ++--
 fs/xfs/libxfs/xfs_dir2.c       |  2 +-
 fs/xfs/libxfs/xfs_dir2_block.c |  2 +-
 fs/xfs/libxfs/xfs_dir2_sf.c    |  8 ++++----
 fs/xfs/libxfs/xfs_inode_fork.c | 10 ++++++----
 fs/xfs/libxfs/xfs_refcount.c   |  9 +++++----
 fs/xfs/libxfs/xfs_rmap.c       |  2 +-
 fs/xfs/scrub/bitmap.c          |  7 ++++---
 fs/xfs/scrub/btree.c           |  4 ++--
 fs/xfs/scrub/refcount.c        |  4 ++--
 fs/xfs/xfs_attr_inactive.c     |  2 +-
 fs/xfs/xfs_attr_list.c         |  2 +-
 fs/xfs/xfs_buf.c               |  5 +++--
 fs/xfs/xfs_filestream.c        |  2 +-
 fs/xfs/xfs_inode.c             |  2 +-
 fs/xfs/xfs_iwalk.c             |  2 +-
 fs/xfs/xfs_log_recover.c       |  7 ++++---
 fs/xfs/xfs_qm.c                |  3 ++-
 fs/xfs/xfs_rtalloc.c           |  2 +-
 fs/xfs/xfs_super.c             |  2 +-
 25 files changed, 55 insertions(+), 47 deletions(-)
fs/xfs/kmem.c +4 -4
index 6e10e565632c..79467813d810 100644
@@ -8,8 +8,8 @@
 #include "xfs_message.h"
 #include "xfs_trace.h"
 
-void *
-kmem_alloc(size_t size, xfs_km_flags_t flags)
+static void *
+__kmem_alloc(size_t size, xfs_km_flags_t flags)
 {
 	int	retries = 0;
 	gfp_t	lflags = kmem_flags_convert(flags);
@@ -72,7 +72,7 @@ kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags)
 	if (WARN_ON_ONCE(align_mask >= PAGE_SIZE))
 		align_mask = PAGE_SIZE - 1;
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr) {
 		if (!((uintptr_t)ptr & align_mask))
 			return ptr;
@@ -88,7 +88,7 @@ kmem_alloc_large(size_t size, xfs_km_flags_t flags)
 
 	trace_kmem_alloc_large(size, flags, _RET_IP_);
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr)
 		return ptr;
 	return __kmem_vmalloc(size, flags);
fs/xfs/kmem.h +0 -1
index a18c27c99721..78a54839430a 100644
@@ -52,7 +52,6 @@ kmem_flags_convert(xfs_km_flags_t flags)
 	return lflags;
 }
 
-extern void *kmem_alloc(size_t, xfs_km_flags_t);
 extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
 
fs/xfs/libxfs/xfs_attr_leaf.c +3 -3
index 67de68584224..807950eca17a 100644
@@ -885,7 +885,7 @@ xfs_attr_shortform_to_leaf(
 	ifp = dp->i_afp;
 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
 	size = be16_to_cpu(sf->hdr.totsize);
-	tmpbuffer = kmem_alloc(size, 0);
+	tmpbuffer = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	ASSERT(tmpbuffer != NULL);
 	memcpy(tmpbuffer, ifp->if_u1.if_data, size);
 	sf = (xfs_attr_shortform_t *)tmpbuffer;
@@ -1073,7 +1073,7 @@ xfs_attr3_leaf_to_shortform(
 
 	trace_xfs_attr_leaf_to_sf(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	if (!tmpbuffer)
 		return -ENOMEM;
 
@@ -1534,7 +1534,7 @@ xfs_attr3_leaf_compact(
 
 	trace_xfs_attr_leaf_compact(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
 	memset(bp->b_addr, 0, args->geo->blksize);
 	leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
fs/xfs/libxfs/xfs_bmap.c +1 -1
index 37596e49b92e..fc5bed95bd44 100644
@@ -6045,7 +6045,7 @@ __xfs_bmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
+	bi = kmalloc(sizeof(struct xfs_bmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&bi->bi_list);
 	bi->bi_type = type;
 	bi->bi_owner = ip;
fs/xfs/libxfs/xfs_da_btree.c +3 -1
index 29c25d1b3b76..efe84c636bd3 100644
@@ -2152,7 +2152,9 @@ xfs_da_grow_inode_int(
 		 * If we didn't get it and the block might work if fragmented,
 		 * try without the CONTIG flag.  Loop until we get it all.
 		 */
-		mapp = kmem_alloc(sizeof(*mapp) * count, 0);
+		mapp = kmalloc(sizeof(*mapp) * count,
+			       GFP_KERNEL | __GFP_NOFAIL);
+
 		for (b = *bno, mapi = 0; b < *bno + count; ) {
 			nmap = min(XFS_BMAP_MAX_NMAP, count);
 			c = (int)(*bno + count - b);
fs/xfs/libxfs/xfs_defer.c +2 -2
index 27c3d150068a..7dd16f208b82 100644
@@ -516,8 +516,8 @@ xfs_defer_add(
 			dfp = NULL;
 	}
 	if (!dfp) {
-		dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
-				KM_NOFS);
+		dfp = kmalloc(sizeof(struct xfs_defer_pending),
+			      GFP_NOFS | __GFP_NOFAIL);
 		dfp->dfp_type = type;
 		dfp->dfp_intent = NULL;
 		dfp->dfp_done = NULL;
fs/xfs/libxfs/xfs_dir2.c +1 -1
index c2deda036271..4777356b4f83 100644
@@ -331,7 +331,7 @@ xfs_dir_cilookup_result(
 					!(args->op_flags & XFS_DA_OP_CILOOKUP))
 		return -EEXIST;
 
-	args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
+	args->value = kmalloc(len, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!args->value)
 		return -ENOMEM;
 
fs/xfs/libxfs/xfs_dir2_block.c +1 -1
index 766f282b706a..54ae07a432e4 100644
@@ -1083,7 +1083,7 @@ xfs_dir2_sf_to_block(
 	 * Copy the directory into a temporary buffer.
 	 * Then pitch the incore inode data so we can make extents.
 	 */
-	sfp = kmem_alloc(ifp->if_bytes, 0);
+	sfp = kmalloc(ifp->if_bytes, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, oldsfp, ifp->if_bytes);
 
 	xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
fs/xfs/libxfs/xfs_dir2_sf.c +4 -4
index f4de4e7b10ef..43d72aebb9cf 100644
@@ -276,7 +276,7 @@ xfs_dir2_block_to_sf(
 	 * format the data into.  Once we have formatted the data, we can free
 	 * the block and copy the formatted data into the inode literal area.
 	 */
-	sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
+	sfp = kmalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
 
 	/*
@@ -530,7 +530,7 @@ xfs_dir2_sf_addname_hard(
 	 */
 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	old_isize = (int)dp->i_d.di_size;
-	buf = kmem_alloc(old_isize, 0);
+	buf = kmalloc(old_isize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
 	memcpy(oldsfp, sfp, old_isize);
 	/*
@@ -1162,7 +1162,7 @@ xfs_dir2_sf_toino4(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 1);
 	memcpy(buf, oldsfp, oldsize);
@@ -1235,7 +1235,7 @@ xfs_dir2_sf_toino8(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 0);
 	memcpy(buf, oldsfp, oldsize);
fs/xfs/libxfs/xfs_inode_fork.c +6 -4
index 82799dddf97d..62c305654657 100644
@@ -153,7 +153,8 @@ xfs_init_local_fork(
 
 	if (size) {
 		real_size = roundup(mem_size, 4);
-		ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
+		ifp->if_u1.if_data = kmalloc(real_size,
+					     GFP_NOFS | __GFP_NOFAIL);
 		memcpy(ifp->if_u1.if_data, data, size);
 		if (zero_terminate)
 			ifp->if_u1.if_data[size] = '\0';
@@ -308,7 +309,7 @@ xfs_iformat_btree(
 	}
 
 	ifp->if_broot_bytes = size;
-	ifp->if_broot = kmem_alloc(size, KM_NOFS);
+	ifp->if_broot = kmalloc(size, GFP_NOFS | __GFP_NOFAIL);
 	ASSERT(ifp->if_broot != NULL);
 	/*
 	 * Copy and convert from the on-disk structure
@@ -373,7 +374,8 @@ xfs_iroot_realloc(
 		 */
 		if (ifp->if_broot_bytes == 0) {
 			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
-			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
+			ifp->if_broot = kmalloc(new_size,
+						GFP_NOFS | __GFP_NOFAIL);
 			ifp->if_broot_bytes = (int)new_size;
 			return;
 		}
@@ -414,7 +416,7 @@ xfs_iroot_realloc(
 	else
 		new_size = 0;
 	if (new_size > 0) {
-		new_broot = kmem_alloc(new_size, KM_NOFS);
+		new_broot = kmalloc(new_size, GFP_NOFS | __GFP_NOFAIL);
 		/*
 		 * First copy over the btree block header.
 		 */
fs/xfs/libxfs/xfs_refcount.c +5 -4
index 07894c53e753..6a89443da50a 100644
@@ -1188,8 +1188,8 @@ __xfs_refcount_add(
 			type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
 			blockcount);
 
-	ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
-			KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_refcount_intent),
+		     GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_startblock = startblock;
@@ -1584,7 +1584,7 @@ struct xfs_refcount_recovery {
 /* Stuff an extent on the recovery list. */
 STATIC int
 xfs_refcount_recover_extent(
-	struct xfs_btree_cur 		*cur,
+	struct xfs_btree_cur		*cur,
 	union xfs_btree_rec		*rec,
 	void				*priv)
 {
@@ -1596,7 +1596,8 @@ xfs_refcount_recover_extent(
 		return -EFSCORRUPTED;
 	}
 
-	rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), 0);
+	rr = kmalloc(sizeof(struct xfs_refcount_recovery),
+		     GFP_KERNEL | __GFP_NOFAIL);
 	xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
 	list_add_tail(&rr->rr_list, debris);
 
fs/xfs/libxfs/xfs_rmap.c +1 -1
index 38e9414878b3..0e1e8cbb8862 100644
@@ -2286,7 +2286,7 @@ __xfs_rmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_rmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_owner = owner;
fs/xfs/scrub/bitmap.c +4 -3
index cabde1c4c235..5787d4f74e71 100644
@@ -25,7 +25,8 @@ xfs_bitmap_set(
 {
 	struct xfs_bitmap_range	*bmr;
 
-	bmr = kmem_alloc(sizeof(struct xfs_bitmap_range), KM_MAYFAIL);
+	bmr = kmalloc(sizeof(struct xfs_bitmap_range),
+		      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bmr)
 		return -ENOMEM;
 
@@ -181,8 +182,8 @@ xfs_bitmap_disunion(
 			 * Deleting from the middle: add the new right extent
 			 * and then shrink the left extent.
 			 */
-			new_br = kmem_alloc(sizeof(struct xfs_bitmap_range),
-					KM_MAYFAIL);
+			new_br = kmalloc(sizeof(struct xfs_bitmap_range),
+					 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 			if (!new_br) {
 				error = -ENOMEM;
 				goto out;
fs/xfs/scrub/btree.c +2 -2
index bed40b605076..857f813681ed 100644
@@ -429,8 +429,8 @@ xchk_btree_check_owner(
 	 * later scanning.
 	 */
 	if (cur->bc_btnum == XFS_BTNUM_BNO || cur->bc_btnum == XFS_BTNUM_RMAP) {
-		co = kmem_alloc(sizeof(struct check_owner),
-				KM_MAYFAIL);
+		co = kmalloc(sizeof(struct check_owner),
+			     GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!co)
 			return -ENOMEM;
 		co->level = level;
fs/xfs/scrub/refcount.c +2 -2
index 985724e81ebf..f5c2e320e416 100644
@@ -125,8 +125,8 @@ xchk_refcountbt_rmap_check(
 		 * is healthy each rmap_irec we see will be in agbno order
 		 * so we don't need insertion sort here.
 		 */
-		frag = kmem_alloc(sizeof(struct xchk_refcnt_frag),
-				KM_MAYFAIL);
+		frag = kmalloc(sizeof(struct xchk_refcnt_frag),
+			       GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!frag)
 			return -ENOMEM;
 		memcpy(&frag->rm, rec, sizeof(frag->rm));
fs/xfs/xfs_attr_inactive.c +1 -1
index 8351b3b611ac..42d7d8cbdb6e 100644
@@ -148,7 +148,7 @@ xfs_attr3_leaf_inactive(
 	 * Allocate storage for a list of all the "remote" value extents.
 	 */
 	size = count * sizeof(xfs_attr_inactive_list_t);
-	list = kmem_alloc(size, 0);
+	list = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 
 	/*
 	 * Identify each of the "remote" value extents.
fs/xfs/xfs_attr_list.c +1 -1
index e1d1c4eb9e69..2a475ca6e353 100644
@@ -116,7 +116,7 @@ xfs_attr_shortform_list(
 	 * It didn't all fit, so we have to sort everything on hashval.
 	 */
 	sbsize = sf->hdr.count * sizeof(*sbuf);
-	sbp = sbuf = kmem_alloc(sbsize, KM_NOFS);
+	sbp = sbuf = kmalloc(sbsize, GFP_NOFS | __GFP_NOFAIL);
 
 	/*
 	 * Scan the attribute list for the rest of the entries, storing
fs/xfs/xfs_buf.c +3 -2
index c70122fbc2a8..7428fe6a322c 100644
@@ -274,8 +274,9 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
-						 page_count, KM_NOFS);
+			bp->b_pages = kmalloc(sizeof(struct page *) *
+					      page_count,
+					      GFP_NOFS | __GFP_NOFAIL);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
fs/xfs/xfs_filestream.c +1 -1
index 9778e4e69e07..38b634cef1ed 100644
@@ -247,7 +247,7 @@ xfs_filestream_pick_ag(
 		return 0;
 
 	err = -ENOMEM;
-	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
+	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!item)
 		goto out_put_ag;
 
fs/xfs/xfs_inode.c +1 -1
index 297b2a73f285..1d1fe67ad237 100644
@@ -3493,7 +3493,7 @@ xfs_iflush_cluster(
 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 
 	cilist_size = igeo->inodes_per_cluster * sizeof(struct xfs_inode *);
-	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
+	cilist = kmalloc(cilist_size, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!cilist)
 		goto out_put;
 
fs/xfs/xfs_iwalk.c +1 -1
index e6006423e140..aa6bc0555d21 100644
@@ -152,7 +152,7 @@ xfs_iwalk_alloc(
 
 	/* Allocate a prefetch buffer for inobt records. */
 	size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
-	iwag->recs = kmem_alloc(size, KM_MAYFAIL);
+	iwag->recs = kmalloc(size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (iwag->recs == NULL)
 		return -ENOMEM;
 
fs/xfs/xfs_log_recover.c +4 -3
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;
 
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
Reviewed-by on v1 5/5
#13 lore raw

Re: [PATCH 5/5] xfs: Convert kmem_alloc() users

Quoted 10 lines · Carlos Maiolino

Use kmalloc() directly.

There is no logic change on kmem_alloc() since it's being removed soon, but for now, kmem_alloc_io() and kmem_alloc_large() still have use for kmem_alloc() due their fallback to vmalloc() and also the alignment check, so we can't completely remove it here.
But, there is no need to export kmem_alloc() to the whole XFS driver anymore, so, convert kmem_alloc() into a static, local function __kmem_alloc().

Looks fine, I'll give it a spin,

--D

Quoted 516 lines

---
V2:
- Rephrase commit log

 fs/xfs/kmem.c                  |  8 ++++----
 fs/xfs/kmem.h                  |  1 -
 fs/xfs/libxfs/xfs_attr_leaf.c  |  6 +++---
 fs/xfs/libxfs/xfs_bmap.c       |  2 +-
 fs/xfs/libxfs/xfs_da_btree.c   |  4 +++-
 fs/xfs/libxfs/xfs_defer.c      |  4 ++--
 fs/xfs/libxfs/xfs_dir2.c       |  2 +-
 fs/xfs/libxfs/xfs_dir2_block.c |  2 +-
 fs/xfs/libxfs/xfs_dir2_sf.c    |  8 ++++----
 fs/xfs/libxfs/xfs_inode_fork.c | 10 ++++++----
 fs/xfs/libxfs/xfs_refcount.c   |  9 +++++----
 fs/xfs/libxfs/xfs_rmap.c       |  2 +-
 fs/xfs/scrub/bitmap.c          |  7 ++++---
 fs/xfs/scrub/btree.c           |  4 ++--
 fs/xfs/scrub/refcount.c        |  4 ++--
 fs/xfs/xfs_attr_inactive.c     |  2 +-
 fs/xfs/xfs_attr_list.c         |  2 +-
 fs/xfs/xfs_buf.c               |  5 +++--
 fs/xfs/xfs_filestream.c        |  2 +-
 fs/xfs/xfs_inode.c             |  2 +-
 fs/xfs/xfs_iwalk.c             |  2 +-
 fs/xfs/xfs_log_recover.c       |  7 ++++---
 fs/xfs/xfs_qm.c                |  3 ++-
 fs/xfs/xfs_rtalloc.c           |  2 +-
 fs/xfs/xfs_super.c             |  2 +-
 25 files changed, 55 insertions(+), 47 deletions(-)
fs/xfs/kmem.c +4 -4
index 6e10e565632c..79467813d810 100644
@@ -8,8 +8,8 @@
 #include "xfs_message.h"
 #include "xfs_trace.h"
 
-void *
-kmem_alloc(size_t size, xfs_km_flags_t flags)
+static void *
+__kmem_alloc(size_t size, xfs_km_flags_t flags)
 {
 	int	retries = 0;
 	gfp_t	lflags = kmem_flags_convert(flags);
@@ -72,7 +72,7 @@ kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags)
 	if (WARN_ON_ONCE(align_mask >= PAGE_SIZE))
 		align_mask = PAGE_SIZE - 1;
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr) {
 		if (!((uintptr_t)ptr & align_mask))
 			return ptr;
@@ -88,7 +88,7 @@ kmem_alloc_large(size_t size, xfs_km_flags_t flags)
 
 	trace_kmem_alloc_large(size, flags, _RET_IP_);
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr)
 		return ptr;
 	return __kmem_vmalloc(size, flags);
fs/xfs/kmem.h +0 -1
index a18c27c99721..78a54839430a 100644
@@ -52,7 +52,6 @@ kmem_flags_convert(xfs_km_flags_t flags)
 	return lflags;
 }
 
-extern void *kmem_alloc(size_t, xfs_km_flags_t);
 extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
 
fs/xfs/libxfs/xfs_attr_leaf.c +3 -3
index 67de68584224..807950eca17a 100644
@@ -885,7 +885,7 @@ xfs_attr_shortform_to_leaf(
 	ifp = dp->i_afp;
 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
 	size = be16_to_cpu(sf->hdr.totsize);
-	tmpbuffer = kmem_alloc(size, 0);
+	tmpbuffer = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	ASSERT(tmpbuffer != NULL);
 	memcpy(tmpbuffer, ifp->if_u1.if_data, size);
 	sf = (xfs_attr_shortform_t *)tmpbuffer;
@@ -1073,7 +1073,7 @@ xfs_attr3_leaf_to_shortform(
 
 	trace_xfs_attr_leaf_to_sf(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	if (!tmpbuffer)
 		return -ENOMEM;
 
@@ -1534,7 +1534,7 @@ xfs_attr3_leaf_compact(
 
 	trace_xfs_attr_leaf_compact(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
 	memset(bp->b_addr, 0, args->geo->blksize);
 	leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
fs/xfs/libxfs/xfs_bmap.c +1 -1
index 37596e49b92e..fc5bed95bd44 100644
@@ -6045,7 +6045,7 @@ __xfs_bmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
+	bi = kmalloc(sizeof(struct xfs_bmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&bi->bi_list);
 	bi->bi_type = type;
 	bi->bi_owner = ip;
fs/xfs/libxfs/xfs_da_btree.c +3 -1
index 29c25d1b3b76..efe84c636bd3 100644
@@ -2152,7 +2152,9 @@ xfs_da_grow_inode_int(
 		 * If we didn't get it and the block might work if fragmented,
 		 * try without the CONTIG flag.  Loop until we get it all.
 		 */
-		mapp = kmem_alloc(sizeof(*mapp) * count, 0);
+		mapp = kmalloc(sizeof(*mapp) * count,
+			       GFP_KERNEL | __GFP_NOFAIL);
+
 		for (b = *bno, mapi = 0; b < *bno + count; ) {
 			nmap = min(XFS_BMAP_MAX_NMAP, count);
 			c = (int)(*bno + count - b);
fs/xfs/libxfs/xfs_defer.c +2 -2
index 27c3d150068a..7dd16f208b82 100644
@@ -516,8 +516,8 @@ xfs_defer_add(
 			dfp = NULL;
 	}
 	if (!dfp) {
-		dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
-				KM_NOFS);
+		dfp = kmalloc(sizeof(struct xfs_defer_pending),
+			      GFP_NOFS | __GFP_NOFAIL);
 		dfp->dfp_type = type;
 		dfp->dfp_intent = NULL;
 		dfp->dfp_done = NULL;
fs/xfs/libxfs/xfs_dir2.c +1 -1
index c2deda036271..4777356b4f83 100644
@@ -331,7 +331,7 @@ xfs_dir_cilookup_result(
 					!(args->op_flags & XFS_DA_OP_CILOOKUP))
 		return -EEXIST;
 
-	args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
+	args->value = kmalloc(len, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!args->value)
 		return -ENOMEM;
 
fs/xfs/libxfs/xfs_dir2_block.c +1 -1
index 766f282b706a..54ae07a432e4 100644
@@ -1083,7 +1083,7 @@ xfs_dir2_sf_to_block(
 	 * Copy the directory into a temporary buffer.
 	 * Then pitch the incore inode data so we can make extents.
 	 */
-	sfp = kmem_alloc(ifp->if_bytes, 0);
+	sfp = kmalloc(ifp->if_bytes, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, oldsfp, ifp->if_bytes);
 
 	xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
fs/xfs/libxfs/xfs_dir2_sf.c +4 -4
index f4de4e7b10ef..43d72aebb9cf 100644
@@ -276,7 +276,7 @@ xfs_dir2_block_to_sf(
 	 * format the data into.  Once we have formatted the data, we can free
 	 * the block and copy the formatted data into the inode literal area.
 	 */
-	sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
+	sfp = kmalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
 
 	/*
@@ -530,7 +530,7 @@ xfs_dir2_sf_addname_hard(
 	 */
 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	old_isize = (int)dp->i_d.di_size;
-	buf = kmem_alloc(old_isize, 0);
+	buf = kmalloc(old_isize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
 	memcpy(oldsfp, sfp, old_isize);
 	/*
@@ -1162,7 +1162,7 @@ xfs_dir2_sf_toino4(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 1);
 	memcpy(buf, oldsfp, oldsize);
@@ -1235,7 +1235,7 @@ xfs_dir2_sf_toino8(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 0);
 	memcpy(buf, oldsfp, oldsize);
fs/xfs/libxfs/xfs_inode_fork.c +6 -4
index 82799dddf97d..62c305654657 100644
@@ -153,7 +153,8 @@ xfs_init_local_fork(
 
 	if (size) {
 		real_size = roundup(mem_size, 4);
-		ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
+		ifp->if_u1.if_data = kmalloc(real_size,
+					     GFP_NOFS | __GFP_NOFAIL);
 		memcpy(ifp->if_u1.if_data, data, size);
 		if (zero_terminate)
 			ifp->if_u1.if_data[size] = '\0';
@@ -308,7 +309,7 @@ xfs_iformat_btree(
 	}
 
 	ifp->if_broot_bytes = size;
-	ifp->if_broot = kmem_alloc(size, KM_NOFS);
+	ifp->if_broot = kmalloc(size, GFP_NOFS | __GFP_NOFAIL);
 	ASSERT(ifp->if_broot != NULL);
 	/*
 	 * Copy and convert from the on-disk structure
@@ -373,7 +374,8 @@ xfs_iroot_realloc(
 		 */
 		if (ifp->if_broot_bytes == 0) {
 			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
-			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
+			ifp->if_broot = kmalloc(new_size,
+						GFP_NOFS | __GFP_NOFAIL);
 			ifp->if_broot_bytes = (int)new_size;
 			return;
 		}
@@ -414,7 +416,7 @@ xfs_iroot_realloc(
 	else
 		new_size = 0;
 	if (new_size > 0) {
-		new_broot = kmem_alloc(new_size, KM_NOFS);
+		new_broot = kmalloc(new_size, GFP_NOFS | __GFP_NOFAIL);
 		/*
 		 * First copy over the btree block header.
 		 */
fs/xfs/libxfs/xfs_refcount.c +5 -4
index 07894c53e753..6a89443da50a 100644
@@ -1188,8 +1188,8 @@ __xfs_refcount_add(
 			type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
 			blockcount);
 
-	ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
-			KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_refcount_intent),
+		     GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_startblock = startblock;
@@ -1584,7 +1584,7 @@ struct xfs_refcount_recovery {
 /* Stuff an extent on the recovery list. */
 STATIC int
 xfs_refcount_recover_extent(
-	struct xfs_btree_cur 		*cur,
+	struct xfs_btree_cur		*cur,
 	union xfs_btree_rec		*rec,
 	void				*priv)
 {
@@ -1596,7 +1596,8 @@ xfs_refcount_recover_extent(
 		return -EFSCORRUPTED;
 	}
 
-	rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), 0);
+	rr = kmalloc(sizeof(struct xfs_refcount_recovery),
+		     GFP_KERNEL | __GFP_NOFAIL);
 	xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
 	list_add_tail(&rr->rr_list, debris);
 
fs/xfs/libxfs/xfs_rmap.c +1 -1
index 38e9414878b3..0e1e8cbb8862 100644
@@ -2286,7 +2286,7 @@ __xfs_rmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_rmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_owner = owner;
fs/xfs/scrub/bitmap.c +4 -3
index cabde1c4c235..5787d4f74e71 100644
@@ -25,7 +25,8 @@ xfs_bitmap_set(
 {
 	struct xfs_bitmap_range	*bmr;
 
-	bmr = kmem_alloc(sizeof(struct xfs_bitmap_range), KM_MAYFAIL);
+	bmr = kmalloc(sizeof(struct xfs_bitmap_range),
+		      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bmr)
 		return -ENOMEM;
 
@@ -181,8 +182,8 @@ xfs_bitmap_disunion(
 			 * Deleting from the middle: add the new right extent
 			 * and then shrink the left extent.
 			 */
-			new_br = kmem_alloc(sizeof(struct xfs_bitmap_range),
-					KM_MAYFAIL);
+			new_br = kmalloc(sizeof(struct xfs_bitmap_range),
+					 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 			if (!new_br) {
 				error = -ENOMEM;
 				goto out;
fs/xfs/scrub/btree.c +2 -2
index bed40b605076..857f813681ed 100644
@@ -429,8 +429,8 @@ xchk_btree_check_owner(
 	 * later scanning.
 	 */
 	if (cur->bc_btnum == XFS_BTNUM_BNO || cur->bc_btnum == XFS_BTNUM_RMAP) {
-		co = kmem_alloc(sizeof(struct check_owner),
-				KM_MAYFAIL);
+		co = kmalloc(sizeof(struct check_owner),
+			     GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!co)
 			return -ENOMEM;
 		co->level = level;
fs/xfs/scrub/refcount.c +2 -2
index 985724e81ebf..f5c2e320e416 100644
@@ -125,8 +125,8 @@ xchk_refcountbt_rmap_check(
 		 * is healthy each rmap_irec we see will be in agbno order
 		 * so we don't need insertion sort here.
 		 */
-		frag = kmem_alloc(sizeof(struct xchk_refcnt_frag),
-				KM_MAYFAIL);
+		frag = kmalloc(sizeof(struct xchk_refcnt_frag),
+			       GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!frag)
 			return -ENOMEM;
 		memcpy(&frag->rm, rec, sizeof(frag->rm));
fs/xfs/xfs_attr_inactive.c +1 -1
index 8351b3b611ac..42d7d8cbdb6e 100644
@@ -148,7 +148,7 @@ xfs_attr3_leaf_inactive(
 	 * Allocate storage for a list of all the "remote" value extents.
 	 */
 	size = count * sizeof(xfs_attr_inactive_list_t);
-	list = kmem_alloc(size, 0);
+	list = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 
 	/*
 	 * Identify each of the "remote" value extents.
fs/xfs/xfs_attr_list.c +1 -1
index e1d1c4eb9e69..2a475ca6e353 100644
@@ -116,7 +116,7 @@ xfs_attr_shortform_list(
 	 * It didn't all fit, so we have to sort everything on hashval.
 	 */
 	sbsize = sf->hdr.count * sizeof(*sbuf);
-	sbp = sbuf = kmem_alloc(sbsize, KM_NOFS);
+	sbp = sbuf = kmalloc(sbsize, GFP_NOFS | __GFP_NOFAIL);
 
 	/*
 	 * Scan the attribute list for the rest of the entries, storing
fs/xfs/xfs_buf.c +3 -2
index c70122fbc2a8..7428fe6a322c 100644
@@ -274,8 +274,9 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
-						 page_count, KM_NOFS);
+			bp->b_pages = kmalloc(sizeof(struct page *) *
+					      page_count,
+					      GFP_NOFS | __GFP_NOFAIL);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
fs/xfs/xfs_filestream.c +1 -1
index 9778e4e69e07..38b634cef1ed 100644
@@ -247,7 +247,7 @@ xfs_filestream_pick_ag(
 		return 0;
 
 	err = -ENOMEM;
-	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
+	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!item)
 		goto out_put_ag;
 
fs/xfs/xfs_inode.c +1 -1
index 297b2a73f285..1d1fe67ad237 100644
@@ -3493,7 +3493,7 @@ xfs_iflush_cluster(
 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 
 	cilist_size = igeo->inodes_per_cluster * sizeof(struct xfs_inode *);
-	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
+	cilist = kmalloc(cilist_size, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!cilist)
 		goto out_put;
 
fs/xfs/xfs_iwalk.c +1 -1
index e6006423e140..aa6bc0555d21 100644
@@ -152,7 +152,7 @@ xfs_iwalk_alloc(
 
 	/* Allocate a prefetch buffer for inobt records. */
 	size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
-	iwag->recs = kmem_alloc(size, KM_MAYFAIL);
+	iwag->recs = kmalloc(size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (iwag->recs == NULL)
 		return -ENOMEM;
 
fs/xfs/xfs_log_recover.c +4 -3
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;
 
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
#14 lore raw

Re: [PATCH 5/5] xfs: Convert kmem_alloc() users

In reply to Carlos Maiolino
Quoted 475 lines · Carlos Maiolino

Use kmalloc() directly.

There is no logic change on kmem_alloc() since it's being removed soon, but for now, kmem_alloc_io() and kmem_alloc_large() still have use for kmem_alloc() due their fallback to vmalloc() and also the alignment check, so we can't completely remove it here.
But, there is no need to export kmem_alloc() to the whole XFS driver anymore, so, convert kmem_alloc() into a static, local function __kmem_alloc().

---
V2:
- Rephrase commit log

 fs/xfs/kmem.c                  |  8 ++++----
 fs/xfs/kmem.h                  |  1 -
 fs/xfs/libxfs/xfs_attr_leaf.c  |  6 +++---
 fs/xfs/libxfs/xfs_bmap.c       |  2 +-
 fs/xfs/libxfs/xfs_da_btree.c   |  4 +++-
 fs/xfs/libxfs/xfs_defer.c      |  4 ++--
 fs/xfs/libxfs/xfs_dir2.c       |  2 +-
 fs/xfs/libxfs/xfs_dir2_block.c |  2 +-
 fs/xfs/libxfs/xfs_dir2_sf.c    |  8 ++++----
 fs/xfs/libxfs/xfs_inode_fork.c | 10 ++++++----
 fs/xfs/libxfs/xfs_refcount.c   |  9 +++++----
 fs/xfs/libxfs/xfs_rmap.c       |  2 +-
 fs/xfs/scrub/bitmap.c          |  7 ++++---
 fs/xfs/scrub/btree.c           |  4 ++--
 fs/xfs/scrub/refcount.c        |  4 ++--
 fs/xfs/xfs_attr_inactive.c     |  2 +-
 fs/xfs/xfs_attr_list.c         |  2 +-
 fs/xfs/xfs_buf.c               |  5 +++--
 fs/xfs/xfs_filestream.c        |  2 +-
 fs/xfs/xfs_inode.c             |  2 +-
 fs/xfs/xfs_iwalk.c             |  2 +-
 fs/xfs/xfs_log_recover.c       |  7 ++++---
 fs/xfs/xfs_qm.c                |  3 ++-
 fs/xfs/xfs_rtalloc.c           |  2 +-
 fs/xfs/xfs_super.c             |  2 +-
 25 files changed, 55 insertions(+), 47 deletions(-)
fs/xfs/kmem.c +4 -4
index 6e10e565632c..79467813d810 100644
@@ -8,8 +8,8 @@
 #include "xfs_message.h"
 #include "xfs_trace.h"
 
-void *
-kmem_alloc(size_t size, xfs_km_flags_t flags)
+static void *
+__kmem_alloc(size_t size, xfs_km_flags_t flags)
 {
 	int	retries = 0;
 	gfp_t	lflags = kmem_flags_convert(flags);
@@ -72,7 +72,7 @@ kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags)
 	if (WARN_ON_ONCE(align_mask >= PAGE_SIZE))
 		align_mask = PAGE_SIZE - 1;
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr) {
 		if (!((uintptr_t)ptr & align_mask))
 			return ptr;
@@ -88,7 +88,7 @@ kmem_alloc_large(size_t size, xfs_km_flags_t flags)
 
 	trace_kmem_alloc_large(size, flags, _RET_IP_);
 
-	ptr = kmem_alloc(size, flags | KM_MAYFAIL);
+	ptr = __kmem_alloc(size, flags | KM_MAYFAIL);
 	if (ptr)
 		return ptr;
 	return __kmem_vmalloc(size, flags);
fs/xfs/kmem.h +0 -1
index a18c27c99721..78a54839430a 100644
@@ -52,7 +52,6 @@ kmem_flags_convert(xfs_km_flags_t flags)
 	return lflags;
 }
 
-extern void *kmem_alloc(size_t, xfs_km_flags_t);
 extern void *kmem_alloc_io(size_t size, int align_mask, xfs_km_flags_t flags);
 extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
 
fs/xfs/libxfs/xfs_attr_leaf.c +3 -3
index 67de68584224..807950eca17a 100644
@@ -885,7 +885,7 @@ xfs_attr_shortform_to_leaf(
 	ifp = dp->i_afp;
 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
 	size = be16_to_cpu(sf->hdr.totsize);
-	tmpbuffer = kmem_alloc(size, 0);
+	tmpbuffer = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 	ASSERT(tmpbuffer != NULL);
 	memcpy(tmpbuffer, ifp->if_u1.if_data, size);
 	sf = (xfs_attr_shortform_t *)tmpbuffer;
@@ -1073,7 +1073,7 @@ xfs_attr3_leaf_to_shortform(
 
 	trace_xfs_attr_leaf_to_sf(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	if (!tmpbuffer)
 		return -ENOMEM;
 
@@ -1534,7 +1534,7 @@ xfs_attr3_leaf_compact(
 
 	trace_xfs_attr_leaf_compact(args);
 
-	tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
 	memset(bp->b_addr, 0, args->geo->blksize);
 	leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
fs/xfs/libxfs/xfs_bmap.c +1 -1
index 37596e49b92e..fc5bed95bd44 100644
@@ -6045,7 +6045,7 @@ __xfs_bmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
+	bi = kmalloc(sizeof(struct xfs_bmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&bi->bi_list);
 	bi->bi_type = type;
 	bi->bi_owner = ip;
fs/xfs/libxfs/xfs_da_btree.c +3 -1
index 29c25d1b3b76..efe84c636bd3 100644
@@ -2152,7 +2152,9 @@ xfs_da_grow_inode_int(
 		 * If we didn't get it and the block might work if fragmented,
 		 * try without the CONTIG flag.  Loop until we get it all.
 		 */
-		mapp = kmem_alloc(sizeof(*mapp) * count, 0);
+		mapp = kmalloc(sizeof(*mapp) * count,
+			       GFP_KERNEL | __GFP_NOFAIL);
+
 		for (b = *bno, mapi = 0; b < *bno + count; ) {
 			nmap = min(XFS_BMAP_MAX_NMAP, count);
 			c = (int)(*bno + count - b);
fs/xfs/libxfs/xfs_defer.c +2 -2
index 27c3d150068a..7dd16f208b82 100644
@@ -516,8 +516,8 @@ xfs_defer_add(
 			dfp = NULL;
 	}
 	if (!dfp) {
-		dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
-				KM_NOFS);
+		dfp = kmalloc(sizeof(struct xfs_defer_pending),
+			      GFP_NOFS | __GFP_NOFAIL);
 		dfp->dfp_type = type;
 		dfp->dfp_intent = NULL;
 		dfp->dfp_done = NULL;
fs/xfs/libxfs/xfs_dir2.c +1 -1
index c2deda036271..4777356b4f83 100644
@@ -331,7 +331,7 @@ xfs_dir_cilookup_result(
 					!(args->op_flags & XFS_DA_OP_CILOOKUP))
 		return -EEXIST;
 
-	args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
+	args->value = kmalloc(len, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!args->value)
 		return -ENOMEM;
 
fs/xfs/libxfs/xfs_dir2_block.c +1 -1
index 766f282b706a..54ae07a432e4 100644
@@ -1083,7 +1083,7 @@ xfs_dir2_sf_to_block(
 	 * Copy the directory into a temporary buffer.
 	 * Then pitch the incore inode data so we can make extents.
 	 */
-	sfp = kmem_alloc(ifp->if_bytes, 0);
+	sfp = kmalloc(ifp->if_bytes, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, oldsfp, ifp->if_bytes);
 
 	xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
fs/xfs/libxfs/xfs_dir2_sf.c +4 -4
index f4de4e7b10ef..43d72aebb9cf 100644
@@ -276,7 +276,7 @@ xfs_dir2_block_to_sf(
 	 * format the data into.  Once we have formatted the data, we can free
 	 * the block and copy the formatted data into the inode literal area.
 	 */
-	sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
+	sfp = kmalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
 
 	/*
@@ -530,7 +530,7 @@ xfs_dir2_sf_addname_hard(
 	 */
 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	old_isize = (int)dp->i_d.di_size;
-	buf = kmem_alloc(old_isize, 0);
+	buf = kmalloc(old_isize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)buf;
 	memcpy(oldsfp, sfp, old_isize);
 	/*
@@ -1162,7 +1162,7 @@ xfs_dir2_sf_toino4(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 1);
 	memcpy(buf, oldsfp, oldsize);
@@ -1235,7 +1235,7 @@ xfs_dir2_sf_toino8(
 	 * Don't want xfs_idata_realloc copying the data here.
 	 */
 	oldsize = dp->i_df.if_bytes;
-	buf = kmem_alloc(oldsize, 0);
+	buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
 	oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
 	ASSERT(oldsfp->i8count == 0);
 	memcpy(buf, oldsfp, oldsize);
fs/xfs/libxfs/xfs_inode_fork.c +6 -4
index 82799dddf97d..62c305654657 100644
@@ -153,7 +153,8 @@ xfs_init_local_fork(
 
 	if (size) {
 		real_size = roundup(mem_size, 4);
-		ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
+		ifp->if_u1.if_data = kmalloc(real_size,
+					     GFP_NOFS | __GFP_NOFAIL);
 		memcpy(ifp->if_u1.if_data, data, size);
 		if (zero_terminate)
 			ifp->if_u1.if_data[size] = '\0';
@@ -308,7 +309,7 @@ xfs_iformat_btree(
 	}
 
 	ifp->if_broot_bytes = size;
-	ifp->if_broot = kmem_alloc(size, KM_NOFS);
+	ifp->if_broot = kmalloc(size, GFP_NOFS | __GFP_NOFAIL);
 	ASSERT(ifp->if_broot != NULL);
 	/*
 	 * Copy and convert from the on-disk structure
@@ -373,7 +374,8 @@ xfs_iroot_realloc(
 		 */
 		if (ifp->if_broot_bytes == 0) {
 			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
-			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
+			ifp->if_broot = kmalloc(new_size,
+						GFP_NOFS | __GFP_NOFAIL);
 			ifp->if_broot_bytes = (int)new_size;
 			return;
 		}
@@ -414,7 +416,7 @@ xfs_iroot_realloc(
 	else
 		new_size = 0;
 	if (new_size > 0) {
-		new_broot = kmem_alloc(new_size, KM_NOFS);
+		new_broot = kmalloc(new_size, GFP_NOFS | __GFP_NOFAIL);
 		/*
 		 * First copy over the btree block header.
 		 */
fs/xfs/libxfs/xfs_refcount.c +5 -4
index 07894c53e753..6a89443da50a 100644
@@ -1188,8 +1188,8 @@ __xfs_refcount_add(
 			type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
 			blockcount);
 
-	ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
-			KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_refcount_intent),
+		     GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_startblock = startblock;
@@ -1584,7 +1584,7 @@ struct xfs_refcount_recovery {
 /* Stuff an extent on the recovery list. */
 STATIC int
 xfs_refcount_recover_extent(
-	struct xfs_btree_cur 		*cur,
+	struct xfs_btree_cur		*cur,
 	union xfs_btree_rec		*rec,
 	void				*priv)
 {
@@ -1596,7 +1596,8 @@ xfs_refcount_recover_extent(
 		return -EFSCORRUPTED;
 	}
 
-	rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), 0);
+	rr = kmalloc(sizeof(struct xfs_refcount_recovery),
+		     GFP_KERNEL | __GFP_NOFAIL);
 	xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
 	list_add_tail(&rr->rr_list, debris);
 
fs/xfs/libxfs/xfs_rmap.c +1 -1
index 38e9414878b3..0e1e8cbb8862 100644
@@ -2286,7 +2286,7 @@ __xfs_rmap_add(
 			bmap->br_blockcount,
 			bmap->br_state);
 
-	ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_NOFS);
+	ri = kmalloc(sizeof(struct xfs_rmap_intent), GFP_NOFS | __GFP_NOFAIL);
 	INIT_LIST_HEAD(&ri->ri_list);
 	ri->ri_type = type;
 	ri->ri_owner = owner;
fs/xfs/scrub/bitmap.c +4 -3
index cabde1c4c235..5787d4f74e71 100644
@@ -25,7 +25,8 @@ xfs_bitmap_set(
 {
 	struct xfs_bitmap_range	*bmr;
 
-	bmr = kmem_alloc(sizeof(struct xfs_bitmap_range), KM_MAYFAIL);
+	bmr = kmalloc(sizeof(struct xfs_bitmap_range),
+		      GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!bmr)
 		return -ENOMEM;
 
@@ -181,8 +182,8 @@ xfs_bitmap_disunion(
 			 * Deleting from the middle: add the new right extent
 			 * and then shrink the left extent.
 			 */
-			new_br = kmem_alloc(sizeof(struct xfs_bitmap_range),
-					KM_MAYFAIL);
+			new_br = kmalloc(sizeof(struct xfs_bitmap_range),
+					 GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 			if (!new_br) {
 				error = -ENOMEM;
 				goto out;
fs/xfs/scrub/btree.c +2 -2
index bed40b605076..857f813681ed 100644
@@ -429,8 +429,8 @@ xchk_btree_check_owner(
 	 * later scanning.
 	 */
 	if (cur->bc_btnum == XFS_BTNUM_BNO || cur->bc_btnum == XFS_BTNUM_RMAP) {
-		co = kmem_alloc(sizeof(struct check_owner),
-				KM_MAYFAIL);
+		co = kmalloc(sizeof(struct check_owner),
+			     GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!co)
 			return -ENOMEM;
 		co->level = level;
fs/xfs/scrub/refcount.c +2 -2
index 985724e81ebf..f5c2e320e416 100644
@@ -125,8 +125,8 @@ xchk_refcountbt_rmap_check(
 		 * is healthy each rmap_irec we see will be in agbno order
 		 * so we don't need insertion sort here.
 		 */
-		frag = kmem_alloc(sizeof(struct xchk_refcnt_frag),
-				KM_MAYFAIL);
+		frag = kmalloc(sizeof(struct xchk_refcnt_frag),
+			       GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 		if (!frag)
 			return -ENOMEM;
 		memcpy(&frag->rm, rec, sizeof(frag->rm));
fs/xfs/xfs_attr_inactive.c +1 -1
index 8351b3b611ac..42d7d8cbdb6e 100644
@@ -148,7 +148,7 @@ xfs_attr3_leaf_inactive(
 	 * Allocate storage for a list of all the "remote" value extents.
 	 */
 	size = count * sizeof(xfs_attr_inactive_list_t);
-	list = kmem_alloc(size, 0);
+	list = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
 
 	/*
 	 * Identify each of the "remote" value extents.
fs/xfs/xfs_attr_list.c +1 -1
index e1d1c4eb9e69..2a475ca6e353 100644
@@ -116,7 +116,7 @@ xfs_attr_shortform_list(
 	 * It didn't all fit, so we have to sort everything on hashval.
 	 */
 	sbsize = sf->hdr.count * sizeof(*sbuf);
-	sbp = sbuf = kmem_alloc(sbsize, KM_NOFS);
+	sbp = sbuf = kmalloc(sbsize, GFP_NOFS | __GFP_NOFAIL);
 
 	/*
 	 * Scan the attribute list for the rest of the entries, storing
fs/xfs/xfs_buf.c +3 -2
index c70122fbc2a8..7428fe6a322c 100644
@@ -274,8 +274,9 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
-						 page_count, KM_NOFS);
+			bp->b_pages = kmalloc(sizeof(struct page *) *
+					      page_count,
+					      GFP_NOFS | __GFP_NOFAIL);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
fs/xfs/xfs_filestream.c +1 -1
index 9778e4e69e07..38b634cef1ed 100644
@@ -247,7 +247,7 @@ xfs_filestream_pick_ag(
 		return 0;
 
 	err = -ENOMEM;
-	item = kmem_alloc(sizeof(*item), KM_MAYFAIL);
+	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!item)
 		goto out_put_ag;
 
fs/xfs/xfs_inode.c +1 -1
index 297b2a73f285..1d1fe67ad237 100644
@@ -3493,7 +3493,7 @@ xfs_iflush_cluster(
 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 
 	cilist_size = igeo->inodes_per_cluster * sizeof(struct xfs_inode *);
-	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
+	cilist = kmalloc(cilist_size, GFP_NOFS | __GFP_RETRY_MAYFAIL);
 	if (!cilist)
 		goto out_put;
 
fs/xfs/xfs_iwalk.c +1 -1
index e6006423e140..aa6bc0555d21 100644
@@ -152,7 +152,7 @@ xfs_iwalk_alloc(
 
 	/* Allocate a prefetch buffer for inobt records. */
 	size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
-	iwag->recs = kmem_alloc(size, KM_MAYFAIL);
+	iwag->recs = kmalloc(size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (iwag->recs == NULL)
 		return -ENOMEM;
 
fs/xfs/xfs_log_recover.c +3 -2
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)

fs/xfs/xfs_log_recover.c xlog_recover_add_to_trans( · line 4277

Quoted 7 lines
fs/xfs/xfs_log_recover.c +1 -1
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;

I noticed that kmalloc is generating warnings with generic/049 when 16k directories (-n size=16k) are enabled. I /think/ this is because it's quite possible to write out an xlog_op_header with a length of more than a single page; log recovery will then try to allocate a huge memory buffer to recover the transaction; and so we try to do a huge NOFAIL allocation, which makes the mm unhappy.

The one thing I've noticed with this conversion series is that the flags translation isn't 100% 1-to-1. Before, kmem_flags_convert didn't explicitly set __GFP_NOFAIL anywhere; we simply took the default behavior. IIRC that means that small allocations actually /are/ guaranteed to succeed, but multipage allocations certainly aren't. This seems to be one place where we could have asked for a lot of memory, failed to get it, and crashed.

Now that we explicitly set NOFAIL in all the places where we don't also check for a null return, I think we're just uncovering latent bugs lurking in the code base. The kernel does actually fulfill the allocation request, but it's clearly not happy.

--D

Relevant snippet of dmesg; everything else was normal:

XFS (sdd): Mounting V5 Filesystem
XFS (sdd): Starting recovery (logdev: internal)

 ------------[ cut here ]------------
 WARNING: CPU: 1 PID: 459342 at mm/page_alloc.c:3275 get_page_from_freelist+0x434/0x1660
 Modules linked in: dm_thin_pool dm_persistent_data dm_bio_prison dm_snapshot dm_bufio dm_flakey xfs libcrc32c ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 ip_set_hash_ip ip_set_hash_net xt_tcpudp xt_set ip_set_hash_mac bfq ip_set nfnetlink ip6table_filter ip6_tables iptable_filter sch_fq_codel ip_tables x_tables nfsv4 af_packet [last unloaded: scsi_debug]
 CPU: 1 PID: 459342 Comm: mount Not tainted 5.4.0-rc3-djw #rc3
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-1ubuntu1 04/01/2014
 RIP: 0010:get_page_from_freelist+0x434/0x1660
 Code: e6 00 00 00 00 48 89 84 24 a0 00 00 00 0f 84 08 fd ff ff f7 84 24 c0 00 00 00 00 80 00 00 74 0c 83 bc 24 84 00 00 00 01 76 02 <0f> 0b 49 8d 87 10 05 00 00 48 89 c7 48 89 84 24 88 00 00 00 e8 03
 RSP: 0018:ffffc900035d3918 EFLAGS: 00010202
 RAX: ffff88803fffb680 RBX: 0000000000002968 RCX: ffffea0000c8e108
 RDX: ffff88803fffbba8 RSI: ffff88803fffb870 RDI: 0000000000000000
 RBP: 0000000000000002 R08: 0000000000000201 R09: 000000000002ff81
 R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000
 R13: 0000000000048cc0 R14: 0000000000000001 R15: ffff88803fffb680
 FS:  00007fcfdf89a080(0000) GS:ffff88803ea00000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000055b202ec48c8 CR3: 000000003bfdc005 CR4: 00000000001606a0
 Call Trace:
  ? kvm_clock_read+0x14/0x30
  __alloc_pages_nodemask+0x172/0x3a0
  kmalloc_order+0x18/0x80
  kmalloc_order_trace+0x1d/0x130
  xlog_recover_add_to_trans+0x4b/0x340 [xfs]
  xlog_recovery_process_trans+0xe9/0xf0 [xfs]
  xlog_recover_process_data+0x9e/0x1f0 [xfs]
  xlog_do_recovery_pass+0x3a9/0x7c0 [xfs]
  xlog_do_log_recovery+0x72/0x150 [xfs]
  xlog_do_recover+0x43/0x2a0 [xfs]
  xlog_recover+0xdf/0x170 [xfs]
  xfs_log_mount+0x2e3/0x300 [xfs]
  xfs_mountfs+0x4e7/0x9f0 [xfs]
  xfs_fc_fill_super+0x2f8/0x520 [xfs]
  ? xfs_fs_destroy_inode+0x4f0/0x4f0 [xfs]
  get_tree_bdev+0x198/0x270
  vfs_get_tree+0x23/0xb0
  do_mount+0x87e/0xa20
  ksys_mount+0xb6/0xd0
  __x64_sys_mount+0x21/0x30
  do_syscall_64+0x50/0x180
  entry_SYSCALL_64_after_hwframe+0x49/0xbe
 RIP: 0033:0x7fcfdf15d3ca
 Code: 48 8b 0d c1 8a 2c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8e 8a 2c 00 f7 d8 64 89 01 48
 RSP: 002b:00007fff0af10a58 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
 RAX: ffffffffffffffda RBX: 000055b202ec1970 RCX: 00007fcfdf15d3ca
 RDX: 000055b202ec1be0 RSI: 000055b202ec1c20 RDI: 000055b202ec1c00
 RBP: 0000000000000000 R08: 000055b202ec1b80 R09: 0000000000000000
 R10: 00000000c0ed0000 R11: 0000000000000202 R12: 000055b202ec1c00
 R13: 000055b202ec1be0 R14: 0000000000000000 R15: 00007fcfdf67e8a4
 irq event stamp: 18398
 hardirqs last  enabled at (18397): [<ffffffff8123738f>] __slab_alloc.isra.83+0x6f/0x80
 hardirqs last disabled at (18398): [<ffffffff81001d8a>] trace_hardirqs_off_thunk+0x1a/0x20
 softirqs last  enabled at (18158): [<ffffffff81a003af>] __do_softirq+0x3af/0x4a4
 softirqs last disabled at (18149): [<ffffffff8106528c>] irq_exit+0xbc/0xe0
 ---[ end trace 3669c914fa8ccac6 ]---

AFAICT this is because inode buffers are 32K on this system

Quoted 44 lines
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
#15 lore raw

Re: [PATCH 5/5] xfs: Convert kmem_alloc() users

Quoted 2 lines · Darrick J. Wong
Quoted 1 line · Carlos Maiolino

Use kmalloc() directly.

<snip all this>

Quoted 47 lines
Quoted 24 lines
fs/xfs/xfs_log_recover.c +3 -2
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)

fs/xfs/xfs_log_recover.c xlog_recover_add_to_trans( · line 4277

Quoted 7 lines
fs/xfs/xfs_log_recover.c +1 -1
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;

I noticed that kmalloc is generating warnings with generic/049 when 16k directories (-n size=16k) are enabled. I /think/ this is because it's quite possible to write out an xlog_op_header with a length of more than a single page; log recovery will then try to allocate a huge memory buffer to recover the transaction; and so we try to do a huge NOFAIL allocation, which makes the mm unhappy.

The one thing I've noticed with this conversion series is that the flags translation isn't 100% 1-to-1. Before, kmem_flags_convert didn't explicitly set __GFP_NOFAIL anywhere; we simply took the default behavior. IIRC that means that small allocations actually /are/ guaranteed to succeed, but multipage allocations certainly aren't. This seems to be one place where we could have asked for a lot of memory, failed to get it, and crashed.

Quoted 4 lines

Now that we explicitly set NOFAIL in all the places where we don't also check for a null return, I think we're just uncovering latent bugs lurking in the code base. The kernel does actually fulfill the allocation request, but it's clearly not happy.

FWIW I ran with various dirsizes and options and it looks like this is the only place where we screw this up... patches soon.

--D

Quoted 106 lines

--D

Relevant snippet of dmesg; everything else was normal:

XFS (sdd): Mounting V5 Filesystem
XFS (sdd): Starting recovery (logdev: internal)

 ------------[ cut here ]------------
 WARNING: CPU: 1 PID: 459342 at mm/page_alloc.c:3275 get_page_from_freelist+0x434/0x1660
 Modules linked in: dm_thin_pool dm_persistent_data dm_bio_prison dm_snapshot dm_bufio dm_flakey xfs libcrc32c ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 ip_set_hash_ip ip_set_hash_net xt_tcpudp xt_set ip_set_hash_mac bfq ip_set nfnetlink ip6table_filter ip6_tables iptable_filter sch_fq_codel ip_tables x_tables nfsv4 af_packet [last unloaded: scsi_debug]
 CPU: 1 PID: 459342 Comm: mount Not tainted 5.4.0-rc3-djw #rc3
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-1ubuntu1 04/01/2014
 RIP: 0010:get_page_from_freelist+0x434/0x1660
 Code: e6 00 00 00 00 48 89 84 24 a0 00 00 00 0f 84 08 fd ff ff f7 84 24 c0 00 00 00 00 80 00 00 74 0c 83 bc 24 84 00 00 00 01 76 02 <0f> 0b 49 8d 87 10 05 00 00 48 89 c7 48 89 84 24 88 00 00 00 e8 03
 RSP: 0018:ffffc900035d3918 EFLAGS: 00010202
 RAX: ffff88803fffb680 RBX: 0000000000002968 RCX: ffffea0000c8e108
 RDX: ffff88803fffbba8 RSI: ffff88803fffb870 RDI: 0000000000000000
 RBP: 0000000000000002 R08: 0000000000000201 R09: 000000000002ff81
 R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000
 R13: 0000000000048cc0 R14: 0000000000000001 R15: ffff88803fffb680
 FS:  00007fcfdf89a080(0000) GS:ffff88803ea00000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000055b202ec48c8 CR3: 000000003bfdc005 CR4: 00000000001606a0
 Call Trace:
  ? kvm_clock_read+0x14/0x30
  __alloc_pages_nodemask+0x172/0x3a0
  kmalloc_order+0x18/0x80
  kmalloc_order_trace+0x1d/0x130
  xlog_recover_add_to_trans+0x4b/0x340 [xfs]
  xlog_recovery_process_trans+0xe9/0xf0 [xfs]
  xlog_recover_process_data+0x9e/0x1f0 [xfs]
  xlog_do_recovery_pass+0x3a9/0x7c0 [xfs]
  xlog_do_log_recovery+0x72/0x150 [xfs]
  xlog_do_recover+0x43/0x2a0 [xfs]
  xlog_recover+0xdf/0x170 [xfs]
  xfs_log_mount+0x2e3/0x300 [xfs]
  xfs_mountfs+0x4e7/0x9f0 [xfs]
  xfs_fc_fill_super+0x2f8/0x520 [xfs]
  ? xfs_fs_destroy_inode+0x4f0/0x4f0 [xfs]
  get_tree_bdev+0x198/0x270
  vfs_get_tree+0x23/0xb0
  do_mount+0x87e/0xa20
  ksys_mount+0xb6/0xd0
  __x64_sys_mount+0x21/0x30
  do_syscall_64+0x50/0x180
  entry_SYSCALL_64_after_hwframe+0x49/0xbe
 RIP: 0033:0x7fcfdf15d3ca
 Code: 48 8b 0d c1 8a 2c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8e 8a 2c 00 f7 d8 64 89 01 48
 RSP: 002b:00007fff0af10a58 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
 RAX: ffffffffffffffda RBX: 000055b202ec1970 RCX: 00007fcfdf15d3ca
 RDX: 000055b202ec1be0 RSI: 000055b202ec1c20 RDI: 000055b202ec1c00
 RBP: 0000000000000000 R08: 000055b202ec1b80 R09: 0000000000000000
 R10: 00000000c0ed0000 R11: 0000000000000202 R12: 000055b202ec1c00
 R13: 000055b202ec1be0 R14: 0000000000000000 R15: 00007fcfdf67e8a4
 irq event stamp: 18398
 hardirqs last  enabled at (18397): [<ffffffff8123738f>] __slab_alloc.isra.83+0x6f/0x80
 hardirqs last disabled at (18398): [<ffffffff81001d8a>] trace_hardirqs_off_thunk+0x1a/0x20
 softirqs last  enabled at (18158): [<ffffffff81a003af>] __do_softirq+0x3af/0x4a4
 softirqs last disabled at (18149): [<ffffffff8106528c>] irq_exit+0xbc/0xe0
 ---[ end trace 3669c914fa8ccac6 ]---

AFAICT this is because inode buffers are 32K on this system

Quoted 44 lines
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
#16 lore raw

Re: [PATCH 5/5] xfs: Convert kmem_alloc() users

Quoted 53 lines · Darrick J. Wong
Quoted 2 lines · Darrick J. Wong
Quoted 1 line · Carlos Maiolino

Use kmalloc() directly.

<snip all this>

Quoted 47 lines
Quoted 24 lines
fs/xfs/xfs_log_recover.c +3 -2
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)

fs/xfs/xfs_log_recover.c xlog_recover_add_to_trans( · line 4277

Quoted 7 lines
fs/xfs/xfs_log_recover.c +1 -1
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;

I noticed that kmalloc is generating warnings with generic/049 when 16k directories (-n size=16k) are enabled. I /think/ this is because it's quite possible to write out an xlog_op_header with a length of more than a single page; log recovery will then try to allocate a huge memory buffer to recover the transaction; and so we try to do a huge NOFAIL allocation, which makes the mm unhappy.

The one thing I've noticed with this conversion series is that the flags translation isn't 100% 1-to-1. Before, kmem_flags_convert didn't explicitly set __GFP_NOFAIL anywhere; we simply took the default behavior. IIRC that means that small allocations actually /are/ guaranteed to succeed, but multipage allocations certainly aren't. This seems to be one place where we could have asked for a lot of memory, failed to get it, and crashed.

Quoted 7 lines
Quoted 4 lines

Now that we explicitly set NOFAIL in all the places where we don't also check for a null return, I think we're just uncovering latent bugs lurking in the code base. The kernel does actually fulfill the allocation request, but it's clearly not happy.

FWIW I ran with various dirsizes and options and it looks like this is the only place where we screw this up... patches soon.

I rescind that statement -- there's enough places in this series where I can't 100% tell that a k{mzre}alloc call asks for a small enough amount of memory to qualify for __GFP_NOFAIL.

I really want this cleanup to start with a straightforward removal of the kmem.c wrappers without any behavior changes. Only after that's done should we move on to things like adding __GFP_NOFAIL to allocations or deciding if/where we can substitute kfree for kvfree.

Munging them together means I can't easily tell if something is seriously broken here (but the WARN_ONs suggest this) and I'd forgotten that the merge window is opening the week of a major US holiday, so I choose to defer this series to 5.6.

--D

Quoted 108 lines

--D

Quoted 106 lines

--D

Relevant snippet of dmesg; everything else was normal:

XFS (sdd): Mounting V5 Filesystem
XFS (sdd): Starting recovery (logdev: internal)

 ------------[ cut here ]------------
 WARNING: CPU: 1 PID: 459342 at mm/page_alloc.c:3275 get_page_from_freelist+0x434/0x1660
 Modules linked in: dm_thin_pool dm_persistent_data dm_bio_prison dm_snapshot dm_bufio dm_flakey xfs libcrc32c ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 ip_set_hash_ip ip_set_hash_net xt_tcpudp xt_set ip_set_hash_mac bfq ip_set nfnetlink ip6table_filter ip6_tables iptable_filter sch_fq_codel ip_tables x_tables nfsv4 af_packet [last unloaded: scsi_debug]
 CPU: 1 PID: 459342 Comm: mount Not tainted 5.4.0-rc3-djw #rc3
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-1ubuntu1 04/01/2014
 RIP: 0010:get_page_from_freelist+0x434/0x1660
 Code: e6 00 00 00 00 48 89 84 24 a0 00 00 00 0f 84 08 fd ff ff f7 84 24 c0 00 00 00 00 80 00 00 74 0c 83 bc 24 84 00 00 00 01 76 02 <0f> 0b 49 8d 87 10 05 00 00 48 89 c7 48 89 84 24 88 00 00 00 e8 03
 RSP: 0018:ffffc900035d3918 EFLAGS: 00010202
 RAX: ffff88803fffb680 RBX: 0000000000002968 RCX: ffffea0000c8e108
 RDX: ffff88803fffbba8 RSI: ffff88803fffb870 RDI: 0000000000000000
 RBP: 0000000000000002 R08: 0000000000000201 R09: 000000000002ff81
 R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000
 R13: 0000000000048cc0 R14: 0000000000000001 R15: ffff88803fffb680
 FS:  00007fcfdf89a080(0000) GS:ffff88803ea00000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000055b202ec48c8 CR3: 000000003bfdc005 CR4: 00000000001606a0
 Call Trace:
  ? kvm_clock_read+0x14/0x30
  __alloc_pages_nodemask+0x172/0x3a0
  kmalloc_order+0x18/0x80
  kmalloc_order_trace+0x1d/0x130
  xlog_recover_add_to_trans+0x4b/0x340 [xfs]
  xlog_recovery_process_trans+0xe9/0xf0 [xfs]
  xlog_recover_process_data+0x9e/0x1f0 [xfs]
  xlog_do_recovery_pass+0x3a9/0x7c0 [xfs]
  xlog_do_log_recovery+0x72/0x150 [xfs]
  xlog_do_recover+0x43/0x2a0 [xfs]
  xlog_recover+0xdf/0x170 [xfs]
  xfs_log_mount+0x2e3/0x300 [xfs]
  xfs_mountfs+0x4e7/0x9f0 [xfs]
  xfs_fc_fill_super+0x2f8/0x520 [xfs]
  ? xfs_fs_destroy_inode+0x4f0/0x4f0 [xfs]
  get_tree_bdev+0x198/0x270
  vfs_get_tree+0x23/0xb0
  do_mount+0x87e/0xa20
  ksys_mount+0xb6/0xd0
  __x64_sys_mount+0x21/0x30
  do_syscall_64+0x50/0x180
  entry_SYSCALL_64_after_hwframe+0x49/0xbe
 RIP: 0033:0x7fcfdf15d3ca
 Code: 48 8b 0d c1 8a 2c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8e 8a 2c 00 f7 d8 64 89 01 48
 RSP: 002b:00007fff0af10a58 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
 RAX: ffffffffffffffda RBX: 000055b202ec1970 RCX: 00007fcfdf15d3ca
 RDX: 000055b202ec1be0 RSI: 000055b202ec1c20 RDI: 000055b202ec1c00
 RBP: 0000000000000000 R08: 000055b202ec1b80 R09: 0000000000000000
 R10: 00000000c0ed0000 R11: 0000000000000202 R12: 000055b202ec1c00
 R13: 000055b202ec1be0 R14: 0000000000000000 R15: 00007fcfdf67e8a4
 irq event stamp: 18398
 hardirqs last  enabled at (18397): [<ffffffff8123738f>] __slab_alloc.isra.83+0x6f/0x80
 hardirqs last disabled at (18398): [<ffffffff81001d8a>] trace_hardirqs_off_thunk+0x1a/0x20
 softirqs last  enabled at (18158): [<ffffffff81a003af>] __do_softirq+0x3af/0x4a4
 softirqs last disabled at (18149): [<ffffffff8106528c>] irq_exit+0xbc/0xe0
 ---[ end trace 3669c914fa8ccac6 ]---

AFAICT this is because inode buffers are 32K on this system

Quoted 44 lines
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
#17 lore raw

Re: [PATCH 5/5] xfs: Convert kmem_alloc() users

Quoted 71 lines · Darrick J. Wong
Quoted 53 lines · Darrick J. Wong
Quoted 2 lines · Darrick J. Wong
Quoted 1 line · Carlos Maiolino

Use kmalloc() directly.

<snip all this>

Quoted 47 lines
Quoted 24 lines
fs/xfs/xfs_log_recover.c +3 -2
index 5423171e0b7d..7bb53fbf32f6 100644
@@ -1962,7 +1962,7 @@ xlog_recover_buffer_pass1(
 		}
 	}
 
-	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
+	bcp = kmalloc(sizeof(struct xfs_buf_cancel), GFP_KERNEL | __GFP_NOFAIL);
 	bcp->bc_blkno = buf_f->blf_blkno;
 	bcp->bc_len = buf_f->blf_len;
 	bcp->bc_refcount = 1;
@@ -2932,7 +2932,8 @@ xlog_recover_inode_pass2(
 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
 		in_f = item->ri_buf[0].i_addr;
 	} else {
-		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
+		in_f = kmalloc(sizeof(struct xfs_inode_log_format),
+			       GFP_KERNEL | __GFP_NOFAIL);
 		need_free = 1;
 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
 		if (error)

fs/xfs/xfs_log_recover.c xlog_recover_add_to_trans( · line 4277

Quoted 7 lines
fs/xfs/xfs_log_recover.c +1 -1
@@ -4271,7 +4272,7 @@ xlog_recover_add_to_trans(
 		return 0;
 	}
 
-	ptr = kmem_alloc(len, 0);
+	ptr = kmalloc(len, GFP_KERNEL | __GFP_NOFAIL);
 	memcpy(ptr, dp, len);
 	in_f = (struct xfs_inode_log_format *)ptr;

I noticed that kmalloc is generating warnings with generic/049 when 16k directories (-n size=16k) are enabled. I /think/ this is because it's quite possible to write out an xlog_op_header with a length of more than a single page; log recovery will then try to allocate a huge memory buffer to recover the transaction; and so we try to do a huge NOFAIL allocation, which makes the mm unhappy.

The one thing I've noticed with this conversion series is that the flags translation isn't 100% 1-to-1. Before, kmem_flags_convert didn't explicitly set __GFP_NOFAIL anywhere; we simply took the default behavior. IIRC that means that small allocations actually /are/ guaranteed to succeed, but multipage allocations certainly aren't. This seems to be one place where we could have asked for a lot of memory, failed to get it, and crashed.

Quoted 7 lines
Quoted 4 lines

Now that we explicitly set NOFAIL in all the places where we don't also check for a null return, I think we're just uncovering latent bugs lurking in the code base. The kernel does actually fulfill the allocation request, but it's clearly not happy.

FWIW I ran with various dirsizes and options and it looks like this is the only place where we screw this up... patches soon.

I rescind that statement -- there's enough places in this series where I can't 100% tell that a k{mzre}alloc call asks for a small enough amount of memory to qualify for __GFP_NOFAIL.

I really want this cleanup to start with a straightforward removal of the kmem.c wrappers without any behavior changes. Only after that's done should we move on to things like adding __GFP_NOFAIL to allocations or deciding if/where we can substitute kfree for kvfree.

Quoted 4 lines

Munging them together means I can't easily tell if something is seriously broken here (but the WARN_ONs suggest this) and I'd forgotten that the merge window is opening the week of a major US holiday, so I choose to defer this series to 5.6.

Thanks, this is the best option IMO too. I'll think a bit about it and check what should we do about it.

Quoted 112 lines

--D

Quoted 108 lines

--D

Quoted 106 lines

--D

Relevant snippet of dmesg; everything else was normal:

XFS (sdd): Mounting V5 Filesystem
XFS (sdd): Starting recovery (logdev: internal)

 ------------[ cut here ]------------
 WARNING: CPU: 1 PID: 459342 at mm/page_alloc.c:3275 get_page_from_freelist+0x434/0x1660
 Modules linked in: dm_thin_pool dm_persistent_data dm_bio_prison dm_snapshot dm_bufio dm_flakey xfs libcrc32c ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 ip_set_hash_ip ip_set_hash_net xt_tcpudp xt_set ip_set_hash_mac bfq ip_set nfnetlink ip6table_filter ip6_tables iptable_filter sch_fq_codel ip_tables x_tables nfsv4 af_packet [last unloaded: scsi_debug]
 CPU: 1 PID: 459342 Comm: mount Not tainted 5.4.0-rc3-djw #rc3
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-1ubuntu1 04/01/2014
 RIP: 0010:get_page_from_freelist+0x434/0x1660
 Code: e6 00 00 00 00 48 89 84 24 a0 00 00 00 0f 84 08 fd ff ff f7 84 24 c0 00 00 00 00 80 00 00 74 0c 83 bc 24 84 00 00 00 01 76 02 <0f> 0b 49 8d 87 10 05 00 00 48 89 c7 48 89 84 24 88 00 00 00 e8 03
 RSP: 0018:ffffc900035d3918 EFLAGS: 00010202
 RAX: ffff88803fffb680 RBX: 0000000000002968 RCX: ffffea0000c8e108
 RDX: ffff88803fffbba8 RSI: ffff88803fffb870 RDI: 0000000000000000
 RBP: 0000000000000002 R08: 0000000000000201 R09: 000000000002ff81
 R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000
 R13: 0000000000048cc0 R14: 0000000000000001 R15: ffff88803fffb680
 FS:  00007fcfdf89a080(0000) GS:ffff88803ea00000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000055b202ec48c8 CR3: 000000003bfdc005 CR4: 00000000001606a0
 Call Trace:
  ? kvm_clock_read+0x14/0x30
  __alloc_pages_nodemask+0x172/0x3a0
  kmalloc_order+0x18/0x80
  kmalloc_order_trace+0x1d/0x130
  xlog_recover_add_to_trans+0x4b/0x340 [xfs]
  xlog_recovery_process_trans+0xe9/0xf0 [xfs]
  xlog_recover_process_data+0x9e/0x1f0 [xfs]
  xlog_do_recovery_pass+0x3a9/0x7c0 [xfs]
  xlog_do_log_recovery+0x72/0x150 [xfs]
  xlog_do_recover+0x43/0x2a0 [xfs]
  xlog_recover+0xdf/0x170 [xfs]
  xfs_log_mount+0x2e3/0x300 [xfs]
  xfs_mountfs+0x4e7/0x9f0 [xfs]
  xfs_fc_fill_super+0x2f8/0x520 [xfs]
  ? xfs_fs_destroy_inode+0x4f0/0x4f0 [xfs]
  get_tree_bdev+0x198/0x270
  vfs_get_tree+0x23/0xb0
  do_mount+0x87e/0xa20
  ksys_mount+0xb6/0xd0
  __x64_sys_mount+0x21/0x30
  do_syscall_64+0x50/0x180
  entry_SYSCALL_64_after_hwframe+0x49/0xbe
 RIP: 0033:0x7fcfdf15d3ca
 Code: 48 8b 0d c1 8a 2c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8e 8a 2c 00 f7 d8 64 89 01 48
 RSP: 002b:00007fff0af10a58 EFLAGS: 00000202 ORIG_RAX: 00000000000000a5
 RAX: ffffffffffffffda RBX: 000055b202ec1970 RCX: 00007fcfdf15d3ca
 RDX: 000055b202ec1be0 RSI: 000055b202ec1c20 RDI: 000055b202ec1c00
 RBP: 0000000000000000 R08: 000055b202ec1b80 R09: 0000000000000000
 R10: 00000000c0ed0000 R11: 0000000000000202 R12: 000055b202ec1c00
 R13: 000055b202ec1be0 R14: 0000000000000000 R15: 00007fcfdf67e8a4
 irq event stamp: 18398
 hardirqs last  enabled at (18397): [<ffffffff8123738f>] __slab_alloc.isra.83+0x6f/0x80
 hardirqs last disabled at (18398): [<ffffffff81001d8a>] trace_hardirqs_off_thunk+0x1a/0x20
 softirqs last  enabled at (18158): [<ffffffff81a003af>] __do_softirq+0x3af/0x4a4
 softirqs last disabled at (18149): [<ffffffff8106528c>] irq_exit+0xbc/0xe0
 ---[ end trace 3669c914fa8ccac6 ]---

AFAICT this is because inode buffers are 32K on this system

Quoted 44 lines
fs/xfs/xfs_qm.c +2 -1
index a2664afa10c3..2993af4a9935 100644
@@ -988,7 +988,8 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_d.di_nblocks == 0)
 		return 0;
 
-	map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
+	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+		      GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
 	maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
fs/xfs/xfs_rtalloc.c +1 -1
index 7f03b4ab3452..dfd419d402ea 100644
@@ -962,7 +962,7 @@ xfs_growfs_rt(
 	/*
 	 * Allocate a new (fake) mount/sb.
 	 */
-	nmp = kmem_alloc(sizeof(*nmp), 0);
+	nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
 	/*
 	 * Loop over the bitmap blocks.
 	 * We will do everything one bitmap block at a time.
fs/xfs/xfs_super.c +1 -1
index cc1933dc652f..eee831681e9c 100644
@@ -1739,7 +1739,7 @@ static int xfs_init_fs_context(
 {
 	struct xfs_mount	*mp;
 
-	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
+	mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL | __GFP_NOFAIL);
 	if (!mp)
 		return -ENOMEM;
 
Signature · 1 line
2.23.0
Signature · 1 line
Carlos
#18 lore raw
In reply to Carlos Maiolino
Quoted 25 lines · Carlos Maiolino

Hi,

in this new series, we remove most of the remaining kmem_* wrappers.

All of the wrappers being removed in this series can be directly replaced by generic kernel kmalloc()/kzalloc() interface.

Only interface kept is kmem_alloc() but has been converted into a local helper.

This series should be applied on top of my previous series aiming to clean up our memory allocation interface.

Darrick, I believe this is slightly different from what you suggested previously, about converting kmem_* interfaces to use GFP flags directly. At least I read that as keeping current kmem_* interface, and getting rid of KM_* flags now.

But, I believe these patches does not change any allocation logic, and after the series we are left with fewer users of KM_* flags users to get rid of, which IMHO will be easier. And also I already had the patches mostly done :)

Let me know if this is ok for you.

Quoted 6 lines

Carlos Maiolino (5):
xfs: remove kmem_zone_zalloc()
xfs: Remove kmem_zone_alloc() wrapper
xfs: remove kmem_zalloc() wrapper
xfs: Remove kmem_realloc
xfs: Convert kmem_alloc() users

Hmmm, just noticed that this never got merged. Whatever happened to this patchset?

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]