lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <IA0PR11MB7185919B9398B745C53C0C0AF8642@IA0PR11MB7185.namprd11.prod.outlook.com>
Date: Thu, 12 Sep 2024 22:50:57 +0000
From: "Kasireddy, Vivek" <vivek.kasireddy@...el.com>
To: Huan Yang <link@...o.com>, Sumit Semwal <sumit.semwal@...aro.org>,
	Christian König <christian.koenig@....com>, Gerd Hoffmann
	<kraxel@...hat.com>, "linux-media@...r.kernel.org"
	<linux-media@...r.kernel.org>, "dri-devel@...ts.freedesktop.org"
	<dri-devel@...ts.freedesktop.org>, "linaro-mm-sig@...ts.linaro.org"
	<linaro-mm-sig@...ts.linaro.org>, "linux-kernel@...r.kernel.org"
	<linux-kernel@...r.kernel.org>
CC: "opensource.kernel@...o.com" <opensource.kernel@...o.com>
Subject: RE: [PATCH v6 6/7] udmabuf: remove udmabuf_folio

Hi Huan,

> Subject: [PATCH v6 6/7] udmabuf: remove udmabuf_folio
> 
> Currently, udmabuf handles folio by creating an unpin list to record
> each folio obtained from the list and unpinning them when released. To
> maintain this approach, many data structures have been established.
> 
> However, maintaining this type of data structure requires a significant
> amount of memory and traversing the list is a substantial overhead,
> which is not friendly to the CPU cache.
> 
> Considering that during creation, we arranged the folio array in the
> order of pin and set the offset according to pgcnt.
> 
> We actually don't need to use unpin_list to unpin during release.
> Instead, we can iterate through the folios array during release and
> unpin any folio that is different from the ones previously accessed.
Sorry, I didn't notice this earlier but the last few lines here do not
reflect how unpin works in this latest patch version. Please update the
commit message to describe how it currently works.

Thanks,
Vivek

> 
> By this, not only saves the overhead of the udmabuf_folio data structure
> but also makes array access more cache-friendly.
> 
> Signed-off-by: Huan Yang <link@...o.com>
> Acked-by: Vivek Kasireddy <vivek.kasireddy@...el.com>
> ---
>  drivers/dma-buf/udmabuf.c | 65 +++++++++++++++++----------------------
>  1 file changed, 29 insertions(+), 36 deletions(-)
> 
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index c2eda960aaaa..0e405a589ca2 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -27,15 +27,21 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a
> dmabuf, in megabytes. Default is
>  struct udmabuf {
>  	pgoff_t pagecount;
>  	struct folio **folios;
> +
> +	/**
> +	 * Unlike folios, pinned_folios is only used for unpin.
> +	 * So, nr_pinned is not the same to pagecount, the pinned_folios
> +	 * only set each folio which already pinned when udmabuf_create.
> +	 * Note that, since a folio may be pinned multiple times, each folio
> +	 * can be added to pinned_folios multiple times, depending on how
> many
> +	 * times the folio has been pinned when create.
> +	 */
> +	pgoff_t nr_pinned;
> +	struct folio **pinned_folios;
> +
>  	struct sg_table *sg;
>  	struct miscdevice *device;
>  	pgoff_t *offsets;
> -	struct list_head unpin_list;
> -};
> -
> -struct udmabuf_folio {
> -	struct folio *folio;
> -	struct list_head list;
>  };
> 
>  static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> @@ -196,38 +202,18 @@ static void unmap_udmabuf(struct
> dma_buf_attachment *at,
>  	return put_sg_table(at->dev, sg, direction);
>  }
> 
> -static void unpin_all_folios(struct list_head *unpin_list)
> +static void unpin_all_folios(struct udmabuf *ubuf)
>  {
> -	struct udmabuf_folio *ubuf_folio;
> +	pgoff_t i;
> 
> -	while (!list_empty(unpin_list)) {
> -		ubuf_folio = list_first_entry(unpin_list,
> -					      struct udmabuf_folio, list);
> -		unpin_folio(ubuf_folio->folio);
> +	for (i = 0; i < ubuf->nr_pinned; ++i)
> +		unpin_folio(ubuf->pinned_folios[i]);
> 
> -		list_del(&ubuf_folio->list);
> -		kfree(ubuf_folio);
> -	}
> -}
> -
> -static int add_to_unpin_list(struct list_head *unpin_list,
> -			     struct folio *folio)
> -{
> -	struct udmabuf_folio *ubuf_folio;
> -
> -	ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
> -	if (!ubuf_folio)
> -		return -ENOMEM;
> -
> -	ubuf_folio->folio = folio;
> -	list_add_tail(&ubuf_folio->list, unpin_list);
> -	return 0;
> +	kvfree(ubuf->pinned_folios);
>  }
> 
>  static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t
> pgcnt)
>  {
> -	INIT_LIST_HEAD(&ubuf->unpin_list);
> -
>  	ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios),
> GFP_KERNEL);
>  	if (!ubuf->folios)
>  		return -ENOMEM;
> @@ -236,12 +222,18 @@ static __always_inline int init_udmabuf(struct
> udmabuf *ubuf, pgoff_t pgcnt)
>  	if (!ubuf->offsets)
>  		return -ENOMEM;
> 
> +	ubuf->pinned_folios = kvmalloc_array(pgcnt,
> +					     sizeof(*ubuf->pinned_folios),
> +					     GFP_KERNEL);
> +	if (!ubuf->pinned_folios)
> +		return -ENOMEM;
> +
>  	return 0;
>  }
> 
>  static __always_inline void deinit_udmabuf(struct udmabuf *ubuf)
>  {
> -	unpin_all_folios(&ubuf->unpin_list);
> +	unpin_all_folios(ubuf);
>  	kvfree(ubuf->offsets);
>  	kvfree(ubuf->folios);
>  }
> @@ -351,9 +343,11 @@ static int export_udmabuf(struct udmabuf *ubuf,
>  static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
>  			       loff_t start, loff_t size)
>  {
> -	pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
> +	pgoff_t nr_pinned = ubuf->nr_pinned;
> +	pgoff_t upgcnt = ubuf->pagecount;
>  	struct folio **folios = NULL;
>  	u32 cur_folio, cur_pgcnt;
> +	pgoff_t pgoff, pgcnt;
>  	long nr_folios;
>  	long ret = 0;
>  	loff_t end;
> @@ -375,9 +369,7 @@ static long udmabuf_pin_folios(struct udmabuf
> *ubuf, struct file *memfd,
>  		pgoff_t subpgoff = pgoff;
>  		size_t fsize = folio_size(folios[cur_folio]);
> 
> -		ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
> -		if (ret < 0)
> -			goto end;
> +		ubuf->pinned_folios[nr_pinned++] = folios[cur_folio];
> 
>  		for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
>  			ubuf->folios[upgcnt] = folios[cur_folio];
> @@ -398,6 +390,7 @@ static long udmabuf_pin_folios(struct udmabuf
> *ubuf, struct file *memfd,
>  	}
>  end:
>  	ubuf->pagecount = upgcnt;
> +	ubuf->nr_pinned = nr_pinned;
>  	kvfree(folios);
>  	return ret;
>  }
> --
> 2.45.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