diff mbox series

[v2,3/4] iio: common: ssp_sensors: drop conditional optimization for simplicity

Message ID 20241214191421.94172-4-vassilisamir@gmail.com (mailing list archive)
State Accepted
Headers show
Series iio: mark scan_timestamp as __private | expand

Commit Message

Vasileios Amoiridis Dec. 14, 2024, 7:14 p.m. UTC
Drop conditional in favor of always calculating the timestamp value.
This simplifies the code and allows to drop usage of internal private
variable "scan_timestamp" of the struct iio_dev.

Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
---
 drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Comments

David Lechner Dec. 16, 2024, 9:57 p.m. UTC | #1
On 12/14/24 1:14 PM, Vasileios Amoiridis wrote:
> Drop conditional in favor of always calculating the timestamp value.
> This simplifies the code and allows to drop usage of internal private
> variable "scan_timestamp" of the struct iio_dev.
> 
> Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
> ---
>  drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
> index caa404edd9d0..6b86b5315694 100644
> --- a/drivers/iio/common/ssp_sensors/ssp_iio.c
> +++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
> @@ -8,6 +8,8 @@
>  #include <linux/iio/kfifo_buf.h>
>  #include <linux/module.h>
>  #include <linux/slab.h>
> +#include <linux/unaligned.h>
> +#include <linux/units.h>
>  #include "ssp_iio_sensor.h"
>  
>  /**
> @@ -70,7 +72,6 @@ EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
>  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
>  			    unsigned int len, int64_t timestamp)
>  {
> -	__le32 time;
>  	int64_t calculated_time = 0;
>  	struct ssp_sensor_data *spd = iio_priv(indio_dev);
>  
> @@ -82,11 +83,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
>  	 */
>  	memcpy(spd->buffer, buf, len);
>  
> -	if (indio_dev->scan_timestamp) {
> -		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
> -		calculated_time =
> -			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
> -	}
> +	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;

Don't we still need to cast to 64 bit to avoid multiplication overflow?

>  
>  	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
>  						  calculated_time);
Vasileios Amoiridis Dec. 17, 2024, 11:41 p.m. UTC | #2
On Mon, Dec 16, 2024 at 03:57:44PM -0600, David Lechner wrote:
> On 12/14/24 1:14 PM, Vasileios Amoiridis wrote:
> > Drop conditional in favor of always calculating the timestamp value.
> > This simplifies the code and allows to drop usage of internal private
> > variable "scan_timestamp" of the struct iio_dev.
> > 
> > Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
> > ---
> >  drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
> >  1 file changed, 3 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
> > index caa404edd9d0..6b86b5315694 100644
> > --- a/drivers/iio/common/ssp_sensors/ssp_iio.c
> > +++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
> > @@ -8,6 +8,8 @@
> >  #include <linux/iio/kfifo_buf.h>
> >  #include <linux/module.h>
> >  #include <linux/slab.h>
> > +#include <linux/unaligned.h>
> > +#include <linux/units.h>
> >  #include "ssp_iio_sensor.h"
> >  
> >  /**
> > @@ -70,7 +72,6 @@ EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
> >  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> >  			    unsigned int len, int64_t timestamp)
> >  {
> > -	__le32 time;
> >  	int64_t calculated_time = 0;
> >  	struct ssp_sensor_data *spd = iio_priv(indio_dev);
> >  
> > @@ -82,11 +83,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> >  	 */
> >  	memcpy(spd->buffer, buf, len);
> >  
> > -	if (indio_dev->scan_timestamp) {
> > -		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
> > -		calculated_time =
> > -			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
> > -	}
> > +	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;
> 
> Don't we still need to cast to 64 bit to avoid multiplication overflow?
>

Hi David,

Thanks for your message!

Aren't we already covered by the fact that MEGA is defined as an
unsigned long?

	include/linux/units.h:12:#define MEGA 1000000UL

Cheers,
Vasilis

