2021-10-12 03:25:00

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v2] scsi: ibmvscsi: Use dma_alloc_noncoherent() instead of get_zeroed_page/dma_map_single()

Replacing get_zeroed_page/free_page/dma_map_single/dma_unmap_single()
with dma_alloc_noncoherent/dma_free_noncoherent() helps to reduce
code size, and simplify the code, and the hardware can keeep DMA
coherent itself.

Signed-off-by: Cai Huoqing <[email protected]>
---
v1->v2:
*Change to dma_alloc/free_noncoherent from dma_alloc/free_coherent.
*Update changelog.

drivers/scsi/ibmvscsi/ibmvfc.c | 16 ++++------------
drivers/scsi/ibmvscsi/ibmvscsi.c | 29 +++++++++--------------------
2 files changed, 13 insertions(+), 32 deletions(-)

diff --git a/drivers/scsi/ibmvscsi/ibmvfc.c b/drivers/scsi/ibmvscsi/ibmvfc.c
index 1f1586ad48fe..6e95fd02fd25 100644
--- a/drivers/scsi/ibmvscsi/ibmvfc.c
+++ b/drivers/scsi/ibmvscsi/ibmvfc.c
@@ -869,8 +869,8 @@ static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
{
struct device *dev = vhost->dev;

- dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
- free_page((unsigned long)queue->msgs.handle);
+ dma_free_noncoherent(dev, PAGE_SIZE, queue->msgs.handle,
+ queue->msg_token, DMA_BIDIRECTIONAL);
queue->msgs.handle = NULL;

ibmvfc_free_event_pool(vhost, queue);
@@ -5663,19 +5663,11 @@ static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
return -ENOMEM;
}

- queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
+ queue->msgs.handle = dma_alloc_noncoherent(dev, PAGE_SIZE, &queue->msg_token,
+ DMA_BIDIRECTIONAL, GFP_KERNEL);
if (!queue->msgs.handle)
return -ENOMEM;

- queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
- DMA_BIDIRECTIONAL);
-
- if (dma_mapping_error(dev, queue->msg_token)) {
- free_page((unsigned long)queue->msgs.handle);
- queue->msgs.handle = NULL;
- return -ENOMEM;
- }
-
queue->cur = 0;
queue->fmt = fmt;
queue->size = PAGE_SIZE / fmt_size;
diff --git a/drivers/scsi/ibmvscsi/ibmvscsi.c b/drivers/scsi/ibmvscsi/ibmvscsi.c
index ea8e01f49cba..68409c298c74 100644
--- a/drivers/scsi/ibmvscsi/ibmvscsi.c
+++ b/drivers/scsi/ibmvscsi/ibmvscsi.c
@@ -151,10 +151,8 @@ static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
msleep(100);
rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
} while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
- dma_unmap_single(hostdata->dev,
- queue->msg_token,
- queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
- free_page((unsigned long)queue->msgs);
+ dma_free_noncoherent(hostdata->dev, PAGE_SIZE,
+ queue->msgs, queue->msg_token, DMA_BIDIRECTIONAL);
}

