* [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
@ 2022-03-31 23:06 Jakob Koschel
2022-03-31 23:06 ` [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable Jakob Koschel
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Jakob Koschel @ 2022-03-31 23:06 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, Jakob Koschel, linux-iio,
linux-kernel, Mike Rapoport, Brian Johannesmeyer,
Cristiano Giuffrida, Bos, H.J.
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.
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
---
drivers/iio/industrialio-buffer.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 208b5193c621..151a77c2affd 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -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
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable
2022-03-31 23:06 [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Jakob Koschel
@ 2022-03-31 23:06 ` Jakob Koschel
2022-04-01 12:43 ` Sa, Nuno
2022-03-31 23:06 ` [PATCH 3/3] iio: sysfs-trigger: " Jakob Koschel
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Jakob Koschel @ 2022-03-31 23:06 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, Jakob Koschel, linux-iio,
linux-kernel, Mike Rapoport, Brian Johannesmeyer,
Cristiano Giuffrida, Bos, H.J.
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.
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
---
drivers/iio/common/ssp_sensors/ssp_spi.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/drivers/iio/common/ssp_sensors/ssp_spi.c b/drivers/iio/common/ssp_sensors/ssp_spi.c
index 769bd9280524..f32b04b63ea1 100644
--- a/drivers/iio/common/ssp_sensors/ssp_spi.c
+++ b/drivers/iio/common/ssp_sensors/ssp_spi.c
@@ -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
--
2.25.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/3] iio: sysfs-trigger: replace usage of found with dedicated list iterator variable
2022-03-31 23:06 [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Jakob Koschel
2022-03-31 23:06 ` [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable Jakob Koschel
@ 2022-03-31 23:06 ` Jakob Koschel
2022-04-01 12:41 ` Sa, Nuno
2022-04-01 12:40 ` [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Sa, Nuno
2022-04-02 15:37 ` Jonathan Cameron
3 siblings, 1 reply; 9+ messages in thread
From: Jakob Koschel @ 2022-03-31 23:06 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, Jakob Koschel, linux-iio,
linux-kernel, Mike Rapoport, Brian Johannesmeyer,
Cristiano Giuffrida, Bos, H.J.
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.
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
---
drivers/iio/trigger/iio-trig-sysfs.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/trigger/iio-trig-sysfs.c b/drivers/iio/trigger/iio-trig-sysfs.c
index 2a4b75897910..f1a8704e6cc1 100644
--- a/drivers/iio/trigger/iio-trig-sysfs.c
+++ b/drivers/iio/trigger/iio-trig-sysfs.c
@@ -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;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* RE: [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
2022-03-31 23:06 [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Jakob Koschel
2022-03-31 23:06 ` [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable Jakob Koschel
2022-03-31 23:06 ` [PATCH 3/3] iio: sysfs-trigger: " Jakob Koschel
@ 2022-04-01 12:40 ` Sa, Nuno
2022-04-01 13:55 ` Jakob Koschel
2022-04-02 15:37 ` Jonathan Cameron
3 siblings, 1 reply; 9+ messages in thread
From: Sa, Nuno @ 2022-04-01 12:40 UTC (permalink / raw)
To: Jakob Koschel, Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, linux-iio, linux-kernel,
Mike Rapoport, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
Hi Jakob,
> -----Original Message-----
> From: Jakob Koschel <jakobkoschel@gmail.com>
> Sent: Friday, April 1, 2022 1:07 AM
> To: Jonathan Cameron <jic23@kernel.org>
> Cc: Lars-Peter Clausen <lars@metafoo.de>; Dan Carpenter
> <dan.carpenter@oracle.com>; Jakob Koschel
> <jakobkoschel@gmail.com>; linux-iio@vger.kernel.org; linux-
> kernel@vger.kernel.org; Mike Rapoport <rppt@kernel.org>; Brian
> Johannesmeyer <bjohannesmeyer@gmail.com>; Cristiano Giuffrida
> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
> Subject: [PATCH 1/3] iio: buffer: remove usage of list iterator variable
> 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
> loop'.
>
> To either continue iterating from that position or start a new
> iteration (if the previous iteration was complete) list_prepare_entry()
> is used.
>
> Link: https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
> =wgRr_D8CB-D9Kg-
> c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/__;!!A3Ni8CS0y
> 2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe
> 0XYG7OQ$ [1]
> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
> ---
> drivers/iio/industrialio-buffer.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-
> buffer.c
> index 208b5193c621..151a77c2affd 100644
> --- a/drivers/iio/industrialio-buffer.c
> +++ b/drivers/iio/industrialio-buffer.c
> @@ -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);
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á
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH 3/3] iio: sysfs-trigger: replace usage of found with dedicated list iterator variable
2022-03-31 23:06 ` [PATCH 3/3] iio: sysfs-trigger: " Jakob Koschel
@ 2022-04-01 12:41 ` Sa, Nuno
0 siblings, 0 replies; 9+ messages in thread
From: Sa, Nuno @ 2022-04-01 12:41 UTC (permalink / raw)
To: Jakob Koschel, Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, linux-iio, linux-kernel,
Mike Rapoport, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
> -----Original Message-----
> From: Jakob Koschel <jakobkoschel@gmail.com>
> Sent: Friday, April 1, 2022 1:07 AM
> To: Jonathan Cameron <jic23@kernel.org>
> Cc: Lars-Peter Clausen <lars@metafoo.de>; Dan Carpenter
> <dan.carpenter@oracle.com>; Jakob Koschel
> <jakobkoschel@gmail.com>; linux-iio@vger.kernel.org; linux-
> kernel@vger.kernel.org; Mike Rapoport <rppt@kernel.org>; Brian
> Johannesmeyer <bjohannesmeyer@gmail.com>; Cristiano Giuffrida
> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
> 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.
>
> Link: https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
> =wgRr_D8CB-D9Kg-
> c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/__;!!A3Ni8CS0y
> 2Y!q8mEciLiCAWjMGEwW9jHDD2DoeaKzrMwtUcRCNAZ0gW9DPBVtJk
> xR5FMPPCJNw$ [1]
> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
> ---
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable
2022-03-31 23:06 ` [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable Jakob Koschel
@ 2022-04-01 12:43 ` Sa, Nuno
0 siblings, 0 replies; 9+ messages in thread
From: Sa, Nuno @ 2022-04-01 12:43 UTC (permalink / raw)
To: Jakob Koschel, Jonathan Cameron
Cc: Lars-Peter Clausen, Dan Carpenter, linux-iio, linux-kernel,
Mike Rapoport, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
> -----Original Message-----
> From: Jakob Koschel <jakobkoschel@gmail.com>
> Sent: Friday, April 1, 2022 1:07 AM
> To: Jonathan Cameron <jic23@kernel.org>
> Cc: Lars-Peter Clausen <lars@metafoo.de>; Dan Carpenter
> <dan.carpenter@oracle.com>; Jakob Koschel
> <jakobkoschel@gmail.com>; linux-iio@vger.kernel.org; linux-
> kernel@vger.kernel.org; Mike Rapoport <rppt@kernel.org>; Brian
> Johannesmeyer <bjohannesmeyer@gmail.com>; Cristiano Giuffrida
> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
> 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.
>
> Link: https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
> =wgRr_D8CB-D9Kg-
> c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/__;!!A3Ni8CS0y
> 2Y!tqcYepOSYys02RcvKD7LiZk3mHwPmSLa_1S0QglWPRZSCoPnXk3ccjY
> _XzsDDA$ [1]
> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
> ---
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
2022-04-01 12:40 ` [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Sa, Nuno
@ 2022-04-01 13:55 ` Jakob Koschel
2022-04-01 14:28 ` Sa, Nuno
0 siblings, 1 reply; 9+ messages in thread
From: Jakob Koschel @ 2022-04-01 13:55 UTC (permalink / raw)
To: Sa, Nuno
Cc: Jonathan Cameron, Lars-Peter Clausen, Dan Carpenter, linux-iio,
linux-kernel, Mike Rapoport, Brian Johannesmeyer,
Cristiano Giuffrida, Bos, H.J.
> On 1. Apr 2022, at 14:40, Sa, Nuno <Nuno.Sa@analog.com> wrote:
>
> Hi Jakob,
>
>> -----Original Message-----
>> From: Jakob Koschel <jakobkoschel@gmail.com>
>> Sent: Friday, April 1, 2022 1:07 AM
>> To: Jonathan Cameron <jic23@kernel.org>
>> Cc: Lars-Peter Clausen <lars@metafoo.de>; Dan Carpenter
>> <dan.carpenter@oracle.com>; Jakob Koschel
>> <jakobkoschel@gmail.com>; linux-iio@vger.kernel.org; linux-
>> kernel@vger.kernel.org; Mike Rapoport <rppt@kernel.org>; Brian
>> Johannesmeyer <bjohannesmeyer@gmail.com>; Cristiano Giuffrida
>> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
>> Subject: [PATCH 1/3] iio: buffer: remove usage of list iterator variable
>> 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
>> loop'.
>>
>> To either continue iterating from that position or start a new
>> iteration (if the previous iteration was complete) list_prepare_entry()
>> is used.
>>
>> Link: https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
>> =wgRr_D8CB-D9Kg-
>> c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/__;!!A3Ni8CS0y
>> 2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe
>> 0XYG7OQ$ [1]
>> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
>> ---
>> drivers/iio/industrialio-buffer.c | 7 +++++--
>> 1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-
>> buffer.c
>> index 208b5193c621..151a77c2affd 100644
>> --- a/drivers/iio/industrialio-buffer.c
>> +++ b/drivers/iio/industrialio-buffer.c
>> @@ -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);
>
> 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?
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.
>
> - Nuno Sá
Thanks,
Jakob
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
2022-04-01 13:55 ` Jakob Koschel
@ 2022-04-01 14:28 ` Sa, Nuno
0 siblings, 0 replies; 9+ messages in thread
From: Sa, Nuno @ 2022-04-01 14:28 UTC (permalink / raw)
To: Jakob Koschel
Cc: Jonathan Cameron, Lars-Peter Clausen, Dan Carpenter, linux-iio,
linux-kernel, Mike Rapoport, Brian Johannesmeyer,
Cristiano Giuffrida, Bos, H.J.
> From: Jakob Koschel <jakobkoschel@gmail.com>
> Sent: Friday, April 1, 2022 3:55 PM
> To: Sa, Nuno <Nuno.Sa@analog.com>
> Cc: Jonathan Cameron <jic23@kernel.org>; Lars-Peter Clausen
> <lars@metafoo.de>; Dan Carpenter <dan.carpenter@oracle.com>;
> linux-iio@vger.kernel.org; linux-kernel@vger.kernel.org; Mike
> Rapoport <rppt@kernel.org>; Brian Johannesmeyer
> <bjohannesmeyer@gmail.com>; Cristiano Giuffrida
> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
> Subject: Re: [PATCH 1/3] iio: buffer: remove usage of list iterator
> variable for list_for_each_entry_continue_reverse()
>
> [External]
>
>
>
> > On 1. Apr 2022, at 14:40, Sa, Nuno <Nuno.Sa@analog.com> wrote:
> >
> > Hi Jakob,
> >
> >> -----Original Message-----
> >> From: Jakob Koschel <jakobkoschel@gmail.com>
> >> Sent: Friday, April 1, 2022 1:07 AM
> >> To: Jonathan Cameron <jic23@kernel.org>
> >> Cc: Lars-Peter Clausen <lars@metafoo.de>; Dan Carpenter
> >> <dan.carpenter@oracle.com>; Jakob Koschel
> >> <jakobkoschel@gmail.com>; linux-iio@vger.kernel.org; linux-
> >> kernel@vger.kernel.org; Mike Rapoport <rppt@kernel.org>; Brian
> >> Johannesmeyer <bjohannesmeyer@gmail.com>; Cristiano
> Giuffrida
> >> <c.giuffrida@vu.nl>; Bos, H.J. <h.j.bos@vu.nl>
> >> Subject: [PATCH 1/3] iio: buffer: remove usage of list iterator
> variable
> >> 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
> >> loop'.
> >>
> >> To either continue iterating from that position or start a new
> >> iteration (if the previous iteration was complete)
> list_prepare_entry()
> >> is used.
> >>
> >> Link:
> https://urldefense.com/v3/__https://lore.kernel.org/all/CAHk-
> >> =wgRr_D8CB-D9Kg-
> >>
> c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/__;!!A3Ni8CS0y
> >>
> 2Y!q8llw5UCaMIsAU7tPtPDhwVor0wy032I7FJHv0VxBZksNuRJF04HjWe
> >> 0XYG7OQ$ [1]
> >> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
> >> ---
> >> drivers/iio/industrialio-buffer.c | 7 +++++--
> >> 1 file changed, 5 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-
> >> buffer.c
> >> index 208b5193c621..151a77c2affd 100644
> >> --- a/drivers/iio/industrialio-buffer.c
> >> +++ b/drivers/iio/industrialio-buffer.c
> >> @@ -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);
> >
> > 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?
>
> 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.
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:
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse()
2022-03-31 23:06 [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Jakob Koschel
` (2 preceding siblings ...)
2022-04-01 12:40 ` [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Sa, Nuno
@ 2022-04-02 15:37 ` Jonathan Cameron
3 siblings, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2022-04-02 15:37 UTC (permalink / raw)
To: Jakob Koschel, Lars-Peter Clausen
Cc: Dan Carpenter, linux-iio, linux-kernel, Mike Rapoport,
Brian Johannesmeyer, Cristiano Giuffrida, Bos, H.J.
On Fri, 1 Apr 2022 01:06:30 +0200
Jakob Koschel <jakobkoschel@gmail.com> 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'.
>
> To either continue iterating from that position or start a new
> iteration (if the previous iteration was complete) list_prepare_entry()
> is used.
>
> Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
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
> ---
> drivers/iio/industrialio-buffer.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
> index 208b5193c621..151a77c2affd 100644
> --- a/drivers/iio/industrialio-buffer.c
> +++ b/drivers/iio/industrialio-buffer.c
> @@ -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
>
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2022-04-02 15:29 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31 23:06 [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Jakob Koschel
2022-03-31 23:06 ` [PATCH 2/3] iio: ssp_sensors: replace usage of found with dedicated list iterator variable Jakob Koschel
2022-04-01 12:43 ` Sa, Nuno
2022-03-31 23:06 ` [PATCH 3/3] iio: sysfs-trigger: " Jakob Koschel
2022-04-01 12:41 ` Sa, Nuno
2022-04-01 12:40 ` [PATCH 1/3] iio: buffer: remove usage of list iterator variable for list_for_each_entry_continue_reverse() Sa, Nuno
2022-04-01 13:55 ` Jakob Koschel
2022-04-01 14:28 ` Sa, Nuno
2022-04-02 15:37 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).