diff mbox series

[29/30] backports: Add sysfs_emit()

Message ID 20201201220415.30582-30-hauke@hauke-m.de (mailing list archive)
State New, archived
Headers show
Series backports: Update to match kernel 5.10-rc6 | expand

Commit Message

Hauke Mehrtens Dec. 1, 2020, 10:04 p.m. UTC
Add the sysfs_emit() function which was added in Linux upstream commit
2efc459d06f1 ("sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs
output") and is now used by drivers-base-devcoredump.c

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 backport/backport-include/linux/sysfs.h | 14 +++++++++++++
 backport/compat/backport-5.10.c         | 27 +++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
diff mbox series

Patch

diff --git a/backport/backport-include/linux/sysfs.h b/backport/backport-include/linux/sysfs.h
index 0b71db54..ad8a8229 100644
--- a/backport/backport-include/linux/sysfs.h
+++ b/backport/backport-include/linux/sysfs.h
@@ -8,4 +8,18 @@ 
 			 _name##_show, _name##_store)
 #endif
 
+#if LINUX_VERSION_IS_LESS(5,10,0)
+#define sysfs_emit LINUX_BACKPORT(sysfs_emit)
+#ifdef CONFIG_SYSFS
+__printf(2, 3)
+int sysfs_emit(char *buf, const char *fmt, ...);
+#else /* CONFIG_SYSFS */
+__printf(2, 3)
+static inline int sysfs_emit(char *buf, const char *fmt, ...)
+{
+	return 0;
+}
+#endif /* CONFIG_SYSFS */
+#endif /* < 5.10 */
+
 #endif /* __BACKPORT_LINUX_SYSFS_H */
diff --git a/backport/compat/backport-5.10.c b/backport/compat/backport-5.10.c
index 141862da..c3a7b7d3 100644
--- a/backport/compat/backport-5.10.c
+++ b/backport/compat/backport-5.10.c
@@ -3,6 +3,7 @@ 
 #include <linux/export.h>
 #include <linux/kernel.h>
 #include <linux/netdevice.h>
+#include <linux/sysfs.h>
 
 /**
  *	dev_fetch_sw_netstats - get per-cpu network device statistics
@@ -52,3 +53,29 @@  int netif_rx_any_context(struct sk_buff *skb)
 		return netif_rx_ni(skb);
 }
 EXPORT_SYMBOL(netif_rx_any_context);
+
+/**
+ *	sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
+ *	@buf:	start of PAGE_SIZE buffer.
+ *	@fmt:	format
+ *	@...:	optional arguments to @format
+ *
+ *
+ * Returns number of characters written to @buf.
+ */
+int sysfs_emit(char *buf, const char *fmt, ...)
+{
+	va_list args;
+	int len;
+
+	if (WARN(!buf || offset_in_page(buf),
+		 "invalid sysfs_emit: buf:%p\n", buf))
+		return 0;
+
+	va_start(args, fmt);
+	len = vscnprintf(buf, PAGE_SIZE, fmt, args);
+	va_end(args);
+
+	return len;
+}
+EXPORT_SYMBOL_GPL(sysfs_emit);