Skip to content

Commit b839dad

Browse files
AstralBobagruenba
authored andcommitted
gfs2: new slab for transactions
This patch adds a new slab for gfs2 transactions. That allows us to reduce kernel memory fragmentation, have better organization of data for analysis of vmcore dumps. A new centralized function is added to free the slab objects, and it exposes use-after-free by giving warnings if a transaction is freed while it still has bd elements attached to its buffers or ail lists. We make sure to initialize those transaction ail lists so we can check their integrity when freeing. At a later time, we should add a slab initialization function to make it more efficient, but for this initial patch I wanted to minimize the impact. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
1 parent cbcc89b commit b839dad

6 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎fs/gfs2/log.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "util.h"
3131
#include "dir.h"
3232
#include "trace_gfs2.h"
33+
#include "trans.h"
3334

3435
static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
3536

@@ -378,7 +379,7 @@ static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
378379
list_del(&tr->tr_list);
379380
gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
380381
gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
381-
kfree(tr);
382+
gfs2_trans_free(sdp, tr);
382383
}
383384

384385
spin_unlock(&sdp->sd_ail_lock);
@@ -863,14 +864,14 @@ static void ail_drain(struct gfs2_sbd *sdp)
863864
gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
864865
gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
865866
list_del(&tr->tr_list);
866-
kfree(tr);
867+
gfs2_trans_free(sdp, tr);
867868
}
868869
while (!list_empty(&sdp->sd_ail2_list)) {
869870
tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
870871
tr_list);
871872
gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
872873
list_del(&tr->tr_list);
873-
kfree(tr);
874+
gfs2_trans_free(sdp, tr);
874875
}
875876
spin_unlock(&sdp->sd_ail_lock);
876877
}
@@ -1008,7 +1009,7 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
10081009
trace_gfs2_log_flush(sdp, 0, flags);
10091010
up_write(&sdp->sd_log_flush_lock);
10101011

1011-
kfree(tr);
1012+
gfs2_trans_free(sdp, tr);
10121013
}
10131014

10141015
/**

‎fs/gfs2/main.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ static int __init init_gfs2_fs(void)
143143
if (!gfs2_qadata_cachep)
144144
goto fail_cachep7;
145145

146+
gfs2_trans_cachep = kmem_cache_create("gfs2_trans",
147+
sizeof(struct gfs2_trans),
148+
0, 0, NULL);
149+
if (!gfs2_trans_cachep)
150+
goto fail_cachep8;
151+
146152
error = register_shrinker(&gfs2_qd_shrinker);
147153
if (error)
148154
goto fail_shrinker;
@@ -194,6 +200,8 @@ static int __init init_gfs2_fs(void)
194200
fail_fs1:
195201
unregister_shrinker(&gfs2_qd_shrinker);
196202
fail_shrinker:
203+
kmem_cache_destroy(gfs2_trans_cachep);
204+
fail_cachep8:
197205
kmem_cache_destroy(gfs2_qadata_cachep);
198206
fail_cachep7:
199207
kmem_cache_destroy(gfs2_quotad_cachep);
@@ -236,6 +244,7 @@ static void __exit exit_gfs2_fs(void)
236244
rcu_barrier();
237245

238246
mempool_destroy(gfs2_page_pool);
247+
kmem_cache_destroy(gfs2_trans_cachep);
239248
kmem_cache_destroy(gfs2_qadata_cachep);
240249
kmem_cache_destroy(gfs2_quotad_cachep);
241250
kmem_cache_destroy(gfs2_rgrpd_cachep);

‎fs/gfs2/trans.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
3737
if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
3838
return -EROFS;
3939

40-
tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
40+
tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
4141
if (!tr)
4242
return -ENOMEM;
4343

@@ -67,7 +67,7 @@ int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
6767

6868
fail:
6969
sb_end_intwrite(sdp->sd_vfs);
70-
kfree(tr);
70+
kmem_cache_free(gfs2_trans_cachep, tr);
7171

7272
return error;
7373
}
@@ -95,7 +95,7 @@ void gfs2_trans_end(struct gfs2_sbd *sdp)
9595
if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
9696
gfs2_log_release(sdp, tr->tr_reserved);
9797
if (alloced) {
98-
kfree(tr);
98+
gfs2_trans_free(sdp, tr);
9999
sb_end_intwrite(sdp->sd_vfs);
100100
}
101101
return;
@@ -111,7 +111,7 @@ void gfs2_trans_end(struct gfs2_sbd *sdp)
111111

112112
gfs2_log_commit(sdp, tr);
113113
if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
114-
kfree(tr);
114+
gfs2_trans_free(sdp, tr);
115115
up_read(&sdp->sd_log_flush_lock);
116116

117117
if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
@@ -278,3 +278,14 @@ void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
278278
gfs2_log_unlock(sdp);
279279
}
280280

281+
void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
282+
{
283+
if (tr == NULL)
284+
return;
285+
286+
gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
287+
gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
288+
gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
289+
gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
290+
kmem_cache_free(gfs2_trans_cachep, tr);
291+
}

‎fs/gfs2/trans.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ extern void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh);
4242
extern void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh);
4343
extern void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd);
4444
extern void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len);
45+
extern void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr);
4546

4647
#endif /* __TRANS_DOT_H__ */

‎fs/gfs2/util.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
3232
struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
3333
struct kmem_cache *gfs2_quotad_cachep __read_mostly;
3434
struct kmem_cache *gfs2_qadata_cachep __read_mostly;
35+
struct kmem_cache *gfs2_trans_cachep __read_mostly;
3536
mempool_t *gfs2_page_pool __read_mostly;
3637

3738
void gfs2_assert_i(struct gfs2_sbd *sdp)

‎fs/gfs2/util.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ extern struct kmem_cache *gfs2_bufdata_cachep;
172172
extern struct kmem_cache *gfs2_rgrpd_cachep;
173173
extern struct kmem_cache *gfs2_quotad_cachep;
174174
extern struct kmem_cache *gfs2_qadata_cachep;
175+
extern struct kmem_cache *gfs2_trans_cachep;
175176
extern mempool_t *gfs2_page_pool;
176177
extern struct workqueue_struct *gfs2_control_wq;
177178

0 commit comments

Comments
 (0)