Skip to content

Commit e64aa65

Browse files
Christoph Hellwigdledford
authored andcommitted
target: enhance and export target_alloc_sgl/target_free_sgl
The SRP target driver will need to allocate and chain it's own SGLs soon. For this export target_alloc_sgl, and add a new argument to it so that it can allocate an additional chain entry that doesn't point to a page. Also export transport_free_sgl after renaming it to target_free_sgl to free these SGLs again. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent a060b56 commit e64aa65

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

drivers/target/target_core_transport.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ static void target_complete_ok_work(struct work_struct *work)
21952195
transport_handle_queue_full(cmd, cmd->se_dev);
21962196
}
21972197

2198-
static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
2198+
void target_free_sgl(struct scatterlist *sgl, int nents)
21992199
{
22002200
struct scatterlist *sg;
22012201
int count;
@@ -2205,6 +2205,7 @@ static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
22052205

22062206
kfree(sgl);
22072207
}
2208+
EXPORT_SYMBOL(target_free_sgl);
22082209

22092210
static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
22102211
{
@@ -2225,7 +2226,7 @@ static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
22252226
static inline void transport_free_pages(struct se_cmd *cmd)
22262227
{
22272228
if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
2228-
transport_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
2229+
target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
22292230
cmd->t_prot_sg = NULL;
22302231
cmd->t_prot_nents = 0;
22312232
}
@@ -2236,7 +2237,7 @@ static inline void transport_free_pages(struct se_cmd *cmd)
22362237
* SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
22372238
*/
22382239
if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
2239-
transport_free_sgl(cmd->t_bidi_data_sg,
2240+
target_free_sgl(cmd->t_bidi_data_sg,
22402241
cmd->t_bidi_data_nents);
22412242
cmd->t_bidi_data_sg = NULL;
22422243
cmd->t_bidi_data_nents = 0;
@@ -2246,11 +2247,11 @@ static inline void transport_free_pages(struct se_cmd *cmd)
22462247
}
22472248
transport_reset_sgl_orig(cmd);
22482249

2249-
transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
2250+
target_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
22502251
cmd->t_data_sg = NULL;
22512252
cmd->t_data_nents = 0;
22522253

2253-
transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
2254+
target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
22542255
cmd->t_bidi_data_sg = NULL;
22552256
cmd->t_bidi_data_nents = 0;
22562257
}
@@ -2324,20 +2325,22 @@ EXPORT_SYMBOL(transport_kunmap_data_sg);
23242325

23252326
int
23262327
target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
2327-
bool zero_page)
2328+
bool zero_page, bool chainable)
23282329
{
23292330
struct scatterlist *sg;
23302331
struct page *page;
23312332
gfp_t zero_flag = (zero_page) ? __GFP_ZERO : 0;
2332-
unsigned int nent;
2333+
unsigned int nalloc, nent;
23332334
int i = 0;
23342335

2335-
nent = DIV_ROUND_UP(length, PAGE_SIZE);
2336-
sg = kmalloc(sizeof(struct scatterlist) * nent, GFP_KERNEL);
2336+
nalloc = nent = DIV_ROUND_UP(length, PAGE_SIZE);
2337+
if (chainable)
2338+
nalloc++;
2339+
sg = kmalloc_array(nalloc, sizeof(struct scatterlist), GFP_KERNEL);
23372340
if (!sg)
23382341
return -ENOMEM;
23392342

2340-
sg_init_table(sg, nent);
2343+
sg_init_table(sg, nalloc);
23412344

23422345
while (length) {
23432346
u32 page_len = min_t(u32, length, PAGE_SIZE);
@@ -2361,6 +2364,7 @@ target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
23612364
kfree(sg);
23622365
return -ENOMEM;
23632366
}
2367+
EXPORT_SYMBOL(target_alloc_sgl);
23642368

23652369
/*
23662370
* Allocate any required resources to execute the command. For writes we
@@ -2376,7 +2380,7 @@ transport_generic_new_cmd(struct se_cmd *cmd)
23762380
if (cmd->prot_op != TARGET_PROT_NORMAL &&
23772381
!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
23782382
ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents,
2379-
cmd->prot_length, true);
2383+
cmd->prot_length, true, false);
23802384
if (ret < 0)
23812385
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
23822386
}
@@ -2401,13 +2405,13 @@ transport_generic_new_cmd(struct se_cmd *cmd)
24012405

24022406
ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
24032407
&cmd->t_bidi_data_nents,
2404-
bidi_length, zero_flag);
2408+
bidi_length, zero_flag, false);
24052409
if (ret < 0)
24062410
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
24072411
}
24082412

24092413
ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
2410-
cmd->data_length, zero_flag);
2414+
cmd->data_length, zero_flag, false);
24112415
if (ret < 0)
24122416
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
24132417
} else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
@@ -2421,7 +2425,7 @@ transport_generic_new_cmd(struct se_cmd *cmd)
24212425

24222426
ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
24232427
&cmd->t_bidi_data_nents,
2424-
caw_length, zero_flag);
2428+
caw_length, zero_flag, false);
24252429
if (ret < 0)
24262430
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
24272431
}

drivers/target/target_core_xcopy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static int target_xcopy_setup_pt_cmd(
563563

564564
if (alloc_mem) {
565565
rc = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
566-
cmd->data_length, false);
566+
cmd->data_length, false, false);
567567
if (rc < 0) {
568568
ret = rc;
569569
goto out;

include/target/target_core_backend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ extern struct configfs_attribute *passthrough_attrib_attrs[];
8585
void *transport_kmap_data_sg(struct se_cmd *);
8686
void transport_kunmap_data_sg(struct se_cmd *);
8787
/* core helpers also used by xcopy during internal command setup */
88-
int target_alloc_sgl(struct scatterlist **, unsigned int *, u32, bool);
8988
sense_reason_t transport_generic_map_mem_to_cmd(struct se_cmd *,
9089
struct scatterlist *, u32, struct scatterlist *, u32);
9190

include/target/target_core_fabric.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ int core_tpg_set_initiator_node_tag(struct se_portal_group *,
185185
int core_tpg_register(struct se_wwn *, struct se_portal_group *, int);
186186
int core_tpg_deregister(struct se_portal_group *);
187187

188+
int target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents,
189+
u32 length, bool zero_page, bool chainable);
190+
void target_free_sgl(struct scatterlist *sgl, int nents);
191+
188192
/*
189193
* The LIO target core uses DMA_TO_DEVICE to mean that data is going
190194
* to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean

0 commit comments

Comments
 (0)