diff mbox series

[v2] tpm: ibmvtpm: Make use of dma_alloc_noncoherent()

Message ID 20211012032442.2423-1-caihuoqing@baidu.com (mailing list archive)
State New, archived
Headers show
Series [v2] tpm: ibmvtpm: Make use of dma_alloc_noncoherent() | expand

Commit Message

Cai,Huoqing Oct. 12, 2021, 3:24 a.m. UTC
Replacing kmalloc/kfree/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 keep DMA coherent itself.

Signed-off-by: Cai Huoqing <caihuoqing@baidu.com>
---
v1->v2:
	*Change to dma_alloc/free_noncoherent from dma_alloc/free_coherent.
	*Update changelog.

 drivers/char/tpm/tpm_ibmvtpm.c | 63 +++++++++++-----------------------
 1 file changed, 20 insertions(+), 43 deletions(-)

Comments

kernel test robot Nov. 12, 2021, 5:12 p.m. UTC | #1
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-20211112]
[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/tpm-ibmvtpm-Make-use-of-dma_alloc_noncoherent/20211012-112627
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc64-randconfig-r026-20211027 (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/8ef2c12d78e4782c08edad107067859612cdb39e
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Cai-Huoqing/tpm-ibmvtpm-Make-use-of-dma_alloc_noncoherent/20211012-112627
        git checkout 8ef2c12d78e4782c08edad107067859612cdb39e
        # 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 <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/char/tpm/tpm_ibmvtpm.c: In function 'tpm_ibmvtpm_remove':
>> drivers/char/tpm/tpm_ibmvtpm.c:359:62: error: 'crq_q' undeclared (first use in this function)
     359 |         dma_free_noncoherent(ibmvtpm->dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr,
         |                                                              ^~~~~
   drivers/char/tpm/tpm_ibmvtpm.c:359:62: note: each undeclared identifier is reported only once for each function it appears in
   drivers/char/tpm/tpm_ibmvtpm.c: In function 'tpm_ibmvtpm_probe':
>> drivers/char/tpm/tpm_ibmvtpm.c:690:51: error: 'struct ibmvtpm_crq_queue' has no member named 'crq_dma_handle'
     690 |                                              crq_q->crq_dma_handle, DMA_BIDIRECTIONAL);
         |                                                   ^~


vim +/crq_q +359 drivers/char/tpm/tpm_ibmvtpm.c

   336	
   337	/**
   338	 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
   339	 * @vdev:	vio device struct
   340	 *
   341	 * Return: Always 0.
   342	 */
   343	static void tpm_ibmvtpm_remove(struct vio_dev *vdev)
   344	{
   345		struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
   346		struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
   347		int rc = 0;
   348	
   349		tpm_chip_unregister(chip);
   350	
   351		free_irq(vdev->irq, ibmvtpm);
   352	
   353		do {
   354			if (rc)
   355				msleep(100);
   356			rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
   357		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
   358	
 > 359		dma_free_noncoherent(ibmvtpm->dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr,
   360				     crq_q->crq_dma_handle, DMA_BIDIRECTIONAL);
   361	
   362		if (ibmvtpm->rtce_buf)
   363			dma_free_noncoherent(ibmvtpm->dev,
   364					     ibmvtpm->rtce_size, ibmvtpm->rtce_buf,
   365					     ibmvtpm->rtce_dma_handle, DMA_BIDIRECTIONAL);
   366	
   367		kfree(ibmvtpm);
   368		/* For tpm_ibmvtpm_get_desired_dma */
   369		dev_set_drvdata(&vdev->dev, NULL);
   370	}
   371	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/char/tpm/tpm_ibmvtpm.c b/drivers/char/tpm/tpm_ibmvtpm.c
index 3af4c07a9342..b4552f8400b8 100644
--- a/drivers/char/tpm/tpm_ibmvtpm.c
+++ b/drivers/char/tpm/tpm_ibmvtpm.c
@@ -356,15 +356,13 @@  static void tpm_ibmvtpm_remove(struct vio_dev *vdev)
 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
 
-	dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
-			 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
-	free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
-
-	if (ibmvtpm->rtce_buf) {
-		dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
-				 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
-		kfree(ibmvtpm->rtce_buf);
-	}
+	dma_free_noncoherent(ibmvtpm->dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr,
+			     crq_q->crq_dma_handle, DMA_BIDIRECTIONAL);
+
+	if (ibmvtpm->rtce_buf)
+		dma_free_noncoherent(ibmvtpm->dev,
+				     ibmvtpm->rtce_size, ibmvtpm->rtce_buf,
+				     ibmvtpm->rtce_dma_handle, DMA_BIDIRECTIONAL);
 
 	kfree(ibmvtpm);
 	/* For tpm_ibmvtpm_get_desired_dma */
@@ -522,23 +520,12 @@  static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
 				return;
 			}
 			ibmvtpm->rtce_size = be16_to_cpu(crq->len);
-			ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
-						    GFP_ATOMIC);
-			if (!ibmvtpm->rtce_buf) {
-				dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
-				return;
-			}
-
-			ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
-				ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
-				DMA_BIDIRECTIONAL);
-
-			if (dma_mapping_error(ibmvtpm->dev,
-					      ibmvtpm->rtce_dma_handle)) {
-				kfree(ibmvtpm->rtce_buf);
-				ibmvtpm->rtce_buf = NULL;
-				dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
-			}
+			ibmvtpm->rtce_buf = dma_alloc_noncoherent(ibmvtpm->dev,
+								  ibmvtpm->rtce_size,
+								  &ibmvtpm->rtce_dma_handle,
+								  DMA_BIDIRECTIONAL, GFP_ATOMIC);
+			if (!ibmvtpm->rtce_buf)
+				dev_err(ibmvtpm->dev, "Failed to dma allocate rtce buffer\n");
 
 			return;
 		case VTPM_GET_VERSION_RES:
@@ -618,22 +605,14 @@  static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
 	ibmvtpm->vdev = vio_dev;
 
 	crq_q = &ibmvtpm->crq_queue;
-	crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
-	if (!crq_q->crq_addr) {
-		dev_err(dev, "Unable to allocate memory for crq_addr\n");
-		goto cleanup;
-	}
 
 	crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
 	init_waitqueue_head(&crq_q->wq);
-	ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
-						 CRQ_RES_BUF_SIZE,
-						 DMA_BIDIRECTIONAL);
-
-	if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
-		dev_err(dev, "dma mapping failed\n");
+	crq_q->crq_addr = dma_alloc_noncoherent(dev, CRQ_RES_BUF_SIZE,
+						&ibmvtpm->crq_dma_handle,
+						DMA_BIDIRECTIONAL, GFP_KERNEL);
+	if (!crq_q->crq_addr)
 		goto cleanup;
-	}
 
 	rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
 				ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
@@ -642,7 +621,7 @@  static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
 
 	if (rc) {
 		dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
-		goto reg_crq_cleanup;
+		goto cleanup;
 	}
 
 	rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
@@ -704,13 +683,11 @@  static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
 	do {
 		rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
 	} while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
-reg_crq_cleanup:
-	dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
-			 DMA_BIDIRECTIONAL);
 cleanup:
 	if (ibmvtpm) {
 		if (crq_q->crq_addr)
-			free_page((unsigned long)crq_q->crq_addr);
+			dma_free_noncoherent(dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr,
+					     crq_q->crq_dma_handle, DMA_BIDIRECTIONAL);
 		kfree(ibmvtpm);
 	}