From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3ACE5409298 for ; Wed, 5 Aug 2026 10:07:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785924469; cv=none; b=RfvKuG/MaqGwEuXt85cCMrNyfR7vk6turgtvqWDpo4N3+EXsR1Nasm07OA0lV2ZxQh0GAFNnFDneI6Czkprae7hG27l4uoFWXBWYVN7uDZLMX/5wG7jh7d7ZfOPojR215wl6cKQdVhoxYgvwGUnamzcDN85qVQsjacgRVEUw/yM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785924469; c=relaxed/simple; bh=LbrFIxF6FYdXr2CjRPqI55f2sMcKnp+Bu9OP9nCMZt0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=evQ9HMRbmdQ0jBM5Bq2fM8bKXzL6bUA1cWedVSvpA+gK4B5sgZUlplzf42ZrCbyT8Qk/Vljjc53ZL5lUtaSzyQ3/AymqJzA8mWpRGvgKO6cFvykQHUNvX6xs2P3PkbgqGAHmdOpzKq+e5v0vs7O6GCyWGVvt150laLyo4eOBUdw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fUJU4mD7; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fUJU4mD7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCFB21F000E9; Wed, 5 Aug 2026 10:07:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785924466; bh=OpH6sf3viGP25/XL26BruqgaIkBu5ms7EKOCOM53Zpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fUJU4mD7++zKSDGatk7iEqLYxdXukaLXAPfjZMSqC3EDLxbHjL6fOn7KHWUu0/6O0 a5JgRjL2pxH68NptmLbnQgkzBnyW9OPJD9cHmXRUVbi4iVHDRJKd5TgPTuLgf+aBxN DQzxJ+6wYNPmOf5xDpBBAYOdz7qEPGeEW2DFv8IShsI3S817lVGYMAMCsQs+EEyvRA 4w3d9djLloGwuWXm45G6lv5KQqKpi8fygivQVwa8cJlG3yZuOQERmWGbfsst+X97vw 8IqCpw99smpx7MkNGOgPuCMqpyfUbNdhp1ogNOVgXi7MGuF6kzyjbiC13nfPcyP3cA 0XSKVs7p6RtrA== From: "Barry Song (Xiaomi)" To: baohua@kernel.org, senozhatsky@chromium.org Cc: akpm@linux-foundation.org, bigeasy@linutronix.de, hdanton@sina.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, minchan@kernel.org, ryncsn@gmail.com, suleiman@google.com, surenb@google.com, yosry.ahmed@linux.dev, zhangdongdong5@xiaomi.com Subject: Re: [RFC PATCH] zram: avoid preemption with CPU-based compression backends Date: Wed, 5 Aug 2026 18:07:40 +0800 Message-Id: <20260805100740.71994-1-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Wed, Aug 5, 2026 at 3:50 PM Barry Song wrote: > > On Wed, Aug 5, 2026 at 1:21 PM Sergey Senozhatsky > wrote: > > > > Hi Barry, > > > > On (26/08/05 17:09), Barry Song wrote: > > > > > This report shows that the zram mutex has become the top lock > > > > > contributing to UI frame drops, even surpassing mmap_lock, which we > > > > > are also addressing in multiple threads. :-) > > > > > > > > Any chance you can share more details?  Are there perhaps RT tasks > > > > in the mix, priority inversion, starvations and so on?  Can proxy > > > > execution address any of those (if it has relevance to the report > > > > you are looking at)? > > > > > > Hi Sergey, > > > > > > talked with our engineers reporting the issue. i believe it is all > > > about priority inversion. > > > proxy execution wont resolve it as we have a sleepable zs-malloc > > > within the mutex. > > >  i believe i need v2 to release the mutex before doing the 2nd stage > > > zs_malloc with > > > direct reclaim. > > > > Well, we cannot just drop the stream mutex and do sleepable zsmalloc > > allocation, because this will invalidate compression buffer.  So we > > then will need to do re-compression.  Something that I was really > > happy to drop [1]. > > We used to do that by an temp GFP_ATOMIC buffer and memcpy: > https://lore.kernel.org/all/1611035683-12732-2-git-send-email-tiantao6@hisilicon.com/ > > As long as we copy `zstrm->buffer` to a temporary buffer, we are > free to go anywhere afterwards. > Hi Sergey, Just as a proof of concept, I changed one path and it seems to work. We release the mutex before calling zs_malloc(), which may enter direct reclaim, and we no longer need the mutex afterwards. also, we can avoid re-compression: >From d1a4cbe63fc2f7336c23bc268b1dffe15b0e7444 Mon Sep 17 00:00:00 2001 From: "Barry Song (Xiaomi)" Date: Wed, 5 Aug 2026 17:53:49 +0800 Subject: [PATCH] zram: avoid doing zs_malloc() with direct reclaim within mutex Signed-off-by: Barry Song (Xiaomi) --- drivers/block/zram/zram_drv.c | 43 ++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index cfa98846ac48..e00d896a101f 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -2218,6 +2218,35 @@ static int write_same_filled_page(struct zram *zram, unsigned long fill, return 0; } +/* + * try non-sleepable allocation for !async backend, then try + * sleepable allocation; for async backend, we always begin + * from sleepable allocation + */ +static unsigned long zram_zs_malloc(struct zram *zram, struct zcomp_strm *zstrm, + size_t comp_len, const int nid, void **bounce) +{ + unsigned long handle; + + handle = zs_malloc(zram->mem_pool, comp_len, + __GFP_KSWAPD_RECLAIM | __GFP_NOWARN | + __GFP_HIGHMEM | __GFP_MOVABLE, nid); + if (!IS_ERR_VALUE(handle)) + return handle; + + *bounce = kmalloc(comp_len, GFP_ATOMIC); + if (!*bounce) + return (unsigned long)ERR_PTR(-ENOMEM); + memcpy(*bounce, zstrm->buffer, comp_len); + + /* Don't hold mutex to do a sleepable allocation */ + zcomp_stream_put(zstrm); + handle = zs_malloc(zram->mem_pool, comp_len, + GFP_NOIO | __GFP_NOWARN | + __GFP_HIGHMEM | __GFP_MOVABLE, nid); + return handle; +} + static int write_incompressible_page(struct zram *zram, struct page *page, u32 index) { @@ -2264,7 +2293,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) int ret = 0; unsigned long handle; unsigned int comp_len; - void *mem; + void *mem, *bounce = NULL; struct zcomp_strm *zstrm; unsigned long element; bool same_filled; @@ -2292,22 +2321,20 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) return write_incompressible_page(zram, page, index); } - handle = zs_malloc(zram->mem_pool, comp_len, - GFP_NOIO | __GFP_NOWARN | - __GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page)); + handle = zram_zs_malloc(zram, zstrm, comp_len, page_to_nid(page), &bounce); if (IS_ERR_VALUE(handle)) { - zcomp_stream_put(zstrm); + bounce ? kfree(bounce) : zcomp_stream_put(zstrm); return PTR_ERR((void *)handle); } if (!zram_can_store_page(zram)) { - zcomp_stream_put(zstrm); + bounce ? kfree(bounce) : zcomp_stream_put(zstrm); zs_free(zram->mem_pool, handle); return -ENOMEM; } - zs_obj_write(zram->mem_pool, handle, zstrm->buffer, comp_len); - zcomp_stream_put(zstrm); + zs_obj_write(zram->mem_pool, handle, bounce ? : zstrm->buffer, comp_len); + bounce ? kfree(bounce) : zcomp_stream_put(zstrm); slot_lock(zram, index); slot_free(zram, index); -- 2.39.3 (Apple Git-146)