dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
@ 2022-03-22  9:52 Michal Koutný
  2022-03-22 16:47 ` T.J. Mercier
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Koutný @ 2022-03-22  9:52 UTC (permalink / raw)
  To: T.J. Mercier
  Cc: Zefan Li, linux-doc, David Airlie, dri-devel, Benjamin Gaignard,
	Kalesh Singh, Joel Fernandes, Shuah Khan, Sumit Semwal, Kenny.Ho,
	Jonathan Corbet, Martijn Coenen, Laura Abbott, linux-media,
	Todd Kjos, linux-kselftest, linaro-mm-sig, Tejun Heo,
	CABdmKX3+mTjxWzgrv44SKWT7mdGnQKMrv6c26d=iWdNPG7f1VQ, cgroups,
	Suren Baghdasaryan, Christian Brauner, Greg Kroah-Hartman,
	linux-kernel, Liam Mark, Christian König, RFC,
	Arve Hjønnevåg, Thomas Zimmermann, Johannes Weiner,
	Hridya Valsaraju

On Mon, Mar 21, 2022 at 04:54:26PM -0700, "T.J. Mercier"
<tjmercier@google.com> wrote:
> Since the charge is duplicated in two cgroups for a short period
> before it is uncharged from the source cgroup I guess the situation
> you're thinking about is a global (or common ancestor) limit?

The common ancestor was on my mind (after the self-shortcut).

> I can see how that would be a problem for transfers done this way and
> an alternative would be to swap the order of the charge operations:
> first uncharge, then try_charge. To be certain the uncharge is
> reversible if the try_charge fails, I think I'd need either a mutex
> used at all gpucg_*charge call sites or access to the gpucg_mutex,

Yes, that'd provide safe conditions for such operations, although I'm
not sure these special types of memory can afford global lock on their
fast paths.

> which implies adding transfer support to gpu.c as part of the gpucg_*
> API itself and calling it here. Am I following correctly here?

My idea was to provide a special API (apart from
gpucp_{try_charge,uncharge}) to facilitate transfers...

> This series doesn't actually add limit support just accounting, but
> I'd like to get it right here.

...which could be implemented (or changed) depending on how the charging
is realized internally.


Michal

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
  2022-03-22  9:52 [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function Michal Koutný
@ 2022-03-22 16:47 ` T.J. Mercier
  2022-03-23 23:37   ` T.J. Mercier
  0 siblings, 1 reply; 6+ messages in thread
From: T.J. Mercier @ 2022-03-22 16:47 UTC (permalink / raw)
  To: Michal Koutný
  Cc: Zefan Li, linux-doc, David Airlie, dri-devel, Benjamin Gaignard,
	Kalesh Singh, Joel Fernandes, Shuah Khan, Sumit Semwal, Kenny.Ho,
	Jonathan Corbet, Martijn Coenen, Laura Abbott, linux-media,
	Todd Kjos, linaro-mm-sig, Tejun Heo,
	Subject: Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer
	function Reply-To: In-Reply-To:,
	cgroups, Suren Baghdasaryan, Christian Brauner,
	Greg Kroah-Hartman, linux-kernel, Liam Mark,
	Christian König, Arve Hjønnevåg,
	Thomas Zimmermann, Johannes Weiner, Hridya Valsaraju

On Tue, Mar 22, 2022 at 2:52 AM Michal Koutný <mkoutny@suse.com> wrote:
>
> On Mon, Mar 21, 2022 at 04:54:26PM -0700, "T.J. Mercier"
> <tjmercier@google.com> wrote:
> > Since the charge is duplicated in two cgroups for a short period
> > before it is uncharged from the source cgroup I guess the situation
> > you're thinking about is a global (or common ancestor) limit?
>
> The common ancestor was on my mind (after the self-shortcut).
>
> > I can see how that would be a problem for transfers done this way and
> > an alternative would be to swap the order of the charge operations:
> > first uncharge, then try_charge. To be certain the uncharge is
> > reversible if the try_charge fails, I think I'd need either a mutex
> > used at all gpucg_*charge call sites or access to the gpucg_mutex,
>
> Yes, that'd provide safe conditions for such operations, although I'm
> not sure these special types of memory can afford global lock on their
> fast paths.

I have a benchmark I think is suitable, so let me try this change to
the transfer implementation and see how it compares.

>
> > which implies adding transfer support to gpu.c as part of the gpucg_*
> > API itself and calling it here. Am I following correctly here?
>
> My idea was to provide a special API (apart from
> gpucp_{try_charge,uncharge}) to facilitate transfers...
>
> > This series doesn't actually add limit support just accounting, but
> > I'd like to get it right here.
>
> ...which could be implemented (or changed) depending on how the charging
> is realized internally.
>
>
> Michal

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
  2022-03-22 16:47 ` T.J. Mercier
