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.
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <[email protected]>
---
arch/arm/mm/ioremap.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/arch/arm/mm/ioremap.c b/arch/arm/mm/ioremap.c
index aa08bcb72db9..417fd5ffa162 100644
--- a/arch/arm/mm/ioremap.c
+++ b/arch/arm/mm/ioremap.c
@@ -90,7 +90,7 @@ struct static_vm *find_static_vm_vaddr(void *vaddr)
void __init add_static_vm_early(struct static_vm *svm)
{
- struct static_vm *curr_svm;
+ struct static_vm *curr_svm = NULL, *iter;
struct vm_struct *vm;
void *vaddr;
@@ -98,13 +98,17 @@ void __init add_static_vm_early(struct static_vm *svm)
vm_area_add_early(vm);
vaddr = vm->addr;
- list_for_each_entry(curr_svm, &static_vmlist, list) {
- vm = &curr_svm->vm;
+ list_for_each_entry(iter, &static_vmlist, list) {
+ vm = &iter->vm;
- if (vm->addr > vaddr)
+ if (vm->addr > vaddr) {
+ curr_svm = iter;
+ list_add_tail(&svm->list, &iter->list);
break;
+ }
}
- list_add_tail(&svm->list, &curr_svm->list);
+ if (!curr_svm)
+ list_add_tail(&svm->list, &static_vmlist);
}
int ioremap_page(unsigned long virt, unsigned long phys,
base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275
--
2.25.1
When list_for_each_entry() completes the iteration over the whole list
without breaking the loop, the iterator value will be a bogus pointer
computed based on the head element.
While it is safe to use the pointer to determine if it was computed
based on the head element, either with list_entry_is_head() or
&pos->member == head, using the iterator variable after the loop should
be avoided.
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].
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <[email protected]>
---
arch/arm/mach-mmp/sram.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/arch/arm/mach-mmp/sram.c b/arch/arm/mach-mmp/sram.c
index ecc46c31004f..f0c175903c41 100644
--- a/arch/arm/mach-mmp/sram.c
+++ b/arch/arm/mach-mmp/sram.c
@@ -39,19 +39,22 @@ static LIST_HEAD(sram_bank_list);
struct gen_pool *sram_get_gpool(char *pool_name)
{
struct sram_bank_info *info = NULL;
+ struct sram_bank_info *iter;
if (!pool_name)
return NULL;
mutex_lock(&sram_lock);
- list_for_each_entry(info, &sram_bank_list, node)
- if (!strcmp(pool_name, info->pool_name))
+ list_for_each_entry(iter, &sram_bank_list, node)
+ if (!strcmp(pool_name, iter->pool_name)) {
+ info = iter;
break;
+ }
mutex_unlock(&sram_lock);
- if (&info->node == &sram_bank_list)
+ if (!info)
return NULL;
return info->gpool;
--
2.25.1
When list_for_each_entry() completes the iteration over the whole list
without breaking the loop, the iterator value will be a bogus pointer
computed based on the head element.
While it is safe to use the pointer to determine if it was computed
based on the head element, either with list_entry_is_head() or
&pos->member == head, using the iterator variable after the loop should
be avoided.
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].
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <[email protected]>
---
arch/arm/plat-pxa/ssp.c | 28 ++++++++++++----------------
1 file changed, 12 insertions(+), 16 deletions(-)
diff --git a/arch/arm/plat-pxa/ssp.c b/arch/arm/plat-pxa/ssp.c
index 563440315acd..6d638f02eac5 100644
--- a/arch/arm/plat-pxa/ssp.c
+++ b/arch/arm/plat-pxa/ssp.c
@@ -38,22 +38,20 @@ static LIST_HEAD(ssp_list);
struct ssp_device *pxa_ssp_request(int port, const char *label)
{
struct ssp_device *ssp = NULL;
+ struct ssp_device *iter;
mutex_lock(&ssp_lock);
- list_for_each_entry(ssp, &ssp_list, node) {
- if (ssp->port_id == port && ssp->use_count == 0) {
- ssp->use_count++;
- ssp->label = label;
+ list_for_each_entry(iter, &ssp_list, node) {
+ if (iter->port_id == port && iter->use_count == 0) {
+ iter->use_count++;
+ iter->label = label;
+ ssp = iter;
break;
}
}
mutex_unlock(&ssp_lock);
-
- if (&ssp->node == &ssp_list)
- return NULL;
-
return ssp;
}
EXPORT_SYMBOL(pxa_ssp_request);
@@ -62,22 +60,20 @@ struct ssp_device *pxa_ssp_request_of(const struct device_node *of_node,
const char *label)
{
struct ssp_device *ssp = NULL;
+ struct ssp_device *iter;
mutex_lock(&ssp_lock);
- list_for_each_entry(ssp, &ssp_list, node) {
- if (ssp->of_node == of_node && ssp->use_count == 0) {
- ssp->use_count++;
- ssp->label = label;
+ list_for_each_entry(iter, &ssp_list, node) {
+ if (iter->of_node == of_node && iter->use_count == 0) {
+ iter->use_count++;
+ iter->label = label;
+ ssp = iter;
break;
}
}
mutex_unlock(&ssp_lock);
-
- if (&ssp->node == &ssp_list)
- return NULL;
-
return ssp;
}
EXPORT_SYMBOL(pxa_ssp_request_of);
--
2.25.1