Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5864840ioo; Wed, 1 Jun 2022 14:25:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRdSbsioK5/jouA3p7h0dpm7mCArrQL+gOJ236GNF3EhXE/qRiRkPu5i2S9BC2uyWR5QlL X-Received: by 2002:a17:903:240a:b0:14e:dad4:5ce4 with SMTP id e10-20020a170903240a00b0014edad45ce4mr1448847plo.125.1654118717016; Wed, 01 Jun 2022 14:25:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654118717; cv=none; d=google.com; s=arc-20160816; b=O5dBGv3km2kgKBWva20ayaJVGubJNzn1A6rYoGzziyVPPKx0Hq1IsHz8tYKsfHBzdD auw+epID6fv09ZdK/HuvoyqZvxFv7IdqQBWMs6b7fQnE5kJoSvBAMtGHWWQUVAHHC2nN GOgxJEiGv0MdPZeBcX61nE5zUt9zMTYnpkjK/0JkNd/p3O/5LK8TKHsCtyppPKKUIIMt iOmqXzQK49Jb8XHRrqOJr/6nPMCgDkgf1OsFTmTntq1hw4gNaEMu5wOSa0aWeoK3fPHP m02aaQ36Y+evhrLOoLUwLNbEg8WCR/ZWIB4SjSwrU4+OZBA0Paoh/Mj8g9A1UIu1qGXI JQKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:cc:from:subject:mime-version:message-id:date :dkim-signature; bh=L5daPTOP8huWXGF9zZZ/YOQgC5+YhUKYKEYEGgJKuXI=; b=wAiuCm7Co7jwP0UyqCNMBOruhdSklZTBmDfNnMyiaYAZDCoKtmsTaCcR9eJa/eYL/p uEdYB5SlX1xgP66JFvKk8wf+dB16Lk8S++DQcm2Pskhf0Hq6pXP8LcIs+TUAeOLQo3zX ZLZohFItIk5KQnVhldinnENJch7P5q/Z1OmjnXX7Hl+QtDri+05lI0qJUW5MDXr96bo2 zhCtLspyiIGJkLuBzqJOIzXopOdL6F3XPcUJ+88dOjOOX02eGEKQ5y5DUlYhFlKU8ld8 jfGLPO3udJlrwKE+MejS/BHKkTSyKTGcdpCjeyntXOjBkTEl28KOhGGI/PZNnjHNWT6d 9LuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ni+vDj4J; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j3-20020a170903028300b0015d56a3c9d0si4205588plr.534.2022.06.01.14.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 14:25:17 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ni+vDj4J; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 43A261AA15F; Wed, 1 Jun 2022 13:15:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348000AbiEaVZg (ORCPT + 99 others); Tue, 31 May 2022 17:25:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348015AbiEaVZc (ORCPT ); Tue, 31 May 2022 17:25:32 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87CC79E9E5 for ; Tue, 31 May 2022 14:25:28 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e191-20020a25e7c8000000b0065b8b75cbfaso9711123ybh.3 for ; Tue, 31 May 2022 14:25:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:cc; bh=L5daPTOP8huWXGF9zZZ/YOQgC5+YhUKYKEYEGgJKuXI=; b=ni+vDj4JsvkDI6uQO/dBS970cz6dZc8DyFe2Zer4xTkNxkSHvK+9iWBnPWmPFlu+yM GldK8tXmwDcVnPqD5rTmrw1O/5Fx84SqlvyYy1O+NdSXpTGCmxmOP42PbCZEsmapK/pI LdPWt3eWdMCmmsdUAyhxzJwSk4K/f3XFD8JLMQDfFMKoV7yl3wD7c0YEyQZkVTKpKRls bQIXSiUx6bvVEbfIXw2C31CtGBGyRZKnhhZI0oqDXke/z5Cd5Z5SAktEbZN0415/W9kX oeGofIRzd3K6V4+k9c5ZaCl6y2CNVM4tUzcRZzthBuZX0q0DdV2YxdbJ4DLgGVowVllW //UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:cc; bh=L5daPTOP8huWXGF9zZZ/YOQgC5+YhUKYKEYEGgJKuXI=; b=DP/qzsRTgH5d1i8yXZOOHz7t7ahLVj0BXbpRakrevV3cJ2ptSqLBJomG4T5p6CklhR OAgzHEC1XTV8USkibes74eAUuNZIwLN3l3A5nziUUNHxasIiI3AvgxNoDD2QcZ5BgQRl i4ZUWERfsMUHuvQAiDmjjruS8HdQkkqaKA7Op8+pHTE38EfteT6+UiDfLHkldmvRNzrw R/KXkPGIKH8VyN+z4/oWeRdBe0H7CA7HsYfG1QdbQDfF/23st1/3LF9E0v15zvNRMuju yEG7IJRqIgIwZ89gVKRdaqlxWUN2SsQOhep4410JXvxB/5ZDw7JAYS+BgnwDFZu770DF K/cA== X-Gm-Message-State: AOAM5301dx6FxoqPIfohlOGQUJieSQffw7TqpZwpYtbfl7LUqHCxDlEr XJ0FqOZzY9WNA2BiCg0ukOJJrQAhWkCBqxcS6w== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:a3c0:2a66:b25c:16df]) (user=kaleshsingh job=sendgmr) by 2002:a25:6588:0:b0:65d:57b9:c470 with SMTP id z130-20020a256588000000b0065d57b9c470mr4071204ybb.142.1654032327734; Tue, 31 May 2022 14:25:27 -0700 (PDT) Date: Tue, 31 May 2022 14:25:13 -0700 Message-Id: <20220531212521.1231133-1-kaleshsingh@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH 0/2] procfs: Add file path and size to /proc//fdinfo From: Kalesh Singh Cc: ilkos@google.com, tjmercier@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , Jonathan Corbet , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Andrew Morton , Johannes Weiner , David Hildenbrand , Christoph Anton Mitterer , Mike Rapoport , Paul Gortmaker , Randy Dunlap , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Processes can pin shared memory by keeping a handle to it through a file descriptor; for instance dmabufs, memfd, and ashmem (in Android). In the case of a memory leak, to identify the process pinning the memory, userspace needs to: - Iterate the /proc//fd/* for each process - Do a readlink on each entry to identify the type of memory from the file path. - stat() each entry to get the size of the memory. The file permissions on /proc//fd/* only allows for the owner or root to perform the operations above; and so is not suitable for capturing the system-wide state in a production environment. This issue was addressed for dmabufs by making /proc/*/fdinfo/* accessible to a process with PTRACE_MODE_READ_FSCREDS credentials[1] To allow the same kind of tracking for other types of shared memory, add the following fields to /proc//fdinfo/: path - This allows identifying the type of memory based on common prefixes: e.g. "/memfd...", "/dmabuf...", "/dev/ashmem..." This was not an issued when dmabuf tracking was introduced because the exp_name field of dmabuf fdinfo could be used to distinguish dmabuf fds from other types. size - To track the amount of memory that is being pinned. dmabufs expose size as an additional field in fdinfo. Remove this and make it a common field for all fds. Access to /proc//fdinfo is governed by PTRACE_MODE_READ_FSCREDS -- the same as for /proc//maps which also exposes the path and size for mapped memory regions. This allows for a system process with PTRACE_MODE_READ_FSCREDS to account the pinned per-process memory via fdinfo. ----- There was some concern about exposing the file path in the RFC[2], to that effect the change was split into separte patches. Also retrieving the file path from fdinfo is guarded by the same capability (PTRACE_MODE_READ) as /proc//maps which also exposes file path, so this may not be an issue. [1] https://lore.kernel.org/r/20210308170651.919148-1-kaleshsingh@google.com/ [2] https://lore.kernel.org/r/20220519214021.3572840-1-kaleshsingh@google.com/ Kalesh Singh (2): procfs: Add 'size' to /proc//fdinfo/ procfs: Add 'path' to /proc//fdinfo/ Documentation/filesystems/proc.rst | 22 ++++++++++++++++++++-- drivers/dma-buf/dma-buf.c | 1 - fs/proc/fd.c | 13 +++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) base-commit: 8ab2afa23bd197df47819a87f0265c0ac95c5b6a -- 2.36.1.255.ge46751e96f-goog