From nobody Fri Dec 19 20:00:37 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 4034C1891AA for ; Fri, 14 Feb 2025 04:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739508815; cv=none; b=YqlojN1BZgXmBbc1otxfx26scaGyAZ7zVr9EIBedrzeCnf9H3o3o+d5fXhUhtRCI2WksRTu6C4nYwo2ae1+SSExha1YsjN1X2TymLQeJEJJngYkMSwzqUpfRegZxmxG2PRuYDvyCSHasZ12QcThSAbHBhhscjhBDplYB0vWll9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739508815; c=relaxed/simple; bh=JVdpqsVkVNBsSrcDuQ+DKnxw+zDr/4gILM29OeA2KLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mhQ6LepMv4TIYD0CaKbMtktkMbq3D/cU9xMLlBpcAFbfZ8JRWlmBzFrtn1i6d+VOcURTXrVLTCa23Sc8Ea4Hf1kLqZEA1YimKkW/B04bEsxjXeXlFmONozu6c9zAmh4XCr6rEwO56eWLLd69u74Vi3EDbjSH7PuIU8YzJdfyFHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BRlo6Lsx; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BRlo6Lsx" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21f2339dcfdso25109815ad.1 for ; Thu, 13 Feb 2025 20:53:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1739508813; x=1740113613; 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=QjapF9ytftJrXMbeHEPYkCcc21hA6s0G0LeFdpvgJ8k=; b=BRlo6Lsx+CjifXz9kUcaF2clII+wXp+NoileBa9pijXCqllf0x0tfwlHu5e3zTxhj7 U4Q2U5GbsBzS/WwuRfpyuikwEDoV1mKVHKtwEnwVxbFzZnIBF3MIrzPhTjBYbhv7Ectm 8EMErG+331hNH47gqhRNYFWlAKZ/eE/zDWhF0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739508813; x=1740113613; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QjapF9ytftJrXMbeHEPYkCcc21hA6s0G0LeFdpvgJ8k=; b=CmxWz4nQrL+fdFDlzmFbRI/J2GyAdgVp9UAckkJ6jvjKkY8TAWXieJqIIeXH9bzKV/ 4W/hqoyVhpkTvzUFG0P3+jC5WzS4q6ITwByHyvVl1C/YOkn3KDiCYbq7DG1vuuxd4de5 wCLhyQ/CmvE15GG6IUKzgGte0oDr4Aiyvb6hM10cmSnfqgkHlDkxOkp5UnzHwhcm8sYZ MEBg/HFfuGnDqzh2KwSr1BSNfnYELESd5ShhxeGKLDY8BUwrLE1LyzpUgBNPSotbbOuO ee6NVdT21l3oMGj12KVqAh/WD78obM9RcWWqF86gnDAUYihIaXjy2Usd+rZW5sfkiZHH DY7w== X-Forwarded-Encrypted: i=1; AJvYcCWZTvaEGGis5O5wJ+aDpu8a13BY5qXtJR3lCHW65H5asBnhX9RmX9qaCH9mBdE04yFWRbg0+QahCZFV1k4=@vger.kernel.org X-Gm-Message-State: AOJu0YzAbGzYde35Y+fxnQi3NnQPX78G8Fb216cWEcZ3ZuVScVSEvm1S YcjR6MzTcldeKOz1HLzwpsSYx/d0Fqv4/5fTzrlFLEG0fiLJLqwZkjS4blPk5Q== X-Gm-Gg: ASbGnct3VyBvzZrdWIsZz1Fp/a9iYz7ez8Qfvw7bK1kIV1+fdb3LR8/p0ox+4ODRcvQ guvFyfRmAtNrdtV7/XsjscAvTf9Lc/uBwy9Aupbs1i2zX4GMrYQhtYjp+KqxFOUP9G6+eAhMNm/ wZLzh4q36KJZw1KGKsG+qw4iyec68JEhEAyrzarNyXYMclQrReDPLXHX+EHmzE7Ayeq1JBokzb6 TEBw5tWRlX8SKDm9tCnU3GpPkB6CnEocH/ShLBeE61mw92OU259lwMFpxmP44lnE67+L8PH/DSo HMso76hzTXa5Ee81nA== X-Google-Smtp-Source: AGHT+IFqSB8efCw3bZ9x/H3wNB/h7WT1Sozgx+u9wqyq8bV1nirLfbUOM2pE4SmRpm8pCa8TR9oGIg== X-Received: by 2002:a17:902:ef05:b0:220:e91f:4408 with SMTP id d9443c01a7336-220e91f487dmr29258795ad.22.1739508812669; Thu, 13 Feb 2025 20:53:32 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:942d:9291:22aa:8126]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2fbf98b4c45sm4198281a91.2.2025.02.13.20.53.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Feb 2025 20:53:32 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton Cc: Yosry Ahmed , Hillf Danton , Kairui Song , Minchan Kim , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCH v6 12/17] zsmalloc: introduce new object mapping API Date: Fri, 14 Feb 2025 13:50:24 +0900 Message-ID: <20250214045208.1388854-13-senozhatsky@chromium.org> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog In-Reply-To: <20250214045208.1388854-1-senozhatsky@chromium.org> References: <20250214045208.1388854-1-senozhatsky@chromium.org> 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" Current object mapping API is a little cumbersome. First, it's inconsistent, sometimes it returns with page-faults disabled and sometimes with page-faults enabled. Second, and most importantly, it enforces atomicity restrictions on its users. zs_map_object() has to return a liner object address which is not always possible because some objects span multiple physical (non-contiguous) pages. For such objects zsmalloc uses a per-CPU buffer to which object's data is copied before a pointer to that per-CPU buffer is returned back to the caller. This leads to another, final, issue - extra memcpy(). Since the caller gets a pointer to per-CPU buffer it can memcpy() data only to that buffer, and during zs_unmap_object() zsmalloc will memcpy() from that per-CPU buffer to physical pages that object in question spans across. New API splits functions by access mode: - zs_obj_read_begin(handle, local_copy) Returns a pointer to handle memory. For objects that span two physical pages a local_copy buffer is used to store object's data before the address is returned to the caller. Otherwise the object's page is kmap_local mapped directly. - zs_obj_read_end(handle, buf) Unmaps the page if it was kmap_local mapped by zs_obj_read_begin(). - zs_obj_write(handle, buf, len) Copies len-bytes from compression buffer to handle memory (takes care of objects that span two pages). This does not need any additional (e.g. per-CPU) buffers and writes the data directly to zsmalloc pool pages. In terms of performance, on a synthetic and completely reproducible test that allocates fixed number of objects of fixed sizes and iterates over those objects, first mapping in RO then in RW mode: OLD API =3D=3D=3D=3D=3D=3D=3D 3 first results out of 10 369,205,778 instructions # 0.80 insn per cycle 40,467,926 branches # 113.732 M/sec 369,002,122 instructions # 0.62 insn per cycle 40,426,145 branches # 189.361 M/sec 369,036,706 instructions # 0.63 insn per cycle 40,430,860 branches # 204.105 M/sec [..] NEW API =3D=3D=3D=3D=3D=3D=3D 3 first results out of 10 265,799,293 instructions # 0.51 insn per cycle 29,834,567 branches # 170.281 M/sec 265,765,970 instructions # 0.55 insn per cycle 29,829,019 branches # 161.602 M/sec 265,764,702 instructions # 0.51 insn per cycle 29,828,015 branches # 189.677 M/sec [..] T-test on all 10 runs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Difference at 95.0% confidence -1.03219e+08 +/- 55308.7 -27.9705% +/- 0.0149878% (Student's t, pooled s =3D 58864.4) The old API will stay around until the remaining users switch to the new one. After that we'll also remove zsmalloc per-CPU buffer and CPU hotplug handling. The split of map(RO) and map(WO) into read_{begin/end}/write is suggested by Yosry Ahmed. Suggested-by: Yosry Ahmed Signed-off-by: Sergey Senozhatsky --- include/linux/zsmalloc.h | 8 +++ mm/zsmalloc.c | 129 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index a48cd0ffe57d..7d70983cf398 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -58,4 +58,12 @@ unsigned long zs_compact(struct zs_pool *pool); unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size= ); =20 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats); + +void *zs_obj_read_begin(struct zs_pool *pool, unsigned long handle, + void *local_copy); +void zs_obj_read_end(struct zs_pool *pool, unsigned long handle, + void *handle_mem); +void zs_obj_write(struct zs_pool *pool, unsigned long handle, + void *handle_mem, size_t mem_len); + #endif diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index bc679a3e1718..51d8080f187a 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1432,6 +1432,135 @@ void zs_unmap_object(struct zs_pool *pool, unsigned= long handle) } EXPORT_SYMBOL_GPL(zs_unmap_object); =20 +void *zs_obj_read_begin(struct zs_pool *pool, unsigned long handle, + void *local_copy) +{ + struct zspage *zspage; + struct zpdesc *zpdesc; + unsigned long obj, off; + unsigned int obj_idx; + struct size_class *class; + void *addr; + + WARN_ON(in_interrupt()); + + /* Guarantee we can get zspage from handle safely */ + read_lock(&pool->lock); + obj =3D handle_to_obj(handle); + obj_to_location(obj, &zpdesc, &obj_idx); + zspage =3D get_zspage(zpdesc); + + /* Make sure migration doesn't move any pages in this zspage */ + zspage_read_lock(zspage); + read_unlock(&pool->lock); + + class =3D zspage_class(pool, zspage); + off =3D offset_in_page(class->size * obj_idx); + + if (off + class->size <=3D PAGE_SIZE) { + /* this object is contained entirely within a page */ + addr =3D kmap_local_zpdesc(zpdesc); + addr +=3D off; + } else { + size_t sizes[2]; + + /* this object spans two pages */ + sizes[0] =3D PAGE_SIZE - off; + sizes[1] =3D class->size - sizes[0]; + addr =3D local_copy; + + memcpy_from_page(addr, zpdesc_page(zpdesc), + off, sizes[0]); + zpdesc =3D get_next_zpdesc(zpdesc); + memcpy_from_page(addr + sizes[0], + zpdesc_page(zpdesc), + 0, sizes[1]); + } + + if (!ZsHugePage(zspage)) + addr +=3D ZS_HANDLE_SIZE; + + return addr; +} +EXPORT_SYMBOL_GPL(zs_obj_read_begin); + +void zs_obj_read_end(struct zs_pool *pool, unsigned long handle, + void *handle_mem) +{ + struct zspage *zspage; + struct zpdesc *zpdesc; + unsigned long obj, off; + unsigned int obj_idx; + struct size_class *class; + + obj =3D handle_to_obj(handle); + obj_to_location(obj, &zpdesc, &obj_idx); + zspage =3D get_zspage(zpdesc); + class =3D zspage_class(pool, zspage); + off =3D offset_in_page(class->size * obj_idx); + + if (off + class->size <=3D PAGE_SIZE) { + if (!ZsHugePage(zspage)) + off +=3D ZS_HANDLE_SIZE; + handle_mem -=3D off; + kunmap_local(handle_mem); + } + + zspage_read_unlock(zspage); +} +EXPORT_SYMBOL_GPL(zs_obj_read_end); + +void zs_obj_write(struct zs_pool *pool, unsigned long handle, + void *handle_mem, size_t mem_len) +{ + struct zspage *zspage; + struct zpdesc *zpdesc; + unsigned long obj, off; + unsigned int obj_idx; + struct size_class *class; + + WARN_ON(in_interrupt()); + + /* Guarantee we can get zspage from handle safely */ + read_lock(&pool->lock); + obj =3D handle_to_obj(handle); + obj_to_location(obj, &zpdesc, &obj_idx); + zspage =3D get_zspage(zpdesc); + + /* Make sure migration doesn't move any pages in this zspage */ + zspage_read_lock(zspage); + read_unlock(&pool->lock); + + class =3D zspage_class(pool, zspage); + off =3D offset_in_page(class->size * obj_idx); + + if (off + class->size <=3D PAGE_SIZE) { + /* this object is contained entirely within a page */ + void *dst =3D kmap_local_zpdesc(zpdesc); + + if (!ZsHugePage(zspage)) + off +=3D ZS_HANDLE_SIZE; + memcpy(dst + off, handle_mem, mem_len); + kunmap_local(dst); + } else { + /* this object spans two pages */ + size_t sizes[2]; + + off +=3D ZS_HANDLE_SIZE; + sizes[0] =3D PAGE_SIZE - off; + sizes[1] =3D mem_len - sizes[0]; + + memcpy_to_page(zpdesc_page(zpdesc), off, + handle_mem, sizes[0]); + zpdesc =3D get_next_zpdesc(zpdesc); + memcpy_to_page(zpdesc_page(zpdesc), 0, + handle_mem + sizes[0], sizes[1]); + } + + zspage_read_unlock(zspage); +} +EXPORT_SYMBOL_GPL(zs_obj_write); + /** * zs_huge_class_size() - Returns the size (in bytes) of the first huge * zsmalloc &size_class. --=20 2.48.1.601.g30ceb7b040-goog