Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp852748pxp; Wed, 16 Mar 2022 19:05:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7+sn3HJ/IDIUZHltZabTCsb7HNeAQIAMbyGl+uTFhNtR9DxWS7XQY1uEBL4SIXWBY6QWn X-Received: by 2002:a17:906:1e4d:b0:6ce:6f7b:aef2 with SMTP id i13-20020a1709061e4d00b006ce6f7baef2mr2397136ejj.130.1647482750283; Wed, 16 Mar 2022 19:05:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647482750; cv=none; d=google.com; s=arc-20160816; b=VggXOqR/VoCtdHfJq+0zShcDPjkNMPUfvAylebpEYrP/TPVoBSd4Ak5Tq0fxPA5SOk zwUZJboi22KXZPE1GHCdc8L5ZKhbP4l7/UOO6xp97EUma77md/U52NgrE68hI/D2E9QG sR0XMVDs9bXyo7/H5VmaCwp+TpFog6RXltWEIasV5Ipzt+VyuZo+wNzvJ2hKCSJ5gcn2 bh6kUdH3nwHz8N1xSMIY6VFnlL2WuaGYz5424rgE8S8zj58J76N4DLgkYwEfhqzAxqOK OVILYmtPNCgbTXGfxQviVBQLBnHol3elfV973a87M7GFWhFgT2cAQqoYLHTtCCo5ZImX nlow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=iu1dDKMiO58IJTgn0Y5YT/ep6c6PH/A2I1xn5i2OO4M=; b=zQZrhlslcZd0kBm6bckwML+15DwWrxxbkl+e0WYrsE86JayrBFu33r7pvLSUrexiSt HnwKPejCI7oAs99JWz1+pLrZ1xcc/6W4bvD12dlDtyo/fFGB3gZnzJhBR0/FnMJkqVqH LKWVBVhVV1NPXj9kyFwk1XfHVlzhS2sw51CKJrw5MA8u6mYZkMy9hYC8sFXbxcWUsIC1 hchPwfBzCdGy4FZIOhoBEODj+HYrTwkmv4slLFn8ssLhfPWBdCPMWQRX5u3JLEwm6X0/ ROL47p4+K+qgDD3uLQiUSEyRnDkikJZXnwr0tP8Lt6JkzgSTt4HMztw3ks9jyke00QgO LWUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="A/QKF0mF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sh20-20020a1709076e9400b006ce6b771f09si2539858ejc.883.2022.03.16.19.05.25; Wed, 16 Mar 2022 19:05:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="A/QKF0mF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343812AbiCOAvc (ORCPT + 99 others); Mon, 14 Mar 2022 20:51:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343600AbiCOAvb (ORCPT ); Mon, 14 Mar 2022 20:51:31 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1C15427F3 for ; Mon, 14 Mar 2022 17:50:19 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id q5so24363555ljb.11 for ; Mon, 14 Mar 2022 17:50:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=iu1dDKMiO58IJTgn0Y5YT/ep6c6PH/A2I1xn5i2OO4M=; b=A/QKF0mF9Rm2tSG6t2Q7FLiZsPmEh4wgSlnqrxSBzJTT18Tx/25nw5YitC+o5T1NIl dHsn3dewSXLFAeqA/+ODgjyTVd/Tx+QWjF8zqtLmD0tIAydAS7721bsNCRqIJbtKOJmx 0AOy5rNA4qcgql5krqMhed3+t3qfYgcMOOsOjE3tnNwFKmr/qDH771GV9A5uGgXx8oaE Vj+L8eHW1IIPVnDdUjCH5rbstiX6McbtDPBKBdIhI8BKsO6vPZplxqdd24rIn7fSlSIs voa9xljon/WNiPC2LT2+MCpCtOQTHWC9qylrpk6wockoK/XRJO4YexgeGVa3vcONg4mj 368Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=iu1dDKMiO58IJTgn0Y5YT/ep6c6PH/A2I1xn5i2OO4M=; b=SiRAs21EZ776C/IRyISaTNXlhocsZgIutcDGeiXRk3IHisMAS9Zt5O46hes8deifHV Y/9d1NDNmWDZ/jmtkLLhMRvpqq1/RstA5JFWSrPPpHL1N7SCcQv5sCvjx184jW6FxvZ0 IG2ApH8teAYQlw8plvZNexGxLySYtMiv0VqkcKFaULQv7Dyur9s0hp0YHlG96lL1s+VY beRQSz87/hHEKnzzXglE/eGl+nJaQF690tTHnJ0Q2ozUYzyOluUrif0o/OK20JyGBvuK iIBGp+8wlANumh4IvBzxR84NCVgRF6h5a0LNJx4yKwdz17FOyi4WizaPXjnoWaSyqi8z oTsA== X-Gm-Message-State: AOAM532fA5t0QlaLj+Kx5Kw8tZvILRl0megsRm6ZNsyj/RX7ivTsbbg5 o38FX6z8h/2jJoVS4sUPE29nhJ3hyrDwt97LOZLcIA== X-Received: by 2002:a2e:7c16:0:b0:244:be33:9718 with SMTP id x22-20020a2e7c16000000b00244be339718mr15092896ljc.467.1647305417826; Mon, 14 Mar 2022 17:50:17 -0700 (PDT) MIME-Version: 1.0 References: <20220309165222.2843651-1-tjmercier@google.com> <20220309165222.2843651-7-tjmercier@google.com> In-Reply-To: <20220309165222.2843651-7-tjmercier@google.com> From: Todd Kjos Date: Mon, 14 Mar 2022 17:50:05 -0700 Message-ID: Subject: Re: [RFC v3 6/8] binder: Add a buffer flag to relinquish ownership of fds To: "T.J. Mercier" Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Jonathan Corbet , Greg Kroah-Hartman , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Hridya Valsaraju , Suren Baghdasaryan , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , Benjamin Gaignard , Liam Mark , Laura Abbott , Brian Starkey , John Stultz , Tejun Heo , Zefan Li , Johannes Weiner , Shuah Khan , kaleshsingh@google.com, Kenny.Ho@amd.com, dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 9, 2022 at 8:52 AM T.J. Mercier wrote: > > From: Hridya Valsaraju > > This patch introduces a buffer flag BINDER_BUFFER_FLAG_SENDER_NO_NEED > that a process sending an fd array to another process over binder IPC > can set to relinquish ownership of the fds being sent for memory > accounting purposes. If the flag is found to be set during the fd array > translation and the fd is for a DMA-BUF, the buffer is uncharged from > the sender's cgroup and charged to the receiving process's cgroup > instead. > > It is up to the sending process to ensure that it closes the fds > regardless of whether the transfer failed or succeeded. > > Most graphics shared memory allocations in Android are done by the > graphics allocator HAL process. On requests from clients, the HAL process > allocates memory and sends the fds to the clients over binder IPC. > The graphics allocator HAL will not retain any references to the > buffers. When the HAL sets the BINDER_BUFFER_FLAG_SENDER_NO_NEED for fd > arrays holding DMA-BUF fds, the gpu cgroup controller will be able to > correctly charge the buffers to the client processes instead of the > graphics allocator HAL. > > Since this is a new feature exposed to userspace, the kernel and userspac= e > must be compatible for the accounting to work for transfers. In all cases > the allocation and transport of DMA buffers via binder will succeed, but > only when both the kernel supports, and userspace depends on this feature > will the transfer accounting work. The possible scenarios are detailed > below: > > 1. new kernel + old userspace > The kernel supports the feature but userspace does not use it. The old > userspace won't mount the new cgroup controller, accounting is not > performed, charge is not transferred. > > 2. old kernel + new userspace > The new cgroup controller is not supported by the kernel, accounting is > not performed, charge is not transferred. > > 3. old kernel + old userspace > Same as #2 > > 4. new kernel + new userspace > Cgroup is mounted, feature is supported and used. > > Signed-off-by: Hridya Valsaraju > Signed-off-by: T.J. Mercier Acked-by: Todd Kjos > > --- > v3 changes > Remove android from title per Todd Kjos. > > Use more common dual author commit message format per John Stultz. > > Include details on behavior for all combinations of kernel/userspace > versions in changelog (thanks Suren Baghdasaryan) per Greg Kroah-Hartman. > > 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=C3=B6nig. > --- > drivers/android/binder.c | 26 ++++++++++++++++++++++++++ > include/uapi/linux/android/binder.h | 1 + > 2 files changed, 27 insertions(+) > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c > index 8351c5638880..f50d88ded188 100644 > --- a/drivers/android/binder.c > +++ b/drivers/android/binder.c > @@ -42,6 +42,7 @@ > > #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > > +#include > #include > #include > #include > @@ -2482,8 +2483,10 @@ static int binder_translate_fd_array(struct list_h= ead *pf_head, > { > binder_size_t fdi, fd_buf_size; > binder_size_t fda_offset; > + bool transfer_gpu_charge =3D false; > const void __user *sender_ufda_base; > struct binder_proc *proc =3D thread->proc; > + struct binder_proc *target_proc =3D t->to_proc; > int ret; > > fd_buf_size =3D sizeof(u32) * fda->num_fds; > @@ -2521,8 +2524,15 @@ static int binder_translate_fd_array(struct list_h= ead *pf_head, > if (ret) > return ret; > > + if (IS_ENABLED(CONFIG_CGROUP_GPU) && > + parent->flags & BINDER_BUFFER_FLAG_SENDER_NO_NEED) > + transfer_gpu_charge =3D true; > + > for (fdi =3D 0; fdi < fda->num_fds; fdi++) { > u32 fd; > + struct dma_buf *dmabuf; > + struct gpucg *gpucg; > + > binder_size_t offset =3D fda_offset + fdi * sizeof(fd); > binder_size_t sender_uoffset =3D fdi * sizeof(fd); > > @@ -2532,6 +2542,22 @@ static int binder_translate_fd_array(struct list_h= ead *pf_head, > in_reply_to); > if (ret) > return ret > 0 ? -EINVAL : ret; > + > + if (!transfer_gpu_charge) > + continue; > + > + dmabuf =3D dma_buf_get(fd); > + if (IS_ERR(dmabuf)) > + continue; > + > + gpucg =3D gpucg_get(target_proc->tsk); > + ret =3D dma_buf_charge_transfer(dmabuf, gpucg); > + if (ret) { > + pr_warn("%d:%d Unable to transfer DMA-BUF fd char= ge to %d", > + proc->pid, thread->pid, target_proc->pid)= ; > + gpucg_put(gpucg); > + } > + dma_buf_put(dmabuf); > } > return 0; > } > diff --git a/include/uapi/linux/android/binder.h b/include/uapi/linux/and= roid/binder.h > index 3246f2c74696..169fd5069a1a 100644 > --- a/include/uapi/linux/android/binder.h > +++ b/include/uapi/linux/android/binder.h > @@ -137,6 +137,7 @@ struct binder_buffer_object { > > enum { > BINDER_BUFFER_FLAG_HAS_PARENT =3D 0x01, > + BINDER_BUFFER_FLAG_SENDER_NO_NEED =3D 0x02, > }; > > /* struct binder_fd_array_object - object describing an array of fds in = a buffer > -- > 2.35.1.616.g0bdcbb4464-goog >