xfs: free the item in xfs_mru_cache_insert on failure
authorChristoph Hellwig <hch@lst.de>
Wed, 14 May 2025 10:50:37 +0000 (10:50 +0000)
committerCarlos Maiolino <cem@kernel.org>
Wed, 14 May 2025 17:00:12 +0000 (19:00 +0200)
Call the provided free_func when xfs_mru_cache_insert as that's what
the callers need to do anyway.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Hans Holmberg <hans.holmberg@wdc.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
fs/xfs/xfs_filestream.c
fs/xfs/xfs_mru_cache.c

index a961aa420c488bb194a49230935b6d496470efd1..044918fbae06e86da6d61419af3adaf8abeacb11 100644 (file)
@@ -304,11 +304,9 @@ xfs_filestream_create_association(
         * for us, so all we need to do here is take another active reference to
         * the perag for the cached association.
         *
-        * If we fail to store the association, we need to drop the fstrms
-        * counter as well as drop the perag reference we take here for the
-        * item. We do not need to return an error for this failure - as long as
-        * we return a referenced AG, the allocation can still go ahead just
-        * fine.
+        * If we fail to store the association, we do not need to return an
+        * error for this failure - as long as we return a referenced AG, the
+        * allocation can still go ahead just fine.
         */
        item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
        if (!item)
@@ -316,14 +314,9 @@ xfs_filestream_create_association(
 
        atomic_inc(&pag_group(args->pag)->xg_active_ref);
        item->pag = args->pag;
-       error = xfs_mru_cache_insert(mp->m_filestream, pino, &item->mru);
-       if (error)
-               goto out_free_item;
+       xfs_mru_cache_insert(mp->m_filestream, pino, &item->mru);
        return 0;
 
-out_free_item:
-       xfs_perag_rele(item->pag);
-       kfree(item);
 out_put_fstrms:
        atomic_dec(&args->pag->pagf_fstrms);
        return 0;
index d0f5b403bdbebc948048e42cd0958b188c62ed90..08443ceec329de0e358f6130313be4d8e184d8c4 100644 (file)
@@ -414,6 +414,8 @@ xfs_mru_cache_destroy(
  * To insert an element, call xfs_mru_cache_insert() with the data store, the
  * element's key and the client data pointer.  This function returns 0 on
  * success or ENOMEM if memory for the data element couldn't be allocated.
+ *
+ * The passed in elem is freed through the per-cache free_func on failure.
  */
 int
 xfs_mru_cache_insert(
@@ -421,14 +423,15 @@ xfs_mru_cache_insert(
        unsigned long           key,
        struct xfs_mru_cache_elem *elem)
 {
-       int                     error;
+       int                     error = -EINVAL;
 
        ASSERT(mru && mru->lists);
        if (!mru || !mru->lists)
-               return -EINVAL;
+               goto out_free;
 
+       error = -ENOMEM;
        if (radix_tree_preload(GFP_KERNEL))
-               return -ENOMEM;
+               goto out_free;
 
        INIT_LIST_HEAD(&elem->list_node);
        elem->key = key;
@@ -440,6 +443,12 @@ xfs_mru_cache_insert(
                _xfs_mru_cache_list_insert(mru, elem);
        spin_unlock(&mru->lock);
 
+       if (error)
+               goto out_free;
+       return 0;
+
+out_free:
+       mru->free_func(mru->data, elem);
        return error;
 }