Skip to content

Commit 4a674f3

Browse files
Stanislav Kinsburskytorvalds
authored andcommitted
ipc: introduce message queue copy feature
This patch is required for checkpoint/restore in userspace. c/r requires some way to get all pending IPC messages without deleting them from the queue (checkpoint can fail and in this case tasks will be resumed, so queue have to be valid). To achive this, new operation flag MSG_COPY for sys_msgrcv() system call was introduced. If this flag was specified, then mtype is interpreted as number of the message to copy. If MSG_COPY is set, then kernel will allocate dummy message with passed size, and then use new copy_msg() helper function to copy desired message (instead of unlinking it from the queue). Notes: 1) Return -ENOSYS if MSG_COPY is specified, but CONFIG_CHECKPOINT_RESTORE is not set. Signed-off-by: Stanislav Kinsbursky <skinsbursky@parallels.com> Cc: Serge Hallyn <serge.hallyn@canonical.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Pavel Emelyanov <xemul@parallels.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f9dd87f commit 4a674f3

4 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎include/uapi/linux/msg.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/* msgrcv options */
1111
#define MSG_NOERROR 010000 /* no error if message is too big */
1212
#define MSG_EXCEPT 020000 /* recv any msg except of specified type.*/
13+
#define MSG_COPY 040000 /* copy (not remove) all queue messages */
1314

1415
/* Obsolete, used only for backwards compatibility and libc5 compiles */
1516
struct msqid_ds {

‎ipc/msg.c‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,45 @@ static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
769769
return msgsz;
770770
}
771771

772+
#ifdef CONFIG_CHECKPOINT_RESTORE
773+
static inline struct msg_msg *fill_copy(unsigned long copy_nr,
774+
unsigned long msg_nr,
775+
struct msg_msg *msg,
776+
struct msg_msg *copy)
777+
{
778+
if (copy_nr == msg_nr)
779+
return copy_msg(msg, copy);
780+
return NULL;
781+
}
782+
783+
static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
784+
int msgflg, long *msgtyp,
785+
unsigned long *copy_number)
786+
{
787+
struct msg_msg *copy;
788+
789+
*copy_number = *msgtyp;
790+
*msgtyp = 0;
791+
/*
792+
* Create dummy message to copy real message to.
793+
*/
794+
copy = load_msg(buf, bufsz);
795+
if (!IS_ERR(copy))
796+
copy->m_ts = bufsz;
797+
return copy;
798+
}
799+
800+
static inline void free_copy(int msgflg, struct msg_msg *copy)
801+
{
802+
if (msgflg & MSG_COPY)
803+
free_msg(copy);
804+
}
805+
#else
806+
#define free_copy(msgflg, copy) do {} while (0)
807+
#define prepare_copy(buf, sz, msgflg, msgtyp, copy_nr) ERR_PTR(-ENOSYS)
808+
#define fill_copy(copy_nr, msg_nr, msg, copy) NULL
809+
#endif
810+
772811
long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
773812
int msgflg,
774813
long (*msg_handler)(void __user *, struct msg_msg *, size_t))
@@ -777,19 +816,29 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
777816
struct msg_msg *msg;
778817
int mode;
779818
struct ipc_namespace *ns;
819+
struct msg_msg *copy;
820+
unsigned long __maybe_unused copy_number;
780821

781822
if (msqid < 0 || (long) bufsz < 0)
782823
return -EINVAL;
824+
if (msgflg & MSG_COPY) {
825+
copy = prepare_copy(buf, bufsz, msgflg, &msgtyp, &copy_number);
826+
if (IS_ERR(copy))
827+
return PTR_ERR(copy);
828+
}
783829
mode = convert_mode(&msgtyp, msgflg);
784830
ns = current->nsproxy->ipc_ns;
785831

786832
msq = msg_lock_check(ns, msqid);
787-
if (IS_ERR(msq))
833+
if (IS_ERR(msq)) {
834+
free_copy(msgflg, copy);
788835
return PTR_ERR(msq);
836+
}
789837

790838
for (;;) {
791839
struct msg_receiver msr_d;
792840
struct list_head *tmp;
841+
long msg_counter = 0;
793842

794843
msg = ERR_PTR(-EACCES);
795844
if (ipcperms(ns, &msq->q_perm, S_IRUGO))
@@ -809,8 +858,15 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
809858
if (mode == SEARCH_LESSEQUAL &&
810859
walk_msg->m_type != 1) {
811860
msgtyp = walk_msg->m_type - 1;
861+
} else if (msgflg & MSG_COPY) {
862+
msg = fill_copy(copy_number,
863+
msg_counter,
864+
walk_msg, copy);
865+
if (msg)
866+
break;
812867
} else
813868
break;
869+
msg_counter++;
814870
}
815871
tmp = tmp->next;
816872
}
@@ -823,6 +879,8 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
823879
msg = ERR_PTR(-E2BIG);
824880
goto out_unlock;
825881
}
882+
if (msgflg & MSG_COPY)
883+
goto out_unlock;
826884
list_del(&msg->m_list);
827885
msq->q_qnum--;
828886
msq->q_rtime = get_seconds();
@@ -906,8 +964,10 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
906964
break;
907965
}
908966
}
909-
if (IS_ERR(msg))
967+
if (IS_ERR(msg)) {
968+
free_copy(msgflg, copy);
910969
return PTR_ERR(msg);
970+
}
911971

912972
bufsz = msg_handler(buf, msg, bufsz);
913973
free_msg(msg);

‎ipc/msgutil.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,45 @@ struct msg_msg *load_msg(const void __user *src, int len)
102102
free_msg(msg);
103103
return ERR_PTR(err);
104104
}
105+
#ifdef CONFIG_CHECKPOINT_RESTORE
106+
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
107+
{
108+
struct msg_msgseg *dst_pseg, *src_pseg;
109+
int len = src->m_ts;
110+
int alen;
111+
112+
BUG_ON(dst == NULL);
113+
if (src->m_ts > dst->m_ts)
114+
return ERR_PTR(-EINVAL);
115+
116+
alen = len;
117+
if (alen > DATALEN_MSG)
118+
alen = DATALEN_MSG;
119+
120+
dst->next = NULL;
121+
dst->security = NULL;
105122

123+
memcpy(dst + 1, src + 1, alen);
124+
125+
len -= alen;
126+
dst_pseg = dst->next;
127+
src_pseg = src->next;
128+
while (len > 0) {
129+
alen = len;
130+
if (alen > DATALEN_SEG)
131+
alen = DATALEN_SEG;
132+
memcpy(dst_pseg + 1, src_pseg + 1, alen);
133+
dst_pseg = dst_pseg->next;
134+
len -= alen;
135+
src_pseg = src_pseg->next;
136+
}
137+
138+
dst->m_type = src->m_type;
139+
dst->m_ts = src->m_ts;
140+
141+
return dst;
142+
}
143+
#endif
106144
int store_msg(void __user *dest, struct msg_msg *msg, int len)
107145
{
108146
int alen;

‎ipc/util.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ int ipc_parse_version (int *cmd);
140140

141141
extern void free_msg(struct msg_msg *msg);
142142
extern struct msg_msg *load_msg(const void __user *src, int len);
143+
extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
143144
extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
144145

145146
extern void recompute_msgmni(struct ipc_namespace *);

0 commit comments

Comments
 (0)