From patchwork Wed Oct 16 16:33:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193779 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5BB0176 for ; Wed, 16 Oct 2019 16:34:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 39A3421928 for ; Wed, 16 Oct 2019 16:34:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405888AbfJPQe3 (ORCPT ); Wed, 16 Oct 2019 12:34:29 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:4190 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727054AbfJPQe3 (ORCPT ); Wed, 16 Oct 2019 12:34:29 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id B4C50A5BB5C39C4F8DC2; Thu, 17 Oct 2019 00:34:26 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:17 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 1/7] rasdaemon: fix cleanup issues in ras-events.c:read_ras_event_all_cpus() Date: Wed, 16 Oct 2019 17:33:55 +0100 Message-ID: <20191016163401.16980-2-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch fix memory leaks and close the open files if the open_trace() or read(fds[i].fd, page, pdata[i].ras->page_size) function calls fail. Signed-off-by: Shiju Jose --- ras-events.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/ras-events.c b/ras-events.c index 3cdac19..d1773b1 100644 --- a/ras-events.c +++ b/ras-events.c @@ -353,6 +353,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, struct pollfd fds[n_cpus]; int warnonce[n_cpus]; char pipe_raw[PATH_MAX]; + int legacy_kernel = 0; #if 0 int need_sleep = 0; #endif @@ -372,6 +373,9 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, return -ENOMEM; } + for (i = 0; i < n_cpus; i++) + fds[i].fd = -1; + for (i = 0; i < n_cpus; i++) { fds[i].events = POLLIN; @@ -382,9 +386,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, fds[i].fd = open_trace(pdata[0].ras, pipe_raw, O_RDONLY); if (fds[i].fd < 0) { log(TERM, LOG_ERR, "Can't open trace_pipe_raw\n"); - kbuffer_free(kbuf); - free(page); - return -EINVAL; + goto error; } } @@ -416,7 +418,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, size = read(fds[i].fd, page, pdata[i].ras->page_size); if (size < 0) { log(TERM, LOG_WARNING, "read\n"); - return -1; + goto error; } else if (size > 0) { kbuffer_load_subbuffer(kbuf, page); @@ -441,6 +443,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, */ if (count_nready == n_cpus) { /* Should only happen with legacy kernels */ + legacy_kernel = 1; break; } #endif @@ -449,12 +452,18 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, /* poll() is not supported. We need to fallback to the old way */ log(TERM, LOG_INFO, "Old kernel detected. Stop listening and fall back to pthread way.\n"); +error: kbuffer_free(kbuf); free(page); - for (i = 0; i < n_cpus; i++) - close(fds[i].fd); + for (i = 0; i < n_cpus; i++) { + if (fds[i].fd > 0) + close(fds[i].fd); + } - return -255; + if (legacy_kernel) + return -255; + else + return -1; } static int read_ras_event(int fd, From patchwork Wed Oct 16 16:33:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193781 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6C60D14DB for ; Wed, 16 Oct 2019 16:34:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5737C21D7A for ; Wed, 16 Oct 2019 16:34:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727054AbfJPQe3 (ORCPT ); Wed, 16 Oct 2019 12:34:29 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:4191 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730251AbfJPQe3 (ORCPT ); Wed, 16 Oct 2019 12:34:29 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id B11D79A0D81127DA638A; Thu, 17 Oct 2019 00:34:26 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:19 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 2/7] rasdaemon: fix memory leak in ras-events.c:handle_ras_events() Date: Wed, 16 Oct 2019 17:33:56 +0100 Message-ID: <20191016163401.16980-3-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch fix memory leak in handle_ras_events() when failed to trace all supported RAS events. Signed-off-by: Shiju Jose --- ras-events.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ras-events.c b/ras-events.c index d1773b1..d543251 100644 --- a/ras-events.c +++ b/ras-events.c @@ -846,7 +846,8 @@ int handle_ras_events(int record_events) if (!num_events) { log(ALL, LOG_INFO, "Failed to trace all supported RAS events. Aborting.\n"); - return EINVAL; + rc = -EINVAL; + goto err; } data = calloc(sizeof(*data), cpus); From patchwork Wed Oct 16 16:33:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193783 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4245876 for ; Wed, 16 Oct 2019 16:34:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2C7DA21D7C for ; Wed, 16 Oct 2019 16:34:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405951AbfJPQed (ORCPT ); Wed, 16 Oct 2019 12:34:33 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4226 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2405459AbfJPQed (ORCPT ); Wed, 16 Oct 2019 12:34:33 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id B80A8A606563A56AFC52; Thu, 17 Oct 2019 00:34:31 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:21 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 3/7] rasdaemon: fix missing fclose in ras-events.c:select_tracing_timestamp() Date: Wed, 16 Oct 2019 17:33:57 +0100 Message-ID: <20191016163401.16980-4-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch adds fix for missing fclose() in select_tracing_timestamp() when return fail if can't parse /proc/uptime. Signed-off-by: Shiju Jose --- ras-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ras-events.c b/ras-events.c index d543251..fc6b288 100644 --- a/ras-events.c +++ b/ras-events.c @@ -600,12 +600,12 @@ static int select_tracing_timestamp(struct ras_events *ras) return 0; } rc = fscanf(fp, "%zu.%u ", &uptime, &j1); + fclose(fp); if (rc <= 0) { log(TERM, LOG_ERR, "Can't parse /proc/uptime!\n"); return -1; } now = time(NULL); - fclose(fp); ras->use_uptime = 1; ras->uptime_diff = now - uptime; From patchwork Wed Oct 16 16:33:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193785 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5E9C41668 for ; Wed, 16 Oct 2019 16:34:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 49C3A21D7C for ; Wed, 16 Oct 2019 16:34:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405459AbfJPQee (ORCPT ); Wed, 16 Oct 2019 12:34:34 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4227 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730251AbfJPQed (ORCPT ); Wed, 16 Oct 2019 12:34:33 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id B4790F4FF67101A94860; Thu, 17 Oct 2019 00:34:31 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:22 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 4/7] rasdaemon: fix memory leak in ras-events.c:add_event_handler() Date: Wed, 16 Oct 2019 17:33:58 +0100 Message-ID: <20191016163401.16980-5-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch rearranges the free(page) call to prevent the memory leak when __toggle_ras_mc_event() fail. Signed-off-by: Shiju Jose --- ras-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ras-events.c b/ras-events.c index fc6b288..f912dae 100644 --- a/ras-events.c +++ b/ras-events.c @@ -688,6 +688,7 @@ static int add_event_handler(struct ras_events *ras, struct pevent *pevent, /* Enable RAS events */ rc = __toggle_ras_mc_event(ras, group, event, 1); + free(page); if (rc < 0) { log(TERM, LOG_ERR, "Can't enable %s:%s tracing\n", group, event); @@ -697,7 +698,6 @@ static int add_event_handler(struct ras_events *ras, struct pevent *pevent, log(ALL, LOG_INFO, "Enabled event %s:%s\n", group, event); - free(page); return 0; } From patchwork Wed Oct 16 16:33:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193787 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0CBF676 for ; Wed, 16 Oct 2019 16:34:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E158D21D80 for ; Wed, 16 Oct 2019 16:34:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404724AbfJPQee (ORCPT ); Wed, 16 Oct 2019 12:34:34 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4228 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2404676AbfJPQee (ORCPT ); Wed, 16 Oct 2019 12:34:34 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id BB5E56CA3D3A6D709533; Thu, 17 Oct 2019 00:34:31 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:24 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 5/7] rasdaemon: delete multiple definitions of ARRAY_SIZE Date: Wed, 16 Oct 2019 17:33:59 +0100 Message-ID: <20191016163401.16980-6-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch deletes multiple definitions of ARRAY_SIZE and move the definition to a common file. Signed-off-by: Shiju Jose --- ras-diskerror-handler.c | 2 -- ras-mce-handler.h | 3 --- ras-record.c | 3 --- ras-record.h | 2 ++ 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/ras-diskerror-handler.c b/ras-diskerror-handler.c index 271dfac..68c0c77 100644 --- a/ras-diskerror-handler.c +++ b/ras-diskerror-handler.c @@ -50,8 +50,6 @@ static const struct { { -EIO, "I/O error" }, }; -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x))) - static const char *get_blk_error(int err) { int i; diff --git a/ras-mce-handler.h b/ras-mce-handler.h index 94395eb..4d615b4 100644 --- a/ras-mce-handler.h +++ b/ras-mce-handler.h @@ -24,9 +24,6 @@ #include "ras-events.h" #include "libtrace/event-parse.h" - -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x))) - enum cputype { CPU_GENERIC, CPU_P6OLD, diff --git a/ras-record.c b/ras-record.c index ae5d359..8f1c550 100644 --- a/ras-record.c +++ b/ras-record.c @@ -35,9 +35,6 @@ #define SQLITE_RAS_DB RASSTATEDIR "/" RAS_DB_FNAME - -#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x))) - /* * Table and functions to handle ras:mc_event */ diff --git a/ras-record.h b/ras-record.h index 5311c67..c9af5ae 100644 --- a/ras-record.h +++ b/ras-record.h @@ -23,6 +23,8 @@ #include #include "config.h" +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x))) + extern long user_hz; struct ras_events *ras; From patchwork Wed Oct 16 16:34:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193791 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ADF9576 for ; Wed, 16 Oct 2019 16:34:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 910EB21A4C for ; Wed, 16 Oct 2019 16:34:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406043AbfJPQep (ORCPT ); Wed, 16 Oct 2019 12:34:45 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4230 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2404676AbfJPQeo (ORCPT ); Wed, 16 Oct 2019 12:34:44 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id D210E2626E956920997E; Thu, 17 Oct 2019 00:34:36 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:26 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 6/7] rasdaemon: add closure and cleanups for the database Date: Wed, 16 Oct 2019 17:34:00 +0100 Message-ID: <20191016163401.16980-7-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org This patch adds closure and cleanups for the sqlite3 database. Signed-off-by: Shiju Jose --- ras-events.c | 14 +++++- ras-non-standard-handler.c | 16 ++++++ ras-non-standard-handler.h | 6 ++- ras-record.c | 120 +++++++++++++++++++++++++++++++++++++++++++++ ras-record.h | 3 ++ 5 files changed, 157 insertions(+), 2 deletions(-) diff --git a/ras-events.c b/ras-events.c index f912dae..d155caa 100644 --- a/ras-events.c +++ b/ras-events.c @@ -418,7 +418,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, size = read(fds[i].fd, page, pdata[i].ras->page_size); if (size < 0) { log(TERM, LOG_WARNING, "read\n"); - goto error; + goto cleanup; } else if (size > 0) { kbuffer_load_subbuffer(kbuf, page); @@ -452,6 +452,13 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, /* poll() is not supported. We need to fallback to the old way */ log(TERM, LOG_INFO, "Old kernel detected. Stop listening and fall back to pthread way.\n"); + +cleanup: + if (pdata[0].ras->record_events) { + unregister_ns_dec_tab(); + ras_mc_event_closedb(pdata[0].cpu, pdata[0].ras); + } + error: kbuffer_free(kbuf); free(page); @@ -540,6 +547,11 @@ static void *handle_ras_events_cpu(void *priv) read_ras_event(fd, pdata, kbuf, page); + if (pdata->ras->record_events) { + unregister_ns_dec_tab(); + ras_mc_event_closedb(pdata->cpu, pdata->ras); + } + close(fd); kbuffer_free(kbuf); free(page); diff --git a/ras-non-standard-handler.c b/ras-non-standard-handler.c index 1b5d67a..d92fd42 100644 --- a/ras-non-standard-handler.c +++ b/ras-non-standard-handler.c @@ -41,6 +41,22 @@ int register_ns_dec_tab(const p_ns_dec_tab tab) void unregister_ns_dec_tab(void) { if (ns_dec_tab) { +#ifdef HAVE_SQLITE3 + p_ns_dec_tab dec_tab; + int i, count; + + for (count = 0; count < dec_tab_count; count++) { + dec_tab = ns_dec_tab[count]; + for (i = 0; dec_tab[i].decode; i++) { + if (dec_tab[i].stmt_dec_record) { + ras_mc_finalize_vendor_table( + dec_tab[i].stmt_dec_record); + dec_tab[i].stmt_dec_record = NULL; + } + } + } +#endif + free(ns_dec_tab); ns_dec_tab = NULL; dec_tab_count = 0; diff --git a/ras-non-standard-handler.h b/ras-non-standard-handler.h index fd9dd92..2b9bf40 100644 --- a/ras-non-standard-handler.h +++ b/ras-non-standard-handler.h @@ -36,8 +36,12 @@ int ras_non_standard_event_handler(struct trace_seq *s, void print_le_hex(struct trace_seq *s, const uint8_t *buf, int index); +#ifdef HAVE_NON_STANDARD int register_ns_dec_tab(const p_ns_dec_tab tab); - void unregister_ns_dec_tab(void); +#else +static inline int register_ns_dec_tab(const p_ns_dec_tab tab) { return 0; }; +static inline void unregister_ns_dec_tab(void) { return; }; +#endif #endif diff --git a/ras-record.c b/ras-record.c index 8f1c550..c6cf61a 100644 --- a/ras-record.c +++ b/ras-record.c @@ -595,6 +595,18 @@ int ras_mc_add_vendor_table(struct ras_events *ras, return rc; } +int ras_mc_finalize_vendor_table(sqlite3_stmt *stmt) +{ + int rc; + + rc = sqlite3_finalize(stmt); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "Failed to finalize sqlite: error = %d\n", rc); + + return rc; +} + int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) { int rc; @@ -692,3 +704,111 @@ int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) ras->db_priv = priv; return 0; } + +int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras) +{ + int rc; + sqlite3 *db; + struct sqlite3_priv *priv = ras->db_priv; + + printf("Calling %s()\n", __func__); + + if (!priv) + return -1; + + db = priv->db; + if (!db) + return -1; + + if (priv->stmt_mc_event) { + rc = sqlite3_finalize(priv->stmt_mc_event); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize mc_event sqlite: error = %d\n", + cpu, rc); + } + +#ifdef HAVE_AER + if (priv->stmt_aer_event) { + rc = sqlite3_finalize(priv->stmt_aer_event); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize aer_event sqlite: error = %d\n", + cpu, rc); + } +#endif + +#ifdef HAVE_EXTLOG + if (priv->stmt_extlog_record) { + rc = sqlite3_finalize(priv->stmt_extlog_record); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize extlog_record sqlite: error = %d\n", + cpu, rc); + } +#endif + + +#ifdef HAVE_MCE + if (priv->stmt_mce_record) { + rc = sqlite3_finalize(priv->stmt_mce_record); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize mce_record sqlite: error = %d\n", + cpu, rc); + } +#endif + +#ifdef HAVE_NON_STANDARD + if (priv->stmt_non_standard_record) { + rc = sqlite3_finalize(priv->stmt_non_standard_record); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize non_standard_record sqlite: error = %d\n", + cpu, rc); + } +#endif + +#ifdef HAVE_ARM + if (priv->stmt_arm_record) { + rc = sqlite3_finalize(priv->stmt_arm_record); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize arm_record sqlite: error = %d\n", + cpu, rc); + } +#endif + +#ifdef HAVE_DEVLINK + if (priv->stmt_devlink_event) { + rc = sqlite3_finalize(priv->stmt_devlink_event); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize devlink_event sqlite: error = %d\n", + cpu, rc); + } +#endif + +#ifdef HAVE_DISKERROR + if (priv->stmt_diskerror_event) { + rc = sqlite3_finalize(priv->stmt_diskerror_event); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to finalize diskerror_event sqlite: error = %d\n", + cpu, rc); + } +#endif + + rc = sqlite3_close_v2(db); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to close sqlite: error = %d\n", cpu, rc); + + rc = sqlite3_shutdown(); + if (rc != SQLITE_OK) + log(TERM, LOG_ERR, + "cpu %u: Failed to shutdown sqlite: error = %d\n", cpu, rc); + free(priv); + + return 0; +} diff --git a/ras-record.h b/ras-record.h index c9af5ae..a67b193 100644 --- a/ras-record.h +++ b/ras-record.h @@ -147,8 +147,10 @@ struct db_table_descriptor { }; int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras); +int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras); int ras_mc_add_vendor_table(struct ras_events *ras, sqlite3_stmt **stmt, const struct db_table_descriptor *db_tab); +int ras_mc_finalize_vendor_table(sqlite3_stmt *stmt); int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev); int ras_store_aer_event(struct ras_events *ras, struct ras_aer_event *ev); int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev); @@ -160,6 +162,7 @@ int ras_store_diskerror_event(struct ras_events *ras, struct diskerror_event *ev #else static inline int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) { return 0; }; +static inline int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras) { return 0; }; static inline int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev) { return 0; }; static inline int ras_store_aer_event(struct ras_events *ras, struct ras_aer_event *ev) { return 0; }; static inline int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev) { return 0; }; From patchwork Wed Oct 16 16:34:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiju Jose X-Patchwork-Id: 11193793 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E6C5914DB for ; Wed, 16 Oct 2019 16:34:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D10EF21928 for ; Wed, 16 Oct 2019 16:34:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404676AbfJPQep (ORCPT ); Wed, 16 Oct 2019 12:34:45 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4229 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2405428AbfJPQep (ORCPT ); Wed, 16 Oct 2019 12:34:45 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id CE2DE6407188DB20BBCB; Thu, 17 Oct 2019 00:34:36 +0800 (CST) Received: from DESKTOP-6T4S3DQ.china.huawei.com (10.202.226.55) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.439.0; Thu, 17 Oct 2019 00:34:28 +0800 From: Shiju Jose To: , CC: , Shiju Jose Subject: [PATCH 7/7] rasdaemon: add signal handling for the cleanup Date: Wed, 16 Oct 2019 17:34:01 +0100 Message-ID: <20191016163401.16980-8-shiju.jose@huawei.com> X-Mailer: git-send-email 2.19.2.windows.1 In-Reply-To: <20191016163401.16980-1-shiju.jose@huawei.com> References: <20191016163401.16980-1-shiju.jose@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.55] X-CFilter-Loop: Reflected Sender: linux-edac-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org Presently rasdaemon would not free allocated memory and would not do other cleanup when the rasdaemon closed with ctrl+c or kill etc. This patch adds handling of the signals SIGINT, SIGTERM, SIGHUP and SIGQUIT and do necessary clean ups when receive the specified signals. Signed-off-by: Shiju Jose --- ras-events.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/ras-events.c b/ras-events.c index d155caa..511c93d 100644 --- a/ras-events.c +++ b/ras-events.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include "libtrace/kbuffer.h" #include "libtrace/event-parse.h" #include "ras-mc-handler.h" @@ -350,7 +352,9 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, int ready, i, count_nready; struct kbuffer *kbuf; void *page; - struct pollfd fds[n_cpus]; + struct pollfd fds[n_cpus + 1]; + struct signalfd_siginfo fdsiginfo; + sigset_t mask; int warnonce[n_cpus]; char pipe_raw[PATH_MAX]; int legacy_kernel = 0; @@ -373,7 +377,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, return -ENOMEM; } - for (i = 0; i < n_cpus; i++) + for (i = 0; i < (n_cpus + 1); i++) fds[i].fd = -1; for (i = 0; i < n_cpus; i++) { @@ -390,15 +394,51 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata, } } + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); + sigaddset(&mask, SIGHUP); + sigaddset(&mask, SIGQUIT); + if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) + log(TERM, LOG_WARNING, "sigprocmask\n"); + fds[n_cpus].events = POLLIN; + fds[n_cpus].fd = signalfd(-1, &mask, 0); + if (fds[n_cpus].fd < 0) { + log(TERM, LOG_WARNING, "signalfd\n"); + goto error; + } + log(TERM, LOG_INFO, "Listening to events for cpus 0 to %d\n", n_cpus - 1); if (pdata[0].ras->record_events) ras_mc_event_opendb(pdata[0].cpu, pdata[0].ras); do { - ready = poll(fds, n_cpus, -1); + ready = poll(fds, (n_cpus + 1), -1); if (ready < 0) { log(TERM, LOG_WARNING, "poll\n"); } + + /* check for the signal */ + if (fds[n_cpus].revents & POLLIN) { + size = read(fds[n_cpus].fd, &fdsiginfo, + sizeof(struct signalfd_siginfo)); + if (size != sizeof(struct signalfd_siginfo)) + log(TERM, LOG_WARNING, "signalfd read\n"); + + if (fdsiginfo.ssi_signo == SIGINT || + fdsiginfo.ssi_signo == SIGTERM || + fdsiginfo.ssi_signo == SIGHUP || + fdsiginfo.ssi_signo == SIGQUIT) { + log(TERM, LOG_INFO, "Recevied signal=%d\n", + fdsiginfo.ssi_signo); + goto cleanup; + } else { + log(TERM, LOG_INFO, + "Received unexpected signal=%d\n", + fdsiginfo.ssi_signo); + } + } + count_nready = 0; for (i = 0; i < n_cpus; i++) { if (fds[i].revents & POLLERR) { @@ -462,7 +502,9 @@ cleanup: error: kbuffer_free(kbuf); free(page); - for (i = 0; i < n_cpus; i++) { + sigprocmask(SIG_UNBLOCK, &mask, NULL); + + for (i = 0; i < (n_cpus + 1); i++) { if (fds[i].fd > 0) close(fds[i].fd); }