All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	Prarit Bhargava <prarit@redhat.com>,
	Richard Cochran <richardcochran@gmail.com>,
	Ingo Molnar <mingo@kernel.org>,
	Sasha Levin <sasha.levin@oracle.com>,
	John Stultz <john.stultz@linaro.org>
Subject: [PATCH 3.10 75/79] timekeeping: Avoid possible deadlock from clock_was_set_delayed
Date: Tue, 11 Feb 2014 11:06:19 -0800	[thread overview]
Message-ID: <20140211184723.140485463@linuxfoundation.org> (raw)
In-Reply-To: <20140211184720.928667275@linuxfoundation.org>

3.10-stable review patch.  If anyone has any objections, please let me know.

------------------

From: John Stultz <john.stultz@linaro.org>

commit 6fdda9a9c5db367130cf32df5d6618d08b89f46a upstream.

As part of normal operaions, the hrtimer subsystem frequently calls
into the timekeeping code, creating a locking order of
  hrtimer locks -> timekeeping locks

clock_was_set_delayed() was suppoed to allow us to avoid deadlocks
between the timekeeping the hrtimer subsystem, so that we could
notify the hrtimer subsytem the time had changed while holding
the timekeeping locks. This was done by scheduling delayed work
that would run later once we were out of the timekeeing code.

But unfortunately the lock chains are complex enoguh that in
scheduling delayed work, we end up eventually trying to grab
an hrtimer lock.

Sasha Levin noticed this in testing when the new seqlock lockdep
enablement triggered the following (somewhat abrieviated) message:

