Skip to contentSkip to conversation

rtc: 88pm860x: fix possible race condition

PATCH New Alexandre Belloni 2 participants 7 messages Mar 11, 2020 – Mar 12, 2020 Real time clock (RTC)

lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/6 rtc: 88pm860x: fix possible race condition

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in the IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

Also remove the unnecessary error message as the core already prints the info.

---
 drivers/rtc/rtc-88pm860x.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/rtc/rtc-88pm860x.c +8 -6
index 4743b16a8d84..1526402e126b 100644
@@ -336,6 +336,10 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 	info->dev = &pdev->dev;
 	dev_set_drvdata(&pdev->dev, info);
 
+	info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(info->rtc_dev))
+		return PTR_ERR(info->rtc_dev);
+
 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
 					rtc_update_handler, IRQF_ONESHOT, "rtc",
 					info);
@@ -377,13 +381,11 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 		}
 	}
 
-	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "88pm860x-rtc",
-					    &pm860x_rtc_ops, THIS_MODULE);
-	ret = PTR_ERR(info->rtc_dev);
-	if (IS_ERR(info->rtc_dev)) {
-		dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
+	info->rtc_dev->ops = &pm860x_rtc_ops;
+
+	ret = rtc_register_device(info->rtc_dev);
+	if (ret)
 		return ret;
-	}
 
 	/*
 	 * enable internal XO instead of internal 3.25MHz clock since it can
Signature · 1 line
2.24.1
author maintainer
#1 lore raw

PATCH 2/6 rtc: 88pm860x: stop setting a default time

It doesn't make sense to set the RTC to a default value at probe time. Let the core handle invalid date and time.

---
 drivers/rtc/rtc-88pm860x.c | 13 -------------
 1 file changed, 13 deletions(-)
drivers/rtc/rtc-88pm860x.c +0 -13
index 1526402e126b..d6aca28905e3 100644
@@ -360,19 +360,6 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "Failed to read initial time.\n");
 		return ret;
 	}
-	if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
-		tm.tm_year = 70;
-		tm.tm_mon = 0;
-		tm.tm_mday = 1;
-		tm.tm_hour = 0;
-		tm.tm_min = 0;
-		tm.tm_sec = 0;
-		ret = pm860x_rtc_set_time(&pdev->dev, &tm);
-		if (ret < 0) {
-			dev_err(&pdev->dev, "Failed to set initial time.\n");
-			return ret;
-		}
-	}
 	rtc_tm_to_time(&tm, &ticks);
 	if (pm860x_rtc_dt_init(pdev, info)) {
 		if (pdata && pdata->sync) {
Signature · 1 line
2.24.1
author maintainer
#2 lore raw

PATCH 3/6 rtc: 88pm860x: stop calling unused callback

In reply to Alexandre Belloni

pdata->sync is not defined by any platform, stop calling it.

---
 drivers/rtc/rtc-88pm860x.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
drivers/rtc/rtc-88pm860x.c +1 -17
index d6aca28905e3..0abf2b194938 100644
@@ -28,7 +28,6 @@ struct pm860x_rtc_info {
 
 	int			irq;
 	int			vrtc;
-	int			(*sync)(unsigned int ticks);
 };
 
 #define REG_VRTC_MEAS1		0x7D
@@ -155,8 +154,6 @@ static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	pm860x_page_reg_write(info->i2c, REG2_DATA, (base >> 8) & 0xFF);
 	pm860x_page_reg_write(info->i2c, REG3_DATA, base & 0xFF);
 
-	if (info->sync)
-		info->sync(ticks);
 	return 0;
 }
 
@@ -317,8 +314,6 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
 	struct pm860x_rtc_pdata *pdata = NULL;
 	struct pm860x_rtc_info *info;
-	struct rtc_time tm;
-	unsigned long ticks = 0;
 	int ret;
 
 	pdata = dev_get_platdata(&pdev->dev);
@@ -355,18 +350,7 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 	pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
 	pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
 
-	ret = pm860x_rtc_read_time(&pdev->dev, &tm);
-	if (ret < 0) {
-		dev_err(&pdev->dev, "Failed to read initial time.\n");
-		return ret;
-	}
-	rtc_tm_to_time(&tm, &ticks);
-	if (pm860x_rtc_dt_init(pdev, info)) {
-		if (pdata && pdata->sync) {
-			pdata->sync(ticks);
-			info->sync = pdata->sync;
-		}
-	}
+	pm860x_rtc_dt_init(pdev, info);
 
 	info->rtc_dev->ops = &pm860x_rtc_ops;
 
Signature · 1 line
2.24.1
#3 lore raw

Re: [PATCH 3/6] rtc: 88pm860x: stop calling unused callback

I love your patch! Perhaps something to improve:

Hi Alexandre,

I love your patch! Perhaps something to improve:

[auto build test WARNING on abelloni/rtc-next]
[also build test WARNING on v5.6-rc5 next-20200311]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Alexandre-Belloni/rtc-88pm860x-fix-possible-race-condition/20200312-073738
base:   https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next
config: i386-randconfig-h002-20200311 (attached as .config)
compiler: gcc-7 (Debian 7.5.0-5) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

drivers/rtc/rtc-88pm860x.c: In function 'pm860x_rtc_probe':
drivers/rtc/rtc-88pm860x.c:309:34: warning: statement with no effect [-Wunused-value]

    #define pm860x_rtc_dt_init(x, y) (-1)
                                     ^
>> drivers/rtc/rtc-88pm860x.c:353:2: note: in expansion of macro 'pm860x_rtc_dt_init'
     pm860x_rtc_dt_init(pdev, info);
     ^~~~~~~~~~~~~~~~~~

Cyclomatic Complexity 5 include/linux/compiler.h:__write_once_size Cyclomatic Complexity 1 include/linux/list.h:INIT_LIST_HEAD Cyclomatic Complexity 1 include/linux/err.h:PTR_ERR
Cyclomatic Complexity 1 include/linux/workqueue.h:__init_work Cyclomatic Complexity 3 include/linux/pm_wakeup.h:device_may_wakeup Cyclomatic Complexity 1 include/linux/device.h:dev_get_drvdata Cyclomatic Complexity 1 include/linux/device.h:dev_set_drvdata Cyclomatic Complexity 1 include/linux/device.h:dev_get_platdata Cyclomatic Complexity 1 include/linux/platform_device.h:platform_get_drvdata Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_suspend Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_resume Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_driver_init Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_remove Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_alarm_irq_enable Cyclomatic Complexity 1 include/linux/rtc.h:rtc_time_to_tm Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_read_alarm Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_read_time Cyclomatic Complexity 1 include/linux/rtc.h:rtc_tm_to_time Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:rtc_next_alarm_time Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_set_alarm Cyclomatic Complexity 2 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_set_time Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:rtc_update_handler Cyclomatic Complexity 1 include/linux/device.h:devm_kzalloc Cyclomatic Complexity 1 include/linux/err.h:IS_ERR
Cyclomatic Complexity 1 include/linux/workqueue.h:queue_delayed_work Cyclomatic Complexity 1 include/linux/workqueue.h:schedule_delayed_work Cyclomatic Complexity 9 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_probe Cyclomatic Complexity 6 drivers/rtc/rtc-88pm860x.c:calibrate_vrtc_work Cyclomatic Complexity 1 drivers/rtc/rtc-88pm860x.c:pm860x_rtc_driver_exit

vim +/pm860x_rtc_dt_init +353 drivers/rtc/rtc-88pm860x.c

311
312 static int pm860x_rtc_probe(struct platform_device *pdev)
313 {
314 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
315 struct pm860x_rtc_pdata *pdata = NULL;
316 struct pm860x_rtc_info *info;
317 int ret;
318
319 pdata = dev_get_platdata(&pdev->dev);
320
321 info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_rtc_info),
322 GFP_KERNEL);
323 if (!info)
324 return -ENOMEM;
325 info->irq = platform_get_irq(pdev, 0);
326 if (info->irq < 0)
327 return info->irq;
328
329 info->chip = chip;
330 info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
331 info->dev = &pdev->dev;
332 dev_set_drvdata(&pdev->dev, info);
333
334 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
335 if (IS_ERR(info->rtc_dev))
336 return PTR_ERR(info->rtc_dev);
337
338 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
339 rtc_update_handler, IRQF_ONESHOT, "rtc",
340 info);
341 if (ret < 0) {
342 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
343 info->irq, ret);
344 return ret;
345 }
346
347 /* set addresses of 32-bit base value for RTC time */
348 pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
349 pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
350 pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
351 pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
352

 > 353		pm860x_rtc_dt_init(pdev, info);

