mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vegard Nossum <vegard.nossum@oracle.com>
To: Akinobu Mita <akinobu.mita@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	Vegard Nossum <vegard.nossum@oracle.com>,
	Hillf Danton <hillf.zj@alibaba-inc.com>
Subject: [PATCH 04/10] fault injection: prevent recursive fault injection
Date: Sun, 16 Oct 2016 17:56:06 +0200	[thread overview]
Message-ID: <20161016155612.4784-4-vegard.nossum@oracle.com> (raw)
In-Reply-To: <20161016155612.4784-1-vegard.nossum@oracle.com>

If something we call in the fail_dump() code path tries to acquire a
resource that might fail (due to fault injection), then we should not
try to recurse back into the fault injection code.

I've seen this happen with the console semaphore in the upcoming
semaphore trylock fault injection code.

Cc: Hillf Danton <hillf.zj@alibaba-inc.com>
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
---
 lib/fault-inject.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/lib/fault-inject.c b/lib/fault-inject.c
index 6a823a5..adba7c9 100644
--- a/lib/fault-inject.c
+++ b/lib/fault-inject.c
@@ -100,6 +100,33 @@ static inline bool fail_stacktrace(struct fault_attr *attr)
 
 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
 
+static DEFINE_PER_CPU(int, fault_active);
+
+static bool __fail(struct fault_attr *attr)
+{
+	bool ret = false;
+
+	/*
+	 * Prevent recursive fault injection (this could happen if for
+	 * example printing the fault would itself run some code that
+	 * could fail)
+	 */
+	preempt_disable();
+	if (unlikely(__this_cpu_inc_return(fault_active) != 1))
+		goto out;
+
+	ret = true;
+	fail_dump(attr);
+
+	if (atomic_read(&attr->times) != -1)
+		atomic_dec_not_zero(&attr->times);
+
+out:
+	__this_cpu_dec(fault_active);
+	preempt_enable();
+	return ret;
+}
+
 /*
  * This code is stolen from failmalloc-1.0
  * http://www.nongnu.org/failmalloc/
@@ -134,12 +161,7 @@ bool should_fail(struct fault_attr *attr, ssize_t size)
 	if (!fail_stacktrace(attr))
 		return false;
 
-	fail_dump(attr);
-
-	if (atomic_read(&attr->times) != -1)
-		atomic_dec_not_zero(&attr->times);
-
-	return true;
+	return __fail(attr);
 }
 EXPORT_SYMBOL_GPL(should_fail);
 
-- 
2.10.0.479.g221bd91

  parent reply	other threads:[~2016-10-16 15:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-16 15:56 [PATCH 01/10] fault injection: fix a few documentation errors Vegard Nossum
2016-10-16 15:56 ` [PATCH 02/10] fault injection: fix Kconfig menu Vegard Nossum
2016-10-18  9:32   ` Ulf Hansson
2016-10-16 15:56 ` [PATCH 03/10] fault injection: allow FAULT_INJECTION_STACKTRACE_FILTER on X86_64 Vegard Nossum
2016-10-16 15:56 ` Vegard Nossum [this message]
2016-10-17  3:58   ` [PATCH 04/10] fault injection: prevent recursive fault injection Hillf Danton
2016-10-16 15:56 ` [PATCH 05/10] fault injection: mutex_trylock() " Vegard Nossum
2016-10-16 16:27   ` Peter Zijlstra
2016-10-16 15:56 ` [PATCH 06/10] fault injection: rt_mutex_trylock() " Vegard Nossum
2016-10-16 16:27   ` Peter Zijlstra
2016-10-16 15:56 ` [PATCH 07/10] fault injection: down_trylock() " Vegard Nossum
2016-10-16 16:27   ` Peter Zijlstra
2016-10-16 15:56 ` [PATCH 08/10] fault injection: down_{read,write}_trylock() " Vegard Nossum
2016-10-16 16:27   ` Peter Zijlstra
2016-10-16 15:56 ` [PATCH 09/10] fault injection: spin_trylock() " Vegard Nossum
2016-10-16 16:28   ` Peter Zijlstra
2016-10-16 15:56 ` [PATCH 10/10] fault injection: inject faults in new/rare callchains Vegard Nossum
2016-10-21  0:32   ` Akinobu Mita
  -- strict thread matches above, loose matches on Subject: below --
2016-08-03 15:05 [PATCH 01/10] fault injection: fix a few documentation errors Vegard Nossum
2016-08-03 15:05 ` [PATCH 04/10] fault injection: prevent recursive fault injection Vegard Nossum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161016155612.4784-4-vegard.nossum@oracle.com \
    --to=vegard.nossum@oracle.com \
    --cc=akinobu.mita@gmail.com \
    --cc=hillf.zj@alibaba-inc.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®