From patchwork Tue Jan 30 01:36:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 13536679 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0A9FC47DA9 for ; Tue, 30 Jan 2024 01:42:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE4386B00B6; Mon, 29 Jan 2024 20:42:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A1FB56B00B7; Mon, 29 Jan 2024 20:42:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 849136B00B8; Mon, 29 Jan 2024 20:42:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6E0C26B00B6 for ; Mon, 29 Jan 2024 20:42:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4ADCEC015F for ; Tue, 30 Jan 2024 01:42:32 +0000 (UTC) X-FDA: 81734277744.05.02CF116 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) by imf13.hostedemail.com (Postfix) with ESMTP id A93B120007 for ; Tue, 30 Jan 2024 01:42:30 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=P2Fal7DA; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf13.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.177 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706578950; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ivn7Y9AQrDvvewOffcfUAdTab4Q3w5wTeKa9SOf1gRc=; b=JRJEf8LEhqlrGGTgohap0p2/E/XtaTlpcnIMzm3nFy3EJNqvscv90SsYiBJ5jE55v5q2AT lUqzCJLr0RJSr80zos0i7h3PeIlXz3dOf0bNNRwQEYpOXpwmKAaL5nMfHlNTjyrF9bLxc2 j5Ibv2QK4IvcoHsIP5hxkJOPjfXK0VI= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=P2Fal7DA; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf13.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.177 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706578950; a=rsa-sha256; cv=none; b=tH6ebsaq8XALj5OgBSYUjp53nuQ0HZyWbed4B/CJaIrL7TYTs+8HQFoo8R4UfS+stxFECk BuEFQ9px/ozMWkereGLWDqw9Reevkc91+OLTp7xxJiO597+g2ied9Qq4fyoXMOUhUcIGuC UdHmshPmrDIMWUCaoe+mnX+R5Q/HNPM= Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-429d7896d35so35391971cf.3 for ; Mon, 29 Jan 2024 17:42:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1706578950; x=1707183750; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ivn7Y9AQrDvvewOffcfUAdTab4Q3w5wTeKa9SOf1gRc=; b=P2Fal7DAZRD7ivMrPLaqdnchFCojMSt4jD/bmv4OfU+Y38yVYnjdD/74HK6x2yAuis ytvNxw9WHMC/bnYp+i7/xl8Pay61IBvb1rcG02zIEa28W83TTBB4+eBoK35/B8JdhwO5 a7EhkwOkwvZi1+KmDl5lo6mVq5XQ4g1isvZP3U1Fxmti9I4JYOBnMxBrD6VPf+ZgpIPG o645Tsr0NBHSvJdvXYwktaIrHfahEZXr6xLXzJVW7zRavSwG3lzm2/DxIn+M/aB4opDi PI1OyvdtNjCHxZYa+hupRLaZFa10hoUbfFgaBhvb8/QVU+LK8xtPYjDJtflngRbr0XqY z9AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706578950; x=1707183750; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ivn7Y9AQrDvvewOffcfUAdTab4Q3w5wTeKa9SOf1gRc=; b=C/ASQ+Ptlvd/1r2OXrLcaFgUDAnkLkNEA5B69XvgbYm/Yi54UQ2QFXkSYKjcc77k49 Zp6Vn0vq9xYPzHm3IU/669OSk90dL4pNODUIm8WJXfoRbUBZQGDpZqLFJ8DNiszUFpsy iBqTZSWY+qLz7fep47cmklCXQHM+jnxKqmLidR3KJCctNoFcDQ/fIrYIYidE7LgRwRiK 8vLtPTbbkzb8Wa99n7sR5vnv3TzNbT9IaBwLzFZyJLi7MM1B1Gxtwb08u9k7+LsRWOmN gLyAI37SujupepZ7uLBUM/rCiVMsZqC6+REeGjScWz7iAihksDd2SHocq9T3rfJyJ6k8 lsyw== X-Gm-Message-State: AOJu0Yz030huPT02moYosZ1wS8vxtWrFUJaHXBc/FwsyfW3JyX7hYMcl m0Ioag4lHYpH9bcX+9ZJ+T4uPhi0ESrhlRCv0OmREzvpaMdZVH+Xl0H1/vmlTkI= X-Google-Smtp-Source: AGHT+IFfZ8rsmJoVVOZQjiDjqNXCo9B7vayXBf6ACzYtz9VPfP8IlcslQtRqPIN1gd49Ezcam+jmmQ== X-Received: by 2002:a05:622a:1e09:b0:42a:600c:2d7e with SMTP id br9-20020a05622a1e0900b0042a600c2d7emr9275227qtb.51.1706578949706; Mon, 29 Jan 2024 17:42:29 -0800 (PST) Received: from localhost (2603-7000-0c01-2716-da5e-d3ff-fee7-26e7.res6.spectrum.com. [2603:7000:c01:2716:da5e:d3ff:fee7:26e7]) by smtp.gmail.com with ESMTPSA id cp7-20020a05622a420700b0042a6859ff3esm3078179qtb.61.2024.01.29.17.42.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 17:42:29 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Nhat Pham , Yosry Ahmed , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 13/20] mm: zswap: function ordering: pool params Date: Mon, 29 Jan 2024 20:36:49 -0500 Message-ID: <20240130014208.565554-14-hannes@cmpxchg.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240130014208.565554-1-hannes@cmpxchg.org> References: <20240130014208.565554-1-hannes@cmpxchg.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A93B120007 X-Stat-Signature: 34bhy91uwe8ys46p6e1tqk1f5z64gkaw X-HE-Tag: 1706578950-606909 X-HE-Meta: U2FsdGVkX19sO3Kghf42ZCjP1axUN7uc/262X+f/2b/TAGQv201THfozuUBDignNGBAqCJSHNWPqE0fCW7+A+IbzQIBR3NgHqq3tdn6IR6yskYDIKgz3uS4SrWdz3Q8oxaoj6C7+koyJfA5zvtfIq75faNKU7NFdWfeeNWLpqDS9rIHHmhnUdj/ip1l4ZkoPgRKnNgtArU5EPbfFhwPj6RUcDddWXmPg9Rq0utup6HDIl6qZnI7fLwadMPwXLbLiMKTRTKx+PUrvxLNX//7umyFU0KeUUTexNutqJ64nVbeg3dyJZ1g25Vj7umnI9J8JiINfpUkQZxugx0q/FxWGIIfUMz6LNwAC6WelypDTS4qZ+vn3n7iTmQCbQ/Ygmwwooj8GA4j6Q486Uo9YJ9tUTO5JpRJOb1OY0HDJvM3H4sYrpxyucct0tHSVQK2Xv91FWSlzJK8vbYp4mgtIm4DW7lLznrW18y4GJl6UzPCFFTdcMGVOCXp9i9xKJLuEpiW3GHvpc/TxxTp0BJM3/NtDfA7aOslasg5mT+a3S/ZLuEgDzNbe03YyQ6S5tSr2Di7WCK4q612kSZpE8pjQRXE5uyI70rtYMGVsmzTKQkcgP8A1uKOha1dVfr+0doiaDXWQktUO8qpETdUgz4BxY36LmYXStRoYNLIWtVwSMKgw97CXVCMKw+R4RLB138ceTyw04/E05bHwb1k0O69hTRBaYb1aiAptM+M1jnxx9uZAq/EwYP2SHp3p7y2A+y4nk6SuOEtD2TPbkmSaW6jLhQxyrZuUzTP46e43gsNjUI4G+BQa6znDZbQ3ToX5L8awRKYU8PPvEovKNE9YpPsQXNCq0ST5UGWsICUPpTOED16NvwLJAPcx8CGaEGzAKt9ADQXKErKSt9/70BftwFdOilGchWavV+4N9hqhmoeR9BCorEUn+rEDjt+T1UtLp/s8zPjh8HLMX6OxsxyWqjepiDb 39ycMag6 bK+Wl4a6ypxkEUPhmGIC3ns1BgRjgj2tgD2gMh5eDUOeO7RIROv083wnxeBtEGVRu+9cQtU3DYRck6I1snXaCmq/M6LhTCBPtGqXNIwLLF8Y4AXbOSOWYQaiIHAUxlwtOMPSXvWYzm4B70c7hyE3kdenYY4M8QoT52tm2oZLG7YzS10C/Q3lUL7xeFQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The parameters primarily control pool attributes. Move those operations up to the pool section. Signed-off-by: Johannes Weiner Reviewed-by: Nhat Pham --- mm/zswap.c | 312 ++++++++++++++++++++++++++--------------------------- 1 file changed, 156 insertions(+), 156 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 168afd6767b3..e650fc587116 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -590,6 +590,162 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) return NULL; } +/********************************* +* param callbacks +**********************************/ + +static bool zswap_pool_changed(const char *s, const struct kernel_param *kp) +{ + /* no change required */ + if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) + return false; + return true; +} + +/* val must be a null-terminated string */ +static int __zswap_param_set(const char *val, const struct kernel_param *kp, + char *type, char *compressor) +{ + struct zswap_pool *pool, *put_pool = NULL; + char *s = strstrip((char *)val); + int ret = 0; + bool new_pool = false; + + mutex_lock(&zswap_init_lock); + switch (zswap_init_state) { + case ZSWAP_UNINIT: + /* if this is load-time (pre-init) param setting, + * don't create a pool; that's done during init. + */ + ret = param_set_charp(s, kp); + break; + case ZSWAP_INIT_SUCCEED: + new_pool = zswap_pool_changed(s, kp); + break; + case ZSWAP_INIT_FAILED: + pr_err("can't set param, initialization failed\n"); + ret = -ENODEV; + } + mutex_unlock(&zswap_init_lock); + + /* no need to create a new pool, return directly */ + if (!new_pool) + return ret; + + if (!type) { + if (!zpool_has_pool(s)) { + pr_err("zpool %s not available\n", s); + return -ENOENT; + } + type = s; + } else if (!compressor) { + if (!crypto_has_acomp(s, 0, 0)) { + pr_err("compressor %s not available\n", s); + return -ENOENT; + } + compressor = s; + } else { + WARN_ON(1); + return -EINVAL; + } + + spin_lock(&zswap_pools_lock); + + pool = zswap_pool_find_get(type, compressor); + if (pool) { + zswap_pool_debug("using existing", pool); + WARN_ON(pool == zswap_pool_current()); + list_del_rcu(&pool->list); + } + + spin_unlock(&zswap_pools_lock); + + if (!pool) + pool = zswap_pool_create(type, compressor); + + if (pool) + ret = param_set_charp(s, kp); + else + ret = -EINVAL; + + spin_lock(&zswap_pools_lock); + + if (!ret) { + put_pool = zswap_pool_current(); + list_add_rcu(&pool->list, &zswap_pools); + zswap_has_pool = true; + } else if (pool) { + /* add the possibly pre-existing pool to the end of the pools + * list; if it's new (and empty) then it'll be removed and + * destroyed by the put after we drop the lock + */ + list_add_tail_rcu(&pool->list, &zswap_pools); + put_pool = pool; + } + + spin_unlock(&zswap_pools_lock); + + if (!zswap_has_pool && !pool) { + /* if initial pool creation failed, and this pool creation also + * failed, maybe both compressor and zpool params were bad. + * Allow changing this param, so pool creation will succeed + * when the other param is changed. We already verified this + * param is ok in the zpool_has_pool() or crypto_has_acomp() + * checks above. + */ + ret = param_set_charp(s, kp); + } + + /* drop the ref from either the old current pool, + * or the new pool we failed to add + */ + if (put_pool) + zswap_pool_put(put_pool); + + return ret; +} + +static int zswap_compressor_param_set(const char *val, + const struct kernel_param *kp) +{ + return __zswap_param_set(val, kp, zswap_zpool_type, NULL); +} + +static int zswap_zpool_param_set(const char *val, + const struct kernel_param *kp) +{ + return __zswap_param_set(val, kp, NULL, zswap_compressor); +} + +static int zswap_enabled_param_set(const char *val, + const struct kernel_param *kp) +{ + int ret = -ENODEV; + + /* if this is load-time (pre-init) param setting, only set param. */ + if (system_state != SYSTEM_RUNNING) + return param_set_bool(val, kp); + + mutex_lock(&zswap_init_lock); + switch (zswap_init_state) { + case ZSWAP_UNINIT: + if (zswap_setup()) + break; + fallthrough; + case ZSWAP_INIT_SUCCEED: + if (!zswap_has_pool) + pr_err("can't enable, no pool configured\n"); + else + ret = param_set_bool(val, kp); + break; + case ZSWAP_INIT_FAILED: + pr_err("can't enable, initialization failed\n"); + } + mutex_unlock(&zswap_init_lock); + + return ret; +} + /* should be called under RCU */ #ifdef CONFIG_MEMCG static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry) @@ -1163,162 +1319,6 @@ static void shrink_worker(struct work_struct *w) zswap_pool_put(pool); } -/********************************* -* param callbacks -**********************************/ - -static bool zswap_pool_changed(const char *s, const struct kernel_param *kp) -{ - /* no change required */ - if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) - return false; - return true; -} - -/* val must be a null-terminated string */ -static int __zswap_param_set(const char *val, const struct kernel_param *kp, - char *type, char *compressor) -{ - struct zswap_pool *pool, *put_pool = NULL; - char *s = strstrip((char *)val); - int ret = 0; - bool new_pool = false; - - mutex_lock(&zswap_init_lock); - switch (zswap_init_state) { - case ZSWAP_UNINIT: - /* if this is load-time (pre-init) param setting, - * don't create a pool; that's done during init. - */ - ret = param_set_charp(s, kp); - break; - case ZSWAP_INIT_SUCCEED: - new_pool = zswap_pool_changed(s, kp); - break; - case ZSWAP_INIT_FAILED: - pr_err("can't set param, initialization failed\n"); - ret = -ENODEV; - } - mutex_unlock(&zswap_init_lock); - - /* no need to create a new pool, return directly */ - if (!new_pool) - return ret; - - if (!type) { - if (!zpool_has_pool(s)) { - pr_err("zpool %s not available\n", s); - return -ENOENT; - } - type = s; - } else if (!compressor) { - if (!crypto_has_acomp(s, 0, 0)) { - pr_err("compressor %s not available\n", s); - return -ENOENT; - } - compressor = s; - } else { - WARN_ON(1); - return -EINVAL; - } - - spin_lock(&zswap_pools_lock); - - pool = zswap_pool_find_get(type, compressor); - if (pool) { - zswap_pool_debug("using existing", pool); - WARN_ON(pool == zswap_pool_current()); - list_del_rcu(&pool->list); - } - - spin_unlock(&zswap_pools_lock); - - if (!pool) - pool = zswap_pool_create(type, compressor); - - if (pool) - ret = param_set_charp(s, kp); - else - ret = -EINVAL; - - spin_lock(&zswap_pools_lock); - - if (!ret) { - put_pool = zswap_pool_current(); - list_add_rcu(&pool->list, &zswap_pools); - zswap_has_pool = true; - } else if (pool) { - /* add the possibly pre-existing pool to the end of the pools - * list; if it's new (and empty) then it'll be removed and - * destroyed by the put after we drop the lock - */ - list_add_tail_rcu(&pool->list, &zswap_pools); - put_pool = pool; - } - - spin_unlock(&zswap_pools_lock); - - if (!zswap_has_pool && !pool) { - /* if initial pool creation failed, and this pool creation also - * failed, maybe both compressor and zpool params were bad. - * Allow changing this param, so pool creation will succeed - * when the other param is changed. We already verified this - * param is ok in the zpool_has_pool() or crypto_has_acomp() - * checks above. - */ - ret = param_set_charp(s, kp); - } - - /* drop the ref from either the old current pool, - * or the new pool we failed to add - */ - if (put_pool) - zswap_pool_put(put_pool); - - return ret; -} - -static int zswap_compressor_param_set(const char *val, - const struct kernel_param *kp) -{ - return __zswap_param_set(val, kp, zswap_zpool_type, NULL); -} - -static int zswap_zpool_param_set(const char *val, - const struct kernel_param *kp) -{ - return __zswap_param_set(val, kp, NULL, zswap_compressor); -} - -static int zswap_enabled_param_set(const char *val, - const struct kernel_param *kp) -{ - int ret = -ENODEV; - - /* if this is load-time (pre-init) param setting, only set param. */ - if (system_state != SYSTEM_RUNNING) - return param_set_bool(val, kp); - - mutex_lock(&zswap_init_lock); - switch (zswap_init_state) { - case ZSWAP_UNINIT: - if (zswap_setup()) - break; - fallthrough; - case ZSWAP_INIT_SUCCEED: - if (!zswap_has_pool) - pr_err("can't enable, no pool configured\n"); - else - ret = param_set_bool(val, kp); - break; - case ZSWAP_INIT_FAILED: - pr_err("can't enable, initialization failed\n"); - } - mutex_unlock(&zswap_init_lock); - - return ret; -} - static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) { struct crypto_acomp_ctx *acomp_ctx;