Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

docs: fault-injection: add requirements of error injectable functions

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.

Link: https://lkml.kernel.org/r/167081321427.387937.15475445689482551048.stgit@devnote3
Link: https://lore.kernel.org/all/20221211115218.2e6e289bb85f8cf53c11aa97@kernel.org/T/#u
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Chris Mason <clm@meta.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Florent Revest <revest@chromium.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Masami Hiramatsu (Google) and committed by
Andrew Morton
bef7ec4e 6338bb05

+69 -2
+65
Documentation/fault-injection/fault-injection.rst
··· 231 231 This feature is intended for systematic testing of faults in a single 232 232 system call. See an example below. 233 233 234 + 235 + Error Injectable Functions 236 + -------------------------- 237 + 238 + This part is for the kenrel developers considering to add a function to 239 + ALLOW_ERROR_INJECTION() macro. 240 + 241 + Requirements for the Error Injectable Functions 242 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 243 + 244 + Since the function-level error injection forcibly changes the code path 245 + and returns an error even if the input and conditions are proper, this can 246 + cause unexpected kernel crash if you allow error injection on the function 247 + which is NOT error injectable. Thus, you (and reviewers) must ensure; 248 + 249 + - The function returns an error code if it fails, and the callers must check 250 + it correctly (need to recover from it). 251 + 252 + - The function does not execute any code which can change any state before 253 + the first error return. The state includes global or local, or input 254 + variable. For example, clear output address storage (e.g. `*ret = NULL`), 255 + increments/decrements counter, set a flag, preempt/irq disable or get 256 + a lock (if those are recovered before returning error, that will be OK.) 257 + 258 + The first requirement is important, and it will result in that the release 259 + (free objects) functions are usually harder to inject errors than allocate 260 + functions. If errors of such release functions are not correctly handled 261 + it will cause a memory leak easily (the caller will confuse that the object 262 + has been released or corrupted.) 263 + 264 + The second one is for the caller which expects the function should always 265 + does something. Thus if the function error injection skips whole of the 266 + function, the expectation is betrayed and causes an unexpected error. 267 + 268 + Type of the Error Injectable Functions 269 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 270 + 271 + Each error injectable functions will have the error type specified by the 272 + ALLOW_ERROR_INJECTION() macro. You have to choose it carefully if you add 273 + a new error injectable function. If the wrong error type is chosen, the 274 + kernel may crash because it may not be able to handle the error. 275 + There are 4 types of errors defined in include/asm-generic/error-injection.h 276 + 277 + EI_ETYPE_NULL 278 + This function will return `NULL` if it fails. e.g. return an allocateed 279 + object address. 280 + 281 + EI_ETYPE_ERRNO 282 + This function will return an `-errno` error code if it fails. e.g. return 283 + -EINVAL if the input is wrong. This will include the functions which will 284 + return an address which encodes `-errno` by ERR_PTR() macro. 285 + 286 + EI_ETYPE_ERRNO_NULL 287 + This function will return an `-errno` or `NULL` if it fails. If the caller 288 + of this function checks the return value with IS_ERR_OR_NULL() macro, this 289 + type will be appropriate. 290 + 291 + EI_ETYPE_TRUE 292 + This function will return `true` (non-zero positive value) if it fails. 293 + 294 + If you specifies a wrong type, for example, EI_TYPE_ERRNO for the function 295 + which returns an allocated object, it may cause a problem because the returned 296 + value is not an object address and the caller can not access to the address. 297 + 298 + 234 299 How to add new fault injection capability 235 300 ----------------------------------------- 236 301
+4 -2
include/asm-generic/error-injection.h
··· 19 19 20 20 #ifdef CONFIG_FUNCTION_ERROR_INJECTION 21 21 /* 22 - * Whitelist generating macro. Specify functions which can be 23 - * error-injectable using this macro. 22 + * Whitelist generating macro. Specify functions which can be error-injectable 23 + * using this macro. If you unsure what is required for the error-injectable 24 + * functions, please read Documentation/fault-injection/fault-injection.rst 25 + * 'Error Injectable Functions' section. 24 26 */ 25 27 #define ALLOW_ERROR_INJECTION(fname, _etype) \ 26 28 static struct error_injection_entry __used \