From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 7EA3A36AB57 for ; Tue, 9 Jun 2026 11:36:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781004962; cv=none; b=G303r/h2/Fn+Y52AwXZ/Ytmmna5UrCuBPExh/k80aFpKz9OFoE0/c5FfvsI6NsPg6DlGZ7WQUxCgI6HYmCpcu1igVoOdHboOHJE9oDQ0rCHIgVDz8fHIsVJzYzpX9rmAWzyyvzs85cHD/YTeFMApGSw0C3ppLWxQYBM8tZGwTEs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781004962; c=relaxed/simple; bh=3sM15z4kj83iEQQiOMeVq7QQg+y5f9KWVZ6Jr9KmiFw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D+w+1nAuXNMqtASKJW1/M1D9jzn9J36EI3txbUtAGRqgxcHxQMXGKQuWNOwxDi5wlp044rS8Qa22GXiJ/xe1ivJifdeDD0WBu36V672mczrzV+yi76xqrjNxmwEH9gEcXBE0ZjxVU/ph1skurJHJOHE0x046bXhBcDzZbXbbQV4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Alr2pubj; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Alr2pubj" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-36b9ec98144so4322631a91.1 for ; Tue, 09 Jun 2026 04:36:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781004961; x=1781609761; 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=AKDSLwDdGTSyq9QGc1Jp5iLqo9aL/jeA2DLoFosrfnI=; b=Alr2pubjLF3R8OCcoEJXmOg2oGoTDaDUro6Qr3/tUmh6mCsfiF1iu2l1+y5w93Bq0Z nNMa2isu1XzsXGDlXN/FrGjfmRq4ArtpSAE4RGrvXs6FQFvmPLJuDTemk7A2Vicxzaz0 y1azzODX7YAU9Ymw2pT+VDrJU2DnFWlTCBFZdk5MhXCM14d4yk6RDrfLkLUzY7jfrsr8 YCY8EDhJNfVfYQW+oX/3NOB17D8luf0/T4ay+onztQiN3BlKhcGFxbraA/qJ8nqovQie Q0nAlMoY9z9nMtZBlLh1FG7ig5K2pITI2k0ZA17lCeAdRV5lN33mLJQDfdvUFt/fsPGs ruHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781004961; x=1781609761; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=AKDSLwDdGTSyq9QGc1Jp5iLqo9aL/jeA2DLoFosrfnI=; b=XOCxJn1dHdHNwY+tIjMSZ+A3tS5vPEluqEV6HtLAI3t8jxk2ePyUR0n/bl6bA/EV+p GmEO84kFdBoVbXnLWOi4Co0D1HlB8z+xshumczM4T08PIvnjCAgwaL4idK8kwaVRW3sX FdG33aCH3tIMXgZM/6OZR0kCfj8MVQfon+LYrTa7s0yJWJL1HZDN7Z4p2kIVQk+KPwMv zuS/rXq7NuJTn0cIljMHZv86Kw1H75jtwBXsI9/7SLOuA+Sn0P6gKdpfB6uBEzAQXeT+ +MiNmvOqGg1N6I3vXuLyAkkW+pYSdZkz+Cw4FT9/9jHf6BnfXZKnxlj7WDDE9JAR7NfI YfWA== X-Forwarded-Encrypted: i=1; AFNElJ+LWBRcF+s98IlWvbGnGI1setSso3DP48arH9ZRI8hcxn6kupqp2GYfFuQDdRa1NVCX/Lm95fN5BvCVhCM=@vger.kernel.org X-Gm-Message-State: AOJu0YwnIg/Qgjog2DyM5yrr1CTLfua9eMWTDx0sksgCdanDu3l+tnMk kKb8d8j/9nZXYXbFkGrDBpZS9kmWX9HRnTLRpsxuWWMY2ndJcN5TNw/6 X-Gm-Gg: Acq92OHqGR+ETVc/Xa+ckxvoteohwqhRSoy4hC8DjaNbvphPU3eYT0POd2YVUY520HT 8feZB35L2uUmpjrzg9tBRxAbQHm9r5iC7XKSqGnVm53b6lwtTBunMncFMn7K5JPtdFeQjGoUdJi lSb3/B4w+0xZCvzTmnGb/cvaCRcwSxy2iDBP318BLA3q2usny+q6iV76nnBP5rsVje7Ky4Udn6p n59vDJmUJL88b1YEYyw8gj9scUyc3J2XginIIE/mNEk0DylStq6sdOIY5IzqQyz9gpmoT46pJ88 G8yGYjzqNEPyfJixXclax8n9faIXQKN4BUGdGJgMFSspoVzxJA3MNR8F4VQlfiBkZk9qwEVunKd HCh661mZZUOev0YsOag20PjlduZ1hqQ0VUuJXlxyxXEP2jLlUIOcjbzrnc8yeaQYLqKclcqRuuD jXN7flNld98rG8ZLnYY1HlUhUFtlfZ5vVfgdJ+LSoJ8cfxGxRV3g== X-Received: by 2002:a17:90b:2dc7:b0:36d:cf58:b79 with SMTP id 98e67ed59e1d1-375211ae901mr3093505a91.19.1781004960720; Tue, 09 Jun 2026 04:36:00 -0700 (PDT) Received: from ubuntu22.mioffice.cn ([43.224.245.232]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36f6c668e98sm19655292a91.5.2026.06.09.04.35.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2026 04:36:00 -0700 (PDT) From: Wenchao Hao To: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Minchan Kim , Sergey Senozhatsky Cc: Nhat Pham , Joshua Hahn , Barry Song , Wenchao Hao Subject: [PATCH v4 2/4] mm/zsmalloc: drop pool->lock from zs_free on 64-bit systems Date: Tue, 9 Jun 2026 19:35:18 +0800 Message-Id: <20260609113520.3507783-3-haowenchao22@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260609113520.3507783-1-haowenchao22@gmail.com> References: <20260609113520.3507783-1-haowenchao22@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Wenchao Hao With class_idx encoded in obj, zs_free() can locate the size_class without holding pool->lock on 64-bit systems. Page migration also takes class->lock and only rewrites the PFN field of obj, so: 1. read obj locklessly, 2. lock the size_class derived from obj's class_idx, 3. re-read obj under class->lock to get a stable PFN. This eliminates the rwlock read-side cacheline bouncing between zs_free() and migration/compaction on multi-core systems. Annotate handle_to_obj()/record_obj() with READ_ONCE()/WRITE_ONCE() to prevent load/store tearing on the lockless read path and silence KCSAN data race reports. When ZS_OBJ_CLASS_BITS == 0 (32-bit, or 64-bit with obj too narrow to hold class_idx), zs_free() keeps pool->lock. Signed-off-by: Wenchao Hao --- mm/zsmalloc.c | 75 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index f84258d63917..fe20ab297542 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -21,6 +21,10 @@ * pool->lock * class->lock * zspage->lock + * + * When ZS_OBJ_CLASS_BITS > 0, zs_free() skips pool->lock; it picks + * the size_class from obj's encoded class_idx and serializes against + * page migration via class->lock. */ #include @@ -457,10 +461,13 @@ static void cache_free_zspage(struct zspage *zspage) kmem_cache_free(zspage_cachep, zspage); } -/* class->lock(which owns the handle) synchronizes races */ +/* + * Pairs with READ_ONCE() in handle_to_obj(): zs_free() may read the + * handle locklessly, so prevent store tearing here. + */ static void record_obj(unsigned long handle, unsigned long obj) { - *(unsigned long *)handle = obj; + WRITE_ONCE(*(unsigned long *)handle, obj); } static inline bool __maybe_unused is_first_zpdesc(struct zpdesc *zpdesc) @@ -817,7 +824,7 @@ static unsigned long location_to_obj(struct zpdesc *zpdesc, unsigned int obj_idx static unsigned long handle_to_obj(unsigned long handle) { - return *(unsigned long *)handle; + return READ_ONCE(*(unsigned long *)handle); } static inline bool obj_allocated(struct zpdesc *zpdesc, void *obj, @@ -1451,10 +1458,58 @@ static void obj_free(int class_size, unsigned long obj) mod_zspage_inuse(zspage, -1); } +/* + * Resolve @handle to its zspage / size_class and acquire class->lock. + * + * When class_idx is encoded in obj (ZS_OBJ_CLASS_BITS > 0), it is + * invariant under page migration, so the handle can be read locklessly + * to pick the size_class. Once class->lock is held migration is + * blocked and the handle is re-read to obtain a stable PFN. + * + * Otherwise (32-bit, or 64-bit fallback paths like UML where the + * encoding is disabled), fall back to pool->lock for the lookup. + */ +#if ZS_OBJ_CLASS_BITS > 0 +static inline void obj_handle_class_lock(struct zs_pool *pool, unsigned long handle, + unsigned long *objp, struct zspage **zspagep, + struct size_class **classp) + __acquires(&(*classp)->lock) +{ + struct zpdesc *f_zpdesc; + unsigned long obj; + + obj = handle_to_obj(handle); + *classp = pool->size_class[obj_to_class_idx(obj)]; + spin_lock(&(*classp)->lock); + /* Re-read under class->lock: PFN is now stable vs migration. */ + obj = handle_to_obj(handle); + obj_to_zpdesc(obj, &f_zpdesc); + *zspagep = get_zspage(f_zpdesc); + *objp = obj; +} +#else +static inline void obj_handle_class_lock(struct zs_pool *pool, unsigned long handle, + unsigned long *objp, struct zspage **zspagep, + struct size_class **classp) + __acquires(&(*classp)->lock) +{ + struct zpdesc *f_zpdesc; + unsigned long obj; + + read_lock(&pool->lock); + obj = handle_to_obj(handle); + obj_to_zpdesc(obj, &f_zpdesc); + *zspagep = get_zspage(f_zpdesc); + *classp = zspage_class(pool, *zspagep); + spin_lock(&(*classp)->lock); + read_unlock(&pool->lock); + *objp = obj; +} +#endif + void zs_free(struct zs_pool *pool, unsigned long handle) { struct zspage *zspage; - struct zpdesc *f_zpdesc; unsigned long obj; struct size_class *class; int fullness; @@ -1462,17 +1517,7 @@ void zs_free(struct zs_pool *pool, unsigned long handle) if (IS_ERR_OR_NULL((void *)handle)) return; - /* - * The pool->lock protects the race with zpage's migration - * so it's safe to get the page from handle. - */ - read_lock(&pool->lock); - 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->lock); + obj_handle_class_lock(pool, handle, &obj, &zspage, &class); class_stat_sub(class, ZS_OBJS_INUSE, 1); obj_free(class->size, obj); -- 2.34.1