1 jbd2: fix potential double free
3 From: Chengguang Xu <cgxu519@gmail.com>
5 When failing from creating cache jbd2_inode_cache, we will destroy the
6 previously created cache jbd2_handle_cache twice. This patch fixes
7 this by moving each cache initialization/destruction to its own
8 separate, individual function.
10 Signed-off-by: Chengguang Xu <cgxu519@gmail.com>
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 - Seperate cache initialization/destruction to individual function.
17 fs/jbd2/journal.c | 51 +++++++++++++++++++++++++++----------------
18 fs/jbd2/revoke.c | 32 +++++++++++++++++----------
19 fs/jbd2/transaction.c | 8 ++++---
20 include/linux/jbd2.h | 8 ++++---
21 4 files changed, 62 insertions(+), 37 deletions(-)
23 diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
24 index 37e16d969925..0f1ac43d0560 100644
25 --- a/fs/jbd2/journal.c
26 +++ b/fs/jbd2/journal.c
27 @@ -2375,22 +2375,19 @@ static struct kmem_cache *jbd2_journal_head_cache;
28 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
31 -static int jbd2_journal_init_journal_head_cache(void)
32 +static int __init jbd2_journal_init_journal_head_cache(void)
36 - J_ASSERT(jbd2_journal_head_cache == NULL);
37 + J_ASSERT(!jbd2_journal_head_cache);
38 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
39 sizeof(struct journal_head),
41 SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU,
44 if (!jbd2_journal_head_cache) {
46 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
53 static void jbd2_journal_destroy_journal_head_cache(void)
54 @@ -2636,28 +2633,38 @@ static void __exit jbd2_remove_jbd_stats_proc_entry(void)
56 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
58 +static int __init jbd2_journal_init_inode_cache(void)
60 + J_ASSERT(!jbd2_inode_cache);
61 + jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
62 + if (!jbd2_inode_cache) {
63 + pr_emerg("JBD2: failed to create inode cache\n");
69 static int __init jbd2_journal_init_handle_cache(void)
71 + J_ASSERT(!jbd2_handle_cache);
72 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
73 - if (jbd2_handle_cache == NULL) {
74 + if (!jbd2_handle_cache) {
75 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
78 - jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
79 - if (jbd2_inode_cache == NULL) {
80 - printk(KERN_EMERG "JBD2: failed to create inode cache\n");
81 - kmem_cache_destroy(jbd2_handle_cache);
87 +static void jbd2_journal_destroy_inode_cache(void)
89 + kmem_cache_destroy(jbd2_inode_cache);
90 + jbd2_inode_cache = NULL;
93 static void jbd2_journal_destroy_handle_cache(void)
95 kmem_cache_destroy(jbd2_handle_cache);
96 jbd2_handle_cache = NULL;
97 - kmem_cache_destroy(jbd2_inode_cache);
98 - jbd2_inode_cache = NULL;
102 @@ -2668,11 +2675,15 @@ static int __init journal_init_caches(void)
106 - ret = jbd2_journal_init_revoke_caches();
107 + ret = jbd2_journal_init_revoke_record_cache();
109 + ret = jbd2_journal_init_revoke_table_cache();
111 ret = jbd2_journal_init_journal_head_cache();
113 ret = jbd2_journal_init_handle_cache();
115 + ret = jbd2_journal_init_inode_cache();
117 ret = jbd2_journal_init_transaction_cache();
119 @@ -2680,9 +2691,11 @@ static int __init journal_init_caches(void)
121 static void jbd2_journal_destroy_caches(void)
123 - jbd2_journal_destroy_revoke_caches();
124 + jbd2_journal_destroy_revoke_record_cache();
125 + jbd2_journal_destroy_revoke_table_cache();
126 jbd2_journal_destroy_journal_head_cache();
127 jbd2_journal_destroy_handle_cache();
128 + jbd2_journal_destroy_inode_cache();
129 jbd2_journal_destroy_transaction_cache();
130 jbd2_journal_destroy_slabs();
132 diff --git a/fs/jbd2/revoke.c b/fs/jbd2/revoke.c
133 index a1143e57a718..69b9bc329964 100644
134 --- a/fs/jbd2/revoke.c
135 +++ b/fs/jbd2/revoke.c
136 @@ -178,33 +178,41 @@ static struct jbd2_revoke_record_s *find_revoke_record(journal_t *journal,
140 -void jbd2_journal_destroy_revoke_caches(void)
141 +void jbd2_journal_destroy_revoke_record_cache(void)
143 kmem_cache_destroy(jbd2_revoke_record_cache);
144 jbd2_revoke_record_cache = NULL;
147 +void jbd2_journal_destroy_revoke_table_cache(void)
149 kmem_cache_destroy(jbd2_revoke_table_cache);
150 jbd2_revoke_table_cache = NULL;
153 -int __init jbd2_journal_init_revoke_caches(void)
154 +int __init jbd2_journal_init_revoke_record_cache(void)
156 J_ASSERT(!jbd2_revoke_record_cache);
157 - J_ASSERT(!jbd2_revoke_table_cache);
159 jbd2_revoke_record_cache = KMEM_CACHE(jbd2_revoke_record_s,
160 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY);
161 - if (!jbd2_revoke_record_cache)
162 - goto record_cache_failure;
164 + if (!jbd2_revoke_record_cache) {
165 + pr_emerg("JBD2: failed to create revoke_record cache\n");
171 +int __init jbd2_journal_init_revoke_table_cache(void)
173 + J_ASSERT(!jbd2_revoke_table_cache);
174 jbd2_revoke_table_cache = KMEM_CACHE(jbd2_revoke_table_s,
176 - if (!jbd2_revoke_table_cache)
177 - goto table_cache_failure;
179 -table_cache_failure:
180 - jbd2_journal_destroy_revoke_caches();
181 -record_cache_failure:
182 + if (!jbd2_revoke_table_cache) {
183 + pr_emerg("JBD2: failed to create revoke_table cache\n");
189 static struct jbd2_revoke_table_s *jbd2_journal_init_revoke_table(int hash_size)
190 diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
191 index f940d31c2adc..8ca4fddc705f 100644
192 --- a/fs/jbd2/transaction.c
193 +++ b/fs/jbd2/transaction.c
194 @@ -42,9 +42,11 @@ int __init jbd2_journal_init_transaction_cache(void)
196 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
198 - if (transaction_cache)
201 + if (!transaction_cache) {
202 + pr_emerg("JBD2: failed to create transaction cache\n");
208 void jbd2_journal_destroy_transaction_cache(void)
209 diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
210 index c2ffff5f9ae2..6c9870e16b19 100644
211 --- a/include/linux/jbd2.h
212 +++ b/include/linux/jbd2.h
213 @@ -1318,7 +1318,7 @@ extern void __wait_on_journal (journal_t *);
215 /* Transaction cache support */
216 extern void jbd2_journal_destroy_transaction_cache(void);
217 -extern int jbd2_journal_init_transaction_cache(void);
218 +extern int __init jbd2_journal_init_transaction_cache(void);
219 extern void jbd2_journal_free_transaction(transaction_t *);
222 @@ -1446,8 +1446,10 @@ static inline void jbd2_free_inode(struct jbd2_inode *jinode)
223 /* Primary revoke support */
224 #define JOURNAL_REVOKE_DEFAULT_HASH 256
225 extern int jbd2_journal_init_revoke(journal_t *, int);
226 -extern void jbd2_journal_destroy_revoke_caches(void);
227 -extern int jbd2_journal_init_revoke_caches(void);
228 +extern void jbd2_journal_destroy_revoke_record_cache(void);
229 +extern void jbd2_journal_destroy_revoke_table_cache(void);
230 +extern int __init jbd2_journal_init_revoke_record_cache(void);
231 +extern int __init jbd2_journal_init_revoke_table_cache(void);
233 extern void jbd2_journal_destroy_revoke(journal_t *);
234 extern int jbd2_journal_revoke (handle_t *, unsigned long long, struct buffer_head *);