From patchwork Wed Jan 29 06:43:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13953467 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 26DADC02192 for ; Wed, 29 Jan 2025 06:49:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABF4F280024; Wed, 29 Jan 2025 01:49:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A6E2D28001A; Wed, 29 Jan 2025 01:49:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C139280024; Wed, 29 Jan 2025 01:49:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6BDD128001A for ; Wed, 29 Jan 2025 01:49:23 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 031F0C09BC for ; Wed, 29 Jan 2025 06:49:22 +0000 (UTC) X-FDA: 83059563006.27.52E5C0D Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf16.hostedemail.com (Postfix) with ESMTP id 2B69A180009 for ; Wed, 29 Jan 2025 06:49:20 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=TIdPVebp; spf=pass (imf16.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.179 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=1738133361; 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=QnAPrjmsHkTmFvv1w6err0gFxFxc4b8ZHvjvrGPNAh8=; b=xRgeDfyd9Q1O+6xkqJ0EVsI5jj0oaqZWwbDWoRktH9Rxyrqh1zXrrNrksDdXSEPPFGVSmI hueDFWJ7GWuEkhGRi3YwCoTwiJce3fnMVqwX6yu3AqGuXfZFUDv4CQF/e06pyX/pP42e64 fBcOYsl4IcEZmObL8HRwkHt7e/7lj9s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738133361; a=rsa-sha256; cv=none; b=gBIsvykLY7nU6t2kSv7aKIH/pWWhRXIPeJnKrgzqMfSPTsoVhmad8t0OEHR3liHqXUlPGz h7O0U0BPoE5sIDGP1bcjVbjg8A+qHFQ+X8pMOYWVa0MfwM6G2wGku7ntJgWuuJ/u3+MhgA i9giRHFdnIktVo+uXK7t5XNlNQ0RR8w= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=TIdPVebp; spf=pass (imf16.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.179 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2166022c5caso103360155ad.2 for ; Tue, 28 Jan 2025 22:49:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1738133360; x=1738738160; 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=QnAPrjmsHkTmFvv1w6err0gFxFxc4b8ZHvjvrGPNAh8=; b=TIdPVebpmvhPv7ccDj71xGSUqXmPSZaTvxrJIakbR7sfbhDffMHhPf6vwa4jYwUlCy hz++CYgjgwERsw6Zr2UW3p+NN4LgkgtlxqGoAVSn/GLDGhrJ1MjgZ/Z7tC0GR2LzgsKu WTrUJ395t4JR8JxH0wH5VTRlLK1g94sWamrhw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738133360; x=1738738160; 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=QnAPrjmsHkTmFvv1w6err0gFxFxc4b8ZHvjvrGPNAh8=; b=ZDPHjMwVVe7Qk/MqOfMP36gjpf3hgaOt6BckWrGz8k7Yxk7s3E2Enb6KlHUPerYakR uDWHWtYku+laNOH+bQQkIJoSOktYUut+JK4+xn7SuYfKIQgyrMtmdsmCbL9z9bKJVrXB iLoOHccwKOfWRsrpJV6p5kyLyLwybKsOPSqIk8CfWDiLInFxV9g9pNGREj52/y3PnN7D y4vjQDAgyA7DEoUnIIIOiH87wEwM7iIisQM8dpYiqyxiRm60t+99IKsjPgWcmAbV6rme aCkmdDvZKin6ygnukD41wpvt5nwUr+E1eKG6653VGxXBBTljtcCQjuyR1+bHcnXDddy/ DI/g== X-Gm-Message-State: AOJu0YzpJhaGuvYk974ppocbS3jXqMUMfcoOv8XE0xl2t0ixkaPh6S5D dlJU6kaOWkviNKrn4j7PZnX82N7v7TNUPtlJ7JgA0nSbgX8kXYp7wJ3txNNHcg== X-Gm-Gg: ASbGnct5V+GLpfWjLMluGPwc6Id/FTGR+MItdVCSS16Qa7EgWUeFyRQahEo2L/sMvfo dybjg9tWGEZOV/9MbLDsEoqIHANwKpbCPArVXUkbiXGkEl3OYnsHrqHfP/hCpqrVDbIiyzrxbds BdaGx08ZA68rLFDFrXGX3WCcp+/MypygbmZNaFU3u4B7ASfE5Xp0oMy/x5V7j50zaRftwfN5TKg vf/1PXbmgJi+lRGmIrNKNSuBJGgzR6tQvuOQ/vmIqcOum9sghjaFSjzBBMpZlmlFnCzNXxjU7DE MV32jvagubf6B8CNng== X-Google-Smtp-Source: AGHT+IHjUFNFjYC3vAlkkDSXkUk59QE2BpQabcLqZFb+C3g33UA0CBbzBu0rrjhlkGsVrTPsZsZh4Q== X-Received: by 2002:a17:902:db03:b0:215:b33b:e26d with SMTP id d9443c01a7336-21dd7c5141fmr27092835ad.21.1738133359961; Tue, 28 Jan 2025 22:49:19 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:b323:d70b:a1b8:1683]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-21da424f61fsm91599795ad.237.2025.01.28.22.49.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 28 Jan 2025 22:49:19 -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: [PATCHv1 3/6] zsmalloc: make zspage lock preemptible Date: Wed, 29 Jan 2025 15:43:49 +0900 Message-ID: <20250129064853.2210753-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog In-Reply-To: <20250129064853.2210753-1-senozhatsky@chromium.org> References: <20250129064853.2210753-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Stat-Signature: 3ss495eqp9yn9cyers1rwfzyozr5nazr X-Rspamd-Queue-Id: 2B69A180009 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738133360-721299 X-HE-Meta: U2FsdGVkX1+IqYT4GxBInspH9sSgdAsT9kQyxioA9DDvU5tv2vNZUJrOakgyzNFI7dKzlSYqrP4X2KEnaOMNNXKdq6+6DQo9oMUGL8OFYnzg3tg0xMQvUZtfadejIhV7kqZcK0SIYcfBR7ZxPBpFAmHekyGvqSxyaZ+bKxpZJf+bvVPxnczG2XKZE9nuQ3PGm+0RIuHPvrWoQ3q41EzNBiF0JnasuYX9W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== 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: Switch over from rwlock_t to a atomic_t variable that takes negative value when the page is under migration, or positive values when the page is used by zsmalloc users (object map, etc.) Using a rwsem per-zspage is a little too memory heavy, a simple atomic_t should suffice. zspage lock is a leaf lock for zs_map_object(), where it's read-acquired. Since this lock now permits preemption extra care needs to be taken when it is write-acquired - all writers grab it in atomic context, so they cannot spin and wait for (potentially preempted) reader to unlock zspage. There are only two writers at this moment - migration and compaction. In both cases we use write-try-lock and bail out if zspage is read locked. Writers, on the other hand, never get preempted, so readers can spin waiting for the writer to unlock zspage. With this we can implement a preemptible object mapping. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 140 +++++++++++++++++++++++++++++++------------------- 1 file changed, 88 insertions(+), 52 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 0f575307675d..8f4011713bc8 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -293,6 +293,9 @@ static inline void free_zpdesc(struct zpdesc *zpdesc) __free_page(page); } +#define ZS_PAGE_UNLOCKED 0 +#define ZS_PAGE_WRLOCKED -1 + struct zspage { struct { unsigned int huge:HUGE_BITS; @@ -305,7 +308,7 @@ struct zspage { struct zpdesc *first_zpdesc; struct list_head list; /* fullness list */ struct zs_pool *pool; - rwlock_t lock; + atomic_t lock; }; struct mapping_area { @@ -315,6 +318,64 @@ struct mapping_area { enum zs_mapmode vm_mm; /* mapping mode */ }; +static void zspage_lock_init(struct zspage *zspage) +{ + atomic_set(&zspage->lock, ZS_PAGE_UNLOCKED); +} + +/* + * zspage lock permits preemption on the reader-side (there can be multiple + * readers). Writers (exclusive zspage ownership), on the other hand, are + * always run in atomic context and cannot spin waiting for a (potentially + * preempted) reader to unlock zspage. This, basically, means that writers + * can only call write-try-lock and must bail out if it didn't succeed. + * + * At the same time, writers cannot reschedule under zspage write-lock, + * so readers can spin waiting for the writer to unlock zspage. + */ +static void zspage_read_lock(struct zspage *zspage) +{ + atomic_t *lock = &zspage->lock; + int old; + + while (1) { + old = atomic_read(lock); + if (old == ZS_PAGE_WRLOCKED) { + cpu_relax(); + continue; + } + + if (atomic_try_cmpxchg(lock, &old, old + 1)) + return; + + cpu_relax(); + } +} + +static void zspage_read_unlock(struct zspage *zspage) +{ + atomic_dec(&zspage->lock); +} + +static int zspage_try_write_lock(struct zspage *zspage) +{ + atomic_t *lock = &zspage->lock; + int old = ZS_PAGE_UNLOCKED; + + preempt_disable(); + if (atomic_try_cmpxchg(lock, &old, ZS_PAGE_WRLOCKED)) + return 1; + + preempt_enable(); + return 0; +} + +static void zspage_write_unlock(struct zspage *zspage) +{ + atomic_set(&zspage->lock, ZS_PAGE_UNLOCKED); + preempt_enable(); +} + /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */ static void SetZsHugePage(struct zspage *zspage) { @@ -326,12 +387,6 @@ static bool ZsHugePage(struct zspage *zspage) return zspage->huge; } -static void migrate_lock_init(struct zspage *zspage); -static void migrate_read_lock(struct zspage *zspage); -static void migrate_read_unlock(struct zspage *zspage); -static void migrate_write_lock(struct zspage *zspage); -static void migrate_write_unlock(struct zspage *zspage); - #ifdef CONFIG_COMPACTION static void kick_deferred_free(struct zs_pool *pool); static void init_deferred_free(struct zs_pool *pool); @@ -1027,7 +1082,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool, return NULL; zspage->magic = ZSPAGE_MAGIC; - migrate_lock_init(zspage); + zspage_lock_init(zspage); for (i = 0; i < class->pages_per_zspage; i++) { struct zpdesc *zpdesc; @@ -1252,7 +1307,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle, * zs_unmap_object API so delegate the locking from class to zspage * which is smaller granularity. */ - migrate_read_lock(zspage); + zspage_read_lock(zspage); pool_read_unlock(pool); class = zspage_class(pool, zspage); @@ -1312,7 +1367,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle) } local_unlock(&zs_map_area.lock); - migrate_read_unlock(zspage); + zspage_read_unlock(zspage); } EXPORT_SYMBOL_GPL(zs_unmap_object); @@ -1706,18 +1761,18 @@ static void lock_zspage(struct zspage *zspage) /* * Pages we haven't locked yet can be migrated off the list while we're * trying to lock them, so we need to be careful and only attempt to - * lock each page under migrate_read_lock(). Otherwise, the page we lock + * lock each page under zspage_read_lock(). Otherwise, the page we lock * may no longer belong to the zspage. This means that we may wait for * the wrong page to unlock, so we must take a reference to the page - * prior to waiting for it to unlock outside migrate_read_lock(). + * prior to waiting for it to unlock outside zspage_read_lock(). */ while (1) { - migrate_read_lock(zspage); + zspage_read_lock(zspage); zpdesc = get_first_zpdesc(zspage); if (zpdesc_trylock(zpdesc)) break; zpdesc_get(zpdesc); - migrate_read_unlock(zspage); + zspage_read_unlock(zspage); zpdesc_wait_locked(zpdesc); zpdesc_put(zpdesc); } @@ -1728,41 +1783,16 @@ static void lock_zspage(struct zspage *zspage) curr_zpdesc = zpdesc; } else { zpdesc_get(zpdesc); - migrate_read_unlock(zspage); + zspage_read_unlock(zspage); zpdesc_wait_locked(zpdesc); zpdesc_put(zpdesc); - migrate_read_lock(zspage); + zspage_read_lock(zspage); } } - migrate_read_unlock(zspage); + zspage_read_unlock(zspage); } #endif /* CONFIG_COMPACTION */ -static void migrate_lock_init(struct zspage *zspage) -{ - rwlock_init(&zspage->lock); -} - -static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock) -{ - read_lock(&zspage->lock); -} - -static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock) -{ - read_unlock(&zspage->lock); -} - -static void migrate_write_lock(struct zspage *zspage) -{ - write_lock(&zspage->lock); -} - -static void migrate_write_unlock(struct zspage *zspage) -{ - write_unlock(&zspage->lock); -} - #ifdef CONFIG_COMPACTION static const struct movable_operations zsmalloc_mops; @@ -1804,7 +1834,7 @@ static bool zs_page_isolate(struct page *page, isolate_mode_t mode) } static int zs_page_migrate(struct page *newpage, struct page *page, - enum migrate_mode mode) + enum migrate_mode mode) { struct zs_pool *pool; struct size_class *class; @@ -1820,15 +1850,12 @@ static int zs_page_migrate(struct page *newpage, struct page *page, VM_BUG_ON_PAGE(!zpdesc_is_isolated(zpdesc), zpdesc_page(zpdesc)); - /* We're committed, tell the world that this is a Zsmalloc page. */ - __zpdesc_set_zsmalloc(newzpdesc); - /* The page is locked, so this pointer must remain valid */ zspage = get_zspage(zpdesc); pool = zspage->pool; /* - * The pool migrate_lock protects the race between zpage migration + * The pool->migrate_lock protects the race between zpage migration * and zs_free. */ pool_write_lock(pool); @@ -1838,8 +1865,15 @@ static int zs_page_migrate(struct page *newpage, struct page *page, * the class lock protects zpage alloc/free in the zspage. */ size_class_lock(class); - /* the migrate_write_lock protects zpage access via zs_map_object */ - migrate_write_lock(zspage); + /* the zspage write_lock protects zpage access via zs_map_object */ + if (!zspage_try_write_lock(zspage)) { + size_class_unlock(class); + pool_write_unlock(pool); + return -EINVAL; + } + + /* We're committed, tell the world that this is a Zsmalloc page. */ + __zpdesc_set_zsmalloc(newzpdesc); offset = get_first_obj_offset(zpdesc); s_addr = kmap_local_zpdesc(zpdesc); @@ -1870,7 +1904,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page, */ pool_write_unlock(pool); size_class_unlock(class); - migrate_write_unlock(zspage); + zspage_write_unlock(zspage); zpdesc_get(newzpdesc); if (zpdesc_zone(newzpdesc) != zpdesc_zone(zpdesc)) { @@ -2006,9 +2040,11 @@ static unsigned long __zs_compact(struct zs_pool *pool, if (!src_zspage) break; - migrate_write_lock(src_zspage); + if (!zspage_try_write_lock(src_zspage)) + break; + migrate_zspage(pool, src_zspage, dst_zspage); - migrate_write_unlock(src_zspage); + zspage_write_unlock(src_zspage); fg = putback_zspage(class, src_zspage); if (fg == ZS_INUSE_RATIO_0) {