From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 022752DCF45 for ; Fri, 26 Jun 2026 01:54:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782438896; cv=none; b=axLRGvOBqGhwX+9RfMQlTtY/7llhLjhoi2eVG5d5cyo+x4h0UumCFTsQUrp2vH8qBfMRQWF6dvuhkFhs5ZpEfPgB0oqTIkg5uBWmFEUacraw7gFerU0EXzQ40dGyT3032swHjA54mmqmKBu/yLp3vdy+WP6JpfA9y6B8NdQyEY0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782438896; c=relaxed/simple; bh=fmQlILGRj1+/n/xuYlK1z+VApqDTRc7N/4m43cUUNMY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Kfv2rkbWOgND/6QEVcoNzwERstitCH9dtBWVsx6OctkQEavVfx2ZRDidKAU/42Td1BC/KzRMCTY1gunCYccX3rVOGQ+3J4qBypVKaGpd/5BMlSPI7lxGrF29FSRriV9bCv2b+WlrO1YnBkNl7uKLDfpX6M5H4mjUcZ+DlXom64o= 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=DRYZCk/y; arc=none smtp.client-ip=209.85.214.175 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="DRYZCk/y" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2c7e8eea816so6550195ad.1 for ; Thu, 25 Jun 2026 18:54:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782438894; x=1783043694; 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=kOGgumAUzRKaLXDbI9Lus6CsTT3DqObp/qlyzB6MNno=; b=DRYZCk/yL/9BO42k6FU8nmPq8sDEDryNvhbfg1deg1lgtIRp/zlXNjqydfCpZbz2Hl 7m0lkSWf0HVwKJQBj2AyS/Y2mxESrZrMT4zw1XuUctB/fOq1rI+kUdHZBozLVRqkCiCP zXyOAuiDxgHelW6m2OLdSFO5Sx+AjU0PWwPzNajrI8MklZve/784DD8nr4jPQQJPktyv Lh1LV1Vqo/IwveMGkv+uNnfoRp7XGLzzChqTFR/H08esvk02DYXXAMzGFJcZw+DHKvZm c5xYGlUnhvsk4OE/SB/kRrqKMr1k24CdyKAJJw/4PdDYgkYdV4EPqxXqFODVCqhXDxf8 N9gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782438894; x=1783043694; 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=kOGgumAUzRKaLXDbI9Lus6CsTT3DqObp/qlyzB6MNno=; b=cDcG59kugcUPjbb6MbPrcBpCFNseyb6kdkryKhS5y8EgN1ErdKK6HMOn+1BR15wVps Nvp7lXn6TL3GxM6Vkh5UoJFhYNEs81+1DZ8RHqDOTCAkdwm1thtZDpAELHSQ00QT5izb IoPInUnBDj4/6w7niDymL9rm/ewiUHVpCGFAkFq+noAWSXMWIaU6u+Oq80GWMc+JfpBW SKmXeoMtCgduIDR/d21LRKnJZv4CT1BmMEdAhpAgGZqxS/2X6PvMVRUaSVq3f1GBJgwy uHQQUG9cJypWSH50REKbfSLR5OsvalnPeIgbdX56sem0S4hrXwbl6AmM4doSc6Nj69OH EnVA== X-Forwarded-Encrypted: i=1; AHgh+RpLtdfEysR5S3Du2ni//HlGxeZHs82S/GumBK+RHqNJhRXUsY5Gnles2w9ouiDzdkZ1eEJZJ/XvaJTAINo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2k4IXjfkmdhkRr9AOE74J9MQ9i/VRgDQhH5YK6OARjlbvzSBs 0wzI7u3zXLGXisaBQq4luaxbA5ipRpQ/0KpHVB50Blhj1vyf8k56bTMw X-Gm-Gg: AfdE7cnkVyzir0x9PzxJn7q055J6D2g0ZW7VFy2WeU9CE13dyYupG0hr5sa8rPg+tQe XF7Vxzh6tgeiRgN80QOZstSnymsmpICUmIDiTG7y5DzNxUKkn5kQdS8TE+UEcn6A938Lem+Egtd DnH6ycP3sBQBYACEen4wmoCtcXM/aF/qldssm3lYLQBoFH0dwi08MrT3sJmd77rTbPfeVaadZbQ 1HHqWCU3ItQ33sgvdV0ksmLU95yg0e7j5f1oJF9/V5T4XCUzxG0f4ypkkWdgEN7FtYY0FhmNHT6 ost8NMnrVFdeT1wyxkf0+U8ggN71oWD2ZXAaf9uvxcYBThoB4wPLATW62wAtGiUSHVEtlnIPyGC yP4VmK+1wJk8WDi4prnSHHEeTNVljwtbhFUtOwbEdLniKpX8vIKoQmtTIYeYNSmhMkoWWBhnu3h gLgUb1c9R1pJC7DIW4tVEPjyUkAA6R X-Received: by 2002:a17:903:946:b0:2c0:fa52:1c47 with SMTP id d9443c01a7336-2c7c400fd0bmr121185375ad.25.1782438894198; Thu, 25 Jun 2026 18:54:54 -0700 (PDT) Received: from ubuntu22.mioffice.cn ([43.224.245.232]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c7f63b2bd7sm29047005ad.48.2026.06.25.18.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jun 2026 18:54:53 -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 v6 2/4] mm/zsmalloc: drop pool->lock from zs_free on 64-bit systems Date: Fri, 26 Jun 2026 09:50:01 +0800 Message-Id: <20260626015003.2965881-3-haowenchao22@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260626015003.2965881-1-haowenchao22@gmail.com> References: <20260626015003.2965881-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. Reviewed-by: Nhat Pham Reviewed-by: Barry Song 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 c4fc06b259af..210a777081b7 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 @@ -463,10 +467,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) @@ -823,7 +830,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, @@ -1457,10 +1464,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_class_get_and_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_class_get_and_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; @@ -1468,17 +1523,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_class_get_and_lock(pool, handle, &obj, &zspage, &class); class_stat_sub(class, ZS_OBJS_INUSE, 1); obj_free(class->size, obj); -- 2.34.1