[PATCH 3/6] kernel: remove iterator use outside the loop
From: Xiaomeng Tong
Date: Tue Mar 01 2022 - 02:59:15 EST
Demonstrations for:
- list_for_each_entry_inside
- list_for_each_entry_continue_inside
- list_for_each_entry_safe_continue_inside
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@xxxxxxxxx>
---
kernel/power/snapshot.c | 28 ++++++++++++++++------------
kernel/signal.c | 6 +++---
2 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 330d49937..75958b5fa 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -625,16 +625,18 @@ static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
for_each_populated_zone(zone) {
unsigned long zone_start, zone_end;
- struct mem_extent *ext, *cur, *aux;
+ struct mem_extent *me = NULL;
zone_start = zone->zone_start_pfn;
zone_end = zone_end_pfn(zone);
- list_for_each_entry(ext, list, hook)
- if (zone_start <= ext->end)
+ list_for_each_entry_inside(ext, struct mem_extent, list, hook)
+ if (zone_start <= ext->end) {
+ me = ext;
break;
+ }
- if (&ext->hook == list || zone_end < ext->start) {
+ if (!me || zone_end < me->start) {
/* New extent is necessary */
struct mem_extent *new_ext;
@@ -645,23 +647,25 @@ static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
}
new_ext->start = zone_start;
new_ext->end = zone_end;
- list_add_tail(&new_ext->hook, &ext->hook);
+ if (!me)
+ list_add_tail(&new_ext->hook, list);
+ else
+ list_add_tail(&new_ext->hook, &me->hook);
continue;
}
/* Merge this zone's range of PFNs with the existing one */
- if (zone_start < ext->start)
- ext->start = zone_start;
- if (zone_end > ext->end)
- ext->end = zone_end;
+ if (zone_start < me->start)
+ me->start = zone_start;
+ if (zone_end > me->end)
+ me->end = zone_end;
/* More merging may be possible */
- cur = ext;
- list_for_each_entry_safe_continue(cur, aux, list, hook) {
+ list_for_each_entry_safe_continue_inside(cur, aux, me, list, hook) {
if (zone_end < cur->start)
break;
if (zone_end < cur->end)
- ext->end = cur->end;
+ me->end = cur->end;
list_del(&cur->hook);
kfree(cur);
}
diff --git a/kernel/signal.c b/kernel/signal.c
index 9b04631ac..da2c20de1 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -711,7 +711,7 @@ static int dequeue_synchronous_signal(kernel_siginfo_t *info)
{
struct task_struct *tsk = current;
struct sigpending *pending = &tsk->pending;
- struct sigqueue *q, *sync = NULL;
+ struct sigqueue *sync = NULL;
/*
* Might a synchronous signal be in the queue?
@@ -722,7 +722,7 @@ static int dequeue_synchronous_signal(kernel_siginfo_t *info)
/*
* Return the first synchronous signal in the queue.
*/
- list_for_each_entry(q, &pending->list, list) {
+ list_for_each_entry_inside(q, struct sigqueue, &pending->list, list) {
/* Synchronous signals have a positive si_code */
if ((q->info.si_code > SI_USER) &&
(sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
@@ -735,7 +735,7 @@ static int dequeue_synchronous_signal(kernel_siginfo_t *info)
/*
* Check if there is another siginfo for the same signal.
*/
- list_for_each_entry_continue(q, &pending->list, list) {
+ list_for_each_entry_continue_inside(q, sync, &pending->list, list) {
if (q->info.si_signo == sync->info.si_signo)
goto still_pending;
}
--
2.17.1