All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bagas Sanjaya <bagasdotme@gmail.com>
To: Li Li <dualli@chromium.org>,
	dualli@google.com, corbet@lwn.net, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	donald.hunter@gmail.com, gregkh@linuxfoundation.org,
	arve@android.com, tkjos@android.com, maco@android.com,
	joel@joelfernandes.org, brauner@kernel.org, cmllamas@google.com,
	surenb@google.com, arnd@arndb.de, masahiroy@kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	netdev@vger.kernel.org, hridya@google.com, smoreland@google.com
Cc: kernel-team@android.com
Subject: Re: [PATCH v4 1/1] binder: report txn errors via generic netlink (genl)
Date: Tue, 22 Oct 2024 08:54:12 +0700	[thread overview]
Message-ID: <ZxcFxFfQM8gc5EEz@archie.me> (raw)
In-Reply-To: <20241021191233.1334897-2-dualli@chromium.org>

[-- Attachment #1: Type: text/plain, Size: 5156 bytes --]

On Mon, Oct 21, 2024 at 12:12:33PM -0700, Li Li wrote:
> diff --git a/Documentation/admin-guide/binder_genl.rst b/Documentation/admin-guide/binder_genl.rst
> new file mode 100644
> index 000000000000..48a0ceab6552
> --- /dev/null
> +++ b/Documentation/admin-guide/binder_genl.rst
> @@ -0,0 +1,92 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +===========================================================
> +Generic Netlink for the Android Binder Driver (Binder Genl)
> +===========================================================
> +
> +The Generic Netlink subsystem in the Linux kernel provides a generic way for
> +the Linux kernel to communicate to the user space applications. In the kernel
                                                     "... via binder driver"?
> +binder driver, it is used to report various kinds of binder transactions to
> +user space administration process. The binder driver allows multiple binder
                                      "The driver allows multiple ..."
> +devices and their corresponding binder contexts. Each binder context has a
                                                    "Each context has ..."
> +independent Generic Netlink for security reason. To prevent untrusted user
> +applications from accessing the netlink data, the kernel driver uses unicast
> +mode instead of multicast.
> +
> +Basically, the user space code use the "set" command to request what kinds
> +of binder transactions should be reported by the kernel binder driver. The
                                    "... reported by the driver."
> +kernel binder driver use "reply" command to acknowledge the request. The
"The driver then use ..."
> +"set" command also register the current user space process to receive the
> +reports. When the user space process exits, the previous request will be
> +reset to prevent any potential leaks.
> +
> +Currently the binder driver can report binder trasnactiosn that "failed"
"Currently the driver can report binder transactions that ..."
> +to reach the target process, or that are "delayed" due to the target process
> +being frozen by cgroup freezer, or that are considered "spam" according to
> +existing logic in binder_alloc.c.
> +
> +When the specified binder transactions happened, the binder driver uses the
"When the specified transactions happen, the driver ..."
> +"report" command to send a generic netlink message to the registered process,
> +containing the payload struct binder_report.
> +
> +More details about the flags, attributes and operations can be found at the
> +the doc sections in Documentations/netlink/specs/binder_genl.yaml and the
> +kernel-doc comments of the new source code in binder.{h|c}.
> +
> +Using Binder Genl
> +-----------------
> +
> +The Binder Genl can be used in the same way as any other generic netlink
> +drivers. The user space application uses a raw netlink socket to send commands
"Userspace application uses ..."
> +to and receive packets from the kernel driver.
> +
> +NOTE: if the user applications that talks to the Binder Genl driver exits,
"Note that if ..." or .. note:: block? I lean towards the latter, though.
> +the kernel driver will automatically reset the configuration to the default
> +and stop sending more reports to prevent leaking memory.
> +
> +Usage example (user space pseudo code):
> +
> +::
> +
> +    // open netlink socket
> +    int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
> +
> +    // bind netlink socket
> +    bind(fd, struct socketaddr);
> +
> +    // get the family id of the binder genl
> +    send(fd, CTRL_CMD_GETFAMILY, CTRL_ATTR_FAMILY_NAME, "binder");
> +    void *data = recv(CTRL_CMD_NEWFAMILY);
> +    __u16 id = nla(data)[CTRL_ATTR_FAMILY_ID];
> +
> +    // enable per-context binder report
> +    send(fd, id, BINDER_GENL_CMD_SET, 0, BINDER_GENL_FLAG_FAILED |
> +            BINDER_GENL_FLAG_DELAYED);
> +
> +    // confirm the per-context configuration
> +    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
> +    __u32 pid =  nla(data)[BINDER_GENL_ATTR_PID];
> +    __u32 flags = nla(data)[BINDER_GENL_ATTR_FLAGS];
> +
> +    // set optional per-process report, overriding the per-context one
> +    send(fd, id, BINDER_GENL_CMD_SET, getpid(),
> +            BINDER_GENL_FLAG_SPAM | BINDER_REPORT_OVERRIDE);
> +
> +    // confirm the optional per-process configuration
> +    void *data = recv(fd, BINDER_GENL_CMD_REPLY);
> +    __u32 pid =  nla(data)[BINDER_GENL_A_ATTR_PID];
> +    __u32 flags = nla(data)[BINDER_GENL_A_ATTR_FLAGS];
> +
> +    // wait and read all binder reports
> +    while (running) {
> +            void *data = recv(fd, BINDER_GENL_CMD_REPORT);
> +            struct binder_report report = nla(data)[BINDER_GENL_A_ATTR_REPORT];
> +
> +            // process struct binder_report
> +            do_something(&report);
> +    }
> +
> +    // clean up
> +    send(fd, id, BINDER_GENL_CMD_SET, 0, 0);
> +    send(fd, id, BINDER_GENL_CMD_SET, getpid(), 0);
> +    close(fd);

Thanks.

-- 
An old man doll... just what I always wanted! - Clara

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2024-10-22  1:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-21 19:12 [PATCH v4 0/1] binder: report txn errors via generic netlink (genl) Li Li
2024-10-21 19:12 ` [PATCH v4 1/1] " Li Li
2024-10-22  1:54   ` Bagas Sanjaya [this message]
2024-10-22 18:05     ` Li Li
2024-10-22 17:06   ` Simon Horman
2024-10-22 18:11     ` Li Li
2024-10-23 12:05       ` Simon Horman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZxcFxFfQM8gc5EEz@archie.me \
    --to=bagasdotme@gmail.com \
    --cc=arnd@arndb.de \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=dualli@chromium.org \
    --cc=dualli@google.com \
    --cc=edumazet@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hridya@google.com \
    --cc=joel@joelfernandes.org \
    --cc=kernel-team@android.com \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=masahiroy@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=smoreland@google.com \
    --cc=surenb@google.com \
    --cc=tkjos@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.