From patchwork Fri Nov 13 22:20:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11905083 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=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,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 77A7AC4742C for ; Fri, 13 Nov 2020 22:42:24 +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 D5D3A22252 for ; Fri, 13 Nov 2020 22:42:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="TV1RXTcJ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="a7iYcqhi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5D3A22252 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=5QBGk8vT0vl3i3smmf/++eZ2QIYF1hKynsAh+Y6gDtk=; b=TV1RXTcJa90wwVdivsCeyjrE/ IXT7SPe+xSz7VGKayRicyHC0bQgo5ZR442AKnjDZgxWxYffuLUr04Iw4pLmAhEwNLQ/8tAdHWDaYS sK/P1PAinh46NvpV4fp7Z+1X5wPx7fhrAdQD3SbLFWbU9vGo+07g9jgiidYILydbma2epEDv2eorY aqdDLcDZGfO9rodVWguF0yncJFOCbZxHkSWjKZPQcbVXMxUOY7+jX3lq7nUpVI1HAza7YbPljtxR5 aFSMb/PGNTDUGgcpTgvui9v8TW3Kf0o92BYRL5kG7PenP+ge9d6ddrSfLZwepd96+2IPEwsRg3KF2 IlbXQT1ag==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhkM-0002NX-DW; Fri, 13 Nov 2020 22:40:46 +0000 Received: from mail-qt1-x84a.google.com ([2607:f8b0:4864:20::84a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhQz-0001bc-MJ for linux-arm-kernel@lists.infradead.org; Fri, 13 Nov 2020 22:20:51 +0000 Received: by mail-qt1-x84a.google.com with SMTP id 7so6601992qtw.23 for ; Fri, 13 Nov 2020 14:20:41 -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=MeFL8PhZCdNKf3Y6a938/A3o+3e79v6OLkm/7y5InEs=; b=a7iYcqhiOEzmoQJIygPhyPCEebvODyOmH8298XwMFZzhnPBc8slvcHMG5pc3xrTPwr eo3pI3jbeiA0ddVRw38Om6NCXa3ppgNdxUTV5JllWdXhzXa8f1tU3INCEQzxkABBvlBc vtnGSglODeaGY/C7wpVTNORwBeFKYAp1vYDzJa/o+IL6JH1hbPvjh9in06nqJiut5VyS 6JrAOcbT8gr53bGUESlGwZGxkUrsI5Pl7Tmybny5kxg4WPgaJ/JzxHtj/bFIrxZgvugG Yw8nsDOV4uCmmz7n9MasLcCSQoxYvuI5QcL+o6R/KpNGKPwxb5KEoglhvBpsMtdv16WZ d7dQ== 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=MeFL8PhZCdNKf3Y6a938/A3o+3e79v6OLkm/7y5InEs=; b=ggPffKiIh/N0ya0jJC25MLOr783ng1G+S5Ur8PlBug52JHPXCginlqtrmw9Hc+rwFX ANMluflmX49Yu+UzvioIaSk7wj2FwEtjHx6bFUoRiLCd34sboAqyEof3GRtuNqfpkUPg Z7uSomTllUKQn2XI0Y4QK/gkbg4ieY27xDwYvs18wixle/381UGCxmajLmjyma6w4Ttd eZiXOeKyqObz8RDRi7a+hJHjNHGnoNHbtd2prgK2YpFgvncdc5sT68HLIFdQlLst6n65 UqQ6hWIfx8uwqF3/31pKmMtEukcLmK0yuEgvxCPEYcgnpbSlMumyL7cYP/+4biZseC1A ocYQ== X-Gm-Message-State: AOAM532U3kg4s5CdkvrvCGyABOh9b/BZna2COPaio+eR9RcleLtNcXjd a1DZ711ICQibdxNOSWqUJ28n0yoW6HS32NZq X-Google-Smtp-Source: ABdhPJxvwWRD6q+V00iGx8/Z5kxruSALO3/XNWuS6mhOxV+VJqQLBGqOmzGaQxVV5xj90d9/dy0xRas9NgWz59wT X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:b6c4:: with SMTP id h4mr4429589qve.35.1605306039132; Fri, 13 Nov 2020 14:20:39 -0800 (PST) Date: Fri, 13 Nov 2020 23:20:00 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH mm v3 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-20201113_172045_914604_A78EB136 X-CRM114-Status: GOOD ( 16.93 ) 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 Link: https://linux-review.googlesource.com/id/Ia9d8191024a12d1374675b3d27197f10193f50bb Reviewed-by: Marco Elver --- 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.