From patchwork Wed Jan 29 08:28:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kenny Cheng X-Patchwork-Id: 13953527 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6624EC0218D for ; Wed, 29 Jan 2025 08:28:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2A2428003C; Wed, 29 Jan 2025 03:28:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CDADC280039; Wed, 29 Jan 2025 03:28:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7A6228003C; Wed, 29 Jan 2025 03:28:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9ACF3280039 for ; Wed, 29 Jan 2025 03:28:34 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1DB77160A4E for ; Wed, 29 Jan 2025 08:28:34 +0000 (UTC) X-FDA: 83059812948.16.F929A15 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 51B3040007 for ; Wed, 29 Jan 2025 08:28:32 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GuRXvJEK; spf=pass (imf04.hostedemail.com: domain of chao.shun.cheng.tw@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=chao.shun.cheng.tw@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738139312; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=MjpgunADEMuBU+/6HJpTGrs29am5J7k2GIEbNsY8OvA=; b=iJusv4HAAUR/TMucJQalO2LFUl4mbz1tdHIBU7pM2XdyJs0mQljA69zoT0fQIugNzAM0/E k+ipiARSS9Qciwd84kA8pCrdstP8/KQE0ZHjdicljePnQ5Zf/uaQM6g93cOxUToO1GZZcx xk3YfWAud8fAkhcL/b2ClOlzFfNxx/w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738139312; a=rsa-sha256; cv=none; b=BEw6nSmZk/4f6MAuEOIforYbJUeDWirBivL1Pu/WTsM+U8gPGJiQrnM+VLfau/4OTsSbP2 KSXb8eyYQnqNNcOpO2Df9e41vWO7iT3n9xLOehnle8ypV4uDwwkOQy30LnmbRGo3Hhd9bX BauIoC4de7uI8fcSgiHGc06dZrCTNaA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GuRXvJEK; spf=pass (imf04.hostedemail.com: domain of chao.shun.cheng.tw@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=chao.shun.cheng.tw@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2167141dfa1so7979375ad.1 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=kvack.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=GuRXvJEKqz5fhDQ3f4WwoFTnEuwMkUkNyflVr1TwQpw0FeSUp+2Tv1Oz5CyKCbarsc 7OhFTOx9MnDt7CCf8k9O3Yz5i1iBGir9YXyOarrGQCG8fUe6gKl2Dl5qOjLYR19sJ/pk 0H1gXY1o8ReaYbjiFrTSZmgX5EYWK2QqHoes2bpwMVHZC8dkzDumQ4e5LVTBKjAxpT5u edO73fxbKhd88O+l/RXukjPLaSZGODl3cFVEu0JW+BlQF0MIC+hzE4Wz7Fgl+x3aE2Zo Um8bxK+UeH0K/0QssTx6TAMmxNIGVoFT4fbHWZTiPOZ2GWy5OxaJPkcTqpARjnai5hoh UQcQ== 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=sQtsUKG8P3S7AALPJM6bTjDWyOSSohH7FfGh3spRtPXpZz8SWJwUimGlP6+Z9ZcLxa J/Fs53b8pPCngsZPPUNuhGSLxImxfiQ12xTNLDc/YD49ZfzRZ9U7yL+0iSQBFefkhHCc f/P18m+a3mRMwYgDdV0NdlcYF7J6J5/1OEdu5Mg349D5YaLsfx1umA5wUfhV4or8z+dK GRIl6IrAqHF5Wqcq/4F2W2bE26kOY+dxwOZsysTFeOCjYBEAjKKHMr0Er4rnNtDqyB1a yANYAmnXWH8OLxrX0SveP+CkJGrRvoSMXyKOliVpMfCepWi4MzvQdYy5aiWJhPU9rGbo WUKQ== X-Gm-Message-State: AOJu0YwqhbGGxr1U19N6dgawpWEd1RdEeS3D3sRHTGktpYViMBvvQ1ix G5Q+E9EgAGvxKPo6pIXpP3+hUtJ0MY0hezqfKUQAhTporP+C5U59 X-Gm-Gg: ASbGncuQZ8x16xfI7fiUVrPQxlE7wKlAwwFCofSCeDCRETLYq1Q/ApjJaXPycrFccbK iI2KX3rOSfZkxP3RFYVBUfi+KlFi2+woFmej5Y/ahz3Vez/GqLoacZvgDP90NWmDVDPXp8KYko8 aftrwhi3eG7zdJPu2k5h5qc53oeGnoMJDoNIa3/sb/fCG002TrQsRX6SJz3n0cdPbm9eGWxc98o KD9eQW3wgLskG69gmx52mp6jpH6oe4AKwqb+A9ZZQxEWEfNBvcf4Z1FjZXybCFnd0deRrR3bL4q XKrJ8EEzQ2DZMxQBeWwjfbqk4VK8z8ai9hFPJQaGqYtbww1R6+BYYtUkEM68dw== 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 MIME-Version: 1.0 X-Stat-Signature: crmqf9ijzfqkw48st7m8zxuxap5j51is X-Rspam-User: X-Rspamd-Queue-Id: 51B3040007 X-Rspamd-Server: rspam03 X-HE-Tag: 1738139312-211273 X-HE-Meta: U2FsdGVkX18eVVnjjkb1e56zvlcq+4TAwEM5qOsKh0SebVFNYzC+Ze8q5QrCCakUPnEYt6mdZ1+FYv1lBnuvwGwVSDjVBYVu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+G3eZUwxQsigUAhpzvS2EDVKfZ/9l7nrQjA9u/E212V4uiUhNJPoKoAIGZRSYvnLq4CUkqyYHuzYtBaVh5GViKIoGzs6XhuEshXrGuGPp0WhtH1BWJoDh8cxG9XjHehbreHYYzNgamj76rHeSwsb/jtWw0A0Byl9R77dwgy4BPKM8QrHc2TbiHsrbphCaXraz4KFXVONpiW4Xx46MYJcIxP2ombyL1XQQDdwhazi5LbFych0VlmvdxJ4nOJko4S9Z9+XahaZYIcKx8MDzz/QEpFd1T6hs333ipyeLkvx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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; } -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 = zpdesc->handle; } else - handle = *(unsigned long *)obj; + handle = *(unsigned long *)link; if (!(handle & OBJ_ALLOCATED_TAG)) return false; @@ -911,7 +911,12 @@ static void init_zspage(struct size_class *class, struct zspage *zspage) struct link_free *link; void *vaddr; - set_first_obj_offset(zpdesc, off); + if (is_first_zpdesc(zpdesc)) { + set_first_obj_offset(zpdesc, off); + off = class->size - ZS_HANDLE_SIZE; + } else { + set_first_obj_offset(zpdesc, off + ZS_HANDLE_SIZE); + } vaddr = kmap_local_zpdesc(zpdesc); link = (struct link_free *)vaddr + off / sizeof(*link); @@ -1095,7 +1100,7 @@ static void __zs_unmap_object(struct mapping_area *area, off += ZS_HANDLE_SIZE; sizes[0] = PAGE_SIZE - off; - sizes[1] = size - sizes[0]; + sizes[1] = size - sizes[0] - ZS_HANDLE_SIZE; /* 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 long handle, ret = __zs_map_object(area, zpdescs, off, class->size); out: - if (likely(!ZsHugePage(zspage))) - ret += 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; struct zpdesc *m_zpdesc; - unsigned long m_offset; + unsigned long obj_off; + unsigned long link_off; void *vaddr; class = pool->size_class[zspage->class]; obj = get_freeobj(zspage); - offset = obj * class->size; - nr_zpdesc = offset >> PAGE_SHIFT; - m_offset = offset_in_page(offset); + obj_off = obj * class->size; + link_off = obj_off + class->size - ZS_HANDLE_SIZE; + nr_obj_zpdesc = obj_off >> PAGE_SHIFT; + nr_link_zpdesc = link_off >> PAGE_SHIFT; + link_off = offset_in_page(link_off); m_zpdesc = get_first_zpdesc(zspage); - for (i = 0; i < nr_zpdesc; i++) + for (i = 0; i < nr_obj_zpdesc; i++) m_zpdesc = get_next_zpdesc(m_zpdesc); - vaddr = kmap_local_zpdesc(m_zpdesc); - link = (struct link_free *)vaddr + m_offset / sizeof(*link); + if (nr_obj_zpdesc == nr_link_zpdesc) + vaddr = kmap_local_zpdesc(m_zpdesc); + else + vaddr = kmap_local_zpdesc(get_next_zpdesc(m_zpdesc)); + + link = (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 obj) obj_to_location(obj, &f_zpdesc, &f_objidx); f_offset = offset_in_page(class_size * f_objidx); + f_offset += class_size - ZS_HANDLE_SIZE; + if (f_offset >= PAGE_SIZE) { + f_zpdesc = get_next_zpdesc(f_zpdesc); + f_offset = offset_in_page(f_offset); + } zspage = get_zspage(f_zpdesc); vaddr = kmap_local_zpdesc(f_zpdesc); @@ -1556,6 +1570,12 @@ static unsigned long find_alloced_obj(struct size_class *class, void *addr = kmap_local_zpdesc(zpdesc); offset = get_first_obj_offset(zpdesc); + + if (is_first_zpdesc(zpdesc)) + offset += class->size - ZS_HANDLE_SIZE; + else + offset -= ZS_HANDLE_SIZE; + offset += class->size * index; while (offset < PAGE_SIZE) { @@ -1806,7 +1826,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page, /* the migrate_write_lock protects zpage access via zs_map_object */ migrate_write_lock(zspage); - offset = get_first_obj_offset(zpdesc); + offset = get_first_obj_offset(zpdesc) + class->size - ZS_HANDLE_SIZE; s_addr = kmap_local_zpdesc(zpdesc); /* @@ -1828,6 +1848,23 @@ static int zs_page_migrate(struct page *newpage, struct page *page, } kunmap_local(s_addr); + /* + * 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 = kmap_local_zpdesc(get_next_zpdesc(zpdesc)); + addr = s_addr + offset_in_page(addr); + if (obj_allocated(zpdesc, addr, &handle)) { + + old_obj = handle_to_obj(handle); + obj_to_location(old_obj, &dummy, &obj_idx); + new_obj = (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,