@ 2022-03-23 23:37   ` T.J. Mercier
  0 siblings, 0 replies; 6+ messages in thread
From: T.J. Mercier @ 2022-03-23 23:37 UTC (permalink / raw)
  To: Michal Koutný
  Cc: Zefan Li, linux-doc, David Airlie, dri-devel, Benjamin Gaignard,
	Kalesh Singh, Joel Fernandes, Shuah Khan, Sumit Semwal, Kenny.Ho,
	Jonathan Corbet, Martijn Coenen, Laura Abbott, linux-media,
	Todd Kjos, linaro-mm-sig, Tejun Heo,
	Subject: Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer
	function Reply-To: In-Reply-To:,
	cgroups, Suren Baghdasaryan, Christian Brauner,
	Greg Kroah-Hartman, linux-kernel, Liam Mark,
	Christian König, Arve Hjønnevåg,
	Thomas Zimmermann, Johannes Weiner, Hridya Valsaraju

On Tue, Mar 22, 2022 at 9:47 AM T.J. Mercier <tjmercier@google.com> wrote:
>
> On Tue, Mar 22, 2022 at 2:52 AM Michal Koutný <mkoutny@suse.com> wrote:
> >
> > On Mon, Mar 21, 2022 at 04:54:26PM -0700, "T.J. Mercier"
> > <tjmercier@google.com> wrote:
> > > Since the charge is duplicated in two cgroups for a short period
> > > before it is uncharged from the source cgroup I guess the situation
> > > you're thinking about is a global (or common ancestor) limit?
> >
> > The common ancestor was on my mind (after the self-shortcut).
> >
> > > I can see how that would be a problem for transfers done this way and
> > > an alternative would be to swap the order of the charge operations:
> > > first uncharge, then try_charge. To be certain the uncharge is
> > > reversible if the try_charge fails, I think I'd need either a mutex
> > > used at all gpucg_*charge call sites or access to the gpucg_mutex,
> >
> > Yes, that'd provide safe conditions for such operations, although I'm
> > not sure these special types of memory can afford global lock on their
> > fast paths.
>
> I have a benchmark I think is suitable, so let me try this change to
> the transfer implementation and see how it compares.

I added a mutex to struct gpucg which is locked when charging the
cgroup initially during allocation, and also only for the source
cgroup during dma_buf_charge_transfer. Then I used a multithreaded
benchmark where each thread allocates 4, 8, 16, or 32 DMA buffers and
then sends them through Binder to another process with charge transfer
enabled. This was intended to generate contention for the mutex in
dma_buf_charge_transfer. The results of this benchmark show that the
difference between a mutex protected charge transfer and an
unprotected charge transfer is within measurement noise. The worst
data point shows about 3% overheard for the mutex.

So I'll prep this change for the next revision. Thanks for pointing it out.
>
> >
> > > which implies adding transfer support to gpu.c as part of the gpucg_*
> > > API itself and calling it here. Am I following correctly here?
> >
> > My idea was to provide a special API (apart from
> > gpucp_{try_charge,uncharge}) to facilitate transfers...
> >
> > > This series doesn't actually add limit support just accounting, but
> > > I'd like to get it right here.
> >
> > ...which could be implemented (or changed) depending on how the charging
> > is realized internally.
> >
> >
> > Michal

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
  2022-03-21 17:45   ` Michal Koutný
@ 2022-03-21 23:54     ` T.J. Mercier
  0 siblings, 0 replies; 6+ messages in thread
From: T.J. Mercier @ 2022-03-21 23:54 UTC (permalink / raw)
  To: Michal Koutný
  Cc: Zefan Li, linux-doc, David Airlie, dri-devel, Benjamin Gaignard,
	Kalesh Singh, Joel Fernandes, Shuah Khan, Sumit Semwal, Kenny.Ho,
	Jonathan Corbet, Martijn Coenen, Laura Abbott, linux-media,
	linux-kselftest, Todd Kjos, linaro-mm-sig, Tejun Heo, cgroups,
	Suren Baghdasaryan, Christian Brauner, Greg Kroah-Hartman,
	linux-kernel, Liam Mark, Christian König,
	Arve Hjønnevåg, Thomas Zimmermann, Johannes Weiner,
	Hridya Valsaraju

On Mon, Mar 21, 2022 at 10:45 AM Michal Koutný <mkoutny@suse.com> wrote:
>
> Hello.
>
> On Wed, Mar 09, 2022 at 04:52:15PM +0000, "T.J. Mercier" <tjmercier@google.com> wrote:
> > +int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg)
> > +{
> > +#ifdef CONFIG_CGROUP_GPU
> > +     struct gpucg *current_gpucg;
> > +     int ret = 0;
> > +
> > +     /*
> > +      * Verify that the cgroup of the process requesting the transfer is the
> > +      * same as the one the buffer is currently charged to.
> > +      */
> > +     current_gpucg = gpucg_get(current);
> > +     mutex_lock(&dmabuf->lock);
> > +     if (current_gpucg != dmabuf->gpucg) {
> > +             ret = -EPERM;
> > +             goto err;
> > +     }
>
> Add a shortcut for gpucg == current_gpucg?

Good idea, thank you!

>
> > +
> > +     ret = gpucg_try_charge(gpucg, dmabuf->gpucg_dev, dmabuf->size);
> > +     if (ret)
> > +             goto err;
> > +
> > +     dmabuf->gpucg = gpucg;
> > +
> > +     /* uncharge the buffer from the cgroup it's currently charged to. */
> > +     gpucg_uncharge(current_gpucg, dmabuf->gpucg_dev, dmabuf->size);
>
> I think gpucg_* API would need to cater for such transfers too since
> possibly transitional breach of a limit during the transfer may
> unnecessarily fail the operation.

Since the charge is duplicated in two cgroups for a short period
before it is uncharged from the source cgroup I guess the situation
you're thinking about is a global (or common ancestor) limit? I can
see how that would be a problem for transfers done this way and an
alternative would be to swap the order of the charge operations: first
uncharge, then try_charge. To be certain the uncharge is reversible if
the try_charge fails, I think I'd need either a mutex used at all
gpucg_*charge call sites or access to the gpucg_mutex, which implies
adding transfer support to gpu.c as part of the gpucg_* API itself and
calling it here. Am I following correctly here?

This series doesn't actually add limit support just accounting, but
I'd like to get it right here.

>
> My 0.02€,
> Michal

Thanks!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
  2022-03-09 16:52 ` [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function T.J. Mercier
@ 2022-03-21 17:45   ` Michal Koutný
  2022-03-21 23:54     ` T.J. Mercier
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Koutný @ 2022-03-21 17:45 UTC (permalink / raw)
  To: T.J. Mercier
  Cc: Zefan Li, linux-doc, David Airlie, dri-devel, Benjamin Gaignard,
	kaleshsingh, Joel Fernandes, Shuah Khan, Sumit Semwal, Kenny.Ho,
	Jonathan Corbet, Martijn Coenen, Laura Abbott, linux-media,
	linux-kselftest, Todd Kjos, linaro-mm-sig, Tejun Heo, cgroups,
	Suren Baghdasaryan, Christian Brauner, Greg Kroah-Hartman,
	linux-kernel, Liam Mark, Christian König,
	Arve Hjønnevåg, Thomas Zimmermann, Johannes Weiner,
	Hridya Valsaraju

Hello.

On Wed, Mar 09, 2022 at 04:52:15PM +0000, "T.J. Mercier" <tjmercier@google.com> wrote:
> +int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg)
> +{
> +#ifdef CONFIG_CGROUP_GPU
> +	struct gpucg *current_gpucg;
> +	int ret = 0;
> +
> +	/*
> +	 * Verify that the cgroup of the process requesting the transfer is the
> +	 * same as the one the buffer is currently charged to.
> +	 */
> +	current_gpucg = gpucg_get(current);
> +	mutex_lock(&dmabuf->lock);
> +	if (current_gpucg != dmabuf->gpucg) {
> +		ret = -EPERM;
> +		goto err;
> +	}

Add a shortcut for gpucg == current_gpucg?

> +
> +	ret = gpucg_try_charge(gpucg, dmabuf->gpucg_dev, dmabuf->size);
> +	if (ret)
> +		goto err;
> +
> +	dmabuf->gpucg = gpucg;
> +
> +	/* uncharge the buffer from the cgroup it's currently charged to. */
> +	gpucg_uncharge(current_gpucg, dmabuf->gpucg_dev, dmabuf->size);

I think gpucg_* API would need to cater for such transfers too since
possibly transitional breach of a limit during the transfer may
unnecessarily fail the operation.

My 0.02€,
Michal

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function
  2022-03-09 16:52 [RFC v3 0/8] Proposal for a GPU cgroup controller T.J. Mercier
@ 2022-03-09 16:52 ` T.J. Mercier
  2022-03-21 17:45   ` Michal Koutný
  0 siblings, 1 reply; 6+ messages in thread
From: T.J. Mercier @ 2022-03-09 16:52 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	David Airlie, Daniel Vetter, Jonathan Corbet, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Martijn Coenen,
	Joel Fernandes, Christian Brauner, Hridya Valsaraju,
	Suren Baghdasaryan, Sumit Semwal, Christian König,
	Benjamin Gaignard, Liam Mark, Laura Abbott, Brian Starkey,
	John Stultz, Tejun Heo, Zefan Li, Johannes Weiner, Shuah Khan
  Cc: linux-doc, Kenny.Ho, linux-kernel, dri-devel, linaro-mm-sig,
	linux-kselftest, kaleshsingh, cgroups, T.J. Mercier, linux-media

From: Hridya Valsaraju <hridya@google.com>

The dma_buf_charge_transfer function provides a way for processes to
transfer charge of a buffer to a different process. This is essential
for the cases where a central allocator process does allocations for
various subsystems, hands over the fd to the client who requested the
memory and drops all references to the allocated memory.

Signed-off-by: Hridya Valsaraju <hridya@google.com>
Signed-off-by: T.J. Mercier <tjmercier@google.com>

---
v3 changes
Use more common dual author commit message format per John Stultz.

v2 changes
Move dma-buf cgroup charge transfer from a dma_buf_op defined by every
heap to a single dma-buf function for all heaps per Daniel Vetter and
Christian König.
---
 drivers/dma-buf/dma-buf.c | 48 +++++++++++++++++++++++++++++++++++++++
 include/linux/dma-buf.h   |  2 ++
 2 files changed, 50 insertions(+)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 83d0d1b91547..55e1b982f840 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1374,6 +1374,54 @@ void dma_buf_vunmap(struct dma_buf *dmabuf, struct dma_buf_map *map)
 }
 EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap, DMA_BUF);
 
