Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1152082pxk; Fri, 4 Sep 2020 01:50:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqOxNpRUHxF5mcDVQ0LmTWC0wUpdSstiBbsR/Dioiq9Jn3D4MPU76yeBSNIOcypMRPBUQU X-Received: by 2002:a17:906:5490:: with SMTP id r16mr5985723ejo.222.1599209427316; Fri, 04 Sep 2020 01:50:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599209427; cv=none; d=google.com; s=arc-20160816; b=RTrexr17nanot3x1N57UocQHEPh9xmVoZKSGz5/LQeIavY9hIwxz3qTkrXD+up5l6L PCm/iclbsQNH6Gm2CogCxpzREThZpekZvwH71LZFLuTqke8TKGglvsxo5wHQVTVsAtWF C8BPvUd8pnOOwiaHQmV4kPHnE2gfhDfeM9Gygaattzdjj0Vg1vngMRTA9ppyLpQPNWkG V+K/qR4qre5yKOBrbFFdzNOIqGv8QlaEtHKAcmSQq16WH/X0ygB5fOW5aBH8Mu0RwL41 TB/BO0Vb+8Qt4OqH2ANJ6QEZztAlck5HMMY36jA4D+kxZKCduZec9kes4MAP/TsOy408 H1+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=sjZQGAZNZNA7LVdwFYrlf0esGeGtJOSgI57WnoxPXW8=; b=iRmTM+KzbN89rzlzk42cvsj6meMJfbCmnK2BVC4EMl+jNVuKSDzUj3cfFvU1VSznKT 9MLYikYmtthgJUMfbJZg6BWU9IM3hqhbsb2f06DURq0L1zJKikBTYEF9skCdS4ZctFDT tpBXOBzRbTPmO2Ei/6kEuPF5jSxJoofU8OuEco2pdDBZ4XKBODgdyy1y13OMLYEUjWc2 X3/Y/huuhU23Pcyknq4wSWY8guCYSo6h/Aak32IXo+e4QXVzS3INLJTMFFlFI0G9IpI0 aWc83+oRS6tgBQrxLCngO2DR/oJ6osyhRqwRxv4889KdzpbvR+IuQgJhz3eY8uiN8MKc lAuw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h2si2848845ejy.315.2020.09.04.01.50.04; Fri, 04 Sep 2020 01:50:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729872AbgIDIsI (ORCPT + 99 others); Fri, 4 Sep 2020 04:48:08 -0400 Received: from mxhk.zte.com.cn ([63.217.80.70]:15300 "EHLO mxhk.zte.com.cn" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726575AbgIDIsH (ORCPT ); Fri, 4 Sep 2020 04:48:07 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.30.14.239]) by Forcepoint Email with ESMTPS id 4E262E4E919A776F1020; Fri, 4 Sep 2020 16:48:04 +0800 (CST) Received: from notes_smtp.zte.com.cn (notes_smtp.zte.com.cn [10.30.1.239]) by mse-fl2.zte.com.cn with ESMTP id 0848m3UC023235; Fri, 4 Sep 2020 16:48:03 +0800 (GMT-8) (envelope-from wang.yi59@zte.com.cn) Received: from fox-host8.localdomain ([10.74.120.8]) by szsmtp06.zte.com.cn (Lotus Domino Release 8.5.3FP6) with ESMTP id 2020090416483927-4846041 ; Fri, 4 Sep 2020 16:48:39 +0800 From: Yi Wang To: minchan@kernel.org Cc: ngupta@vflare.org, sergey.senozhatsky.work@gmail.com, axboe@kernel.dk, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, xue.zhihong@zte.com.cn, wang.yi59@zte.com.cn, jiang.xuexin@zte.com.cn, zhanglin Subject: [PATCH v2] zram: add restriction on dynamic zram device creation Date: Fri, 4 Sep 2020 16:52:10 +0800 Message-Id: <1599209530-41015-1-git-send-email-wang.yi59@zte.com.cn> X-Mailer: git-send-email 1.8.3.1 X-MIMETrack: Itemize by SMTP Server on SZSMTP06/server/zte_ltd(Release 8.5.3FP6|November 21, 2013) at 2020-09-04 16:48:39, Serialize by Router on notes_smtp/zte_ltd(Release 9.0.1FP7|August 17, 2016) at 2020-09-04 16:48:12, Serialize complete at 2020-09-04 16:48:12 X-MAIL: mse-fl2.zte.com.cn 0848m3UC023235 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: zhanglin Add max_num_devices to limit dynamic zram device creation to prevent potential OOM Signed-off-by: zhanglin Signed-off-by: Yi Wang --- v1->v2: change hard-coded initial max_num_devices into configurable way. drivers/block/zram/Kconfig | 7 +++++++ drivers/block/zram/zram_drv.c | 28 +++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index fe7a4b7d30cf..54a369932417 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -37,3 +37,10 @@ config ZRAM_MEMORY_TRACKING /sys/kernel/debug/zram/zramX/block_state. See Documentation/admin-guide/blockdev/zram.rst for more information. + +config ZRAM_DEV_MAX_COUNT + int "Number of zram devices to be created" + depends on ZRAM + default 256 + help + This option specifies the maximum number of zram devices. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 36d49159140f..d1022f3c04c4 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -43,8 +43,9 @@ static DEFINE_MUTEX(zram_index_mutex); static int zram_major; static const char *default_compressor = "lzo-rle"; -/* Module params (documentation at end) */ static unsigned int num_devices = 1; +/* Module params (documentation at end) */ +static unsigned int max_num_devices = CONFIG_ZRAM_DEV_MAX_COUNT; /* * Pages that compress to sizes equals or greater than this are stored * uncompressed in memory. @@ -2013,10 +2014,16 @@ static ssize_t hot_add_show(struct class *class, struct class_attribute *attr, char *buf) { - int ret; + int ret = -ENOSPC; mutex_lock(&zram_index_mutex); + if (num_devices >= max_num_devices) { + mutex_unlock(&zram_index_mutex); + return ret; + } ret = zram_add(); + if (ret >= 0) + num_devices += 1; mutex_unlock(&zram_index_mutex); if (ret < 0) @@ -2046,8 +2053,10 @@ static ssize_t hot_remove_store(struct class *class, zram = idr_find(&zram_index_idr, dev_id); if (zram) { ret = zram_remove(zram); - if (!ret) + if (!ret) { idr_remove(&zram_index_idr, dev_id); + num_devices -= 1; + } } else { ret = -ENODEV; } @@ -2089,6 +2098,7 @@ static void destroy_devices(void) static int __init zram_init(void) { int ret; + unsigned int i; ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare", zcomp_cpu_up_prepare, zcomp_cpu_dead); @@ -2111,13 +2121,17 @@ static int __init zram_init(void) return -EBUSY; } - while (num_devices != 0) { + if (num_devices > max_num_devices) { + pr_err("Number of pre-created zram devices over limit\n"); + goto out_error; + } + + for (i = 0; i < num_devices; i++) { mutex_lock(&zram_index_mutex); ret = zram_add(); mutex_unlock(&zram_index_mutex); if (ret < 0) goto out_error; - num_devices--; } return 0; @@ -2135,8 +2149,8 @@ static void __exit zram_exit(void) module_init(zram_init); module_exit(zram_exit); -module_param(num_devices, uint, 0); -MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices"); +module_param(max_num_devices, uint, 0); +MODULE_PARM_DESC(max_num_devices, "Max number of created zram devices"); MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Nitin Gupta "); -- 2.17.1