Skip to content

Commit 854648d

Browse files
ewoodevsunghan-chang
authored andcommitted
pm: Add NULL validation and code cleanup
- pm_dvfs: Add NULL check for dvfs_ops before dereferencing - pm_idle: Add NULL check for sleep_ops before dereferencing - pm_idle: Fix meaningless comparison (wakeup_src < PM_WAKEUP_UNKNOWN) - pm_metrics: Replace pm_alloc/free with kmm_calloc/kmm_free - pm.h: Remove pm_alloc macro (replaced with direct kmm_callo - Remove pm_check_domain function, use direct NULL checks instead Signed-off-by: Eunwoo Nam <eunwoo.nam@samsung.com>
1 parent 61eb7b7 commit 854648d

File tree

9 files changed

+21
-81
lines changed

9 files changed

+21
-81
lines changed

os/pm/pm.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@
6767

6868
#ifdef CONFIG_PM
6969

70-
/****************************************************************************
71-
* Pre-processor Definitions
72-
****************************************************************************/
73-
74-
#ifdef CONFIG_MM_KERNEL_HEAP
75-
#define pm_alloc(num, size) kmm_calloc(num, size)
76-
#else
77-
#define pm_alloc(num, size) calloc(num, size)
78-
#endif
79-
8070
/* Function-like macros *****************************************************/
8171
/****************************************************************************
8272
* Name: pm_lock
@@ -171,23 +161,6 @@ EXTERN struct pm_global_s g_pmglobals;
171161
/************************************************************************************
172162
* Public Function Prototypes
173163
************************************************************************************/
174-
/****************************************************************************
175-
* Name: pm_check_domain
176-
*
177-
* Description:
178-
* This function is called inside PM internal APIs to check whether the
179-
* domain pointer is valid.
180-
*
181-
* Input Parameters:
182-
* domain - Pointer to the domain structure
183-
*
184-
* Returned Value:
185-
* 0 (OK) - If domain is valid
186-
* -1 (ERROR) - If domain is not valid
187-
*
188-
****************************************************************************/
189-
int pm_check_domain(FAR struct pm_domain_s *domain);
190-
191164
/****************************************************************************
192165
* Name: pm_checkstate
193166
*

os/pm/pm_domain_register.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,6 @@ FAR struct pm_domain_s *pm_domain_find(FAR const char *domain_name)
7373
return NULL;
7474
}
7575

76-
/****************************************************************************
77-
* Name: pm_check_domain
78-
*
79-
* Description:
80-
* This function is called inside PM internal APIs to check whether the
81-
* domain pointer is valid.
82-
*
83-
* Input Parameters:
84-
* domain - Pointer to the domain structure
85-
*
86-
* Returned Value:
87-
* 0 (OK) - If domain is valid
88-
* -1 (ERROR) - If domain is not valid
89-
*
90-
****************************************************************************/
91-
int pm_check_domain(FAR struct pm_domain_s *domain)
92-
{
93-
if (domain == NULL) {
94-
set_errno(EINVAL);
95-
pmdbg("Invalid Domain Pointer: NULL\n");
96-
return ERROR;
97-
}
98-
/* Additional checks can be added here if necessary, e.g., if the domain
99-
* is still in the list. For now, a non-NULL pointer is considered valid.
100-
*/
101-
return OK;
102-
}
103-
10476
/****************************************************************************
10577
* Name: pm_domain_register
10678
*

os/pm/pm_dvfs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ void pm_dvfs(int div_lvl)
103103
* 3 -> 300Mhz
104104
*/
105105
/* Placed in os/arch/arm/src/amebasmart/amebasmart_pmc.c, can be invoked directly */
106-
g_pmglobals.dvfs_ops->adjust_dvfs(div_lvl);
106+
if (g_pmglobals.dvfs_ops && g_pmglobals.dvfs_ops->adjust_dvfs) {
107+
g_pmglobals.dvfs_ops->adjust_dvfs(div_lvl);
108+
}
107109
return;
108110
}
109111

os/pm/pm_idle.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ static void enable_and_compensate_systick(void)
233233
struct pm_sleep_ops *sleep_ops = g_pmglobals.sleep_ops;
234234
clock_t missing_tick;
235235

236-
if (!sleep_ops->get_missingtick) {
237-
return;
236+
if (!sleep_ops || !sleep_ops->get_missingtick) {
237+
goto enable_timer;
238238
}
239239

240240
missing_tick = sleep_ops->get_missingtick();
@@ -257,8 +257,9 @@ static void enable_and_compensate_systick(void)
257257
*/
258258
wd_timer_nohz(missing_tick);
259259
}
260-
#endif
261260

