Linux 6.9-rc1
[linux-2.6-microblaze.git] / kernel / relay.c
index 6a611e7..a8e90e9 100644 (file)
@@ -60,10 +60,7 @@ static const struct vm_operations_struct relay_file_mmap_ops = {
  */
 static struct page **relay_alloc_page_array(unsigned int n_pages)
 {
-       const size_t pa_size = n_pages * sizeof(struct page *);
-       if (pa_size > PAGE_SIZE)
-               return vzalloc(pa_size);
-       return kzalloc(pa_size, GFP_KERNEL);
+       return kvcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
 }
 
 /*
@@ -94,7 +91,7 @@ static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
                return -EINVAL;
 
        vma->vm_ops = &relay_file_mmap_ops;
-       vma->vm_flags |= VM_DONTEXPAND;
+       vm_flags_set(vma, VM_DONTEXPAND);
        vma->vm_private_data = buf;
 
        return 0;
@@ -151,13 +148,13 @@ static struct rchan_buf *relay_create_buf(struct rchan *chan)
 {
        struct rchan_buf *buf;
 
-       if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t *))
+       if (chan->n_subbufs > KMALLOC_MAX_SIZE / sizeof(size_t))
                return NULL;
 
        buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
        if (!buf)
                return NULL;
-       buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t *),
+       buf->padding = kmalloc_array(chan->n_subbufs, sizeof(size_t),
                                     GFP_KERNEL);
        if (!buf->padding)
                goto free_buf;
@@ -378,7 +375,7 @@ static struct dentry *relay_create_buf_file(struct rchan *chan,
  */
 static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
 {
-       struct rchan_buf *buf = NULL;
+       struct rchan_buf *buf;
        struct dentry *dentry;
 
        if (chan->is_global)
@@ -510,7 +507,7 @@ struct rchan *relay_open(const char *base_filename,
        chan->private_data = private_data;
        if (base_filename) {
                chan->has_base_filename = 1;
-               strlcpy(chan->base_filename, base_filename, NAME_MAX);
+               strscpy(chan->base_filename, base_filename, NAME_MAX);
        }
        chan->cb = cb;
        kref_init(&chan->kref);
@@ -581,7 +578,7 @@ int relay_late_setup_files(struct rchan *chan,
        if (!chan || !base_filename)
                return -EINVAL;
 
-       strlcpy(chan->base_filename, base_filename, NAME_MAX);
+       strscpy(chan->base_filename, base_filename, NAME_MAX);
 
        mutex_lock(&relay_channels_mutex);
        /* Is chan already set up? */
@@ -992,7 +989,8 @@ static size_t relay_file_read_start_pos(struct rchan_buf *buf)
        size_t subbuf_size = buf->chan->subbuf_size;
        size_t n_subbufs = buf->chan->n_subbufs;
        size_t consumed = buf->subbufs_consumed % n_subbufs;
-       size_t read_pos = consumed * subbuf_size + buf->bytes_consumed;
+       size_t read_pos = (consumed * subbuf_size + buf->bytes_consumed)
+                       % (n_subbufs * subbuf_size);
 
        read_subbuf = read_pos / subbuf_size;
        padding = buf->padding[read_subbuf];
@@ -1075,167 +1073,6 @@ static ssize_t relay_file_read(struct file *filp,
        return written;
 }
 
-static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
-{
-       rbuf->bytes_consumed += bytes_consumed;
-
-       if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
-               relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
-               rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
-       }
-}
-
-static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
-                                  struct pipe_buffer *buf)
-{
-       struct rchan_buf *rbuf;
-
-       rbuf = (struct rchan_buf *)page_private(buf->page);
-       relay_consume_bytes(rbuf, buf->private);
-}
-
-static const struct pipe_buf_operations relay_pipe_buf_ops = {
-       .release        = relay_pipe_buf_release,
-       .try_steal      = generic_pipe_buf_try_steal,
-       .get            = generic_pipe_buf_get,
-};
-
-static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
-{
-}
-
-/*
- *     subbuf_splice_actor - splice up to one subbuf's worth of data
- */
-static ssize_t subbuf_splice_actor(struct file *in,
-                              loff_t *ppos,
-                              struct pipe_inode_info *pipe,
-                              size_t len,
-                              unsigned int flags,
-                              int *nonpad_ret)
-{
-       unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
-       struct rchan_buf *rbuf = in->private_data;
-       unsigned int subbuf_size = rbuf->chan->subbuf_size;
-       uint64_t pos = (uint64_t) *ppos;
-       uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
-       size_t read_start = (size_t) do_div(pos, alloc_size);
-       size_t read_subbuf = read_start / subbuf_size;
-       size_t padding = rbuf->padding[read_subbuf];
-       size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
-       struct page *pages[PIPE_DEF_BUFFERS];
-       struct partial_page partial[PIPE_DEF_BUFFERS];
-       struct splice_pipe_desc spd = {
-               .pages = pages,
-               .nr_pages = 0,
-               .nr_pages_max = PIPE_DEF_BUFFERS,
-               .partial = partial,
-               .ops = &relay_pipe_buf_ops,
-               .spd_release = relay_page_release,
-       };
-       ssize_t ret;
-
-       if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
-               return 0;
-       if (splice_grow_spd(pipe, &spd))
-               return -ENOMEM;
-
-       /*
-        * Adjust read len, if longer than what is available
-        */
-       if (len > (subbuf_size - read_start % subbuf_size))
-               len = subbuf_size - read_start % subbuf_size;
-
-       subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
-       pidx = (read_start / PAGE_SIZE) % subbuf_pages;
-       poff = read_start & ~PAGE_MASK;
-       nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max);
-
-       for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
-               unsigned int this_len, this_end, private;
-               unsigned int cur_pos = read_start + total_len;
-
-               if (!len)
-                       break;
-
-               this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
-               private = this_len;
-
-               spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
-               spd.partial[spd.nr_pages].offset = poff;
-
-               this_end = cur_pos + this_len;
-               if (this_end >= nonpad_end) {
-                       this_len = nonpad_end - cur_pos;
-                       private = this_len + padding;
-               }
-               spd.partial[spd.nr_pages].len = this_len;
-               spd.partial[spd.nr_pages].private = private;
-
-               len -= this_len;
-               total_len += this_len;
-               poff = 0;
-               pidx = (pidx + 1) % subbuf_pages;
-
-               if (this_end >= nonpad_end) {
-                       spd.nr_pages++;
-                       break;
-               }
-       }
-
-       ret = 0;
-       if (!spd.nr_pages)
-               goto out;
-
-       ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
-       if (ret < 0 || ret < total_len)
-               goto out;
-
-        if (read_start + ret == nonpad_end)
-                ret += padding;
-
-out:
-       splice_shrink_spd(&spd);
-       return ret;
-}
-
-static ssize_t relay_file_splice_read(struct file *in,
-                                     loff_t *ppos,
-                                     struct pipe_inode_info *pipe,
-                                     size_t len,
-                                     unsigned int flags)
-{
-       ssize_t spliced;
-       int ret;
-       int nonpad_ret = 0;
-
-       ret = 0;
-       spliced = 0;
-
-       while (len && !spliced) {
-               ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
-               if (ret < 0)
-                       break;
-               else if (!ret) {
-                       if (flags & SPLICE_F_NONBLOCK)
-                               ret = -EAGAIN;
-                       break;
-               }
-
-               *ppos += ret;
-               if (ret > len)
-                       len = 0;
-               else
-                       len -= ret;
-               spliced += nonpad_ret;
-               nonpad_ret = 0;
-       }
-
-       if (spliced)
-               return spliced;
-
-       return ret;
-}
 
 const struct file_operations relay_file_operations = {
        .open           = relay_file_open,
@@ -1244,6 +1081,5 @@ const struct file_operations relay_file_operations = {
        .read           = relay_file_read,
        .llseek         = no_llseek,
        .release        = relay_file_release,
-       .splice_read    = relay_file_splice_read,
 };
 EXPORT_SYMBOL_GPL(relay_file_operations);