From patchwork Tue Nov 10 22:10: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: 11895511 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C351315E6 for ; Tue, 10 Nov 2020 22:11:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6A30C20781 for ; Tue, 10 Nov 2020 22:11:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="fVXSSC7c" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6A30C20781 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 311CF6B0071; Tue, 10 Nov 2020 17:11:15 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 24F3E6B0072; Tue, 10 Nov 2020 17:11:15 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 029406B0073; Tue, 10 Nov 2020 17:11:14 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id C5E846B0071 for ; Tue, 10 Nov 2020 17:11:14 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6DFBD1EF3 for ; Tue, 10 Nov 2020 22:11:14 +0000 (UTC) X-FDA: 77469905268.22.rake00_0a03e78272f8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 4545D18038E6B for ; Tue, 10 Nov 2020 22:11:14 +0000 (UTC) X-Spam-Summary: 1,0,0,401216c09c1571cc,d41d8cd98f00b204,3arcrxwokcnaw9zd0k69h72aa270.ya8749gj-886hwy6.ad2@flex--andreyknvl.bounces.google.com,,RULES_HIT:2:41:69:152:355:379:541:800:960:966:968:973:988:989:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1593:1594:1605:1730:1747:1777:1792:1981:2194:2196:2199:2200:2393:2559:2562:2693:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3870:3871:4049:4120:4250:4321:4385:4605:5007:6261:6653:6742:7903:8603:9036:9592:9969:10004:11026:11232:11473:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:14096:14097:14394:14659:21080:21324:21365:21444:21451:21627:21740:21987:21990:30012:30054,0,RBL:209.85.222.201:@flex--andreyknvl.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100;04yfzjyghpdguh3ymyr34bw5r589myc1d6ysfbkihmheiaanpo8qxnec4s8rcwj.cuig84ojm6a5s5osnocridsauea3ynbic86597dnmz7gxjyher7ghcy13hfu3bz.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not b ulk,SPF: X-HE-Tag: rake00_0a03e78272f8 X-Filterd-Recvd-Size: 9544 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 22:11:13 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id 141so170735qkh.18 for ; Tue, 10 Nov 2020 14:11:13 -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=XraCnBPyNFRl2jORnfbsPA7ISP+sfWOQd9nhL08xkeo=; b=fVXSSC7cfeCDIfU41Noa0mJO8puvATk0+IQ4GhtqI2Kq4FB/275whIS09Hnd8/7oTJ NdVOcz/dkJgeIF1pY46dpH6Jj3b5CqOqH/QYDoTURAeUT8jz0gBtWvpGvR3ilgTL4WyX 6YzRxQel/PJKBd2LrP9+LzfZWZwul4Dg4y/dh7GKFZQ0vRfouKsYgXfoEuip6IHKej/X /izMOn2OuwoBWhlv5sv3ChsDWFgRQRmYw/bWIwyart2srj8DhlhT/GKVv0I2kQkj40cp Scbk1AJAw8aO5Q74SeIgaNPG+pMXEjlWxzeCjkjrppK1xzZT0dtDXQ42pjHwqMuY3sv0 nR+g== 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=XraCnBPyNFRl2jORnfbsPA7ISP+sfWOQd9nhL08xkeo=; b=YmBQP+xTt9PjcNCBUlGxJ8LmLqad4ym7WVQoTq8k1TSZ4hcW/Da6aX63AFOq94AvC6 wDoGd/3ld6GiZ+xEbMN/Gd0hxMW92vxS294W7swBKSWN0GJ2LXScGRNymDYXx46S1S4G D3Pd5Oh7webzg5kaSPMxGHipYvhBl/bzuU/JlTV26h26cFWSWlhMu6tzw7UpTRx8tOJ0 wffeDZAd79ciWndiaxBbcUZBax1Kq1aspgAfQfmggN22bzGQwK5DcbAW1u903zmErvzz uBZiQ1WlsV+AWII1z1jbrmOh5+deDHpIojR63vM6kqtiFYQz7QPk7FRjDzzLV8Iof8O+ BQyA== X-Gm-Message-State: AOAM532qv27jE7+FQHI1zEhqVgi0r4RwshHFfYkfvduM3DLAo2ZihRmf 1lL0iOpkNmkYu/84G0tMJ1mm1w6Xt7Yuzi9C X-Google-Smtp-Source: ABdhPJzSxoGNsPcu4UF+qbJOdNFXdI7X5DGRcZEkljCnpOt/+/SDKbkTVRFUVO7+heoNmP1YTCSqeQpv/X5xNGFj X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:ad4:4303:: with SMTP id c3mr22199156qvs.13.1605046273097; Tue, 10 Nov 2020 14:11:13 -0800 (PST) Date: Tue, 10 Nov 2020 23:10:00 +0100 In-Reply-To: Message-Id: <4d261d4cbd9842c51cb6f9b36cadc9054cabe86b.1605046192.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog Subject: [PATCH v9 03/44] kasan: group vmalloc code From: Andrey Konovalov To: Catalin Marinas Cc: Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , Andrew Morton , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This is a preparatory commit for the upcoming addition of a new hardware tag-based (MTE-based) KASAN mode. Group all vmalloc-related function declarations in include/linux/kasan.h, and their implementations in mm/kasan/common.c. No functional changes. Signed-off-by: Andrey Konovalov Signed-off-by: Vincenzo Frascino Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko --- Change-Id: Ie20b6c689203cd6de4fd7f2c465ec081c00c5f15 --- include/linux/kasan.h | 41 +++++++++++++---------- mm/kasan/common.c | 78 ++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 56 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 30d343b4a40a..59538e795df4 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -75,19 +75,6 @@ struct kasan_cache { int free_meta_offset; }; -/* - * These functions provide a special case to support backing module - * allocations with real shadow memory. With KASAN vmalloc, the special - * case is unnecessary, as the work is handled in the generic case. - */ -#ifndef CONFIG_KASAN_VMALLOC -int kasan_module_alloc(void *addr, size_t size); -void kasan_free_shadow(const struct vm_struct *vm); -#else -static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } -static inline void kasan_free_shadow(const struct vm_struct *vm) {} -#endif - int kasan_add_zero_shadow(void *start, unsigned long size); void kasan_remove_zero_shadow(void *start, unsigned long size); @@ -156,9 +143,6 @@ static inline bool kasan_slab_free(struct kmem_cache *s, void *object, return false; } -static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } -static inline void kasan_free_shadow(const struct vm_struct *vm) {} - static inline int kasan_add_zero_shadow(void *start, unsigned long size) { return 0; @@ -211,13 +195,16 @@ static inline void *kasan_reset_tag(const void *addr) #endif /* CONFIG_KASAN_SW_TAGS */ #ifdef CONFIG_KASAN_VMALLOC + int kasan_populate_vmalloc(unsigned long addr, unsigned long size); void kasan_poison_vmalloc(const void *start, unsigned long size); void kasan_unpoison_vmalloc(const void *start, unsigned long size); void kasan_release_vmalloc(unsigned long start, unsigned long end, unsigned long free_region_start, unsigned long free_region_end); -#else + +#else /* CONFIG_KASAN_VMALLOC */ + static inline int kasan_populate_vmalloc(unsigned long start, unsigned long size) { @@ -232,7 +219,25 @@ static inline void kasan_release_vmalloc(unsigned long start, unsigned long end, unsigned long free_region_start, unsigned long free_region_end) {} -#endif + +#endif /* CONFIG_KASAN_VMALLOC */ + +#if defined(CONFIG_KASAN) && !defined(CONFIG_KASAN_VMALLOC) + +/* + * These functions provide a special case to support backing module + * allocations with real shadow memory. With KASAN vmalloc, the special + * case is unnecessary, as the work is handled in the generic case. + */ +int kasan_module_alloc(void *addr, size_t size); +void kasan_free_shadow(const struct vm_struct *vm); + +#else /* CONFIG_KASAN && !CONFIG_KASAN_VMALLOC */ + +static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } +static inline void kasan_free_shadow(const struct vm_struct *vm) {} + +#endif /* CONFIG_KASAN && !CONFIG_KASAN_VMALLOC */ #ifdef CONFIG_KASAN_INLINE void kasan_non_canonical_hook(unsigned long addr); diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 33d863f55db1..89e5ef9417a7 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -536,44 +536,6 @@ void kasan_kfree_large(void *ptr, unsigned long ip) /* The object will be poisoned by page_alloc. */ } -#ifndef CONFIG_KASAN_VMALLOC -int kasan_module_alloc(void *addr, size_t size) -{ - void *ret; - size_t scaled_size; - size_t shadow_size; - unsigned long shadow_start; - - shadow_start = (unsigned long)kasan_mem_to_shadow(addr); - scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT; - shadow_size = round_up(scaled_size, PAGE_SIZE); - - if (WARN_ON(!PAGE_ALIGNED(shadow_start))) - return -EINVAL; - - ret = __vmalloc_node_range(shadow_size, 1, shadow_start, - shadow_start + shadow_size, - GFP_KERNEL, - PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE, - __builtin_return_address(0)); - - if (ret) { - __memset(ret, KASAN_SHADOW_INIT, shadow_size); - find_vm_area(addr)->flags |= VM_KASAN; - kmemleak_ignore(ret); - return 0; - } - - return -ENOMEM; -} - -void kasan_free_shadow(const struct vm_struct *vm) -{ - if (vm->flags & VM_KASAN) - vfree(kasan_mem_to_shadow(vm->addr)); -} -#endif - #ifdef CONFIG_MEMORY_HOTPLUG static bool shadow_mapped(unsigned long addr) { @@ -685,6 +647,7 @@ core_initcall(kasan_memhotplug_init); #endif #ifdef CONFIG_KASAN_VMALLOC + static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr, void *unused) { @@ -923,4 +886,43 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end, (unsigned long)shadow_end); } } + +#else /* CONFIG_KASAN_VMALLOC */ + +int kasan_module_alloc(void *addr, size_t size) +{ + void *ret; + size_t scaled_size; + size_t shadow_size; + unsigned long shadow_start; + + shadow_start = (unsigned long)kasan_mem_to_shadow(addr); + scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT; + shadow_size = round_up(scaled_size, PAGE_SIZE); + + if (WARN_ON(!PAGE_ALIGNED(shadow_start))) + return -EINVAL; + + ret = __vmalloc_node_range(shadow_size, 1, shadow_start, + shadow_start + shadow_size, + GFP_KERNEL, + PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE, + __builtin_return_address(0)); + + if (ret) { + __memset(ret, KASAN_SHADOW_INIT, shadow_size); + find_vm_area(addr)->flags |= VM_KASAN; + kmemleak_ignore(ret); + return 0; + } + + return -ENOMEM; +} + +void kasan_free_shadow(const struct vm_struct *vm) +{ + if (vm->flags & VM_KASAN) + vfree(kasan_mem_to_shadow(vm->addr)); +} + #endif