Skip to content

Commit 269d28a

Browse files
polarvidRbb666
authored andcommitted
feat: mm: install page in a discrete method
This patch refactors the `rt_page_install` function by delegating specific operations to new helper functions. Besides, the contiguous page regions are now separated to segments of fixed size. The goal is to improve the overall code readability, enhance modularity, and ensure better handling of page installation logic through clearer separation of concerns. Changes: - Introduced `_get_mpr_ready_n_install` to encapsulate memory preparation and page installation logic. - Added `_update_region_list` for updating the installed page registry. - Defined `_PAGE_STRIPE` for optimized region processing. - Modified `rt_page_install` to handle regions in smaller chunks using helper functions for better maintainability. - Improved locking mechanisms with `rt_spin_lock` for thread safety. - Reduced code duplication and clarified shadow region calculations. Signed-off-by: Shell <[email protected]>
1 parent 9386411 commit 269d28a

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

components/mm/mm_page.c

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,34 +1112,82 @@ static int _load_mpr_area(void *head, void *tail)
11121112
return err;
11131113
}
11141114

1115-
int rt_page_install(rt_region_t region)
1115+
static int _get_mpr_ready_n_install(rt_ubase_t inst_head, rt_ubase_t inst_end)
11161116
{
1117-
int err = -RT_EINVAL;
1117+
int err;
11181118
rt_region_t shadow;
1119+
rt_region_t region =
1120+
{
1121+
.start = inst_head,
1122+
.end = inst_end,
1123+
};
11191124
void *head, *tail;
11201125

1126+
shadow.start = region.start & ~shadow_mask;
1127+
shadow.end = FLOOR(region.end, shadow_mask + 1);
1128+
head = addr_to_page(page_start, (void *)shadow.start);
1129+
tail = addr_to_page(page_start, (void *)shadow.end);
1130+
1131+
err = _load_mpr_area(head, tail);
1132+
1133+
if (err == RT_EOK)
1134+
{
1135+
_install_page(rt_mpr_start, region, _page_insert);
1136+
}
1137+
1138+
return err;
1139+
}
1140+
1141+
static void _update_region_list(struct installed_page_reg *member,
1142+
rt_ubase_t inst_head, rt_ubase_t inst_end)
1143+
{
1144+
rt_spin_lock_init(&member->lock);
1145+
1146+
rt_spin_lock(&_inst_page_reg_lock);
1147+
1148+
member->region_area.start = inst_head;
1149+
member->region_area.end = inst_end;
1150+
member->usage_trace = ut_bitmap;
1151+
1152+
member->next = _inst_page_reg_head;
1153+
_inst_page_reg_head = member;
1154+
1155+
rt_spin_unlock(&_inst_page_reg_lock);
1156+
}
1157+
1158+
#define _PAGE_STRIPE (1 << (RT_PAGE_MAX_ORDER + ARCH_PAGE_SHIFT - 1))
1159+
int rt_page_install(rt_region_t region)
1160+
{
1161+
int err = -RT_EINVAL;
1162+
11211163
if (region.end != region.start && !(region.start & ARCH_PAGE_MASK) &&
11221164
!(region.end & ARCH_PAGE_MASK))
11231165
{
1124-
shadow.start = region.start & ~shadow_mask;
1125-
shadow.end = FLOOR(region.end, shadow_mask + 1);
1126-
head = addr_to_page(page_start, (void *)shadow.start);
1127-
tail = addr_to_page(page_start, (void *)shadow.end);
1128-
1129-
err = _load_mpr_area(head, tail);
1166+
rt_ubase_t inst_head = region.start;
1167+
rt_ubase_t inst_end = region.end;
1168+
rt_ubase_t iter = inst_head;
1169+
struct installed_page_reg *installed_pgreg =
1170+
rt_malloc(sizeof(*installed_pgreg));
11301171

1131-
if (err == RT_EOK)
1172+
if (installed_pgreg)
11321173
{
1133-
struct installed_page_reg *installed_pgreg =
1134-
rt_malloc(sizeof(*installed_pgreg));
1174+
_update_region_list(installed_pgreg, inst_head, inst_end);
11351175

1136-
if (installed_pgreg)
1176+
if ((rt_ubase_t)iter & shadow_mask)
11371177
{
1138-
installed_pgreg->region_area.start = region.start;
1139-
installed_pgreg->region_area.end = region.end;
1178+
iter = RT_ALIGN((rt_ubase_t)inst_head, _PAGE_STRIPE);
1179+
_get_mpr_ready_n_install(inst_head, iter < inst_end ? iter : inst_end);
1180+
}
11401181

1141-
_update_region_list(installed_pgreg);
1142-
_install_page(rt_mpr_start, region, _page_insert);
1182+
for (rt_ubase_t next = iter + _PAGE_STRIPE; next < inst_end;
1183+
iter = next, next += _PAGE_STRIPE)
1184+
{
1185+
_get_mpr_ready_n_install(iter, next);
1186+
}
1187+
1188+
if (iter < inst_end)
1189+
{
1190+
_get_mpr_ready_n_install(iter, inst_end);
11431191
}
11441192
}
11451193
}

0 commit comments

Comments
 (0)