/**
@@ -331,18 +329,12 @@ static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
int retrc;
struct vio_dev *vdev = to_vio_dev(hostdata->dev);

- queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
-
- if (!queue->msgs)
- goto malloc_failed;
queue->size = PAGE_SIZE / sizeof(*queue->msgs);
-
- queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
- queue->size * sizeof(*queue->msgs),
- DMA_BIDIRECTIONAL);
-
- if (dma_mapping_error(hostdata->dev, queue->msg_token))
- goto map_failed;
+ queue->msgs = dma_alloc_noncoherent(hostdata->dev,
+ PAGE_SIZE, &queue->msg_token,
+ DMA_BIDIRECTIONAL, GFP_KERNEL);
+ if (!queue->msg)
+ goto malloc_failed;

gather_partition_info();
set_adapter_info(hostdata);
@@ -395,11 +387,8 @@ static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
} while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
reg_crq_failed:
- dma_unmap_single(hostdata->dev,
- queue->msg_token,
- queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
- map_failed:
- free_page((unsigned long)queue->msgs);
+ dma_free_noncoherent(hostdata->dev, PAGE_SIZE, queue->msg,
+ queue->msg_token, DMA_BIDIRECTIONAL);
malloc_failed:
return -1;
}
--
2.25.1


2021-10-15 01:28:33

by Nathan Lynch

[permalink] [raw]
Subject: Re: [PATCH v2] scsi: ibmvscsi: Use dma_alloc_noncoherent() instead of get_zeroed_page/dma_map_single()

Cai Huoqing <[email protected]> writes:
> @@ -331,18 +329,12 @@ static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
> int retrc;
> struct vio_dev *vdev = to_vio_dev(hostdata->dev);
>
> - queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
> -
> - if (!queue->msgs)
> - goto malloc_failed;
> queue->size = PAGE_SIZE / sizeof(*queue->msgs);
> -
> - queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
> - queue->size * sizeof(*queue->msgs),
> - DMA_BIDIRECTIONAL);
> -
> - if (dma_mapping_error(hostdata->dev, queue->msg_token))
> - goto map_failed;
> + queue->msgs = dma_alloc_noncoherent(hostdata->dev,
> + PAGE_SIZE, &queue->msg_token,
> + DMA_BIDIRECTIONAL, GFP_KERNEL);
> + if (!queue->msg)
> + goto malloc_failed;


This version appears to retain the build breakage from v1 which was
reported here:

https://lore.kernel.org/linuxppc-dev/[email protected]/

drivers/scsi/ibmvscsi/ibmvscsi.c: In function 'ibmvscsi_init_crq_queue':
>> drivers/scsi/ibmvscsi/ibmvscsi.c:334:21: error: 'struct crq_queue' has no member named 'msg'; did you mean 'msgs'?
334 | if (!queue->msg)
| ^~~
| msgs
drivers/scsi/ibmvscsi/ibmvscsi.c:388:60: error: 'struct crq_queue' has no member named 'msg'; did you mean 'msgs'?
388 | dma_free_coherent(hostdata->dev, PAGE_SIZE, queue->msg, queue->msg_token);
| ^~~
| msgs

2021-10-15 03:55:49

by Tyrel Datwyler

[permalink] [raw]
Subject: Re: [PATCH v2] scsi: ibmvscsi: Use dma_alloc_noncoherent() instead of get_zeroed_page/dma_map_single()

On 10/11/21 8:23 PM, Cai Huoqing wrote:
> Replacing get_zeroed_page/free_page/dma_map_single/dma_unmap_single()
> with dma_alloc_noncoherent/dma_free_noncoherent() helps to reduce
> code size, and simplify the code, and the hardware can keeep DMA
> coherent itsel
Not sure why the switch from coherent in v1 to noncoherent in v2. I think that
was unnecessary and I believe requires explicit synchronization via
dma_sync_single_{for_device|for_cpu} calls.

Further, as both kernel-bot and Nathan have already pointed out this doesn't
even compile.

-Tyrel

>
> Signed-off-by: Cai Huoqing <[email protected]>
> ---
> v1->v2:
> *Change to dma_alloc/free_noncoherent from dma_alloc/free_coherent.
> *Update changelog.
>
> drivers/scsi/ibmvscsi/ibmvfc.c | 16 ++++------------
> drivers/scsi/ibmvscsi/ibmvscsi.c | 29 +++++++++--------------------
> 2 files changed, 13 insertions(+), 32 deletions(-)
>
> diff --git a/drivers/scsi/ibmvscsi/ibmvfc.c b/drivers/scsi/ibmvscsi/ibmvfc.c
> index 1f1586ad48fe..6e95fd02fd25 100644
> --- a/drivers/scsi/ibmvscsi/ibmvfc.c
> +++ b/drivers/scsi/ibmvscsi/ibmvfc.c
> @@ -869,8 +869,8 @@ static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
> {
> struct device *dev = vhost->dev;
>
> - dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
> - free_page((unsigned long)queue->msgs.handle);
> + dma_free_noncoherent(dev, PAGE_SIZE, queue->msgs.handle,
> + queue->msg_token, DMA_BIDIRECTIONAL);
> queue->msgs.handle = NULL;
>
> ibmvfc_free_event_pool(vhost, queue);
> @@ -5663,19 +5663,11 @@ static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
> return -ENOMEM;
> }
>
> - queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
> + queue->msgs.handle = dma_alloc_noncoherent(dev, PAGE_SIZE, &queue->msg_token,
> + DMA_BIDIRECTIONAL, GFP_KERNEL);
> if (!queue->msgs.handle)
> return -ENOMEM;
>
> - queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
> - DMA_BIDIRECTIONAL);
> -
> - if (dma_mapping_error(dev, queue->msg_token)) {
> - free_page((unsigned long)queue->msgs.handle);
> - queue->msgs.handle = NULL;
> - return -ENOMEM;
> - }
> -
> queue->cur = 0;
> queue->fmt = fmt;
> queue->size = PAGE_SIZE / fmt_size;
> diff --git a/drivers/scsi/ibmvscsi/ibmvscsi.c b/drivers/scsi/ibmvscsi/ibmvscsi.c
> index ea8e01f49cba..68409c298c74 100644
> --- a/drivers/scsi/ibmvscsi/ibmvscsi.c
> +++ b/drivers/scsi/ibmvscsi/ibmvscsi.c
> @@ -151,10 +151,8 @@ static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
> msleep(100);
> rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
> } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
> - dma_unmap_single(hostdata->dev,
> - queue->msg_token,
> - queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
> - free_page((unsigned long)queue->msgs);
> + dma_free_noncoherent(hostdata->dev, PAGE_SIZE,
> + queue->msgs, queue->msg_token, DMA_BIDIRECTIONAL);
> }
>
> /**
> @@ -331,18 +329,12 @@ static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
> int retrc;
> struct vio_dev *vdev = to_vio_dev(hostdata->dev);
>
> - queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
> -
> - if (!queue->msgs)
> - goto malloc_failed;
> queue->size = PAGE_SIZE / sizeof(*queue->msgs);
> -
> - queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
> - queue->size * sizeof(*queue->msgs),
> - DMA_BIDIRECTIONAL);
> -
> - if (dma_mapping_error(hostdata->dev, queue->msg_token))
> - goto map_failed;
> + queue->msgs = dma_alloc_noncoherent(hostdata->dev,
> + PAGE_SIZE, &queue->msg_token,
> + DMA_BIDIRECTIONAL, GFP_KERNEL);
> + if (!queue->msg)
> + goto malloc_failed;
>
> gather_partition_info();
> set_adapter_info(hostdata);
> @@ -395,11 +387,8 @@ static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
> rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
> } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
> reg_crq_failed:
> - dma_unmap_single(hostdata->dev,
> - queue->msg_token,
> - queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
> - map_failed:
> - free_page((unsigned long)queue->msgs);
> + dma_free_noncoherent(hostdata->dev, PAGE_SIZE, queue->msg,
> + queue->msg_token, DMA_BIDIRECTIONAL);
> malloc_failed:
> return -1;
> }
>

2021-11-08 21:06:00

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2] scsi: ibmvscsi: Use dma_alloc_noncoherent() instead of get_zeroed_page/dma_map_single()

Hi Cai,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.15 next-20211108]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Cai-Huoqing/scsi-ibmvscsi-Use-dma_alloc_noncoherent-instead-of-get_zeroed_page-dma_map_single/20211012-112447
base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc64-defconfig (attached as .config)
compiler: powerpc64-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ff4f34f9b1f3a0f14ed7472b8f4da0e12dc63d56
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Cai-Huoqing/scsi-ibmvscsi-Use-dma_alloc_noncoherent-instead-of-get_zeroed_page-dma_map_single/20211012-112447
git checkout ff4f34f9b1f3a0f14ed7472b8f4da0e12dc63d56
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=powerpc SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

drivers/scsi/ibmvscsi/ibmvscsi.c: In function 'ibmvscsi_init_crq_queue':
>> drivers/scsi/ibmvscsi/ibmvscsi.c:336:21: error: 'struct crq_queue' has no member named 'msg'; did you mean 'msgs'?
336 | if (!queue->msg)
| ^~~
| msgs
drivers/scsi/ibmvscsi/ibmvscsi.c:390:63: error: 'struct crq_queue' has no member named 'msg'; did you mean 'msgs'?
390 | dma_free_noncoherent(hostdata->dev, PAGE_SIZE, queue->msg,
| ^~~
| msgs


vim +336 drivers/scsi/ibmvscsi/ibmvscsi.c

313
314 /**
315 * ibmvscsi_init_crq_queue() - Initializes and registers CRQ with hypervisor
316 * @queue: crq_queue to initialize and register
317 * @hostdata: ibmvscsi_host_data of host
318 * @max_requests: maximum requests (unused)
319 *
320 * Allocates a page for messages, maps it for dma, and registers
321 * the crq with the hypervisor.
322 * Returns zero on success.
323 */
324 static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
325 struct ibmvscsi_host_data *hostdata,
326 int max_requests)
327 {
328 int rc;
329 int retrc;
330 struct vio_dev *vdev = to_vio_dev(hostdata->dev);
331
332 queue->size = PAGE_SIZE / sizeof(*queue->msgs);
333 queue->msgs = dma_alloc_noncoherent(hostdata->dev,
334 PAGE_SIZE, &queue->msg_token,
335 DMA_BIDIRECTIONAL, GFP_KERNEL);
> 336 if (!queue->msg)
337 goto malloc_failed;
338
339 gather_partition_info();
340 set_adapter_info(hostdata);
341
342 retrc = rc = plpar_hcall_norets(H_REG_CRQ,
343 vdev->unit_address,
344 queue->msg_token, PAGE_SIZE);
345 if (rc == H_RESOURCE)
346 /* maybe kexecing and resource is busy. try a reset */
347 rc = ibmvscsi_reset_crq_queue(queue,
348 hostdata);
349
350 if (rc == H_CLOSED) {
351 /* Adapter is good, but other end is not ready */
352 dev_warn(hostdata->dev, "Partner adapter not ready\n");
353 retrc = 0;
354 } else if (rc != 0) {
355 dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
356 goto reg_crq_failed;
357 }
358
359 queue->cur = 0;
360 spin_lock_init(&queue->lock);
361
362 tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
363 (unsigned long)hostdata);
364
365 if (request_irq(vdev->irq,
366 ibmvscsi_handle_event,
367 0, "ibmvscsi", (void *)hostdata) != 0) {
368 dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
369 vdev->irq);
370 goto req_irq_failed;
371 }
372
373 rc = vio_enable_interrupts(vdev);
374 if (rc != 0) {
375 dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
376 goto req_irq_failed;
377 }
378
379 return retrc;
380
381 req_irq_failed:
382 tasklet_kill(&hostdata->srp_task);
383 rc = 0;
384 do {
385 if (rc)
386 msleep(100);
387 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
388 } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
389 reg_crq_failed:
390 dma_free_noncoherent(hostdata->dev, PAGE_SIZE, queue->msg,
391 queue->msg_token, DMA_BIDIRECTIONAL);
392 malloc_failed:
393 return -1;
394 }
395

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (5.16 kB)
.config.gz (26.48 kB)
Download all attachments