Skip to contentSkip to conversation

staging: android: ashmem: remove usage of list iterator after the loop body

PATCH In review Jakob Koschel 3 participants 4 messages Mar 31, 2022 – Nov 20, 2022 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: android: ashmem: remove usage of list iterator 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 head needs to be used explicitly if the variable is NULL.

---
 drivers/staging/android/ashmem.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/staging/android/ashmem.c +12 -9
index ddbde3f8430e..a1e245827cf6 100644
@@ -703,30 +703,33 @@ static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
 static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
 			struct ashmem_range **new_range)
 {
-	struct ashmem_range *range, *next;
+	struct ashmem_range *range = NULL, *iter, *next;
 	unsigned int purged = ASHMEM_NOT_PURGED;
 
 restart:
-	list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
+	list_for_each_entry_safe(iter, next, &asma->unpinned_list, unpinned) {
 		/* short circuit: this is our insertion point */
-		if (range_before_page(range, pgstart))
+		if (range_before_page(iter, pgstart)) {
+			range = iter;
 			break;
+		}
 
 		/*
 		 * The user can ask us to unpin pages that are already entirely
 		 * or partially pinned. We handle those two cases here.
 		 */
-		if (page_range_subsumed_by_range(range, pgstart, pgend))
+		if (page_range_subsumed_by_range(iter, pgstart, pgend))
 			return 0;
-		if (page_range_in_range(range, pgstart, pgend)) {
-			pgstart = min(range->pgstart, pgstart);
-			pgend = max(range->pgend, pgend);
-			purged |= range->purged;
-			range_del(range);
+		if (page_range_in_range(iter, pgstart, pgend)) {
+			pgstart = min(iter->pgstart, pgstart);
+			pgend = max(iter->pgend, pgend);
+			purged |= iter->purged;
+			range_del(iter);
 			goto restart;
 		}
 	}
 
+	range = list_prepare_entry(range, &asma->unpinned_list, unpinned);
 	range_alloc(asma, range, purged, pgstart, pgend, new_range);
 	return 0;
 }

base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275

Signature · 1 line
2.25.1
#1 lore raw
Quoted 9 lines · Jakob Koschel

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 head needs to be used explicitly if the variable is NULL.

Quoted 5 lines
---
 drivers/staging/android/ashmem.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

What tree did you make this against? This file is no longer in Linus's tree.

thanks,

greg k-h

#2 lore raw
Quoted 14 lines · Greg Kroah-Hartman
Quoted 9 lines · Jakob Koschel

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 head needs to be used explicitly if the variable is NULL.

Quoted 8 lines
Quoted 5 lines

---
drivers/staging/android/ashmem.c | 21 ++++++++++++---------

1 file changed, 12 insertions(+), 9 deletions(-)

What tree did you make this against? This file is no longer in Linus's tree.

Looks like the driver was removed just a few hours after I rebased. Feel free to just ignore this patch.

Quoted 4 lines

thanks,

greg k-h

Thanks,
Jakob

#3 lore raw
In reply to Jakob Koschel
Quoted 64 lines · Jakob Koschel

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 head needs to be used explicitly if the variable is NULL.

---
  drivers/staging/android/ashmem.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
drivers/staging/android/ashmem.c +12 -9
index ddbde3f8430e..a1e245827cf6 100644
@@ -703,30 +703,33 @@ static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
  static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
  			struct ashmem_range **new_range)
  {
-	struct ashmem_range *range, *next;
+	struct ashmem_range *range = NULL, *iter, *next;
  	unsigned int purged = ASHMEM_NOT_PURGED;
 
  restart:
-	list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
+	list_for_each_entry_safe(iter, next, &asma->unpinned_list, unpinned) {
  		/* short circuit: this is our insertion point */
-		if (range_before_page(range, pgstart))
+		if (range_before_page(iter, pgstart)) {
+			range = iter;
  			break;
+		}
 
  		/*
  		 * The user can ask us to unpin pages that are already entirely
  		 * or partially pinned. We handle those two cases here.
  		 */
-		if (page_range_subsumed_by_range(range, pgstart, pgend))
+		if (page_range_subsumed_by_range(iter, pgstart, pgend))
  			return 0;
-		if (page_range_in_range(range, pgstart, pgend)) {
-			pgstart = min(range->pgstart, pgstart);
-			pgend = max(range->pgend, pgend);
-			purged |= range->purged;
-			range_del(range);
+		if (page_range_in_range(iter, pgstart, pgend)) {
+			pgstart = min(iter->pgstart, pgstart);
+			pgend = max(iter->pgend, pgend);
+			purged |= iter->purged;
+			range_del(iter);
  			goto restart;
  		}
  	}
 
+	range = list_prepare_entry(range, &asma->unpinned_list, unpinned);
  	range_alloc(asma, range, purged, pgstart, pgend, new_range);
  	return 0;
  }

base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275