From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 6256F29B77E for ; Wed, 27 May 2026 12:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779883305; cv=none; b=lM+zvsIdX68GIOete6/mfwHLiLQVIuLzq2CcZBY7XZLEfICnB1X8slX5/6uQkPLvgVngqP564d/k9IbZ67P1uyJuXhQwrj1311tqbfkSvaz3E2QZhwZ1Zt1FyQrOMlTDGFJzTbZIt/e6DD00BlhWHtnhecLwh9C27NGt9W3SCM0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779883305; c=relaxed/simple; bh=Xxr95N7FoI88mjkApknerWpKhMCUee+03JJlZDpcOYk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f3Pl8y3bnwPy/zp3SaqLEaHy/P7iGddqTQ1XWIA1/6SDVc+msuCDoJwrNIwwLinwygrrOGjd/jV+SVI5b0+DQn+DxyYuVkEWwIfvUJ/tYymUh72aQYfAcD+qDU9RM01AkepO1DwsoYuqSQhTM9wMZVlefGDSdVcE6/WAcW0R1rc= 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=qLqEzAsR; arc=none smtp.client-ip=209.85.214.174 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="qLqEzAsR" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2b458ca2296so86998425ad.0 for ; Wed, 27 May 2026 05:01:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779883302; x=1780488102; 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=8QbCz8rR6YzSa7i52By5W+WoT1arBQd3V3OYvXGtxm4=; b=qLqEzAsRPQYMqJjHi86/hkjULC01KzRqdks3iTYNAYAtf3RlDcToZfIMzQ2dcyFhCB KC6xoy3BfpVQE54ZP88zN8u2ZOFlSh6bASc6cHNJ3p+wXuh3CvUn/3J5Rq0kPc13zHMZ rBCl25SRzdvAxgw4IpilrSNoHqekS7icvPFbt4feDSRA8SpRFWvglRQm6upZ5AEXkDlm 3ft6ozLd5bDwPpDssbA7osh10v2bOR71zGhEWcPHYWV0/QOuszkrrih1ING+mTVNJNB+ LxLtXey/GvAeXt4f/S00e1A/GGzGK54MhewnxJ5t6wI7tEA/KvSOhQXo9ItHDCJoEPqT DsZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779883302; x=1780488102; 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=8QbCz8rR6YzSa7i52By5W+WoT1arBQd3V3OYvXGtxm4=; b=swxEhXA556IkL6dzzeviKHuyvIfDZ5BeHrhtVeSCvcKrL4XQeBHeP841U1vFnnezjd 2V2sYgzcaMqpiHQfK/3V7jQCM2tY0jeEsw369yYLyvHye/CpihBZdjKHKllzScSSuiN1 46CYAttwIIPXU9UhM3tbv3DTyp4bTdW8lJ6I89wAWlMFDLf0rq3VSFa4lAkuIG3SsfCF 9VAaXEz7qJzf7jbbI2dxddBRTer1yfBKko/iOqY8Ke/UMOWznCl2ycviv0fWYKf2LepO opY/DISqcBSjPYgFmwoubltl+U5HFa3Ttotqpa1bjXZ0xmCX5xNd2xEpu25p0K6H6nga Y9Cw== X-Forwarded-Encrypted: i=1; AFNElJ+ew5AMoyqatRUkHCfJTQK1BYWbGAQhimpV5n2Dj1iD48R3+FjF3lypZHP1dlPGd+LKzySYQdQdfPu1EIE=@vger.kernel.org X-Gm-Message-State: AOJu0YyQt5EH6iwnHZphMGjZcR/xnVc3xvyjuYPQfQ62zEeXvg7v9vop kgtCzp7xmzQPNuOUmET+FilRB35B2Iy38+CgsWJyGzXc1h1lvRD3iYKk X-Gm-Gg: Acq92OEbWMF5RUhmo5fFEtV2uL8V+wtXDIBK7J1RI3/lEOkcYderOoR6HhJQqqpReQH ggLIwUsp28D137vp0nQJxd63Xm6iD6uWVRNoucw/b5Or1bcZo6xgakNBuHZC7ZPhmwP1V2e9iDG +x4/I/askrjnHWapt6ZO9QpZU+hddUCC7tcw8k57/eJyLR0dc8o+V9Y1vvwOVAPSHBHKtvi2CWo RtHvF4CkzeSlEykqftLprKTzx32HfrECsQrKbGZl26x/7UrruQuwU6OPjjgSOLM4w4KkRpY52vd wAKMxdGW3+tb/tb6nVKxQCKCpxUmA3oyT1E7gD9/0/MCvzrJKxlc7yy8zBWZpuyta0nfAniNYfI s+CK/G3+oCzYecuXR6Rcfi4WopGxW6E/ebcWr43inQfn8PB4c9yuu9kuXD9N9ByGuuOK+nLF2AD ppOW9jrI9wVDuQCkMqYqRbbjSOZfdFNzlzCXNmmo90bkca0xcX X-Received: by 2002:a17:903:19e8:b0:2bd:b6f4:4500 with SMTP id d9443c01a7336-2beb038c123mr252408395ad.11.1779883301141; Wed, 27 May 2026 05:01:41 -0700 (PDT) Received: from ubuntu22.mioffice.cn ([43.224.245.232]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2beb56b5886sm136250465ad.20.2026.05.27.05.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 May 2026 05:01:40 -0700 (PDT) From: Wenchao Hao X-Google-Original-From: Wenchao Hao To: Andrew Morton , Nhat Pham , Yosry Ahmed , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Minchan Kim , Sergey Senozhatsky Cc: Wenchao Hao Subject: [RFC PATCH v2 2/3] mm/zsmalloc: drop pool->lock from zs_free on 64-bit systems Date: Wed, 27 May 2026 19:59:29 +0800 Message-Id: <20260527115930.3138213-3-haowenchao@xiaomi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260527115930.3138213-1-haowenchao@xiaomi.com> References: <20260527115930.3138213-1-haowenchao@xiaomi.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 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. On 32-bit systems pool->lock is preserved. Signed-off-by: Wenchao Hao --- mm/zsmalloc.c | 67 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 6b0014b43408..88d10f814da9 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -21,6 +21,10 @@ * pool->lock * class->lock * zspage->lock + * + * On 64-bit systems zs_free() does not take pool->lock; it locates + * the size_class using class_idx encoded in obj and serializes against + * page migration via class->lock. */ #include @@ -1432,10 +1436,59 @@ 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. + * + * 64-bit: class_idx is encoded in obj and 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. + * + * 32-bit: the unlocked load of *(unsigned long *)handle is not + * single-copy atomic and class_idx is not encoded (ZS_OBJ_CLASS_BITS + * == 0), so fall back to pool->lock for the lookup. + */ +#if BITS_PER_LONG >= 64 +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; @@ -1443,17 +1496,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