Skip to content

Commit 0791e36

Browse files
cyrillostorvalds
authored andcommitted
kcmp: add KCMP_EPOLL_TFD mode to compare epoll target files
With current epoll architecture target files are addressed with file_struct and file descriptor number, where the last is not unique. Moreover files can be transferred from another process via unix socket, added into queue and closed then so we won't find this descriptor in the task fdinfo list. Thus to checkpoint and restore such processes CRIU needs to find out where exactly the target file is present to add it into epoll queue. For this sake one can use kcmp call where some particular target file from the queue is compared with arbitrary file passed as an argument. Because epoll target files can have same file descriptor number but different file_struct a caller should explicitly specify the offset within. To test if some particular file is matching entry inside epoll one have to - fill kcmp_epoll_slot structure with epoll file descriptor, target file number and target file offset (in case if only one target is present then it should be 0) - call kcmp as kcmp(pid1, pid2, KCMP_EPOLL_TFD, fd, &kcmp_epoll_slot) - the kernel fetch file pointer matching file descriptor @fd of pid1 - lookups for file struct in epoll queue of pid2 and returns traditional 0,1,2 result for sorting purpose Link: http://lkml.kernel.org/r/20170424154423.511592110@gmail.com Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org> Acked-by: Andrey Vagin <avagin@openvz.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Pavel Emelyanov <xemul@virtuozzo.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Jason Baron <jbaron@akamai.com> Cc: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 77493f0 commit 0791e36

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎fs/eventpoll.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,48 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
10771077
return epir;
10781078
}
10791079

1080+
static struct epitem *ep_find_tfd(struct eventpoll *ep, int tfd, unsigned long toff)
1081+
{
1082+
struct rb_node *rbp;
1083+
struct epitem *epi;
1084+
1085+
for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
1086+
epi = rb_entry(rbp, struct epitem, rbn);
1087+
if (epi->ffd.fd == tfd) {
1088+
if (toff == 0)
1089+
return epi;
1090+
else
1091+
toff--;
1092+
}
1093+
cond_resched();
1094+
}
1095+
1096+
return NULL;
1097+
}
1098+
1099+
struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd,
1100+
unsigned long toff)
1101+
{
1102+
struct file *file_raw;
1103+
struct eventpoll *ep;
1104+
struct epitem *epi;
1105+
1106+
if (!is_file_epoll(file))
1107+
return ERR_PTR(-EINVAL);
1108+
1109+
ep = file->private_data;
1110+
1111+
mutex_lock(&ep->mtx);
1112+
epi = ep_find_tfd(ep, tfd, toff);
1113+
if (epi)
1114+
file_raw = epi->ffd.file;
1115+
else
1116+
file_raw = ERR_PTR(-ENOENT);
1117+
mutex_unlock(&ep->mtx);
1118+
1119+
return file_raw;
1120+
}
1121+
10801122
/*
10811123
* This is the callback that is passed to the wait queue wakeup
10821124
* mechanism. It is called by the stored file descriptors when they

‎include/linux/eventpoll.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define _LINUX_EVENTPOLL_H
1515

1616
#include <uapi/linux/eventpoll.h>
17+
#include <uapi/linux/kcmp.h>
1718

1819

1920
/* Forward declarations to avoid compiler errors */
@@ -22,6 +23,8 @@ struct file;
2223

2324
#ifdef CONFIG_EPOLL
2425

26+
struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, unsigned long toff);
27+
2528
/* Used to initialize the epoll bits inside the "struct file" */
2629
static inline void eventpoll_init_file(struct file *file)
2730
{

‎include/uapi/linux/kcmp.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _UAPI_LINUX_KCMP_H
22
#define _UAPI_LINUX_KCMP_H
33

4+
#include <linux/types.h>
5+
46
/* Comparison type */
57
enum kcmp_type {
68
KCMP_FILE,
@@ -10,8 +12,16 @@ enum kcmp_type {
1012
KCMP_SIGHAND,
1113
KCMP_IO,
1214
KCMP_SYSVSEM,
15+
KCMP_EPOLL_TFD,
1316

1417
KCMP_TYPES,
1518
};
1619

20+
/* Slot for KCMP_EPOLL_TFD */
21+
struct kcmp_epoll_slot {
22+
__u32 efd; /* epoll file descriptor */
23+
__u32 tfd; /* target file number */
24+
__u32 toff; /* target offset within same numbered sequence */
25+
};
26+
1727
#endif /* _UAPI_LINUX_KCMP_H */

‎kernel/kcmp.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <linux/bug.h>
1212
#include <linux/err.h>
1313
#include <linux/kcmp.h>
14+
#include <linux/capability.h>
15+
#include <linux/list.h>
16+
#include <linux/eventpoll.h>
17+
#include <linux/file.h>
1418

1519
#include <asm/unistd.h>
1620

@@ -94,6 +98,56 @@ static int kcmp_lock(struct mutex *m1, struct mutex *m2)
9498
return err;
9599
}
96100

101+
#ifdef CONFIG_EPOLL
102+
static int kcmp_epoll_target(struct task_struct *task1,
103+
struct task_struct *task2,
104+
unsigned long idx1,
105+
struct kcmp_epoll_slot __user *uslot)
106+
{
107+
struct file *filp, *filp_epoll, *filp_tgt;
108+
struct kcmp_epoll_slot slot;
109+
struct files_struct *files;
110+
111+
if (copy_from_user(&slot, uslot, sizeof(slot)))
112+
return -EFAULT;
113+
114+
filp = get_file_raw_ptr(task1, idx1);
115+
if (!filp)
116+
return -EBADF;
117+
118+
files = get_files_struct(task2);
119+
if (!files)
120+
return -EBADF;
121+
122+
spin_lock(&files->file_lock);
123+
filp_epoll = fcheck_files(files, slot.efd);
124+
if (filp_epoll)
125+
get_file(filp_epoll);
126+
else
127+
filp_tgt = ERR_PTR(-EBADF);
128+
spin_unlock(&files->file_lock);
129+
put_files_struct(files);
130+
131+
if (filp_epoll) {
132+
filp_tgt = get_epoll_tfile_raw_ptr(filp_epoll, slot.tfd, slot.toff);
133+
fput(filp_epoll);
134+
} else
135+
136+
if (IS_ERR(filp_tgt))
137+
return PTR_ERR(filp_tgt);
138+
139+
return kcmp_ptr(filp, filp_tgt, KCMP_FILE);
140+
}
141+
#else
142+
static int kcmp_epoll_target(struct task_struct *task1,
143+
struct task_struct *task2,
144+
unsigned long idx1,
145+
struct kcmp_epoll_slot __user *uslot)
146+
{
147+
return -EOPNOTSUPP;
148+
}
149+
#endif
150+
97151
SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
98152
unsigned long, idx1, unsigned long, idx2)
99153
{
@@ -165,6 +219,9 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
165219
ret = -EOPNOTSUPP;
166220
#endif
167221
break;
222+
case KCMP_EPOLL_TFD:
223+
ret = kcmp_epoll_target(task1, task2, idx1, (void *)idx2);
224+
break;
168225
default:
169226
ret = -EINVAL;
170227
break;

0 commit comments

Comments
 (0)