From patchwork Wed Jul 3 04:05:52 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: 13721072 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 E588AC30653 for ; Wed, 3 Jul 2024 04:01:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CC786B0088; Wed, 3 Jul 2024 00:01:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 656366B0089; Wed, 3 Jul 2024 00:01:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CFB96B008A; Wed, 3 Jul 2024 00:01:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 27A126B0088 for ; Wed, 3 Jul 2024 00:01:22 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 898E7120725 for ; Wed, 3 Jul 2024 04:01:21 +0000 (UTC) X-FDA: 82297091562.12.9835862 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf02.hostedemail.com (Postfix) with ESMTP id 51D0E8001C for ; Wed, 3 Jul 2024 04:01:18 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=M3fvr9YU; spf=pass (imf02.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719979257; 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=55UKQZVXLllTbhZrfzXYatMV5+KqGDMpDwKffSYOCMg=; b=bh7m5xHvXiKSfAU2jMKshnkbvwS7ZKJd2KNUxUkth9+fs89X4z5LyVSYAmE0KGp7b8w88R FR3jP8NUC4hFNcDbt80r6jKp2bvIhyziDGtz3p+sY31gVtXtdyCMJVRRV09DtaXQJUwZMV oqYhUMPgiqo03UkCAiLu1ypp1YzqVV8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719979257; a=rsa-sha256; cv=none; b=QE3UnUKmamTN6HzoapWHoB4ZGdgpHTXn19mZU1opPa+++6EzRZ2DXVsBZNiRkT+CPFQpkg +ty/XnVfrs93r5IL8pC8R73ew7n5sfL/hVXqa/zmNvjrHHHgLi4xkqT7nzCzzR/l47UbKA Sj4jZ9UnM5P81EodXkcBhZuUYeIzAn4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=M3fvr9YU; spf=pass (imf02.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7B865CE28A0; Wed, 3 Jul 2024 04:01:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81E71C4AF0C; Wed, 3 Jul 2024 04:01:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719979275; bh=mgpKO63FhRCf6bCfWvth1dWCysiouyCKf660VxcU2X8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M3fvr9YU3RoqDYqi7LepVhhyobK2kETlXMib4NHfNfY6Xg3gkhcZVMOUv3/ph7BuW FYbmXaLXttnr8mKSiGOtqWQCsfx4B9jfmUcCX3oooPG6ckNgLhLvFFhGujDl7UYG/m paouB5VQgKocBJwp7NyPedhmvhxN6Tz9jY3PwV7x56sVrli8XW0YcF4tZh7Tnh1SFl 6NVukRxT9usEljzgLjDhoVPOfEYQIRDXJesuIfvKKRdbh+vB59OBL8c4p2Du1xGNhv XAMnJJPn1Ygq4VTLpVP9TL5nRFRoczdwlPPIdyHOOy1gZx+qIsuZ3u4IVD4BP5/3gv OXB0uZFAiuBsg== 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 v2 02/20] mm/zsmalloc: use zpdesc in trylock_zspage/lock_zspage Date: Wed, 3 Jul 2024 12:05:52 +0800 Message-ID: <20240703040613.681396-3-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240703040613.681396-1-alexs@kernel.org> References: <20240703040613.681396-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 51D0E8001C X-Stat-Signature: 8ixfsnrggbabtcxbx7qdjuoft77k5pqf X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1719979278-991786 X-HE-Meta: U2FsdGVkX1+/vYPbwwS5t2D0ozYOeJ3De05Ht2TjMCGAgjymW00qBVZDA9F2R2dam1RCC9tWl/UkpXxYli0o8qfMCX31rVl+y5ExwyqkGT7Ha5f61gR2KwRAuIqSLPEsVDWnPJorV64ZrnJ2LVRlPGnCHxE6JwWWqsfPQ6wzjO6pB27EOXL9v17JVi3BBfi8eRGCW84ar4daX0rCRD6VTfGCpOJReHgknL+rcm+SdgNWUCc8UmERBEm+lkEJssSQagwxYoaBu0+CXICVCOoov/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vWoA== 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 To use zpdesc in trylock_zspage/lock_zspage funcs, we add couple of helpers: zpdesc_lock/zpdesc_unlock/zpdesc_trylock/zpdesc_wait_locked and zpdesc_get/zpdesc_put for this purpose. Here we use the folio series func in guts for 2 reasons, one zswap.zpool only get single page, and use folio could save some compound_head checking; two, folio_put could bypass devmap checking that we don't need. Originally-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Signed-off-by: Alex Shi --- mm/zpdesc.h | 30 ++++++++++++++++++++++++ mm/zsmalloc.c | 64 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 73 insertions(+), 21 deletions(-) diff --git a/mm/zpdesc.h b/mm/zpdesc.h index 4455a663ee84..8a359d228507 100644 --- a/mm/zpdesc.h +++ b/mm/zpdesc.h @@ -61,4 +61,34 @@ static_assert(sizeof(struct zpdesc) <= sizeof(struct page)); const struct page *: (const struct zpdesc *)(p), \ struct page *: (struct zpdesc *)(p))) +static inline void zpdesc_lock(struct zpdesc *zpdesc) +{ + folio_lock(zpdesc_folio(zpdesc)); +} + +static inline bool zpdesc_trylock(struct zpdesc *zpdesc) +{ + return folio_trylock(zpdesc_folio(zpdesc)); +} + +static inline void zpdesc_unlock(struct zpdesc *zpdesc) +{ + folio_unlock(zpdesc_folio(zpdesc)); +} + +static inline void zpdesc_wait_locked(struct zpdesc *zpdesc) +{ + folio_wait_locked(zpdesc_folio(zpdesc)); +} + +static inline void zpdesc_get(struct zpdesc *zpdesc) +{ + folio_get(zpdesc_folio(zpdesc)); +} + +static inline void zpdesc_put(struct zpdesc *zpdesc) +{ + folio_put(zpdesc_folio(zpdesc)); +} + #endif diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index a532851025f9..243677a9c6d2 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -433,13 +433,17 @@ static __maybe_unused int is_first_page(struct page *page) return PagePrivate(page); } +static int is_first_zpdesc(struct zpdesc *zpdesc) +{ + return PagePrivate(zpdesc_page(zpdesc)); +} + /* Protected by class->lock */ static inline int get_zspage_inuse(struct zspage *zspage) { return zspage->inuse; } - static inline void mod_zspage_inuse(struct zspage *zspage, int val) { zspage->inuse += val; @@ -453,6 +457,14 @@ static inline struct page *get_first_page(struct zspage *zspage) return first_page; } +static struct zpdesc *get_first_zpdesc(struct zspage *zspage) +{ + struct zpdesc *first_zpdesc = zspage->first_zpdesc; + + VM_BUG_ON_PAGE(!is_first_zpdesc(first_zpdesc), zpdesc_page(first_zpdesc)); + return first_zpdesc; +} + #define FIRST_OBJ_PAGE_TYPE_MASK 0xffff static inline void reset_first_obj_offset(struct page *page) @@ -745,6 +757,16 @@ static struct page *get_next_page(struct page *page) return (struct page *)page->index; } +static struct zpdesc *get_next_zpdesc(struct zpdesc *zpdesc) +{ + struct zspage *zspage = get_zspage(zpdesc_page(zpdesc)); + + if (unlikely(ZsHugePage(zspage))) + return NULL; + + return zpdesc->next; +} + /** * obj_to_location - get (, ) from encoded object value * @obj: the encoded object value @@ -815,11 +837,11 @@ static void reset_page(struct page *page) static int trylock_zspage(struct zspage *zspage) { - struct page *cursor, *fail; + struct zpdesc *cursor, *fail; - for (cursor = get_first_page(zspage); cursor != NULL; cursor = - get_next_page(cursor)) { - if (!trylock_page(cursor)) { + for (cursor = get_first_zpdesc(zspage); cursor != NULL; cursor = + get_next_zpdesc(cursor)) { + if (!zpdesc_trylock(cursor)) { fail = cursor; goto unlock; } @@ -827,9 +849,9 @@ static int trylock_zspage(struct zspage *zspage) return 1; unlock: - for (cursor = get_first_page(zspage); cursor != fail; cursor = - get_next_page(cursor)) - unlock_page(cursor); + for (cursor = get_first_zpdesc(zspage); cursor != fail; cursor = + get_next_zpdesc(cursor)) + zpdesc_unlock(cursor); return 0; } @@ -1658,7 +1680,7 @@ static int putback_zspage(struct size_class *class, struct zspage *zspage) */ static void lock_zspage(struct zspage *zspage) { - struct page *curr_page, *page; + struct zpdesc *curr_zpdesc, *zpdesc; /* * Pages we haven't locked yet can be migrated off the list while we're @@ -1670,24 +1692,24 @@ static void lock_zspage(struct zspage *zspage) */ while (1) { migrate_read_lock(zspage); - page = get_first_page(zspage); - if (trylock_page(page)) + zpdesc = get_first_zpdesc(zspage); + if (zpdesc_trylock(zpdesc)) break; - get_page(page); + zpdesc_get(zpdesc); migrate_read_unlock(zspage); - wait_on_page_locked(page); - put_page(page); + zpdesc_wait_locked(zpdesc); + zpdesc_put(zpdesc); } - curr_page = page; - while ((page = get_next_page(curr_page))) { - if (trylock_page(page)) { - curr_page = page; + curr_zpdesc = zpdesc; + while ((zpdesc = get_next_zpdesc(curr_zpdesc))) { + if (zpdesc_trylock(zpdesc)) { + curr_zpdesc = zpdesc; } else { - get_page(page); + zpdesc_get(zpdesc); migrate_read_unlock(zspage); - wait_on_page_locked(page); - put_page(page); + zpdesc_wait_locked(zpdesc); + zpdesc_put(zpdesc); migrate_read_lock(zspage); } }