From patchwork Thu Dec 21 18:35:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 182404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp611587dyi; Thu, 21 Dec 2023 10:36:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGUjrCTmWC8gUMRRw+J//rjSoT9fG1VRBSUPOi5+LBSFAPW+/euM0yFsabKKQVSM3T7bQft X-Received: by 2002:a17:90a:1288:b0:28b:c572:23ef with SMTP id g8-20020a17090a128800b0028bc57223efmr183043pja.9.1703183819145; Thu, 21 Dec 2023 10:36:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703183819; cv=none; d=google.com; s=arc-20160816; b=NUav+lhSHUy4n5A//aNsiGGnYTJLWkGsk320abN0V91CBHj52YDhifQTzDLNsKMQVt 76G8nGTDYVyIlTClj4DGGtLticWuQEcjoPFAF3pBUtySXaWMKberwhQ62ufLJH17WYsu RBgd44VXgShAWFn0VrS4bz9pB0TBAtleV6VTxQqQ9x7QCIByeuAFIbjM8ZFcQBf0wURg WLvejnm6315YrFhx+TwD0cYzNsR/bnyOTnKQrA4eWPGH9Rj4ThvSqOhhEVKHimi/828X TbBu7rhff86jfoiJdWWGmCzUHTfI7olu0J94nnpWUYkak9XhJseRV0URXZXxvdHUkqoI 7z9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=DILt3p53uNIRMlyKqLmSfi67XoCveJqLdgVMwZq9yXA=; fh=E9W45FwZb6Id+g4Lg+hTR4TMK1PXvrXS/qWrVP2FBRY=; b=0qaIwrchyRIF9rqJlRuwJHsgOf+4JdBQPEdjt3n5G50RQuv+iK6E5esBjSinIANGvS uSoxcjt1NUC7hpwnI3x4pse0kmuQHI60RWZaiADSb9PX2+Ev9etTg++BazP9RyjAtwGj vru/kLY8DRWNe9AVR1WBNKwAiPyFx/hti/mL3Wk76rt8Ohz5LB4EHQoP+azkWB/ZaI8M kY0qO4Vm7jD98v4VQbAg61lkSfetIT1L/mCx7gjyOnaJ4d9iXhnb4VIpwl5lyA3RZUIZ NAzeo79LrWhn8eEyDQV4xEO6V/QGVWSxf8kdCr9bZ1uZ3y2+MRY3MZr0grI6J3+7uUpd +ieg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=tbPj3uPo; spf=pass (google.com: domain of linux-kernel+bounces-8935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8935-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m8-20020a17090a3f8800b0028bf094cad8si1424531pjc.173.2023.12.21.10.36.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 10:36:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=tbPj3uPo; spf=pass (google.com: domain of linux-kernel+bounces-8935-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8935-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C41B2286874 for ; Thu, 21 Dec 2023 18:36:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5C597319A; Thu, 21 Dec 2023 18:35:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="tbPj3uPo" X-Original-To: linux-kernel@vger.kernel.org Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EC15BA30 for ; Thu, 21 Dec 2023 18:35:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703183747; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=DILt3p53uNIRMlyKqLmSfi67XoCveJqLdgVMwZq9yXA=; b=tbPj3uPoBR5fvyz4IQYqOUCK1dVNmUbtUl682TOpZhrRNOI2YNW915Z6Ra8mnw382p2lYY HAd5JqqGBNdGgKffBGvfA3Jgnw4Cwc00qn3tU0JtUl5qPdxqASFQcj+BykHY8fWr1UcDvc K8EcdixFixDgAws3WeDglq4de+F8pvM= From: andrey.konovalov@linux.dev To: Andrew Morton , Juntong Deng Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 1/4] kasan: clean up kasan_cache_create Date: Thu, 21 Dec 2023 19:35:37 +0100 Message-Id: <20231221183540.168428-1-andrey.konovalov@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785917676107532167 X-GMAIL-MSGID: 1785917676107532167 From: Andrey Konovalov Reorganize the code to avoid nested if/else checks to improve the readability. Also drop the confusing comments about KMALLOC_MAX_SIZE checks: they are relevant for both SLUB and SLAB (originally, the comments likely confused KMALLOC_MAX_SIZE with KMALLOC_MAX_CACHE_SIZE). Fixes: a5989d4ed40c ("kasan: improve free meta storage in Generic KASAN") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/generic.c | 67 +++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 54e20b2bc3e1..769e43e05d0b 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -381,16 +381,11 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, ok_size = *size; - /* Add alloc meta into redzone. */ + /* Add alloc meta into the redzone. */ cache->kasan_info.alloc_meta_offset = *size; *size += sizeof(struct kasan_alloc_meta); - /* - * If alloc meta doesn't fit, don't add it. - * This can only happen with SLAB, as it has KMALLOC_MAX_SIZE equal - * to KMALLOC_MAX_CACHE_SIZE and doesn't fall back to page_alloc for - * larger sizes. - */ + /* If alloc meta doesn't fit, don't add it. */ if (*size > KMALLOC_MAX_SIZE) { cache->kasan_info.alloc_meta_offset = 0; *size = ok_size; @@ -401,36 +396,52 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, orig_alloc_meta_offset = cache->kasan_info.alloc_meta_offset; /* - * Add free meta into redzone when it's not possible to store + * Store free meta in the redzone when it's not possible to store * it in the object. This is the case when: * 1. Object is SLAB_TYPESAFE_BY_RCU, which means that it can * be touched after it was freed, or * 2. Object has a constructor, which means it's expected to - * retain its content until the next allocation, or - * 3. Object is too small and SLUB DEBUG is enabled. Avoid - * free meta that exceeds the object size corrupts the - * SLUB DEBUG metadata. - * Otherwise cache->kasan_info.free_meta_offset = 0 is implied. - * If the object is smaller than the free meta and SLUB DEBUG - * is not enabled, it is still possible to store part of the - * free meta in the object. + * retain its content until the next allocation. */ if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor) { cache->kasan_info.free_meta_offset = *size; *size += sizeof(struct kasan_free_meta); - } else if (cache->object_size < sizeof(struct kasan_free_meta)) { - if (__slub_debug_enabled()) { - cache->kasan_info.free_meta_offset = *size; - *size += sizeof(struct kasan_free_meta); - } else { - rem_free_meta_size = sizeof(struct kasan_free_meta) - - cache->object_size; - *size += rem_free_meta_size; - if (cache->kasan_info.alloc_meta_offset != 0) - cache->kasan_info.alloc_meta_offset += rem_free_meta_size; - } + goto free_meta_added; + } + + /* + * Otherwise, if the object is large enough to contain free meta, + * store it within the object. + */ + if (sizeof(struct kasan_free_meta) <= cache->object_size) { + /* cache->kasan_info.free_meta_offset = 0 is implied. */ + goto free_meta_added; } + /* + * For smaller objects, store the beginning of free meta within the + * object and the end in the redzone. And thus shift the location of + * alloc meta to free up space for free meta. + * This is only possible when slub_debug is disabled, as otherwise + * the end of free meta will overlap with slub_debug metadata. + */ + if (!__slub_debug_enabled()) { + rem_free_meta_size = sizeof(struct kasan_free_meta) - + cache->object_size; + *size += rem_free_meta_size; + if (cache->kasan_info.alloc_meta_offset != 0) + cache->kasan_info.alloc_meta_offset += rem_free_meta_size; + goto free_meta_added; + } + + /* + * If the object is small and slub_debug is enabled, store free meta + * in the redzone after alloc meta. + */ + cache->kasan_info.free_meta_offset = *size; + *size += sizeof(struct kasan_free_meta); + +free_meta_added: /* If free meta doesn't fit, don't add it. */ if (*size > KMALLOC_MAX_SIZE) { cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; @@ -440,7 +451,7 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, /* Calculate size with optimal redzone. */ optimal_size = cache->object_size + optimal_redzone(cache->object_size); - /* Limit it with KMALLOC_MAX_SIZE (relevant for SLAB only). */ + /* Limit it with KMALLOC_MAX_SIZE. */ if (optimal_size > KMALLOC_MAX_SIZE) optimal_size = KMALLOC_MAX_SIZE; /* Use optimal size if the size with added metas is not large enough. */ From patchwork Thu Dec 21 18:35:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 182405 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp611614dyi; Thu, 21 Dec 2023 10:37:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGkAOGoN++d/Y7z6bnVcKO++eEymShL8XHOmT1wkhgPXHbo0VvqRnOEWIjp3Nzh5fd2SFZB X-Received: by 2002:a17:90a:784e:b0:28a:c616:ff1c with SMTP id y14-20020a17090a784e00b0028ac616ff1cmr141990pjl.97.1703183821734; Thu, 21 Dec 2023 10:37:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703183821; cv=none; d=google.com; s=arc-20160816; b=FXS+6Jk1YCtpNNCm5TkJ7AL3EI5salFnJgPpq+MS8xqnayz5XC0j57Sw8XSAH/uQvb y/YkGoZnZaLme48zmxPvkt7A0W4UCSpnLfgy73eloXOkVEhVqecHFuTdL0fFg6vDq1h9 XyOMj4YbpKJ8m1Dm202BfaeDF9Bqz08s+uE8DfC+F4R4tYLA1QSO7Dzc9V2ch8J/A7/y Nmn0EaUde9aFe4LQwurUdyg+1m2iyHZNdeL9Yw9cDG+Yr/FrfT3Y1YNghUTpDVycv+QT CVp8puW1D8BYxcfLF6Fb1P2C4ybYz36ZQWf9cJAFx4xXfbsw6WAG9+xKOgKQT1Zkr8xx df5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=9xuoTXuQnTI+pN/bKZ3i8KybAohyAUoP+IGmtMicRak=; fh=E9W45FwZb6Id+g4Lg+hTR4TMK1PXvrXS/qWrVP2FBRY=; b=GL6IfK/MQaxkQ1oHBV9ew1sexO78MpfWXB7bcYaibnRM6mtlSD4QWOAEh2NOvx8HNA 5XF6KABFcOdovBTx9lc6ytJjTDKPQsI3OLqkjY3/rIou2Y0vnY6z7o3rY/DF/qB0hZ6z yqXUreIAqFu+QqleSFSlR4dkMEKjqaNc9NE7Je5J6msVAri/iXoej4XfN/TmZrqJV+Fx JDhZNu6JfrbHwJYkZJKu+PTsNwxLaFTwNUCgrugx47uZc4Gsh8NKuvl1DF+l6hceJsu+ oplsG1BlnPWTMbVRlMOuWPjx3c4KHSUZ8/n4LsIVUFFjg0inTTluyZCSYULpYgTKorKc GuZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=WarMWAg0; spf=pass (google.com: domain of linux-kernel+bounces-8936-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8936-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w7-20020a17090aad4700b0028b3dcfcf5dsi2071959pjv.0.2023.12.21.10.37.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 10:37:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8936-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=WarMWAg0; spf=pass (google.com: domain of linux-kernel+bounces-8936-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8936-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 728482863A4 for ; Thu, 21 Dec 2023 18:36:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 08F1A745CD; Thu, 21 Dec 2023 18:35:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WarMWAg0" X-Original-To: linux-kernel@vger.kernel.org Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B97F76280C for ; Thu, 21 Dec 2023 18:35:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703183747; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9xuoTXuQnTI+pN/bKZ3i8KybAohyAUoP+IGmtMicRak=; b=WarMWAg0K7SHak78ygWtlpgqTMwt7mumTYTiIrRXvERxbmLV9b2mB09CJkEoY/kvKqecGT LJD1Iz9YrXuxQjpfGtqzVdMCmE1Di3rxLYHjeVLXbJ+cka5BV4sF8NvnLlPNSEtQ8xSxsE gYC+xeKuS4JVZvylLWKrrqmi4VV2GMA= From: andrey.konovalov@linux.dev To: Andrew Morton , Juntong Deng Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 2/4] kasan: reuse kasan_track in kasan_stack_ring_entry Date: Thu, 21 Dec 2023 19:35:38 +0100 Message-Id: <20231221183540.168428-2-andrey.konovalov@linux.dev> In-Reply-To: <20231221183540.168428-1-andrey.konovalov@linux.dev> References: <20231221183540.168428-1-andrey.konovalov@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785917679033489252 X-GMAIL-MSGID: 1785917679033489252 From: Andrey Konovalov Avoid duplicating fields of kasan_track in kasan_stack_ring_entry: reuse the structure. Fixes: 5d4c6ac94694 ("kasan: record and report more information") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/kasan.h | 7 +------ mm/kasan/report_tags.c | 12 ++++++------ mm/kasan/tags.c | 12 ++++++------ 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 5e298e3ac909..9072ce4c1263 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -279,13 +279,8 @@ struct kasan_free_meta { struct kasan_stack_ring_entry { void *ptr; size_t size; - u32 pid; - depot_stack_handle_t stack; + struct kasan_track track; bool is_free; -#ifdef CONFIG_KASAN_EXTRA_INFO - u64 cpu:20; - u64 timestamp:44; -#endif /* CONFIG_KASAN_EXTRA_INFO */ }; struct kasan_stack_ring { diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 979f284c2497..688b9d70b04a 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -31,8 +31,8 @@ static const char *get_common_bug_type(struct kasan_report_info *info) static void kasan_complete_extra_report_info(struct kasan_track *track, struct kasan_stack_ring_entry *entry) { - track->cpu = entry->cpu; - track->timestamp = entry->timestamp; + track->cpu = entry->track.cpu; + track->timestamp = entry->track.timestamp; } #endif /* CONFIG_KASAN_EXTRA_INFO */ @@ -80,8 +80,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (free_found) break; - info->free_track.pid = entry->pid; - info->free_track.stack = entry->stack; + info->free_track.pid = entry->track.pid; + info->free_track.stack = entry->track.stack; #ifdef CONFIG_KASAN_EXTRA_INFO kasan_complete_extra_report_info(&info->free_track, entry); #endif /* CONFIG_KASAN_EXTRA_INFO */ @@ -98,8 +98,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (alloc_found) break; - info->alloc_track.pid = entry->pid; - info->alloc_track.stack = entry->stack; + info->alloc_track.pid = entry->track.pid; + info->alloc_track.stack = entry->track.stack; #ifdef CONFIG_KASAN_EXTRA_INFO kasan_complete_extra_report_info(&info->alloc_track, entry); #endif /* CONFIG_KASAN_EXTRA_INFO */ diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index c13b198b8302..c4d14dbf27c0 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -100,8 +100,8 @@ static void save_extra_info(struct kasan_stack_ring_entry *entry) u32 cpu = raw_smp_processor_id(); u64 ts_nsec = local_clock(); - entry->cpu = cpu; - entry->timestamp = ts_nsec >> 3; + entry->track.cpu = cpu; + entry->track.timestamp = ts_nsec >> 3; } #endif /* CONFIG_KASAN_EXTRA_INFO */ @@ -134,15 +134,15 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ - old_stack = entry->stack; + old_stack = entry->track.stack; entry->size = cache->object_size; - entry->pid = current->pid; - entry->stack = stack; - entry->is_free = is_free; + entry->track.pid = current->pid; + entry->track.stack = stack; #ifdef CONFIG_KASAN_EXTRA_INFO save_extra_info(entry); #endif /* CONFIG_KASAN_EXTRA_INFO */ + entry->is_free = is_free; entry->ptr = object; From patchwork Thu Dec 21 18:35:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 182402 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp611489dyi; Thu, 21 Dec 2023 10:36:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQ/O5l2ndgH9oomeTM8hhTuFnzjCIQH9A4fE2YuES/LXNSOvW840LSXvEeypdbvc2z5BvC X-Received: by 2002:a05:6808:2083:b0:3b8:3e9c:af97 with SMTP id s3-20020a056808208300b003b83e9caf97mr124405oiw.48.1703183809138; Thu, 21 Dec 2023 10:36:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703183808; cv=none; d=google.com; s=arc-20160816; b=x3Ukgfdcfe4YBC//6BlVcf9GAkNjnfPRTy5KhKQwu/4auSJ5hvKRlCSwzqkR8F5Z4K qMMUwOlIF65RSNCxfSM0dWz874tFTF75eXDDzF09+35RzFaH07B2kuMxOalpHGHZ2jOK YEbkH3Ha/CK5tUla6T8Mha0vDHLeSndFqo/45m3oR/A3/1pZI4AHOapMUuB9CdrHHf+z zaC+ehuaNme8OuWFn9OI5bfyj5cEUJeDRjgDSWSTYKyITnWeSY5E7SOpMYSs+Ie4loqR pOWUw1v3LXWaz+vhlJRWVZHUYbr4nLbKdb/37rgq6wdFDPIfIhLHNNKBA2WjgTHU9BNz hQMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=wLcCaNYp1HtmrZAPrigLSWnYXTa9aslZ8ruUHa0DHbo=; fh=E9W45FwZb6Id+g4Lg+hTR4TMK1PXvrXS/qWrVP2FBRY=; b=HZJZCUxioy3MEpl8UZhgTGUJouzfbb9Gp7BS3tfjtEIuTfT4WP6GJyLFtfVxRW1YDv fKtu/IeiShrazzYNQsBU04LuWeJMAEE7iDXyzt5J9n+oYpe3kFY1LwYWXqFwBEJ3FZdt N6oCIFMAVSLqUt7O92ZSwYRQfD30xnPQzIfldRIQR06EhZrrCIypMA68EfRVjeVr4dLJ fH3z4uR9o8sljlAE1x2IfWCpAs52jjLQKHGy6FGAGYXuzqwWIVAOO+Z2Q1wUzN/yzCGD v47Ho//0hL6jMkmCeyZgt44ho3Ptqu4Fu689KIJKR4xyUnm6fyj+2z0dICj1tYRdaWHJ eweA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Wp4SgxAx; spf=pass (google.com: domain of linux-kernel+bounces-8937-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8937-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a3-20020a0ca983000000b0067f3a788cd4si2584501qvb.349.2023.12.21.10.36.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 10:36:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8937-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Wp4SgxAx; spf=pass (google.com: domain of linux-kernel+bounces-8937-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8937-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CB7061C243B0 for ; Thu, 21 Dec 2023 18:36:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6CF68745F1; Thu, 21 Dec 2023 18:35:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Wp4SgxAx" X-Original-To: linux-kernel@vger.kernel.org Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 459C963505 for ; Thu, 21 Dec 2023 18:35:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703183748; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wLcCaNYp1HtmrZAPrigLSWnYXTa9aslZ8ruUHa0DHbo=; b=Wp4SgxAxzf33U9vG6eCpb57dWh/1TqzRC2qPSlMKfzo4YFjahK6jxrjxwucpfy7HNf515z HVTQaBE2QE4c7zE1ojOnJfPr31drq3YJKN5R0J3wvccZ6BKYlSSN5VG/UPs4jM7chLnJJ2 iVscHV7PW/6Z659boGsoyF6DsXA5Ssw= From: andrey.konovalov@linux.dev To: Andrew Morton , Juntong Deng Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 3/4] kasan: simplify saving extra info into tracks Date: Thu, 21 Dec 2023 19:35:39 +0100 Message-Id: <20231221183540.168428-3-andrey.konovalov@linux.dev> In-Reply-To: <20231221183540.168428-1-andrey.konovalov@linux.dev> References: <20231221183540.168428-1-andrey.konovalov@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785917664847037605 X-GMAIL-MSGID: 1785917664847037605 From: Andrey Konovalov Avoid duplicating code for saving extra info into tracks: reuse the common function for this. Fixes: 5d4c6ac94694 ("kasan: record and report more information") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/common.c | 12 ++++++++++-- mm/kasan/generic.c | 4 ++-- mm/kasan/kasan.h | 3 ++- mm/kasan/tags.c | 17 +---------------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index fe6c4b43ad9f..d004a0f4406c 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -48,7 +48,7 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags) return stack_depot_save_flags(entries, nr_entries, flags, depot_flags); } -void kasan_set_track(struct kasan_track *track, gfp_t flags) +void kasan_set_track(struct kasan_track *track, depot_stack_handle_t stack) { #ifdef CONFIG_KASAN_EXTRA_INFO u32 cpu = raw_smp_processor_id(); @@ -58,8 +58,16 @@ void kasan_set_track(struct kasan_track *track, gfp_t flags) track->timestamp = ts_nsec >> 3; #endif /* CONFIG_KASAN_EXTRA_INFO */ track->pid = current->pid; - track->stack = kasan_save_stack(flags, + track->stack = stack; +} + +void kasan_save_track(struct kasan_track *track, gfp_t flags) +{ + depot_stack_handle_t stack; + + stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); + kasan_set_track(track, stack); } #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 769e43e05d0b..11b575707b05 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -553,7 +553,7 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) stack_depot_put(alloc_meta->aux_stack[1]); __memset(alloc_meta, 0, sizeof(*alloc_meta)); - kasan_set_track(&alloc_meta->alloc_track, flags); + kasan_save_track(&alloc_meta->alloc_track, flags); } void kasan_save_free_info(struct kmem_cache *cache, void *object) @@ -564,7 +564,7 @@ void kasan_save_free_info(struct kmem_cache *cache, void *object) if (!free_meta) return; - kasan_set_track(&free_meta->free_track, 0); + kasan_save_track(&free_meta->free_track, 0); /* The object was freed and has free track set. */ *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREETRACK; } diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 9072ce4c1263..31fb6bb26fed 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -372,7 +372,8 @@ static inline void kasan_init_object_meta(struct kmem_cache *cache, const void * #endif depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags); -void kasan_set_track(struct kasan_track *track, gfp_t flags); +void kasan_set_track(struct kasan_track *track, depot_stack_handle_t stack); +void kasan_save_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index c4d14dbf27c0..d65d48b85f90 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -94,17 +94,6 @@ void __init kasan_init_tags(void) } } -#ifdef CONFIG_KASAN_EXTRA_INFO -static void save_extra_info(struct kasan_stack_ring_entry *entry) -{ - u32 cpu = raw_smp_processor_id(); - u64 ts_nsec = local_clock(); - - entry->track.cpu = cpu; - entry->track.timestamp = ts_nsec >> 3; -} -#endif /* CONFIG_KASAN_EXTRA_INFO */ - static void save_stack_info(struct kmem_cache *cache, void *object, gfp_t gfp_flags, bool is_free) { @@ -137,11 +126,7 @@ static void save_stack_info(struct kmem_cache *cache, void *object, old_stack = entry->track.stack; entry->size = cache->object_size; - entry->track.pid = current->pid; - entry->track.stack = stack; -#ifdef CONFIG_KASAN_EXTRA_INFO - save_extra_info(entry); -#endif /* CONFIG_KASAN_EXTRA_INFO */ + kasan_set_track(&entry->track, stack); entry->is_free = is_free; entry->ptr = object; From patchwork Thu Dec 21 18:35:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 182403 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp611550dyi; Thu, 21 Dec 2023 10:36:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEw7FbsfGmJgMgsxhK68Z+J919TmOiqH+k+H+6I9yPo4WeYGMVqvXmDBjVUrfBShuc5xPas X-Received: by 2002:a50:c319:0:b0:553:2e38:5f26 with SMTP id a25-20020a50c319000000b005532e385f26mr23279edb.7.1703183816585; Thu, 21 Dec 2023 10:36:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703183816; cv=none; d=google.com; s=arc-20160816; b=cFB0rbLqehdXr1DeJdqy46/SSWPm8/l4kF7ETpBGCojcPASLwT8cqifvbjdDhg+FHQ fGNFxfDFJHmn+2UEH9wlCgWtcF/JlF+TY0RaJ/e1lOdg1oMKR7W7JI7P+QkeDJtCGBGu KOmaRteHGepzDhc55ygiCtPaVOjQf9FMZg5AqapyD+hoYNm9LC31EzQqOcFQDB1b1dhZ uBXcmRpseMGC8QZmAOmfY3/ng888Q4GUExV3Ch77Eskt39KMDWSC0bfFIjCzn7CozwSF gK2I6LXeTE4QxvaEZ0ZtLogB+rBkPKCTZzcRmmEQU2qy85iUyga3OYfJGzdO3LCPhpN+ V72w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=FiI0FbwykV/2XbgOuJsaOX93jnuQZGsyNc8LlrDSdxY=; fh=E9W45FwZb6Id+g4Lg+hTR4TMK1PXvrXS/qWrVP2FBRY=; b=xDrxlQ/ydFRxi0gIeAmDQ1oCKITzHv157tJpKJYr7vfjWjEBgVcLOUuVCQ67Ddwwwy fcsfP9zinA05CfVYKOWWrSabXyGxv58MYccVBtJlZWomsDXGOgu9aQT+E7H0Kj12LWtQ rw552AtBQBkobtC4j0xikyKyMZ5pttMrM9Z+MV6wArteYWsLMKtKzLz7SdZlIbuRncMz C/Otaisio068Oi/jDAeagKC9SbSfunq/yb8GjbaUMgAjaZnmp4fvz1zk6ETKzKx396Ir Vax+RWS/qvRo71OmtFj32LF6wtQFJWJ336qDiVyVsq4SEK93jIvaoso7ofqxDb4YGamw 9IhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iZOz0xdv; spf=pass (google.com: domain of linux-kernel+bounces-8938-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8938-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id v28-20020a50a45c000000b0055390294599si1087975edb.660.2023.12.21.10.36.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 10:36:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8938-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iZOz0xdv; spf=pass (google.com: domain of linux-kernel+bounces-8938-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8938-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 21C451F227B8 for ; Thu, 21 Dec 2023 18:36:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A7B976095; Thu, 21 Dec 2023 18:35:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="iZOz0xdv" X-Original-To: linux-kernel@vger.kernel.org Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19DA8BA5F for ; Thu, 21 Dec 2023 18:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1703183749; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FiI0FbwykV/2XbgOuJsaOX93jnuQZGsyNc8LlrDSdxY=; b=iZOz0xdvhazUGbDD/nkQ4ayAtlGnpXZBnWnzL0gh008WaAjCs/ptKU+lof1bjeEEnJT4H3 m/04qiY03QGGFOPq+9Um1KjCZvu3EYR64rQmdOa8BsiMcU4UbcAkLwg4eDeQRQK3p7PEb3 QlQ59oWVTbABhYd7X+p6qVYXgnw5aDw= From: andrey.konovalov@linux.dev To: Andrew Morton , Juntong Deng Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 4/4] kasan: simplify kasan_complete_mode_report_info for tag-based modes Date: Thu, 21 Dec 2023 19:35:40 +0100 Message-Id: <20231221183540.168428-4-andrey.konovalov@linux.dev> In-Reply-To: <20231221183540.168428-1-andrey.konovalov@linux.dev> References: <20231221183540.168428-1-andrey.konovalov@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785917673639783275 X-GMAIL-MSGID: 1785917673639783275 From: Andrey Konovalov memcpy the alloc/free tracks when collecting the information about a bad access instead of copying fields one by one. Fixes: 5d4c6ac94694 ("kasan: record and report more information") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/report_tags.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 688b9d70b04a..d15f8f580e2c 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -27,15 +27,6 @@ static const char *get_common_bug_type(struct kasan_report_info *info) return "invalid-access"; } -#ifdef CONFIG_KASAN_EXTRA_INFO -static void kasan_complete_extra_report_info(struct kasan_track *track, - struct kasan_stack_ring_entry *entry) -{ - track->cpu = entry->track.cpu; - track->timestamp = entry->track.timestamp; -} -#endif /* CONFIG_KASAN_EXTRA_INFO */ - void kasan_complete_mode_report_info(struct kasan_report_info *info) { unsigned long flags; @@ -80,11 +71,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (free_found) break; - info->free_track.pid = entry->track.pid; - info->free_track.stack = entry->track.stack; -#ifdef CONFIG_KASAN_EXTRA_INFO - kasan_complete_extra_report_info(&info->free_track, entry); -#endif /* CONFIG_KASAN_EXTRA_INFO */ + memcpy(&info->free_track, &entry->track, + sizeof(info->free_track)); free_found = true; /* @@ -98,11 +86,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (alloc_found) break; - info->alloc_track.pid = entry->track.pid; - info->alloc_track.stack = entry->track.stack; -#ifdef CONFIG_KASAN_EXTRA_INFO - kasan_complete_extra_report_info(&info->alloc_track, entry); -#endif /* CONFIG_KASAN_EXTRA_INFO */ + memcpy(&info->alloc_track, &entry->track, + sizeof(info->alloc_track)); alloc_found = true; /*