From patchwork Mon Nov 23 20:14:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11926677 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70EB1C2D0E4 for ; Mon, 23 Nov 2020 20:35:37 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EEA2320715 for ; Mon, 23 Nov 2020 20:35:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Om28kFj3"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="Xdpxb1Vf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EEA2320715 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=lCq6GnRY3F53jm8I1QbX9RBjhYJWpAkeeDOxsJ1IhuY=; b=Om28kFj3LZJAbtw0Zj9je8sT+ 4pO80qbztcQDTVbi6NPQ7mr/MoWtSP8pJTw4EOZlf9RrSFgcN0m4O8g58CAWxjEQo4WRDVe/k3eGi XBJ60gu5ORQVPa4AkzTH9xgxD/4vnPcLCNuMcEFU1Urs62d7PA7EbTmtWr7dwEMw6PMnz1GqBCXtm YDO6Ulnqc4tM9b/Q71gDh+MvdeFEEOmYqetOYw4gA7dfzRjgGn3nH0hsMYn8rzw7VYMQ9At4pQnA5 7MiWVCkV5d6KfnUK0o3BrYxLJZWOWlfXMRMN3lD2nt/FN/uC5FsS3XXkPlwGzGp7Z/oDT4BEJ3+Br knPCGNkzw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1khIXE-00025s-7g; Mon, 23 Nov 2020 20:34:04 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1khIFB-0002Gi-VD for linux-arm-kernel@lists.infradead.org; Mon, 23 Nov 2020 20:15:40 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id cu18so13683699qvb.17 for ; Mon, 23 Nov 2020 12:15:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=S3Qrn/dExjiymjsgYUNfOEnR/LmRZMAIXzYoVslXjjw=; b=Xdpxb1Vf3s/kPw2pHNSS9JiFvoC2ZXTVqLGV29CN101YnRfcUJj54e13O+4shFzNRR UxJOJrDwgBms68V+NwgrrY57TQLSJEvHK/P93J5NSj47DQVWAHehli8I2fKm+zJJPseI DL5ssg+ltuq7C1TTwAD2z1DpU/DJvdCdIOygSieLe91+DwvRmebsYMl4yr/oD38kVwwr 9rUFFMTvdMatQepRl0EqTXJ5CGN9B2lWsJ/P/3Hu1bfQydR3x3KE0ir05X90+OrKNFaU 9lVY0TxUO2ZPp+UK87A2vvY6Cj4xos035dsYERhzZ0F3VH109GzKqChJQihY3dhXwauS r94A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=S3Qrn/dExjiymjsgYUNfOEnR/LmRZMAIXzYoVslXjjw=; b=iwxNrBloPD1T4rx93U1Z9/cGnaNSDQpNSai6Tl96UR8j4++w7ixeZVGnlv1q4Dm/HJ EfGODQclLXbTvUfPTQrhHzkTfZOKQU0wNyHbaAXk699nSCtRHWuZ4pC1gHWaVI6yaykq rjqohfOTAIzqMEBGa2+WJX6WEhR50ucxNK43tfi54elQGmBhRrgDVwvfODzfSqQtoHR9 t7MGnO0e2il26eQB8L0c9KRUagJsuhXxECi0Akn7KPIONBMiR22jwcB2tIvjWl83CV4i gsjdgYbD04bnOsB7NwwuVPWjfMhnabcn51zW7v9tTYyRbqxuS8D8/af2BZM8xINXdo3v IEYQ== X-Gm-Message-State: AOAM533o30DkylggygzbPKKIMptC+tCzqLXsxhQesQWfjfCsVMZZxT1M hJQaz8l2XoUXT1t4PTxtvFcEcB4BLip6Z5tI X-Google-Smtp-Source: ABdhPJzP0MC5E8U031n1ajvN8bRvma1gdRfUKfjzvSoZePlaiwYB+rq0QqGVBK+BNceQRzCZ0IBw7rLdYKvu+Rd/ X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:ad4:470d:: with SMTP id k13mr1306441qvz.40.1606162517097; Mon, 23 Nov 2020 12:15:17 -0800 (PST) Date: Mon, 23 Nov 2020 21:14:40 +0100 In-Reply-To: Message-Id: <7007955b69eb31b5376a7dc1e0f4ac49138504f2.1606162397.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH mm v4 10/19] kasan: inline (un)poison_range and check_invalid_free From: Andrey Konovalov To: Andrew Morton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201123_151526_672856_1E46088E X-CRM114-Status: GOOD ( 17.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, Marco Elver , Catalin Marinas , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Konovalov , Andrey Ryabinin , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Using (un)poison_range() or check_invalid_free() currently results in function calls. Move their definitions to mm/kasan/kasan.h and turn them into static inline functions for hardware tag-based mode to avoid unneeded function calls. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver Link: https://linux-review.googlesource.com/id/Ia9d8191024a12d1374675b3d27197f10193f50bb --- mm/kasan/hw_tags.c | 30 ------------------------------ mm/kasan/kasan.h | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c index 3cdd87d189f6..863fed4edd3f 100644 --- a/mm/kasan/hw_tags.c +++ b/mm/kasan/hw_tags.c @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -31,35 +30,6 @@ void __init kasan_init_hw_tags(void) pr_info("KernelAddressSanitizer initialized\n"); } -void poison_range(const void *address, size_t size, u8 value) -{ - /* Skip KFENCE memory if called explicitly outside of sl*b. */ - if (is_kfence_address(address)) - return; - - hw_set_mem_tag_range(kasan_reset_tag(address), - round_up(size, KASAN_GRANULE_SIZE), value); -} - -void unpoison_range(const void *address, size_t size) -{ - /* Skip KFENCE memory if called explicitly outside of sl*b. */ - if (is_kfence_address(address)) - return; - - hw_set_mem_tag_range(kasan_reset_tag(address), - round_up(size, KASAN_GRANULE_SIZE), get_tag(address)); -} - -bool check_invalid_free(void *addr) -{ - u8 ptr_tag = get_tag(addr); - u8 mem_tag = hw_get_mem_tag(addr); - - return (mem_tag == KASAN_TAG_INVALID) || - (ptr_tag != KASAN_TAG_KERNEL && ptr_tag != mem_tag); -} - void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag) { diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 7876a2547b7d..8aa83b7ad79e 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -3,6 +3,7 @@ #define __MM_KASAN_KASAN_H #include +#include #include #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) @@ -154,9 +155,6 @@ struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, const void *object); -void poison_range(const void *address, size_t size, u8 value); -void unpoison_range(const void *address, size_t size); - #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) static inline const void *kasan_shadow_to_mem(const void *shadow_addr) @@ -196,8 +194,6 @@ void print_tags(u8 addr_tag, const void *addr); static inline void print_tags(u8 addr_tag, const void *addr) { } #endif -bool check_invalid_free(void *addr); - void *find_first_bad_addr(void *addr, size_t size); const char *get_bug_type(struct kasan_access_info *info); void metadata_fetch_row(char *buffer, void *row); @@ -278,6 +274,45 @@ static inline u8 random_tag(void) { return hw_get_random_tag(); } static inline u8 random_tag(void) { return 0; } #endif +#ifdef CONFIG_KASAN_HW_TAGS + +static inline void poison_range(const void *address, size_t size, u8 value) +{ + /* Skip KFENCE memory if called explicitly outside of sl*b. */ + if (is_kfence_address(address)) + return; + + hw_set_mem_tag_range(kasan_reset_tag(address), + round_up(size, KASAN_GRANULE_SIZE), value); +} + +static inline void unpoison_range(const void *address, size_t size) +{ + /* Skip KFENCE memory if called explicitly outside of sl*b. */ + if (is_kfence_address(address)) + return; + + hw_set_mem_tag_range(kasan_reset_tag(address), + round_up(size, KASAN_GRANULE_SIZE), get_tag(address)); +} + +static inline bool check_invalid_free(void *addr) +{ + u8 ptr_tag = get_tag(addr); + u8 mem_tag = hw_get_mem_tag(addr); + + return (mem_tag == KASAN_TAG_INVALID) || + (ptr_tag != KASAN_TAG_KERNEL && ptr_tag != mem_tag); +} + +#else /* CONFIG_KASAN_HW_TAGS */ + +void poison_range(const void *address, size_t size, u8 value); +void unpoison_range(const void *address, size_t size); +bool check_invalid_free(void *addr); + +#endif /* CONFIG_KASAN_HW_TAGS */ + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations.