[PATCH 1/2] Squashfs: add assert to cache entry refcount

From: Ajeet Yadav
Date: Fri Dec 23 2011 - 06:55:48 EST


squashfs_cache_put() function decrements refcount, if the given
function is incorrectly used, refcount can have negative value.

Add ASSERT to entry->refcount, enabled if CONFIG_SQUASHFS_DEBUGGER
is defined.

Signed-off-by: Ajeet Yadav <ajeet.yadav.77@xxxxxxxxx>
---
fs/squashfs/cache.c | 8 ++++----
fs/squashfs/squashfs_fs.h | 10 ++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/squashfs/cache.c b/fs/squashfs/cache.c
index f744be9..88d986a 100644
--- a/fs/squashfs/cache.c
+++ b/fs/squashfs/cache.c
@@ -58,7 +58,7 @@
#include "squashfs.h"

/*
- * Look-up block in cache, and increment usage count. If not in cache, read
+ * Look-up block in cache, and increment reference count. If not in
cache, read
* and decompress it from disk.
*/
struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
@@ -177,15 +177,15 @@ out:


/*
- * Release cache entry, once usage count is zero it can be reused.
+ * Release cache entry, once reference count is zero it can be reused.
*/
void squashfs_cache_put(struct squashfs_cache_entry *entry)
{
struct squashfs_cache *cache = entry->cache;

spin_lock(&cache->lock);
- entry->refcount--;
- if (entry->refcount == 0) {
+ ASSERT(entry->refcount);
+ if (!(--entry->refcount)) {
cache->unused++;
/*
* If there's any processes waiting for a block to become
diff --git a/fs/squashfs/squashfs_fs.h b/fs/squashfs/squashfs_fs.h
index e8e1464..db6cf53 100644
--- a/fs/squashfs/squashfs_fs.h
+++ b/fs/squashfs/squashfs_fs.h
@@ -463,4 +463,14 @@ struct squashfs_xattr_id_table {
__le32 unused;
};

+#ifdef CONFIG_SQUASHFS_DEBUGGER
+#define ASSERT(x) \
+ if (!(x)) { \
+ printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
+ __FILE__, __LINE__, #x); \
+ }
+#else
+#define ASSERT(x) do { } while (0)
+#endif
+
#endif
--
1.6.0.3
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/