From patchwork Fri Jun 28 03:11:17 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: 13715387 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 9B941C2BD09 for ; Fri, 28 Jun 2024 03:07:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1116D6B0099; Thu, 27 Jun 2024 23:07:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C0046B009A; Thu, 27 Jun 2024 23:07:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA26B6B009B; Thu, 27 Jun 2024 23:06:59 -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 CAD7A6B0099 for ; Thu, 27 Jun 2024 23:06:59 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8191B1C27D0 for ; Fri, 28 Jun 2024 03:06:59 +0000 (UTC) X-FDA: 82278810558.14.921E5B5 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id C4BD91C0008 for ; Fri, 28 Jun 2024 03:06:57 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uxYPxwOw; spf=pass (imf21.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 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=1719544000; 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=hLk/LA9ZIbUceyqGEON0eZxJ2K6QMdqjnRcs/r3mo0M=; b=Hnz4uVSXQKG6M3Zme8heSEHG8WcQw2aDeGc265WzMvJAAnJD6E9t1P7SrTeewJ7qOYuGWJ fac+93eD570GV2F4bAkDoJ4rtIJZ+THMj7PtGjkhcjzVQkMBkfmMfpl8+mOaSWWwpd4e+6 UWsS2TTguQQ1j72S5rN1sM364Ds9LEY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719544000; a=rsa-sha256; cv=none; b=WrLtiLOXTDnRz6zrns5eGevf3KC4CWzT2oREQnQ8ktaRGGunoYMcNUChgaVIu9KyOCpTgS ljlX103wyiCoW0NprhjkhpcateO+1+q6deQF1n1KQXa1MnUJPRwv3gf7tl9xZSGclHXJdC afd8EsZTcxN/yw0LnxbxtLCC+nKR/Lw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uxYPxwOw; spf=pass (imf21.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 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 dfw.source.kernel.org (Postfix) with ESMTP id E5E006205A; Fri, 28 Jun 2024 03:06:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89772C2BBFC; Fri, 28 Jun 2024 03:06:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719544016; bh=wX8wNBVCAeYaipWwjXbga4FkUGc1GqAIKlVyc5psJK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uxYPxwOwKZkbAWcRCcj/faaq1syYlYM8BWU58yRNfmwar7N4AMMqbCcR61gQJ3hmJ nB9GYWciKXfEiUS4NLgEoiQquUcjB/0zyoiAKOBO8+xHDR4G1NEdIJ+wzy6h/WKB6H CLp9kQKd6Nbc+hxoc8kVP2YhPDyc6geIASE7dPiNVOMTMETM/IOUEOHBiw4hZ7rRVu 65oZiddBo5ncFNhCp1hfrtQm0GC4UA4uaVNCI03ksluRAywiDglD098AbmGckA0pU3 FHTvWD16NC64U9SYB4VXsPbRUDHmAuFRN4VfeFfhLySAXPbaYWFPevnO5EHDrJ8et0 7d+6X6G15pkQg== 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 Cc: Alex Shi Subject: [PATCH 02/20] mm/zsmalloc: use zpdesc in trylock_zspage/lock_zspage Date: Fri, 28 Jun 2024 11:11:17 +0800 Message-ID: <20240628031138.429622-3-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240628031138.429622-1-alexs@kernel.org> References: <20240628031138.429622-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C4BD91C0008 X-Stat-Signature: qsa8acsgphttazhdzuexgaz1muhjegzc X-HE-Tag: 1719544017-617130 X-HE-Meta: U2FsdGVkX1/Bz8EDbXHYSKtLYn0K0Z8OTT6jgffoncG21fcU6Mapk4Wg+iUEFZJN2wB85DGCnFiHE5dsKP1Wfge/v+RYm+GYSdckxLxgvv8fxBL1pk4CA6m6PBmLDF4CPg/sDRbltCpZ5DQULc3LEZw6g2Ze4BYEkJItHd8PVwuT9/JcjFASk4oQuZ69/2kXJqEGqNnnu+B4f4FcsZBgRO2XADCnKcqxXElRCj7ZSYIRvgaQqh58xKq+Sywa5K0vDfq0Y+9r1tFyAQf1Fg3ABgjs36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4MpscDqixnBv3aInoIgu77tY+2oEce19PP 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 a1ab5ebaa936..fd95277843d5 100644 --- a/mm/zpdesc.h +++ b/mm/zpdesc.h @@ -53,4 +53,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 67bb80b7413a..9835121109d1 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -435,13 +435,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; @@ -455,6 +459,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) @@ -747,6 +759,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 @@ -817,11 +839,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; } @@ -829,9 +851,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; } @@ -1663,7 +1685,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 @@ -1675,24 +1697,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); } }