Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp1430441ybj; Fri, 20 Sep 2019 10:18:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqxYDuFa1XAc2w/nugpm4bRkNVnnHiwtLe5kTchCuh2zvjKcr0UIQOJSXYksMbeXqRqsg7sQ X-Received: by 2002:a17:906:eb86:: with SMTP id mh6mr19676845ejb.171.1568999928568; Fri, 20 Sep 2019 10:18:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568999928; cv=none; d=google.com; s=arc-20160816; b=zok+36gOYTsWX367s1vruuL1nqnjk+Ukx8MjOMhwFW1NNJIUACPly+6cD3FIhYiGFg TZVRZzpoRgJ7hDsCT2QxxN7fn8FoHmAAWvIbH+/iDu664+C6/RnG8U+msy20ahLju3Tg qKZLXyPSSdhA1W2Ee/Ej6YO2j+Px+HZMjhBc24FfN4Elg0YjVqlU9vz/mr9pQ36hLN9p +xqbXXphh5QruA2AYoMueYDocxTKrECI0Oa2rzysf6qiI+Ji05zXPDWL6cUlx4oDJX+p b4yqZw+pfAhyGnilRrnwqexpkqAMCPwIFUZz4yZoFpcQdkd7oG8RHHVY54zEqsRSq9AM 5yFA== 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=hKlwomkaadQVE6EvX6p/eCBxwZ0imIEtb9JcaSfKLB4=; b=EhKbSqsOjcZq81ll4OXbrkpOPKuHFYyZsR0vXSE1F0y3+MWcMMvFq52RF5bC0l/zZ/ zHfKJBRQBxR+gJKsVduZGEpjOAU9SH6QfCHYvl8s3m9MXqwbVQ4Xfva4i5N9OIuQD9Ny 1f3htwNrsvw90B9JobAQVXnzNbksHBLmR0p4CoOd+CC/QL3uJpYcw2gCLhhxixySW85e yEl0Vf/+65JbGhcATc5OcRLpEdPDu/A4onxbekJLsnmVwajHXpHb099b+vNbd5xM6m+P REffz9fG5QHczzyZNT2r+BWT2KiiDWdJg7eTN3lmxZtqLQcDJMoHcerDDYDd3RSa+B/V 1mcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r24si1597919edy.417.2019.09.20.10.18.25; Fri, 20 Sep 2019 10:18:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437231AbfITGfa (ORCPT + 99 others); Fri, 20 Sep 2019 02:35:30 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:57736 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389232AbfITGfa (ORCPT ); Fri, 20 Sep 2019 02:35:30 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04446;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=26;SR=0;TI=SMTPD_---0TcrUSCY_1568961310; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0TcrUSCY_1568961310) by smtp.aliyun-inc.com(127.0.0.1); Fri, 20 Sep 2019 14:35:14 +0800 From: Hui Zhu To: sjenning@redhat.com, ddstreet@ieee.org, akpm@linux-foundation.org, mhocko@suse.com, willy@infradead.org, chris@chris-wilson.co.uk, hannes@cmpxchg.org, ziqian.lzq@antfin.com, osandov@fb.com, ying.huang@intel.com, aryabinin@virtuozzo.com, vovoy@chromium.org, richard.weiyang@gmail.com, jgg@ziepe.ca, dan.j.williams@intel.com, rppt@linux.ibm.com, jglisse@redhat.com, b.zolnierkie@samsung.com, axboe@kernel.dk, dennis@kernel.org, josef@toxicpanda.com, tj@kernel.org, oleg@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Hui Zhu Subject: [RFC v2] zswap: Add CONFIG_ZSWAP_IO_SWITCH to handle swap IO issue Date: Fri, 20 Sep 2019 14:35:07 +0800 Message-Id: <1568961307-32419-1-git-send-email-teawaterz@linux.alibaba.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is the second version of this patch. The previous version is in https://lkml.org/lkml/2019/9/11/935 I updated the commit introduction and Kconfig because it is not clear. Currently, I use a VM that has 2 CPUs, 4G memory and 4G swap file. I found that swap will affect the IO performance when it is running. So I open zswap to handle it because it just use CPU cycles but not disk IO. It work OK but I found that zswap is slower than normal swap in this VM. zswap is about 300M/s and normal swap is about 500M/s. (The reason is disk inside VM has fscache in host machine.) So open zswap is make memory shrinker slower but good for IO performance in this VM. So I just want zswap work when the disk of the swap file is under high IO load. This commit is designed for this idea. It add two parameters read_in_flight_limit and write_in_flight_limit to zswap. In zswap_frontswap_store, pages will be stored to zswap only when the IO in flight number of swap device is bigger than zswap_read_in_flight_limit or zswap_write_in_flight_limit when zswap is enabled. Then the zswap just work when the IO in flight number of swap device is low. Signed-off-by: Hui Zhu --- include/linux/swap.h | 3 +++ mm/Kconfig | 18 +++++++++++++++++ mm/page_io.c | 16 +++++++++++++++ mm/zswap.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/include/linux/swap.h b/include/linux/swap.h index de2c67a..82b621f 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -389,6 +389,9 @@ extern void end_swap_bio_write(struct bio *bio); extern int __swap_writepage(struct page *page, struct writeback_control *wbc, bio_end_io_t end_write_func); extern int swap_set_page_dirty(struct page *page); +#ifdef CONFIG_ZSWAP_IO_SWITCH +extern void swap_io_in_flight(struct page *page, unsigned int inflight[2]); +#endif int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, unsigned long nr_pages, sector_t start_block); diff --git a/mm/Kconfig b/mm/Kconfig index 56cec63..5408d65 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -546,6 +546,24 @@ config ZSWAP they have not be fully explored on the large set of potential configurations and workloads that exist. +config ZSWAP_IO_SWITCH + bool "Compressed cache for swap pages according to the IO status" + depends on ZSWAP + def_bool n + help + This function help the system that normal swap speed is higher + than zswap speed to handle the swap IO issue. + For example, a VM that is disk device is not set cache config or + set cache=writeback. + + This function make zswap just work when the disk of the swap file + is under high IO load. + It add two parameters read_in_flight_limit and write_in_flight_limit to + zswap. When zswap is enabled, pages will be stored to zswap only + when the IO in flight number of swap device is bigger than + zswap_read_in_flight_limit or zswap_write_in_flight_limit. + If unsure, say "n". + config ZPOOL tristate "Common API for compressed memory storage" help diff --git a/mm/page_io.c b/mm/page_io.c index 24ee600..e66b050 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -434,3 +434,19 @@ int swap_set_page_dirty(struct page *page) return __set_page_dirty_no_writeback(page); } } + +#ifdef CONFIG_ZSWAP_IO_SWITCH +void swap_io_in_flight(struct page *page, unsigned int inflight[2]) +{ + struct swap_info_struct *sis = page_swap_info(page); + + if (!sis->bdev) { + inflight[0] = 0; + inflight[1] = 0; + return; + } + + part_in_flight_rw(bdev_get_queue(sis->bdev), sis->bdev->bd_part, + inflight); +} +#endif diff --git a/mm/zswap.c b/mm/zswap.c index 0e22744..1255645 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -62,6 +62,13 @@ static u64 zswap_reject_compress_poor; static u64 zswap_reject_alloc_fail; /* Store failed because the entry metadata could not be allocated (rare) */ static u64 zswap_reject_kmemcache_fail; +#ifdef CONFIG_ZSWAP_IO_SWITCH +/* Store failed because zswap_read_in_flight_limit or + * zswap_write_in_flight_limit is bigger than IO in flight number of + * swap device + */ +static u64 zswap_reject_io; +#endif /* Duplicate store was encountered (rare) */ static u64 zswap_duplicate_entry; @@ -114,6 +121,22 @@ static bool zswap_same_filled_pages_enabled = true; module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled, bool, 0644); +#ifdef CONFIG_ZSWAP_IO_SWITCH +/* zswap will not try to store the page if zswap_read_in_flight_limit is + * bigger than IO read in flight number of swap device + */ +static unsigned int zswap_read_in_flight_limit; +module_param_named(read_in_flight_limit, zswap_read_in_flight_limit, + uint, 0644); + +/* zswap will not try to store the page if zswap_write_in_flight_limit is + * bigger than IO write in flight number of swap device + */ +static unsigned int zswap_write_in_flight_limit; +module_param_named(write_in_flight_limit, zswap_write_in_flight_limit, + uint, 0644); +#endif + /********************************* * data structures **********************************/ @@ -1009,6 +1032,34 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, goto reject; } +#ifdef CONFIG_ZSWAP_IO_SWITCH + if (zswap_read_in_flight_limit || zswap_write_in_flight_limit) { + unsigned int inflight[2]; + bool should_swap = false; + + swap_io_in_flight(page, inflight); + + if (zswap_write_in_flight_limit && + inflight[1] < zswap_write_in_flight_limit) + should_swap = true; + + if (zswap_read_in_flight_limit && + (should_swap || + (!should_swap && !zswap_write_in_flight_limit))) { + if (inflight[0] < zswap_read_in_flight_limit) + should_swap = true; + else + should_swap = false; + } + + if (should_swap) { + zswap_reject_io++; + ret = -EIO; + goto reject; + } + } +#endif + /* reclaim space if needed */ if (zswap_is_full()) { zswap_pool_limit_hit++; @@ -1264,6 +1315,10 @@ static int __init zswap_debugfs_init(void) zswap_debugfs_root, &zswap_reject_kmemcache_fail); debugfs_create_u64("reject_compress_poor", 0444, zswap_debugfs_root, &zswap_reject_compress_poor); +#ifdef CONFIG_ZSWAP_IO_SWITCH + debugfs_create_u64("reject_io", 0444, + zswap_debugfs_root, &zswap_reject_io); +#endif debugfs_create_u64("written_back_pages", 0444, zswap_debugfs_root, &zswap_written_back_pages); debugfs_create_u64("duplicate_entry", 0444, -- 2.7.4