linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] PM / devfreq: Strengthen the handling of governor null pointer
@ 2022-07-01  4:53 Schspa Shi
  0 siblings, 0 replies; only message in thread
From: Schspa Shi @ 2022-07-01  4:53 UTC (permalink / raw)
  To: myungjoo.ham, kyungmin.park, cw00.choi; +Cc: linux-pm, linux-kernel, Schspa Shi

When new governor start failed & the old governor start failed too,
or the governor is removed, the devfreq->governor will be NULL, but
we still access it's member from devfreq->governor->name, which will
be a crash.

To fix it, change the position of Null pointer judgment before usage.

Another, If the governor is NULL, we try to assign a new governor to
it.

Signed-off-by: Schspa Shi <schspa@gmail.com>
---
 drivers/devfreq/devfreq.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 01474daf4548..30d7f8c128a6 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -1270,7 +1270,13 @@ int devfreq_add_governor(struct devfreq_governor *governor)
 		int ret = 0;
 		struct device *dev = devfreq->dev.parent;
 
-		if (!strncmp(devfreq->governor->name, governor->name,
+		/*
+		 * When new governor start failed & the old governor start
+		 * failed too. devfreq->governor will be NULL.
+		 * Try to assign the new governor to this devfreq device.
+		 */
+		if (!devfreq->governor ||
+			!strncmp(devfreq->governor->name, governor->name,
 			     DEVFREQ_NAME_LEN)) {
 			/* The following should never occur */
 			if (devfreq->governor) {
@@ -1358,15 +1364,15 @@ int devfreq_remove_governor(struct devfreq_governor *governor)
 		int ret;
 		struct device *dev = devfreq->dev.parent;
 
+		/* we should have a devfreq governor! */
+		if (!devfreq->governor) {
+			dev_warn(dev, "%s: Governor %s NOT present\n",
+				__func__, governor->name);
+			continue;
+		}
+
 		if (!strncmp(devfreq->governor->name, governor->name,
 			     DEVFREQ_NAME_LEN)) {
-			/* we should have a devfreq governor! */
-			if (!devfreq->governor) {
-				dev_warn(dev, "%s: Governor %s NOT present\n",
-					 __func__, governor->name);
-				continue;
-				/* Fall through */
-			}
 			ret = devfreq->governor->event_handler(devfreq,
 						DEVFREQ_GOV_STOP, NULL);
 			if (ret) {
-- 
2.29.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-01  4:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-01  4:53 [PATCH] PM / devfreq: Strengthen the handling of governor null pointer Schspa Shi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).