Skip to contentSkip to conversation

Fix segfault when destroying in-use filters in pvs -a.

PATCH New Milan Broz 2 participants 4 messages Mar 10, 2010 – Mar 15, 2010

lore
Share & tools
lore

Milan Broz

author
#0 lore raw

PATCH 1/1 Fix segfault when destroying in-use filters in pvs -a.

Because pvs -a iterate throught the whole device cache we should not refresh filters inside iteration.

Fix it by introducing simple reference counter and test it in refresh_filters() call.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=571963

---
 lib/commands/toolcontext.c      |    5 +++++
 lib/device/dev-cache.c          |    2 ++
 lib/device/dev-cache.h          |    1 +
 lib/filters/filter-composite.c  |    1 +
 lib/filters/filter-md.c         |    1 +
 lib/filters/filter-persistent.c |    1 +
 lib/filters/filter-regex.c      |    1 +
 lib/filters/filter-sysfs.c      |    1 +
 lib/filters/filter.c            |    1 +
 9 files changed, 14 insertions(+), 0 deletions(-)
lib/commands/toolcontext.c +5 -0
index 7aac361..a78efe8 100644
@@ -1240,6 +1240,11 @@ int refresh_filters(struct cmd_context *cmd)
 {
 	int r, saved_ignore_suspended_devices = ignore_suspended_devices();
 
+	if (cmd->filter && cmd->filter->in_use) {
+		log_debug("Skipping filter refresh currently in use.");
+		return 1;
+	}
+
 	if (cmd->filter) {
 		cmd->filter->destroy(cmd->filter);
 		cmd->filter = NULL;
lib/device/dev-cache.c +2 -0
index 45c0e8f..933d83a 100644
@@ -743,12 +743,14 @@ struct dev_iter *dev_iter_create(struct dev_filter *f, int dev_scan)
 
 	di->current = btree_first(_cache.devices);
 	di->filter = f;
+	di->filter->in_use++;
 
 	return di;
 }
 
 void dev_iter_destroy(struct dev_iter *iter)
 {
+	iter->filter->in_use--;
 	dm_free(iter);
 }
 
lib/device/dev-cache.h +1 -0
index 96bf013..9314a5c 100644
@@ -24,6 +24,7 @@
 struct dev_filter {
 	int (*passes_filter) (struct dev_filter * f, struct device * dev);
 	void (*destroy) (struct dev_filter * f);
+	unsigned in_use;
 	void *private;
 };
 
lib/filters/filter-composite.c +1 -0
index d1606d3..55aedaf 100644
@@ -70,6 +70,7 @@ struct dev_filter *composite_filter_create(int n, struct dev_filter **filters)
 	cft->passes_filter = _and_p;
 	cft->destroy = _composite_destroy;
 	cft->private = filters_copy;
+	cft->in_use = 0;
 
 	return cft;
 }
lib/filters/filter-md.c +1 -0
index c1ecff7..0e9857d 100644
@@ -60,6 +60,7 @@ struct dev_filter *md_filter_create(void)
 	f->passes_filter = _ignore_md;
 	f->destroy = _destroy;
 	f->private = NULL;
+	f->in_use = 0;
 
 	return f;
 }
lib/filters/filter-persistent.c +1 -0
index 9243a94..3c7639c 100644
@@ -317,6 +317,7 @@ struct dev_filter *persistent_filter_create(struct dev_filter *real,
 	f->passes_filter = _lookup_p;
 	f->destroy = _persistent_destroy;
 	f->private = pf;
+	f->in_use = 0;
 
 	return f;
 
lib/filters/filter-regex.c +1 -0
index 1d415a4..7903314 100644
@@ -207,6 +207,7 @@ struct dev_filter *regex_filter_create(struct config_value *patterns)
 	f->passes_filter = _accept_p;
 	f->destroy = _regex_destroy;
 	f->private = rf;
+	f->in_use = 0;
 	return f;
 
       bad:
lib/filters/filter-sysfs.c +1 -0
index 1220b3a..30e29b3 100644
@@ -317,6 +317,7 @@ struct dev_filter *sysfs_filter_create(const char *sysfs_dir)
 	f->passes_filter = _accept_p;
 	f->destroy = _destroy;
 	f->private = ds;
+	f->in_use = 0;
 	return f;
 
  bad:
lib/filters/filter.c +1 -0
index b33d099..7e3edfe 100644
@@ -319,6 +319,7 @@ struct dev_filter *lvm_type_filter_create(const char *proc,
 	f->passes_filter = _passes_lvm_type_device_filter;
 	f->destroy = lvm_type_filter_destroy;
 	f->private = NULL;
+	f->in_use = 0;
 
 	if (!_scan_proc_dev(proc, cn)) {
 		dm_free(f);
Signature · 1 line
1.7.0
#1 lore raw

PATCH 1/1 Fix segfault when destroying in-use filters in pvs -a.

Quoted 7 lines · Milan Broz

Because pvs -a iterate throught the whole device cache we should not refresh filters inside iteration.

Fix it by introducing simple reference counter and test it in refresh_filters() call.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=571963

I've tested this patch. By a pvs command with this patch, no segfault occurred on the same test cases which caused segfault.

Thanks,
Taka

#2 lore raw

PATCH 1/1 Fix segfault when destroying in-use filters in pvs -a.

Milan,

Quoted 8 lines · Takahiro Yasui
Quoted 7 lines · Milan Broz

Because pvs -a iterate throught the whole device cache we should not refresh filters inside iteration.

Fix it by introducing simple reference counter and test it in refresh_filters() call.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=571963

This patch worked fine in my test case, but I have one question about your patch. I assume that you added refresh_filters() because it was necessary, but this patch prevents filter from being refreshed if it is in use.

Don't we need to refresh the filter when in_use flag is decremented and becomes 0 after or in dev_iter_destroy()?

Thanks,
Taka

author
#3 lore raw

PATCH 1/1 Fix segfault when destroying in-use filters in pvs -a.

Quoted 5 lines · Takahiro Yasui
Quoted 4 lines
Quoted 4 lines

Fix it by introducing simple reference counter and test it in refresh_filters() call.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=571963

Quoted 7 lines

This patch worked fine in my test case, but I have one question about your patch. I assume that you added refresh_filters() because it was necessary, but this patch prevents filter from being refreshed if it is in use.

Don't we need to refresh the filter when in_use flag is decremented and becomes 0 after or in dev_iter_destroy()?

well, the patch is workaround. It should be correct fot _this_ one place.

The full refresh (including filter refresh) should be done in scan_vgs_for_pvs() (before the iteration loop) so later refresh should not be needed.
(So we should fix code to not full scan here in the first place...)

I see several more problems with repeated full refresh now, so maybe some more intrusive solution will be needed.

Milan