iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
PATCH 1/3 iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
In preparation to limit the scope of the list iterator variable to the list traversal loop, use a dedicated pointer to iterate through the list [1].
Since that variable should not be used past the loop iteration, a separate variable is used to 'remember the current location within the loop'.
To either continue iterating from that position or start a new iteration (if the previous iteration was complete) list_prepare_entry() is used.
--- drivers/iio/industrialio-buffer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/iio/industrialio-buffer.c +5 -2
@@ -1059,7 +1059,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, struct iio_device_config *config) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (indio_dev); -struct iio_buffer *buffer; +struct iio_buffer *buffer, *tmp =NULL ;int ret; indio_dev->active_scan_mask = config->scan_mask;
@@ -1097,8 +1097,10 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,list_for_each_entry (buffer, &iio_dev_opaque->buffer_list, buffer_list) { ret =iio_buffer_enable (buffer, indio_dev); -if (ret) +if (ret) { + tmp = buffer; goto err_disable_buffers; + } }if (indio_dev->currentmode ==INDIO_BUFFER_TRIGGERED ) {
@@ -1125,6 +1127,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, indio_dev->pollfunc); } err_disable_buffers: + buffer =list_prepare_entry (tmp, &iio_dev_opaque->buffer_list, buffer_list);list_for_each_entry_continue_reverse (buffer, &iio_dev_opaque->buffer_list, buffer_list)iio_buffer_disable (buffer, indio_dev);
base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275 --
2.25.1
PATCH 2/3 iio: ssp_sensors: 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/iio/common/ssp_sensors/ssp_spi.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +6 -7
@@ -331,12 +331,11 @@ static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)/* threaded irq */ int ssp_irq_msg (struct ssp_data *data) { -bool found = false;char *buffer;u8 msg_type;int ret;u16 length, msg_options; -struct ssp_msg *msg, *n; +struct ssp_msg *msg =NULL , *iter, *n; ret =spi_read (data->spi, data->header_buffer,SSP_HEADER_BUFFER_SIZE );if (ret <0 ) {
@@ -362,15 +361,15 @@ int ssp_irq_msg(struct ssp_data *data)* received with no order */ mutex_lock (&data->pending_lock); -list_for_each_entry_safe (msg, n, &data->pending_list, list) { -if (msg->options == msg_options) { -list_del (&msg->list); - found = true; +list_for_each_entry_safe (iter, n, &data->pending_list, list) { +if (iter->options == msg_options) { +list_del (&iter->list); + msg = iter;break ; } } -if (!found) { +if (!msg) {/* * here can be implemented dead messages handling * but the slave should not send such ones - it is to
Signature · 1 line
2.25.1
RE: [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable
Quoted 28 lines
-----Original Message-----
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 1:07 AM
To: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; Jakob Koschel <[email protected]>; [email protected]; linux- [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; Cristiano Giuffrida <[email protected]>; Bos, H.J. <[email protected]>
Subject: [PATCH 2/3] iio:ssp_sensors: replace usage of found with dedicated list iterator variable[External]
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.
Quoted 7 lines
=wgRr_D8CB-D9Kg- [email protected]/__;!!A3Ni8CS0y 2Y!tqcYepOSYys02RcvKD7LiZk3mHwPmSLa_1S0QglWPRZSCoPnXk3ccjY _XzsDDA$ [1]
Signed-off-by Jakob Koschel <[email protected]>---
PATCH 3/3 iio: sysfs-trigger: replace usage of found with dedicated list iterator variable
In reply to Jakob KoschelTo 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/iio/trigger/iio-trig-sysfs.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iio/trigger/iio-trig-sysfs.c +5 -6
@@ -176,16 +176,15 @@ static int iio_sysfs_trigger_probe(int id)static int iio_sysfs_trigger_remove (int id) { -bool foundit = false; -struct iio_sysfs_trig *t; +struct iio_sysfs_trig *t =NULL , *iter;mutex_lock (&iio_sysfs_trig_list_mut); -list_for_each_entry (t, &iio_sysfs_trig_list, l) -if (id == t->id) { - foundit = true; +list_for_each_entry (iter, &iio_sysfs_trig_list, l) +if (id == iter->id) { + t = iter;break ; } -if (!foundit) { +if (!t) {mutex_unlock (&iio_sysfs_trig_list_mut);return -EINVAL ; }
Signature · 1 line
2.25.1
RE: [PATCH 3/3] iio: sysfs-trigger: replace usage of found with dedicated list iterator variable
Quoted 28 lines
-----Original Message-----
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 1:07 AM
To: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; Jakob Koschel <[email protected]>; [email protected]; linux- [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; Cristiano Giuffrida <[email protected]>; Bos, H.J. <[email protected]>
Subject: [PATCH 3/3] iio: sysfs-trigger: replace usage of found with dedicated list iterator variable[External]
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.
Quoted 7 lines
=wgRr_D8CB-D9Kg- [email protected]/__;!!A3Ni8CS0y 2Y!q8mEciLiCAWjMGEwW9jHDD2DoeaKzrMwtUcRCNAZ0gW9DPBVtJk xR5FMPPCJNw$ [1]
Signed-off-by Jakob Koschel <[email protected]>---
Sa, Nuno
reviewerHi Jakob,
Quoted 68 lines
-----Original Message-----
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 1:07 AM
To: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; Jakob Koschel <[email protected]>; [email protected]; linux- [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; Cristiano Giuffrida <[email protected]>; Bos, H.J. <[email protected]>
Subject: [PATCH 1/3] iio: buffer: remove usage of list iterator variable forlist_for_each_entry_continue_reverse()[External]
In preparation to limit the scope of the list iterator variable to the list traversal loop, use a dedicated pointer to iterate through the list [1].
Since that variable should not be used past the loop iteration, a separate variable is used to 'remember the current location within the loop'.
To either continue iterating from that position or start a new iteration (if the previous iteration was complete)
list_prepare_entry()is used.=wgRr_D8CB-D9Kg- [email protected]/__;!!A3Ni8CS0y 2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe 0XYG7OQ$ [1]
Signed-off-by Jakob Koschel <[email protected]>--- drivers/iio/industrialio-buffer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0
-0
buffer.c
index208b5193c621..151a77c2affd100644drivers/iio/industrialio-buffer.c +1
-1@@ -1059,7 +1059,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, struct iio_device_config *config) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (indio_dev); -struct iio_buffer *buffer; +struct iio_buffer *buffer, *tmp =NULL ;int ret;drivers/iio/industrialio-buffer.c +3
-1indio_dev->active_scan_mask = config->scan_mask;@@ -1097,8 +1097,10 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { ret =iio_buffer_enable (buffer, indio_dev); -if (ret) +if (ret) { + tmp = buffer; goto err_disable_buffers; + } }drivers/iio/industrialio-buffer.c +0
-0if (indio_dev->currentmode ==INDIO_BUFFER_TRIGGERED ) {
drivers/iio/industrialio-buffer.c static int iio_enable_buffers(struct iio_dev · line 1132
Quoted 6 lines
drivers/iio/industrialio-buffer.c +1
-0@@ -1125,6 +1127,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, indio_dev->pollfunc); } err_disable_buffers: + buffer =list_prepare_entry (tmp, &iio_dev_opaque- >buffer_list, buffer_list);
Ok, it's Friday so I might be seeing ghosts... But looking at 'list_prepare_entry()'... If tmp != NULL, then all goes well but if tmp == NULL, then we get
list_entry(&iio_dev_opaque->buffer_list, struct iio_buffer, buffer_list) which would require 'struct iio_dev_opaque'. It looks like like 'list_prepare_entry()' assumes that pos and head are of the same type...
Am I missing something?
- Nuno Sá
Quoted 81 lines · Nuno
Hi Jakob,
Quoted 73 lines
-----Original Message-----
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 1:07 AM
To: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; Jakob Koschel <[email protected]>; [email protected]; linux- [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; Cristiano Giuffrida <[email protected]>; Bos, H.J. <[email protected]>
Subject: [PATCH 1/3] iio: buffer: remove usage of list iterator variable forlist_for_each_entry_continue_reverse()[External]
In preparation to limit the scope of the list iterator variable to the list traversal loop, use a dedicated pointer to iterate through the list [1].
Since that variable should not be used past the loop iteration, a separate variable is used to 'remember the current location within the loop'.
To either continue iterating from that position or start a new iteration (if the previous iteration was complete)
list_prepare_entry()is used.=wgRr_D8CB-D9Kg- [email protected]/__;!!A3Ni8CS0y 2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe 0XYG7OQ$ [1]
Signed-off-by Jakob Koschel <[email protected]>---
drivers/iio/industrialio-buffer.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0
-0
buffer.c
index208b5193c621..151a77c2affd100644drivers/iio/industrialio-buffer.c +1
-1@@ -1059,7 +1059,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, struct iio_device_config *config) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (indio_dev); -struct iio_buffer *buffer; +struct iio_buffer *buffer, *tmp =NULL ;int ret;indio_dev->active_scan_mask = config->scan_mask;drivers/iio/industrialio-buffer.c +3
-1@@ -1097,8 +1097,10 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { ret =iio_buffer_enable (buffer, indio_dev); -if (ret) +if (ret) { + tmp = buffer; goto err_disable_buffers; + } }if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {drivers/iio/industrialio-buffer.c +0
-0@@ -1125,6 +1127,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, indio_dev->pollfunc); }
err_disable_buffers:drivers/iio/industrialio-buffer.c +1
-0+ buffer = list_prepare_entry(tmp, &iio_dev_opaque-Quoted 1 line
Quoted 1 line
buffer_list,buffer_list);
Quoted 8 lines
Ok, it's Friday so I might be seeing ghosts... But looking at '
list_prepare_entry()'... If tmp != NULL, then all goes well but if tmp == NULL, then we get
list_entry(&iio_dev_opaque->buffer_list, struct iio_buffer, buffer_list)which would require 'structiio_dev_opaque'. It looks like like 'list_prepare_entry()' assumes that pos and head are of the same type...Am I missing something?
The list iterators are weird in this perspective...
If you look at the original code, list_for_each_entry_continue_reverse() is called on 'buffer'.
'buffer' would be a valid struct element of &iio_dev_opaque->buffer_list if the break is hit, but if no break is hit in the earlier list_for_each_entry() buffer is not a valid entry.
Before the terminating condition of list_for_each_entry() is met, it essentially does:
buffer = list_entry(&iio_dev_opaque->buffer_list, typeof(*buffer), buffer_list);
the buffer returned here is not a valid pointer to struct however.
But since list_for_each_entry_continue_reverse() immediately calls list_prev_entry(buffer, buffer_list) on it you end up with the last entry of the list again and start iterating with that one.
It's a very weird design choice but since list_for_each_entry_continue_reverse() expects a pointer to the element struct and not the list_head struct, you need to pass in this 'bogus' pointer if you want it to start on the head element.
Keep in mind that the code here is just a more explicit version of this 'type confusion' whereas with the original code it was just hidden within the list_for_each_entry() macro and far less obvious. The functionality is exactly the same.
PS: list_prepare_entry() was made for this use case, so basically it is expected to craft this bogus pointer from the head if pos == NULL.
Quoted 2 lines
- Nuno Sá
Thanks,
Jakob
Quoted 131 lines
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 3:55 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; [email protected]; [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; Cristiano Giuffrida <[email protected]>; Bos, H.J. <[email protected]>
Subject: Re: [PATCH 1/3] iio: buffer: remove usage of list iterator variable forlist_for_each_entry_continue_reverse()[External]
Quoted 13 lines · Nuno
Hi Jakob,
Quoted 9 lines
-----Original Message-----
From: Jakob Koschel <[email protected]>
Sent: Friday, April 1, 2022 1:07 AM
To: Jonathan Cameron <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Dan Carpenter <[email protected]>; Jakob Koschel <[email protected]>; [email protected]; linux- [email protected]; Mike Rapoport <[email protected]>; Brian Johannesmeyer <[email protected]>; CristianoGiuffrida
Quoted 2 lines
Quoted 2 lines
<[email protected]>; Bos, H.J. <[email protected]>
Subject: [PATCH 1/3] iio: buffer: remove usage of list iteratorvariable
Quoted 10 lines
Quoted 10 lines
for
list_for_each_entry_continue_reverse()[External]
In preparation to limit the scope of the list iterator variable to the list traversal loop, use a dedicated pointer to iterate through the list [1].
Since that variable should not be used past the loop iteration, a separate variable is used to 'remember the current location within
the
Quoted 4 lines
Quoted 4 lines
loop'.
To either continue iterating from that position or start a new iteration (if the previous iteration was complete)
list_prepare_entry()Quoted 3 lines
Quoted 3 lines
is used.
Link:
https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
Quoted 2 lines
Quoted 2 lines
=wgRr_D8CB-D9Kg-
[email protected]/__;!!A3Ni8CS0y
Quoted 1 line
Quoted 1 line
2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe
Quoted 6 lines
Quoted 6 lines
0XYG7OQ$ [1]
Signed-off-by Jakob Koschel <[email protected]>---
drivers/iio/industrialio-buffer.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)Quoted 6 lines
drivers/iio/industrialio-buffer.c static int iio_enable_buffers(struct
Quoted 6 lines
drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0
-0
buffer.c
index208b5193c621..151a77c2affd100644drivers/iio/industrialio-buffer.c +0
-0@@ -1059,7 +1059,7 @@ static int iio_enable_buffers(struct
iio_devQuoted 11 lines
drivers/iio/industrialio-buffer.c static int iio_enable_buffers(struct
Quoted 11 lines
drivers/iio/industrialio-buffer.c +1
-1*indio_dev, struct iio_device_config *config) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (indio_dev); -struct iio_buffer *buffer; +struct iio_buffer *buffer, *tmp =NULL ;int ret;indio_dev->active_scan_mask = config->scan_mask;drivers/iio/industrialio-buffer.c +0
-0@@ -1097,8 +1097,10 @@ static int iio_enable_buffers(struct
iio_devQuoted 12 lines
Quoted 12 lines
drivers/iio/industrialio-buffer.c +3
-1*indio_dev, list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { ret =iio_buffer_enable (buffer, indio_dev); -if (ret) +if (ret) { + tmp = buffer; goto err_disable_buffers; + } }Quoted 2 lines
drivers/iio/industrialio-buffer.c static int iio_enable_buffers(struct
Quoted 2 lines
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {drivers/iio/industrialio-buffer.c +0
-0@@ -1125,6 +1127,7 @@ static int iio_enable_buffers(struct
iio_devQuoted 8 lines
Quoted 6 lines
drivers/iio/industrialio-buffer.c +0
-0*indio_dev, indio_dev->pollfunc); }
err_disable_buffers:drivers/iio/industrialio-buffer.c +1
-0+ buffer = list_prepare_entry(tmp, &iio_dev_opaque-Quoted 1 line
buffer_list,buffer_list);Ok, it's Friday so I might be seeing ghosts... But looking at
'
list_prepare_entry()'...Quoted 3 lines
If tmp != NULL, then all goes well but if tmp == NULL, then we get
list_entry(&iio_dev_opaque->buffer_list, struct
iio_buffer,
buffer_list) whichQuoted 1 line
would require 'struct
iio_dev_opaque'. It looks like like'
list_prepare_entry()'Quoted 3 lines
assumes that pos and head are of the same type...
Am I missing something?
The list iterators are weird in this perspective...
If you look at the original code,
list_for_each_entry_continue_reverse()is called on 'buffer'.'buffer' would be a valid struct element of &iio_dev_opaque-
Quoted 1 line
buffer_listif the break is hit,but if no break is hit in the earlier
list_for_each_entry()buffer is not a valid entry.Before the terminating condition of
list_for_each_entry()is met, it essentially does:
Quoted 8 lines
buffer = list_entry(&iio_dev_opaque->buffer_list,
typeof(*buffer),buffer_list);the buffer returned here is not a valid pointer to struct however. But since
list_for_each_entry_continue_reverse()immediately callslist_prev_entry(buffer, buffer_list)
on it you end up with the last entry of the list again and start iterating with that one.
Ahh I see it now... I really never had noticed this dance but in fact the same dance is also used to detect the end of list_for_each_entry()...
Ok, then:
Jonathan Cameron
maintainerQuoted 8 lines · On Fri, 1 Apr 2022 01:06:30 +0200 Jakob Koschel <[email protected]> wrote:
In preparation to limit the scope of the list iterator variable to the list traversal loop, use a dedicated pointer to iterate through the list [1].
Since that variable should not be used past the loop iteration, a separate variable is used to 'remember the current location within the loop'.
Quoted 6 lines
To either continue iterating from that position or start a new iteration (if the previous iteration was complete)
list_prepare_entry()is used.
Hi Jakob,
Series applied to the togreg branch of iio.git and pushed out as testing to let 0-day take a poke at it. Note I'll be rebasing that branch on rc1 once available before pushing it out for linux-next to pick up.
Note I'm happy to add additional tags if anyone else takes a look at it.
Thanks
Jonathan
Quoted 42 lines
--- drivers/iio/industrialio-buffer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/iio/industrialio-buffer.c +5
-2@@ -1059,7 +1059,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, struct iio_device_config *config) {struct iio_dev_opaque *iio_dev_opaque =to_iio_dev_opaque (indio_dev); -struct iio_buffer *buffer; +struct iio_buffer *buffer, *tmp =NULL ;int ret; indio_dev->active_scan_mask = config->scan_mask;@@ -1097,8 +1097,10 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,list_for_each_entry (buffer, &iio_dev_opaque->buffer_list, buffer_list) { ret =iio_buffer_enable (buffer, indio_dev); -if (ret) +if (ret) { + tmp = buffer; goto err_disable_buffers; + } }if (indio_dev->currentmode ==INDIO_BUFFER_TRIGGERED ) {@@ -1125,6 +1127,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev, indio_dev->pollfunc); } err_disable_buffers: + buffer =list_prepare_entry (tmp, &iio_dev_opaque->buffer_list, buffer_list);list_for_each_entry_continue_reverse (buffer, &iio_dev_opaque->buffer_list, buffer_list)iio_buffer_disable (buffer, indio_dev);base-commit:
f82da161ea75dc4db21b2499e4b1facd36dab275--
2.25.1