Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5904439rdb; Thu, 14 Dec 2023 03:08:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIin3jrxBvOlNzoxDEvZQ6W4q7FQzX8Kciaivis/+dDj025q+GeV0jBVqj/pl6O7cqPt40 X-Received: by 2002:a05:6a20:2d90:b0:18f:97c:9298 with SMTP id bf16-20020a056a202d9000b0018f097c9298mr4556731pzb.125.1702552089949; Thu, 14 Dec 2023 03:08:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702552089; cv=none; d=google.com; s=arc-20160816; b=cf/eTtoTQDkf8dS3nfjBiK52JGusnWDKwmPcfFauJM37viCDmCl87FOLruSR/gGe76 mBm97Wvyp/jABYruPHXlxkgpKyCg4lKCd7GxqaVgzB3vzzww4Va2ImM38vVSk7HCCwjs 2SKLP0yfrHItsBoFMNjj8PJiv7wsSavvm+bUUTI7BWyLhUUCY7VxXvZ2mv97p0lqMB+N 1aGpNtVgA6c/bescttc2V9FKQiUgZlelaDRTxRbHcDv98XfRhvAVyBXAzdXRRCjFCTFZ snFOrQ5m3JRAMk3EY7r1N/EMcTDr51MMy+Jn/4+/oXFLKSVs71aGifpIjFUXBrmPlJ8Y mRkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=qETeNVW4T3lC++QM3orD9LTmmtp3iyUET5aUKhJnmac=; fh=bwsiIJU4ZySHCYgdbJrawkDYkx7XsqbTjYxIEnch01E=; b=wvACoMK9TSCw415UE6c0AFHWOqXKPZo7WzOVCrZg7Q7vEA9Erch7dCkdvMu/72Yg1Q PKF+lLxmhyo4kWbSr6eQ1ETWNGRjgGO2ChM7SYaxpZmZ/TJSv4+WBqQJm/CWdvXVf2rM QmtbmRwUVL64WKfEgQN9+m+FQH2DEuL7FK1+J0ie01o5euoW1pKwsPgjT1/WXJpOpWop 1/lvG9mHxg4Zikq0CIDQHaVVwlUQela3PHcE2gcqh+n0uToQkOwre98j+WfOPD+TzojA 3ZNKhLu2fUXeqFyRM16ofeNQ/r7kcV4ss6mAeZsGlTLo4XwcdVRjskud0KSulG7WvjbW 4v2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ojei8kpb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id s36-20020a056a0017a400b006bca1246564si10913112pfg.271.2023.12.14.03.08.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 03:08:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ojei8kpb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id B29378326692; Thu, 14 Dec 2023 03:08:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444078AbjLNLHd (ORCPT + 99 others); Thu, 14 Dec 2023 06:07:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443986AbjLNLHP (ORCPT ); Thu, 14 Dec 2023 06:07:15 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 761001B5 for ; Thu, 14 Dec 2023 03:07:03 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5caf61210e3so89343197b3.0 for ; Thu, 14 Dec 2023 03:07:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702552022; x=1703156822; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qETeNVW4T3lC++QM3orD9LTmmtp3iyUET5aUKhJnmac=; b=ojei8kpbI+wY1OznPPHL2kaANXhM6JnJeL8IVtH12058RZvTm/bD64zV5wnDJLp/Qa A9eJcQZgHE+ilUO+CXFqSAvIgL1UPf5gRR3sWomh2OxBDma74AvUVUUnaauR6F7M4k3c HPI1CwiTW8sDyIX3WImfL2kv2cs+boLr/BXh798bIaWMSwXiVjwiqeu+0nqKF+JmNr84 UYL5WZ7GKN2jO5g/pkmeNv3rER3pFo0+7/o7Xc8gDnTXLjrceDiGO51zYES0Eaj82fxS nt/tOp1O5tRJydnuwiMtmvNtQvIyn6M4iu/TIDbwBirGdXl5i9THAt9L9bLI8LNQXEnn pgZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702552022; x=1703156822; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qETeNVW4T3lC++QM3orD9LTmmtp3iyUET5aUKhJnmac=; b=aIPhjAMZazVUJRQRCsRVSY5aqn4GiRrmQONGthkJk+qpZ7Yq6j1wbIhacl5wfjuoyw Hu8qzf8a0nD056WP9NsQUdmsgxTP+bVO+h9nKdUCJln4gJR3No8DLhAEbymZQEQIfROL rMtD6mkl7W1/5xp+4VNqXjo0N1144xfiSDOVXcqSqD2KGbDIs4zZgxSwKXo2f7yZ55bg uu5ElxnViq+lwsqSjF6AhxUDVYGFPPh9in6xzTUp0n3dM3F+SaAFZIxeyqdq/2A9Ounl 5FhZWFngvgQLGi2cOS2mC0lSAdqvoE9QwVpaOICtQjAu+WZ1xsklfamAOn2fPI9aX+Eu zo4Q== X-Gm-Message-State: AOJu0Yz5ERAcWU+0YLnuy9EdRSMNo3REiua95y3ASPXbYmfZrIYayAUG GRxHqlN1VANhtmuSfwDRhSiBbb7crzE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:8447:3e89:1f77:ff8a]) (user=glider job=sendgmr) by 2002:a05:690c:a81:b0:5df:4a9b:fb6c with SMTP id ci1-20020a05690c0a8100b005df4a9bfb6cmr109550ywb.3.1702552022462; Thu, 14 Dec 2023 03:07:02 -0800 (PST) Date: Thu, 14 Dec 2023 12:06:38 +0100 In-Reply-To: <20231214110639.2294687-1-glider@google.com> Mime-Version: 1.0 References: <20231214110639.2294687-1-glider@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231214110639.2294687-7-glider@google.com> Subject: [PATCH v10-mte 6/7] arm64: mte: add compression support to mteswap.c From: Alexander Potapenko To: glider@google.com, catalin.marinas@arm.com, will@kernel.org, pcc@google.com, andreyknvl@gmail.com, andriy.shevchenko@linux.intel.com, aleksander.lobakin@intel.com, linux@rasmusvillemoes.dk, yury.norov@gmail.com, alexandru.elisei@arm.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, eugenis@google.com, syednwaris@gmail.com, william.gray@linaro.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 14 Dec 2023 03:08:05 -0800 (PST) Update mteswap.c to perform inline compression of memory tags when possible. If CONFIG_ARM64_MTE_COMP is enabled, mteswap.c will attempt to compress saved tags for a struct page and store them directly in Xarray entry instead of wasting heap space. Soon after booting Android, tag compression saves ~2x memory previously spent by mteswap.c on tag allocations. On a moderately loaded device with ~20% tagged pages, this leads to saving several megabytes of kernel heap: # cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368 (statistics collection is introduced in the following patch) Signed-off-by: Alexander Potapenko Reviewed-by: Catalin Marinas --- v10-mte: - added Catalin's Reviewed-by: v9: - as requested by Yury Norov, split off statistics collection into a separate patch - minor fixes v8: - adapt to the new compression API, abandon mteswap_{no,}comp.c - move stats collection to mteswap.c v5: - drop a dead variable from _mte_free_saved_tags() in mteswap_comp.c - ensure MTE compression works with arbitrary page sizes - update patch description v4: - minor code simplifications suggested by Andy Shevchenko, added missing header dependencies - changed compression API names to reflect modifications made to memcomp.h (as suggested by Yury Norov) v3: - Addressed comments by Andy Shevchenko in another patch: - fixed includes order - replaced u64 with unsigned long - added MODULE_IMPORT_NS(MTECOMP) --- arch/arm64/mm/mteswap.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/mteswap.c b/arch/arm64/mm/mteswap.c index a31833e3ddc54..70f5c8ecd640d 100644 --- a/arch/arm64/mm/mteswap.c +++ b/arch/arm64/mm/mteswap.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include "mtecomp.h" static DEFINE_XARRAY(mte_pages); @@ -17,12 +19,13 @@ void *mte_allocate_tag_storage(void) void mte_free_tag_storage(char *storage) { - kfree(storage); + if (!mte_is_compressed(storage)) + kfree(storage); } int mte_save_tags(struct page *page) { - void *tag_storage, *ret; + void *tag_storage, *compressed_storage, *ret; if (!page_mte_tagged(page)) return 0; @@ -32,6 +35,11 @@ int mte_save_tags(struct page *page) return -ENOMEM; mte_save_page_tags(page_address(page), tag_storage); + compressed_storage = mte_compress(tag_storage); + if (compressed_storage) { + mte_free_tag_storage(tag_storage); + tag_storage = compressed_storage; + } /* lookup the swap entry.val from the page */ ret = xa_store(&mte_pages, page_swap_entry(page).val, tag_storage, @@ -50,13 +58,20 @@ int mte_save_tags(struct page *page) void mte_restore_tags(swp_entry_t entry, struct page *page) { void *tags = xa_load(&mte_pages, entry.val); + void *tag_storage = NULL; if (!tags) return; if (try_page_mte_tagging(page)) { + if (mte_is_compressed(tags)) { + tag_storage = mte_allocate_tag_storage(); + mte_decompress(tags, tag_storage); + tags = tag_storage; + } mte_restore_page_tags(page_address(page), tags); set_page_mte_tagged(page); + mte_free_tag_storage(tag_storage); } } -- 2.43.0.472.g3155946c3a-goog