[PATCH 4/8] counter: interrupt-cnt: Use container_of instead of struct counter_device::priv

From: Uwe Kleine-König
Date: Tue Dec 21 2021 - 05:46:10 EST


Using counter->priv is a memory read and so more expensive than
container_of which is only an addition.

So container_of is expected to be a tad faster, it's type-safe, and
produces smaller code (ARCH=arm allmodconfig):

add/remove: 0/0 grow/shrink: 0/6 up/down: 0/-60 (-60)
Function old new delta
interrupt_cnt_write 136 128 -8
interrupt_cnt_read 92 84 -8
interrupt_cnt_enable_write 180 172 -8
interrupt_cnt_enable_read 76 68 -8
interrupt_cnt_probe 1048 1036 -12
interrupt_cnt_signal_read 164 148 -16
Total: Before=2841, After=2781, chg -2.11%

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx>
---
drivers/counter/interrupt-cnt.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/counter/interrupt-cnt.c b/drivers/counter/interrupt-cnt.c
index 8514a87fcbee..c61628aa5c32 100644
--- a/drivers/counter/interrupt-cnt.c
+++ b/drivers/counter/interrupt-cnt.c
@@ -25,6 +25,11 @@ struct interrupt_cnt_priv {
struct counter_count cnts;
};

+static inline struct interrupt_cnt_priv *interrupt_cnt_from_counter(struct counter_device *counter)
+{
+ return container_of(counter, struct interrupt_cnt_priv, counter);
+}
+
static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
{
struct interrupt_cnt_priv *priv = dev_id;
@@ -37,7 +42,7 @@ static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
static int interrupt_cnt_enable_read(struct counter_device *counter,
struct counter_count *count, u8 *enable)
{
- struct interrupt_cnt_priv *priv = counter->priv;
+ struct interrupt_cnt_priv *priv = interrupt_cnt_from_counter(counter);

*enable = priv->enabled;

@@ -47,7 +52,7 @@ static int interrupt_cnt_enable_read(struct counter_device *counter,
static int interrupt_cnt_enable_write(struct counter_device *counter,
struct counter_count *count, u8 enable)
{
- struct interrupt_cnt_priv *priv = counter->priv;
+ struct interrupt_cnt_priv *priv = interrupt_cnt_from_counter(counter);

if (priv->enabled == enable)
return 0;
@@ -85,7 +90,7 @@ static int interrupt_cnt_action_read(struct counter_device *counter,
static int interrupt_cnt_read(struct counter_device *counter,
struct counter_count *count, u64 *val)
{
- struct interrupt_cnt_priv *priv = counter->priv;
+ struct interrupt_cnt_priv *priv = interrupt_cnt_from_counter(counter);

*val = atomic_read(&priv->count);

@@ -95,7 +100,7 @@ static int interrupt_cnt_read(struct counter_device *counter,
static int interrupt_cnt_write(struct counter_device *counter,
struct counter_count *count, const u64 val)
{
- struct interrupt_cnt_priv *priv = counter->priv;
+ struct interrupt_cnt_priv *priv = interrupt_cnt_from_counter(counter);

if (val != (typeof(priv->count.counter))val)
return -ERANGE;
@@ -122,7 +127,7 @@ static int interrupt_cnt_signal_read(struct counter_device *counter,
struct counter_signal *signal,
enum counter_signal_level *level)
{
- struct interrupt_cnt_priv *priv = counter->priv;
+ struct interrupt_cnt_priv *priv = interrupt_cnt_from_counter(counter);
int ret;

if (!priv->gpio)
@@ -199,7 +204,6 @@ static int interrupt_cnt_probe(struct platform_device *pdev)
priv->cnts.ext = interrupt_cnt_ext;
priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);

- priv->counter.priv = priv;
priv->counter.name = dev_name(dev);
priv->counter.parent = dev;
priv->counter.ops = &interrupt_cnt_ops;
--
2.33.0