From 77771ff24c03a59fc96a7f41199a6b73530a418a Mon Sep 17 00:00:00 2001 From: Wang Shilong Date: Wed, 15 May 2019 09:52:37 +0800 Subject: [PATCH 1/1] LU-12299 libcfs: fix panic for too large cpu partions If cpu partions larger than online cpus, following calcuation will be 0: num = num_online_cpus() / ncpt; And it will trigger following panic in cfs_cpt_choose_ncpus() LASSERT(number > 0); We actually did not support this, instead of panic it, return failure is better. Also fix a invalid pointer access if we failed to init @cfs_cpt_table, as it will be converted to ERR_PTR() if error happen. Change-Id: I49daadd8f0c7d22aa78d08248d8c085781740768 Signed-off-by: Wang Shilong Reviewed-on: https://review.whamcloud.com/34864 Tested-by: Jenkins Reviewed-by: Andreas Dilger Tested-by: Maloo Reviewed-by: Gu Zheng Reviewed-by: Yang Sheng Reviewed-by: Oleg Drokin --- libcfs/libcfs/libcfs_cpu.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libcfs/libcfs/libcfs_cpu.c b/libcfs/libcfs/libcfs_cpu.c index 6eb9591..e183986 100644 --- a/libcfs/libcfs/libcfs_cpu.c +++ b/libcfs/libcfs/libcfs_cpu.c @@ -868,7 +868,14 @@ static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt) if (ncpt <= 0) ncpt = num; - if (ncpt > num_online_cpus() || ncpt > 4 * num) { + if (ncpt > num_online_cpus()) { + rc = -EINVAL; + CERROR("libcfs: CPU partition count %d > cores %d: rc = %d\n", + ncpt, num_online_cpus(), rc); + goto failed; + } + + if (ncpt > 4 * num) { CWARN("CPU partition number %d is larger than suggested value (%d), your system may have performance issue or run out of memory while under pressure\n", ncpt, num); } @@ -1230,7 +1237,7 @@ int cfs_cpu_init(void) failed_alloc_table: put_online_cpus(); - if (cfs_cpt_table) + if (!IS_ERR_OR_NULL(cfs_cpt_table)) cfs_cpt_table_free(cfs_cpt_table); #ifdef CONFIG_HOTPLUG_CPU -- 1.8.3.1