Commit 6fb00f55 authored by Pavel Tikhomirov's avatar Pavel Tikhomirov Committed by Pavel Emelyanov

memory: don't use parent memdump if detected possible pid reuse

We have a problem when a pid is reused between consequent dumps we can't
understand if pagemap and pages from images of parent dump are invalid
to restore these pid already. That can lead even to wrong memory
restored for these pid, see the test in last patch.

So these is a try do separate processes with (likely) invalid previous
memory dump from processes with 100% valid previous dump.

For that we use the value of /proc/<pid>/stat's start_time and also the
timestamp of each (pre)dump. If the start time is strictly less than the
timestamp, that means that the pagemap for these pid from previous dump
is valid - was done for exactly the same process.

Creation time is in centiseconds by default so if predump is really fast
(<1csec) we can have false negative decisions for some processes, but in
case of long running processes we are fine.

https://jira.sw.ru/browse/PSBM-67502

v2: remove __maybe_unused for get_parent_stats; fix get_parent_stats to
have static typing; print warning only if unsure; check has_dump_uptime
v3: read parent stats from image only once; reuse stat from previous
parse_pid_stat call on dump
v4: move code to function; use unsigned long long for ticks; put
proc_pid_stat on mem_dump_ctl; print warning on all pid-reuse cases
v5: free parent's stats entry properly, pass it in arguments to
(pre_)dump_one_task
v6: free parent's stats in error path too
v7: zero init parent_se
v8: improve error message
v9: switch to inventory image from stats, if pid-reuse fails - fail
current dump
Signed-off-by: 's avatarPavel Tikhomirov <ptikhomirov@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 9725b7be
......@@ -1138,7 +1138,7 @@ err:
return ret;
}
static int pre_dump_one_task(struct pstree_item *item)
static int pre_dump_one_task(struct pstree_item *item, InventoryEntry *parent_ie)
{
pid_t pid = item->pid->real;
struct vm_area_list vmas;
......@@ -1197,6 +1197,8 @@ static int pre_dump_one_task(struct pstree_item *item)
mdc.pre_dump = true;
mdc.lazy = false;
mdc.stat = NULL;
mdc.parent_ie = parent_ie;
ret = parasite_dump_pages_seized(item, &vmas, &mdc, parasite_ctl);
if (ret)
......@@ -1215,7 +1217,7 @@ err_cure:
goto err_free;
}
static int dump_one_task(struct pstree_item *item)
static int dump_one_task(struct pstree_item *item, InventoryEntry *parent_ie)
{
pid_t pid = item->pid->real;
struct vm_area_list vmas;
......@@ -1357,6 +1359,8 @@ static int dump_one_task(struct pstree_item *item)
mdc.pre_dump = false;
mdc.lazy = opts.lazy_pages;
mdc.stat = &pps_buf;
mdc.parent_ie = parent_ie;
ret = parasite_dump_pages_seized(item, &vmas, &mdc, parasite_ctl);
if (ret)
......@@ -1544,6 +1548,7 @@ err:
int cr_pre_dump_tasks(pid_t pid)
{
InventoryEntry *parent_ie = NULL;
struct pstree_item *item;
int ret = -1;
......@@ -1595,10 +1600,18 @@ int cr_pre_dump_tasks(pid_t pid)
if (collect_namespaces(false) < 0)
goto err;
/* Errors handled later in detect_pid_reuse */
parent_ie = get_parent_inventory();
for_each_pstree_item(item)
if (pre_dump_one_task(item))
if (pre_dump_one_task(item, parent_ie))
goto err;
if (parent_ie) {
inventory_entry__free_unpacked(parent_ie, NULL);
parent_ie = NULL;
}
ret = cr_dump_shmem();
if (ret)
goto err;
......@@ -1608,6 +1621,9 @@ int cr_pre_dump_tasks(pid_t pid)
ret = 0;
err:
if (parent_ie)
inventory_entry__free_unpacked(parent_ie, NULL);
return cr_pre_dump_finish(ret);
}
......@@ -1721,6 +1737,7 @@ static int cr_dump_finish(int ret)
int cr_dump_tasks(pid_t pid)
{
InventoryEntry he = INVENTORY_ENTRY__INIT;
InventoryEntry *parent_ie = NULL;
struct pstree_item *item;
int pre_dump_ret = 0;
int ret = -1;
......@@ -1808,11 +1825,19 @@ int cr_dump_tasks(pid_t pid)
if (collect_seccomp_filters() < 0)
goto err;
/* Errors handled later in detect_pid_reuse */
parent_ie = get_parent_inventory();
for_each_pstree_item(item) {
if (dump_one_task(item))
if (dump_one_task(item, parent_ie))
goto err;
}
if (parent_ie) {
inventory_entry__free_unpacked(parent_ie, NULL);
parent_ie = NULL;
}
/*
* It may happen that a process has completed but its files in
* /proc/PID/ are still open by another process. If the PID has been
......@@ -1870,5 +1895,8 @@ int cr_dump_tasks(pid_t pid)
if (ret)
goto err;
err:
if (parent_ie)
inventory_entry__free_unpacked(parent_ie, NULL);
return cr_dump_finish(ret);
}
......@@ -116,7 +116,7 @@ void prepare_inventory_pre_dump(InventoryEntry *he)
he->has_dump_uptime = true;
}
__maybe_unused InventoryEntry *get_parent_inventory(void)
InventoryEntry *get_parent_inventory(void)
{
struct cr_img *img;
InventoryEntry *ie;
......
......@@ -4,6 +4,9 @@
#include <stdbool.h>
#include "int.h"
#include "vma.pb-c.h"
#include "pid.h"
#include "proc_parse.h"
#include "inventory.pb-c.h"
struct parasite_ctl;
struct vm_area_list;
......@@ -12,8 +15,10 @@ struct pstree_item;
struct vma_area;
struct mem_dump_ctl {
bool pre_dump;
bool lazy;
bool pre_dump;
bool lazy;
struct proc_pid_stat *stat;
InventoryEntry *parent_ie;
};
extern bool vma_has_guard_gap_hidden(struct vma_area *vma);
......
......@@ -290,6 +290,46 @@ static int xfer_pages(struct page_pipe *pp, struct page_xfer *xfer)
return ret;
}
static int detect_pid_reuse(struct pstree_item *item,
struct proc_pid_stat* pps,
InventoryEntry *parent_ie)
{
unsigned long long dump_ticks;
struct proc_pid_stat pps_buf;
unsigned long long tps; /* ticks per second */
int ret;
tps = sysconf(_SC_CLK_TCK);
if (tps == -1) {
pr_perror("Failed to get clock ticks via sysconf");
return -1;
}
if (!pps) {
pps = &pps_buf;
ret = parse_pid_stat(item->pid->real, pps);
if (ret < 0)
return -1;
}
if (!parent_ie) {
pr_err("Pid-reuse detection failed: no parent inventory, " \
"check warnings in get_parent_stats\n");
return -1;
}
dump_ticks = parent_ie->dump_uptime/(USEC_PER_SEC/tps);
if (pps->start_time >= dump_ticks) {
/* Print "*" if unsure */
pr_warn("Pid reuse%s detected for pid %d\n",
pps_buf.start_time == dump_ticks ? "*" : "",
item->pid->real);
return 1;
}
return 0;
}
static int __parasite_dump_pages_seized(struct pstree_item *item,
struct parasite_dump_pages_args *args,
struct vm_area_list *vma_area_list,
......@@ -303,6 +343,7 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
int ret, exit_code = -1;
unsigned cpp_flags = 0;
unsigned long pmc_size;
int possible_pid_reuse = 0;
pr_info("\n");
pr_info("Dumping pages (type: %d pid: %d)\n", CR_FD_PAGES, item->pid->real);
......@@ -356,6 +397,14 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
xfer.parent = NULL + 1;
}
if (xfer.parent) {
possible_pid_reuse = detect_pid_reuse(item, mdc->stat,
mdc->parent_ie);
if (possible_pid_reuse == -1)
goto out_xfer;
}
/*
* Step 1 -- generate the pagemap
*/
......@@ -382,7 +431,7 @@ static int __parasite_dump_pages_seized(struct pstree_item *item,
else {
again:
ret = generate_iovs(vma_area, pp, map, &off,
has_parent);
has_parent && !possible_pid_reuse);
if (ret == -EAGAIN) {
BUG_ON(!(pp->flags & PP_CHUNK_MODE));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment