From patchwork Tue Dec 13 14:11:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 32839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp143505wrn; Tue, 13 Dec 2022 06:13:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf7nh+jKSov25b9gEgVX0cAeG6fLX2lGUxcn1yHBR7A5tL69xFOAHXw0Yy7hHxo4HAkxoZBW X-Received: by 2002:a17:902:c086:b0:189:d3d9:9308 with SMTP id j6-20020a170902c08600b00189d3d99308mr21607512pld.34.1670940835292; Tue, 13 Dec 2022 06:13:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670940835; cv=none; d=google.com; s=arc-20160816; b=zFN+OZRRPcT53dn9EAloODMhCO6zBAdAHBTC3WyhsxzHDU22YzE1zf0HBaLCcXjNeB eSxvxkev+GTYks5wv2Jh8JeMiq/t3xja42JgCpKo1cwHAKP9Hd/PFhGv+7UJF0LQh/xV gFnzC9QCy6Xkm/TgWTaRidPU/BdRtMdZXFJPXUCQMHuq0m4fXfhcUqZ6iEx95SUm/xqq KQJuYNCYovs7qZehwE0uUpySO2lxWwW2VSgHfKoydkZztEsMX5jwlO/RzA8HfWhI53xV 7SfWp/tApYPfe9h2Q3dNfgf85C0Ol+/npIFIp+Xd4acpK6BalcMkywMmnqbZdz4sDBhB dTYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wtS40n064VLArigg3Ta54CRGeLXfu1xgcwV7icQJ+fc=; b=xvXXcX93FFb3QD1Y2HN7/Kq+/I67nUK80fcZqb5Ol13UYNP4OWAaodFaSGNzo0oeKt du32r9ijDQ/dqLqd7wkCe48eINzzztel51VwbWYOyXJvxi+MYCDiEImAotloNzd0G2Pm 5xtRM+hRG2SbI1ddHrG9/rWjx5aydeEtTjFx8hG1ibCQQ9nI4ka0wLbZWf0WXIK1OU7S GPa3+S8EpLcJ3YMYpakiTY0gStN3g9W7oZqkd9dyDtsTXXBnV2HnDo1/CLXJ8MSmQ22X N68PXmZFnhkSKb9D/NBeoieh4VYVQSN+hHW3YybGdwlIPq8nvLFuJeAhBB5lSDRDBoBf hZ8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PUxzoLVp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n14-20020a170902d2ce00b00174ea015ee8si13434942plc.592.2022.12.13.06.13.41; Tue, 13 Dec 2022 06:13:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PUxzoLVp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235958AbiLMONH (ORCPT + 99 others); Tue, 13 Dec 2022 09:13:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235924AbiLMOMb (ORCPT ); Tue, 13 Dec 2022 09:12:31 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 282741CFDD; Tue, 13 Dec 2022 06:11:28 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AB197B81189; Tue, 13 Dec 2022 14:11:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E5C0C433D2; Tue, 13 Dec 2022 14:11:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670940686; bh=i4+X36aP4iUnP5dBT/vzjdrM0ePyEqx+ak2BmIRIs+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PUxzoLVpYu2qisXG8MRJM5a56Uhdf/Ac/MoPyQmvI/hmHJ396CvYKXxq0iI31bsu2 SaILArKAyI1DM2hqnBH6YCjZXr+cFzR1f/baQbcdt109Esjg3dQVDqAYrwMuIEgYYh iQpMkz2pDY3vmKSDVqWOxFXR6acAtblYHHS8hPV16FgL5ccWG35AoMzE+SG+RysjIa r/53WQCnFsMXLnJ2/q3uDdJ6S2To+/aNMBz86cUV1aXQRLd4XNea6TPUuTl9ueUlPS XWilQ42gRdNf5ZphpXp57CRHOPQ89MR3NaMiHCD4qid+f0foYHeRp5rfQw4MIc/1ja SdEa8IQT1prSw== From: "Masami Hiramatsu (Google)" To: LKML Cc: bpf@vger.kernel.org, Borislav Petkov , Alexei Starovoitov , Steven Rostedt , Linus Torvalds , Masami Hiramatsu , Andrew Morton , Peter Zijlstra , Kees Cook , Josh Poimboeuf , KP Singh , Mark Rutland , Florent Revest , Greg Kroah-Hartman , Christoph Hellwig , Chris Mason , Jonathan Corbet , linux-doc@vger.kernel.org, Akinobu Mita , Randy Dunlap Subject: [PATCH v2 1/2] error-injection: Remove EI_ETYPE_NONE Date: Tue, 13 Dec 2022 23:11:21 +0900 Message-Id: <167094068123.608798.9238149148720683524.stgit@devnote3> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog In-Reply-To: <167094067084.608798.11303550366840600235.stgit@devnote3> References: <167094067084.608798.11303550366840600235.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752108457000578390?= X-GMAIL-MSGID: =?utf-8?q?1752108457000578390?= From: Masami Hiramatsu (Google) Since the EI_ETYPE_NONE is confusing type, replace it with appropriate errno. The EI_ETYPE_NONE has been introduced for a dummy (error) value, but it can mislead people that they can use ALLOW_ERROR_INJECTION(func, NONE). So remove it from the EI_ETYPE and use appropriate errno instead. Signed-off-by: Masami Hiramatsu (Google) Fixes: 663faf9f7bee ("error-injection: Add injectable error types") --- Changes in v2: - include linux/errno.h --- include/asm-generic/error-injection.h | 1 - include/linux/error-injection.h | 3 ++- lib/error-inject.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/asm-generic/error-injection.h b/include/asm-generic/error-injection.h index fbca56bd9cbc..c0b9d3217ed9 100644 --- a/include/asm-generic/error-injection.h +++ b/include/asm-generic/error-injection.h @@ -4,7 +4,6 @@ #if defined(__KERNEL__) && !defined(__ASSEMBLY__) enum { - EI_ETYPE_NONE, /* Dummy value for undefined case */ EI_ETYPE_NULL, /* Return NULL if failure */ EI_ETYPE_ERRNO, /* Return -ERRNO if failure */ EI_ETYPE_ERRNO_NULL, /* Return -ERRNO or NULL if failure */ diff --git a/include/linux/error-injection.h b/include/linux/error-injection.h index 635a95caf29f..20e738f4eae8 100644 --- a/include/linux/error-injection.h +++ b/include/linux/error-injection.h @@ -3,6 +3,7 @@ #define _LINUX_ERROR_INJECTION_H #include +#include #include #ifdef CONFIG_FUNCTION_ERROR_INJECTION @@ -19,7 +20,7 @@ static inline bool within_error_injection_list(unsigned long addr) static inline int get_injectable_error_type(unsigned long addr) { - return EI_ETYPE_NONE; + return -EOPNOTSUPP; } #endif diff --git a/lib/error-inject.c b/lib/error-inject.c index 1afca1b1cdea..32c14770508e 100644 --- a/lib/error-inject.c +++ b/lib/error-inject.c @@ -40,7 +40,7 @@ bool within_error_injection_list(unsigned long addr) int get_injectable_error_type(unsigned long addr) { struct ei_entry *ent; - int ei_type = EI_ETYPE_NONE; + int ei_type = -EINVAL; mutex_lock(&ei_mutex); list_for_each_entry(ent, &error_injection_list, list) { From patchwork Tue Dec 13 14:11:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 32842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp153997wrn; Tue, 13 Dec 2022 06:31:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf4P27ESFaEXoWRoAJIXt/GYBODqe1ZMlBmgUC7SxHQiSNY9BzOdVYu7Ac5HFzO/JyFGbKtf X-Received: by 2002:a05:6402:3606:b0:461:57c1:84ea with SMTP id el6-20020a056402360600b0046157c184eamr15513046edb.3.1670941901663; Tue, 13 Dec 2022 06:31:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670941901; cv=none; d=google.com; s=arc-20160816; b=mA7MhaXRyYb1H9IAVBzIfLufmIGmXDQCc+VqxYfdSzNlE22fTN4vqtSzVuwhttV1V7 sICKzf33QeVvTbemxxRGA9cRoxov4cJezA+EKaEYeVFJYOJ8AgNa8XDqAAsa8qSgR70C CUjn+Wkt703nnO6K4oDOKRh7SjtZxAgioO60EAN3RjMfYX6W1nv2Ew7JKABCBRKA8TBO E5pbhgilRNx4Jm/O4NrY2j1j0fBEWPdLaw2vjyg94JS9+JzY7C3fVBdzRt9pxi2I+4Md EBTPF1PDL4klU695ng1pEaMLGQwc1rZjLtkd68NNPkegiDjGYIEaEM/vn6V/PENvWjGE dyQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=g3GlY7Vh6orvQnUAUjAJv+CcWmhyIPifSNFxlFeo3qg=; b=yyHBI1doXYAnn4AJKogjmEuUoFh+6uA2m9u7LSdLWV0Mtjp7genEiZUqbFjmTOq0EK YdnnUOPsV3D6WyOAwFHXwNaVo2hh1GegK4cn90TIcYY3OmCCWdEbyOkafaQVF9PA+uRR TIciHHhJAjUfbLm7Bxlb133ZeS1piE2SAkTAZQ/OJtOk+uG0MbOzVk9bmTRahDxPvGBZ Q+E8JHqS8UkGcXb+SIfMXX25ItuwcPba/aBTGFqFwnox02iaYBh0SXV+X5NAd98f9Een qA6w8GrxiLZ57Ov5kZkjmfJ8wwjkcfAxkYF7viOLxPYHxqaf/EjWDrcWX6+nMouTD4Gf oMlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SanjH8Fb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v19-20020a50a453000000b00461a7bb34c2si9264735edb.473.2022.12.13.06.31.17; Tue, 13 Dec 2022 06:31:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SanjH8Fb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235067AbiLMONP (ORCPT + 99 others); Tue, 13 Dec 2022 09:13:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235956AbiLMOMg (ORCPT ); Tue, 13 Dec 2022 09:12:36 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7429320BEF; Tue, 13 Dec 2022 06:11:39 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 307E4B81204; Tue, 13 Dec 2022 14:11:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A207FC433EF; Tue, 13 Dec 2022 14:11:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670940697; bh=ro10CqJEpDOOkWURK+9ji5eHns6LfgisIWEJmB7FR+c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SanjH8Fb0ODeBWUz3j1ivH32DpSj5Ca4m7gw/NdLbVmlty6u0V1Igza15BuLQxgLB C8Titz5NReQhGCN8R30WuDqou/FQyfgL2FdQZUgcaRPzUvfvOs5+rU5/N7AhVWNB/V sQTwrqX2Tddl8lLyDD1hT4yCWM7M/pz6nCvb/vRxKm/oSyvL/A5Ukc9ZseZ0fa19tU kyWwvHDQKxmasw7sd5DK3mucDS25TYLtGTADfR588x+nY7DbnQlcAzK1BsT0of3e0z UapAHLpim/YV8D0l/0a/9Sufg1kvJ3hJnlpyzYddMf9TpzB811gS9tU4a7GyZRLx3w gCZn8LpeY9ugw== From: "Masami Hiramatsu (Google)" To: LKML Cc: bpf@vger.kernel.org, Borislav Petkov , Alexei Starovoitov , Steven Rostedt , Linus Torvalds , Masami Hiramatsu , Andrew Morton , Peter Zijlstra , Kees Cook , Josh Poimboeuf , KP Singh , Mark Rutland , Florent Revest , Greg Kroah-Hartman , Christoph Hellwig , Chris Mason , Jonathan Corbet , linux-doc@vger.kernel.org, Akinobu Mita , Randy Dunlap Subject: [PATCH v2 2/2] docs: fault-injection: Add requirements of error injectable functions Date: Tue, 13 Dec 2022 23:11:31 +0900 Message-Id: <167094069168.608798.9644454927302716989.stgit@devnote3> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog In-Reply-To: <167094067084.608798.11303550366840600235.stgit@devnote3> References: <167094067084.608798.11303550366840600235.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752109575347423633?= X-GMAIL-MSGID: =?utf-8?q?1752109575347423633?= From: Masami Hiramatsu (Google) Add a section about the requirements of the error injectable functions and the type of errors. Since this section must be read before using ALLOW_ERROR_INJECTION() macro, that section is referred from the comment of the macro too. Signed-off-by: Masami Hiramatsu (Google) Link: https://lore.kernel.org/all/20221211115218.2e6e289bb85f8cf53c11aa97@kernel.org/T/#u --- Changes in v2: - Fix typos and misses according to Randy's comment. --- Documentation/fault-injection/fault-injection.rst | 65 +++++++++++++++++++++ include/asm-generic/error-injection.h | 6 +- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/Documentation/fault-injection/fault-injection.rst b/Documentation/fault-injection/fault-injection.rst index 17779a2772e5..76bc9c857761 100644 --- a/Documentation/fault-injection/fault-injection.rst +++ b/Documentation/fault-injection/fault-injection.rst @@ -233,6 +233,71 @@ proc entries This feature is intended for systematic testing of faults in a single system call. See an example below. + +Error Injectable Functions +-------------------------- + +This part is for the kenrel developers considering adding a function +using the ALLOW_ERROR_INJECTION() macro. + +Requirements for the Error Injectable Functions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Since the function-level error injection forcibly changes the code path +and returns an error even if the input and conditions are proper, this can +cause unexpected kernel crash if you allow error injection on the function +which is NOT error injectable. Thus, you (and reviewers) must ensure; + +- The function returns an error code if it fails, and the callers must check + it correctly (need to recover from it). + +- The function does not execute any code which can change any state before + the first error return. The state includes global or local, or input + variable. For example, clear output address storage (e.g. `*ret = NULL`), + increment/decrement counter, set a flag, preempt/irq disable or get + a lock (if those are recovered before returning error, that will be OK.) + +The first requirement is important, and it will result in that the release +(free objects) functions are usually harder to inject errors than allocate +functions. If errors of such release functions are not correctly handled +it will cause a memory leak easily (the caller will confuse that the object +has been released or corrupted.) + +The second one is for the caller which expects the function should always +do something. Thus if the function error injection skips all of the +function, the expectation is betrayed and causes an unexpected error. + +Type of the Error Injectable Functions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Each error injectable function will have the error type specified by the +ALLOW_ERROR_INJECTION() macro. You have to choose it carefully if you add +a new error injectable function. If the wrong error type is chosen, the +kernel may crash because it may not be able to handle the error. +There are 4 types of errors defined in include/asm-generic/error-injection.h + +EI_ETYPE_NULL + This function will return `NULL` if it fails. e.g. return an allocated + object address. + +EI_ETYPE_ERRNO + This function will return an `-errno` error code if it fails. e.g. return + -EINVAL if the input is wrong. This will include the functions which will + return an address which encodes `-errno` by ERR_PTR() macro. + +EI_ETYPE_ERRNO_NULL + This function will return an `-errno` or `NULL` if it fails. If the caller + of this function checks the return value with IS_ERR_OR_NULL() macro, this + type will be appropriate. + +EI_ETYPE_TRUE + This function will return `true` (non-zero positive value) if it fails. + +If you specify a wrong type, for example, EI_TYPE_ERRNO for the function +which returns an allocated object, it may cause a problem because the returned +value is not an object address and the caller can not access to the address. + + How to add new fault injection capability ----------------------------------------- diff --git a/include/asm-generic/error-injection.h b/include/asm-generic/error-injection.h index c0b9d3217ed9..18324d7aa56d 100644 --- a/include/asm-generic/error-injection.h +++ b/include/asm-generic/error-injection.h @@ -19,8 +19,10 @@ struct pt_regs; #ifdef CONFIG_FUNCTION_ERROR_INJECTION /* - * Whitelist generating macro. Specify functions which can be - * error-injectable using this macro. + * Whitelist generating macro. Specify functions which can be error-injectable + * using this macro. If you are unsure what is required for the error-injectable + * functions, please read Documentation/fault-injection/fault-injection.rst + * 'Error Injectable Functions' section. */ #define ALLOW_ERROR_INJECTION(fname, _etype) \ static struct error_injection_entry __used \