public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86, efi: Don't recursively acquire rtc_lock
@ 2011-08-03 21:04 Matt Fleming
  2011-08-03 23:10 ` Tony Luck
                   ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: Matt Fleming @ 2011-08-03 21:04 UTC (permalink / raw)
  To: linux-kernel
  Cc: Matt Fleming, Jan Beulich, Matthew Garrett, H. Peter Anvin,
	Tony Luck, Fenghua Yu, Alessandro Zummo

From: Matt Fleming <matt.fleming@linux.intel.com>

A deadlock was introduced on x86 in commit ef68c8f87ed1 ("x86:
Serialize EFI time accesses on rtc_lock") because efi_get_time() and
friends can be called with rtc_lock already held by
read_persistent_time(), e.g.

timekeeping_init()
    read_persistent_clock()	<-- acquire rtc_lock
        efi_get_time()
            phys_efi_get_time()	<-- acquire rtc_lock <DEADLOCK>

Move the locking up into the caller of efi.get_time() and provide some
wrappers for use in other parts of the kernel instead of calling
efi.get_time(), etc directly. This way we can hide the rtc_lock dance
inside of arch/x86.

Cc: Jan Beulich <JBeulich@novell.com>
Cc: Matthew Garrett <mjg@redhat.com>
Cc: "H. Peter Anvin" <hpa@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Signed-off-by: Matt Fleming <matt.fleming@linux.intel.com>
---
 arch/ia64/kernel/efi.c      |   10 ++++++++
 arch/x86/platform/efi/efi.c |   49 ++++++++++++++++++++++++++----------------
 drivers/char/efirtc.c       |    6 ++--
 drivers/rtc/rtc-efi.c       |    8 +++---
 include/linux/efi.h         |    4 ++-
 5 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/arch/ia64/kernel/efi.c b/arch/ia64/kernel/efi.c
index 6fc03af..775e05f 100644
--- a/arch/ia64/kernel/efi.c
+++ b/arch/ia64/kernel/efi.c
@@ -229,6 +229,16 @@ STUB_SET_VARIABLE(virt, id)
 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
 STUB_RESET_SYSTEM(virt, id)
 
+efi_status_t efi_get_time(efi_time_t *tm, efi_time_cap_t *cap)
+{
+	return efi.get_time(tm, cap);
+}
+
+efi_status_t efi_set_time(efi_time_t *tm)
+{
+	return efi.set_time(tm);
+}
+
 void
 efi_gettimeofday (struct timespec *ts)
 {
diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
index 3ae4128..735a470 100644
--- a/arch/x86/platform/efi/efi.c
+++ b/arch/x86/platform/efi/efi.c
@@ -89,24 +89,12 @@ early_param("add_efi_memmap", setup_add_efi_memmap);
 
 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
 {
-	unsigned long flags;
-	efi_status_t status;
-
-	spin_lock_irqsave(&rtc_lock, flags);
-	status = efi_call_virt2(get_time, tm, tc);
-	spin_unlock_irqrestore(&rtc_lock, flags);
-	return status;
+	return efi_call_virt2(get_time, tm, tc);
 }
 
 static efi_status_t virt_efi_set_time(efi_time_t *tm)
 {
-	unsigned long flags;
-	efi_status_t status;
-
-	spin_lock_irqsave(&rtc_lock, flags);
-	status = efi_call_virt1(set_time, tm);
-	spin_unlock_irqrestore(&rtc_lock, flags);
-	return status;
+	return efi_call_virt1(set_time, tm);
 }
 
 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
@@ -232,14 +220,11 @@ static efi_status_t __init phys_efi_set_virtual_address_map(
 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
 					     efi_time_cap_t *tc)
 {
-	unsigned long flags;
 	efi_status_t status;
 
-	spin_lock_irqsave(&rtc_lock, flags);
 	efi_call_phys_prelog();
 	status = efi_call_phys2(efi_phys.get_time, tm, tc);
 	efi_call_phys_epilog();
-	spin_unlock_irqrestore(&rtc_lock, flags);
 	return status;
 }
 
@@ -250,6 +235,8 @@ int efi_set_rtc_mmss(unsigned long nowtime)
 	efi_time_t 	eft;
 	efi_time_cap_t 	cap;
 
+	assert_spin_locked(&rtc_lock);
+
 	status = efi.get_time(&eft, &cap);
 	if (status != EFI_SUCCESS) {
 		printk(KERN_ERR "Oops: efitime: can't read time!\n");
@@ -272,12 +259,14 @@ int efi_set_rtc_mmss(unsigned long nowtime)
 	return 0;
 }
 
-unsigned long efi_get_time(void)
+unsigned long efi_get_time_locked(void)
 {
 	efi_status_t status;
 	efi_time_t eft;
 	efi_time_cap_t cap;
 
+	assert_spin_locked(&rtc_lock);
+
 	status = efi.get_time(&eft, &cap);
 	if (status != EFI_SUCCESS)
 		printk(KERN_ERR "Oops: efitime: can't read time!\n");
@@ -286,6 +275,28 @@ unsigned long efi_get_time(void)
 		      eft.minute, eft.second);
 }
 
+efi_status_t efi_get_time(efi_time_t *tm, efi_time_cap_t *cap)
+{
+	unsigned long flags;
+	efi_status_t status;
+
+	spin_lock_irqsave(&rtc_lock, flags);
+	status = efi.get_time(tm, cap);
+	spin_unlock_irqrestore(&rtc_lock, flags);
+	return status;
+}
+
+efi_status_t efi_set_time(efi_time_t *tm)
+{
+	unsigned long flags;
+	efi_status_t status;
+
+	spin_lock_irqsave(&rtc_lock, flags);
+	status = efi.set_time(tm);
+	spin_unlock_irqrestore(&rtc_lock, flags);
+	return status;
+}
+
 /*
  * Tell the kernel about the EFI memory map.  This might include
  * more than the max 128 entries that can fit in the e820 legacy
@@ -571,7 +582,7 @@ void __init efi_init(void)
 		do_add_efi_memmap();
 
 #ifdef CONFIG_X86_32
-	x86_platform.get_wallclock = efi_get_time;
+	x86_platform.get_wallclock = efi_get_time_locked;
 	x86_platform.set_wallclock = efi_set_rtc_mmss;
 #endif
 
diff --git a/drivers/char/efirtc.c b/drivers/char/efirtc.c
index 53c524e..cdb1bed 100644
--- a/drivers/char/efirtc.c
+++ b/drivers/char/efirtc.c
@@ -174,7 +174,7 @@ static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
 		case RTC_RD_TIME:
 			spin_lock_irqsave(&efi_rtc_lock, flags);
 
-			status = efi.get_time(&eft, &cap);
+			status = efi_get_time(&eft, &cap);
 
 			spin_unlock_irqrestore(&efi_rtc_lock,flags);
 
@@ -201,7 +201,7 @@ static long efi_rtc_ioctl(struct file *file, unsigned int cmd,
 
 			spin_lock_irqsave(&efi_rtc_lock, flags);
 
-			status = efi.set_time(&eft);
+			status = efi_set_time(&eft);
 
 			spin_unlock_irqrestore(&efi_rtc_lock,flags);
 
@@ -312,7 +312,7 @@ efi_rtc_get_status(char *buf)
 
 	spin_lock_irqsave(&efi_rtc_lock, flags);
 
-	efi.get_time(&eft, &cap);
+	efi_get_time(&eft, &cap);
 	efi.get_wakeup_time(&enabled, &pending, &alm);
 
 	spin_unlock_irqrestore(&efi_rtc_lock,flags);
diff --git a/drivers/rtc/rtc-efi.c b/drivers/rtc/rtc-efi.c
index 5502923..6307f5c 100644
--- a/drivers/rtc/rtc-efi.c
+++ b/drivers/rtc/rtc-efi.c
@@ -153,7 +153,7 @@ static int efi_read_time(struct device *dev, struct rtc_time *tm)
 	efi_time_t eft;
 	efi_time_cap_t cap;
 
-	status = efi.get_time(&eft, &cap);
+	status = efi_get_time(&eft, &cap);
 
 	if (status != EFI_SUCCESS) {
 		/* should never happen */
@@ -166,21 +166,21 @@ static int efi_read_time(struct device *dev, struct rtc_time *tm)
 	return rtc_valid_tm(tm);
 }
 
-static int efi_set_time(struct device *dev, struct rtc_time *tm)
+static int rtc_efi_set_time(struct device *dev, struct rtc_time *tm)
 {
 	efi_status_t status;
 	efi_time_t eft;
 
 	convert_to_efi_time(tm, &eft);
 
-	status = efi.set_time(&eft);
+	status = efi_set_time(&eft);
 
 	return status == EFI_SUCCESS ? 0 : -EINVAL;
 }
 
 static const struct rtc_class_ops efi_rtc_ops = {
 	.read_time = efi_read_time,
-	.set_time = efi_set_time,
+	.set_time = rtc_efi_set_time,
 	.read_alarm = efi_read_alarm,
 	.set_alarm = efi_set_alarm,
 };
diff --git a/include/linux/efi.h b/include/linux/efi.h
index c5709ed..57cd963 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -454,7 +454,9 @@ extern u64 efi_mem_attribute (unsigned long phys_addr, unsigned long size);
 extern int __init efi_uart_console_only (void);
 extern void efi_initialize_iomem_resources(struct resource *code_resource,
 		struct resource *data_resource, struct resource *bss_resource);
-extern unsigned long efi_get_time(void);
+extern unsigned long efi_get_time_locked(void);
+extern efi_status_t efi_get_time(efi_time_t *tm, efi_time_cap_t *cap);
+extern efi_status_t efi_set_time(efi_time_t *tm);
 extern int efi_set_rtc_mmss(unsigned long nowtime);
 extern void efi_reserve_boot_services(void);
 extern struct efi_memory_map memmap;
-- 
1.7.4.4


^ permalink raw reply related	[flat|nested] 25+ messages in thread
* Re: [PATCH] x86, efi: Don't recursively acquire rtc_lock
@ 2011-08-04  2:53 Jan Beulich
  2011-08-04  3:04 ` Matthew Garrett
  2011-08-04  9:33 ` Matt Fleming
  0 siblings, 2 replies; 25+ messages in thread
From: Jan Beulich @ 2011-08-04  2:53 UTC (permalink / raw)
  To: matt, linux-kernel; +Cc: fenghua.yu, tony.luck, hpa, matt.fleming, mjg, a.zummo

>>> Matt Fleming 08/03/11 11:04 PM >>> 
>From: Matt Fleming 
>
>A deadlock was introduced on x86 in commit ef68c8f87ed1 ("x86: 
>Serialize EFI time accesses on rtc_lock") because efi_get_time() and 
>friends can be called with rtc_lock already held by 
>read_persistent_time(), e.g. 
>
>timekeeping_init() 
>read_persistent_clock() <-- acquire rtc_lock 
>efi_get_time() 
>phys_efi_get_time() <-- acquire rtc_lock 
>
>Move the locking up into the caller of efi.get_time() and provide some 
>wrappers for use in other parts of the kernel instead of calling 
>efi.get_time(), etc directly. This way we can hide the rtc_lock dance 
>inside of arch/x86. 

Seems the wrong approach to me: The call happening with the lock held
is the wrong part imo, and hence the fix ought to be to drop the lock
there.

Jan

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: [PATCH] x86, efi: Don't recursively acquire rtc_lock
@ 2011-08-05 17:02 Jan Beulich
  0 siblings, 0 replies; 25+ messages in thread
From: Jan Beulich @ 2011-08-05 17:02 UTC (permalink / raw)
  To: matt, johnstul; +Cc: fenghua.yu, tony.luck, hpa, mjg, a.zummo, linux-kernel

>>> Matt Fleming  08/04/11 5:37 PM >>>
>On Thu, 2011-08-04 at 03:22 -0700, john stultz wrote:
>> Sorry if this should be obvious, but is there a reason your not using
>> your own internal lock for serializing the efi bits rather then using
>> the rtc_lock?
>
>Jan wrote the original code that started using the rtc_lock inside the
>EFI bits here, ef68c8f87ed1 ("x86: Serialize EFI time accesses on
>rtc_lock").
>
>Jan?
 
Matthew Garrett already replied to this - it's (on x86) a requirement of the EFI spec.

Jan


^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: [PATCH] x86, efi: Don't recursively acquire rtc_lock
@ 2011-08-05 17:04 Jan Beulich
  2011-08-08 13:40 ` Matt Fleming
  0 siblings, 1 reply; 25+ messages in thread
