[RFC 2/5] hostfs: make dentry sorting stable allocation wise
Johannes Berg
johannes at sipsolutions.net
Thu Nov 19 05:24:39 PST 2026
From: Benjamin Berg <benjamin.berg at intel.com>
The order and size of kmalloc/kfree will affect memory management in the
long run. Hide the any differences of the undefined dentry ordering by
the host not by sorting the list but by swapping the included
information. This makes it seem to the UML kernel as if the allocations
happened in our dentry sort order.
Signed-off-by: Benjamin Berg <benjamin.berg at intel.com>
Signed-off-by: Johannes Berg <johannes.berg at intel.com>
---
fs/hostfs/hostfs_kern.c | 53 ++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 19 deletions(-)
diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c
index e8419b96b869..224683fd74cb 100644
--- a/fs/hostfs/hostfs_kern.c
+++ b/fs/hostfs/hostfs_kern.c
@@ -34,7 +34,7 @@ struct hostfs_dentry_list {
unsigned int type;
int name_len;
- char name[];
+ char name[NAME_MAX + 1];
};
struct hostfs_inode_info {
@@ -296,18 +296,6 @@ static const struct super_operations hostfs_sbops = {
.show_options = hostfs_show_options,
};
-static int hostfs_dentry_sort(void *priv,
- const struct list_head *a,
- const struct list_head *b)
-{
- struct hostfs_dentry_list *hdl_a =
- list_entry(a, struct hostfs_dentry_list, list);
- struct hostfs_dentry_list *hdl_b =
- list_entry(b, struct hostfs_dentry_list, list);
-
- return strcmp(hdl_a->name, hdl_b->name);
-}
-
static loff_t hostfs_seekdir(struct file *file, loff_t offset, int whence)
{
struct inode *inode = file->f_mapping->host;
@@ -352,24 +340,51 @@ static int hostfs_readdir(struct file *file, struct dir_context *ctx)
goto discard_cache;
while ((name = read_dir(dir, &next, &ino, &len, &type))) {
- hdl = kzalloc(struct_size(hdl, name, len + 1),
- GFP_KERNEL);
+ hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
if (!hdl) {
error = ENOMEM;
goto discard_cache;
}
+ INIT_LIST_HEAD(&hdl->list);
+ list_add_tail(&hdl->list, dentry_list);
+
+ /*
+ * This is a bit evil. We created a new entry, but we
+ * don't simply use it. We bubble the empty list item
+ * up by copying the contained data for all items that
+ * should be sorted later. Then we use this empty item
+ * to store the new dentry.
+ * By doing this the order that the dentries were
+ * returned by the host is completely hidden from the
+ * UML kernel. If that was not the case then the order
+ * of kmalloc/kfree could vary between runs.
+ */
+ while (!list_is_first(&hdl->list, dentry_list)) {
+ struct hostfs_dentry_list *prev =
+ list_entry(hdl->list.prev,
+ struct hostfs_dentry_list,
+ list);
+
+ if (strcmp(name, prev->name) >= 0)
+ break;
+
+ hdl->ino = prev->ino;
+ hdl->type = prev->type;
+ hdl->name_len = prev->name_len;
+ memcpy(hdl->name, prev->name,
+ prev->name_len + 1);
+
+ hdl = prev;
+ }
+
hdl->name_len = len;
hdl->ino = ino;
hdl->type = type;
memcpy(hdl->name, name, len + 1);
- INIT_LIST_HEAD(&hdl->list);
- list_add_tail(&hdl->list, dentry_list);
}
close_dir(dir);
-
- list_sort(NULL, dentry_list, hostfs_dentry_sort);
}
pos = -1;
--
2.56.1
More information about the linux-um
mailing list