]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - lib/scatterlist.c
lib/genalloc.c: fix allocation of aligned buffer from non-aligned chunk
[mirror_ubuntu-bionic-kernel.git] / lib / scatterlist.c
index 7c1c55f7daaa832df2aef731cd218d4ed80f44dc..00857ff550833e17f2b7f3b9542f93fe4bcf7047 100644 (file)
@@ -474,6 +474,111 @@ int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
 }
 EXPORT_SYMBOL(sg_alloc_table_from_pages);
 
+#ifdef CONFIG_SGL_ALLOC
+
+/**
+ * sgl_alloc_order - allocate a scatterlist and its pages
+ * @length: Length in bytes of the scatterlist. Must be at least one
+ * @order: Second argument for alloc_pages()
+ * @chainable: Whether or not to allocate an extra element in the scatterlist
+ *     for scatterlist chaining purposes
+ * @gfp: Memory allocation flags
+ * @nent_p: [out] Number of entries in the scatterlist that have pages
+ *
+ * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
+ */
+struct scatterlist *sgl_alloc_order(unsigned long long length,
+                                   unsigned int order, bool chainable,
+                                   gfp_t gfp, unsigned int *nent_p)
+{
+       struct scatterlist *sgl, *sg;
+       struct page *page;
+       unsigned int nent, nalloc;
+       u32 elem_len;
+
+       nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
+       /* Check for integer overflow */
+       if (length > (nent << (PAGE_SHIFT + order)))
+               return NULL;
+       nalloc = nent;
+       if (chainable) {
+               /* Check for integer overflow */
+               if (nalloc + 1 < nalloc)
+                       return NULL;
+               nalloc++;
+       }
+       sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
+                           (gfp & ~GFP_DMA) | __GFP_ZERO);
+       if (!sgl)
+               return NULL;
+
+       sg_init_table(sgl, nent);
+       sg = sgl;
+       while (length) {
+               elem_len = min_t(u64, length, PAGE_SIZE << order);
+               page = alloc_pages(gfp, order);
+               if (!page) {
+                       sgl_free(sgl);
+                       return NULL;
+               }
+
+               sg_set_page(sg, page, elem_len, 0);
+               length -= elem_len;
+               sg = sg_next(sg);
+       }
+       WARN_ON_ONCE(sg);
+       if (nent_p)
+               *nent_p = nent;
+       return sgl;
+}
+EXPORT_SYMBOL(sgl_alloc_order);
+
+/**
+ * sgl_alloc - allocate a scatterlist and its pages
+ * @length: Length in bytes of the scatterlist
+ * @gfp: Memory allocation flags
+ * @nent_p: [out] Number of entries in the scatterlist
+ *
+ * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
+ */
+struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
+                             unsigned int *nent_p)
+{
+       return sgl_alloc_order(length, 0, false, gfp, nent_p);
+}
+EXPORT_SYMBOL(sgl_alloc);
+
+/**
+ * sgl_free_order - free a scatterlist and its pages
+ * @sgl: Scatterlist with one or more elements
+ * @order: Second argument for __free_pages()
+ */
+void sgl_free_order(struct scatterlist *sgl, int order)
+{
+       struct scatterlist *sg;
+       struct page *page;
+
+       for (sg = sgl; sg; sg = sg_next(sg)) {
+               page = sg_page(sg);
+               if (page)
+                       __free_pages(page, order);
+       }
+       kfree(sgl);
+}
+EXPORT_SYMBOL(sgl_free_order);
+
+/**
+ * sgl_free - free a scatterlist and its pages
+ * @sgl: Scatterlist with one or more elements
+ */
+void sgl_free(struct scatterlist *sgl)
+{
+       sgl_free_order(sgl, 0);
+}
+EXPORT_SYMBOL(sgl_free);
+
+#endif /* CONFIG_SGL_ALLOC */
+
 void __sg_page_iter_start(struct sg_page_iter *piter,
                          struct scatterlist *sglist, unsigned int nents,
                          unsigned long pgoffset)
@@ -537,17 +642,18 @@ static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
 {
        if (!miter->__remaining) {
                struct scatterlist *sg;
-               unsigned long pgoffset;
 
                if (!__sg_page_iter_next(&miter->piter))
                        return false;
 
                sg = miter->piter.sg;
-               pgoffset = miter->piter.sg_pgoffset;
 
-               miter->__offset = pgoffset ? 0 : sg->offset;
+               miter->__offset = miter->piter.sg_pgoffset ? 0 : sg->offset;
+               miter->piter.sg_pgoffset += miter->__offset >> PAGE_SHIFT;
+               miter->__offset &= PAGE_SIZE - 1;
                miter->__remaining = sg->offset + sg->length -
-                               (pgoffset << PAGE_SHIFT) - miter->__offset;
+                                    (miter->piter.sg_pgoffset << PAGE_SHIFT) -
+                                    miter->__offset;
                miter->__remaining = min_t(unsigned long, miter->__remaining,
                                           PAGE_SIZE - miter->__offset);
        }