Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp265899img; Thu, 21 Mar 2019 19:52:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqyLL29Qwe6Ze50ZDMNCLz8VbGQ1wgFpTMhh1JerBhy2iMh2bEgFL2tVnxoni59PQmdMkwkS X-Received: by 2002:a17:902:3283:: with SMTP id z3mr7026787plb.236.1553223173260; Thu, 21 Mar 2019 19:52:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553223173; cv=none; d=google.com; s=arc-20160816; b=jMJJ4NHUy/Vj7pYFiIJGhL8rOdf1UsgT37M2n+tQpvQG5rj1HPL49pNg36HLdso5Yr uTUnb4gIDvJ4fvIf9bGPhhgyCGi8ExN+iofZRXko/F46H454IiM3BzbEHmMF5bn4FxwT SL+09id2jfGNCdoM4XZQD2ZSfJwGoCZHF/ANv4ouBWzbzJuEhhF9PY8AHGaLJC4D2SPw XItV5hssiaohUmiHzHL647uzKXtwwGEoLXIDG5/OQb1Q3nQVpfDRoiyqWfG9x46dOQI3 2YeEoUUZRJf6+XVFrv6Pbx1+aHjlmnyCzGwmizC/IQ67IwuRMSeN5nrH8mMdQbOv3xE9 ibfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=uq1bNToWBxtBjnkgoSUY1XGyp5z9AMAwbgi7ilfYWCs=; b=dsWNbeNzbpdFf65egi7W9PY920cJFPQ9dRA5XozXNO3CzoBbq1JiLXbvsjxQq1MCLv oZhFS4p+KVE8PgM9qnTsigpkGBg8MIu6Ma+LDM8hBegsp6Ih4+TPUzew/KvpLyg5/6Lc gArXUs7/P9RpHZunFIMbIguNm4+RyrMxc4aq6AJGEnTeR8Uk5zWiPS7Ak4OMBHaIMPcT 9Jcb1Ccm32bUZEOg9m687o4V3wlBmt79BVu4QtS02/FYda2ysBc/p/Nhi4CVavS4GZkr KG3A0Jn0rbO1qUE4eHuZoIf72G0+ho0imFOYOOttoMStZyrB4lWWw4gp3hXRMXcTGWoC /kwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=f1m3dYbZ; 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 w23si5357016pga.454.2019.03.21.19.52.35; Thu, 21 Mar 2019 19:52:53 -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=f1m3dYbZ; 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 S1727509AbfCVCvr (ORCPT + 99 others); Thu, 21 Mar 2019 22:51:47 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:41764 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727470AbfCVCvr (ORCPT ); Thu, 21 Mar 2019 22:51:47 -0400 Received: by mail-pg1-f201.google.com with SMTP id d128so827132pgc.8 for ; Thu, 21 Mar 2019 19:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uq1bNToWBxtBjnkgoSUY1XGyp5z9AMAwbgi7ilfYWCs=; b=f1m3dYbZi/vuTWyQvUEMEjaPrwCBWyYWDHfsotLoxbA0DbqRfofWgP0cwSRTo+MKLg 7dr185sZhOxwJ1fXJKJjz6ssUMLmST3ZxTXEQjGQWoeMEg3ISVZzZ/UqUu2c1PXmQofR J4VORxEFLTna9DVn7Nta4hTJ2lepKJMc2SbZHhEgNTgZhksuHu/cXxuYfMdcl0qksu9d KrsLBBKp9rsCR0MqEZUNqTer8LZdlkUDoLjk+oblZUk2NJJtCAizxxvUqDeLhsf09JP4 UtAaLFbTgfkl5aeOZwCNwR/aVe2OV6DaQwX0Ov343yCichCp6UIt94bJRLH3RZBMYUZy 3q+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uq1bNToWBxtBjnkgoSUY1XGyp5z9AMAwbgi7ilfYWCs=; b=Ba2Eq0Y0+ehYv0QDGJJCCrbxUXpiPbZqMgujMlnguJUBQtvlOvMkm0fI7JEGXN3Hd4 vTZVSDpmOPRWHSqaqfoBGLo3xAibLY5w6cEFB0MecVJDroxiWmAO2IOZ2nUObBQpO+rT VpaILmq2ZhiLviklP0aH9CnrnsaIOBM/bluUo+Ap9AdGV/Dfr6ocpHCBzUeyzN3t7kXT +W/1VHhrppT1H8NwDdUDt0K6YcZZYp/f98hiDIsBg9iAp0x4V0giHTSpHOmbO9YMyMm7 jxy8I13eIbiIidFe1lfUptN7R8gMQbLMZjUZ7biJ9FVpQzLY7weO2I7iRv/jHuSbJiUy xkEQ== X-Gm-Message-State: APjAAAVPSTJ+B8VFtfy/Wf/lgsKfqIga9jcwWlHvpasOxyT1u8g2q0iD I4PDAdQCY6G7Yg1s0i6ttTTpdymFEWrRBnonrntiU0KcdaHadr/jEgvPAhFyFI7JJUMJqfCyhzn zCEcurf31mchFn1AgQGrfgKJ0bUQHoJ8j0DQyE6tpyO5cx5WTMHPU0nYIwpAHZWF9Nk0= X-Received: by 2002:a63:94:: with SMTP id 142mr6579104pga.277.1553223105940; Thu, 21 Mar 2019 19:51:45 -0700 (PDT) Date: Thu, 21 Mar 2019 19:51:33 -0700 In-Reply-To: <20190322025135.118201-1-fengc@google.com> Message-Id: <20190322025135.118201-2-fengc@google.com> Mime-Version: 1.0 References: <20190322025135.118201-1-fengc@google.com> X-Mailer: git-send-email 2.21.0.225.g810b269d1ac-goog Subject: [RFC v2 1/3] dma-buf: give each buffer a full-fledged inode From: Chenbo Feng To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org Cc: kernel-team@android.com, Sumit Semwal , erickreyes@google.com, Daniel Vetter , Greg Hackmann , Chenbo Feng Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Hackmann By traversing /proc/*/fd and /proc/*/map_files, processes with CAP_ADMIN can get a lot of fine-grained data about how shmem buffers are shared among processes. 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 currently charged to the buffer (st_blocks / 512). In contrast, all dma-bufs share the same anonymous inode. So 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. On systems with 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. Replace the singleton inode with full-fledged inodes allocated by alloc_anon_inode(). This involves creating and mounting a mini-pseudo-filesystem for dma-buf, following the example in fs/aio.c. Signed-off-by: Greg Hackmann Signed-off-by: Chenbo Feng --- drivers/dma-buf/dma-buf.c | 63 ++++++++++++++++++++++++++++++++++---- include/uapi/linux/magic.h | 1 + 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 7c858020d14b..ffd5a2ad7d6f 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -34,8 +34,10 @@ #include #include #include +#include #include +#include static inline int is_dma_buf_file(struct file *); @@ -46,6 +48,25 @@ struct dma_buf_list { static struct dma_buf_list db_list; +static const struct dentry_operations dma_buf_dentry_ops = { + .d_dname = simple_dname, +}; + +static struct vfsmount *dma_buf_mnt; + +static struct dentry *dma_buf_fs_mount(struct file_system_type *fs_type, + int flags, const char *name, void *data) +{ + return mount_pseudo(fs_type, "dmabuf:", NULL, &dma_buf_dentry_ops, + DMA_BUF_MAGIC); +} + +static struct file_system_type dma_buf_fs_type = { + .name = "dmabuf", + .mount = dma_buf_fs_mount, + .kill_sb = kill_anon_super, +}; + static int dma_buf_release(struct inode *inode, struct file *file) { struct dma_buf *dmabuf; @@ -338,6 +359,31 @@ static inline int is_dma_buf_file(struct file *file) return file->f_op == &dma_buf_fops; } +static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags) +{ + struct file *file; + struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb); + + if (IS_ERR(inode)) + return ERR_CAST(inode); + + inode->i_size = dmabuf->size; + inode_set_bytes(inode, dmabuf->size); + + file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf", + flags, &dma_buf_fops); + if (IS_ERR(file)) + goto err_alloc_file; + file->f_flags = flags & (O_ACCMODE | O_NONBLOCK); + file->private_data = dmabuf; + + return file; + +err_alloc_file: + iput(inode); + return file; +} + /** * DOC: dma buf device access * @@ -433,8 +479,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) } dmabuf->resv = resv; - file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, - exp_info->flags); + file = dma_buf_getfile(dmabuf, exp_info->flags); if (IS_ERR(file)) { ret = PTR_ERR(file); goto err_dmabuf; @@ -1025,8 +1070,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused) return ret; seq_puts(s, "\nDma-buf Objects:\n"); - seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\n", - "size", "flags", "mode", "count"); + seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n", + "size", "flags", "mode", "count", "ino"); list_for_each_entry(buf_obj, &db_list.head, list_node) { ret = mutex_lock_interruptible(&buf_obj->lock); @@ -1037,11 +1082,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused) continue; } - seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n", + seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\n", buf_obj->size, buf_obj->file->f_flags, buf_obj->file->f_mode, file_count(buf_obj->file), - buf_obj->exp_name); + buf_obj->exp_name, + file_inode(buf_obj->file)->i_ino); robj = buf_obj->resv; while (true) { @@ -1136,6 +1182,10 @@ static inline void dma_buf_uninit_debugfs(void) static int __init dma_buf_init(void) { + dma_buf_mnt = kern_mount(&dma_buf_fs_type); + if (IS_ERR(dma_buf_mnt)) + return PTR_ERR(dma_buf_mnt); + mutex_init(&db_list.lock); INIT_LIST_HEAD(&db_list.head); dma_buf_init_debugfs(); @@ -1146,5 +1196,6 @@ subsys_initcall(dma_buf_init); static void __exit dma_buf_deinit(void) { dma_buf_uninit_debugfs(); + kern_unmount(dma_buf_mnt); } __exitcall(dma_buf_deinit); diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index f8c00045d537..665e18627f78 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -91,5 +91,6 @@ #define UDF_SUPER_MAGIC 0x15013346 #define BALLOON_KVM_MAGIC 0x13661366 #define ZSMALLOC_MAGIC 0x58295829 +#define DMA_BUF_MAGIC 0x444d4142 /* "DMAB" */ #endif /* __LINUX_MAGIC_H__ */ -- 2.21.0.225.g810b269d1ac-goog