> >  
> >  	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
> >  						  calculated_time);
>
David Lechner Dec. 18, 2024, 3:17 p.m. UTC | #3
On 12/17/24 5:41 PM, Vasileios Amoiridis wrote:
> On Mon, Dec 16, 2024 at 03:57:44PM -0600, David Lechner wrote:
>> On 12/14/24 1:14 PM, Vasileios Amoiridis wrote:
>>> Drop conditional in favor of always calculating the timestamp value.
>>> This simplifies the code and allows to drop usage of internal private
>>> variable "scan_timestamp" of the struct iio_dev.
>>>
>>> Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
>>> ---
>>>  drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
>>>  1 file changed, 3 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
>>> index caa404edd9d0..6b86b5315694 100644
>>> --- a/drivers/iio/common/ssp_sensors/ssp_iio.c
>>> +++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
>>> @@ -8,6 +8,8 @@
>>>  #include <linux/iio/kfifo_buf.h>
>>>  #include <linux/module.h>
>>>  #include <linux/slab.h>
>>> +#include <linux/unaligned.h>
>>> +#include <linux/units.h>
>>>  #include "ssp_iio_sensor.h"
>>>  
>>>  /**
>>> @@ -70,7 +72,6 @@ EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
>>>  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
>>>  			    unsigned int len, int64_t timestamp)
>>>  {
>>> -	__le32 time;
>>>  	int64_t calculated_time = 0;
>>>  	struct ssp_sensor_data *spd = iio_priv(indio_dev);
>>>  
>>> @@ -82,11 +83,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
>>>  	 */
>>>  	memcpy(spd->buffer, buf, len);
>>>  
>>> -	if (indio_dev->scan_timestamp) {
>>> -		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
>>> -		calculated_time =
>>> -			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
>>> -	}
>>> +	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;
>>
>> Don't we still need to cast to 64 bit to avoid multiplication overflow?
>>
> 
> Hi David,
> 
> Thanks for your message!
> 
> Aren't we already covered by the fact that MEGA is defined as an
> unsigned long?

That is only 64-bits on 64-bit architectures, so could still overflow on
32-bit architectures where long is 32-bit.

> 
> 	include/linux/units.h:12:#define MEGA 1000000UL
> 
> Cheers,
> Vasilis
> 
>>>  
>>>  	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
>>>  						  calculated_time);
>>
Vasileios Amoiridis Dec. 18, 2024, 9:13 p.m. UTC | #4
On Wed, Dec 18, 2024 at 09:17:44AM -0600, David Lechner wrote:
> On 12/17/24 5:41 PM, Vasileios Amoiridis wrote:
> > On Mon, Dec 16, 2024 at 03:57:44PM -0600, David Lechner wrote:
> >> On 12/14/24 1:14 PM, Vasileios Amoiridis wrote:
> >>> Drop conditional in favor of always calculating the timestamp value.
> >>> This simplifies the code and allows to drop usage of internal private
> >>> variable "scan_timestamp" of the struct iio_dev.
> >>>
> >>> Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
> >>> ---
> >>>  drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
> >>>  1 file changed, 3 insertions(+), 6 deletions(-)
> >>>
> >>> diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
> >>> index caa404edd9d0..6b86b5315694 100644
> >>> --- a/drivers/iio/common/ssp_sensors/ssp_iio.c
> >>> +++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
> >>> @@ -8,6 +8,8 @@
> >>>  #include <linux/iio/kfifo_buf.h>
> >>>  #include <linux/module.h>
> >>>  #include <linux/slab.h>
> >>> +#include <linux/unaligned.h>
> >>> +#include <linux/units.h>
> >>>  #include "ssp_iio_sensor.h"
> >>>  
> >>>  /**
> >>> @@ -70,7 +72,6 @@ EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
> >>>  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> >>>  			    unsigned int len, int64_t timestamp)
> >>>  {
> >>> -	__le32 time;
> >>>  	int64_t calculated_time = 0;
> >>>  	struct ssp_sensor_data *spd = iio_priv(indio_dev);
> >>>  
> >>> @@ -82,11 +83,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> >>>  	 */
> >>>  	memcpy(spd->buffer, buf, len);
> >>>  
> >>> -	if (indio_dev->scan_timestamp) {
> >>> -		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
> >>> -		calculated_time =
> >>> -			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
> >>> -	}
> >>> +	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;
> >>
> >> Don't we still need to cast to 64 bit to avoid multiplication overflow?
> >>
> > 
> > Hi David,
> > 
> > Thanks for your message!
> > 
> > Aren't we already covered by the fact that MEGA is defined as an
> > unsigned long?
> 
> That is only 64-bits on 64-bit architectures, so could still overflow on
> 32-bit architectures where long is 32-bit.
> 

Hi David,

Hmmm, I think you are right. I can fix it in next, iteration. I will
wait also for Jonathan's comments on the rest of the series.

Cheers,
Vasilis

> > 
> > 	include/linux/units.h:12:#define MEGA 1000000UL
> > 
> > Cheers,
> > Vasilis
> > 
> >>>  
> >>>  	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
> >>>  						  calculated_time);
> >>
>
Jonathan Cameron Dec. 19, 2024, 5:47 p.m. UTC | #5
On Wed, 18 Dec 2024 22:13:55 +0100
Vasileios Amoiridis <vassilisamir@gmail.com> wrote:

