From patchwork Mon Jan 27 07:59:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13951055 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 DEDC3C0218C for ; Mon, 27 Jan 2025 08:03:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70436280116; Mon, 27 Jan 2025 03:03:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B6042800E8; Mon, 27 Jan 2025 03:03:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 555C7280116; Mon, 27 Jan 2025 03:03:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3708F2800E8 for ; Mon, 27 Jan 2025 03:03:27 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CBB78820C3 for ; Mon, 27 Jan 2025 08:03:26 +0000 (UTC) X-FDA: 83052492012.27.C3C1F1B Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf19.hostedemail.com (Postfix) with ESMTP id E74AA1A0006 for ; Mon, 27 Jan 2025 08:03:24 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="c92d/bH0"; spf=pass (imf19.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.170 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737965005; 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=PXEed/HJJxd4qZRhg1cqdVLafAkTWhNb+3EhlC5igm8=; b=5WZu2q/+7lvg/pVixp7YLQaTjCYe9fScXZ8oavsJWhEDo3dLqxFhdHZtfSMwc1mIvfkt8G kIt4tijXYu9vQpyzAVTYAzv/sPmV8DZA8EHcR6/w5wPSF+4B6L/1Ow7JDnzwYt5hXa5VGq Ugb5UeXRPRQTruoTn5lKwGJ5s2cpfBI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="c92d/bH0"; spf=pass (imf19.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.170 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737965005; a=rsa-sha256; cv=none; b=DBVxYQ3Y9dJdsLSiazg6xC9yYaZrBLPuFO/4S0FFxYI2J4e2HfNCGWMVfBzACWmHsWm2hT Qu83/anTuH445Qj9YlmZbT3IosCR9VNd/RqSG7IgKiNZvd8TX+B+uYXaKkYv0M/+cFTcAK WHkQv3UaV0PDPGcJod97ZmEREoaVJS8= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2164b662090so77919525ad.1 for ; Mon, 27 Jan 2025 00:03:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1737965004; x=1738569804; darn=kvack.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=PXEed/HJJxd4qZRhg1cqdVLafAkTWhNb+3EhlC5igm8=; b=c92d/bH02dvUolx+U2AStQtdH5vf5/ymPEp4YLdNaYkDVugn2Y2USXdsWo0pBjiJ0h 5LEbBQHMvvuJybTHZV63Rhf4y1/XBVSpPoNJLQjxHxlNZa8NwSt/K+rRCH9OugTmBKX2 VbS2Yh20mIVL/KZIX8MKNEBVicDX6eKnith+4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737965004; x=1738569804; 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=PXEed/HJJxd4qZRhg1cqdVLafAkTWhNb+3EhlC5igm8=; b=iJ4OMHKeitYNNbGJBY4vMudq8JJ08okgEeHPoajjd2ydLyvAVdZmCEvgjK7t1jUY2G j6te5G/n9JV1IiW3Yj24CR+DyJPD9VTsRuUGuAqL411Ub5xSk/Vmcrdpa5Y1YpY1QH7I A/d4+Dd87ORlSlf+ixDtZrG+74T1igDiTbpgenxsP00Ko+3sWEg5Vt/VpCtcics00mGM v4E7cDHzne0v/+UVOUf/ziFSMQc9VsS7MDgm7vkkaF6Yino52s7VGjwLIaTmyufMkI6z GzTBpUPBEMqzmmfARmmPBmL8QGncuJYIXpV9WccT5JNL+LQmbngpRNq81+dYz/L9I5uW Q20g== X-Gm-Message-State: AOJu0YxlrpVHqg49IHJogIAUGTAcjp8zYCCxv/XzVf9qOJmK13o2V6wS GP0ssxS0ZF5ol+1lndtO9qnT8+ElSlhDFVhSwjJCZLZwuVLJ/NTp2uw3h3tIyQ== X-Gm-Gg: ASbGncvC/0QmqJMaSi6Ypk/EBoIEAFTKc510yr+U1QOobG6LK62QqF0GpXT24Iu3FJb 0aPyHFNqcxSywsHf+5V0Q1vEB4cey/Yzwwh7TxCFGBrXNag3rC/NL44mLrhvs5R2X7fgy9XkkZI oh8ilKP2CeoTPSGduTOaPAbriRKDN2L3NdmOLygiQiGmtysUNOh2+ggvlwIpvgSaSbxYKabY5MQ 3Gvlw0GskWd6/eZ3i9D/JX73SCQgTaR5fUm+crsrVY4eqVrodAYFxi05swugg2jwJD+a6f3tiq0 eEJezuI= X-Google-Smtp-Source: AGHT+IEL57Fr+lPzD2FowIHfSuVgiI3V3YUg02jovjSl+Fh1RpWQYSxO6GJ+HfktJR9cPnagmL7JRg== X-Received: by 2002:a05:6a00:2e15:b0:728:e906:e45a with SMTP id d2e1a72fcca58-72dafbfd7e4mr61554805b3a.24.1737965002160; Mon, 27 Jan 2025 00:03:22 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:566d:6152:c049:8d3a]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-72f8a78ee7bsm6686774b3a.170.2025.01.27.00.03.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Jan 2025 00:03:21 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim , Johannes Weiner , Yosry Ahmed , Nhat Pham Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [RFC PATCH 3/6] zsmalloc: convert to sleepable pool lock Date: Mon, 27 Jan 2025 16:59:28 +0900 Message-ID: <20250127080254.1302026-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250127080254.1302026-1-senozhatsky@chromium.org> References: <20250127080254.1302026-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E74AA1A0006 X-Stat-Signature: eh4j9pp7oj3rycsxs464b1grsh36m7xc X-HE-Tag: 1737965004-973344 X-HE-Meta: U2FsdGVkX1+c/iLbBxv2/f7gXS9BDnQCMFUhh1ccRn61jISeslz12Ts54ZVgtbRppwqok7iqIGv5TKXYMsDXsJwtqJ0/uwQSluFD/gUB9NEYKk8jS/IgSG09q6nx0JOJzvUDSYN5RswnjsZd/QZXumhRt/pkbhpM1Qps9OnanMuK53WqUvqr20ja0KIW8kl632bqx6UqWa7QqjfhXjMzvRXbcGnvrfZl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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: Switch over from rwlock_t to rwsemaphore, also introduce simple helpers to lock/unlock the pool. This is needed to make zsmalloc preemptible in the future. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 58 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 28a75bfbeaa6..751871ec533f 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -204,8 +204,8 @@ struct link_free { }; struct zs_pool { - const char *name; - + /* protect page/zspage migration */ + struct rw_semaphore migrate_lock; struct size_class *size_class[ZS_SIZE_CLASSES]; struct kmem_cache *handle_cachep; struct kmem_cache *zspage_cachep; @@ -216,6 +216,7 @@ struct zs_pool { /* Compact classes */ struct shrinker *shrinker; + atomic_t compaction_in_progress; #ifdef CONFIG_ZSMALLOC_STAT struct dentry *stat_dentry; @@ -223,11 +224,34 @@ struct zs_pool { #ifdef CONFIG_COMPACTION struct work_struct free_work; #endif - /* protect page/zspage migration */ - rwlock_t migrate_lock; - atomic_t compaction_in_progress; + const char *name; }; +static void pool_write_unlock(struct zs_pool *pool) +{ + up_write(&pool->migrate_lock); +} + +static void pool_write_lock(struct zs_pool *pool) +{ + down_write(&pool->migrate_lock); +} + +static void pool_read_unlock(struct zs_pool *pool) +{ + up_read(&pool->migrate_lock); +} + +static void pool_read_lock(struct zs_pool *pool) +{ + down_read(&pool->migrate_lock); +} + +static bool zspool_lock_is_contended(struct zs_pool *pool) +{ + return rwsem_is_contended(&pool->migrate_lock); +} + static inline void zpdesc_set_first(struct zpdesc *zpdesc) { SetPagePrivate(zpdesc_page(zpdesc)); @@ -1251,7 +1275,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle, BUG_ON(in_interrupt()); /* It guarantees it can get zspage from handle safely */ - read_lock(&pool->migrate_lock); + pool_read_lock(pool); obj = handle_to_obj(handle); obj_to_location(obj, &zpdesc, &obj_idx); zspage = get_zspage(zpdesc); @@ -1263,7 +1287,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle, * which is smaller granularity. */ zspage_read_lock(zspage); - read_unlock(&pool->migrate_lock); + pool_read_unlock(pool); class = zspage_class(pool, zspage); off = offset_in_page(class->size * obj_idx); @@ -1498,13 +1522,13 @@ void zs_free(struct zs_pool *pool, unsigned long handle) * The pool->migrate_lock protects the race with zpage's migration * so it's safe to get the page from handle. */ - read_lock(&pool->migrate_lock); + pool_read_lock(pool); obj = handle_to_obj(handle); obj_to_zpdesc(obj, &f_zpdesc); zspage = get_zspage(f_zpdesc); class = zspage_class(pool, zspage); spin_lock(&class->lock); - read_unlock(&pool->migrate_lock); + pool_read_unlock(pool); class_stat_sub(class, ZS_OBJS_INUSE, 1); obj_free(class->size, obj); @@ -1816,7 +1840,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page, * The pool migrate_lock protects the race between zpage migration * and zs_free. */ - write_lock(&pool->migrate_lock); + pool_write_lock(pool); class = zspage_class(pool, zspage); /* @@ -1853,7 +1877,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page, * Since we complete the data copy and set up new zspage structure, * it's okay to release migration_lock. */ - write_unlock(&pool->migrate_lock); + pool_write_unlock(pool); spin_unlock(&class->lock); zspage_write_unlock(zspage); @@ -1976,7 +2000,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, * protect the race between zpage migration and zs_free * as well as zpage allocation/free */ - write_lock(&pool->migrate_lock); + pool_write_lock(pool); spin_lock(&class->lock); while (zs_can_compact(class)) { int fg; @@ -2003,14 +2027,14 @@ static unsigned long __zs_compact(struct zs_pool *pool, src_zspage = NULL; if (get_fullness_group(class, dst_zspage) == ZS_INUSE_RATIO_100 - || rwlock_is_contended(&pool->migrate_lock)) { + || zspool_lock_is_contended(pool)) { putback_zspage(class, dst_zspage); dst_zspage = NULL; spin_unlock(&class->lock); - write_unlock(&pool->migrate_lock); + pool_write_unlock(pool); cond_resched(); - write_lock(&pool->migrate_lock); + pool_write_lock(pool); spin_lock(&class->lock); } } @@ -2022,7 +2046,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, putback_zspage(class, dst_zspage); spin_unlock(&class->lock); - write_unlock(&pool->migrate_lock); + pool_write_unlock(pool); return pages_freed; } @@ -2159,7 +2183,7 @@ struct zs_pool *zs_create_pool(const char *name) return NULL; init_deferred_free(pool); - rwlock_init(&pool->migrate_lock); + init_rwsem(&pool->migrate_lock); atomic_set(&pool->compaction_in_progress, 0); pool->name = kstrdup(name, GFP_KERNEL);