From patchwork Thu Dec 9 22:15:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12695538 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4A00EC433EF for ; Thu, 9 Dec 2021 22:18:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc: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=+HedJypEsy4hFwFjCAFHuqL0xSuCVLcQqfl1mcMjjUU=; b=4XRheIPoqTNUP+7XIS1Gh8TlJU XvD/MWjBfKalZqFBcHp2S3JIKaPnt+UTmi3rnITbo/8osxTLvYqC4EUWOVuljWQHoD90SjqAIiOjh wlKHr0s50WxF7BQVHHiXmWgzy4VbUcyZWeiiZy36IiEjWzogpbO4J0wnMXa8OjsJFRbKL7DhQzT/r x2hrWMqXa7C/ShgrpS5/ezLcllfYJLa+bGvAvJ2D1O8xOxbnP/zOi4RjbF8/8O9azLAuD3/ZJqTWs UqhrNb30lLqxC3kKymgsKYnEvmifzFJj2tIA2rprnHjJESUsNs/EZJ0r4kYatoLLKPhR1BupIcXrV RB20UOTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvRiX-000CYR-R0; Thu, 09 Dec 2021 22:16:47 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvRhj-000CB4-V6 for linux-arm-kernel@lists.infradead.org; Thu, 09 Dec 2021 22:15:57 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id l28-20020a25b31c000000b005c27dd4987bso13014702ybj.18 for ; Thu, 09 Dec 2021 14:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dwdgd8JweL+y6+nrXqZEHKOZ4Er9g7fAxzLKfl2roN8=; b=gdXEWqkFty7aXvoUbqtxrKuTbwrsUWyeKzBc/2qK05ncxkeU68IGN+z7CBSBSLhvq0 VSKZ5zuaZcR1YIagxQBuut2Z1O2JrZ0EYA/8DH5YHg6GIumsATva7EN7ZhQK7SVPg4zI B/jm3bOv5kZxpYfijvhqhh27odhS/YcaM02HZ31iiV7B46zKtsD4t+mz0JcFE0ajrOGR fC6aqvEfq4GxpxT5lvscXBoZSk5GHmZaUMF6Xhe+RQ/+JXgaZumiw9nurVXihAKKtdLm +Occtswm+rWEXKNqAKRP+rNrebfRZmBeaNNCo/NCqPoBWjlhaX/yMiwnSOuqhynp2Wgz Y81Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dwdgd8JweL+y6+nrXqZEHKOZ4Er9g7fAxzLKfl2roN8=; b=NmL/4Tm7PuPcg1q/WjdRlD7fCRNckBOgr1MrKVwVE1sq7Ki9w2Bu1t973LEgjvqLwl kqIKSCYZ2/HqEL09ZJsMpg+yQ/lmnYZSrKIbwDYEahgkdU5tiNL69gtzqx9yWTIazr98 RlN6YxH6IqAIfIgcrmXsgn4zaApHkds2lV1d5u8xnocyJdWXpADY60V0+mvhmJDuxGvu PeNUFYBIPhVTtI7A59RYtRWeIhDoMnoSqP9kQeJbDzO7LwMzrUnUozwxQ4OTjIa3ijwN cpomkBOyOj23oQ9ZHmSeRlHKosNDD6xjiQuleOauvwNnyfllFXFwjq8TmgVLapxg5/dU ebAw== X-Gm-Message-State: AOAM530D93bDm0gK+4pl001XPGRpZeG7er3rQC/tTLeZBSnHSEZ2+4FB 9g2/QkjIVD2UDTCQUJUaI+BFcuA= X-Google-Smtp-Source: ABdhPJwKihBmfBMEk5ZMArBh1OYKiVcJoYUSS/GWhVtMWG2HAxD/AOhvEyywlKZX0yJ1PDn2dw6nRVI= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:f233:e324:8aa0:f65c]) (user=pcc job=sendgmr) by 2002:a25:8441:: with SMTP id r1mr9421270ybm.269.1639088154069; Thu, 09 Dec 2021 14:15:54 -0800 (PST) Date: Thu, 9 Dec 2021 14:15:38 -0800 In-Reply-To: <20211209221545.2333249-1-pcc@google.com> Message-Id: <20211209221545.2333249-2-pcc@google.com> Mime-Version: 1.0 References: <20211209221545.2333249-1-pcc@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v4 1/7] include: split out uaccess instrumentation into a separate header From: Peter Collingbourne To: Catalin Marinas , Will Deacon , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Thomas Gleixner , Andy Lutomirski , Kees Cook , Andrew Morton , Masahiro Yamada , Sami Tolvanen , YiFei Zhu , Mark Rutland , Frederic Weisbecker , Viresh Kumar , Andrey Konovalov , Peter Collingbourne , Gabriel Krisman Bertazi , Chris Hyser , Daniel Vetter , Chris Wilson , Arnd Bergmann , Dmitry Vyukov , Christian Brauner , "Eric W. Biederman" , Alexey Gladkov , Ran Xiaokai , David Hildenbrand , Xiaofeng Cao , Cyrill Gorcunov , Thomas Cedeno , Marco Elver , Alexander Potapenko Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Evgenii Stepanov X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211209_141556_077569_439F7168 X-CRM114-Status: GOOD ( 21.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In an upcoming change we are going to add uaccess instrumentation that uses inline access to struct task_struct from the instrumentation routines. Because instrumentation.h is included from many places including (recursively) from sched.h this would otherwise lead to a circular dependency. Break the dependency by moving uaccess instrumentation routines into a separate header, instrumentation-uaccess.h. Link: https://linux-review.googlesource.com/id/I625728db0c8db374e13e4ebc54985ac5c79ace7d Signed-off-by: Peter Collingbourne Acked-by: Dmitry Vyukov Reviewed-by: Marco Elver --- include/linux/instrumented-uaccess.h | 49 ++++++++++++++++++++++++++++ include/linux/instrumented.h | 34 ------------------- include/linux/uaccess.h | 2 +- lib/iov_iter.c | 2 +- lib/usercopy.c | 2 +- 5 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 include/linux/instrumented-uaccess.h diff --git a/include/linux/instrumented-uaccess.h b/include/linux/instrumented-uaccess.h new file mode 100644 index 000000000000..ece549088e50 --- /dev/null +++ b/include/linux/instrumented-uaccess.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * This header provides generic wrappers for memory access instrumentation for + * uaccess routines that the compiler cannot emit for: KASAN, KCSAN. + */ +#ifndef _LINUX_INSTRUMENTED_UACCESS_H +#define _LINUX_INSTRUMENTED_UACCESS_H + +#include +#include +#include +#include + +/** + * instrument_copy_to_user - instrument reads of copy_to_user + * + * Instrument reads from kernel memory, that are due to copy_to_user (and + * variants). The instrumentation must be inserted before the accesses. + * + * @to destination address + * @from source address + * @n number of bytes to copy + */ +static __always_inline void +instrument_copy_to_user(void __user *to, const void *from, unsigned long n) +{ + kasan_check_read(from, n); + kcsan_check_read(from, n); +} + +/** + * instrument_copy_from_user - instrument writes of copy_from_user + * + * Instrument writes to kernel memory, that are due to copy_from_user (and + * variants). The instrumentation should be inserted before the accesses. + * + * @to destination address + * @from source address + * @n number of bytes to copy + */ +static __always_inline void +instrument_copy_from_user(const void *to, const void __user *from, unsigned long n) +{ + kasan_check_write(to, n); + kcsan_check_write(to, n); +} + +#endif /* _LINUX_INSTRUMENTED_UACCESS_H */ diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index 42faebbaa202..b68f415510c7 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -102,38 +102,4 @@ static __always_inline void instrument_atomic_read_write(const volatile void *v, kcsan_check_atomic_read_write(v, size); } -/** - * instrument_copy_to_user - instrument reads of copy_to_user - * - * Instrument reads from kernel memory, that are due to copy_to_user (and - * variants). The instrumentation must be inserted before the accesses. - * - * @to destination address - * @from source address - * @n number of bytes to copy - */ -static __always_inline void -instrument_copy_to_user(void __user *to, const void *from, unsigned long n) -{ - kasan_check_read(from, n); - kcsan_check_read(from, n); -} - -/** - * instrument_copy_from_user - instrument writes of copy_from_user - * - * Instrument writes to kernel memory, that are due to copy_from_user (and - * variants). The instrumentation should be inserted before the accesses. - * - * @to destination address - * @from source address - * @n number of bytes to copy - */ -static __always_inline void -instrument_copy_from_user(const void *to, const void __user *from, unsigned long n) -{ - kasan_check_write(to, n); - kcsan_check_write(to, n); -} - #endif /* _LINUX_INSTRUMENTED_H */ diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index ac0394087f7d..c0c467e39657 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -3,7 +3,7 @@ #define __LINUX_UACCESS_H__ #include -#include +#include #include #include #include diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 66a740e6e153..3f9dc6df7102 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #define PIPE_PARANOIA /* for now */ diff --git a/lib/usercopy.c b/lib/usercopy.c index 7413dd300516..1cd188e62d06 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include -#include +#include #include /* out-of-line parts */