Skip to contentSkip to conversation

btt: fix uninitialized err_lock

PATCH In review Jeff Moyer 2 participants 3 messages Dec 13, 2017 – Jan 15, 2018

Subsystems (2)
lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

PATCH 1/1 btt: fix uninitialized err_lock

Hi,

When a sector mode namespace is initially created, the arena's err_lock is not initialized. If, on the other hand, the namespace already exists, the mutex is initialized. To fix the issue, I moved the mutex initialization into the arena_alloc, which is called by both discover_arenas and create_arenas.

This was discovered on an older kernel where mutex_trylock checks the count to determine whether the lock is held. Because the data structure is kzalloc-d, that count was 0 (held), and I/O to the device would hang forever waiting for the lock to be released (see btt_write_pg, for example). Current kernels have a different mutex implementation that checks for a non-null owner, and so this doesn't show up as a problem. If that lock were ever contended, it might cause issues, but you'd have to be really unlucky, I think.

drivers/nvdimm/btt.c +1 -1
index e949e33..5860f99 100644
@@ -630,6 +630,7 @@ static struct arena_info *alloc_arena(struct btt *btt, size_t size,
 		return NULL;
 	arena->nd_btt = btt->nd_btt;
 	arena->sector_size = btt->sector_size;
+	mutex_init(&arena->err_lock);
 
 	if (!size)
 		return arena;
@@ -758,7 +759,6 @@ static int discover_arenas(struct btt *btt)
 		arena->external_lba_start = cur_nlba;
 		parse_arena_meta(arena, super, cur_off);
 
-		mutex_init(&arena->err_lock);
 		ret = btt_freelist_init(arena);
 		if (ret)
 			goto out;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Jeff Moyer

Hi,

When a sector mode namespace is initially created, the arena's err_lock
is not initialized. If, on the other hand, the namespace already exists, the mutex is initialized. To fix the issue, I moved the mutex
initialization into the arena_alloc, which is called by both discover_arenas and create_arenas.

This was discovered on an older kernel where mutex_trylock checks the count to determine whether the lock is held. Because the data structure
is kzalloc-d, that count was 0 (held), and I/O to the device would hang
forever waiting for the lock to be released (see btt_write_pg, for example). Current kernels have a different mutex implementation that checks for a non-null owner, and so this doesn't show up as a problem.
If that lock were ever contended, it might cause issues, but you'd have
to be really unlucky, I think.

Quoted 1 line

Ah, good find - the fix looks good to me.

Quoted 15 lines
drivers/nvdimm/btt.c +1 -0
index e949e33..5860f99 100644
@@ -630,6 +630,7 @@ static struct arena_info *alloc_arena(struct btt
 *btt, size_t size,
 		return NULL;
 	arena->nd_btt = btt->nd_btt;
 	arena->sector_size = btt->sector_size;
+	mutex_init(&arena->err_lock);
 
 	if (!size)
drivers/nvdimm/btt.c +0 -0
 		return arena;

drivers/nvdimm/btt.c static int discover_arenas(struct btt *btt) · line 764

Quoted 7 lines
drivers/nvdimm/btt.c +0 -1
@@ -758,7 +759,6 @@ static int discover_arenas(struct btt *btt)
 		arena->external_lba_start = cur_nlba;
 		parse_arena_meta(arena, super, cur_off);
 
-		mutex_init(&arena->err_lock);
 		ret = btt_freelist_init(arena);
 		if (ret)
 			goto out;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author
#2 lore raw

Dan,

Any chance you can pull this one in? (I know you've been busy.)

-Jeff

Quoted 47 lines · Vishal L
Quoted 23 lines · Jeff Moyer

Hi,

When a sector mode namespace is initially created, the arena's err_lock
is not initialized. If, on the other hand, the namespace already exists, the mutex is initialized. To fix the issue, I moved the mutex
initialization into the arena_alloc, which is called by both discover_arenas and create_arenas.

This was discovered on an older kernel where mutex_trylock checks the count to determine whether the lock is held. Because the data structure
is kzalloc-d, that count was 0 (held), and I/O to the device would hang
forever waiting for the lock to be released (see btt_write_pg, for example). Current kernels have a different mutex implementation that checks for a non-null owner, and so this doesn't show up as a problem.
If that lock were ever contended, it might cause issues, but you'd have
to be really unlucky, I think.

Quoted 1 line

Ah, good find - the fix looks good to me.

Quoted 18 lines
drivers/nvdimm/btt.c +1 -0
index e949e33..5860f99 100644
@@ -630,6 +630,7 @@ static struct arena_info *alloc_arena(struct btt
 *btt, size_t size,
 		return NULL;
 	arena->nd_btt = btt->nd_btt;
 	arena->sector_size = btt->sector_size;
+	mutex_init(&arena->err_lock);
 
 	if (!size)
drivers/nvdimm/btt.c +0 -0
 		return arena;
@@ -758,7 +759,6 @@ static int discover_arenas(struct btt *btt)
 		arena->external_lba_start = cur_nlba;
 		parse_arena_meta(arena, super, cur_off);
 
Quoted 4 lines

drivers/nvdimm/btt.c static int discover_arenas(struct btt *btt) · line 764

Quoted 4 lines
drivers/nvdimm/btt.c +0 -1
-		mutex_init(&arena->err_lock);
 		ret = btt_freelist_init(arena);
 		if (ret)
 			goto out;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm