Path: csiph.com!aioe.org!bofh.it!news.nic.it!robomod From: Ruchi Kandoi Newsgroups: linux.kernel Subject: [RFC 0/6] Module for tracking/accounting shared memory buffers Date: Wed, 12 Oct 2016 02:00:01 +0200 Message-ID: X-Original-To: kandoiruchi@google.com, gregkh@linuxfoundation.org, arve@android.com, riandrews@android.com, sumit.semwal@linaro.org, arnd@arndb.de, labbott@redhat.com, viro@zeniv.linux.org.uk, jlayton@poochiereds.net, bfields@fieldses.org, mingo@redhat.com, peterz@infradead.org, akpm@linux-foundation.org, keescook@chromium.org, mhocko@suse.com, oleg@redhat.com, john.stultz@linaro.org, mguzik@redhat.com, jdanis@google.com, adobriyan@gmail.com, ghackmann@google.com, kirill.shutemov@linux.intel.com, vbabka@suse.cz, dave.hansen@linux.intel.com, dan.j.williams@intel.com, hannes@cmpxchg.org, iamjoonsoo.kim@lge.com, luto@kernel.org, tj@kernel.org, vdavydov.dev@gmail.com, ebiederm@xmission.com, linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id; bh=+sVSJNLsYMtUqvuhkSX30BtTjT+1LqqOgydrCzP8Hdg=; b=TE2lnpIA/8lpsmE283sSD4BCzAwZZjfX15mJsXA/vsl6eGcAscaEnK5wr/OXwq4ldU AoKBsoyYawUQatFMQ6PLLzymSj5vJWXfsbGuaaFozy+RYGj0JY/uqJA9XpExNQU6z9+W R97vk2W0Wgdd4t6ofpHQWf355J9riomUd/LDRZD15Qh09cNH4Btr+mpTfaMa0kWJ/B20 2Nz3I6yIraj6kk7eW9dhvl48Off7uqui62G8WDb1DMhpb4mH7TF3jYI2VJjd3SuOuuM8 JgQ5vzyIz9FrnS/r4DdgM5Q+7le4xsz5hSNagOiAQyqnohscmnuSL3T1JAqQIyF1GpC+ D95Q== X-Google-Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=+sVSJNLsYMtUqvuhkSX30BtTjT+1LqqOgydrCzP8Hdg=; b=QNWwajiYY1Oh3C+fw4lIh6o/KNxNd8R2AQ7XMgnRj+2I43C0S3M4nD9cKwJ1ojOv4m NyC2VW7nbRaN2W3IWLlmbpOPg8/EVwtNOiryiidImf+h+CDQsySFXdhIbXH6jn4JE9PE w3H08OFjx++FMjxo2LLD/wuwUGTg40A8tX58Fey/ofSbsVWU6C9SDukxA2/EDPRBXdE1 vcY+NmXrX4aCPSlyZNqtEhfqMgtwLaxmH2cjCtKK5oDLgthXCcT5ELDpCfiYNFY/DWHK INewlvHUFGd5pExB3GGvauAzVR8KHsAJrq0J5TRWlxQPv5mr6jfgp4cZ1eQAdFMpCJUa CwZg== X-Gm-Message-State: AA6/9RmyE7bJIxu1SNAVtGjMjMc0uVFCU60jn/YOqdP0V1picRY21FdkE3ELA1zN7PQbFbx2 X-Received: by 10.98.7.129 with SMTP id 1mr814105pfh.110.1476229828080; Tue, 11 Oct 2016 16:50:28 -0700 (PDT) X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Sender: robomod@news.nic.it List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Approved: robomod@news.nic.it Lines: 66 Organization: linux.* mail to news gateway X-Original-Date: Tue, 11 Oct 2016 16:50:04 -0700 X-Original-Message-ID: <1476229810-26570-1-git-send-email-kandoiruchi@google.com> X-Original-Sender: linux-kernel-owner@vger.kernel.org Xref: csiph.com linux.kernel:1499296 This patchstack introduces a new "memtrack" module for tracking and accounting memory exported to userspace as shared buffers, like dma-buf fds or GEM handles. Any process holding a reference to these buffers will keep the kernel from reclaiming its backing pages. mm counters don't provide a complete picture of these allocations, since they only account for pages that are mapped into a process's address space. This problem is especially bad for systems like Android that use dma-buf fds to share graphics and multimedia buffers between processes: these allocations are often large, have complex sharing patterns, and are rarely mapped into every process that holds a reference to them. memtrack maintains a per-process list of shared buffer references, which is exported to userspace as /proc/[pid]/memtrack. Buffers can be optionally "tagged" with a short string: for example, Android userspace would use this tag to identify whether buffers were allocated on behalf of the camera stack, GL, etc. memtrack also exports the VMAs associated with these buffers so that pages already included in the process's mm counters aren't double-counted. Shared-buffer allocators can hook into memtrack by embedding struct memtrack_buffer in their buffer metadata, calling memtrack_buffer_{init,remove} at buffer allocation and free time, and memtrack_buffer_{install,uninstall} when a userspace process takes or drops a reference to the buffer. For fd-backed buffers like dma-bufs, hooks in fdtable.c and fork.c automatically notify memtrack when references are added or removed from a process's fd table. This patchstack adds memtrack hooks into dma-buf and ion. If there's upstream interest in memtrack, it can be extended to other memory allocators as well, such as GEM implementations. Greg Hackmann (1): drivers: staging: ion: add ION_IOC_TAG ioctl Ruchi Kandoi (5): fs: add installed and uninstalled file_operations drivers: misc: add memtrack dma-buf: add memtrack support memtrack: Adds the accounting to keep track of all mmaped/unmapped pages. memtrack: Add memtrack accounting for forked processes. drivers/android/binder.c | 4 +- drivers/dma-buf/dma-buf.c | 37 +++ drivers/misc/Kconfig | 16 + drivers/misc/Makefile | 1 + drivers/misc/memtrack.c | 516 ++++++++++++++++++++++++++++++++ drivers/staging/android/ion/ion-ioctl.c | 17 ++ drivers/staging/android/ion/ion.c | 60 +++- drivers/staging/android/ion/ion_priv.h | 2 + drivers/staging/android/uapi/ion.h | 25 ++ fs/file.c | 38 ++- fs/open.c | 2 +- fs/proc/base.c | 4 + include/linux/dma-buf.h | 5 + include/linux/fdtable.h | 4 +- include/linux/fs.h | 2 + include/linux/memtrack.h | 130 ++++++++ include/linux/mm.h | 3 + include/linux/sched.h | 3 + kernel/fork.c | 23 +- 19 files changed, 875 insertions(+), 17 deletions(-) create mode 100644 drivers/misc/memtrack.c create mode 100644 include/linux/memtrack.h -- 2.8.0.rc3.226.g39d4020