diff --git a/kernel/pgo/fs.c b/kernel/pgo/fs.c index ef985159dad36f..0ce0dc9caf7a76 100644 --- a/kernel/pgo/fs.c +++ b/kernel/pgo/fs.c @@ -24,13 +24,14 @@ #include #include #include +#include #include "pgo.h" static struct dentry *directory; struct prf_private_data { void *buffer; - unsigned long size; + size_t size; }; /* @@ -213,6 +214,7 @@ static inline unsigned long prf_get_padding(unsigned long size) return 7 & (sizeof(u64) - size % sizeof(u64)); } +/* Note: caller *must* hold pgo_lock */ static unsigned long prf_buffer_size(void) { return sizeof(struct llvm_prf_header) + @@ -225,18 +227,22 @@ static unsigned long prf_buffer_size(void) /* * Serialize the profiling data into a format LLVM's tools can understand. + * Returns actual buffer size in p->size. + * Note: p->buffer must point into vzalloc()'d + * area of at least prf_buffer_size() in size. * Note: caller *must* hold pgo_lock. */ -static int prf_serialize(struct prf_private_data *p) +static int prf_serialize(struct prf_private_data *p, size_t buf_size) { int err = 0; void *buffer; + /* get buffer size, again. */ p->size = prf_buffer_size(); - p->buffer = vzalloc(p->size); - if (!p->buffer) { - err = -ENOMEM; + /* check for unlikely overflow. */ + if (p->size > buf_size) { + err = -EAGAIN; goto out; } @@ -259,27 +265,53 @@ static int prf_open(struct inode *inode, struct file *file) { struct prf_private_data *data; unsigned long flags; - int err; + size_t buf_size; + int err = 0; data = kzalloc(sizeof(*data), GFP_KERNEL); if (!data) { err = -ENOMEM; - goto out; + goto out_free; } + /* get initial buffer size */ flags = prf_lock(); + data->size = prf_buffer_size(); + prf_unlock(flags); - err = prf_serialize(data); - if (unlikely(err)) { - kfree(data); - goto out_unlock; - } + do { + if (data->buffer) + vfree(data->buffer); + + /* allocate, round up to page size. */ + buf_size = PAGE_ALIGN(data->size); + data->buffer = vzalloc(buf_size); + + if (!data->buffer) { + err = -ENOMEM; + goto out_free; + } + + /* + * try serialize and get actual + * data length in data->size + */ + flags = prf_lock(); + err = prf_serialize(data, buf_size); + prf_unlock(flags); + /* in unlikely case, try again. */ + } while (err == -EAGAIN); + + if (err) + goto out_free; file->private_data = data; + return 0; -out_unlock: - prf_unlock(flags); -out: +out_free: + if (data) + vfree(data->buffer); + kfree(data); return err; }