From db889a70afab82447fb19c73e12b5e21f1e6c245 Mon Sep 17 00:00:00 2001
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date: Sat, 16 Oct 2021 10:49:03 +0200
Subject: [PATCH 039/158] gen_stats: Add gnet_stats_add_queue().

This function will replace __gnet_stats_copy_queue(). It reads all
arguments and adds them into the passed gnet_stats_queue argument.
In contrast to __gnet_stats_copy_queue() it also copies the qlen member.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
---
 include/net/gen_stats.h |  3 +++
 net/core/gen_stats.c    | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Index: linux-5.15.19-rt29/include/net/gen_stats.h
===================================================================
@ linux-5.15.19-rt29/include/net/gen_stats.h:65 @ int gnet_stats_copy_queue(struct gnet_du
 void __gnet_stats_copy_queue(struct gnet_stats_queue *qstats,
 			     const struct gnet_stats_queue __percpu *cpu_q,
 			     const struct gnet_stats_queue *q, __u32 qlen);
+void gnet_stats_add_queue(struct gnet_stats_queue *qstats,
+			  const struct gnet_stats_queue __percpu *cpu_q,
+			  const struct gnet_stats_queue *q);
 int gnet_stats_copy_app(struct gnet_dump *d, void *st, int len);
 
 int gnet_stats_finish_copy(struct gnet_dump *d);
Index: linux-5.15.19-rt29/net/core/gen_stats.c
===================================================================
--- linux-5.15.19-rt29.orig/net/core/gen_stats.c
+++ linux-5.15.19-rt29/net/core/gen_stats.c
@ linux-5.15.19-rt29/include/net/gen_stats.h:324 @ void __gnet_stats_copy_queue(struct gnet
 }
 EXPORT_SYMBOL(__gnet_stats_copy_queue);
 
+static void gnet_stats_add_queue_cpu(struct gnet_stats_queue *qstats,
+				     const struct gnet_stats_queue __percpu *q)
+{
+	int i;
+
+	for_each_possible_cpu(i) {
+		const struct gnet_stats_queue *qcpu = per_cpu_ptr(q, i);
+
+		qstats->qlen += qcpu->backlog;
+		qstats->backlog += qcpu->backlog;
+		qstats->drops += qcpu->drops;
+		qstats->requeues += qcpu->requeues;
+		qstats->overlimits += qcpu->overlimits;
+	}
+}
+
+void gnet_stats_add_queue(struct gnet_stats_queue *qstats,
+			  const struct gnet_stats_queue __percpu *cpu,
+			  const struct gnet_stats_queue *q)
+{
+	if (cpu) {
+		gnet_stats_add_queue_cpu(qstats, cpu);
+	} else {
+		qstats->qlen += q->qlen;
+		qstats->backlog += q->backlog;
+		qstats->drops += q->drops;
+		qstats->requeues += q->requeues;
+		qstats->overlimits += q->overlimits;
+	}
+}
+EXPORT_SYMBOL(gnet_stats_add_queue);
+
 /**
  * gnet_stats_copy_queue - copy queue statistics into statistics TLV
  * @d: dumping handle