jbd2: allocate transaction from separate slab cache
authorYongqiang Yang <xiaoqiangnk@gmail.com>
Mon, 20 Feb 2012 22:53:02 +0000 (17:53 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 20 Feb 2012 22:53:02 +0000 (17:53 -0500)
There is normally only a handful of these active at any one time, but
putting them in a separate slab cache makes debugging memory
corruption problems easier.  Manish Katiyar also wanted this make it
easier to test memory failure scenarios in the jbd2 layer.

Cc: Manish Katiyar <mkatiyar@gmail.com>
Signed-off-by: Yongqiang Yang <xiaoqiangnk@gmail.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
fs/jbd2/checkpoint.c
fs/jbd2/commit.c
fs/jbd2/journal.c
fs/jbd2/transaction.c
include/linux/jbd2.h

index 453c9068b7d7a9d494d345a59d74a400863101d0..253e91890e71ce2e20e10ee9f0f3b015ae495c05 100644 (file)
@@ -722,7 +722,7 @@ int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
                                    transaction->t_tid, stats);
 
        __jbd2_journal_drop_transaction(journal, transaction);
-       kfree(transaction);
+       jbd2_journal_free_transaction(transaction);
 
        /* Just in case anybody was waiting for more transactions to be
            checkpointed... */
index 5069b84751509e65bb9689c153ac87e36c26b843..8adc5d460f5632cd17557b133ae29c9f7128f9e0 100644 (file)
@@ -1048,7 +1048,7 @@ restart_loop:
        jbd_debug(1, "JBD2: commit %d complete, head %d\n",
                  journal->j_commit_sequence, journal->j_tail_sequence);
        if (to_free)
-               kfree(commit_transaction);
+               jbd2_journal_free_transaction(commit_transaction);
 
        wake_up(&journal->j_wait_done_commit);
 }
index 93a595c69616b4ffc9f222aec8bd7266ea4bbbcd..aa8f5986f8da32041f381e67722c303a2c410b0a 100644 (file)
@@ -2361,6 +2361,8 @@ static int __init journal_init_caches(void)
                ret = journal_init_jbd2_journal_head_cache();
        if (ret == 0)
                ret = journal_init_handle_cache();
+       if (ret == 0)
+               ret = jbd2_journal_init_transaction_cache();
        return ret;
 }
 
@@ -2369,6 +2371,7 @@ static void jbd2_journal_destroy_caches(void)
        jbd2_journal_destroy_revoke_caches();
        jbd2_journal_destroy_jbd2_journal_head_cache();
        jbd2_journal_destroy_handle_cache();
+       jbd2_journal_destroy_transaction_cache();
        jbd2_journal_destroy_slabs();
 }
 
index 5265330625484b7475d3073e2d358ec2cefaf71a..d0a8b017b28137786d240d955aa4ef809e6769f7 100644 (file)
 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
 static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
 
+static struct kmem_cache *transaction_cache;
+int __init jbd2_journal_init_transaction_cache(void)
+{
+       J_ASSERT(!transaction_cache);
+       transaction_cache = kmem_cache_create("jbd2_transaction_s",
+                                       sizeof(transaction_t),
+                                       0,
+                                       SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
+                                       NULL);
+       if (transaction_cache)
+               return 0;
+       return -ENOMEM;
+}
+
+void jbd2_journal_destroy_transaction_cache(void)
+{
+       if (transaction_cache) {
+               kmem_cache_destroy(transaction_cache);
+               transaction_cache = NULL;
+       }
+}
+
+void jbd2_journal_free_transaction(transaction_t *transaction)
+{
+       if (unlikely(ZERO_OR_NULL_PTR(transaction)))
+               return;
+       kmem_cache_free(transaction_cache, transaction);
+}
+
 /*
  * jbd2_get_transaction: obtain a new transaction_t object.
  *
@@ -133,7 +162,8 @@ static int start_this_handle(journal_t *journal, handle_t *handle,
 
 alloc_transaction:
        if (!journal->j_running_transaction) {
-               new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
+               new_transaction = kmem_cache_alloc(transaction_cache,
+                                                  gfp_mask | __GFP_ZERO);
                if (!new_transaction) {
                        /*
                         * If __GFP_FS is not present, then we may be
@@ -162,7 +192,7 @@ repeat:
        if (is_journal_aborted(journal) ||
            (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
                read_unlock(&journal->j_state_lock);
-               kfree(new_transaction);
+               jbd2_journal_free_transaction(new_transaction);
                return -EROFS;
        }
 
@@ -284,7 +314,7 @@ repeat:
        read_unlock(&journal->j_state_lock);
 
        lock_map_acquire(&handle->h_lockdep_map);
-       kfree(new_transaction);
+       jbd2_journal_free_transaction(new_transaction);
        return 0;
 }
 
index 5557baefed60b5f5bb3dd0984bbbe7f2e0a4c8fc..46eef77e6ab836d44e509d34f447b999dcdbcceb 100644 (file)
@@ -1020,6 +1020,11 @@ jbd2_journal_write_metadata_buffer(transaction_t   *transaction,
 /* Transaction locking */
 extern void            __wait_on_journal (journal_t *);
 
+/* Transaction cache support */
+extern void jbd2_journal_destroy_transaction_cache(void);
+extern int  jbd2_journal_init_transaction_cache(void);
+extern void jbd2_journal_free_transaction(transaction_t *);
+
 /*
  * Journal locking.
  *
This page took 0.031087 seconds and 5 git commands to generate.