From patchwork Tue Aug 13 08:45:48 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: 13761552 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 D4DC5C531DD for ; Tue, 13 Aug 2024 08:41:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2BA46B009E; Tue, 13 Aug 2024 04:41:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8D826B009F; Tue, 13 Aug 2024 04:41:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C94CF6B00A0; Tue, 13 Aug 2024 04:41:21 -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 9753D6B009E for ; Tue, 13 Aug 2024 04:41:21 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 463E5C06D5 for ; Tue, 13 Aug 2024 08:41:21 +0000 (UTC) X-FDA: 82446577962.30.388EFAC Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id DA4644001B for ; Tue, 13 Aug 2024 08:41:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="JjhJrf/w"; spf=pass (imf01.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=1723538467; 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=YWv08D5bwyusSokvG2XzW2e1J+umZPeFR1do5jiHGv8=; b=2TV/hhYV76t/iC9545yFO2r0GxrvZxEbHn8M/XItSdlN5RqYT13GS9itQdtN+nnMN2niku MW91e0jIBwl+84uHN/IwgT2TzzB3eE9hlO6fAhkcP1x1xDYGmxedkrKekeyv8r5RwRH8jo WViZOF/SqNKAQ97wSR478KHHcKbFOFA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="JjhJrf/w"; spf=pass (imf01.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723538467; a=rsa-sha256; cv=none; b=0DLFHm1l/3wMTugf6lVLXoLfX3YdaB+WNSCicLjG6+rAqjzVB2CuX6UWtW9w2ZVksczvls BfDSYpznGoGFnKefJu/vu6rPGL2MpO3i3QKDc4a47ipbBeO9QzD3sePZ5OGomOqpXYoRR5 eR5ytv2jok9LeFzAYnc/8S/6kT/Akmo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 11BC0CE122F; Tue, 13 Aug 2024 08:41:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2888C4AF10; Tue, 13 Aug 2024 08:41:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723538474; bh=PQtx1dFIRykqdoAci1ap2Dlk7DGGX895wpZmxxTa/no=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JjhJrf/wtkBI9UEBsUcawJtJs8gmNmxHrh1Mo480fI0G6gJrj94bzUPkFtEItVqaS vvk0HHpWOyA7tNNTEQi1OU+L8oYA8et3hJzE0UaFiy78T/w0wveVetYoCybv1xZK62 NzuSeIFYgYQ6JtJxEw3MRxT41R0yHnWLIUbY2OTTnF8YYcp2OAEnS6xAlUbToI8mEI ot2el35HS7YnXfjX0Jak615XPEbamwZncUhoSUIXXPWDvOhKVojS7FDYeyhsjH78nq WuhR1Ueh4Scg40xQw0c26/I0EtwDGn1eQe2qGoIibIksi2gBQ+R+qISZyjaFJIxj1N bcX3wurSiw0Pg== 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 v6 02/21] mm/zsmalloc: use zpdesc in trylock_zspage()/lock_zspage() Date: Tue, 13 Aug 2024 16:45:48 +0800 Message-ID: <20240813084611.4122571-3-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240813084611.4122571-1-alexs@kernel.org> References: <20240813084611.4122571-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: a16y5whrfx9tnm4nh7bg8tgaomax9k3x X-Rspamd-Queue-Id: DA4644001B X-Rspamd-Server: rspam11 X-HE-Tag: 1723538478-830316 X-HE-Meta: U2FsdGVkX18xkZlWwgTxwn7w/sMnYou58dpYqiz3EEbGBaqX71LYzX5tLLkaDNPLMtnjPTAlqlp+GT5wxl1z7MxJrQPGN3bKDUR2DdADQCsdSUr9ik7zPJBdPTtH+gnqmnYNImz7X4M14fzKCD2R21MwY+kq8gnw4+45dWHIcasEQwGVyYfWExHjSRfL3WsWcMytlbXPQWgHv51TIg1sm0G8D8KsXn/hYjYkE40xqirT42HlK9E9mYIKOlNIneB2iSpWswAkfkrKysYsdfYDvP9EAcXm6MaYX7dQ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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. BTW, thanks Intel LKP found a build warning on the patch. 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 721ef8861131..782b5ad67cda 100644 --- a/mm/zpdesc.h +++ b/mm/zpdesc.h @@ -69,4 +69,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 30f0a7abbda3..25c90224f21f 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 inline bool 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); } }