From nobody Fri Jan 31 00:15:55 2025 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 838FD1537C8 for ; Mon, 27 Jan 2025 08:03:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737965006; cv=none; b=eOhrP46bzGaXhAMZQLtEAqRiJVxOr+6/zVxf6wcW++vevnsyUS0+/bKIu+SgQ/SbFH/VaId0m78qh2yR3N86CAVPUoXhp2l7B4rludkdujJJt4NFsHyZNkRFiWBePGkZG5DjgibSFKTjb4yg/Cs6DiFxEVy1J4NROjA64f1K0NA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737965006; c=relaxed/simple; bh=TXWJJhcxS2MPvpGIStG8zEpEBFUw2RTEBOjOrPbhrfs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GPc6RpPNEOD/rZyAXEN2AeOvGeMrd0cF/OUU/re4ny1D98SG4C3WPNTxDsNTkUPt73YLK7b1gKfIYgAqxTxr2sGVtawvFBNy/04LuZ6wKBAregpkJyvUcADN/Zy2+F3q80XUsq/fO8GEvaRiRs5+4Y8Lfw439n57ckbPQVWf+Pk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=SgQwiRvU; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="SgQwiRvU" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2161eb94cceso48063115ad.2 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=vger.kernel.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=SgQwiRvUi8IKY5TlI/3nkhh5gFMBg5Z3pwFEHaaDvgveRnFioXNYx0spLdPrGYfybu xdbryI7VtndXRHGABQcKE8ESchfmBMyrN55agHHOpNbUY0k2B6RA2l6TMgxwVhFbNt4g Tu7nsoHCV2YF7tSJed+AfOxZpJlfTGJi7javQ= 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=sqA2IhfcPi9IffKWbDmDAvyJj1e/IvQjMArNrYSUNs0XGEbwagnqh5d8T29RAocGrI hdSifOZiCCFo8Cm11RKPcZg3k4OMdJxvRT3XvZiDobq0sgAytZSFNleAR6fZqLRAQanE FkGM818Z9LagyoGyWKHZgQk+8rjXwAeNMJtBA2yDrLKEDA+mPXj+9eJ9BdelyzHwq8nZ K9x0MZuh6elDwx4sNT2LQsoaS6Ax90zH3oZmAcg+Kw4Bz49+Cb0LeJ3EUapBQY1JoyIM fn/NP/ra4HZgJLde6SIVsMoQDJOqTW2YywbxVrH1uYYPXKzqeJiLXgtaXgaynpuOXA+O 36YQ== X-Forwarded-Encrypted: i=1; AJvYcCV1cyoGraANDQvWt3wXWSgyZiVpGdIknBcbIBxrw/sHzVBpA+usBz2ZcwU7YZHLLH/hw5ntirjlo9XpdKA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/DjKtpMJ7hirAgLmHyfZCglnkne4J3faRvf5VdLq/niuaX7/4 XaXmsZ2LQh7v4tJfvoPCAExaMJm3/W0y/a69LwITAYUTR7nt/7qQ34vqm4SR4w== X-Gm-Gg: ASbGncvBKqeNZlwKuQVW6kqpB0jR7CVhGDr3KDx0p9TLjHnnWtC3jxDdcSI1OB83WUx /ef97Mq2QKR6VGATzj21wVvEy7Hn9ibzfyXxs+yGm/GpPMVkvjAGxGxQpTcy53nujUtPC9iUP8E CMLzF1dvHXyxpu2mUDi9214Rn11u4ObhYMtC6Gd5gSw3WJYz5gfwsQ3HglBH79FEJyhpTsHbH18 gmJ5HqeIgWb3vQiDUnntxcA5637P0eX4a/0Nrd80+LFX7Xajr6FercRtSxwbrxvEIxGafirNfGR yGq6KKs= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 { }; =20 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 { =20 /* Compact classes */ struct shrinker *shrinker; + atomic_t compaction_in_progress; =20 #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; }; =20 +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 lo= ng handle, BUG_ON(in_interrupt()); =20 /* It guarantees it can get zspage from handle safely */ - read_lock(&pool->migrate_lock); + pool_read_lock(pool); obj =3D handle_to_obj(handle); obj_to_location(obj, &zpdesc, &obj_idx); zspage =3D get_zspage(zpdesc); @@ -1263,7 +1287,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned lo= ng handle, * which is smaller granularity. */ zspage_read_lock(zspage); - read_unlock(&pool->migrate_lock); + pool_read_unlock(pool); =20 class =3D zspage_class(pool, zspage); off =3D offset_in_page(class->size * obj_idx); @@ -1498,13 +1522,13 @@ void zs_free(struct zs_pool *pool, unsigned long ha= ndle) * 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 =3D handle_to_obj(handle); obj_to_zpdesc(obj, &f_zpdesc); zspage =3D get_zspage(f_zpdesc); class =3D zspage_class(pool, zspage); spin_lock(&class->lock); - read_unlock(&pool->migrate_lock); + pool_read_unlock(pool); =20 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, stru= ct 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 =3D zspage_class(pool, zspage); =20 /* @@ -1853,7 +1877,7 @@ static int zs_page_migrate(struct page *newpage, stru= ct 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); =20 @@ -1976,7 +2000,7 @@ static unsigned long __zs_compact(struct zs_pool *poo= l, * 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 *p= ool, src_zspage =3D NULL; =20 if (get_fullness_group(class, dst_zspage) =3D=3D ZS_INUSE_RATIO_100 - || rwlock_is_contended(&pool->migrate_lock)) { + || zspool_lock_is_contended(pool)) { putback_zspage(class, dst_zspage); dst_zspage =3D NULL; =20 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 *poo= l, putback_zspage(class, dst_zspage); =20 spin_unlock(&class->lock); - write_unlock(&pool->migrate_lock); + pool_write_unlock(pool); =20 return pages_freed; } @@ -2159,7 +2183,7 @@ struct zs_pool *zs_create_pool(const char *name) return NULL; =20 init_deferred_free(pool); - rwlock_init(&pool->migrate_lock); + init_rwsem(&pool->migrate_lock); atomic_set(&pool->compaction_in_progress, 0); =20 pool->name =3D kstrdup(name, GFP_KERNEL); --=20 2.48.1.262.g85cc9f2d1e-goog