Skip to contentSkip to conversation

soc/tegra: pmc: Turn powergates_lock into spinlock

PATCH In review Dmitry Osipenko 2 participants 7 messages Aug 30, 2018 – Oct 19, 2018 Tegra architecture support

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 soc/tegra: pmc: Turn powergates_lock into spinlock

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)
---
 drivers/soc/tegra/pmc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/soc/tegra/pmc.c +18 -18
index 2d6f3fcf3211..d6bc9f66f1cd 100644
@@ -186,7 +186,7 @@ struct tegra_pmc_soc {
  * @lp0_vec_phys: physical base address of the LP0 warm boot code
  * @lp0_vec_size: size of the LP0 warm boot code
  * @powergates_available: Bitmap of available power gates
- * @powergates_lock: mutex for power gate register access
+ * @powergates_lock: lock for power gate register access
  */
 struct tegra_pmc {
 	struct device *dev;
@@ -215,7 +215,7 @@ struct tegra_pmc {
 	u32 lp0_vec_size;
 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
 
-	struct mutex powergates_lock;
+	spinlock_t powergates_lock;
 };
 
 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
@@ -288,10 +288,10 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	if (tegra_powergate_state(id) == new_state) {
-		mutex_unlock(&pmc->powergates_lock);
+		spin_unlock(&pmc->powergates_lock);
 		return 0;
 	}
 
@@ -300,7 +300,7 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	err = readx_poll_timeout(tegra_powergate_state, id, status,
 				 status == new_state, 10, 100000);
 
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 
 	return err;
 }
@@ -309,7 +309,7 @@ static int __tegra_powergate_remove_clamping(unsigned int id)
 {
 	u32 mask;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	/*
 	 * On Tegra124 and later, the clamps for the GPU are controlled by a
@@ -336,7 +336,7 @@ static int __tegra_powergate_remove_clamping(unsigned int id)
 	tegra_pmc_writel(mask, REMOVE_CLAMPING);
 
 out:
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 
 	return 0;
 }
@@ -529,9 +529,9 @@ int tegra_powergate_is_powered(unsigned int id)
 	if (!tegra_powergate_is_valid(id))
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 	status = tegra_powergate_state(id);
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 
 	return status;
 }
@@ -998,7 +998,7 @@ int tegra_io_pad_power_enable(enum tegra_io_pad id)
 	u32 mask;
 	int err;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	err = tegra_io_pad_prepare(id, &request, &status, &mask);
 	if (err < 0) {
@@ -1017,7 +1017,7 @@ int tegra_io_pad_power_enable(enum tegra_io_pad id)
 	tegra_io_pad_unprepare();
 
 unlock:
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 	return err;
 }
 EXPORT_SYMBOL(tegra_io_pad_power_enable);
@@ -1034,7 +1034,7 @@ int tegra_io_pad_power_disable(enum tegra_io_pad id)
 	u32 mask;
 	int err;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	err = tegra_io_pad_prepare(id, &request, &status, &mask);
 	if (err < 0) {
@@ -1053,7 +1053,7 @@ int tegra_io_pad_power_disable(enum tegra_io_pad id)
 	tegra_io_pad_unprepare();
 
 unlock:
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 	return err;
 }
 EXPORT_SYMBOL(tegra_io_pad_power_disable);
@@ -1071,7 +1071,7 @@ int tegra_io_pad_set_voltage(enum tegra_io_pad id,
 	if (pad->voltage == UINT_MAX)
 		return -ENOTSUPP;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	/* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
 	value = tegra_pmc_readl(PMC_PWR_DET);
@@ -1088,7 +1088,7 @@ int tegra_io_pad_set_voltage(enum tegra_io_pad id,
 
 	tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
 
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 
 	usleep_range(100, 250);
 
@@ -1436,10 +1436,10 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 		return err;
 	}
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 	iounmap(pmc->base);
 	pmc->base = base;
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);
 
 	return 0;
 }
@@ -1919,7 +1919,7 @@ static int __init tegra_pmc_early_init(void)
 	struct resource regs;
 	bool invert;
 
-	mutex_init(&pmc->powergates_lock);
+	spin_lock_init(&pmc->powergates_lock);
 
 	np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
 	if (!np) {
Signature · 1 line
2.18.0
#1 lore raw

PATCH 2/2 soc/tegra: pmc: Make readx_poll_timeout atomic

This fixes splat like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y on Tegra30.

 BUG: sleeping function called from invalid context at drivers/soc/tegra/pmc.c:301
 in_atomic(): 1, irqs_disabled(): 0, pid: 1, name: swapper/0
 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.18.0-next-20180821-00176-g7a0f73ae4245 #824
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0588264>] (tegra_powergate_set+0xb0/0x1e8)
 [<c0588264>] (tegra_powergate_set) from [<c05899f4>] (tegra_pmc_cpu_power_on+0x44/0x50)
 [<c05899f4>] (tegra_pmc_cpu_power_on) from [<c0122554>] (tegra_boot_secondary+0x20c/0x288)
 [<c0122554>] (tegra_boot_secondary) from [<c011051c>] (__cpu_up+0xb8/0x14c)
 [<c011051c>] (__cpu_up) from [<c01282a0>] (bringup_cpu+0x30/0xfc)
 [<c01282a0>] (bringup_cpu) from [<c01276bc>] (cpuhp_invoke_callback+0xd0/0x98c)
 [<c01276bc>] (cpuhp_invoke_callback) from [<c0128b3c>] (_cpu_up+0xe0/0x1c0)
 [<c0128b3c>] (_cpu_up) from [<c0128e04>] (do_cpu_up+0x70/0xa4)
 [<c0128e04>] (do_cpu_up) from [<c0128e54>] (cpu_up+0x1c/0x20)
 [<c0128e54>] (cpu_up) from [<c12112e0>] (smp_init+0xb0/0x100)
 [<c12112e0>] (smp_init) from [<c12010d0>] (kernel_init_freeable+0x16c/0x398)
 [<c12010d0>] (kernel_init_freeable) from [<c0be76e4>] (kernel_init+0x18/0x124)
 [<c0be76e4>] (kernel_init) from [<c01010f0>] (ret_from_fork+0x14/0x24)
---
 drivers/soc/tegra/pmc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/soc/tegra/pmc.c +2 -2
index d6bc9f66f1cd..2d5d4b0c211a 100644
@@ -297,8 +297,8 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 
 	tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
 
-	err = readx_poll_timeout(tegra_powergate_state, id, status,
-				 status == new_state, 10, 100000);
+	err = readx_poll_timeout_atomic(tegra_powergate_state, id, status,
+					status == new_state, 10, 100000);
 
 	spin_unlock(&pmc->powergates_lock);
 
Signature · 1 line
2.18.0
#2 lore raw
In reply to Dmitry Osipenko
Quoted 24 lines · Dmitry Osipenko

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)
Quoted 1 line

ping

maintainer
#3 lore raw
In reply to Dmitry Osipenko
Quoted 15 lines · Dmitry Osipenko

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
Quoted 8 lines
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)

Given the above, rather than converting to a spinlock I wonder if we are just better off dropping the mutex completely from tegra_powergate_is_powered()? Otherwise ...

Quoted 41 lines
---
 drivers/soc/tegra/pmc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/soc/tegra/pmc.c +4 -4
index 2d6f3fcf3211..d6bc9f66f1cd 100644
@@ -186,7 +186,7 @@ struct tegra_pmc_soc {
  * @lp0_vec_phys: physical base address of the LP0 warm boot code
  * @lp0_vec_size: size of the LP0 warm boot code
  * @powergates_available: Bitmap of available power gates
- * @powergates_lock: mutex for power gate register access
+ * @powergates_lock: lock for power gate register access
  */
 struct tegra_pmc {
 	struct device *dev;
@@ -215,7 +215,7 @@ struct tegra_pmc {
 	u32 lp0_vec_size;
 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
 
-	struct mutex powergates_lock;
+	spinlock_t powergates_lock;
 };
 
 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
@@ -288,10 +288,10 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	if (tegra_powergate_state(id) == new_state) {
-		mutex_unlock(&pmc->powergates_lock);
+		spin_unlock(&pmc->powergates_lock);
 		return 0;
 	}
 

drivers/soc/tegra/pmc.c static int tegra_powergate_set(unsigned int id, bool new_state) · line 303

Quoted 5 lines
drivers/soc/tegra/pmc.c +1 -1
@@ -300,7 +300,7 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	err = readx_poll_timeout(tegra_powergate_state, id, status,
 				 status == new_state, 10, 100000);
 
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);

... the above readx_poll_timeout needs to be converted to the atomic version. Furthermore, the above 100ms timeout is probably not suited to spinlock.

Cheers
Jon

Signature · 1 line
nvpublic
#4 lore raw
Quoted 74 lines · Jon Hunter
Quoted 15 lines · Dmitry Osipenko

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
Quoted 8 lines
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)

Given the above, rather than converting to a spinlock I wonder if we are just better off dropping the mutex completely from tegra_powergate_is_powered()? Otherwise ...

Quoted 44 lines
---
 drivers/soc/tegra/pmc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/soc/tegra/pmc.c +4 -4
index 2d6f3fcf3211..d6bc9f66f1cd 100644
@@ -186,7 +186,7 @@ struct tegra_pmc_soc {
  * @lp0_vec_phys: physical base address of the LP0 warm boot code
  * @lp0_vec_size: size of the LP0 warm boot code
  * @powergates_available: Bitmap of available power gates
- * @powergates_lock: mutex for power gate register access
+ * @powergates_lock: lock for power gate register access
  */
 struct tegra_pmc {
 	struct device *dev;
@@ -215,7 +215,7 @@ struct tegra_pmc {
 	u32 lp0_vec_size;
 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
 
-	struct mutex powergates_lock;
+	spinlock_t powergates_lock;
 };
 
 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
@@ -288,10 +288,10 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	if (tegra_powergate_state(id) == new_state) {
-		mutex_unlock(&pmc->powergates_lock);
+		spin_unlock(&pmc->powergates_lock);
 		return 0;
 	}
 
@@ -300,7 +300,7 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	err = readx_poll_timeout(tegra_powergate_state, id, status,
 				 status == new_state, 10, 100000);
 
Quoted 6 lines

drivers/soc/tegra/pmc.c static int tegra_powergate_set(unsigned int id, bool new_state) · line 303

Quoted 2 lines
drivers/soc/tegra/pmc.c +1 -1
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);

... the above readx_poll_timeout needs to be converted to the atomic version. Furthermore, the above 100ms timeout is probably not suited to spinlock.

It's converted in the second patch. Seems mutex indeed could be dropped from tegra_powergate_is_powered, at least for now I can't recall why decided to keep the locking. Thank you for the review, I'll try to drop the mutex and come back with v2 if it will be fine.

maintainer
#5 lore raw
Quoted 75 lines · Dmitry Osipenko
Quoted 74 lines · Jon Hunter
Quoted 15 lines · Dmitry Osipenko

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
Quoted 8 lines
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)

Given the above, rather than converting to a spinlock I wonder if we are just better off dropping the mutex completely from tegra_powergate_is_powered()? Otherwise ...

Quoted 44 lines
---
 drivers/soc/tegra/pmc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/soc/tegra/pmc.c +4 -4
index 2d6f3fcf3211..d6bc9f66f1cd 100644
@@ -186,7 +186,7 @@ struct tegra_pmc_soc {
  * @lp0_vec_phys: physical base address of the LP0 warm boot code
  * @lp0_vec_size: size of the LP0 warm boot code
  * @powergates_available: Bitmap of available power gates
- * @powergates_lock: mutex for power gate register access
+ * @powergates_lock: lock for power gate register access
  */
 struct tegra_pmc {
 	struct device *dev;
@@ -215,7 +215,7 @@ struct tegra_pmc {
 	u32 lp0_vec_size;
 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
 
-	struct mutex powergates_lock;
+	spinlock_t powergates_lock;
 };
 
 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
@@ -288,10 +288,10 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	if (tegra_powergate_state(id) == new_state) {
-		mutex_unlock(&pmc->powergates_lock);
+		spin_unlock(&pmc->powergates_lock);
 		return 0;
 	}
 
@@ -300,7 +300,7 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	err = readx_poll_timeout(tegra_powergate_state, id, status,
 				 status == new_state, 10, 100000);
 
Quoted 8 lines
Quoted 6 lines

drivers/soc/tegra/pmc.c static int tegra_powergate_set(unsigned int id, bool new_state) · line 303

Quoted 2 lines
drivers/soc/tegra/pmc.c +1 -1
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);

... the above readx_poll_timeout needs to be converted to the atomic version. Furthermore, the above 100ms timeout is probably not suited to spinlock.

It's converted in the second patch. Seems mutex indeed could be dropped from tegra_powergate_is_powered, at least for now I can't recall why decided to keep the locking. Thank you for the review, I'll try to drop the mutex and come back with v2 if it will be fine.

Sorry if you were waiting for my response, but yes sounds good to me.

Cheers
Jon

Signature · 1 line
nvpublic
#6 lore raw
Quoted 84 lines · Jon Hunter
Quoted 82 lines · Dmitry Osipenko
Quoted 80 lines · Jon Hunter
Quoted 15 lines · Dmitry Osipenko

This fixes splats like the one below if CONFIG_DEBUG_ATOMIC_SLEEP=y and machine (Tegra30) booted with SMP=n or all secondary CPU's are put offline.

 BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254
 in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/0
 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        4.18.0-next-20180821-00180-gc3ebb6544e44-dirty #823
 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
 [<c01134f4>] (unwind_backtrace) from [<c010db2c>] (show_stack+0x20/0x24)
 [<c010db2c>] (show_stack) from [<c0bd0f3c>] (dump_stack+0x94/0xa8)
 [<c0bd0f3c>] (dump_stack) from [<c0151df8>] (___might_sleep+0x13c/0x174)
 [<c0151df8>] (___might_sleep) from [<c0151ea0>] (__might_sleep+0x70/0xa8)
 [<c0151ea0>] (__might_sleep) from [<c0bec2b8>] (mutex_lock+0x2c/0x70)
 [<c0bec2b8>] (mutex_lock) from [<c0589844>] (tegra_powergate_is_powered+0x44/0xa8)
 [<c0589844>] (tegra_powergate_is_powered) from [<c0581a60>] (tegra30_cpu_rail_off_ready+0x30/0x74)
Quoted 8 lines
 [<c0581a60>] (tegra30_cpu_rail_off_ready) from [<c0122244>] (tegra30_idle_lp2+0xa0/0x108)
 [<c0122244>] (tegra30_idle_lp2) from [<c0853438>] (cpuidle_enter_state+0x140/0x540)
 [<c0853438>] (cpuidle_enter_state) from [<c08538a4>] (cpuidle_enter+0x40/0x4c)
 [<c08538a4>] (cpuidle_enter) from [<c01595e0>] (call_cpuidle+0x30/0x48)
 [<c01595e0>] (call_cpuidle) from [<c01599f8>] (do_idle+0x238/0x28c)
 [<c01599f8>] (do_idle) from [<c0159d28>] (cpu_startup_entry+0x28/0x2c)
 [<c0159d28>] (cpu_startup_entry) from [<c0be76c8>] (rest_init+0xd8/0xdc)
 [<c0be76c8>] (rest_init) from [<c1200f50>] (start_kernel+0x41c/0x430)

Given the above, rather than converting to a spinlock I wonder if we are just better off dropping the mutex completely from tegra_powergate_is_powered()? Otherwise ...

Quoted 41 lines
---
 drivers/soc/tegra/pmc.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/soc/tegra/pmc.c +4 -4
index 2d6f3fcf3211..d6bc9f66f1cd 100644
@@ -186,7 +186,7 @@ struct tegra_pmc_soc {
  * @lp0_vec_phys: physical base address of the LP0 warm boot code
  * @lp0_vec_size: size of the LP0 warm boot code
  * @powergates_available: Bitmap of available power gates
- * @powergates_lock: mutex for power gate register access
+ * @powergates_lock: lock for power gate register access
  */
 struct tegra_pmc {
 	struct device *dev;
@@ -215,7 +215,7 @@ struct tegra_pmc {
 	u32 lp0_vec_size;
 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
 
-	struct mutex powergates_lock;
+	spinlock_t powergates_lock;
 };
 
 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
@@ -288,10 +288,10 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
 		return -EINVAL;
 
-	mutex_lock(&pmc->powergates_lock);
+	spin_lock(&pmc->powergates_lock);
 
 	if (tegra_powergate_state(id) == new_state) {
-		mutex_unlock(&pmc->powergates_lock);
+		spin_unlock(&pmc->powergates_lock);
 		return 0;
 	}
 

drivers/soc/tegra/pmc.c static int tegra_powergate_set(unsigned int id, bool new_state) · line 303

Quoted 5 lines
drivers/soc/tegra/pmc.c +1 -1
@@ -300,7 +300,7 @@ static int tegra_powergate_set(unsigned int id, bool new_state)
 	err = readx_poll_timeout(tegra_powergate_state, id, status,
 				 status == new_state, 10, 100000);
 
-	mutex_unlock(&pmc->powergates_lock);
+	spin_unlock(&pmc->powergates_lock);

... the above readx_poll_timeout needs to be converted to the atomic version. Furthermore, the above 100ms timeout is probably not suited to spinlock.

Quoted 3 lines
Quoted 1 line

It's converted in the second patch. Seems mutex indeed could be dropped from tegra_powergate_is_powered, at least for now I can't recall why decided to keep the locking. Thank you for the review, I'll try to drop the mutex and come back with v2 if it will be fine.

Sorry if you were waiting for my response, but yes sounds good to me.

I was just a bit busy lately, but thank you very much for the attention. Sorry if you were waiting for the update of the patch, very likely that I'll update this and some of the other patches around Monday.