Skip to content

Commit a5091fd

Browse files
Davidlohr Buesotorvalds
authored andcommitted
ipc/mqueue: optimize msg_get()
Our msg priorities became an rbtree as of d662985 ("ipc/mqueue: improve performance of send/recv"). However, consuming a msg in msg_get() remains logarithmic (still being better than the case before of course). By applying well known techniques to cache pointers we can have the node with the highest priority in O(1), which is specially nice for the rt cases. Furthermore, some callers can call msg_get() in a loop. A new msg_tree_erase() helper is also added to encapsulate the tree removal and node_cache game. Passes ltp mq testcases. Link: http://lkml.kernel.org/r/20190321190216.1719-2-dave@stgolabs.net Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Cc: Manfred Spraul <manfred@colorfullife.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0ecb582 commit a5091fd

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

‎ipc/mqueue.c‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct mqueue_inode_info {
7676
wait_queue_head_t wait_q;
7777

7878
struct rb_root msg_tree;
79+
struct rb_node *msg_tree_rightmost;
7980
struct posix_msg_tree_node *node_cache;
8081
struct mq_attr attr;
8182

@@ -131,6 +132,7 @@ static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
131132
{
132133
struct rb_node **p, *parent = NULL;
133134
struct posix_msg_tree_node *leaf;
135+
bool rightmost = true;
134136

135137
p = &info->msg_tree.rb_node;
136138
while (*p) {
@@ -139,9 +141,10 @@ static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
139141

140142
if (likely(leaf->priority == msg->m_type))
141143
goto insert_msg;
142-
else if (msg->m_type < leaf->priority)
144+
else if (msg->m_type < leaf->priority) {
143145
p = &(*p)->rb_left;
144-
else
146+
rightmost = false;
147+
} else
145148
p = &(*p)->rb_right;
146149
}
147150
if (info->node_cache) {
@@ -154,6 +157,10 @@ static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
154157
INIT_LIST_HEAD(&leaf->msg_list);
155158
}
156159
leaf->priority = msg->m_type;
160+
161+
if (rightmost)
162+
info->msg_tree_rightmost = &leaf->rb_node;
163+
157164
rb_link_node(&leaf->rb_node, parent, p);
158165
rb_insert_color(&leaf->rb_node, &info->msg_tree);
159166
insert_msg:
@@ -163,23 +170,35 @@ static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
163170
return 0;
164171
}
165172

173+
static inline void msg_tree_erase(struct posix_msg_tree_node *leaf,
174+
struct mqueue_inode_info *info)
175+
{
176+
struct rb_node *node = &leaf->rb_node;
177+
178+
if (info->msg_tree_rightmost == node)
179+
info->msg_tree_rightmost = rb_prev(node);
180+
181+
rb_erase(node, &info->msg_tree);
182+
if (info->node_cache) {
183+
kfree(leaf);
184+
} else {
185+
info->node_cache = leaf;
186+
}
187+
}
188+
166189
static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
167190
{
168-
struct rb_node **p, *parent = NULL;
191+
struct rb_node *parent = NULL;
169192
struct posix_msg_tree_node *leaf;
170193
struct msg_msg *msg;
171194

172195
try_again:
173-
p = &info->msg_tree.rb_node;
174-
while (*p) {
175-
parent = *p;
176-
/*
177-
* During insert, low priorities go to the left and high to the
178-
* right. On receive, we want the highest priorities first, so
179-
* walk all the way to the right.
180-
*/
181-
p = &(*p)->rb_right;
182-
}
196+
/*
197+
* During insert, low priorities go to the left and high to the
198+
* right. On receive, we want the highest priorities first, so
199+
* walk all the way to the right.
200+
*/
201+
parent = info->msg_tree_rightmost;
183202
if (!parent) {
184203
if (info->attr.mq_curmsgs) {
185204
pr_warn_once("Inconsistency in POSIX message queue, "
@@ -194,24 +213,14 @@ static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
194213
pr_warn_once("Inconsistency in POSIX message queue, "
195214
"empty leaf node but we haven't implemented "
196215
"lazy leaf delete!\n");
197-
rb_erase(&leaf->rb_node, &info->msg_tree);
198-
if (info->node_cache) {
199-
kfree(leaf);
200-
} else {
201-
info->node_cache = leaf;
202-
}
216+
msg_tree_erase(leaf, info);
203217
goto try_again;
204218
} else {
205219
msg = list_first_entry(&leaf->msg_list,
206220
struct msg_msg, m_list);
207221
list_del(&msg->m_list);
208222
if (list_empty(&leaf->msg_list)) {
209-
rb_erase(&leaf->rb_node, &info->msg_tree);
210-
if (info->node_cache) {
211-
kfree(leaf);
212-
} else {
213-
info->node_cache = leaf;
214-
}
223+
msg_tree_erase(leaf, info);
215224
}
216225
}
217226
info->attr.mq_curmsgs--;
@@ -254,6 +263,7 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
254263
info->qsize = 0;
255264
info->user = NULL; /* set when all is ok */
256265
info->msg_tree = RB_ROOT;
266+
info->msg_tree_rightmost = NULL;
257267
info->node_cache = NULL;
258268
memset(&info->attr, 0, sizeof(info->attr));
259269
info->attr.mq_maxmsg = min(ipc_ns->mq_msg_max,

0 commit comments

Comments
 (0)