From nobody Wed Feb 11 08:33:20 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 AF33933981 for ; Wed, 29 Jan 2025 08:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738139314; cv=none; b=PRP6lxwAH67i9qOU43HE3F2Ycc3gu1KyjB0q7VaCAqolExa89LpvnRy1mu7gyNmsERbgPZ1FxicP+KKNtcFceBzmTL6rJuvnoSSO57xIJswuWJnUbB43z5iNU5V9L5QzL04yHc3Ctuvu4foKPJkCyUeQo/wSEO3S3cc6ljX8Oh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738139314; c=relaxed/simple; bh=IMYQmo1ATHTPx95qY6jNB+WnN6hc4VmYNssRVl5i9lU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=uMxkLQ3u/jKwLCIydiN2Yndby8iHyrRuz6lwIwtybScUIptMngVuqA6XnL6/T367WwlRXcYH5x3RdE6vd4PXKHs7IwPOPzxY9bmO/RWTgR39sYblOMh6G8jIP9Pok+KNszXMgw4NCEpjQAA4pKJI1LKsSRVMVJRlkNVoyYnsGJs= 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=AL4StmrN; arc=none smtp.client-ip=209.85.214.177 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="AL4StmrN" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-216395e151bso6011385ad.0 for ; Wed, 29 Jan 2025 00:28:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738139311; x=1738744111; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MjpgunADEMuBU+/6HJpTGrs29am5J7k2GIEbNsY8OvA=; b=AL4StmrN518oQuzmim/Xn5eJxJf7wTBvX1+UP2hGjUKG6gEN3CSnoW5q7dVH9uXV2P GDVmV4jwaDMMXMTncMjbXIsjb2r5Xbn0ySuI2kpjVdV3N2dS3TCX1WR3DayeHEqGsPpk VoL9W2ex0U3zAMf0z/t51aH/x+kTqr16qKG4lYleUPHw8F5EcvXIAvuf8R3CGofr4P3G 9xsOGed2EL+9aULpuxNR/XpdVuEuEOz9moCfEbLnamqOFJJwqH0g99Twpxh/35EkLBy7 TvvqE7Z+/9+GxfN9QST8MaJmmf4k2VymwBePlH+8HjYflMoZFRFduIKrpxw1SVh29FSK lxrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738139311; x=1738744111; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MjpgunADEMuBU+/6HJpTGrs29am5J7k2GIEbNsY8OvA=; b=aE4Dg6Pv2Nxbr5Q3ouVWb8zVlO0h1u8Dk8RQz95ENWOVK9/SJTP1+fpTOnzo039yUD iM4qTCrS/3PrPXwV/blIOi46OJjuoa77ZtrJ5pOEWR6PRYoFJAtOzBz4Fi6AnflZT9EK LpLoEQ9wixJNngY0tyMVwLQx4Hw0DUBa9Ia3/Cj+f0T+l5FeHJCw7jwihAzJs2PDxFWK AizGzJZcqrO6aNJJ9Pucf6xkclINODsyT9twR0WQO1bfbl37J4yLjdC3F9oOj502jCXA nmAkA6pW4CTOxSW8nWU1QhjFxyuVrvOGDUJmOZiQ72IlCuZZo8aBcLEFaH1/93LvfTa9 YWTA== X-Forwarded-Encrypted: i=1; AJvYcCW22ktVbtPUDY8GLYaQ/MB18PBL+IIxY69RG9ajRCD/r9tOOepH/wZnyoqoHbXNI6/bei27ixiH5w3vTAs=@vger.kernel.org X-Gm-Message-State: AOJu0YxC1WkHrmDN33jq2Jk50fb3ifgbdMX+QSVD4x438z96rBZ4Bmne Eqc51VNCrLWEoer8v4kJ5kp3zx0s8682GZMw42F1xWi9OJ/fYUmz X-Gm-Gg: ASbGncs87LZnqlhlWB7e3upb3Pi6843nGIBfKItFnlj3YBhyHBRZ8nvhurvBPbtJXnc bVvIcPCilDJ1C6kn/kDJmtjxV4OK9MOumaKp72gQg9W9NPiDB/n0eeVi+ZYanJMNT7YHBCBpLe3 CUSiokeoYdnumcJ/dfTa5zkr1mSIIM90VVhG5lSktnvm6O2cSDYvlgEzCgVdChNe+xCzme19Khl ffD58khvljf1RmyTthpUR4o/5108DyI0XEwTEzAPOwOFqT/C8/5XUpsRJMW3FH+blYzoye8VP5E AiBqG3tjA8g0tjXzorSvu/gUf2VCtCnvmRfrTElone+zdR2JrNQhWtfRGiUHYQ== X-Google-Smtp-Source: AGHT+IHajbvnv5ZrxZSCdxKFEgjCaRuy+LYKxNNMB6GKx8nOcM6S+BvjyQcyE2PUEnc0O9YeaGDNrA== X-Received: by 2002:a17:902:d58d:b0:215:a808:61cf with SMTP id d9443c01a7336-21dd7829a0emr38336095ad.25.1738139310784; Wed, 29 Jan 2025 00:28:30 -0800 (PST) Received: from mm2dtv09.. (60-251-198-229.hinet-ip.hinet.net. [60.251.198.229]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da3d9c61bsm94154405ad.12.2025.01.29.00.28.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jan 2025 00:28:30 -0800 (PST) From: Kenny Cheng To: minchan@kernel.org, senozhatsky@chromium.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, c.s.cheng@realtek.com, Kenny Cheng Subject: [PATCH v2] zsmalloc: Fix address alignment in zspage for performance improvement Date: Wed, 29 Jan 2025 16:28:25 +0800 Message-Id: <20250129082825.3760555-1-chao.shun.cheng.tw@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The zspage consists of multiple objects, each containing a "link struct" to connect to the next object. The "link struct" is placed at the beginning of each object. On a 32-bit system, the size of the "link struct" is 4 bytes, which means the address returned by `zs_map_object` is always 4-byte aligned. For better performance, zram compression/decompression is offloaded to hardware designed by the IC vendor. For example, Realtek's hardware requires 16-byte alignment. However, due to the 4-byte alignment, a `memcpy` operation is needed to move data from the 4-byte aligned address to the 16-byte aligned address, which negatively impacts zram performance. This patch places "link struct" in the tail of the object that ensures the address returned by `zs_map_object` is aligned to the class size and DELTA, thereby improving zram performance by eliminating unnecessary memory copying. Signed-off-by: Kenny Cheng --- V1 -> V2: Fixed compiler errors mm/zsmalloc.c | 69 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 817626a351f8..b6436e109fd8 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -800,7 +800,7 @@ static unsigned long handle_to_obj(unsigned long handle) return *(unsigned long *)handle; } =20 -static inline bool obj_allocated(struct zpdesc *zpdesc, void *obj, +static inline bool obj_allocated(struct zpdesc *zpdesc, void *link, unsigned long *phandle) { unsigned long handle; @@ -810,7 +810,7 @@ static inline bool obj_allocated(struct zpdesc *zpdesc,= void *obj, VM_BUG_ON_PAGE(!is_first_zpdesc(zpdesc), zpdesc_page(zpdesc)); handle =3D zpdesc->handle; } else - handle =3D *(unsigned long *)obj; + handle =3D *(unsigned long *)link; =20 if (!(handle & OBJ_ALLOCATED_TAG)) return false; @@ -911,7 +911,12 @@ static void init_zspage(struct size_class *class, stru= ct zspage *zspage) struct link_free *link; void *vaddr; =20 - set_first_obj_offset(zpdesc, off); + if (is_first_zpdesc(zpdesc)) { + set_first_obj_offset(zpdesc, off); + off =3D class->size - ZS_HANDLE_SIZE; + } else { + set_first_obj_offset(zpdesc, off + ZS_HANDLE_SIZE); + } =20 vaddr =3D kmap_local_zpdesc(zpdesc); link =3D (struct link_free *)vaddr + off / sizeof(*link); @@ -1095,7 +1100,7 @@ static void __zs_unmap_object(struct mapping_area *ar= ea, off +=3D ZS_HANDLE_SIZE; =20 sizes[0] =3D PAGE_SIZE - off; - sizes[1] =3D size - sizes[0]; + sizes[1] =3D size - sizes[0] - ZS_HANDLE_SIZE; =20 /* copy per-cpu buffer to object */ memcpy_to_page(zpdesc_page(zpdescs[0]), off, buf, sizes[0]); @@ -1240,9 +1245,6 @@ void *zs_map_object(struct zs_pool *pool, unsigned lo= ng handle, =20 ret =3D __zs_map_object(area, zpdescs, off, class->size); out: - if (likely(!ZsHugePage(zspage))) - ret +=3D ZS_HANDLE_SIZE; - return ret; } EXPORT_SYMBOL_GPL(zs_map_object); @@ -1303,28 +1305,35 @@ EXPORT_SYMBOL_GPL(zs_huge_class_size); static unsigned long obj_malloc(struct zs_pool *pool, struct zspage *zspage, unsigned long handle) { - int i, nr_zpdesc, offset; + int i, nr_obj_zpdesc, nr_link_zpdesc; unsigned long obj; struct link_free *link; struct size_class *class; =20 struct zpdesc *m_zpdesc; - unsigned long m_offset; + unsigned long obj_off; + unsigned long link_off; void *vaddr; =20 class =3D pool->size_class[zspage->class]; obj =3D get_freeobj(zspage); =20 - offset =3D obj * class->size; - nr_zpdesc =3D offset >> PAGE_SHIFT; - m_offset =3D offset_in_page(offset); + obj_off =3D obj * class->size; + link_off =3D obj_off + class->size - ZS_HANDLE_SIZE; + nr_obj_zpdesc =3D obj_off >> PAGE_SHIFT; + nr_link_zpdesc =3D link_off >> PAGE_SHIFT; + link_off =3D offset_in_page(link_off); m_zpdesc =3D get_first_zpdesc(zspage); =20 - for (i =3D 0; i < nr_zpdesc; i++) + for (i =3D 0; i < nr_obj_zpdesc; i++) m_zpdesc =3D get_next_zpdesc(m_zpdesc); =20 - vaddr =3D kmap_local_zpdesc(m_zpdesc); - link =3D (struct link_free *)vaddr + m_offset / sizeof(*link); + if (nr_obj_zpdesc =3D=3D nr_link_zpdesc) + vaddr =3D kmap_local_zpdesc(m_zpdesc); + else + vaddr =3D kmap_local_zpdesc(get_next_zpdesc(m_zpdesc)); + + link =3D (struct link_free *)vaddr + link_off / sizeof(*link); set_freeobj(zspage, link->next >> OBJ_TAG_BITS); if (likely(!ZsHugePage(zspage))) /* record handle in the header of allocated chunk */ @@ -1422,6 +1431,11 @@ static void obj_free(int class_size, unsigned long o= bj) =20 obj_to_location(obj, &f_zpdesc, &f_objidx); f_offset =3D offset_in_page(class_size * f_objidx); + f_offset +=3D class_size - ZS_HANDLE_SIZE; + if (f_offset >=3D PAGE_SIZE) { + f_zpdesc =3D get_next_zpdesc(f_zpdesc); + f_offset =3D offset_in_page(f_offset); + } zspage =3D get_zspage(f_zpdesc); =20 vaddr =3D kmap_local_zpdesc(f_zpdesc); @@ -1556,6 +1570,12 @@ static unsigned long find_alloced_obj(struct size_cl= ass *class, void *addr =3D kmap_local_zpdesc(zpdesc); =20 offset =3D get_first_obj_offset(zpdesc); + + if (is_first_zpdesc(zpdesc)) + offset +=3D class->size - ZS_HANDLE_SIZE; + else + offset -=3D ZS_HANDLE_SIZE; + offset +=3D class->size * index; =20 while (offset < PAGE_SIZE) { @@ -1806,7 +1826,7 @@ static int zs_page_migrate(struct page *newpage, stru= ct page *page, /* the migrate_write_lock protects zpage access via zs_map_object */ migrate_write_lock(zspage); =20 - offset =3D get_first_obj_offset(zpdesc); + offset =3D get_first_obj_offset(zpdesc) + class->size - ZS_HANDLE_SIZE; s_addr =3D kmap_local_zpdesc(zpdesc); =20 /* @@ -1828,6 +1848,23 @@ static int zs_page_migrate(struct page *newpage, str= uct page *page, } kunmap_local(s_addr); =20 + /* + * Don't forget to check if there is an obj's link on the next page. + * If there is, it also needs to be handled. + */ + if (offset_in_page(addr) && get_next_zpdesc(zpdesc)) { + s_addr =3D kmap_local_zpdesc(get_next_zpdesc(zpdesc)); + addr =3D s_addr + offset_in_page(addr); + if (obj_allocated(zpdesc, addr, &handle)) { + + old_obj =3D handle_to_obj(handle); + obj_to_location(old_obj, &dummy, &obj_idx); + new_obj =3D (unsigned long)location_to_obj(newzpdesc, obj_idx); + record_obj(handle, new_obj); + } + kunmap_local(s_addr); + } + replace_sub_page(class, zspage, newzpdesc, zpdesc); /* * Since we complete the data copy and set up new zspage structure, --=20 2.34.1