xref: /wlan-driver/qca-wifi-host-cmn/qdf/linux/src/qdf_periodic_work.c (revision 5113495b16420b49004c444715d2daae2066e7dc)
1 /*
2  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "qdf_periodic_work.h"
21 #include "qdf_status.h"
22 #include "qdf_trace.h"
23 #include "qdf_types.h"
24 
25 #ifdef WLAN_PERIODIC_WORK_DEBUG
26 #include "qdf_tracker.h"
27 
28 #define qdf_pwork_tracker_bits 2 /* 4 buckets */
29 static qdf_tracker_declare(qdf_pwork_tracker, qdf_pwork_tracker_bits,
30 			   "periodic work leaks", "periodic work create",
31 			   "periodic work destroy");
32 
qdf_periodic_work_feature_init(void)33 void qdf_periodic_work_feature_init(void)
34 {
35 	qdf_tracker_init(&qdf_pwork_tracker);
36 }
37 
qdf_periodic_work_feature_deinit(void)38 void qdf_periodic_work_feature_deinit(void)
39 {
40 	qdf_tracker_deinit(&qdf_pwork_tracker);
41 }
42 
qdf_periodic_work_check_for_leaks(void)43 void qdf_periodic_work_check_for_leaks(void)
44 {
45 	qdf_tracker_check_for_leaks(&qdf_pwork_tracker);
46 }
47 
qdf_pwork_dbg_track(struct qdf_periodic_work * pwork,const char * func,uint32_t line)48 static inline QDF_STATUS qdf_pwork_dbg_track(struct qdf_periodic_work *pwork,
49 					     const char *func, uint32_t line)
50 {
51 	return qdf_tracker_track(&qdf_pwork_tracker, pwork, func, line);
52 }
53 
qdf_pwork_dbg_untrack(struct qdf_periodic_work * pwork,const char * func,uint32_t line)54 static inline void qdf_pwork_dbg_untrack(struct qdf_periodic_work *pwork,
55 					 const char *func, uint32_t line)
56 {
57 	qdf_tracker_untrack(&qdf_pwork_tracker, pwork, func, line);
58 }
59 #else
qdf_pwork_dbg_track(struct qdf_periodic_work * pwork,const char * func,uint32_t line)60 static inline QDF_STATUS qdf_pwork_dbg_track(struct qdf_periodic_work *pwork,
61 					     const char *func, uint32_t line)
62 {
63 	return QDF_STATUS_SUCCESS;
64 }
65 
qdf_pwork_dbg_untrack(struct qdf_periodic_work * pwork,const char * func,uint32_t line)66 static inline void qdf_pwork_dbg_untrack(struct qdf_periodic_work *pwork,
67 					 const char *func, uint32_t line)
68 { }
69 #endif /* WLAN_PERIODIC_WORK_DEBUG */
70 
__qdf_periodic_work_handler(struct work_struct * work)71 static void __qdf_periodic_work_handler(struct work_struct *work)
72 {
73 	struct qdf_periodic_work *pwork =
74 		container_of(work, struct qdf_periodic_work, dwork.work);
75 	uint32_t msec;
76 
77 	pwork->callback(pwork->context);
78 
79 	/* this is intentionally racy; see qdf_periodic_work_stop_sync() */
80 	msec = pwork->msec;
81 	if (msec)
82 		schedule_delayed_work(&pwork->dwork, msecs_to_jiffies(msec));
83 }
84 
__qdf_periodic_work_create(struct qdf_periodic_work * pwork,qdf_periodic_work_cb callback,void * context,const char * func,uint32_t line)85 QDF_STATUS __qdf_periodic_work_create(struct qdf_periodic_work *pwork,
86 				      qdf_periodic_work_cb callback,
87 				      void *context,
88 				      const char *func, uint32_t line)
89 {
90 	QDF_STATUS status;
91 
92 	QDF_BUG(pwork);
93 	QDF_BUG(callback);
94 	if (!pwork || !callback)
95 		return QDF_STATUS_E_INVAL;
96 
97 	status = qdf_pwork_dbg_track(pwork, func, line);
98 	if (QDF_IS_STATUS_ERROR(status))
99 		return status;
100 
101 	INIT_DEFERRABLE_WORK(&pwork->dwork, __qdf_periodic_work_handler);
102 	pwork->callback = callback;
103 	pwork->context = context;
104 	pwork->msec = 0;
105 
106 	return QDF_STATUS_SUCCESS;
107 }
108 
__qdf_periodic_work_destroy(struct qdf_periodic_work * pwork,const char * func,uint32_t line)109 void __qdf_periodic_work_destroy(struct qdf_periodic_work *pwork,
110 				 const char *func, uint32_t line)
111 {
112 	qdf_periodic_work_stop_sync(pwork);
113 	qdf_pwork_dbg_untrack(pwork, func, line);
114 }
115 
qdf_periodic_work_start(struct qdf_periodic_work * pwork,uint32_t msec)116 bool qdf_periodic_work_start(struct qdf_periodic_work *pwork, uint32_t msec)
117 {
118 	QDF_BUG(msec);
119 	if (!msec)
120 		return false;
121 
122 	pwork->msec = msec;
123 
124 	return schedule_delayed_work(&pwork->dwork, msecs_to_jiffies(msec));
125 }
126 
qdf_periodic_work_stop_async(struct qdf_periodic_work * pwork)127 bool qdf_periodic_work_stop_async(struct qdf_periodic_work *pwork)
128 {
129 	bool pending = pwork->msec != 0;
130 
131 	pwork->msec = 0;
132 	cancel_delayed_work(&pwork->dwork);
133 
134 	return pending;
135 }
136 
qdf_periodic_work_stop_sync(struct qdf_periodic_work * pwork)137 bool qdf_periodic_work_stop_sync(struct qdf_periodic_work *pwork)
138 {
139 	bool pending = pwork->msec != 0;
140 
141 	/* To avoid using a lock, signal that the work shouldn't be restarted,
142 	 * and cancel_sync in a loop. There is a very small race window, and
143 	 * thus the work may occasionally need to be cancelled more than once.
144 	 */
145 	pwork->msec = 0;
146 	while (cancel_delayed_work_sync(&pwork->dwork))
147 		; /* no-op*/
148 
149 	return pending;
150 }
151 
152