From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A63DF3C1F47 for ; Tue, 30 Jun 2026 02:50:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782787827; cv=none; b=B3kHQcjuBM2W1oKnU3fTqa9uun1buu81aT1EkdE8zJ2/aYEnd4Rwju7DI2xtphCVp8YK5I3jmEsNnmTrig3pMb/aX9+YSJeLMm8LNEcrQWRL7hI/F2RewuNSaucO+Q7ZJMSBVn2IEQFcXuQkxFhB8q8F3SyZfACqeytdP7p3iAw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782787827; c=relaxed/simple; bh=bjQaQEkQryl8ELOvuiTKa5PzBGXtZvKbqWGG+yETmd8=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=UqFvqwSPy9CcvODtIFUbbKMGgm7EIR4jfT+y62eBPrl8eEO7Jg+r2Ty+BzuuJ7Tm4b6q3S8KM/rLRRT71BZUR7FXX0Wme/3Mvx25rvA65HAA37wkYUf9apj5p/mJs68KX623Mzwn4RfDy1n5gj8y9EU/JAXGuZW6OZ1QaajkgnM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=NCFL8Snk; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="NCFL8Snk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E03FE1F000E9; Tue, 30 Jun 2026 02:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1782787826; bh=yRQY4R85CQV6CHcgnQiXFWcOY+A+FDGHfy/QBTilh3U=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=NCFL8SnkgYr3rOPqNRDdqRsDtJs0HVXR4V2GiDaujiPLLtRT+vNs3SWWM8uReE8En ZR5ZVLEm+hpN6LDO1aYRB08a2NikV5WsdHzU4E0xEcYtQ1l3zs8pnEHcearvMjcFU+ AkSftbk8nSaAguEWAoTZGolT1bZDn1jb3QANc1OU= Date: Mon, 29 Jun 2026 19:50:25 -0700 From: Andrew Morton To: Wenchao Hao Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Minchan Kim , Sergey Senozhatsky , Nhat Pham , Joshua Hahn , Barry Song , Wenchao Hao Subject: Re: [PATCH v6 2/4] mm/zsmalloc: drop pool->lock from zs_free on 64-bit systems Message-Id: <20260629195025.0bbad03ddb714f72615cab53@linux-foundation.org> In-Reply-To: <20260626015003.2965881-3-haowenchao22@gmail.com> References: <20260626015003.2965881-1-haowenchao22@gmail.com> <20260626015003.2965881-3-haowenchao22@gmail.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 26 Jun 2026 09:50:01 +0800 Wenchao Hao wrote: > 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. This thing is still causing problems. How about this? From: Andrew Morton Subject: mm-zsmalloc-drop-pool-lock-from-zs_free-on-64-bit-systems-fix Date: Mon Jun 29 07:48:04 PM PDT 2026 build fix mm/zsmalloc.c: In function 'obj_class_get_and_lock': mm/zsmalloc.c:1481:36: error: implicit declaration of function 'obj_to_class_idx' [-Wimplicit-function-declaration] 1481 | *classp = pool->size_class[obj_to_class_idx(obj)]; | ^~~~~~~~~~~~~~~~ Cc: Barry Song Cc: Joshua Hahn Cc: Minchan Kim Cc: Nhat Pham Cc: Sergey Senozhatsky Cc: Wenchao Hao Cc: Xueyuan Chen Signed-off-by: Andrew Morton --- mm/zsmalloc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- a/mm/zsmalloc.c~mm-zsmalloc-drop-pool-lock-from-zs_free-on-64-bit-systems-fix +++ a/mm/zsmalloc.c @@ -1457,6 +1457,12 @@ static void obj_free(int class_size, uns mod_zspage_inuse(zspage, -1); } +/* Folds to 0 when ZS_OBJ_CLASS_BITS == 0; no ifdef needed at callers. */ +static unsigned int obj_to_class_idx(unsigned long obj) +{ + return (obj >> ZS_OBJ_IDX_BITS) & ZS_OBJ_CLASS_MASK; +} + /* * Resolve @handle to its zspage / size_class and acquire class->lock. * @@ -1759,12 +1765,6 @@ static void lock_zspage(struct zspage *z zspage_read_unlock(zspage); } -/* Folds to 0 when ZS_OBJ_CLASS_BITS == 0; no ifdef needed at callers. */ -static unsigned int obj_to_class_idx(unsigned long obj) -{ - return (obj >> ZS_OBJ_IDX_BITS) & ZS_OBJ_CLASS_MASK; -} - static void replace_sub_page(struct size_class *class, struct zspage *zspage, struct zpdesc *newzpdesc, struct zpdesc *oldzpdesc) { _