> On Wed, Dec 18, 2024 at 09:17:44AM -0600, David Lechner wrote:
> > On 12/17/24 5:41 PM, Vasileios Amoiridis wrote:  
> > > On Mon, Dec 16, 2024 at 03:57:44PM -0600, David Lechner wrote:  
> > >> On 12/14/24 1:14 PM, Vasileios Amoiridis wrote:  
> > >>> Drop conditional in favor of always calculating the timestamp value.
> > >>> This simplifies the code and allows to drop usage of internal private
> > >>> variable "scan_timestamp" of the struct iio_dev.
> > >>>
> > >>> Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
> > >>> ---
> > >>>  drivers/iio/common/ssp_sensors/ssp_iio.c | 9 +++------
> > >>>  1 file changed, 3 insertions(+), 6 deletions(-)
> > >>>
> > >>> diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
> > >>> index caa404edd9d0..6b86b5315694 100644
> > >>> --- a/drivers/iio/common/ssp_sensors/ssp_iio.c
> > >>> +++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
> > >>> @@ -8,6 +8,8 @@
> > >>>  #include <linux/iio/kfifo_buf.h>
> > >>>  #include <linux/module.h>
> > >>>  #include <linux/slab.h>
> > >>> +#include <linux/unaligned.h>
> > >>> +#include <linux/units.h>
> > >>>  #include "ssp_iio_sensor.h"
> > >>>  
> > >>>  /**
> > >>> @@ -70,7 +72,6 @@ EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
> > >>>  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> > >>>  			    unsigned int len, int64_t timestamp)
> > >>>  {
> > >>> -	__le32 time;
> > >>>  	int64_t calculated_time = 0;
> > >>>  	struct ssp_sensor_data *spd = iio_priv(indio_dev);
> > >>>  
> > >>> @@ -82,11 +83,7 @@ int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
> > >>>  	 */
> > >>>  	memcpy(spd->buffer, buf, len);
> > >>>  
> > >>> -	if (indio_dev->scan_timestamp) {
> > >>> -		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
> > >>> -		calculated_time =
> > >>> -			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
> > >>> -	}
> > >>> +	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;  
> > >>
> > >> Don't we still need to cast to 64 bit to avoid multiplication overflow?
> > >>  
> > > 
> > > Hi David,
> > > 
> > > Thanks for your message!
> > > 
> > > Aren't we already covered by the fact that MEGA is defined as an
> > > unsigned long?  
> > 
> > That is only 64-bits on 64-bit architectures, so could still overflow on
> > 32-bit architectures where long is 32-bit.
> >   
> 
> Hi David,
> 
> Hmmm, I think you are right. I can fix it in next, iteration. I will
> wait also for Jonathan's comments on the rest of the series.
> 
I tweaked it and applied.  Also dropped the initial assignment of calculated_time given it is now
always set before use.

Thanks,

Jonathan


> Cheers,
> Vasilis
> 
> > > 
> > > 	include/linux/units.h:12:#define MEGA 1000000UL
> > > 
> > > Cheers,
> > > Vasilis
> > >   
> > >>>  
> > >>>  	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
> > >>>  						  calculated_time);  
> > >>  
> >
diff mbox series

Patch

diff --git a/drivers/iio/common/ssp_sensors/ssp_iio.c b/drivers/iio/common/ssp_sensors/ssp_iio.c
index caa404edd9d0..6b86b5315694 100644
--- a/drivers/iio/common/ssp_sensors/ssp_iio.c
+++ b/drivers/iio/common/ssp_sensors/ssp_iio.c
@@ -8,6 +8,8 @@ 
 #include <linux/iio/kfifo_buf.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/unaligned.h>
+#include <linux/units.h>
 #include "ssp_iio_sensor.h"
 
 /**
@@ -70,7 +72,6 @@  EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, "IIO_SSP_SENSORS");
 int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
 			    unsigned int len, int64_t timestamp)
 {
-	__le32 time;
 	int64_t calculated_time = 0;
 	struct ssp_sensor_data *spd = iio_priv(indio_dev);
 
@@ -82,11 +83,7 @@  int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
 	 */
 	memcpy(spd->buffer, buf, len);
 
-	if (indio_dev->scan_timestamp) {
-		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
-		calculated_time =
-			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
-	}
+	calculated_time = timestamp + get_unaligned_le32(buf + len) * MEGA;
 
 	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
 						  calculated_time);