From c938caa73fa348dd9f14fff858f8335532b94a20 Mon Sep 17 00:00:00 2001 From: znzjugod Date: Mon, 30 Oct 2023 19:37:58 +0800 Subject: [PATCH] backport upstream patches --- ...able-create-if-some-cpus-are-offline.patch | 178 ++++++++++++++++++ ...service_in-comment-out-syslog_target.patch | 30 +++ rasdaemon.spec | 10 +- 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 backport-rasdaemon-fix-table-create-if-some-cpus-are-offline.patch create mode 100644 backport-rasdaemon-service_in-comment-out-syslog_target.patch diff --git a/backport-rasdaemon-fix-table-create-if-some-cpus-are-offline.patch b/backport-rasdaemon-fix-table-create-if-some-cpus-are-offline.patch new file mode 100644 index 0000000..18aa4bf --- /dev/null +++ b/backport-rasdaemon-fix-table-create-if-some-cpus-are-offline.patch @@ -0,0 +1,178 @@ +From 6f7851f72d8464c7a20a248d4abf4362de8f0ba9 Mon Sep 17 00:00:00 2001 +From: Shiju Jose +Date: Sun, 5 Mar 2023 23:14:42 +0000 +Subject: [PATCH] rasdaemon: fix table create if some cpus are offline + +Reference:https://github.com/mchehab/rasdaemon/commit/6f7851f72d8464c7a20a248d4abf4362de8f0ba9 +Conflict:NA + +Fix for regression in ras_mc_create_table() if some cpus are offline +at the system start + +Issue: + +Regression in the ras_mc_create_table() if some of the cpus are offline +at the system start when run the rasdaemon. + +This issue is reproducible in ras_mc_create_table() with decode and +record non-standard events and reproducible sometimes with +ras_mc_create_table() for the standard events. + +Also in the multi thread way, there is memory leak in ras_mc_event_opendb() +as struct sqlite3_priv *priv and sqlite3 *db allocated/initialized per +thread, but stored in the common struct ras_events ras in pthread data, +which is shared across the threads. + +Reason: + +when the system starts with some of the cpus offline and then run +the rasdaemon, read_ras_event_all_cpus() exit with error and switch to +the multi thread way. However read() in read_ras_event() return error in +threads for each of the offline CPUs and does clean up including calling +ras_mc_event_closedb(). + +Since the 'struct ras_events ras' passed in the pthread_data to each of the +threads is common, struct sqlite3_priv *priv and sqlite3 *db allocated/ +initialized per thread and stored in the common 'struct ras_events ras', +are getting overwritten in each ras_mc_event_opendb()(which called from +pthread per cpu), result memory leak. + +Also when ras_mc_event_closedb() is called in the above error case from +the threads corresponding to the offline cpus, close the sqlite3 *db and +free sqlite3_priv *priv stored in the common 'struct ras_events ras', +result regression when accessing priv->db in the ras_mc_create_table() +from another context later. + +Solution: + +In ras_mc_event_opendb(), allocate struct sqlite3_priv *priv, +init sqlite3 *db and create tables common for the threads with shared +'struct ras_events ras' based on a reference count and free them in the +same way. + +Also protect critical code ras_mc_event_opendb() and ras_mc_event_closedb() +using mutex in the multi thread case from any regression caused by the +thread pre-emption. + +Reported-by: Lei Feng +Signed-off-by: Shiju Jose +Signed-off-by: Mauro Carvalho Chehab +--- + ras-events.c | 16 +++++++++++++++- + ras-events.h | 4 +++- + ras-record.c | 12 ++++++++++++ + 3 files changed, 30 insertions(+), 2 deletions(-) + +diff --git a/ras-events.c b/ras-events.c +index 49e4f9a..5fe8e19 100644 +--- a/ras-events.c ++++ b/ras-events.c +@@ -625,19 +625,25 @@ static void *handle_ras_events_cpu(void *priv) + + log(TERM, LOG_INFO, "Listening to events on cpu %d\n", pdata->cpu); + if (pdata->ras->record_events) { ++ pthread_mutex_lock(&pdata->ras->db_lock); + if (ras_mc_event_opendb(pdata->cpu, pdata->ras)) { ++ pthread_mutex_unlock(&pdata->ras->db_lock); + log(TERM, LOG_ERR, "Can't open database\n"); + close(fd); + kbuffer_free(kbuf); + free(page); + return 0; + } ++ pthread_mutex_unlock(&pdata->ras->db_lock); + } + + read_ras_event(fd, pdata, kbuf, page); + +- if (pdata->ras->record_events) ++ if (pdata->ras->record_events) { ++ pthread_mutex_lock(&pdata->ras->db_lock); + ras_mc_event_closedb(pdata->cpu, pdata->ras); ++ pthread_mutex_unlock(&pdata->ras->db_lock); ++ } + + close(fd); + kbuffer_free(kbuf); +@@ -993,6 +999,11 @@ int handle_ras_events(int record_events) + + /* Poll doesn't work on this kernel. Fallback to pthread way */ + if (rc == -255) { ++ if (pthread_mutex_init(&ras->db_lock, NULL) != 0) { ++ log(SYSLOG, LOG_INFO, "sqlite db lock init has failed\n"); ++ goto err; ++ } ++ + log(SYSLOG, LOG_INFO, + "Opening one thread per cpu (%d threads)\n", cpus); + for (i = 0; i < cpus; i++) { +@@ -1005,6 +1016,8 @@ int handle_ras_events(int record_events) + i); + while (--i) + pthread_cancel(data[i].thread); ++ ++ pthread_mutex_destroy(&ras->db_lock); + goto err; + } + } +@@ -1012,6 +1025,7 @@ int handle_ras_events(int record_events) + /* Wait for all threads to complete */ + for (i = 0; i < cpus; i++) + pthread_join(data[i].thread, NULL); ++ pthread_mutex_destroy(&ras->db_lock); + } + + log(SYSLOG, LOG_INFO, "Huh! something got wrong. Aborting.\n"); +diff --git a/ras-events.h b/ras-events.h +index 6c9f507..649b0c0 100644 +--- a/ras-events.h ++++ b/ras-events.h +@@ -56,7 +56,9 @@ struct ras_events { + time_t uptime_diff; + + /* For ras-record */ +- void *db_priv; ++ void *db_priv; ++ int db_ref_count; ++ pthread_mutex_t db_lock; + + /* For the mce handler */ + struct mce_priv *mce_priv; +diff --git a/ras-record.c b/ras-record.c +index a367939..adc97a4 100644 +--- a/ras-record.c ++++ b/ras-record.c +@@ -763,6 +763,10 @@ int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) + + printf("Calling %s()\n", __FUNCTION__); + ++ ras->db_ref_count++; ++ if (ras->db_ref_count > 1) ++ return 0; ++ + ras->db_priv = NULL; + + priv = calloc(1, sizeof(*priv)); +@@ -912,6 +916,13 @@ int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras) + + printf("Calling %s()\n", __func__); + ++ if (ras->db_ref_count > 0) ++ ras->db_ref_count--; ++ else ++ return -1; ++ if (ras->db_ref_count > 0) ++ return 0; ++ + if (!priv) + return -1; + +@@ -1018,6 +1029,7 @@ int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras) + log(TERM, LOG_ERR, + "cpu %u: Failed to shutdown sqlite: error = %d\n", cpu, rc); + free(priv); ++ ras->db_priv = NULL; + + return 0; + } + diff --git a/backport-rasdaemon-service_in-comment-out-syslog_target.patch b/backport-rasdaemon-service_in-comment-out-syslog_target.patch new file mode 100644 index 0000000..5be1b8c --- /dev/null +++ b/backport-rasdaemon-service_in-comment-out-syslog_target.patch @@ -0,0 +1,30 @@ +From 5ae17fe14ae823c87caa2f4e48ea19cb30958e62 Mon Sep 17 00:00:00 2001 +From: Evils +Date: Sat, 11 Dec 2021 03:27:05 +0100 +Subject: [PATCH] rasdaemon.service.in: comment out syslog.target + +Reference:https://github.com/mchehab/rasdaemon/commit/5ae17fe14ae823c87caa2f4e48ea19cb30958e62 +Conflict:NA + +syslog is only used when the daemon runs in backround mode + this service is configured to run in foreground mode + +Signed-off-by: Mauro Carvalho Chehab +--- + misc/rasdaemon.service.in | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/misc/rasdaemon.service.in b/misc/rasdaemon.service.in +index 4ef3d2c..ff68b53 100644 +--- a/misc/rasdaemon.service.in ++++ b/misc/rasdaemon.service.in +@@ -1,6 +1,7 @@ + [Unit] + Description=RAS daemon to log the RAS events +-After=syslog.target ++# only needed when not running in foreground (--foreground | -f) ++#After=syslog.target + + [Service] + EnvironmentFile=@SYSCONFDEFDIR@/rasdaemon + diff --git a/rasdaemon.spec b/rasdaemon.spec index 12cd3a8..7a707a0 100644 --- a/rasdaemon.spec +++ b/rasdaemon.spec @@ -1,6 +1,6 @@ Name: rasdaemon Version: 0.6.7 -Release: 7 +Release: 8 License: GPLv2 Summary: Utility to get Platform Reliability, Availability and Serviceability (RAS) reports via the Kernel tracing events URL: https://github.com/mchehab/rasdaemon.git @@ -41,6 +41,8 @@ Patch6000: backport-rasdaemon-ras-mc-ctl-Fix-script-to-parse-dimm-sizes.patch Patch6001: backport-rasdaemon-Fix-for-a-memory-out-of-bounds-issue-and-o.patch Patch6002: backport-rasdaemon-ras-memory-failure-handler-handle-localtim.patch Patch6003: backport-rasdaemon-ras-report-fix-possible-but-unlikely-file-.patch +Patch6004: backport-rasdaemon-fix-table-create-if-some-cpus-are-offline.patch +Patch6005: backport-rasdaemon-service_in-comment-out-syslog_target.patch %description The rasdaemon program is a daemon which monitors the platform @@ -86,6 +88,12 @@ rm INSTALL %{buildroot}/usr/include/*.h /usr/bin/systemctl enable rasdaemon.service >/dev/null 2>&1 || : %changelog +* Mon Oct 30 2023 zhangnan - 0.6.7-8 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:backport upstream patches + * Sat Jun 17 2023 yanglongkang - 0.6.7-7 - Type:bugfix - ID:NA -- Gitee