From patchwork Wed Jan 29 07:18:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kenny Cheng X-Patchwork-Id: 13953484 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 73E82C02192 for ; Wed, 29 Jan 2025 07:19:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD591280032; Wed, 29 Jan 2025 02:19:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A85FC280029; Wed, 29 Jan 2025 02:19:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 924DB280032; Wed, 29 Jan 2025 02:19:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 74FF8280029 for ; Wed, 29 Jan 2025 02:19:12 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1C5CA809BA for ; Wed, 29 Jan 2025 07:19:12 +0000 (UTC) X-FDA: 83059638144.07.DB81CB1 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf09.hostedemail.com (Postfix) with ESMTP id 5570E140002 for ; Wed, 29 Jan 2025 07:19:10 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dESuOIJF; spf=pass (imf09.hostedemail.com: domain of chao.shun.cheng.tw@gmail.com designates 209.85.214.171 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=1738135150; 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=Nax48tkolk8BT7eFMo189OaiWR8OHkXF1xwCG4eXbvo=; b=g8sy2K7bI6kcyi4E3H2h186KnyUknKLDJS5LbFiEW0+/5yDbE8Gz9oDEyQoSOPv2Q4fS1V ZXK+DPW+OmfAtyZhUT8j6tAo4uEE5Q7gYM5jMt6hUmz4lchkYNzz6Zt0HUU83W+6g6R9bY ht5PNIpeKUsldUptUIyoZyjCOI6XvdE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dESuOIJF; spf=pass (imf09.hostedemail.com: domain of chao.shun.cheng.tw@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=chao.shun.cheng.tw@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738135150; a=rsa-sha256; cv=none; b=vh89EYyjIC/WM06LzOE/DoYfk2IDTnP3Us+gyJTFX+nzTPy5qxhJA9RQWWKa4avon+HepQ kZj6AXky2R4IEKpmh8r3JnalCjb6T32c/OgKGLKsQ7yT1vtUE8R08Biou1eAh/A+/KmvDA qeT+DJJ6aRj/ggAGEHsx/IUWYx2/06g= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2156e078563so92033595ad.2 for ; Tue, 28 Jan 2025 23:19:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738135149; x=1738739949; 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=Nax48tkolk8BT7eFMo189OaiWR8OHkXF1xwCG4eXbvo=; b=dESuOIJFfhNxBc9Bh165+PtK5CLQ/OzHyMl22DB2RgboBrYYPYwMttCnmAEF8yqpl6 xX7OYpkiPXd9MzS+37hcWCvwXO9ROBM6d/C7tjOSzZywMA6c3aAYsBun4nBnVM0gc6VU fU3e0keenFZyNxFidKOTp+Y4dMi0NtGcCngYoMHj0sLCT3Q9NDiY6z6oKvy0a0U6xPGi dsJP1CuNCsI/0fzGYFkw6xBGv/hkDuHfYuCm8FgYNUbajfH4r/Ktlbh4y5P88QWVLnVi toB+tOvU9nV62uFanXqgffCjgV5i0XQDqCRsrP0sAuDfGAHNH2opQlW2o9d6ef37mJnh 7FTA== 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=kzNQwACIbtcrVy4wEcnuMZmeK6A4BcdF+cJdizq8xmyE/2uvGBgwboBud+ekKLSZ2V GtJuM/9KbTuA+yBlOqcED/AZ1e48whMONIZd8oflvRUf431wjhgRyBAzvxnzqUblF4Hq AwrPdAlCXk3uPhTVqsqW56OJVDX8Z8iP4QWqUVQqGVJyy6KT5JOla5aDPVud+REoIQd0 crMyfIvEH/cPSm6Q8XaVuhnlT4khlwSKz1OMVmIGlfGILRDZ2OgzI/Lqp4i5QPIG3hNl ayOtJ6S64Vly+asG2Gf0/YfnBWl5PbSiBbl9NgUkT8KkRJwpg9MwE83kOWkjqGc6KtjB JGxw== X-Gm-Message-State: AOJu0Yy/7rHNDXz7neTMAMxpYnQIidP/JlPahGA+eHsrYyhYgtgcEkek KMkSc2p6AcjbDU7A/xmwFZ4KwUVHGizgSdyNhm5PfH+uZ1JGcmDC X-Gm-Gg: ASbGnct05dVoZFEU2p3Puw2Q/WVsqoJbMz9qojUrWuf1uRQ/2wXRSX8bqoEqnSYWwrU baCx18fl3Pnqf/blxkBluf618RDzfHeR4s5QUU1LDRyDyUeWUx1jH/Y0jS4cD1fA7QRtolRSXcu B1GY4ip8okQ1A+wO4WRkkL+4hhiSTpJk2FCiB37EuP0Y+4pBEybpCIzoNl1f8GJBZlc3EeYCqqL D6WMXzQ1LXxB4bnN3VrA04ChYCIUpkkTf6godK7vz9zL8LGtcQpJKTLvw0861FoQPvohGOId7gj iaduo3QhvtY4Jq1cDFgtCPgBXfLHcmkgz2Z4vxS72divTgPZYETZsFOwSTLeS5l7tdJyKKn3 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 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5570E140002 X-Stat-Signature: 57iysja6gq16tnw3h5udfjpunpfaeb47 X-Rspam-User: X-HE-Tag: 1738135150-106677 X-HE-Meta: U2FsdGVkX18pO7N1EMaQgJZETx9xDJaXZ9LDqMq8sIs3Ul/dx43G01YeLQ4pAZs9Q+udJricgvNwxcTdKY+LqeGCZsn3Y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z6s0smG/Bg4FsCWAaH490aMYaCFMZp1Gs9kWM/U7aEUySKBJFXef7EV8mT2uu8hlKEEyhirC6GxuXgn50R0UnlQJg65KEAy2MbMwuZdmx0uzUfYHAXdFUeFvqN21EFo0fWcRBNl2xWmznPfc8dOSjnzvU+zTUcfXqcNuLlMevNgHkTFmAH1hDQ8Gnu+2tOvr4Hde1ClAom1/Koyz/9RtDF8sBis9TN4Yb7W2KZjS32AaZvoNVfR6EMt2dm+wtY7L+N1LY/EVpHeboA77UcbnKpW0jbFI0t61A1t7nLwOVCHM X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 --- 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; } -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(offset); 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,