From nobody Fri Jan 31 00:06:29 2025 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 DE2FE1FCD0F for ; Mon, 27 Jan 2025 08:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737965020; cv=none; b=iihfzcBqKFUOGPm1Qhh/DB0Rwamr5Ui4qaFdDvFUnlR3v33CRgAVdQGfZac1fT74QWxXoOD/mnmoY/n8x4PTTlxjJ0ehwomTJinTtzAvPYqSqnsTiAKJYXMzUAn9qLz5yjYfqV+V1i4s1d1saadsYReUopbGngBKF+AzxFeGG8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737965020; c=relaxed/simple; bh=J1Dr2ObHXa85bIpSWFK2jLpTfm0PI7vlevCUfEJwayA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BxetSeOqexXoHoc/wsq9E0gCw754Rzga1oGHmoy5As8Vygr8JnNzsQSC3yeiks1N0A3BHmv2RifKaNwZwesDI3lyk/Ojp4lX23smZt+QUy5ZT4o4EmHAhiffel+63xzm1SMVndXOfFiLlron26D6+oJvGisG7BuOqEdYVmf6rCo= 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=XuHARh6C; arc=none smtp.client-ip=209.85.214.182 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="XuHARh6C" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2164b662090so77923505ad.1 for ; Mon, 27 Jan 2025 00:03:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737965018; x=1738569818; 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=651sDMCEJHysCKLNgE9iOdg6zJ4Jwg3Nj3A7ATDjnVc=; b=XuHARh6C66rnxRgIUYlJhYNsFcAcQtsOGakeoz1MX+I2ArM2LvWQLRKNw474b29TDp j0eALp3yncrEwYR1IoI10PdgrPT1ARzTUCL7x1tNhXVB37ubgqVfpHnNTX7hAPNOh10q OUgBoZgadvBX4q0lLr9k1PEBHg9sQws62N67w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737965018; x=1738569818; 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=651sDMCEJHysCKLNgE9iOdg6zJ4Jwg3Nj3A7ATDjnVc=; b=txSjGWf+0U7bDkUH06Z53ybWvJqZpitUfzEPEn4UdIT5mWVAg8NEjDuuApS+iBv0+k JyVJFBJKnrgRlZaPrvBrfKlgAoqlM+ZDEl0P9rCM3d/gte0KidCWe1WsNT7JIfOumYls roiDOawX4URzt7712R46pr6TEmA4nNKcy1vMJ78fDQsD+KYCP+ePAcICfvO7uGSgW8uo jCNHHrKgcXq3nZnILEiYWbd6WfLaTgoyFENNonsLl9mLfH9CpS12lAi2i51xxfH9TpOZ B61CkT8zKLDCjaZR7LEthRNb33rA9Pyc34sdiWRwwYaKbi0NM36fD+9TKg+qQENBUDrO CeWQ== X-Forwarded-Encrypted: i=1; AJvYcCXEyNT6KtlYkcv/j0CVzWv3nmt2LpSjgQJVgolbWFIG65X39BOuA+FpzU8+vsrBLQubXKiLevVHQftTqlo=@vger.kernel.org X-Gm-Message-State: AOJu0YyigjF+JA/NEBoeiVYKHP8Xp0sQyozp6LEqjMhtG+KBuGsdwauq 4nY6ghyhyiqVuhPdcBYZ4w6uW6zPn7e7Sgnkosfw0M7i8l7GVlyNuXO0N8xSvQ== X-Gm-Gg: ASbGnctAcZXjF+4MeitX+TXDRNZT67ReoXOx8EtDujwo7+Wi73OyjGYWL0SZ1lbeKzz g8dlvdtBKTlymKW/7kO6C7LzBEJObDV9xTaYTSxN4k5OIQ+US+boxLHy1dgqD1YnNAWKRXUsSCC 5jhQS+O3ZNHkKMaPOZg2qSm31Q7wG4Pf9VdLNkmLbD7JAmWZeuBTc3tzDucg9T6sDa8srqg6g/S VacqbZBlcBCIkwZRxTwQJkHmzmmTHrfy4GB0Sb+gPB4GoRABlynXXcK3RMFVYhrEi5Zpsn4Unto H+9kCcA= X-Google-Smtp-Source: AGHT+IF4OL0p2X71WwYse+YFBRvC81hL1zRDbOr33Q/MhbIwiQRVy63K3CZ31U6CYc/ujJCK075RbA== X-Received: by 2002:a17:902:db0a:b0:216:7761:cc49 with SMTP id d9443c01a7336-21c355f70c7mr542469735ad.47.1737965018362; Mon, 27 Jan 2025 00:03:38 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:566d:6152:c049:8d3a]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-ac496bbce61sm5749314a12.68.2025.01.27.00.03.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Jan 2025 00:03:38 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim , Johannes Weiner , Yosry Ahmed , Nhat Pham Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [RFC PATCH 6/6] zram: switch over to zshandle mapping API Date: Mon, 27 Jan 2025 16:59:31 +0900 Message-ID: <20250127080254.1302026-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250127080254.1302026-1-senozhatsky@chromium.org> References: <20250127080254.1302026-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" Use new zsmalloc handle mapping API so now zram read() becomes preemptible. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zcomp.c | 4 +- drivers/block/zram/zcomp.h | 2 + drivers/block/zram/zram_drv.c | 103 ++++++++++++++++++---------------- 3 files changed, 61 insertions(+), 48 deletions(-) diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index efd5919808d9..9b373ab1ee0b 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -45,6 +45,7 @@ static const struct zcomp_ops *backends[] =3D { static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *strm) { comp->ops->destroy_ctx(&strm->ctx); + vfree(strm->handle_mem_copy); vfree(strm->buffer); kfree(strm); } @@ -66,12 +67,13 @@ static struct zcomp_strm *zcomp_strm_alloc(struct zcomp= *comp) return NULL; } =20 + strm->handle_mem_copy =3D vzalloc(PAGE_SIZE); /* * allocate 2 pages. 1 for compressed data, plus 1 extra in case if * compressed data is larger than the original one. */ strm->buffer =3D vzalloc(2 * PAGE_SIZE); - if (!strm->buffer) { + if (!strm->buffer || !strm->handle_mem_copy) { zcomp_strm_free(comp, strm); return NULL; } diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 62330829db3f..f003f09820a5 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -34,6 +34,8 @@ struct zcomp_strm { struct list_head entry; /* compression buffer */ void *buffer; + /* handle object memory copy */ + void *handle_mem_copy; struct zcomp_ctx ctx; }; =20 diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 9c72beb86ab0..120055b11520 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1558,37 +1558,43 @@ static int read_same_filled_page(struct zram *zram,= struct page *page, static int read_incompressible_page(struct zram *zram, struct page *page, u32 index) { - unsigned long handle; - void *src, *dst; + struct zs_handle_mapping hm; + void *dst; =20 - handle =3D zram_get_handle(zram, index); - src =3D zs_map_object(zram->mem_pool, handle, ZS_MM_RO); + hm.handle =3D zram_get_handle(zram, index); + hm.mode =3D ZS_MM_RO; + + zs_map_handle(zram->mem_pool, &hm); dst =3D kmap_local_page(page); - copy_page(dst, src); + copy_page(dst, hm.handle_mem); kunmap_local(dst); - zs_unmap_object(zram->mem_pool, handle); + zs_unmap_handle(zram->mem_pool, &hm); =20 return 0; } =20 static int read_compressed_page(struct zram *zram, struct page *page, u32 = index) { + struct zs_handle_mapping hm; struct zcomp_strm *zstrm; - unsigned long handle; unsigned int size; - void *src, *dst; + void *dst; int ret, prio; =20 - handle =3D zram_get_handle(zram, index); size =3D zram_get_obj_size(zram, index); prio =3D zram_get_priority(zram, index); =20 zstrm =3D zcomp_stream_get(zram->comps[prio]); - src =3D zs_map_object(zram->mem_pool, handle, ZS_MM_RO); + hm.handle =3D zram_get_handle(zram, index); + hm.mode =3D ZS_MM_RO; + hm.local_copy =3D zstrm->handle_mem_copy; + + zs_map_handle(zram->mem_pool, &hm); dst =3D kmap_local_page(page); - ret =3D zcomp_decompress(zram->comps[prio], zstrm, src, size, dst); + ret =3D zcomp_decompress(zram->comps[prio], zstrm, + hm.handle_mem, size, dst); kunmap_local(dst); - zs_unmap_object(zram->mem_pool, handle); + zs_unmap_handle(zram->mem_pool, &hm); zcomp_stream_put(zram->comps[prio], zstrm); =20 return ret; @@ -1683,33 +1689,34 @@ static int write_same_filled_page(struct zram *zram= , unsigned long fill, static int write_incompressible_page(struct zram *zram, struct page *page, u32 index) { - unsigned long handle; - void *src, *dst; + struct zs_handle_mapping hm; + void *src; =20 /* * This function is called from preemptible context so we don't need * to do optimistic and fallback to pessimistic handle allocation, * like we do for compressible pages. */ - handle =3D zs_malloc(zram->mem_pool, PAGE_SIZE, - GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); - if (IS_ERR_VALUE(handle)) - return PTR_ERR((void *)handle); + hm.handle =3D zs_malloc(zram->mem_pool, PAGE_SIZE, + GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); + if (IS_ERR_VALUE(hm.handle)) + return PTR_ERR((void *)hm.handle); =20 if (!zram_can_store_page(zram)) { - zs_free(zram->mem_pool, handle); + zs_free(zram->mem_pool, hm.handle); return -ENOMEM; } =20 - dst =3D zs_map_object(zram->mem_pool, handle, ZS_MM_WO); + hm.mode =3D ZS_MM_WO; + zs_map_handle(zram->mem_pool, &hm); src =3D kmap_local_page(page); - memcpy(dst, src, PAGE_SIZE); + memcpy(hm.handle_mem, src, PAGE_SIZE); kunmap_local(src); - zs_unmap_object(zram->mem_pool, handle); + zs_unmap_handle(zram->mem_pool, &hm); =20 zram_slot_write_lock(zram, index); zram_set_flag(zram, index, ZRAM_HUGE); - zram_set_handle(zram, index, handle); + zram_set_handle(zram, index, hm.handle); zram_set_obj_size(zram, index, PAGE_SIZE); zram_slot_write_unlock(zram, index); =20 @@ -1724,9 +1731,9 @@ static int write_incompressible_page(struct zram *zra= m, struct page *page, static int zram_write_page(struct zram *zram, struct page *page, u32 index) { int ret =3D 0; - unsigned long handle; + struct zs_handle_mapping hm; unsigned int comp_len; - void *dst, *mem; + void *mem; struct zcomp_strm *zstrm; unsigned long element; bool same_filled; @@ -1758,25 +1765,26 @@ static int zram_write_page(struct zram *zram, struc= t page *page, u32 index) return write_incompressible_page(zram, page, index); } =20 - handle =3D zs_malloc(zram->mem_pool, comp_len, - GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); - if (IS_ERR_VALUE(handle)) - return PTR_ERR((void *)handle); + hm.handle =3D zs_malloc(zram->mem_pool, comp_len, + GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); + if (IS_ERR_VALUE(hm.handle)) + return PTR_ERR((void *)hm.handle); =20 if (!zram_can_store_page(zram)) { zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP], zstrm); - zs_free(zram->mem_pool, handle); + zs_free(zram->mem_pool, hm.handle); return -ENOMEM; } =20 - dst =3D zs_map_object(zram->mem_pool, handle, ZS_MM_WO); - - memcpy(dst, zstrm->buffer, comp_len); + hm.mode =3D ZS_MM_WO; + hm.local_copy =3D zstrm->handle_mem_copy; + zs_map_handle(zram->mem_pool, &hm); + memcpy(hm.handle_mem, zstrm->buffer, comp_len); + zs_unmap_handle(zram->mem_pool, &hm); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP], zstrm); - zs_unmap_object(zram->mem_pool, handle); =20 zram_slot_write_lock(zram, index); - zram_set_handle(zram, index, handle); + zram_set_handle(zram, index, hm.handle); zram_set_obj_size(zram, index, comp_len); zram_slot_write_unlock(zram, index); =20 @@ -1875,14 +1883,14 @@ static int recompress_slot(struct zram *zram, u32 i= ndex, struct page *page, u32 prio_max) { struct zcomp_strm *zstrm =3D NULL; + struct zs_handle_mapping hm; 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 =3D 0; - void *src, *dst; + void *src; int ret; =20 handle_old =3D zram_get_handle(zram, index); @@ -2000,34 +2008,35 @@ static int recompress_slot(struct zram *zram, u32 i= ndex, struct page *page, =20 /* 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); + hm.handle =3D zs_malloc(zram->mem_pool, comp_len_new, + GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); zram_slot_write_lock(zram, index); =20 /* * 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)) { + if (IS_ERR_VALUE(hm.handle)) { zcomp_stream_put(zram->comps[prio], zstrm); - return PTR_ERR((void *)handle_new); + return PTR_ERR((void *)hm.handle); } =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); + zs_free(zram->mem_pool, hm.handle); return 0; } =20 - dst =3D zs_map_object(zram->mem_pool, handle_new, ZS_MM_WO); - memcpy(dst, zstrm->buffer, comp_len_new); + hm.mode =3D ZS_MM_WO; + hm.local_copy =3D zstrm->handle_mem_copy; + zs_map_handle(zram->mem_pool, &hm); + memcpy(hm.handle_mem, zstrm->buffer, comp_len_new); + zs_unmap_handle(zram->mem_pool, &hm); zcomp_stream_put(zram->comps[prio], zstrm); =20 - zs_unmap_object(zram->mem_pool, handle_new); - zram_free_page(zram, index); - zram_set_handle(zram, index, handle_new); + zram_set_handle(zram, index, hm.handle); zram_set_obj_size(zram, index, comp_len_new); zram_set_priority(zram, index, prio); =20 --=20 2.48.1.262.g85cc9f2d1e-goog