iio: mark scan_timestamp __private
cover iio: mark scan_timestamp __private
The scan_timestamp value of the struct iio_dev, even though is an internal variable, it is being used in some drivers. To avoid any unwanted overwrites of this value, create a getter and when all the drivers are converted, mark the variable as __private.
The patch is an RFC because the added value might not be considered high enough by someone to be implemented and/or it might need to be done in a different way since it touches multiple drivers.
Vasileios Amoiridis (6):
iio: create accessor for iio_dev->scan_timestamp
iio: make use of iio_is_soft_ts_enabled()
iio: adc: dln2-adc: make use of iio_is_soft_ts_enabled()
iio: adc: max1363: make use of iio_is_soft_ts_enabled()
iio: common: ssp_sensors: make use of iio_is_soft_ts_enabled()
iio: core: mark scan_timestamp as __private
drivers/iio/adc/dln2-adc.c | 2 +- drivers/iio/adc/max1363.c | 2 +- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 +- drivers/iio/industrialio-buffer.c | 2 +- include/linux/iio/buffer.h | 2 +- include/linux/iio/iio.h | 11 ++++++++++- 6 files changed, 15 insertions(+), 6 deletions(-)
base-commit: a61ff7eac77e86de828fe28c4e42b8ae9ec2b195
Signature · 1 line
2.43.0
RFC PATCH 1/6 iio: create accessor for iio_dev->scan_timestamp
"scan_timestamp" is supposed to be an internal member of the iio device structure. However, there are some drivers that are using it directly.
For that reason, the following accessor is created:
iio_is_soft_ts_enabled()
The goal of this accessor, is to ultimately mark "scan_timestamp" as a __private member of the struct iio_dev.
--- include/linux/iio/iio.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/iio/iio.h +9 -0
@@ -902,6 +902,15 @@ int iio_active_scan_mask_index(struct iio_dev *indio_dev);for_each_set_bit ((chan), (indio_dev)->active_scan_mask, \iio_get_masklength (indio_dev)) +/** +* iio_is_soft_ts_enabled - Check if the software timestamp is enabled +* @indio_dev: the IIO device + */ +static inline bool iio_is_soft_ts_enabled (const struct iio_dev *indio_dev) +{ +return indio_dev->scan_timestamp; +} +ssize_t iio_format_value (char *buf,unsigned int type,int size,int *vals); int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
Signature · 1 line
2.43.0
RFC PATCH 2/6 iio: make use of iio_is_soft_ts_enabled()
In reply to Vasileios AmoiridisUse the iio_is_soft_ts_enabled() accessor to access the value of the scan_timestamp. This way, it can be marked as __private when there are no direct accessors of it.
--- include/linux/iio/buffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/buffer.h +1 -1
@@ -37,7 +37,7 @@ int iio_pop_from_buffer(struct iio_buffer *buffer, void *data);static inline int iio_push_to_buffers_with_timestamp (struct iio_dev *indio_dev,void *data,int64_t timestamp) { -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {size_t ts_offset = indio_dev->scan_bytes /sizeof (int64_t ) -1 ; ((int64_t *)data)[ts_offset] = timestamp; }
Signature · 1 line
2.43.0
RFC PATCH 3/6 iio: adc: dln2-adc: make use of iio_is_soft_ts_enabled()
In reply to Vasileios AmoiridisUse the iio_is_soft_ts_enabled() accessor to access the value of the scan_timestamp. This way, it can be marked as __private when there are no direct accessors of it.
--- drivers/iio/adc/dln2-adc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/dln2-adc.c +1 -1
@@ -128,7 +128,7 @@ static void dln2_adc_update_demux(struct dln2_adc *dln2) in_loc +=2 ; } -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {size_t ts_offset = indio_dev->scan_bytes /sizeof (int64_t ) -1 ; dln2->ts_pad_offset = out_loc;
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH RFC 3/6] iio: adc: dln2-adc: make use of iio_is_soft_ts_enabled()
Quoted 5 lines · On Sat, 30 Nov 2024 01:27:07 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>
The original code looks to me like a micro optimization that we should consider dropping to reduce complexity. It is only used to zero a hole in a structure conditionally if the timestamp is enabled.
Better I think to just drop all the ts_pad_offset etc stuff in favour of just zeroing the whole of the data structure in dln2_adc_trigger_h() whether or not the timestamp is enabled.
My guess is that on a reasonably performance CPU the occasional cost of a branch miss prediction will outweigh zeroing a fairly small structure anyway.
+CC Jack who wrote this driver.
Jonathan
Quoted 17 lines
--- drivers/iio/adc/dln2-adc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/adc/dln2-adc.c +1
-1@@ -128,7 +128,7 @@ static void dln2_adc_update_demux(struct dln2_adc *dln2) in_loc +=2 ; } -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {size_t ts_offset = indio_dev->scan_bytes /sizeof (int64_t ) -1 ; dln2->ts_pad_offset = out_loc;
Re: [PATCH RFC 3/6] iio: adc: dln2-adc: make use of iio_is_soft_ts_enabled()
Quoted 17 lines · Jonathan Cameron
Quoted 5 lines · On Sat, 30 Nov 2024 01:27:07 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>The original code looks to me like a micro optimization that we should consider dropping to reduce complexity. It is only used to zero a hole in a structure conditionally if the timestamp is enabled.
Better I think to just drop all the
ts_pad_offsetetc stuff in favour of just zeroing the whole of the data structure indln2_adc_trigger_h()whether or not the timestamp is enabled.
Quoted 9 lines
My guess is that on a reasonably performance CPU the occasional cost of a branch miss prediction will outweigh zeroing a fairly small structure anyway.
+CC Jack who wrote this driver.
Jonathan
Hi Jonathan,
Thanks for the review once again! It looks like it should be fairly straightforward to drop the zeroing of the ts_pad_{offset/length} so indeed, if Jack doesn't have anything strong against it I could move forward and send a v2.
Cheers,
Vasilis
Quoted 18 lines
Quoted 17 lines
--- drivers/iio/adc/dln2-adc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/adc/dln2-adc.c +1
-1@@ -128,7 +128,7 @@ static void dln2_adc_update_demux(struct dln2_adc *dln2) in_loc +=2 ; } -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {size_t ts_offset = indio_dev->scan_bytes /sizeof (int64_t ) -1 ; dln2->ts_pad_offset = out_loc;
RFC PATCH 4/6 iio: adc: max1363: make use of iio_is_soft_ts_enabled()
In reply to Vasileios AmoiridisUse the iio_is_soft_ts_enabled() accessor to access the value of the scan_timestamp. This way, it can be marked as __private when there are no direct accessors of it.
--- drivers/iio/adc/max1363.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/max1363.c +1 -1
@@ -1473,7 +1473,7 @@ static irqreturn_t max1363_trigger_handler(int irq, void *p) d_size = numvals*2 ; else d_size = numvals; -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) { d_size +=sizeof (s64);if (d_size %sizeof (s64)) d_size +=sizeof (s64) - (d_size %sizeof (s64));
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH RFC 4/6] iio: adc: max1363: make use of iio_is_soft_ts_enabled()
Quoted 5 lines · On Sat, 30 Nov 2024 01:27:08 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>
Hmm. A younger me one ;)
A simple allocation that is always big enough is going to cost us very little. Should almost certainly be using kzalloc.
I'd change this driver to just stick an array of size 12 * 2 + 8 so 32bytes in the iio_priv()
(12 channels, possibly of 2 bytes each + aligned timestamp) and always use that for the rx_buf.
Both a simplification and probably a performance improvement as well by dropping the frequent allocations.
Quoted 17 lines
--- drivers/iio/adc/max1363.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/adc/max1363.c +1
-1@@ -1473,7 +1473,7 @@ static irqreturn_t max1363_trigger_handler(int irq, void *p) d_size = numvals*2 ; else d_size = numvals; -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) { d_size +=sizeof (s64);if (d_size %sizeof (s64)) d_size +=sizeof (s64) - (d_size %sizeof (s64));
Re: [PATCH RFC 4/6] iio: adc: max1363: make use of iio_is_soft_ts_enabled()
Quoted 13 lines · Jonathan Cameron
Quoted 5 lines · On Sat, 30 Nov 2024 01:27:08 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>Hmm. A younger me one ;)
A simple allocation that is always big enough is going to cost us very little. Should almost certainly be using kzalloc.
Quoted 9 lines
I'd change this driver to just stick an array of size 12 * 2 + 8 so 32bytes in the
iio_priv()
(12 channels, possibly of 2 bytes each + aligned timestamp) and always use that for therx_buf.Both a simplification and probably a performance improvement as well by dropping the frequent allocations.
Hi Jonathan,
I see your point, I can implement this as well.
Cheers,
Vasilis
Quoted 18 lines
Quoted 17 lines
--- drivers/iio/adc/max1363.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/adc/max1363.c +1
-1@@ -1473,7 +1473,7 @@ static irqreturn_t max1363_trigger_handler(int irq, void *p) d_size = numvals*2 ; else d_size = numvals; -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) { d_size +=sizeof (s64);if (d_size %sizeof (s64)) d_size +=sizeof (s64) - (d_size %sizeof (s64));
RFC PATCH 5/6 iio: common: ssp_sensors: make use of iio_is_soft_ts_enabled()
In reply to Vasileios AmoiridisUse the iio_is_soft_ts_enabled() accessor to access the value of the scan_timestamp. This way, it can be marked as __private when there are no direct accessors of it.
--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/ssp_sensors/ssp_iio.c +1 -1
@@ -82,7 +82,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,*/ memcpy (spd->buffer, buf, len); -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ;
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH RFC 5/6] iio: common: ssp_sensors: make use of iio_is_soft_ts_enabled()
Quoted 15 lines · On Sat, 30 Nov 2024 01:27:09 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/common/ssp_sensors/ssp_iio.c +0
-0
drivers/iio/common/ssp_sensors/ssp_iio.c int ssp_common_process_data(struct iio_dev *indio_dev, void *buf, · line 85
Quoted 5 lines
drivers/iio/common/ssp_sensors/ssp_iio.c +1
-1@@ -82,7 +82,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,*/ memcpy (spd->buffer, buf, len); -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {
It might be simpler to just drop this conditional in favour of always computing the value.
drivers/iio/common/ssp_sensors/ssp_iio.c int ssp_common_process_data(struct iio_dev *indio_dev, void *buf, · line 88
Quoted 3 lines
drivers/iio/common/ssp_sensors/ssp_iio.c +0
-0memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ;
Good to replace this with something more modern like.
calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;
Jonathan
Re: [PATCH RFC 5/6] iio: common: ssp_sensors: make use of iio_is_soft_ts_enabled()
Quoted 33 lines · Jonathan Cameron
Quoted 15 lines · On Sat, 30 Nov 2024 01:27:09 +0100 Vasileios Amoiridis…
Use the
iio_is_soft_ts_enabled()accessor to access the value of thescan_timestamp. This way, it can be marked as__privatewhen there are no direct accessors of it.Signed-off-by Vasileios Amoiridis <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_iio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/common/ssp_sensors/ssp_iio.c +0
-0drivers/iio/common/ssp_sensors/ssp_iio.c int ssp_common_process_data(struct iio_dev *indio_dev, void *buf, · line 85
Quoted 5 lines
drivers/iio/common/ssp_sensors/ssp_iio.c +1
-1@@ -82,7 +82,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,*/ memcpy (spd->buffer, buf, len); -if (indio_dev->scan_timestamp) { +if (iio_is_soft_ts_enabled (indio_dev)) {It might be simpler to just drop this conditional in favour of always computing the value.
drivers/iio/common/ssp_sensors/ssp_iio.c int ssp_common_process_data(struct iio_dev *indio_dev, void *buf, · line 88
Quoted 3 lines
drivers/iio/common/ssp_sensors/ssp_iio.c +0
-0memcpy (&time, &((char *)buf)[len],SSP_TIME_SIZE ); calculated_time = timestamp + (int64_t )le32_to_cpu (time) *1000000 ;
Quoted 7 lines
Good to replace this with something more modern like.
calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;Jonathan
Hi Jonathan,
I could definitely do this, thanks!
Cheers,
Vasilis
RFC PATCH 6/6 iio: core: mark scan_timestamp as __private
In reply to Vasileios AmoiridisSince there are no more direct accesses to the indio_dev->scan_timestamp value, it can be marked as __private and use the macro ACCESS_PRIVATE() in order to access it. Like this, static checkers will be able to inform in case someone tries to either write to the value, or read its value directly.
--- drivers/iio/industrialio-buffer.c | 2 +- include/linux/iio/iio.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iio/industrialio-buffer.c +1 -1
@@ -1137,7 +1137,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,int ret; indio_dev->active_scan_mask = config->scan_mask; - indio_dev->scan_timestamp = config->scan_timestamp; +ACCESS_PRIVATE (indio_dev, scan_timestamp) = config->scan_timestamp; indio_dev->scan_bytes = config->scan_bytes; iio_dev_opaque->currentmode = config->mode;
include/linux/iio/iio.h +2 -2
@@ -611,7 +611,7 @@ struct iio_dev {const unsigned long *available_scan_masks;unsigned int __private masklength;const unsigned long *active_scan_mask; -bool scan_timestamp; +bool __private scan_timestamp;struct iio_trigger *trig;struct iio_poll_func *pollfunc;struct iio_poll_func *pollfunc_event;
@@ -908,7 +908,7 @@ int iio_active_scan_mask_index(struct iio_dev *indio_dev);*/ static inline bool iio_is_soft_ts_enabled (const struct iio_dev *indio_dev) { -return indio_dev->scan_timestamp; +return ACCESS_PRIVATE (indio_dev, scan_timestamp); }ssize_t iio_format_value (char *buf,unsigned int type,int size,int *vals);
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH RFC 6/6] iio: core: mark scan_timestamp as __private
Quoted 39 lines · On Sat, 30 Nov 2024 01:27:10 +0100 Vasileios Amoiridis…
Since there are no more direct accesses to the
indio_dev->scan_timestampvalue, it can be marked as__privateand use the macroACCESS_PRIVATE()in order to access it. Like this, static checkers will be able to inform in case someone tries to either write to the value, or read its value directly.Signed-off-by Vasileios Amoiridis <[email protected]>--- drivers/iio/industrialio-buffer.c | 2 +- include/linux/iio/iio.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/iio/industrialio-buffer.c +1
-1@@ -1137,7 +1137,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,int ret; indio_dev->active_scan_mask = config->scan_mask; - indio_dev->scan_timestamp = config->scan_timestamp; +ACCESS_PRIVATE (indio_dev, scan_timestamp) = config->scan_timestamp; indio_dev->scan_bytes = config->scan_bytes; iio_dev_opaque->currentmode = config->mode;include/linux/iio/iio.h +1
-1@@ -611,7 +611,7 @@ struct iio_dev {const unsigned long *available_scan_masks;unsigned int __private masklength;const unsigned long *active_scan_mask; -bool scan_timestamp; +bool __private scan_timestamp;struct iio_trigger *trig;struct iio_poll_func *pollfunc;struct iio_poll_func *pollfunc_event;
include/linux/iio/iio.h int iio_active_scan_mask_index(struct iio_dev *indio_dev); · line 911
Quoted 5 lines
include/linux/iio/iio.h +1
-1@@ -908,7 +908,7 @@ int iio_active_scan_mask_index(struct iio_dev *indio_dev);*/ static inline bool iio_is_soft_ts_enabled (const struct iio_dev *indio_dev) { -return indio_dev->scan_timestamp; +return ACCESS_PRIVATE (indio_dev, scan_timestamp);
If we only end up with one use of this (based on feedback on other drivers) I'd tempted to deliberately not provide this convenience function and instead just use ACCESS_PRIVATE() directly in iio_push_to_buffers_with_timestamp()
Nice work. Particularly by highlighting some 'odd corners' in drivers that probably make no real sense to keep ;)
Jonathan
Quoted 3 lines
include/linux/iio/iio.h +0
-0}ssize_t iio_format_value (char *buf,unsigned int type,int size,int *vals);
Re: [PATCH RFC 6/6] iio: core: mark scan_timestamp as __private
Quoted 51 lines · Jonathan Cameron
Quoted 39 lines · On Sat, 30 Nov 2024 01:27:10 +0100 Vasileios Amoiridis…
Since there are no more direct accesses to the
indio_dev->scan_timestampvalue, it can be marked as__privateand use the macroACCESS_PRIVATE()in order to access it. Like this, static checkers will be able to inform in case someone tries to either write to the value, or read its value directly.Signed-off-by Vasileios Amoiridis <[email protected]>--- drivers/iio/industrialio-buffer.c | 2 +- include/linux/iio/iio.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/iio/industrialio-buffer.c +1
-1@@ -1137,7 +1137,7 @@ static int iio_enable_buffers(struct iio_dev *indio_dev,int ret; indio_dev->active_scan_mask = config->scan_mask; - indio_dev->scan_timestamp = config->scan_timestamp; +ACCESS_PRIVATE (indio_dev, scan_timestamp) = config->scan_timestamp; indio_dev->scan_bytes = config->scan_bytes; iio_dev_opaque->currentmode = config->mode;include/linux/iio/iio.h +1
-1@@ -611,7 +611,7 @@ struct iio_dev {const unsigned long *available_scan_masks;unsigned int __private masklength;const unsigned long *active_scan_mask; -bool scan_timestamp; +bool __private scan_timestamp;struct iio_trigger *trig;struct iio_poll_func *pollfunc;struct iio_poll_func *pollfunc_event;include/linux/iio/iio.h int iio_active_scan_mask_index(struct iio_dev *indio_dev); · line 911
Quoted 5 lines
include/linux/iio/iio.h +1
-1@@ -908,7 +908,7 @@ int iio_active_scan_mask_index(struct iio_dev *indio_dev);*/ static inline bool iio_is_soft_ts_enabled (const struct iio_dev *indio_dev) { -return indio_dev->scan_timestamp; +return ACCESS_PRIVATE (indio_dev, scan_timestamp);If we only end up with one use of this (based on feedback on other drivers) I'd tempted to deliberately not provide this convenience function and instead just use
ACCESS_PRIVATE()directly iniio_push_to_buffers_with_timestamp()
Quoted 10 lines
Nice work. Particularly by highlighting some 'odd corners' in drivers that probably make no real sense to keep ;)
Jonathan
Quoted 3 lines
include/linux/iio/iio.h +0
-0}ssize_t iio_format_value (char *buf,unsigned int type,int size,int *vals);
Hi Jonathan,
Indeed, if it is only one case that this is being used, it wouldn't make sense to provide an accessor. I wouldn't think of going directly to touch the drivers without sending this RFC first, so it's good that you like the solution of optimizing the drivers themselves. I might find some time before the weekend to spin a v2 to discuss. Thanks for your time, your comments are always of great help!
Cheers,
Vasilis