From nobody Fri Dec 19 21:54:36 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 383A41C2DA2 for ; Wed, 22 Jan 2025 05:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737525580; cv=none; b=o2w8aGtD3IiEco2f4Ep13T7CGUVPgQHr/v7D/FXUZI0vORPO4TXGEW7i5jfOveVnbwyWNGkrl38HPoYyz8ABwEDICxgcfGgQwCEm9UJQ2P5sa/mAm5aFwR+nl+WluC0x3ve9eFmoJ3Vo23H/Cx7fI+BU7MWo9J3HUcwBMSq2WAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737525580; c=relaxed/simple; bh=+ib/PEbdXlxjG+X7FzKlPeDQ6Vb0QZjNB7Cca9ibMCk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VfeIwxXgeyMA26JWrDEYPXTejXINeQItpzsk85lgc/aFEtYaazSmp9VfijPLBWOOGk9Uh7NZoAT+NGvGBzsew3/OOgX7a85v8sc9a5B4fd+gLy4RQnpeuzx5QCMiXrYiYJyIGga7u/LaMpDrmEoJ3RXG5IsVmiQwTaX8+8sEnTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=PRQB/RDn; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PRQB/RDn" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21654fdd5daso111814005ad.1 for ; Tue, 21 Jan 2025 21:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737525578; x=1738130378; darn=vger.kernel.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=PRQB/RDnnbHgaFCAwWAg/8afO9FYX4tBYi3DkE0mI+BUsQO7nOBpEI8rIXTpoLkMM+ 4SoHUWC27XVX6LAE+mVHAot2YXTmbtHUguucyNa72M0ugUywID9DLXa19vru/P5tA2SZ 5B3eq/uipfpDU4t6OsaGfJhXEbGrhG/ptDIpc= 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=cScwkc+FCuDo0sfOQdtbJVgGE72uTZxZ8MzgcTPyiNnWACs18GCuO9QhJFiJ5TAkG6 XWLhpTzE6wUyCqyYN8qLrh2JsDYVkveeXNsP2a/aBJxEdxhU8g1cHBrs9IXrefua6FB2 VxGjC/9Y/6ArwTDDjXA/+/rhSqqTR3OBzWQ506PEtokDX3HvjKVqNVY5D3AFx4uUNKZm dtk84HihnTTMkM+uapWJPbzZ1VtqNtrECWQgGcNtcou4Vyczwmw6QgGaIPGYjgPBxSyW Hbr6hHrlBtgTL5qyIvftTVaXbsZQvCihcO2K7OV1SzzY5dVJeIRTrY98/HAFkk3kMqK7 3OZw== X-Forwarded-Encrypted: i=1; AJvYcCXIPBVkaAWVb8wCris5nOj1R4vjK3P4eeb/0cnp6O7h/wfy0RwGBOip4JemtdxJENX7t2qkBpPZCyV+foQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yyho545g/xY1otBKlUpHRPPx0SZBy8XtnKDGUj2FI0JqcIHxDLJ UW6R4wXOHF9bA4OahohhUpj1cIgF4y5AtGrr8NlZ1dTrZ1Vhal7bjcRvj0sNPA== X-Gm-Gg: ASbGncu4L1jsuQb6UrLwOJLojfzT7ZB8nJp2Qf0A8MQkNrkxVzX6U2q1jeWhmLWJZ55 aA4msRkXFHjqn5CdndP3nEdQIC8nD3+275aNNE/IHb+oIjTm4b7CjKTNspBI+1F5VDT48MvtvWw zyv9/QTaze2dmTG04g4utlv66HQ7orncNOgiGfeoH6mkWXFxkGu0M1J2rvsAQgl5I/4tRXlypkx GdGOvIkcY3F0wcY+oUaVoCBjdOoRNkY+NenzwRABCzBPEOQFkgsAC/05fhLqEVDO8YuLUpu 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 in= dex, struct page *page, zram_clear_flag(zram, index, ZRAM_IDLE); =20 class_index_old =3D 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 =3D 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 in= dex, struct page *page, if (!zram->comps[prio]) continue; =20 - /* - * Skip if the object is already re-compressed with a higher - * priority algorithm (or same algorithm). - */ - if (prio <=3D zram_get_priority(zram, index)) - continue; - num_recomps++; zstrm =3D zcomp_stream_get(zram->comps[prio]); src =3D kmap_local_page(page); @@ -1947,10 +1948,8 @@ static int recompress_slot(struct zram *zram, u32 in= dex, struct page *page, src, &comp_len_new); kunmap_local(src); =20 - if (ret) { - zcomp_stream_put(zram->comps[prio], zstrm); - return ret; - } + if (ret) + break; =20 class_index_new =3D zs_lookup_class_index(zram->mem_pool, comp_len_new); @@ -1966,6 +1965,19 @@ static int recompress_slot(struct zram *zram, u32 in= dex, struct page *page, break; } =20 + 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 i= ndex, struct page *page, if (threshold && comp_len_new >=3D threshold) return 0; =20 - /* - * 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 =3D 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); } =20 + /* 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 =3D 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); --=20 2.48.0.rc2.279.g1de40edade-goog