[PATCH 04/11] lib/scatterlist: Extract scatterlist allocation from __sg_alloc_table
Tvrtko Ursulin
tursulin at ursulin.net
Fri Mar 2 10:54:59 UTC 2018
From: Tvrtko Ursulin <tvrtko.ursulin at intel.com>
Separating out the allocation engine will come useful in the following
patch.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin at intel.com>
Cc: Bart Van Assche <bart.vanassche at wdc.com>
Cc: Hannes Reinecke <hare at suse.com>
Cc: Johannes Thumshirn <jthumshirn at suse.de>
Cc: Jens Axboe <axboe at kernel.dk>
---
lib/scatterlist.c | 78 ++++++++++++++++++++++++++++++++++---------------------
1 file changed, 48 insertions(+), 30 deletions(-)
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index ee4b9f0fe00b..e137de4c36c9 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -253,34 +253,20 @@ void sg_free_table(struct sg_table *table)
}
EXPORT_SYMBOL(sg_free_table);
-/**
- * __sg_alloc_table - Allocate and initialize an sg table with given allocator
- * @table: The sg table header to use
- * @nents: Number of entries in sg list
- * @max_ents: The maximum number of entries the allocator returns per call
- * @gfp_mask: GFP allocation mask
- * @alloc_fn: Allocator to use
- *
- * Description:
- * This function returns a @table @nents long. The allocator is
- * defined to return scatterlist chunks of maximum size @max_ents.
- * Thus if @nents is bigger than @max_ents, the scatterlists will be
- * chained in units of @max_ents.
- *
- * Notes:
- * If this function returns non-0 (eg failure), the caller must call
- * __sg_free_table() to cleanup any leftover allocations.
- *
- **/
-int __sg_alloc_table(struct sg_table *table, unsigned int nents,
- unsigned int max_ents, struct scatterlist *first_chunk,
- gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
-{
- struct scatterlist *sg, *prv;
+static int
+__sg_alloc_scatterlist(unsigned int nents,
+ unsigned int max_ents,
+ struct scatterlist *first_chunk,
+ gfp_t gfp_mask,
+ sg_alloc_fn *alloc_fn,
+ unsigned int *out_nents,
+ unsigned int *out_orig_nents,
+ struct scatterlist **out_sgl)
+{
+ struct scatterlist *sg, *prv, *_sgl = NULL;
+ unsigned int _nents = 0, _orig_nents = 0;
unsigned int left;
- memset(table, 0, sizeof(*table));
-
if (nents == 0)
return -EINVAL;
#ifndef CONFIG_ARCH_HAS_SG_CHAIN
@@ -315,13 +301,13 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
* confused.
*/
if (prv)
- table->nents = ++table->orig_nents;
+ _nents = ++_orig_nents;
- return -ENOMEM;
+ return -ENOMEM;
}
sg_init_table(sg, alloc_size);
- table->nents = table->orig_nents += sg_size;
+ _nents = _orig_nents += sg_size;
/*
* If this is the first mapping, assign the sg table header.
@@ -330,7 +316,7 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
if (prv)
sg_chain(prv, max_ents, sg);
else
- table->sgl = sg;
+ _sgl = sg;
/*
* If no more entries after this one, mark the end
@@ -341,8 +327,40 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
prv = sg;
} while (left);
+ *out_nents = _nents;
+ *out_orig_nents = _orig_nents;
+ *out_sgl = _sgl;
+
return 0;
}
+
+/**
+ * __sg_alloc_table - Allocate and initialize an sg table with given allocator
+ * @table: The sg table header to use
+ * @nents: Number of entries in sg list
+ * @max_ents: The maximum number of entries the allocator returns per call
+ * @gfp_mask: GFP allocation mask
+ * @alloc_fn: Allocator to use
+ *
+ * Description:
+ * This function returns a @table @nents long. The allocator is
+ * defined to return scatterlist chunks of maximum size @max_ents.
+ * Thus if @nents is bigger than @max_ents, the scatterlists will be
+ * chained in units of @max_ents.
+ *
+ * Notes:
+ * If this function returns non-0 (eg failure), the caller must call
+ * __sg_free_table() to cleanup any leftover allocations.
+ *
+ **/
+int __sg_alloc_table(struct sg_table *table, unsigned int nents,
+ unsigned int max_ents, struct scatterlist *first_chunk,
+ gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
+{
+ return __sg_alloc_scatterlist(nents, max_ents, first_chunk, gfp_mask,
+ alloc_fn, &table->nents,
+ &table->orig_nents, &table->sgl);
+}
EXPORT_SYMBOL(__sg_alloc_table);
/**
--
2.14.1
More information about the Intel-gfx-trybot
mailing list