1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifdef LOSCFG_KERNEL_PLIMITS
32 #include "los_config.h"
33 #include "los_process_pri.h"
34 #include "los_process.h"
35 #include "los_plimits.h"
36 #include "los_task_pri.h"
37 #include "los_processlimit.h"
38
39 STATIC PidLimit *g_rootPidLimit = NULL;
40
PidLimiterInit(UINTPTR limit)41 VOID PidLimiterInit(UINTPTR limit)
42 {
43 PidLimit *pidLimit = (PidLimit *)limit;
44 if (pidLimit == NULL) {
45 return;
46 }
47
48 pidLimit->pidLimit = LOS_GetSystemProcessMaximum();
49 pidLimit->priorityLimit = OS_USER_PROCESS_PRIORITY_HIGHEST;
50 g_rootPidLimit = pidLimit;
51 }
52
PidLimiterAlloc(VOID)53 VOID *PidLimiterAlloc(VOID)
54 {
55 PidLimit *plimite = (PidLimit *)LOS_MemAlloc(m_aucSysMem1, sizeof(PidLimit));
56 if (plimite == NULL) {
57 return NULL;
58 }
59 (VOID)memset_s(plimite, sizeof(PidLimit), 0, sizeof(PidLimit));
60 return (VOID *)plimite;
61 }
62
PidLimterFree(UINTPTR limit)63 VOID PidLimterFree(UINTPTR limit)
64 {
65 PidLimit *pidLimit = (PidLimit *)limit;
66 if (pidLimit == NULL) {
67 return;
68 }
69
70 (VOID)LOS_MemFree(m_aucSysMem1, (VOID *)limit);
71 }
72
PidLimitMigrateCheck(UINTPTR curr,UINTPTR parent)73 BOOL PidLimitMigrateCheck(UINTPTR curr, UINTPTR parent)
74 {
75 PidLimit *currPidLimit = (PidLimit *)curr;
76 PidLimit *parentPidLimit = (PidLimit *)parent;
77 if ((currPidLimit == NULL) || (parentPidLimit == NULL)) {
78 return FALSE;
79 }
80
81 if ((currPidLimit->pidCount + parentPidLimit->pidCount) >= parentPidLimit->pidLimit) {
82 return FALSE;
83 }
84 return TRUE;
85 }
86
OsPidLimitAddProcessCheck(UINTPTR limit,UINTPTR process)87 BOOL OsPidLimitAddProcessCheck(UINTPTR limit, UINTPTR process)
88 {
89 (VOID)process;
90 PidLimit *pidLimit = (PidLimit *)limit;
91 if (pidLimit->pidCount >= pidLimit->pidLimit) {
92 return FALSE;
93 }
94 return TRUE;
95 }
96
OsPidLimitAddProcess(UINTPTR limit,UINTPTR process)97 VOID OsPidLimitAddProcess(UINTPTR limit, UINTPTR process)
98 {
99 (VOID)process;
100 PidLimit *plimits = (PidLimit *)limit;
101
102 plimits->pidCount++;
103 return;
104 }
105
OsPidLimitDelProcess(UINTPTR limit,UINTPTR process)106 VOID OsPidLimitDelProcess(UINTPTR limit, UINTPTR process)
107 {
108 (VOID)process;
109 PidLimit *plimits = (PidLimit *)limit;
110
111 plimits->pidCount--;
112 return;
113 }
114
PidLimiterCopy(UINTPTR curr,UINTPTR parent)115 VOID PidLimiterCopy(UINTPTR curr, UINTPTR parent)
116 {
117 PidLimit *currPidLimit = (PidLimit *)curr;
118 PidLimit *parentPidLimit = (PidLimit *)parent;
119 if ((currPidLimit == NULL) || (parentPidLimit == NULL)) {
120 return;
121 }
122
123 currPidLimit->pidLimit = parentPidLimit->pidLimit;
124 currPidLimit->priorityLimit = parentPidLimit->priorityLimit;
125 currPidLimit->pidCount = 0;
126 return;
127 }
128
PidLimitSetPidLimit(PidLimit * pidLimit,UINT32 pidMax)129 UINT32 PidLimitSetPidLimit(PidLimit *pidLimit, UINT32 pidMax)
130 {
131 UINT32 intSave;
132
133 if ((pidLimit == NULL) || (pidMax > LOS_GetSystemProcessMaximum())) {
134 return EINVAL;
135 }
136
137 if (pidLimit == g_rootPidLimit) {
138 return EPERM;
139 }
140
141 SCHEDULER_LOCK(intSave);
142 if (pidLimit->pidCount >= pidMax) {
143 SCHEDULER_UNLOCK(intSave);
144 return EPERM;
145 }
146
147 pidLimit->pidLimit = pidMax;
148 SCHEDULER_UNLOCK(intSave);
149 return LOS_OK;
150 }
151
PidLimitSetPriorityLimit(PidLimit * pidLimit,UINT32 priority)152 UINT32 PidLimitSetPriorityLimit(PidLimit *pidLimit, UINT32 priority)
153 {
154 UINT32 intSave;
155
156 if ((pidLimit == NULL) ||
157 (priority < OS_USER_PROCESS_PRIORITY_HIGHEST) ||
158 (priority > OS_PROCESS_PRIORITY_LOWEST)) {
159 return EINVAL;
160 }
161
162 if (pidLimit == g_rootPidLimit) {
163 return EPERM;
164 }
165
166 SCHEDULER_LOCK(intSave);
167 pidLimit->priorityLimit = priority;
168 SCHEDULER_UNLOCK(intSave);
169 return LOS_OK;
170 }
171
OsPidLimitGetPriorityLimit(VOID)172 UINT16 OsPidLimitGetPriorityLimit(VOID)
173 {
174 UINT32 intSave;
175 SCHEDULER_LOCK(intSave);
176 LosProcessCB *run = OsCurrProcessGet();
177 PidLimit *pidLimit = (PidLimit *)run->plimits->limitsList[PROCESS_LIMITER_ID_PIDS];
178 UINT16 priority = pidLimit->priorityLimit;
179 SCHEDULER_UNLOCK(intSave);
180 return priority;
181 }
182 #endif
183