Skip to content

Commit 53d3e0c

Browse files
mihalicynrst0git
authored andcommitted
criu/mem: refactor should_dump_page helper
Make should_dump_page to return int to indicate failure, also return useful data back through the struct page_info structure passed as a pointer. Also, correspondingly convert all call sites. No functional changes intended, except fixing a bug in should_dump_page() as it could return (-1) when pmc_fill() fails, while caller didn't expect that before. Signed-off-by: Alexander Mikhalitsyn <[email protected]>
1 parent 0852cf9 commit 53d3e0c

File tree

3 files changed

+68
-34
lines changed

3 files changed

+68
-34
lines changed

criu/include/mem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ int prepare_vmas(struct pstree_item *t, struct task_restore_args *ta);
4949
int unmap_guard_pages(struct pstree_item *t);
5050
int prepare_mappings(struct pstree_item *t);
5151

52-
u64 should_dump_page(pmc_t *pmc, VmaEntry *vmae, u64 vaddr, bool *softdirty);
52+
struct page_info {
53+
u64 next;
54+
bool softdirty;
55+
};
56+
57+
int should_dump_page(pmc_t *pmc, VmaEntry *vmae, u64 vaddr, struct page_info *page_info);
58+
5359
#endif /* __CR_MEM_H__ */

criu/mem.c

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,44 +115,64 @@ static bool should_dump_entire_vma(VmaEntry *vmae)
115115
}
116116

117117
/*
118-
* should_dump_page returns vaddr if an addressed page has to be dumped.
119-
* Otherwise, it returns an address that has to be inspected next.
118+
* should_dump_page writes vaddr in page_info->next if an addressed page has to be dumped.
119+
* Otherwise, it writes an address that has to be inspected next.
120120
*/
121-
u64 should_dump_page(pmc_t *pmc, VmaEntry *vmae, u64 vaddr, bool *softdirty)
121+
int should_dump_page(pmc_t *pmc, VmaEntry *vmae, u64 vaddr, struct page_info *page_info)
122122
{
123+
if (!page_info)
124+
goto err;
125+
123126
if (vaddr >= pmc->end && pmc_fill(pmc, vaddr, vmae->end))
124-
return -1;
127+
goto err;
125128

126129
if (pmc->regs) {
127130
while (1) {
128-
if (pmc->regs_idx == pmc->regs_len)
129-
return pmc->end;
131+
if (pmc->regs_idx == pmc->regs_len) {
132+
page_info->next = pmc->end;
133+
return 0;
134+
}
135+
130136
if (vaddr < pmc->regs[pmc->regs_idx].end)
131137
break;
132138
pmc->regs_idx++;
133139
}
134-
if (vaddr < pmc->regs[pmc->regs_idx].start)
135-
return pmc->regs[pmc->regs_idx].start;
136-
if (softdirty)
137-
*softdirty = pmc->regs[pmc->regs_idx].categories & PAGE_IS_SOFT_DIRTY;
138-
return vaddr;
140+
141+
if (vaddr < pmc->regs[pmc->regs_idx].start) {
142+
page_info->next = pmc->regs[pmc->regs_idx].start;
143+
return 0;
144+
}
145+
146+
page_info->softdirty = pmc->regs[pmc->regs_idx].categories & PAGE_IS_SOFT_DIRTY;
147+
page_info->next = vaddr;
148+
return 0;
139149
} else {
140150
u64 pme = pmc->map[PAGE_PFN(vaddr - pmc->start)];
141151

142152
/*
143153
* Optimisation for private mapping pages, that haven't
144154
* yet being COW-ed
145155
*/
146-
if (vma_entry_is(vmae, VMA_FILE_PRIVATE) && (pme & PME_FILE))
147-
return vaddr + PAGE_SIZE;
156+
if (vma_entry_is(vmae, VMA_FILE_PRIVATE) && (pme & PME_FILE)) {
157+
page_info->next = vaddr + PAGE_SIZE;
158+
return 0;
159+
}
160+
148161
if ((pme & (PME_PRESENT | PME_SWAP)) && !__page_is_zero(pme)) {
149-
if (softdirty)
150-
*softdirty = pme & PME_SOFT_DIRTY;
151-
return vaddr;
162+
page_info->softdirty = pme & PME_SOFT_DIRTY;
163+
page_info->next = vaddr;
164+
return 0;
152165
}
153166

154-
return vaddr + PAGE_SIZE;
167+
page_info->next = vaddr + PAGE_SIZE;
168+
return 0;
155169
}
170+
171+
err:
172+
pr_err("should_dump_page failed on vma "
173+
"%#016" PRIx64 "-%#016" PRIx64 " vaddr=%#016" PRIx64 "\n",
174+
vmae->start, vmae->end, vaddr);
175+
return -1;
156176
}
157177