[  251.100221] ======================================================
[  251.100221] [ INFO: possible circular locking dependency detected ]
[  251.100221] 3.13.0-rc2-next-20131206-sasha-00005-g8be2375-dirty #4053 Not tainted
[  251.101967] -------------------------------------------------------
[  251.101967] kworker/10:1/4506 is trying to acquire lock:
[  251.101967]  (timekeeper_seq){----..}, at: [<ffffffff81160e96>] retrigger_next_event+0x56/0x70
[  251.101967]
[  251.101967] but task is already holding lock:
[  251.101967]  (hrtimer_bases.lock#11){-.-...}, at: [<ffffffff81160e7c>] retrigger_next_event+0x3c/0x70
[  251.101967]
[  251.101967] which lock already depends on the new lock.
[  251.101967]
[  251.101967]
[  251.101967] the existing dependency chain (in reverse order) is:
[  251.101967]
-> #5 (hrtimer_bases.lock#11){-.-...}:
[snipped]
-> #4 (&rt_b->rt_runtime_lock){-.-...}:
[snipped]
-> #3 (&rq->lock){-.-.-.}:
[snipped]
-> #2 (&p->pi_lock){-.-.-.}:
[snipped]
-> #1 (&(&pool->lock)->rlock){-.-...}:
[  251.101967]        [<ffffffff81194803>] validate_chain+0x6c3/0x7b0
[  251.101967]        [<ffffffff81194d9d>] __lock_acquire+0x4ad/0x580
[  251.101967]        [<ffffffff81194ff2>] lock_acquire+0x182/0x1d0
[  251.101967]        [<ffffffff84398500>] _raw_spin_lock+0x40/0x80
[  251.101967]        [<ffffffff81153e69>] __queue_work+0x1a9/0x3f0
[  251.101967]        [<ffffffff81154168>] queue_work_on+0x98/0x120
[  251.101967]        [<ffffffff81161351>] clock_was_set_delayed+0x21/0x30
[  251.101967]        [<ffffffff811c4bd1>] do_adjtimex+0x111/0x160
[  251.101967]        [<ffffffff811e2711>] compat_sys_adjtimex+0x41/0x70
[  251.101967]        [<ffffffff843a4b49>] ia32_sysret+0x0/0x5
[  251.101967]
-> #0 (timekeeper_seq){----..}:
[snipped]
[  251.101967] other info that might help us debug this:
[  251.101967]
[  251.101967] Chain exists of:
  timekeeper_seq --> &rt_b->rt_runtime_lock --> hrtimer_bases.lock#11

[  251.101967]  Possible unsafe locking scenario:
[  251.101967]
[  251.101967]        CPU0                    CPU1
[  251.101967]        ----                    ----
[  251.101967]   lock(hrtimer_bases.lock#11);
[  251.101967]                                lock(&rt_b->rt_runtime_lock);
[  251.101967]                                lock(hrtimer_bases.lock#11);
[  251.101967]   lock(timekeeper_seq);
[  251.101967]
[  251.101967]  *** DEADLOCK ***
[  251.101967]
[  251.101967] 3 locks held by kworker/10:1/4506:
[  251.101967]  #0:  (events){.+.+.+}, at: [<ffffffff81154960>] process_one_work+0x200/0x530
[  251.101967]  #1:  (hrtimer_work){+.+...}, at: [<ffffffff81154960>] process_one_work+0x200/0x530
[  251.101967]  #2:  (hrtimer_bases.lock#11){-.-...}, at: [<ffffffff81160e7c>] retrigger_next_event+0x3c/0x70
[  251.101967]
[  251.101967] stack backtrace:
[  251.101967] CPU: 10 PID: 4506 Comm: kworker/10:1 Not tainted 3.13.0-rc2-next-20131206-sasha-00005-g8be2375-dirty #4053
[  251.101967] Workqueue: events clock_was_set_work

So the best solution is to avoid calling clock_was_set_delayed() while
holding the timekeeping lock, and instead using a flag variable to
decide if we should call clock_was_set() once we've released the locks.

This works for the case here, where the do_adjtimex() was the deadlock
trigger point. Unfortuantely, in update_wall_time() we still hold
the jiffies lock, which would deadlock with the ipi triggered by
clock_was_set(), preventing us from calling it even after we drop the
timekeeping lock. So instead call clock_was_set_delayed() at that point.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Sasha Levin <sasha.levin@oracle.com>
Reported-by: Sasha Levin <sasha.levin@oracle.com>
Tested-by: Sasha Levin <sasha.levin@oracle.com>
Signed-off-by: John Stultz <john.stultz@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 kernel/time/timekeeping.c |   25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1239,9 +1239,10 @@ out_adjust:
  * It also calls into the NTP code to handle leapsecond processing.
  *
  */
-static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
+static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
 {
 	u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
+	unsigned int clock_set = 0;
 
 	while (tk->xtime_nsec >= nsecps) {
 		int leap;
@@ -1263,9 +1264,10 @@ static inline void accumulate_nsecs_to_s
 
 			__timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
 
-			clock_was_set_delayed();
+			clock_set = 1;
 		}
 	}
+	return clock_set;
 }
 
 /**
@@ -1278,7 +1280,8 @@ static inline void accumulate_nsecs_to_s
  * Returns the unconsumed cycles.
  */
 static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
-						u32 shift)
+						u32 shift,
+						unsigned int *clock_set)
 {
 	cycle_t interval = tk->cycle_interval << shift;
 	u64 raw_nsecs;
@@ -1292,7 +1295,7 @@ static cycle_t logarithmic_accumulation(
 	tk->cycle_last += interval;
 
 	tk->xtime_nsec += tk->xtime_interval << shift;
-	accumulate_nsecs_to_secs(tk);
+	*clock_set |= accumulate_nsecs_to_secs(tk);
 
 	/* Accumulate raw time */
 	raw_nsecs = (u64)tk->raw_interval << shift;
@@ -1350,6 +1353,7 @@ static void update_wall_time(void)
 	struct timekeeper *tk = &shadow_timekeeper;
 	cycle_t offset;
 	int shift = 0, maxshift;
+	unsigned int clock_set = 0;
 	unsigned long flags;
 
 	raw_spin_lock_irqsave(&timekeeper_lock, flags);
@@ -1384,7 +1388,8 @@ static void update_wall_time(void)
 	maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
 	shift = min(shift, maxshift);
 	while (offset >= tk->cycle_interval) {
-		offset = logarithmic_accumulation(tk, offset, shift);
+		offset = logarithmic_accumulation(tk, offset, shift,
+							&clock_set);
 		if (offset < tk->cycle_interval<<shift)
 			shift--;
 	}
@@ -1402,7 +1407,7 @@ static void update_wall_time(void)
 	 * Finally, make sure that after the rounding
 	 * xtime_nsec isn't larger than NSEC_PER_SEC
 	 */
-	accumulate_nsecs_to_secs(tk);
+	clock_set |= accumulate_nsecs_to_secs(tk);
 
 	write_seqcount_begin(&timekeeper_seq);
 	/* Update clock->cycle_last with the new value */
@@ -1422,6 +1427,10 @@ static void update_wall_time(void)
 	write_seqcount_end(&timekeeper_seq);
 out:
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
+	if (clock_set)
+		/* have to call outside the timekeeper_seq */
+		clock_was_set_delayed();
+
 }
 
 /**
@@ -1681,11 +1690,13 @@ int do_adjtimex(struct timex *txc)
 	if (tai != orig_tai) {
 		__timekeeping_set_tai_offset(tk, tai);
 		timekeeping_update(tk, false, true);
-		clock_was_set_delayed();
 	}
 	write_seqcount_end(&timekeeper_seq);
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
 
+	if (tai != orig_tai)
+		clock_was_set();
+
 	ntp_notify_cmos_timer();
 
 	return ret;



  parent reply	other threads:[~2014-02-11 19:21 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-11 19:05 [PATCH 3.10 00/79] 3.10.30-stable review Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 01/79] SELinux: Fix memory leak upon loading policy Greg Kroah-Hartman
2014-02-11 19:05   ` Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 02/79] tracing: Have trace buffer point back to trace_array Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 03/79] tracing: Check if tracing is enabled in trace_puts() Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 04/79] arch/sh/kernel/kgdb.c: add missing #include <linux/sched.h> Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 05/79] intel-iommu: fix off-by-one in pagetable freeing Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 06/79] Revert "EISA: Initialize device before its resources" Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 07/79] fuse: fix pipe_buf_operations Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 08/79] audit: reset audit backlog wait time after error recovery Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 09/79] audit: correct a type mismatch in audit_syscall_exit() Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 10/79] mm/memory-failure.c: shift page lock from head page to tail page after thp split Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 11/79] mm/page-writeback.c: fix dirty_balance_reserve subtraction from dirtyable memory Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 12/79] mm/page-writeback.c: do not count anon pages as " Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 13/79] mmc: fix host release issue after discard operation Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 14/79] mmc: atmel-mci: fix timeout errors in SDIO mode when using DMA Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 15/79] slub: Fix calculation of cpu slabs Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 16/79] turbostat: Dont put unprocessed uapi headers in the include path Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 18/79] ACPI / init: Flag use of ACPI and ACPI idioms for power supplies to regulator API Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 19/79] compat: fix sys_fanotify_mark Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 20/79] fs/compat: fix parameter handling for compat readv/writev syscalls Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 21/79] fs/compat: fix lookup_dcookie() parameter handling Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 22/79] tile: remove compat_sys_lookup_dcookie declaration to fix compile error Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 23/79] mtd: mxc_nand: remove duplicated ecc_stats counting Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 24/79] ore: Fix wrong math in allocation of per device BIO Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 25/79] xtensa: xtfpga: fix definitions of platform devices Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 26/79] IB/qib: Fix QP check when looping back to/from QP1 Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 27/79] spi/bcm63xx: dont substract prepend length from total length Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 28/79] spidev: fix hang when transfer_one_message fails Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 29/79] NFSv4: OPEN must handle the NFS4ERR_IO return code correctly Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 30/79] nfs4.1: properly handle ENOTSUP in SECINFO_NO_NAME Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 31/79] NFSv4.1: Handle errors correctly in nfs41_walk_client_list Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 32/79] nfs4: fix discover_server_trunking use after free Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 33/79] pnfs: Proper delay for NFS4ERR_RECALLCONFLICT in layout_get_done Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 34/79] sunrpc: Fix infinite loop in RPC state machine Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 35/79] dm thin: fix discard support to a previously shared block Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 36/79] dm thin: initialize dm_thin_new_mapping returned by get_next_mapping Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 37/79] dm: wait until embedded kobject is released before destroying a device Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 38/79] dm space map common: make sure new space is used during extend Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 39/79] dm space map metadata: fix extending the space map Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 40/79] dm space map metadata: fix bug in resizing of thin metadata Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 43/79] mm, oom: base root bonus on current usage Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 44/79] media: anysee: fix non-working E30 Combo Plus DVB-T Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 45/79] [media] dib8000: make 32 bits read atomic Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 46/79] [media] media: s5p_mfc: remove s5p_mfc_get_node_type() function Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 47/79] [media] nxt200x: increase write buffer size Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 48/79] [media] dib8000: fix regression with dib807x Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 49/79] [media] m88rs2000: add m88rs2000_set_carrieroffset Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 50/79] [media] m88rs2000: set symbol rate accurately Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 52/79] drm/radeon: disable ss on DP for DCE3.x Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 53/79] drm/radeon: fix surface sync in fence on cayman (v2) Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 54/79] drm/radeon: set the full cache bit for fences on r7xx+ Greg Kroah-Hartman
2014-02-11 19:05 ` [PATCH 3.10 55/79] drm/radeon: fix DAC interrupt handling on DCE5+ Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 56/79] drm/radeon/DCE4+: clear bios scratch dpms bit (v2) Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 57/79] dm sysfs: fix a module unload race Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 58/79] drm/nouveau: fix m2mf copy to tiled gart Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 59/79] drm/i915: Flush outstanding requests before allocating new seqno Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 60/79] drm/i915: Fix the offset issue for the stolen GEM objects Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 61/79] drm/i915: VLV2 - Fix hotplug detect bits Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 62/79] i915: remove pm_qos request on error Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 63/79] drm/cirrus: correct register values for 16bpp Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 64/79] drm/mgag200: fix typo causing bw limits to be ignored on some chips Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 65/79] mfd: lpc_ich: Add support for Intel Avoton SoC Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 66/79] mfd: lpc_ich: iTCO_wdt patch for Intel Coleto Creek DeviceIDs Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 67/79] i2c: i801: SMBus " Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 68/79] ftrace: Synchronize setting function_trace_op with ftrace_trace_function Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 69/79] ftrace: Fix synchronization location disabling and freeing ftrace_ops Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 70/79] ftrace: Have function graph only trace based on global_ops filters Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 71/79] timekeeping: Fix lost updates to tai adjustment Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 72/79] timekeeping: Fix CLOCK_TAI timer/nanosleep delays Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 73/79] timekeeping: Fix missing timekeeping_update in suspend path Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 74/79] rtc-cmos: Add an alarm disable quirk Greg Kroah-Hartman
2014-02-11 19:06 ` Greg Kroah-Hartman [this message]
2014-02-11 19:06 ` [PATCH 3.10 76/79] intel_pstate: Add Haswell CPU models Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 77/79] intel_pstate: fix no_turbo Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 78/79] intel_pstate: Improve accuracy by not truncating until final result Greg Kroah-Hartman
2014-02-11 19:06 ` [PATCH 3.10 79/79] intel_pstate: Correct calculation of min pstate value Greg Kroah-Hartman
2014-02-12  4:20 ` [PATCH 3.10 00/79] 3.10.30-stable review Guenter Roeck
2014-02-12 18:57   ` Shuah Khan

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=20140211184723.140485463@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=prarit@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=sasha.levin@oracle.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.