Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1435427yba; Thu, 9 May 2019 17:01:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqxM1Xd+LyrbXQEccVJRPbmmO/Hq+RsIGnBLSKDfFLmeh8kOtuESZlwMSTCfg9QtYJqlSNMv X-Received: by 2002:aa7:8ec6:: with SMTP id b6mr9328522pfr.234.1557446515159; Thu, 09 May 2019 17:01:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557446515; cv=none; d=google.com; s=arc-20160816; b=X39VEXfhRJc+CmU8yLFLO/Kv4r5Zgdl6EEs95JRDlFJdFpUWGhuSkKmrGJbJdOtUnG npsocfK33oBFOpQFNyeS5phLNK06/1cf3gN5fEdGOx5bZG/Ifcs4ErW8DwiPsrXXsZDs 30PEGuT47EKz33Xgzi3fABK2Y+StjwxKmZNVi8iOPkdj/4U1fvNnHgU2xWoRAmr4Fh1U H2l3C+Z5oRnntbseNoqniwnZKpEW7Aoi0aW35/I1rsuTO6zzbMDzDGcPBFsfa6590P1Q O73ukjqyVwpXA/zJIkVcVaagZs2LGvdUQMMdR+6oRJAYSDOafjhLNSNjB/1R+xwYed0b HkFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:from :subject:mime-version:message-id:date:dkim-signature; bh=M35jpHh+HUdAc93UKOjDO7JbyHiHSckhQH2gYDGobXY=; b=jea3K3/nNxdyVGLm1oofYpWdeuV1U9XDYX0+LphkqHrMeMRzAeH0UAv9qBjl5VeDXX /FUjsbtPadj4Mib8HhnV4BW8a3bAlFG8MmIH/n1pOJBKiQJy8bjYl9QOWPtvtf/HdIwo pViuZz2ex8ppADoviEF9PsjdDDFZGadSzp6l30/K//0oz7CcKHCN9c0vkJ6TlXhjmlLi cSXFSJZR0eT3Pu7y9bjPxhh4h+Sv3ZO5melHBVxOI0JKPfV5+Tf1qOEP/88SLEJDtGJI CWVGBYo+uzUqofkz8moxG03slV/0Dng5vQ7JJlsCPAFbI1oyZrg9DkVl8W5tc9RP2faw iFrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tSZ3t6N4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t1si4970023pgu.572.2019.05.09.17.01.36; Thu, 09 May 2019 17:01:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tSZ3t6N4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726873AbfEJAAj (ORCPT + 99 others); Thu, 9 May 2019 20:00:39 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:43807 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726845AbfEJAAi (ORCPT ); Thu, 9 May 2019 20:00:38 -0400 Received: by mail-qt1-f202.google.com with SMTP id q32so4436936qtk.10 for ; Thu, 09 May 2019 17:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=M35jpHh+HUdAc93UKOjDO7JbyHiHSckhQH2gYDGobXY=; b=tSZ3t6N4Z1hNAMYvgS92eIL566ndUhmt5xsPPoUSXPzweWrogmxc+KJ10EVf3pttkF merVBSZHG0xLznvz4TKUL7L/GZ2cjBzn9Uqwa07TqFu+hek4xO0PQZfpn+9AYYxEqHRe iG61HVtIimI9EQNzmCMq2HhMo1YFeWooHmUb1dCm4Km/pTzPbAhRn7WUXoNAQprfpOYl KpalQjheFynt2AC8LlfdwPcJT2Dd9exlL1kqGHdpBmSVkj/RAc0c7GLZHzWoZStRx/DU OVyvgiKkjGPP01EUrs0iwK5npCTDem8hdHBbtbZEpi9IcUifRkYzYt5ron+RYMbsxchS oAYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=M35jpHh+HUdAc93UKOjDO7JbyHiHSckhQH2gYDGobXY=; b=bOMphADQCPN2fUDeqQeOxcCeTkOZqZkuWNfYZ3xUsusnw0qFqntBWWP8WIGMIkgAM2 TxhCgsqzzvdVbOnwo0kBxKdkU4sV0ToIG+TxqgXao8O/9kypOEaomWewUzGHrHh8m8g5 2jJA/GGaNmnL/4j9EYFSlCAdN5igDs17ZPUT4XLLGHBqE6krtEydJB30RNmB0FREoTO3 TcfRbOucStMduwt2BV1RJJXMBOkWyTnnwzg6Er/TqHexhJP45/LzsK97wL0/xzg3cFdo HmSbq0niUMfBinh22kfbPv9W/3guZiLbV4nCXBzZN/9+xiOav4gLX5zp1dgwQ55eJJ0l 0TSQ== X-Gm-Message-State: APjAAAXEIJW3rjAjpnRmheixQWkagktIqUdPXEuQL2hXXN11Gj8HgGa4 lufgFDGUuy9biUb5rpSJsza9PZa7vF+1x87H8lf36YfF+8WOfbmy01YPYmbyGMfFJsMQliw9HTs SqbFdvhEOLo9UG3U9xsJ8x1geF+fskWaFJ6WWDp2p86EbLh1zAeeI0DjaT+QQl0RYfqQ= X-Received: by 2002:ac8:29af:: with SMTP id 44mr6461067qts.352.1557446435276; Thu, 09 May 2019 17:00:35 -0700 (PDT) Date: Thu, 9 May 2019 17:00:29 -0700 Message-Id: <20190510000032.40749-1-fengc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [dma-buf v3 0/3] Improve the dma-buf tracking From: Chenbo Feng To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org Cc: Sumit Semwal , Daniel Vetter , erickreyes@google.com, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, all dma-bufs share the same anonymous inode. While we can count how many dma-buf fds or mappings a process has, we can't get the size of the backing buffers or tell if two entries point to the same dma-buf. And in debugfs, we can get a per-buffer breakdown of size and reference count, but can't tell which processes are actually holding the references to each buffer. To resolve the issue above and provide better method for userspace to track the dma-buf usage across different processes, the following changes are proposed in dma-buf kernel side. First of all, replace the singleton inode inside the dma-buf subsystem with a mini-filesystem, and assign each dma-buf a unique inode out of this filesystem. With this change, calling stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's size (st_size), and even the number of pages assigned to each dma-buffer. Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo so in the case where a buffer is mmap()ed into a process=E2=80=99s address = space but all remaining fds have been closed, we can still get the dma-buf information and try to accociate it with the process by searching the proc/pid/maps and looking for the corresponding inode number exposed in dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs which lets userspace assign short names (e.g., "CAMERA") to buffers. This information can be extremely helpful for tracking and accounting shared buffers based on their usage and original purpose. Last but not least, add dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler to dma_buf_file_operations. The handler will print the file_count and name of each buffer. Change in v2: * Add a check to prevent changing dma-buf name when it is attached to devices. * Fixed some compile warnings Change in v3: * Removed the GET_DMA_BUF_NAME ioctls, add the dma_buf pointer to dentry d_fsdata so the name can be displayed in proc/pid/maps and proc/pid/map_files. Greg Hackmann (3): dma-buf: give each buffer a full-fledged inode dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls dma-buf: add show_fdinfo handler drivers/dma-buf/dma-buf.c | 122 +++++++++++++++++++++++++++++++++-- include/linux/dma-buf.h | 5 +- include/uapi/linux/dma-buf.h | 3 + include/uapi/linux/magic.h | 1 + 4 files changed, 124 insertions(+), 7 deletions(-) --=20 2.21.0.1020.gf2820cf01a-goog