Blame SOURCES/0001-eal-abstract-away-the-auxiliary-vector.patch

c7ffa4
From 2ed9bf330709e75c2f066f6ec13ece1ecdb4e9db Mon Sep 17 00:00:00 2001
c7ffa4
From: Aaron Conole <aconole@redhat.com>
c7ffa4
Date: Mon, 2 Apr 2018 14:24:34 -0400
c7ffa4
Subject: [PATCH] eal: abstract away the auxiliary vector
c7ffa4
c7ffa4
Rather than attempting to load the contents of the auxv directly,
c7ffa4
prefer to use an exposed API - and if that doesn't exist then attempt
c7ffa4
to load the vector.  This is because on some systems, when a user
c7ffa4
is downgraded, the /proc/self/auxv file retains the old ownership
c7ffa4
and permissions.  The original method of /proc/self/auxv is retained.
c7ffa4
c7ffa4
This also removes a potential abort() in the code when compiled with
c7ffa4
NDEBUG.  A quick parse of the code shows that many (if not all) of
c7ffa4
the CPU flag parsing isn't used internally, so it should be okay.
c7ffa4
c7ffa4
Signed-off-by: Aaron Conole <aconole@redhat.com>
c7ffa4
Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
c7ffa4
---
c7ffa4
 lib/librte_eal/common/arch/arm/rte_cpuflags.c | 20 +----
c7ffa4
 .../common/arch/ppc_64/rte_cpuflags.c         | 15 +---
c7ffa4
 lib/librte_eal/common/eal_common_cpuflags.c   | 79 +++++++++++++++++++
c7ffa4
 .../common/include/generic/rte_cpuflags.h     | 21 +++++
c7ffa4
 4 files changed, 106 insertions(+), 29 deletions(-)
