Skip to contentSkip to conversation

dm snapshot: remove usage of list iterator for list_add() after the loop body

PATCH Superseded Jakob Koschel 1 participant 2 messages Mar 31, 2022 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dm snapshot: remove usage of list iterator for list_add() after the loop body

In preparation to limit the scope of a list iterator to the list traversal loop, use a dedicated pointer to point to the found element [1].

Before, the code implicitly used the head when no element was found when using &pos->list. Since the new variable is only set if an element was found, the list_add() is performed within the loop and only done after the loop if it is done on the list head directly.

---
 drivers/md/dm-snap.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/md/dm-snap.c +8 -4
index 0d336b5ec571..23386a6e67e7 100644
@@ -528,13 +528,17 @@ static int __validate_exception_handover(struct dm_snapshot *snap)
 
 static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
 {
-	struct dm_snapshot *l;
+	struct dm_snapshot *l = NULL, *iter;
 
 	/* Sort the list according to chunk size, largest-first smallest-last */
-	list_for_each_entry(l, &o->snapshots, list)
-		if (l->store->chunk_size < s->store->chunk_size)
+	list_for_each_entry(iter, &o->snapshots, list)
+		if (iter->store->chunk_size < s->store->chunk_size) {
+			l = iter;
+			list_add_tail(&s->list, &iter->list);
 			break;
-	list_add_tail(&s->list, &l->list);
+		}
+	if (!l)
+		list_add_tail(&s->list, &o->snapshots);
 }
 
 /*

base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275

Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 dm: replace usage of found with dedicated list iterator variable

To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body.

To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1].

This removes the need to use a found variable and simply checking if the variable was set, can determine if the break/goto was hit.

---
 drivers/md/dm-table.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/md/dm-table.c +5 -6
index 03541cfc2317..9cf87954c05a 100644
@@ -424,17 +424,16 @@ static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  */
 void dm_put_device(struct dm_target *ti, struct dm_dev *d)
 {
-	int found = 0;
 	struct list_head *devices = &ti->table->devices;
-	struct dm_dev_internal *dd;
+	struct dm_dev_internal *dd = NULL, *iter;
 
-	list_for_each_entry(dd, devices, list) {
-		if (dd->dm_dev == d) {
-			found = 1;
+	list_for_each_entry(iter, devices, list) {
+		if (iter->dm_dev == d) {
+			dd = iter;
 			break;
 		}
 	}
-	if (!found) {
+	if (!dd) {
 		DMWARN("%s: device %s not in table devices list",
 		       dm_device_name(ti->table->md), d->name);
 		return;
Signature · 1 line
2.25.1