+/**
+ * dma_buf_charge_transfer - Change the GPU cgroup to which the provided dma_buf
+ * is charged.
+ * @dmabuf:	[in]	buffer whose charge will be migrated to a different GPU
+ *			cgroup
+ * @gpucg:	[in]	the destination GPU cgroup for dmabuf's charge
+ *
+ * Only tasks that belong to the same cgroup the buffer is currently charged to
+ * may call this function, otherwise it will return -EPERM.
+ *
+ * Returns 0 on success, or a negative errno code otherwise.
+ */
+int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg)
+{
+#ifdef CONFIG_CGROUP_GPU
+	struct gpucg *current_gpucg;
+	int ret = 0;
+
+	/*
+	 * Verify that the cgroup of the process requesting the transfer is the
+	 * same as the one the buffer is currently charged to.
+	 */
+	current_gpucg = gpucg_get(current);
+	mutex_lock(&dmabuf->lock);
+	if (current_gpucg != dmabuf->gpucg) {
+		ret = -EPERM;
+		goto err;
+	}
+
+	ret = gpucg_try_charge(gpucg, dmabuf->gpucg_dev, dmabuf->size);
+	if (ret)
+		goto err;
+
+	dmabuf->gpucg = gpucg;
+
+	/* uncharge the buffer from the cgroup it's currently charged to. */
+	gpucg_uncharge(current_gpucg, dmabuf->gpucg_dev, dmabuf->size);
+
+err:
+	mutex_unlock(&dmabuf->lock);
+	gpucg_put(current_gpucg);
+	return ret;
+#else
+	return 0;
+#endif /* CONFIG_CGROUP_GPU */
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_charge_transfer, DMA_BUF);
+
 #ifdef CONFIG_DEBUG_FS
 static int dma_buf_debug_show(struct seq_file *s, void *unused)
 {
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 742f29c3daaf..85c940c08867 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -646,4 +646,6 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
 		 unsigned long);
 int dma_buf_vmap(struct dma_buf *dmabuf, struct dma_buf_map *map);
 void dma_buf_vunmap(struct dma_buf *dmabuf, struct dma_buf_map *map);
+
+int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg);
 #endif /* __DMA_BUF_H__ */
-- 
2.35.1.616.g0bdcbb4464-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-03-23 23:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22  9:52 [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function Michal Koutný
2022-03-22 16:47 ` T.J. Mercier
2022-03-23 23:37   ` T.J. Mercier
  -- strict thread matches above, loose matches on Subject: below --
2022-03-09 16:52 [RFC v3 0/8] Proposal for a GPU cgroup controller T.J. Mercier
2022-03-09 16:52 ` [RFC v3 5/8] dmabuf: Add gpu cgroup charge transfer function T.J. Mercier
2022-03-21 17:45   ` Michal Koutný
2022-03-21 23:54     ` T.J. Mercier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).