158178
bool page_is_zero(u64 pme)
@@ -202,14 +222,15 @@ static int generate_iovs(struct pstree_item *item, struct vma_area *vma, struct
202222
nr_scanned = 0;
203223
for (vaddr = *pvaddr; vaddr < vma->e->end; vaddr += PAGE_SIZE, nr_scanned++) {
204224
unsigned int ppb_flags = 0;
205-
bool softdirty = false;
206-
u64 next;
225+
struct page_info page_info = {};
207226
int st;
208227

209228
/* If dump_all_pages is true, should_dump_page is called to get pme. */
210-
next = should_dump_page(pmc, vma->e, vaddr, &softdirty);
211-
if (!dump_all_pages && next != vaddr) {
212-
vaddr = next - PAGE_SIZE;
229+
if (should_dump_page(pmc, vma->e, vaddr, &page_info))
230+
return -1;
231+
232+
if (!dump_all_pages && page_info.next != vaddr) {
233+
vaddr = page_info.next - PAGE_SIZE;
213234
continue;
214235
}
215236

@@ -223,7 +244,7 @@ static int generate_iovs(struct pstree_item *item, struct vma_area *vma, struct
223244
* page. The latter would be checked in page-xfer.
224245
*/
225246

226-
if (has_parent && page_in_parent(softdirty)) {
247+
if (has_parent && page_in_parent(page_info.softdirty)) {
227248
ret = page_pipe_add_hole(pp, vaddr, PP_HOLE_PARENT);
228249
st = 0;
229250
} else {

criu/shmem.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,34 @@ static int expand_shmem(struct shmem_info *si, unsigned long new_size)
206206
return 0;
207207
}
208208

209-
static void update_shmem_pmaps(struct shmem_info *si, pmc_t *pmc, VmaEntry *vma)
209+
static int update_shmem_pmaps(struct shmem_info *si, pmc_t *pmc, VmaEntry *vma)
210210
{
211211
unsigned long shmem_pfn, vma_pfn, vma_pgcnt;
212212
u64 vaddr;
213213

214214
if (!is_shmem_tracking_en())
215-
return;
215+
return 0;
216216

217217
vma_pgcnt = DIV_ROUND_UP(si->size - vma->pgoff, PAGE_SIZE);
218218
for (vma_pfn = 0, vaddr = vma->start; vma_pfn < vma_pgcnt; ++vma_pfn, vaddr += PAGE_SIZE) {
219-
bool softdirty = false;
220-
u64 next;
219+
struct page_info page_info = {};
220+
221+
if (should_dump_page(pmc, vma, vaddr, &page_info))
222+
return -1;
221223

222-
next = should_dump_page(pmc, vma, vaddr, &softdirty);
223-
if (next != vaddr) {
224-
vaddr = next - PAGE_SIZE;
224+
if (page_info.next != vaddr) {
225+
vaddr = page_info.next - PAGE_SIZE;
225226
continue;
226227
}
227228

228229
shmem_pfn = vma_pfn + DIV_ROUND_UP(vma->pgoff, PAGE_SIZE);
229-
if (softdirty)
230+
if (page_info.softdirty)
230231
set_pstate(si->pstate_map, shmem_pfn, PST_DIRTY);
231232
else
232233
set_pstate(si->pstate_map, shmem_pfn, PST_DUMP);
233234
}
235+
236+
return 0;
234237
}
235238

236239
int collect_sysv_shmem(unsigned long shmid, unsigned long size)
@@ -667,7 +670,9 @@ int add_shmem_area(pid_t pid, VmaEntry *vma, pmc_t *pmc)
667670
if (expand_shmem(si, size))
668671
return -1;
669672
}
670-
update_shmem_pmaps(si, pmc, vma);
673+
674+
if (update_shmem_pmaps(si, pmc, vma))
675+
return -1;
671676

672677
return 0;
673678
}
@@ -684,7 +689,9 @@ int add_shmem_area(pid_t pid, VmaEntry *vma, pmc_t *pmc)
684689

685690
if (expand_shmem(si, size))
686691
return -1;
687-
update_shmem_pmaps(si, pmc, vma);
692+
693+
if (update_shmem_pmaps(si, pmc, vma))
694+
return -1;
688695

689696
return 0;
690697
}

0 commit comments

Comments
 (0)