261+
enable_timer:
262+
#endif
262263
(void)up_timer_enable();
263264
}
264265

@@ -317,7 +318,7 @@ static int set_pm_wakeup_timer(int delay)
317318
return OK;
318319
}
319320
#else
320-
#define get_next_wakeup_time() (ERROR)
321+
#define get_next_wakeup_time() (0)
321322
#define set_pm_wakeup_timer(delay) (OK)
322323
#endif
323324

@@ -340,9 +341,9 @@ static void update_wakeup_reason(void)
340341
pm_wakeup_reason_code_t wakeup_src;
341342
struct pm_sleep_ops *sleep_ops = g_pmglobals.sleep_ops;
342343

343-
if (sleep_ops->get_wakeupreason) {
344+
if (sleep_ops && sleep_ops->get_wakeupreason) {
344345
wakeup_src = sleep_ops->get_wakeupreason();
345-
if (wakeup_src < PM_WAKEUP_UNKNOWN || wakeup_src >= PM_WAKEUP_SRC_COUNT) {
346+
if (wakeup_src >= PM_WAKEUP_SRC_COUNT) {
346347
wakeup_src = PM_WAKEUP_UNKNOWN;
347348
}
348349

os/pm/pm_metrics.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <tinyara/config.h>
2020
#include <tinyara/pm/pm.h>
21+
#include <tinyara/kmalloc.h>
2122
#include <time.h>
2223
#include <queue.h>
2324
#include <debug.h>
@@ -298,7 +299,7 @@ int pm_metrics(int milliseconds)
298299
pm_lock();
299300

300301
/* Allocate memory for initializing PM Metrics measurements */
301-
g_pm_metrics = (pm_metric_t *)pm_alloc(1, sizeof(pm_metric_t));
302+
g_pm_metrics = (pm_metric_t *)kmm_calloc(1, sizeof(pm_metric_t));
302303
if (g_pm_metrics == NULL) {
303304
set_errno(ENOMEM);
304305
pmdbg("Unable to initialize pm_metrics, error = %d\n", get_errno());
@@ -344,7 +345,7 @@ int pm_metrics(int milliseconds)
344345
/* Show PM Metrics Results */
345346
pm_print_metrics((double)(end_time - start_time), n_domains);
346347
/* Free allocated memory */
347-
free(g_pm_metrics); /* Use pm_free for consistency */
348+
kmm_free(g_pm_metrics);
348349
g_pm_metrics = NULL;
349350

350351
/* Unlock PM Metrics for other threads */

os/pm/pm_resume.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ int pm_resume(FAR struct pm_domain_s *domain)
105105

106106
flags = enter_critical_section();
107107

108-
ret = pm_check_domain(domain);
109-
if (ret != OK) {
110-
goto errout;
111-
}
112-
113108
if (domain->suspend_count <= 0) {
114109
ret = ERROR;
115110
set_errno(ERANGE);

os/pm/pm_suspend.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,13 @@ int pm_suspend(FAR struct pm_domain_s *domain)
102102
irqstate_t flags;
103103
int ret = OK;
104104

105-
flags = enter_critical_section();
106-
107-
/* pm_check_domain is implicitly handled by checking for NULL domain pointer
108-
* and ensuring the domain structure is valid.
109-
* If more checks are needed, pm_check_domain(domain) can be called here.
110-
*/
111-
ret = pm_check_domain(domain);
112-
if (ret != OK) {
113-
goto errout;
105+
if (domain == NULL) {
106+
set_errno(EINVAL);
107+
return ERROR;
114108
}
115109

110+
flags = enter_critical_section();
111+
116112
if (domain->suspend_count >= UINT16_MAX) {
117113
ret = ERROR;
118114
set_errno(ERANGE);

os/pm/pm_suspendcount.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989

9090
int pm_suspendcount(FAR struct pm_domain_s *domain)
9191
{
92-
/* Check if domain pointer is valid */
93-
if (pm_check_domain(domain) != OK) {
92+
if (domain == NULL) {
93+
set_errno(EINVAL);
9494
return ERROR;
9595
}
9696
return domain->suspend_count;

os/pm/pm_timedsuspend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int pm_timedsuspend(struct pm_domain_s *domain, unsigned int milliseconds)
126126
int ret = ERROR;
127127
int delay = MSEC2TICK(milliseconds);
128128

129-
if (pm_check_domain(domain)) {
129+
if (domain == NULL) {
130130
set_errno(EINVAL);
131131
return ret;
132132
}

0 commit comments

Comments
 (0)