354
355 info->rtc_dev->ops = &pm860x_rtc_ops;
356
357 ret = rtc_register_device(info->rtc_dev);
358 if (ret)
359 return ret;
360
361 /*
362 * enable internal XO instead of internal 3.25MHz clock since it can
363 * free running in PMIC power-down state.
364 */
365 pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
366

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#4 lore raw

PATCH 4/6 rtc: 88pm860x: set range

In reply to Alexandre Belloni

The 88pm860x RTC is a 32bit read only seconds counter with a 32bit offset.

---
 drivers/rtc/rtc-88pm860x.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-88pm860x.c +1 -0
index 0abf2b194938..f5933a08454c 100644
@@ -353,6 +353,7 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
 	pm860x_rtc_dt_init(pdev, info);
 
 	info->rtc_dev->ops = &pm860x_rtc_ops;
+	info->rtc_dev->range_max = U32_MAX;
 
 	ret = rtc_register_device(info->rtc_dev);
 	if (ret)
Signature · 1 line
2.24.1
author maintainer
#5 lore raw

PATCH 5/6 rtc: 88pm860x: stop mangling alarm time

In reply to Alexandre Belloni

The RTC core always passes a valid alarm time there is no need to modify it.

---
 drivers/rtc/rtc-88pm860x.c | 41 +-------------------------------------
 1 file changed, 1 insertion(+), 40 deletions(-)
