From nobody Sun Dec 14 21:54:17 2025 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 470FF15A868 for ; Wed, 18 Dec 2024 06:35:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734503737; cv=none; b=BSmh9OLjiSHZQtmqNVyONJxiRlnLXa/s1gcbLUOMTVZNVoJUlKSDAez5rTPoubBnIOm0hWjVwZTB6r6FNSSJ1ATH+C5zDpc3DVUGJX8nquBJ35eZvolaR01giQLspLqnasWsoehzRBznd9HQZDiw4AKBzOHvAVvgYqHF/Zx7reM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734503737; c=relaxed/simple; bh=cQK9+yohWJfVY3p/FismgfK2Y4qEB4nHwdEOOmxzU7w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jMYwjxVxzQCxYe/83NVIMpQ2csTGyjwWCzabWQWAygZa4kvIjJukZ3Rqm/L6nMWYZA4DOq+S4D8NilDCr90GoWjSohyiNT5DCIBetX5yGbIPjXjxF1uNkAmNdsMh16SdEhMB0Z8dB+aAXJFfXqHSNsVKm3w5u5u6Dk0aIzWjpyA= 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=KP0yF2si; arc=none smtp.client-ip=209.85.210.173 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="KP0yF2si" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-72739105e02so6659184b3a.0 for ; Tue, 17 Dec 2024 22:35:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1734503735; x=1735108535; 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=kcFgQDfUGNa4RpmE9ZvQmkln//OMWOItv9XoEUwGa60=; b=KP0yF2si3zZU3TctkJ9AoC1IQVzntBYcd2wp4qFi2jI8ojJPzqwgpaG8qNuvaS0gzu IlAxbxCihjnpa8s5ZBO1xQsGL9+sWpnp2Yhtbq5h3iu79+OhDf/Ttpt4sU8fspM+XN4S K/od3QpBjwlbRHo7JzffSA5Jj+r4UlQ1D3xZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734503735; x=1735108535; 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=kcFgQDfUGNa4RpmE9ZvQmkln//OMWOItv9XoEUwGa60=; b=TVHgNCqAI/g8UTgpM/Y98H+szyfb/pb8NVB1FtEpFBUJIAhtpHiv+vUw44uPNc5gu5 /XuXC0xSVoBb9BJO7OjJQkw7Q4XBd1rMI8qOiKgEOz/OIzPDayz2Em9Db7vLAgLkSzVW EYAJ6dWwCSmZ9N86qWxZi7IC+ZEGULphllvs4KEU26XHqeYyJ9oZXsh5k7n5F6wGUkZE WnoB7LbgATHRg29WuNbk9Tz8o8o7WEnCpyRDilHYQtWklP+L6s9JRda+A4t7TogrtGSm 2d1RrPcDeudzTRmgkry2kU45lCrvRXRS6DyOUsxL7bRdZ/h9eQkSOTikD5xJsmpYWqni Wm1w== X-Forwarded-Encrypted: i=1; AJvYcCUKhI5XVNy7ki8A+CPiUEAeXepxihhiqa7m7fRuAhi0PCZfgPavq9HHZ5LHwbwX2q89t37oQ07L+zCA34E=@vger.kernel.org X-Gm-Message-State: AOJu0YylBEzDWRJOPtFHYD8YS09jyobuZTZhBcFmV90tBnCA8s0hl6Po VAJVuZnv5NfS1lnH5yCu4ELmBeMleDsLZbBzmsBZsVcPZWa9cP8eBYt7kRB/ng== X-Gm-Gg: ASbGncv8ZrtkndqsBAdWsEIrDmix1ouSki14f1VBTwu9vkqnKoN/2LBTYp4kJFMbeTB kEs7V+J3bs5fQh8woyTCR2KfPua7qsKWvEDXbXy3qt9KhgK7JSsTuR3QPg6y8+v7HO6DsBMFLcw a/4uMcmDNv40GA1m3mO9HiqjpN/kYpVlECRaxEBfH1unaQsXwk90nHaYaPpWCPlausY8QeQSSSF M2lXfMYBBoifKOqs2e8XGsMKFocY/jDI/uTlXJzd6hYCLWOS22Knb44Sn0= X-Google-Smtp-Source: AGHT+IH4+dvzwEYz6RMzf3+gGHyop3pBmEqsMorp12eUh0yPbMZK6wxgVt40cdSmpIiL6/jj76oo3w== X-Received: by 2002:a05:6a20:3d88:b0:1e1:b023:6c98 with SMTP id adf61e73a8af0-1e5b482147emr2917670637.26.1734503735547; Tue, 17 Dec 2024 22:35:35 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:3bcc:36cc:b9fe:9379]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-72918b77335sm7690904b3a.118.2024.12.17.22.35.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Dec 2024 22:35:35 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton Cc: Minchan Kim , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 4/7] zram: factor out ZRAM_HUGE write Date: Wed, 18 Dec 2024 15:34:21 +0900 Message-ID: <20241218063513.297475-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241218063513.297475-1-senozhatsky@chromium.org> References: <20241218063513.297475-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" zram_write_page() handles: ZRAM_SAME pages (which was already factored out) stores, regular page stores and ZRAM_HUGE pages stores. ZRAM_HUGE handling adds a significant amount of complexity. Instead, we can handle ZRAM_HUGE in a separate function. This allows us to simplify zs_handle allocations slow-path, as it now does not handle ZRAM_HUGE case. ZRAM_HUGE zs_handle allocation, on the other hand, can now drop __GFP_KSWAPD_RECLAIM because we handle ZRAM_HUGE in preemptible context (outside of local-lock scope). Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 136 +++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 53 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 89f3aaa23329..1339776bc6c5 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -132,6 +132,27 @@ static inline bool zram_allocated(struct zram *zram, u= 32 index) zram_test_flag(zram, index, ZRAM_WB); } =20 +static inline void update_used_max(struct zram *zram, const unsigned long = pages) +{ + unsigned long cur_max =3D atomic_long_read(&zram->stats.max_used_pages); + + do { + if (cur_max >=3D pages) + return; + } while (!atomic_long_try_cmpxchg(&zram->stats.max_used_pages, + &cur_max, pages)); +} + +static bool zram_can_store_page(struct zram *zram) +{ + unsigned long alloced_pages; + + alloced_pages =3D zs_get_total_pages(zram->mem_pool); + update_used_max(zram, alloced_pages); + + return !zram->limit_pages || alloced_pages <=3D zram->limit_pages; +} + #if PAGE_SIZE !=3D 4096 static inline bool is_partial_io(struct bio_vec *bvec) { @@ -266,18 +287,6 @@ static struct zram_pp_slot *select_pp_slot(struct zram= _pp_ctl *ctl) } #endif =20 -static inline void update_used_max(struct zram *zram, - const unsigned long pages) -{ - unsigned long cur_max =3D atomic_long_read(&zram->stats.max_used_pages); - - do { - if (cur_max >=3D pages) - return; - } while (!atomic_long_try_cmpxchg(&zram->stats.max_used_pages, - &cur_max, pages)); -} - static inline void zram_fill_page(void *ptr, unsigned long len, unsigned long value) { @@ -1638,13 +1647,54 @@ static int write_same_filled_page(struct zram *zram= , unsigned long fill, return 0; } =20 +static int write_incompressible_page(struct zram *zram, struct page *page, + u32 index) +{ + unsigned long handle; + void *src, *dst; + + /* + * 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); + + if (!zram_can_store_page(zram)) { + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); + zs_free(zram->mem_pool, handle); + return -ENOMEM; + } + + dst =3D zs_map_object(zram->mem_pool, handle, ZS_MM_WO); + src =3D kmap_local_page(page); + memcpy(dst, src, PAGE_SIZE); + kunmap_local(src); + zs_unmap_object(zram->mem_pool, handle); + + zram_slot_lock(zram, index); + zram_set_flag(zram, index, ZRAM_HUGE); + zram_set_handle(zram, index, handle); + zram_set_obj_size(zram, index, PAGE_SIZE); + zram_slot_unlock(zram, index); + + atomic64_add(PAGE_SIZE, &zram->stats.compr_data_size); + atomic64_inc(&zram->stats.huge_pages); + atomic64_inc(&zram->stats.huge_pages_since); + atomic64_inc(&zram->stats.pages_stored); + + return 0; +} + static int zram_write_page(struct zram *zram, struct page *page, u32 index) { int ret =3D 0; - unsigned long alloced_pages; unsigned long handle =3D -ENOMEM; unsigned int comp_len =3D 0; - void *src, *dst, *mem; + void *dst, *mem; struct zcomp_strm *zstrm; unsigned long element =3D 0; bool same_filled; @@ -1662,10 +1712,10 @@ static int zram_write_page(struct zram *zram, struc= t page *page, u32 index) =20 compress_again: zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]); - src =3D kmap_local_page(page); + mem =3D kmap_local_page(page); ret =3D zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm, - src, &comp_len); - kunmap_local(src); + mem, &comp_len); + kunmap_local(mem); =20 if (unlikely(ret)) { zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); @@ -1674,8 +1724,11 @@ static int zram_write_page(struct zram *zram, struct= page *page, u32 index) return ret; } =20 - if (comp_len >=3D huge_class_size) - comp_len =3D PAGE_SIZE; + if (comp_len >=3D huge_class_size) { + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); + return write_incompressible_page(zram, page, index); + } + /* * handle allocation has 2 paths: * a) fast path is executed with preemption disabled (for @@ -1691,35 +1744,23 @@ static int zram_write_page(struct zram *zram, struc= t page *page, u32 index) */ if (IS_ERR_VALUE(handle)) handle =3D zs_malloc(zram->mem_pool, comp_len, - __GFP_KSWAPD_RECLAIM | - __GFP_NOWARN | - __GFP_HIGHMEM | - __GFP_MOVABLE); + __GFP_KSWAPD_RECLAIM | + __GFP_NOWARN | + __GFP_HIGHMEM | + __GFP_MOVABLE); if (IS_ERR_VALUE(handle)) { zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); atomic64_inc(&zram->stats.writestall); handle =3D zs_malloc(zram->mem_pool, comp_len, - GFP_NOIO | __GFP_HIGHMEM | - __GFP_MOVABLE); + GFP_NOIO | __GFP_HIGHMEM | + __GFP_MOVABLE); if (IS_ERR_VALUE(handle)) return PTR_ERR((void *)handle); =20 - if (comp_len !=3D PAGE_SIZE) - goto compress_again; - /* - * If the page is not compressible, you need to acquire the - * lock and execute the code below. The zcomp_stream_get() - * call is needed to disable the cpu hotplug and grab the - * zstrm buffer back. It is necessary that the dereferencing - * of the zstrm variable below occurs correctly. - */ - zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]); + goto compress_again; } =20 - alloced_pages =3D zs_get_total_pages(zram->mem_pool); - update_used_max(zram, alloced_pages); - - if (zram->limit_pages && alloced_pages > zram->limit_pages) { + if (!zram_can_store_page(zram)) { zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); zs_free(zram->mem_pool, handle); return -ENOMEM; @@ -1727,30 +1768,19 @@ static int zram_write_page(struct zram *zram, struc= t page *page, u32 index) =20 dst =3D zs_map_object(zram->mem_pool, handle, ZS_MM_WO); =20 - src =3D zstrm->buffer; - if (comp_len =3D=3D PAGE_SIZE) - src =3D kmap_local_page(page); - memcpy(dst, src, comp_len); - if (comp_len =3D=3D PAGE_SIZE) - kunmap_local(src); - + memcpy(dst, zstrm->buffer, comp_len); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); zs_unmap_object(zram->mem_pool, handle); - atomic64_add(comp_len, &zram->stats.compr_data_size); =20 zram_slot_lock(zram, index); - if (comp_len =3D=3D PAGE_SIZE) { - zram_set_flag(zram, index, ZRAM_HUGE); - atomic64_inc(&zram->stats.huge_pages); - atomic64_inc(&zram->stats.huge_pages_since); - } - zram_set_handle(zram, index, handle); zram_set_obj_size(zram, index, comp_len); zram_slot_unlock(zram, index); =20 /* Update stats */ atomic64_inc(&zram->stats.pages_stored); + atomic64_add(comp_len, &zram->stats.compr_data_size); + return ret; } =20 --=20 2.47.1.613.gc27f4b7a9f-goog