From nobody Wed Feb 11 08:33:21 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 9C3E7191F95 for ; Wed, 29 Jan 2025 07:19:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738135152; cv=none; b=YiDdd8PjFPfE3tdbkpMKu5/mjScMfdMZPyMP52RWvuKp2H/lK8Z+n7PjwXarQfl04komSaEodvurWNYofBN2+TdAlBr9dcNaiW+mNPHJtyXtNw2GQsSbhnFgFr4rHW99IrvHIEAKVNBFyTaDvxCi/73/s0gZksDIcfPXIrmHJAc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738135152; c=relaxed/simple; bh=kolr+LxOFieZpnISN7erjud4lXWfysg/9ufcX4Pbho8=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=QZKLXj1oahXXkRdRfKXtCbAuQx6whBPe8ATkeeiEQZEQuG44CCidWIDduLENxRxRakN+4fN9KWeIiZ2aGGmJ1i22QIJNQVC/+b+xjG/M+twfIwCs80BynK+3tgE3gIk0YL3iR3lamUZVjfI1MMw5YDfXyyn0Z2xHs2xyqTmV67c= 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=WXD2SCW9; arc=none smtp.client-ip=209.85.214.171 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="WXD2SCW9" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2166360285dso110197215ad.1 for ; Tue, 28 Jan 2025 23:19:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738135149; x=1738739949; 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=Nax48tkolk8BT7eFMo189OaiWR8OHkXF1xwCG4eXbvo=; b=WXD2SCW9nikD3MIawH2Dssc6e0LGAsiyWdsgloPY40pxxsKPNVknVIZHm1W1dr+R9C cuWG3MynsBk7CWtXYA1fwkNOLyCEP7mVui4o927hjdRY8drbJ2zR1rwLkJ2P/hRe9vEL Y4n+17d8Qpd8V0eTDZ5p+W2Xx7x/aX4uODxr5s7iZFmViKa1op8qsTjLuoh0SSvc+QIc jThpQ0VamlvES3fIU6dIR7Mm/ePXDYlzx8ukd9bZp8OOvQGeyeGmPvpIx+8t7swzlY+M 4ibhLAL6rtaLCEmzJ50tYfd53966h6s/8Bfkk3anZnHi5K24yBA8LY8eHRitQ6/dlUZJ JUaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738135149; x=1738739949; 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=Nax48tkolk8BT7eFMo189OaiWR8OHkXF1xwCG4eXbvo=; b=oLSnbZkDQjmf+7+QLvnGXyWUioU8k8Q7XU21alYdLE/Ma5tDUQ5yC9D5/Iws8E+R/Q +wgHMIDssOeETJjZoCg1EmhApojzQkqBiOW/DtJDtL3uEbL+mCF1wdJT3rN0NBCadep+ GQCOagIZRt63i3oNwJzDo/ptp7fGO0aa33Leq7iziiQY8krTTW81Mqz6CxA+izOV343j cLHhTGf6kFzxsGOOOMSynuEtU2pXPehIZOq7Xgai1vZ+QNhDRAK73LLBoLWitVTo/kS/ kJo+w6gFdyZiGFQorFk7eeZNcJFrSW18Vwguv83MMDfCYJSbV80vs5mX5MuUlDdfbGnQ MZ3A== X-Forwarded-Encrypted: i=1; AJvYcCUikTfJcLrmgAP6/b3TT9vfC4PhInxta4iwFqFttia3maiwjgPQgeenhReXfVS94+9WarB0CaOgCQRRHzo=@vger.kernel.org X-Gm-Message-State: AOJu0Yxp2XnFhCB3qJJOx6tgQiUujFnYoUZFT4llrp0NoFD+XvQ4RGuR Qxy7vQDRU5qx3hCtpC7uonZP7TrHdXkMXFWbsjw9olK9ghNUkOOh X-Gm-Gg: ASbGncuDIjpjYMZ6ydShJ7chaWmil2zIhMoGcCff/+9zKEG0NxxRvf85NyBeI3jc8pi QKGfqju78YYuH0xMCDgwDYCvBGXiKCzIu7xxuVXoRodg12QHNUc5T8WJmW1gfHprWI3zZfbXQD/ hI3h5qNCgZBdcRyJhBcj/E6dDbLEQbaYdmQFkp7Nf8yeI1QOpQSNsM5zsgSHa6S27i60usYpu3V wZMqtEPggrehVUzkoGr2JEghm+90n4sEKSB5M9YI2OKQfWJ94jwEXMnNKiXZ1d0d2EKVHTBGlVI H6eg4/sDfJfixVJgBUeHRAa4kVQ3LNFu4T6brdhHHBLtjkwCXBsKyE1WN6uaK7wnhqGVPr60 X-Google-Smtp-Source: AGHT+IEjoPr6fHBvyhK0LfaLjEN06QpbjPl5KVe6zMtZhQF4zeSd362RlngZOZxYiaqBl6sr6XKvtQ== X-Received: by 2002:a17:903:2446:b0:216:48f4:4f1a with SMTP id d9443c01a7336-21dd7c62be9mr32304825ad.16.1738135148842; Tue, 28 Jan 2025 23:19:08 -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-21da3d9dfaasm93657245ad.45.2025.01.28.23.19.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jan 2025 23:19:08 -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 v1] zsmalloc: Fix address alignment in zspage for performance improvement Date: Wed, 29 Jan 2025 15:18:29 +0800 Message-Id: <20250129071829.3621449-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 --- mm/zsmalloc.c | 69 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 817626a351f8..0d14bc8a8a6a 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(offset); 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