list_for_each()
itself is fine.
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
The problem is: struct list_head task_list = sem->task_list;
You are creating a structure like:
|--------------------------------------------------------
\/ |
head #a "sem->task_list" ---element 1 .... element #N -----
^
head #b "task_list" -------------|
list_for_each()
should stop when "pos" == head #a
, but you are not using head #a "sem->task_list"
for iteration, but head #b "task_list"
, so the stop condition is "pos" == "task_list"
is not reached.
Should fix like:
list_for_each(next, &sem->task_list) {