From patchwork Thu Jan 30 11:10:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13954491 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 D5BC1C0218D for ; Thu, 30 Jan 2025 11:12:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DAF4280288; Thu, 30 Jan 2025 06:12:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 439B22800D6; Thu, 30 Jan 2025 06:12:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B2D3280288; Thu, 30 Jan 2025 06:12:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 07E1D2800D6 for ; Thu, 30 Jan 2025 06:12:31 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2297147095 for ; Thu, 30 Jan 2025 11:12:10 +0000 (UTC) X-FDA: 83063854020.28.B1237D1 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf14.hostedemail.com (Postfix) with ESMTP id 3319210000E for ; Thu, 30 Jan 2025 11:12:08 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CXYuKn10; spf=pass (imf14.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738235528; 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=MLxgJRPaVNwNF6Kcm4INbLog064v7nke9leBvmEg3e4=; b=M7Eb6V4ctAd1sLxWyV2/T3uIid5PpuyEI9/Q++aB50Lv58brJVAS7v8kvmreUc9PZNMdmm RkX0iTrqpJGH566fVj/hilKq8wFsliJYrXm0BtzGLViyzCfGPmZ1WRCmVyhtfGXa6eXEkM QjUrIzvTT1o7wqXfov4q2H9H66mPwnc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CXYuKn10; spf=pass (imf14.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738235528; a=rsa-sha256; cv=none; b=gSa15OVjL/NuLV152p9lsPqDcWF41NL/DsH9g20gekkb97422v0NXX/JfEsJtPkPYoCbP5 cMWhoL6eqAgRNYqY3MbKSHp37BKYYmAZ5Ktjg2EQ3FYiF4bFFZt4FnD2T7G7ADZklcBWSc kSfFg7hdhXK1B4UwH3dWg6NyZwZjxnw= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2161eb94cceso6961845ad.2 for ; Thu, 30 Jan 2025 03:12:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1738235527; x=1738840327; 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=MLxgJRPaVNwNF6Kcm4INbLog064v7nke9leBvmEg3e4=; b=CXYuKn10dbv58WqVoL7XTwWDWI0HLqY3yDIR3J1pchqVpytzS4ojatJlxx/lTeaZEz 8tGcfDrgWlJeSviwa6fpkZMXQKy3LzTLHpTA2sRqMkqlAgFiIDAhpUf+sfWq9Hn2O5rX RzyiWIOwIKN6ZECkjNiOSRXkTQCNrZhCvVxPs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738235527; x=1738840327; 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=MLxgJRPaVNwNF6Kcm4INbLog064v7nke9leBvmEg3e4=; b=TuFSLRe65WH0B0OMPHsKyFiCh4jwCbok/DQPFKF2VovvW2cBNperPN/VK3id7KSy1L TgLFdPZrYpYXzOyQ60UJuvV5Ge+PbT429Ntqh0QDO9tHF5FZCjMaTT72Zfl5/AIvj9tg YFNPHayn+kKxpg1j5H4U/qT6+b288CKAJpfKGY/VmNtnfWYrp+3jHkSmSzqQr0qMkTA4 v4JDrDb3juhnODqA/2gCCFmouEZqiM9okS5bbUQ2sTyodgfDpGwZ1jy7aqKCTUipD/dG MZRKw2F43e1i7LJSY8/bzZ0LFquaM1rlJJ1n9i0yRSehl1yKfJsG+zMtiNozG+6w9Gsy C8+w== X-Forwarded-Encrypted: i=1; AJvYcCVHHlimJ+o3JZ9BjZlyU4cITHYUnlocwWT7Gf8JZa1B21UCqK6+5jikkXIimcYPgsAG+p9pt+aXKQ==@kvack.org X-Gm-Message-State: AOJu0YwIhbz+GnoXFrVSTZq0/PJZgsiBgHCZt0oYIiwiHD3SzgJH7D/q C2yOnY9IKyc3/KyWgwlCEym1zbzyK329mPROhV1LhgdozKIaNt3bmBLfrsZw+s2WHj7xL64zu5A = X-Gm-Gg: ASbGncswYwdelbLKKGrQMDot1Ph9CGdC+cW+a7L2UhyGdCx95YCQJNEbNAVZpzIC8Gr nVmvtO+oc9ihT9ww/cMY8ArDn/9gEcIeNwloIQMZe4d5jqwI7pVL+xsrZSthlxPUv89eZqOc3Pz /QcNQg67vDWoDPc1fPuGOnocJLCDbBrZNC+j4cbx5VSh8L8y2zINvTj8DWAm2mx0al+hpR7PuhD wHv/mNJQYBkeR/LWEuRPGQXt/nZY90jnCjPDB3rOhNCAQmew2CcdVrn+lAeJvqUT1CtnNntKIF8 XsmY9CpI/X+SR44z X-Google-Smtp-Source: AGHT+IEmgqIzatKzhSxji3bLavuavRKHjn6PnEoK7vJ9Qf8RpneZSWnCVW2omFdSy80mZ0nOsxnoUg== X-Received: by 2002:a05:6a20:9c8e:b0:1e6:8f39:d607 with SMTP id adf61e73a8af0-1ed7a6b17bcmr12484502637.31.1738235527034; Thu, 30 Jan 2025 03:12:07 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:d4ce:e744:f46b:4fb]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-72fe6429218sm1210151b3a.66.2025.01.30.03.12.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jan 2025 03:12:06 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton Cc: Minchan Kim , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 11/11] zram: unlock slot during recompression Date: Thu, 30 Jan 2025 20:10:56 +0900 Message-ID: <20250130111105.2861324-12-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250130111105.2861324-1-senozhatsky@chromium.org> References: <20250130111105.2861324-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3319210000E X-Stat-Signature: sy16n5pdsuqpytwb9wjwqnbhdtt4ndsp X-Rspam-User: X-HE-Tag: 1738235527-61490 X-HE-Meta: U2FsdGVkX18AmIvfPDFafucSNuEM8yi3m/kk4DbDgSVdj85V4HtoB3JUIPk4bOu2TcU0r0VvfV1GZQsgn2ICqXKnb8B+SUG/qS9hKvGwHJF+8eug6x4wyFdYWc1s3vMTrABxHAXkRkaf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X1bgj5RC/FBIiy0/UaUJ8ClQeIBn8po5Xxi33o4dex4ETYcmOJMcKBhZJlSVnmwbAggIVvvRwd5fHv0oXlb7q+shYX21jJb7qeaFFfDVYBDOWSluNtwc9g== 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: Recompression, like writeback, makes a local copy of slot data (we need to decompress it anyway) before post-processing so we can unlock slot-entry once we have that local copy. Unlock the entry 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 slot-entry write-lock before zsmalloc allocation and grab it again after the allocation. In both cases, once the slot-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 | 80 +++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 500d6c8b17fc..a6bc1c2dfbe6 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1871,14 +1871,13 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, u64 *num_recomp_pages, u32 threshold, u32 prio, u32 prio_max) { - struct zcomp_strm *zstrm = NULL; + struct zcomp_strm *zstrm; unsigned long handle_old; unsigned long handle_new; unsigned int comp_len_old; unsigned int comp_len_new; unsigned int class_index_old; unsigned int class_index_new; - u32 num_recomps = 0; void *src, *dst; int ret; @@ -1905,6 +1904,13 @@ 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); + prio = max(prio, zram_get_priority(zram, index) + 1); + /* Slot data copied out - unlock its bucket */ + zram_slot_write_unlock(zram, index); + /* Recompression slots scan takes care of this, but just in case */ + if (prio >= prio_max) + return 0; + /* * Iterate the secondary comp algorithms list (in order of priority) * and try to recompress the page. @@ -1913,24 +1919,14 @@ 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); ret = zcomp_compress(zram->comps[prio], zstrm, 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); @@ -1939,6 +1935,7 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, if (class_index_new >= class_index_old || (threshold && comp_len_new >= threshold)) { zcomp_stream_put(zram->comps[prio], zstrm); + zstrm = NULL; continue; } @@ -1946,14 +1943,7 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, break; } - /* - * We did not try to recompress, e.g. when we have only one - * secondary algorithm and the page is already recompressed - * using that algorithm - */ - if (!zstrm) - return 0; - + zram_slot_write_lock(zram, index); /* * Decrement the limit (if set) on pages we can recompress, even * when current recompression was unsuccessful or did not compress @@ -1963,37 +1953,55 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, if (*num_recomp_pages) *num_recomp_pages -= 1; - if (class_index_new >= class_index_old) { + /* Compression error */ + if (ret) { + zcomp_stream_put(zram->comps[prio], zstrm); + return ret; + } + + if (!zstrm) { /* * Secondary algorithms failed to re-compress the page - * in a way that would save memory, mark the object as - * incompressible so that we will not try to compress - * it again. + * in a way that would save memory. * - * We need to make sure that all secondary algorithms have - * failed, so we test if the number of recompressions matches - * the number of active secondary algorithms. + * Mark the object incompressible if the max-priority + * algorithm couldn't re-compress it. */ - if (num_recomps == zram->num_active_comps - 1) + if (prio < zram->num_active_comps) + return 0; + if (zram_test_flag(zram, index, ZRAM_PP_SLOT)) zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE); return 0; } - /* Successful recompression but above threshold */ - if (threshold && comp_len_new >= threshold) + /* Slot has been modified concurrently */ + if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) { + zcomp_stream_put(zram->comps[prio], zstrm); 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);