From: Jan Beulich @ 2011-08-05 17:04 UTC (permalink / raw)
  To: matt; +Cc: fenghua.yu, tony.luck, hpa, mjg, a.zummo, linux-kernel

>>> Matt Fleming  08/04/11 11:33 AM >>>
>On Thu, 2011-08-04 at 03:53 +0100, Jan Beulich wrote:
>> 
>> Seems the wrong approach to me: The call happening with the lock held
>> is the wrong part imo, and hence the fix ought to be to drop the lock
>> there.
>
>But what about other platforms that provide a get_wallclock()
>implementation such as the kvm or xen code? If we called get_wallclock()

Virtual platforms will have to take care of the serialization in the
host anyway, so the guest side implementation of getwallclock et al
is entirely unaffected.

>without rtc_lock held we'd be requiring everyone to lock it in their
>clock code, which is unnecessary work and increases the amount of code
>that touches rtc_lock (not to mention spreading it across several
>files).
>
>I think it's much better to do the locking as high up the callstack as
>possible and preferably in as few places as possible.

I agree to the "as few places as possible" part.

Jan


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2011-08-30 18:27 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-03 21:04 [PATCH] x86, efi: Don't recursively acquire rtc_lock Matt Fleming
2011-08-03 23:10 ` Tony Luck
2011-08-03 23:12 ` H. Peter Anvin
2011-08-04 10:22 ` john stultz
2011-08-04 10:36   ` Matt Fleming
2011-08-04 11:36   ` Matthew Garrett
  -- strict thread matches above, loose matches on Subject: below --
2011-08-04  2:53 Jan Beulich
2011-08-04  3:04 ` Matthew Garrett
2011-08-04  9:33 ` Matt Fleming
2011-08-05 17:02 Jan Beulich
2011-08-05 17:04 Jan Beulich
2011-08-08 13:40 ` Matt Fleming
2011-08-08 14:07   ` Jan Beulich
2011-08-10  9:03     ` Ingo Molnar
2011-08-10  9:36       ` Jan Beulich
2011-08-10  9:51         ` Ingo Molnar
2011-08-10 13:12           ` Jan Beulich
2011-08-15 18:18             ` Matt Fleming
2011-08-16  6:22               ` Jan Beulich
2011-08-16  9:14                 ` Matt Fleming
2011-08-30 15:45                   ` Matthew Garrett
2011-08-30 16:07                     ` Matt Fleming
2011-08-30 18:27                       ` H. Peter Anvin
2011-08-10 14:55           ` Jan Beulich
2011-08-08 14:18   ` Avi Kivity

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox