From patchwork Mon Jan 27 07:29:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13951029 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 E745FC0218C for ; Mon, 27 Jan 2025 07:30:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 717352800DA; Mon, 27 Jan 2025 02:30:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C673280129; Mon, 27 Jan 2025 02:30:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56DC92800DA; Mon, 27 Jan 2025 02:30: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 316212800DA for ; Mon, 27 Jan 2025 02:30:31 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DC7BF1C99C0 for ; Mon, 27 Jan 2025 07:30:30 +0000 (UTC) X-FDA: 83052409020.19.84974CD Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf30.hostedemail.com (Postfix) with ESMTP id CAC608000F for ; Mon, 27 Jan 2025 07:30:28 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=EKebMEEg; spf=pass (imf30.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.172 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=1737963028; 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=9PZlb0bpCrEa0EBxHNBCQaxxKLzdyJEYyXpra8I/P5g=; b=J+5un3W0V2ngk8nmD7cUZykX05P6D37aDIyNF9/zRhxJxMjHnm0/NN/OAk7JZvkJXGWCRZ pkhl2TpcrcTL2DfRHkC81FAsdaJb1NKsSZ2rs42G9vRoDBr35IvPcvCJV3HjY/1MoB81BJ MFlkwwyN6oZdRXFjkwiHRykOSplhxSs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=EKebMEEg; spf=pass (imf30.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.172 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=1737963028; a=rsa-sha256; cv=none; b=u8TYZ7wMOd2mTPdt6+tWYcypKZJgnWVhDzU/CrqBCLx6kWnbGR5maYiyDvyJTrrUM86s6b MNcZ/d4fU7JJ8tpOHyHaZcZIyNYeBQav99RGcPaRtQPAo04nTscRs9Ehrc5AACOvhSLqGw /4qCciVCmvwbX9HcNEfC9zQB8EKp/7k= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2167141dfa1so70816315ad.1 for ; Sun, 26 Jan 2025 23:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737963027; x=1738567827; 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=9PZlb0bpCrEa0EBxHNBCQaxxKLzdyJEYyXpra8I/P5g=; b=EKebMEEgBqMhedceitSCfrDPJTgIke6GGtWEPn/Kmusg75WI3bItLz///Xyo2jl2CR Hm0JcxtXAihsEO8E9DY+jLwh/GHKzfpxk79U3lFlCEiLTmu4FJ2pcCmJ/AAgQB8S0JDU GKXrXLo/e0zkf/2tAjq+w8x052yZdoVsaKY0M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737963027; x=1738567827; 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=9PZlb0bpCrEa0EBxHNBCQaxxKLzdyJEYyXpra8I/P5g=; b=pAtxWulbEBtp+6jGiG6kIpCNXiKoX5U/IgLeN8RVC2vLciDJiKIzxBxDCKNGnE1EUe 4SSwGrrqqT9fhGzPBv/lrdq4sTi/I9jq6wf3N1PmFHzVrEy3CfDeYSGy4Z5w6Slkn1mU lLtueM6842432sIlzu6G4l+416IrI/FXXcNt4ls0sdhUrUQenT5Y+a3reVzTctwb5F5T Sgjl9vpwqHZuju2qVwHIT/wqfO/fa/8XVzRkcaHkObYVzyRIQx24SuuQQKPQjGVKwB06 EMf1ZWWgIhVcXuNbGcIrwpmbcwIuh7TA0gk2FqxLWZaL7+1d+L2X3SI6Bk28r8a+aGWI lP0g== X-Gm-Message-State: AOJu0Yz7mG8OSY6u1R9ybU3uE1lVzItR1sE5C2QYXMbQCaGXOdLO+j5e bqh1kE3z0mbd9WWJM2PliqjsCECxaj46tUQgqTChdbzv9AD3ewn770UPMoJfgw== X-Gm-Gg: ASbGnctmlFGe+SAeyAZ9LrzGbp25MMx2KtE607AS5LpxG0FIZw8QE3s9K7dzD0a9n5n 7i407shPdfOv/Yv8Tc6wXCVl2qzGQsKvZ5RwdntXeNACSkuRgr5MtGmvs/hjNKMrYqROMMi/Y6T BWBd3SvNOP0+FH6TxokmQwqIEJHexEcBS74Dxw6vR+4ixubFaWb1xkhLcg2Ie8gVkHSclEd3mPL yjDrJqL6/MkYWu3qDnADhIgaDeVolgrmqZh2iHneJ7N0NUYll0hzN2TJuAxx7kfc2NztRKrQ7Ed /u4ulUI= X-Google-Smtp-Source: AGHT+IGG3TeaG4wWi40jK1p6mY3yatjcS1oAjwA+7dpgpn76mSRyGup3rx8p2HazaczZbRSX+r3sbw== X-Received: by 2002:a17:903:40c8:b0:216:6284:8a00 with SMTP id d9443c01a7336-21da4a11a0bmr202401385ad.2.1737963027560; Sun, 26 Jan 2025 23:30:27 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:566d:6152:c049:8d3a]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-21da4141123sm57299285ad.112.2025.01.26.23.30.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Jan 2025 23:30:27 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 9/9] zram: unlock slot during recompression Date: Mon, 27 Jan 2025 16:29:20 +0900 Message-ID: <20250127072932.1289973-10-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250127072932.1289973-1-senozhatsky@chromium.org> References: <20250127072932.1289973-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CAC608000F X-Stat-Signature: 3yy7jqxdrk7hutiphfjuf7teutzpbnos X-Rspam-User: X-HE-Tag: 1737963028-449195 X-HE-Meta: U2FsdGVkX1/mwpRkNSWuDXFxq0/370jDhCv5Mgm4hFILaXCkphoPVGcHctG7Wt86w8YUD3jFz346Vau0DQ5bL/uHPKKdJkqpNy1P8OXKKrjLh/pc6WqfVq2/9HVb27eWrnenW84EvDY1AUnGMAwmKCqHjCpchJnxDZdUBPXQDd+aLW4PkLylPpYYrGctifuIu1lAGZ4Y21fntmMIvokXsowo7v9MrarnfHquexM+PRE5L7S8c8/4En3tvVTEEIDwzfubc/m9st8JXOMJeIpdcFzoHOJHX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, 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 | 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 07cae8fe9b66..ad3e8885b0d2 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1908,6 +1908,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. @@ -1916,13 +1924,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); @@ -1930,10 +1931,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); @@ -1949,6 +1948,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 @@ -1986,17 +1998,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);