add patch create-ext4_feat-kobject-dynamically
[ext4-patch-queue.git] / mbcache-fix-potential-double-count-when-removing-entry
blobbe65e819de9e6267f29f2b9796c3a75fac95b018
1 mbcache: fix potential double counting when removing entry
3 Entries are removed from the mb_cache entry in two places:
4 mb_cache_shrink() and mb_cache_entry_delete().  The mb_cache_shrink()
5 function finds the entry to delete via the cache->c_list pointer,
6 while mb_cache_entry_delete() finds the entry via the hash lists.
8 If the two functions race with each other, trying to delete an entry
9 at the same time, it's possible for cache->c_entry_count to get
10 decremented twice for that one entry.  Fix this by checking to see if
11 entry is still on the cache list before removing it and dropping
12 c_entry_count.
14 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 ---
16  fs/mbcache.c | 6 ++++--
17  1 file changed, 4 insertions(+), 2 deletions(-)
19 diff --git a/fs/mbcache.c b/fs/mbcache.c
20 index 49c5b25bfa8c..0851af5c1c3d 100644
21 --- a/fs/mbcache.c
22 +++ b/fs/mbcache.c
23 @@ -290,8 +290,10 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
24                         list_move_tail(&entry->e_list, &cache->c_list);
25                         continue;
26                 }
27 -               list_del_init(&entry->e_list);
28 -               cache->c_entry_count--;
29 +               if (!list_empty(&entry->e_list)) {
30 +                       list_del_init(&entry->e_list);
31 +                       cache->c_entry_count--;
32 +               }
33                 /*
34                  * We keep LRU list reference so that entry doesn't go away
35                  * from under us.