From nobody Thu Jan 30 23:58:53 2025 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 5310D1FCCE7 for ; Mon, 27 Jan 2025 07:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737963029; cv=none; b=NmpaPsQAdXmaMSgocJNtisS4Zg2zHhOyIFKIbNVrLnd0jXcyQoI3GY/OulhcjsUBfNSsNz2HPD2V4duddCeENBL1+WvuLPvmjKD4gPXk1dDRLiqI2Ox3X49jErf7J+qGGABMtNMu7sAkqXM+Qe+vcRNGGVCGWR9uN0quowmmSos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737963029; c=relaxed/simple; bh=Hf98AyQ6lwoMxllJeMlrmQv2v8eJCPBdKlL8SUemZ8c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ieFxusF7psDYqg5T5h4vzz8cw0DdI23ybPw/KteJu5if3M/AXVvIpHgu2h1YlzbyOiZKdPUITvNSwBy+jgjOiIKCGddcOpMPfMS5JvQTNIR2Jm+89Y9chFc0VZA1SIblP/jMOMZy0lhUi1FCXTlZ7ITf7UrNHP8wcWa30qC7s8o= 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=f+tmfVLf; arc=none smtp.client-ip=209.85.214.179 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="f+tmfVLf" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-21631789fcdso66709055ad.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=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=9PZlb0bpCrEa0EBxHNBCQaxxKLzdyJEYyXpra8I/P5g=; b=f+tmfVLfsftGBNYme/+V0lYO0auvCxTtMUnCLkGB4ko73CYC2jDAISLOy8e2iypF68 sz/kR23/NPlBIkLnuKkqpuj+XByjCc8JqHHysak6dHTZGP4nJXtPp9MfwOfFgNieWJKv eYgIRN1qwEq3N1smF64OxJVgJR52fdiC1DJBo= 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=cac7WMg93yW4d8dzILRX2v8dVTiUY0EaCRGr0/rrrpXrk1gDqjGIqUeKfci9BaY33x Dsz5MJqLuRyA0rnVFGcb9jM90OAPZ/RdHQ7Twm6TSO4tCcAgFQ1rAt8kNCYUE2FzQogC esVd/dY4djBRZMhA6nqCSyuzwbI6Mfc2iktAsiu9Xf0hs+wCZUHQ0Uu5tHT3CrWbP55O JIOj67fwU+dxa88Xw9CVWXTs+U5EkK4HhejTEg8mA/Jz6y4hQ9L2rSEO2w/J92YtHMAJ 1pXO6ZKmj03GGUKb+Z5O4RdNO8mHAQ9X8v4uLtCv6b1jytxlEQo5f93exs0aImN3gu6Q 2zPg== X-Forwarded-Encrypted: i=1; AJvYcCWuusA/rzt7VuZTdRcMNG7GToVLxrJD5l3x2QdUE3z5ic9ISr825NURCB5CY39pxzu3W/r2fYzlK2yRR98=@vger.kernel.org X-Gm-Message-State: AOJu0YwVDKqdTj1bRrn2BNXvE8lLbcmo+Ytwn+cXewsbmnGKF/y6ZfG6 CYn+0cAWBdpVloo9KTKq3eRvHtbZ51ce/I1g7o9tt2vUvYOxsVnWGEDzSUUsKw== X-Gm-Gg: ASbGncvPOVcj+B1TdHPPdVKA1jmz121c/MLANL+lrYk61hO9i4KHR9estRKYRF/ZPtN BHa+D9jcfcDIgu0jVSbH+Hxm9p7Wr1uwl5fdIxcsXs+9PjrGwExkvpJOcRbwGkD3BfmzTZQXOV0 8N3GyJqeNK29EEcK+TpF3VxbLpft42a+JNYPTD/+KxXsP/LxcOSuDzUeX20kVsIdnqZ8lA8og0X Pj1Txq5S8T+1U07OFO6zZJochUPRQxaEZ9Pk75ATaZZ1K8qNjvYUX5kmiO4gdh/OoqxjIYdKK+f bpgzEss= 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> 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" 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 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. @@ -1916,13 +1924,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); @@ -1930,10 +1931,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); @@ -1949,6 +1948,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 @@ -1986,17 +1998,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.1.262.g85cc9f2d1e-goog