drivers/rtc/rtc-88pm860x.c +1 -40
index f5933a08454c..e0b18227514b 100644
@@ -75,33 +75,6 @@ static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
 	return 0;
 }
 
-/*
- * Calculate the next alarm time given the requested alarm time mask
- * and the current time.
- */
-static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
-				struct rtc_time *alrm)
-{
-	unsigned long next_time;
-	unsigned long now_time;
-
-	next->tm_year = now->tm_year;
-	next->tm_mon = now->tm_mon;
-	next->tm_mday = now->tm_mday;
-	next->tm_hour = alrm->tm_hour;
-	next->tm_min = alrm->tm_min;
-	next->tm_sec = alrm->tm_sec;
-
-	rtc_tm_to_time(now, &now_time);
-	rtc_tm_to_time(next, &next_time);
-
-	if (next_time < now_time) {
-		/* Advance one day */
-		next_time += 60 * 60 * 24;
-		rtc_time_to_tm(next_time, next);
-	}
-}
-
 static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
 	struct pm860x_rtc_info *info = dev_get_drvdata(dev);
@@ -187,7 +160,6 @@ static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
 	struct pm860x_rtc_info *info = dev_get_drvdata(dev);
-	struct rtc_time now_tm, alarm_tm;
 	unsigned long ticks, base, data;
 	unsigned char buf[8];
 	int mask;
@@ -200,18 +172,7 @@ static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
 		(buf[5] << 8) | buf[7];
 
-	/* load 32-bit read-only counter */
-	pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
-	data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
-		(buf[1] << 8) | buf[0];
-	ticks = base + data;
-	dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
-		base, data, ticks);
-
-	rtc_time_to_tm(ticks, &now_tm);
-	rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
-	/* get new ticks for alarm in 24 hours */
-	rtc_tm_to_time(&alarm_tm, &ticks);
+	rtc_tm_to_time(&alrm->time, &ticks);
 	data = ticks - base;
 
 	buf[0] = data & 0xff;
Signature · 1 line
2.24.1
author maintainer
#6 lore raw

PATCH 6/6 rtc: 88pm860x: switch to rtc_time64_to_tm/rtc_tm_to_time64

In reply to Alexandre Belloni

Call the 64bit versions of rtc_tm time conversion.

---
 drivers/rtc/rtc-88pm860x.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/rtc/rtc-88pm860x.c +4 -4
index e0b18227514b..88bda3e072d8 100644
@@ -95,7 +95,7 @@ static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
 		base, data, ticks);
 
-	rtc_time_to_tm(ticks, tm);
+	rtc_time64_to_tm(ticks, tm);
 
 	return 0;
 }
@@ -112,7 +112,7 @@ static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
 			1900 + tm->tm_year);
 		return -EINVAL;
 	}
-	rtc_tm_to_time(tm, &ticks);
+	ticks = rtc_tm_to_time64(tm);
 
 	/* load 32-bit read-only counter */
 	pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
@@ -150,7 +150,7 @@ static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
 		base, data, ticks);
 
-	rtc_time_to_tm(ticks, &alrm->time);
+	rtc_time64_to_tm(ticks, &alrm->time);
 	ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
 	alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
 	alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
@@ -172,7 +172,7 @@ static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
 		(buf[5] << 8) | buf[7];
 
-	rtc_tm_to_time(&alrm->time, &ticks);
+	ticks = rtc_tm_to_time64(&alrm->time);
 	data = ticks - base;
 
 	buf[0] = data & 0xff;
Signature · 1 line
2.24.1