From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <jia.guo@intel.com>
Received: from mga09.intel.com (mga09.intel.com [134.134.136.24])
 by dpdk.org (Postfix) with ESMTP id E71675A6A
 for <dev@dpdk.org>; Tue,  2 Oct 2018 14:32:01 +0200 (CEST)
X-Amp-Result: SKIPPED(no attachment in message)
X-Amp-File-Uploaded: False
Received: from fmsmga006.fm.intel.com ([10.253.24.20])
 by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;
 02 Oct 2018 05:32:01 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.54,331,1534834800"; d="scan'208";a="268749958"
Received: from jeffguo-s2600wt2.sh.intel.com (HELO localhost.localdomain)
 ([10.67.110.10])
 by fmsmga006.fm.intel.com with ESMTP; 02 Oct 2018 05:31:58 -0700
From: Jeff Guo <jia.guo@intel.com>
To: stephen@networkplumber.org, bruce.richardson@intel.com,
 ferruh.yigit@intel.com, konstantin.ananyev@intel.com,
 gaetan.rivet@6wind.com, jingjing.wu@intel.com, thomas@monjalon.net,
 motih@mellanox.com, matan@mellanox.com, harry.van.haaren@intel.com,
 qi.z.zhang@intel.com, shaopeng.he@intel.com, bernard.iremonger@intel.com,
 arybchenko@solarflare.com, wenzhuo.lu@intel.com, anatoly.burakov@intel.com,
 jerin.jacob@caviumnetworks.com
Cc: jblunck@infradead.org, shreyansh.jain@nxp.com, dev@dpdk.org,
 jia.guo@intel.com, helin.zhang@intel.com
Date: Tue,  2 Oct 2018 20:35:24 +0800
Message-Id: <1538483726-96411-6-git-send-email-jia.guo@intel.com>
X-Mailer: git-send-email 2.7.4
In-Reply-To: <1538483726-96411-1-git-send-email-jia.guo@intel.com>
References: <1498711073-42917-1-git-send-email-jia.guo@intel.com>
 <1538483726-96411-1-git-send-email-jia.guo@intel.com>
Subject: [dpdk-dev] [PATCH v12 5/7] bus: add helper to handle sigbus
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Tue, 02 Oct 2018 12:32:02 -0000

This patch aims to add a helper to iterate over all buses to find the
relevant bus to handle the sigbus error.

Signed-off-by: Jeff Guo <jia.guo@intel.com>
Acked-by: Shaopeng He <shaopeng.he@intel.com>
---
v12->v11:
no change.
---
 lib/librte_eal/common/eal_common_bus.c | 43 ++++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_private.h    | 13 ++++++++++
 2 files changed, 56 insertions(+)

diff --git a/lib/librte_eal/common/eal_common_bus.c b/lib/librte_eal/common/eal_common_bus.c
index 0943851..62b7318 100644
--- a/lib/librte_eal/common/eal_common_bus.c
+++ b/lib/librte_eal/common/eal_common_bus.c
@@ -37,6 +37,7 @@
 #include <rte_bus.h>
 #include <rte_debug.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "eal_private.h"
 
@@ -242,3 +243,45 @@ rte_bus_get_iommu_class(void)
 	}
 	return mode;
 }
+
+static int
+bus_handle_sigbus(const struct rte_bus *bus,
+			const void *failure_addr)
+{
+	int ret;
+
+	if (!bus->sigbus_handler)
+		return -1;
+
+	ret = bus->sigbus_handler(failure_addr);
+
+	/* find bus but handle failed, keep the errno be set. */
+	if (ret < 0 && rte_errno == 0)
+		rte_errno = ENOTSUP;
+
+	return ret > 0;
+}
+
+int
+rte_bus_sigbus_handler(const void *failure_addr)
+{
+	struct rte_bus *bus;
+
+	int ret = 0;
+	int old_errno = rte_errno;
+
+	rte_errno = 0;
+
+	bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
+	/* can not find bus. */
+	if (!bus)
+		return 1;
+	/* find bus but handle failed, pass on the new errno. */
+	else if (rte_errno != 0)
+		return -1;
+
+	/* restore the old errno. */
+	rte_errno = old_errno;
+
+	return ret;
+}
diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
index 4f809a8..a2d1528 100644
--- a/lib/librte_eal/common/eal_private.h
+++ b/lib/librte_eal/common/eal_private.h
@@ -304,4 +304,17 @@ int
 rte_devargs_layers_parse(struct rte_devargs *devargs,
 			 const char *devstr);
 
+/**
+ * Iterate over all buses to find the corresponding bus to handle the sigbus
+ * error.
+ * @param failure_addr
+ *	Pointer of the fault address of the sigbus error.
+ *
+ * @return
+ *	 0 success to handle the sigbus.
+ *	-1 failed to handle the sigbus
+ *	 1 no bus can handler the sigbus
+ */
+int rte_bus_sigbus_handler(const void *failure_addr);
+
 #endif /* _EAL_PRIVATE_H_ */
-- 
2.7.4