c7ffa4
c7ffa4
diff --git a/lib/librte_eal/common/arch/arm/rte_cpuflags.c b/lib/librte_eal/common/arch/arm/rte_cpuflags.c
c7ffa4
index 390a19a26..caf3dc83a 100644
c7ffa4
--- a/lib/librte_eal/common/arch/arm/rte_cpuflags.c
c7ffa4
+++ b/lib/librte_eal/common/arch/arm/rte_cpuflags.c
c7ffa4
@@ -105,22 +105,10 @@ const struct feature_entry rte_cpu_feature_table[] = {
c7ffa4
 static void
c7ffa4
 rte_cpu_get_features(hwcap_registers_t out)
c7ffa4
 {
c7ffa4
-	int auxv_fd;
c7ffa4
-	_Elfx_auxv_t auxv;
c7ffa4
-
c7ffa4
-	auxv_fd = open("/proc/self/auxv", O_RDONLY);
c7ffa4
-	assert(auxv_fd != -1);
c7ffa4
-	while (read(auxv_fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
c7ffa4
-		if (auxv.a_type == AT_HWCAP) {
c7ffa4
-			out[REG_HWCAP] = auxv.a_un.a_val;
c7ffa4
-		} else if (auxv.a_type == AT_HWCAP2) {
c7ffa4
-			out[REG_HWCAP2] = auxv.a_un.a_val;
c7ffa4
-		} else if (auxv.a_type == AT_PLATFORM) {
c7ffa4
-			if (!strcmp((const char *)auxv.a_un.a_val, PLATFORM_STR))
c7ffa4
-				out[REG_PLATFORM] = 0x0001;
c7ffa4
-		}
c7ffa4
-	}
c7ffa4
-	close(auxv_fd);
c7ffa4
+	out[REG_HWCAP] = rte_cpu_getauxval(AT_HWCAP);
c7ffa4
+	out[REG_HWCAP2] = rte_cpu_getauxval(AT_HWCAP2);
c7ffa4
+	if (!rte_cpu_strcmp_auxval(AT_PLATFORM, PLATFORM_STR))
c7ffa4
+		out[REG_PLATFORM] = 0x0001;
c7ffa4
 }
c7ffa4
 
c7ffa4
 /*
c7ffa4
diff --git a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
c7ffa4
index 970a61c5e..e7a82452b 100644
c7ffa4
--- a/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
c7ffa4
+++ b/lib/librte_eal/common/arch/ppc_64/rte_cpuflags.c
c7ffa4
@@ -104,19 +104,8 @@ const struct feature_entry rte_cpu_feature_table[] = {
c7ffa4
 static void
c7ffa4
 rte_cpu_get_features(hwcap_registers_t out)
c7ffa4
 {
c7ffa4
-	int auxv_fd;
c7ffa4
-	Elf64_auxv_t auxv;
c7ffa4
-
c7ffa4
-	auxv_fd = open("/proc/self/auxv", O_RDONLY);
c7ffa4
-	assert(auxv_fd != -1);
c7ffa4
-	while (read(auxv_fd, &auxv,
c7ffa4
-		sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t)) {
c7ffa4
-		if (auxv.a_type == AT_HWCAP)
c7ffa4
-			out[REG_HWCAP] = auxv.a_un.a_val;
c7ffa4
-		else if (auxv.a_type == AT_HWCAP2)
c7ffa4
-			out[REG_HWCAP2] = auxv.a_un.a_val;
c7ffa4
-	}
c7ffa4
-	close(auxv_fd);
c7ffa4
+	out[REG_HWCAP] = rte_cpu_getauxval(AT_HWCAP);
c7ffa4
+	out[REG_HWCAP2] = rte_cpu_getauxval(AT_HWCAP2);
c7ffa4
 }
c7ffa4
 
c7ffa4
 /*
c7ffa4
diff --git a/lib/librte_eal/common/eal_common_cpuflags.c b/lib/librte_eal/common/eal_common_cpuflags.c
c7ffa4
index 3a055f7c7..a09667563 100644
c7ffa4
--- a/lib/librte_eal/common/eal_common_cpuflags.c
c7ffa4
+++ b/lib/librte_eal/common/eal_common_cpuflags.c
c7ffa4
@@ -2,11 +2,90 @@
c7ffa4
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c7ffa4
  */
c7ffa4
 
c7ffa4
+#include <elf.h>
c7ffa4
+#include <fcntl.h>
c7ffa4
 #include <stdio.h>
c7ffa4
+#include <string.h>
c7ffa4
+#include <sys/stat.h>
c7ffa4
+#include <sys/types.h>
c7ffa4
+#include <unistd.h>
c7ffa4
+
c7ffa4
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
c7ffa4
+#if __GLIBC_PREREQ(2, 16)
c7ffa4
+#include <sys/auxv.h>
c7ffa4
+#define HAS_AUXV 1
c7ffa4
+#endif
c7ffa4
+#endif
c7ffa4
 
c7ffa4
 #include <rte_common.h>
c7ffa4
 #include <rte_cpuflags.h>
c7ffa4
 
c7ffa4
+#ifndef HAS_AUXV
c7ffa4
+static unsigned long
c7ffa4
+getauxval(unsigned long type)
c7ffa4
+{
c7ffa4
+	errno = ENOTSUP;
c7ffa4
+	return 0;
c7ffa4
+}
c7ffa4
+#endif
c7ffa4
+
c7ffa4
+#ifdef RTE_ARCH_64
c7ffa4
+typedef Elf64_auxv_t Internal_Elfx_auxv_t;
c7ffa4
+#else
c7ffa4
+typedef Elf32_auxv_t Internal_Elfx_auxv_t;
c7ffa4
+#endif
c7ffa4
+
c7ffa4
+
c7ffa4
+/**
c7ffa4
+ * Provides a method for retrieving values from the auxiliary vector and
c7ffa4
+ * possibly running a string comparison.
c7ffa4
+ *
c7ffa4
+ * @return Always returns a result.  When the result is 0, check errno
c7ffa4
+ * to see if an error occurred during processing.
c7ffa4
+ */
c7ffa4
+static unsigned long
c7ffa4
+_rte_cpu_getauxval(unsigned long type, const char *str)
c7ffa4
+{
c7ffa4
+	unsigned long val;
c7ffa4
+
c7ffa4
+	errno = 0;
c7ffa4
+	val = getauxval(type);
c7ffa4
+
c7ffa4
+	if (!val && (errno == ENOTSUP || errno == ENOENT)) {
c7ffa4
+		int auxv_fd = open("/proc/self/auxv", O_RDONLY);
c7ffa4
+		Internal_Elfx_auxv_t auxv;
c7ffa4
+
c7ffa4
+		if (auxv_fd == -1)
c7ffa4
+			return 0;
c7ffa4
+
c7ffa4
+		errno = ENOENT;
c7ffa4
+		while (read(auxv_fd, &auxv, sizeof(auxv)) == sizeof(auxv)) {
c7ffa4
+			if (auxv.a_type == type) {
c7ffa4
+				errno = 0;
c7ffa4
+				val = auxv.a_un.a_val;
c7ffa4
+				if (str)
c7ffa4
+					val = strcmp((const char *)val, str);
c7ffa4
+				break;
c7ffa4
+			}
c7ffa4
+		}
c7ffa4
+		close(auxv_fd);
c7ffa4
+	}
c7ffa4
+
c7ffa4
+	return val;
c7ffa4
+}
c7ffa4
+
c7ffa4
+unsigned long
c7ffa4
+rte_cpu_getauxval(unsigned long type)
c7ffa4
+{
c7ffa4
+	return _rte_cpu_getauxval(type, NULL);
c7ffa4
+}
c7ffa4
+
c7ffa4
+int
c7ffa4
+rte_cpu_strcmp_auxval(unsigned long type, const char *str)
c7ffa4
+{
c7ffa4
+	return _rte_cpu_getauxval(type, str);
c7ffa4
+}
c7ffa4
+
c7ffa4
 /**
c7ffa4
  * Checks if the machine is adequate for running the binary. If it is not, the
c7ffa4
  * program exits with status 1.
c7ffa4
diff --git a/lib/librte_eal/common/include/generic/rte_cpuflags.h b/lib/librte_eal/common/include/generic/rte_cpuflags.h
c7ffa4
index 8d31687d8..156ea0029 100644
c7ffa4
--- a/lib/librte_eal/common/include/generic/rte_cpuflags.h
c7ffa4
+++ b/lib/librte_eal/common/include/generic/rte_cpuflags.h
c7ffa4
@@ -64,4 +64,25 @@ rte_cpu_check_supported(void);
c7ffa4
 int
c7ffa4
 rte_cpu_is_supported(void);
c7ffa4
 
c7ffa4
+/**
c7ffa4
+ * This function attempts to retrieve a value from the auxiliary vector.
c7ffa4
+ * If it is unsuccessful, the result will be 0, and errno will be set.
c7ffa4
+ *
c7ffa4
+ * @return A value from the auxiliary vector.  When the value is 0, check
c7ffa4
+ * errno to determine if an error occurred.
c7ffa4
+ */
c7ffa4
+unsigned long
c7ffa4
+rte_cpu_getauxval(unsigned long type);
c7ffa4
+
c7ffa4
+/**
c7ffa4
+ * This function retrieves a value from the auxiliary vector, and compares it
c7ffa4
+ * as a string against the value retrieved.
c7ffa4
+ *
c7ffa4
+ * @return The result of calling strcmp() against the value retrieved from
c7ffa4
+ * the auxiliary vector.  When the value is 0 (meaning a match is found),
c7ffa4
+ * check errno to determine if an error occurred.
c7ffa4
+ */
c7ffa4
+int
c7ffa4
+rte_cpu_strcmp_auxval(unsigned long type, const char *str);
c7ffa4
+
c7ffa4
 #endif /* _RTE_CPUFLAGS_H_ */
c7ffa4
-- 
c7ffa4
2.17.0
c7ffa4