From patchwork Mon Sep 2 07:21:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexs@kernel.org X-Patchwork-Id: 13786825 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B3B0CA0ED3 for ; Mon, 2 Sep 2024 07:16:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A51048D0084; Mon, 2 Sep 2024 03:16:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FF538D002D; Mon, 2 Sep 2024 03:16:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89F8D8D0084; Mon, 2 Sep 2024 03:16:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 67E6C8D002D for ; Mon, 2 Sep 2024 03:16:39 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 07CD1121A19 for ; Mon, 2 Sep 2024 07:16:39 +0000 (UTC) X-FDA: 82518940518.13.5290E7D Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf06.hostedemail.com (Postfix) with ESMTP id 69D88180008 for ; Mon, 2 Sep 2024 07:16:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=agYKSAil; spf=pass (imf06.hostedemail.com: domain of alexs@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725261323; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dVJxbLAsEPgItqfNqHAQC63pv9j1Kl1PzimEv9vqIR4=; b=2mP5eRgIPEqS5K/4+bYk/1ue38MQ2tk02woLQwOLHqY3JPdwDMQbVg+q7JfXOMBBLODNHi ZyEs0Sos/LWWj3lXCrD4yxx7LzCVQQ2/i0CN4DnGSTPlSvJWd8t3vXmpWerFQrGOADlMan 52QmVaDZA6O9s8gAZjbfhK/bpVFegWw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=agYKSAil; spf=pass (imf06.hostedemail.com: domain of alexs@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725261323; a=rsa-sha256; cv=none; b=2N3VI3M256JdYET2D9nBnhhD3AUkmugeDdv8EORLm1crSJNUNSnp8XavGdMe5S/yIsjp7V 80yn7I0QizMUcncb81rypcU8tqjY/DVOQzayxZs8ppNP1GCp965THJGobKqxjrlZPvuEcY V1BDjqj2NTYKIuGUnkEr91VPFA1e480= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 7F772A41714; Mon, 2 Sep 2024 07:16:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BAB1C4CEC2; Mon, 2 Sep 2024 07:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725261396; bh=35oS5cqvsOoGfro8SlCHvjRjEwfOQy4xgC0vABlxtJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=agYKSAilfuYO5J7eGOLpdxzw52rI9hxDd1DKce1cIpm9Fjt0aSVLplL3wmg3sHr/G HcnCqtqK+RRS9/TCwa5CYuM7L/NqpTsTB4pDnREzgFVbLECV8qmZhLyt0sh+yUJ0TS FRx3J5kf7s+dBjFhx4zq5oHD+lnChGZzvxxnvQkR9PX/G6oLAS0039chUbgygF+8tp 8ZPsA6lo80t4DbiB6RX9oxVQLDuR6S6ekvtWBhywuT7ZpbwpxXZEVZhDdCjGsMqA2d 2YOFTQU8dknUtnJQPcYzUM2Dpjx2Gq7vW03RUznWSZB8bJNXXeiZPGqYcGs17eJXul wy6Zp76/km4Ug== From: alexs@kernel.org To: Vitaly Wool , Miaohe Lin , Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, minchan@kernel.org, willy@infradead.org, senozhatsky@chromium.org, david@redhat.com, 42.hyeyoo@gmail.com, Yosry Ahmed , nphamcs@gmail.com Cc: Alex Shi Subject: [PATCH v7 01/21] mm/zsmalloc: add zpdesc memory descriptor for zswap.zpool Date: Mon, 2 Sep 2024 15:21:12 +0800 Message-ID: <20240902072136.578720-2-alexs@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240902072136.578720-1-alexs@kernel.org> References: <20240902072136.578720-1-alexs@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ewod7e6bytcbxtk1bi3p7nupq67j5x1e X-Rspam-User: X-Rspamd-Queue-Id: 69D88180008 X-Rspamd-Server: rspam02 X-HE-Tag: 1725261397-724873 X-HE-Meta: U2FsdGVkX19dTIY7J975NA4akCnVCmiokCtow3aOGoVXYMlaof1GBFpA9BDaxOJP+gWWmwBbI0jvCoiRaR40op405kqmQFamEZbTGVo3Rwy2jZM5VmuupfsfYJW6KKJDe5wg5a5Bm+hMV7eoA1V4KBfBizpnSay5aMAIFNd0rKk09A0KsVKOzpdPgSRlDuqV8gmpYrY3ABoF9v9gq1gsiRprYB93YMxPrmoPnn7F8sBLoQ9PiYDmhglwCZAKMFgeFu9rzNRKenk1DL0zfYij8NUxb0Pw2uQmfXyWbbX2EEn3iEParT8AMBYLj+AgZN2CobUQCiowyXIhTuJYUd6YKAQ4+fziDb4TyHgdLvblNfc3iq9cRS/XTxZxSoGvXrbW3CiX1ffKGVdWvigufBPK9PS4SDQy4GQmoG0aK0pbVI29eJatrXnnXfn736bxbEfwsG5pVaalwUrkcWVmtLZBisIzo6LmFb1bMVeG9u1+81jJe6KcSpj9taKjkxaSC1pee36FSCzURIrtZjcjmlbSm33HkgDvqiM3vsEfWLuo85AJXw8Pm8MK7poW7mn/eYZW2F5/l/YysPU7TZCcBlkQxvxnC1lcPHDskdq0GcmvDRDpU48R11XZxbFEqIz4Efj8XqxgSHGw73xAa6hDkWFr53d47sZ7Qad+akINqRlVnTcwwCVVPYpyGYTRJRVjbgw2FvAjhpRbAuv01zKRoCJh0uIeTY8/cMleZA15hEMGYIIkleUAtNvcMa47RVPF6MQpbGY7yKnn+EPMvYK29mDYDFU+lDiq/rmjSrQovzrHHm8kV4/L4rIytf5Q2RKcTlQ5BlGkd/WLLvJ2fuxqLZSF+sjuWcUK664yUbWNFFldVhuI5+WGL42lSvFBpt8XKBNYny2scMQnnuh2uUaa5pLFqfyolvc9Nw98trbG505aXfZ5zvNDTjaiNVLR3NE677dp1mPA4yRxdnmz/+0KM2+ R4Qlabs1 29Etlo/InxwhtKFoZaVP00o8ad6yt013893hdLiD4F7a+2A/mc08db52ew5p6ano5wv8MCbchfCicuJrdl0qqLrNs91IuS3v/AKG1/hPxExOcq3YhE4tV/fiSH26N5wrN0Wnas5yitJGnB76sg5OKTHri+08P5sxmXG8d0lCmjJee+MzjiFyg36KCPsLlmBG8S+IR+HqvSX1kLgeXNnsOj8Qkc1CWOM7d+0vYnrLalrzOCf40NGam4QN/zTFFwaVBqWqXA+61OA1L9B6iLOqF5VJoAUzZtumZUBWU/m5g5xHUisl6WsMZjCA1ebJPXIjms1H1xjWEu43ff35uE+s7Eic26g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alex Shi The 1st patch introduces new memory descriptor zpdesc and rename zspage.first_page to zspage.first_zpdesc, no functional change. We removed PG_owner_priv_1 since it was moved to zspage after commit a41ec880aa7b ("zsmalloc: move huge compressed obj from page to zspage"). And keep the memcg_data member, since as Yosry pointed out: "When the pages are freed, put_page() -> folio_put() -> __folio_put() will call mem_cgroup_uncharge(). The latter will call folio_memcg() (which reads folio->memcg_data) to figure out if uncharging needs to be done. There are also other similar code paths that will check folio->memcg_data. It is currently expected to be present for all folios. So until we have custom code paths per-folio type for allocation/freeing/etc, we need to keep folio->memcg_data present and properly initialized." Originally-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Signed-off-by: Alex Shi --- mm/zpdesc.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/zsmalloc.c | 9 ++++--- 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 mm/zpdesc.h diff --git a/mm/zpdesc.h b/mm/zpdesc.h new file mode 100644 index 000000000000..721ef8861131 --- /dev/null +++ b/mm/zpdesc.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* zpdesc.h: zswap.zpool memory descriptor + * + * Written by Alex Shi + * Hyeonggon Yoo <42.hyeyoo@gmail.com> + */ +#ifndef __MM_ZPDESC_H__ +#define __MM_ZPDESC_H__ + +/* + * struct zpdesc - Memory descriptor for zpool memory, now is for zsmalloc + * @flags: Page flags, PG_private: identifies the first component page + * @lru: Indirectly used by page migration + * @mops: Used by page migration + * @next: Next zpdesc in a zspage in zsmalloc zpool + * @handle: For huge zspage in zsmalloc zpool + * @zspage: Points to the zspage this zpdesc is a part of + * @first_obj_offset: First object offset in zsmalloc zpool + * @_refcount: Indirectly use by page migration + * @memcg_data: Memory Control Group data. + * + * This struct overlays struct page for now. Do not modify without a good + * understanding of the issues. + */ +struct zpdesc { + unsigned long flags; + struct list_head lru; + struct movable_operations *mops; + union { + /* Next zpdescs in a zspage in zsmalloc zpool */ + struct zpdesc *next; + /* For huge zspage in zsmalloc zpool */ + unsigned long handle; + }; + struct zspage *zspage; + unsigned int first_obj_offset; + atomic_t _refcount; +#ifdef CONFIG_MEMCG + unsigned long memcg_data; +#endif +}; +#define ZPDESC_MATCH(pg, zp) \ + static_assert(offsetof(struct page, pg) == offsetof(struct zpdesc, zp)) + +ZPDESC_MATCH(flags, flags); +ZPDESC_MATCH(lru, lru); +ZPDESC_MATCH(mapping, mops); +ZPDESC_MATCH(index, next); +ZPDESC_MATCH(index, handle); +ZPDESC_MATCH(private, zspage); +ZPDESC_MATCH(page_type, first_obj_offset); +ZPDESC_MATCH(_refcount, _refcount); +#ifdef CONFIG_MEMCG +ZPDESC_MATCH(memcg_data, memcg_data); +#endif +#undef ZPDESC_MATCH +static_assert(sizeof(struct zpdesc) <= sizeof(struct page)); + +#define zpdesc_page(zp) (_Generic((zp), \ + const struct zpdesc *: (const struct page *)(zp), \ + struct zpdesc *: (struct page *)(zp))) + +/* Using folio conversion to skip compound_head checking */ +#define zpdesc_folio(zp) (_Generic((zp), \ + const struct zpdesc *: (const struct folio *)(zp), \ + struct zpdesc *: (struct folio *)(zp))) + +#define page_zpdesc(p) (_Generic((p), \ + const struct page *: (const struct zpdesc *)(p), \ + struct page *: (struct zpdesc *)(p))) + +#endif diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 73a3ec5b21ad..5d3e27083c72 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -64,6 +64,7 @@ #include #include #include +#include "zpdesc.h" #define ZSPAGE_MAGIC 0x58 @@ -251,7 +252,7 @@ struct zspage { }; unsigned int inuse; unsigned int freeobj; - struct page *first_page; + struct zpdesc *first_zpdesc; struct list_head list; /* fullness list */ struct zs_pool *pool; rwlock_t lock; @@ -446,7 +447,7 @@ static inline void mod_zspage_inuse(struct zspage *zspage, int val) static inline struct page *get_first_page(struct zspage *zspage) { - struct page *first_page = zspage->first_page; + struct page *first_page = zpdesc_page(zspage->first_zpdesc); VM_BUG_ON_PAGE(!is_first_page(first_page), first_page); return first_page; @@ -939,7 +940,7 @@ static void create_page_chain(struct size_class *class, struct zspage *zspage, set_page_private(page, (unsigned long)zspage); page->index = 0; if (i == 0) { - zspage->first_page = page; + zspage->first_zpdesc = page_zpdesc(page); SetPagePrivate(page); if (unlikely(class->objs_per_zspage == 1 && class->pages_per_zspage == 1)) @@ -1315,7 +1316,7 @@ static unsigned long obj_malloc(struct zs_pool *pool, link->handle = handle | OBJ_ALLOCATED_TAG; else /* record handle to page->index */ - zspage->first_page->index = handle | OBJ_ALLOCATED_TAG; + zspage->first_zpdesc->handle = handle | OBJ_ALLOCATED_TAG; kunmap_atomic(vaddr); mod_zspage_inuse(zspage, 1);