From patchwork Wed Jan 22 05:57:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13946864 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 80CCFC02182 for ; Wed, 22 Jan 2025 05:59:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 13D6B280004; Wed, 22 Jan 2025 00:59:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C5D4280001; Wed, 22 Jan 2025 00:59:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA9C0280004; Wed, 22 Jan 2025 00:59:41 -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 CB535280001 for ; Wed, 22 Jan 2025 00:59:41 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 753A314138E for ; Wed, 22 Jan 2025 05:59:41 +0000 (UTC) X-FDA: 83034036162.30.8AC5C35 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf07.hostedemail.com (Postfix) with ESMTP id 9DFCA40007 for ; Wed, 22 Jan 2025 05:59:39 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=SziaP01p; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf07.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737525579; a=rsa-sha256; cv=none; b=MpMisBKbbHzXmawlWqtqaJMlmhFlnYodAFJ5Pdp2bqIseIUgZyPUwM7FiIGGArYWOPyTKH erZciicOc31CDPBpIQSMpUXeaWebApvY5aFjvGHVqfrx6Cneq0YFgBa1ebVkWH9007j53y WPXdLdZkFZ4MA8a+xDkXEkj6Koz0Zhw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=SziaP01p; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf07.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737525579; 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=URQGVN2RLRBAm/FHMFqf5kTEz8apzib8vA7ECF9oUaE=; b=l77rvw3N1Mf/qF9Iu0LerdG3ycBNB9v5Q/p7XyM6l9UEcai15SGHKpUL/OFr6tCNcP2nXL rAUw8cLfjBxc+tfLhDHWLOlpG2pOmMaBJrLSTfmow61FFaAw2ImkQlQPC2k5wBOaRyodQu t/y8p5JOWUnpYDl8KXCpxFC4OPUDIDU= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2166022c5caso102999515ad.2 for ; Tue, 21 Jan 2025 21:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737525578; x=1738130378; 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=URQGVN2RLRBAm/FHMFqf5kTEz8apzib8vA7ECF9oUaE=; b=SziaP01pLwV1RKpB+Nw6e+8ZxpuBUeyFaDXwk3TKIta5lRG4119v77wiewapiFfgf3 ru7jOz+Mc+nSgdquqOyoTNWDyg5CVboY8MhKqXgGwxYyWx1KPf170+/Y8PcCGhJz769q 5GalG2DcLxBZgjP/WAA6G5GkzgLzDGIic+5pE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737525578; x=1738130378; 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=URQGVN2RLRBAm/FHMFqf5kTEz8apzib8vA7ECF9oUaE=; b=vybKPEdhHG4QuZYzLLsbdh3MurisoE0dpx/2DeLfDqPWPNl2wjMExqiE7o20KgvFvb QxhA7tG7pe9zTl/ZSqo8NVlVkYrD3BiGvvN+G2sk941zfYVMUrqHO6hZEI3g5FLUQAod J1Zzv9FAyhn5amHOjP1MeUYnvIB+wUjrk20Zrodmmf5ZetB7VcFLJAEGs2ic1ecoAe8I bHq2scHzqJbRDjUskbaIvkqyo3vJxmKvjMQmDs4N11zFzwoTZ0v39+LRVmCETjzZTWNu MIYC3uABXi8V8n0AqzL3fh2J+pIjQpN82z0fA/9m05Rw+e652WRqQXMZjd1dAJbUoYez vIQg== X-Gm-Message-State: AOJu0YxvvQuDsqSQc+bj1ACq+n4ibYzqg49lF+AbFjv8zE+tg7BJ2Spg OvnoGsU6EM4NU5Dj+qHHjHYq7YkDVZDv8ofQwuvf7f3WCRUQwfo2FO6JOCxvVdoquqthD4WrmRc = X-Gm-Gg: ASbGncvSRtBqwhqEjyNg6/fkQ9/8bCrb1cNxvLzWYA2TMbgyJ9DoM0KwMlmXdzeYgDJ /marALRfi/KdChAN1uVlwdLIvNxu7ZqmGDOR3i5NvZGHbbQPIQ1hsE5Hdw5s8gH1apq8HVzCffg y6FZnzM7mWmzi4gLNjxsjN9zRT53O2NMp7lZMeD+TFq8PxCmxE4UNXW384JhiKOp8gUX9PxeRIT C0tNjAK6WRoZxJvVyDnen3DdeeY8GaDsbsVIS2Dq85CqFFfglWcXUjPp+9QZ//IgAaGKyCP X-Google-Smtp-Source: AGHT+IHfDORXl8892ZPFv7Yg9s3aa+pqcjz8wYjoVNgZaPfPzukdMK7LQbRYaeoP/wmNeG24p8MYWA== X-Received: by 2002:a17:902:d2c5:b0:216:779a:d5f3 with SMTP id d9443c01a7336-21c353edbd7mr334668355ad.14.1737525578444; Tue, 21 Jan 2025 21:59:38 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:2902:8f0f:12b3:c251]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-21c2d3ac3e2sm86558565ad.139.2025.01.21.21.59.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Jan 2025 21:59:38 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCH 7/7] zram: unlock slot bucket during recompression Date: Wed, 22 Jan 2025 14:57:45 +0900 Message-ID: <20250122055831.3341175-8-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog In-Reply-To: <20250122055831.3341175-1-senozhatsky@chromium.org> References: <20250122055831.3341175-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 9DFCA40007 X-Rspamd-Server: rspam10 X-Stat-Signature: wmx6td1qc4qa9uabe7sdirmxwcjimud8 X-HE-Tag: 1737525579-953084 X-HE-Meta: U2FsdGVkX19jewCQND0fjKZDBTclRnVjxKrHj1OwG6A6J7GZ11ohdw10MLsUpk3dUfCqCP790jwn+iidWQp9C8DYMur1u1hMZFrqWnifao693zJ+YWoyuVOK0WCwzuzSZUML35jdCwxmhQ1mDtZCOUt1ljYNGlC4EMg30ald1mdVWyNtR/A9c9x3awjnoJl3Ss1YPwc99n1IgB8r/DL9GbYWtWFluhCmC4DHKawtUrgzGQA42GGpj+dRqH3LzzpypBX1qRDMNSuQLSL37I+Pud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amicity=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: As of now recompress_slot() is called under slot bucket write-lock, which is suboptimal as it blocks access to a huge number of entries. The good news is that recompression, like writeback, makes a local copy of slot data (we need to decompress it anyway) before post-processing so we can unlock slot bucket once we have that local copy. Unlock the bucket write-lock before recompression loop (secondary algorithms can be tried out one by one, in order of priority) and re-acquire it right after the loop. There is one more potentially costly operation recompress_slot() does - new zs_handle allocation, which can schedule(). Release the bucket write-lock before zsmalloc allocation and grab it again after the allocation. In both cases, once the bucket lock is re-acquired we examine slot's ZRAM_PP_SLOT flag to make sure that the slot has not been modified by a concurrent operation. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 53 +++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index d516f968321e..0413438e4500 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1925,6 +1925,14 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, zram_clear_flag(zram, index, ZRAM_IDLE); class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old); + + /* + * Set prio to one past current slot's compression prio, so that + * we automatically skip lower priority algorithms. + */ + prio = zram_get_priority(zram, index) + 1; + /* Slot data copied out - unlock its bucket */ + zram_slot_write_unlock(zram, index); /* * Iterate the secondary comp algorithms list (in order of priority) * and try to recompress the page. @@ -1933,13 +1941,6 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, if (!zram->comps[prio]) continue; - /* - * Skip if the object is already re-compressed with a higher - * priority algorithm (or same algorithm). - */ - if (prio <= zram_get_priority(zram, index)) - continue; - num_recomps++; zstrm = zcomp_stream_get(zram->comps[prio]); src = kmap_local_page(page); @@ -1947,10 +1948,8 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, src, &comp_len_new); kunmap_local(src); - if (ret) { - zcomp_stream_put(zram->comps[prio], zstrm); - return ret; - } + if (ret) + break; class_index_new = zs_lookup_class_index(zram->mem_pool, comp_len_new); @@ -1966,6 +1965,19 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, break; } + zram_slot_write_lock(zram, index); + /* Compression error */ + if (ret) { + zcomp_stream_put(zram->comps[prio], zstrm); + return ret; + } + + /* Slot has been modified concurrently */ + if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) { + zcomp_stream_put(zram->comps[prio], zstrm); + return 0; + } + /* * We did not try to recompress, e.g. when we have only one * secondary algorithm and the page is already recompressed @@ -2003,17 +2015,28 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, if (threshold && comp_len_new >= threshold) return 0; - /* - * If we cannot alloc memory for recompressed object then we bail out - * and simply keep the old (existing) object in zsmalloc. - */ + /* zsmalloc handle allocation can schedule, unlock slot's bucket */ + zram_slot_write_unlock(zram, index); handle_new = zs_malloc(zram->mem_pool, comp_len_new, GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); + zram_slot_write_lock(zram, index); + + /* + * If we couldn't allocate memory for recompressed object then bail + * out and simply keep the old (existing) object in mempool. + */ if (IS_ERR_VALUE(handle_new)) { zcomp_stream_put(zram->comps[prio], zstrm); return PTR_ERR((void *)handle_new); } + /* Slot has been modified concurrently */ + if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) { + zcomp_stream_put(zram->comps[prio], zstrm); + zs_free(zram->mem_pool, handle_new); + return 0; + } + dst = zs_map_object(zram->mem_pool, handle_new, ZS_MM_WO); memcpy(dst, zstrm->buffer, comp_len_new); zcomp_stream_put(zram->comps[prio], zstrm);