From patchwork Mon Jan 9 21:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "T.J. Mercier" X-Patchwork-Id: 13094385 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8D06C5479D for ; Mon, 9 Jan 2023 21:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 559538E0001; Mon, 9 Jan 2023 16:38:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 50AD2900002; Mon, 9 Jan 2023 16:38:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AC578E0006; Mon, 9 Jan 2023 16:38:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2D13B8E0001 for ; Mon, 9 Jan 2023 16:38:27 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B88774106E for ; Mon, 9 Jan 2023 21:38:26 +0000 (UTC) X-FDA: 80336574612.17.8FB4B62 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf02.hostedemail.com (Postfix) with ESMTP id 27D078000A for ; Mon, 9 Jan 2023 21:38:25 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MIKhBBBn; spf=pass (imf02.hostedemail.com: domain of 3UIm8YwkKCHkqgjboZfbodlldib.Zljifkru-jjhsXZh.lod@flex--tjmercier.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3UIm8YwkKCHkqgjboZfbodlldib.Zljifkru-jjhsXZh.lod@flex--tjmercier.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673300305; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZzZnon9AuhDOmqXXwy+4pPV98gk0DXyzNf2lwwH3FVc=; b=5yaQ7PEIXRuIteuRBLLRvzY1JHkcBq9zPEPpioEjJ0DvdaxcKjnMHvoV5I58Bu/moI7dv/ 8DIkuEd8T1LtYelfhxLYg5HcvB2o6ObvMl6Wm+hsPR5eYFvOpiNbq/f6hcmoSebtRGuae7 kvFJrCOspM0U8eMlJxVqtKbw2ueYXGo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=MIKhBBBn; spf=pass (imf02.hostedemail.com: domain of 3UIm8YwkKCHkqgjboZfbodlldib.Zljifkru-jjhsXZh.lod@flex--tjmercier.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3UIm8YwkKCHkqgjboZfbodlldib.Zljifkru-jjhsXZh.lod@flex--tjmercier.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673300305; a=rsa-sha256; cv=none; b=jfhB7/0Ykt10Bdgl1l3PJJg05gm0Q5002E9Qhe5FQKhH0LIYAkSwe3hj0PjeXBAB0ZUCRh KBdV54p15OIgW609fHRlCb1UQ2a5+aAAwel2NO4POFfpHdd3SY5hUSIvDZwjL4HanCzXK2 Pe+ZJhgb98Twu/R0KH0X74cRVRG39OQ= Received: by mail-yb1-f202.google.com with SMTP id r8-20020a252b08000000b007b989d5e105so7055720ybr.11 for ; Mon, 09 Jan 2023 13:38:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZzZnon9AuhDOmqXXwy+4pPV98gk0DXyzNf2lwwH3FVc=; b=MIKhBBBnQ+bqQnJww8/fPsZhi0whxCGIUG6kEGUGMp4FOf1NaJ2uHO5gbQnelxNiQA /zZow36TcojzDwPLoUOvG98yay8W+H/pD2HMrI1l93vbQq7yL0EPGLWZQ0R6+VY7DRtV JNWPRrorFPERhG5pCoV73P8K4BeUX6WEsXjfyFLgqrSJKvyOp3o0siNMo696W/r6yKXU y8RJ2vfZCTDjh6Oi2w1rHjBj4wQlmL0t+ZRPOFSxOTrZS7vGovErpCiunYz6Rey8BJDi cncB+zuYtfJ9rQv3FhWwefPz3sWwVINattaPynPAVTeC+nbR6FJw+i6h1qaddB0J7iUL VYgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZzZnon9AuhDOmqXXwy+4pPV98gk0DXyzNf2lwwH3FVc=; b=wnkwryb5+xIbitYjuLCKB9+4Ou23/1SJFQTaM6w/Wo8pPkGlM9YvXyprRbmQqoSpxP FkNVzfrRuGF6lHS6SM7gPy/mcGk/pPRh007ykiLlGNVNWpm6MYOS+qdVap9KUtxvXUKb jXtKwsDzvY/ndoKK9Jiq4RZb4p+LMHpggBD+r7fworL1nC+msBwFEThKXlArrgLllAbZ lNIQkKV3m7C+y3k7xHN8lx8itzpxaRKPuBu3jrQG5KW0QrcWov1J0dFitGMp7IHd0BvF qDICvvUT2ebuXu++6gMl3gfVZIZPemoedxQGlt8i5b+ctviitV/ksu8yzag8jxZuplZe J0hQ== X-Gm-Message-State: AFqh2kooj2E0At7Kpf3+IMVai09Y6MZcuGs5lrdLZ635g2qWonqA3Pbp eLjh1FZLVC5iz43znTtNI/5499NQijica5M= X-Google-Smtp-Source: AMrXdXtOrlHW8ygC4EeYcDKs1WQIoeE3W+TEcqNZ/tFdoHO00Cnd/PEn3XI5TMhGx2JhnY0yguaIzWNDLrFIktg= X-Received: from tj.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:53a]) (user=tjmercier job=sendgmr) by 2002:a81:6784:0:b0:460:c029:6c76 with SMTP id b126-20020a816784000000b00460c0296c76mr2274300ywc.515.1673300304313; Mon, 09 Jan 2023 13:38:24 -0800 (PST) Date: Mon, 9 Jan 2023 21:38:04 +0000 In-Reply-To: <20230109213809.418135-1-tjmercier@google.com> Mime-Version: 1.0 References: <20230109213809.418135-1-tjmercier@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230109213809.418135-2-tjmercier@google.com> Subject: [PATCH 1/4] memcg: Track exported dma-buffers From: "T.J. Mercier" To: tjmercier@google.com, Tejun Heo , Zefan Li , Johannes Weiner , Jonathan Corbet , Sumit Semwal , " =?utf-8?q?Christian_K=C3=B6nig?= " , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton Cc: daniel.vetter@ffwll.ch, android-mm@google.com, jstultz@google.com, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org X-Rspamd-Queue-Id: 27D078000A X-Stat-Signature: 913uxhyfmmwhxmcffc9nopx456zcmsc8 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1673300305-144787 X-HE-Meta: U2FsdGVkX1+zi7YhBMVqBxrlk1eetWpUZU/XHySQqFFek8k9AaK2aVa5bEfS+30QXKn5qZDwzgUyLmOaZncHakImFK4K95xsSvAo9oBOPsNjn3BBm4h2GMjbo9ynKzOhfUHR4f5dElvEl9mHAO48KqSNNlE6JKhKM35YbJAilWeVoSPBSOZ3SF14spTvrLpOB8RQSq7ECL6j5yK78GMNsusjB0H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XP5+HLkf4Avu/CxLNJA/1p8tGP4zwxRFILwkIwaGCHSmi0N7pZmq6OeERAQlnKOPeSjCus5PvGifP2ajniSnKugPCpXOS5jtTPm4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When a buffer is exported to userspace, use memcg to attribute the buffer to the allocating cgroup until all buffer references are released. Unlike the dmabuf sysfs stats implementation, this memcg accounting avoids contention over the kernfs_rwsem incurred when creating or removing nodes. Signed-off-by: T.J. Mercier --- Documentation/admin-guide/cgroup-v2.rst | 4 ++++ drivers/dma-buf/dma-buf.c | 5 +++++ include/linux/dma-buf.h | 3 +++ include/linux/memcontrol.h | 1 + mm/memcontrol.c | 4 ++++ 5 files changed, 17 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index c8ae7c897f14..538ae22bc514 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1455,6 +1455,10 @@ PAGE_SIZE multiple when read back. Amount of memory used for storing in-kernel data structures. + dmabuf (npn) + Amount of memory used for exported DMA buffers allocated by the cgroup. + Stays with the allocating cgroup regardless of how the buffer is shared. + workingset_refault_anon Number of refaults of previously evicted anonymous pages. diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index e6528767efc7..ac45dd101c4d 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -75,6 +75,8 @@ static void dma_buf_release(struct dentry *dentry) */ BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active); + mod_memcg_state(dmabuf->memcg, MEMCG_DMABUF, -dmabuf->size); + mem_cgroup_put(dmabuf->memcg); dma_buf_stats_teardown(dmabuf); dmabuf->ops->release(dmabuf); @@ -673,6 +675,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) if (ret) goto err_dmabuf; + dmabuf->memcg = get_mem_cgroup_from_mm(current->mm); + mod_memcg_state(dmabuf->memcg, MEMCG_DMABUF, dmabuf->size); + file->private_data = dmabuf; file->f_path.dentry->d_fsdata = dmabuf; dmabuf->file = file; diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 6fa8d4e29719..1f0ffb8e4bf5 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -22,6 +22,7 @@ #include #include #include +#include struct device; struct dma_buf; @@ -446,6 +447,8 @@ struct dma_buf { struct dma_buf *dmabuf; } *sysfs_entry; #endif + /* The cgroup to which this buffer is currently attributed */ + struct mem_cgroup *memcg; }; /** diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d3c8203cab6c..1c1da2da20a6 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -37,6 +37,7 @@ enum memcg_stat_item { MEMCG_KMEM, MEMCG_ZSWAP_B, MEMCG_ZSWAPPED, + MEMCG_DMABUF, MEMCG_NR_STAT, }; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ab457f0394ab..680189bec7e0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1502,6 +1502,7 @@ static const struct memory_stat memory_stats[] = { { "unevictable", NR_UNEVICTABLE }, { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, + { "dmabuf", MEMCG_DMABUF }, /* The memory events */ { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, @@ -1519,6 +1520,7 @@ static int memcg_page_state_unit(int item) switch (item) { case MEMCG_PERCPU_B: case MEMCG_ZSWAP_B: + case MEMCG_DMABUF: case NR_SLAB_RECLAIMABLE_B: case NR_SLAB_UNRECLAIMABLE_B: case WORKINGSET_REFAULT_ANON: @@ -4042,6 +4044,7 @@ static const unsigned int memcg1_stats[] = { WORKINGSET_REFAULT_ANON, WORKINGSET_REFAULT_FILE, MEMCG_SWAP, + MEMCG_DMABUF, }; static const char *const memcg1_stat_names[] = { @@ -4057,6 +4060,7 @@ static const char *const memcg1_stat_names[] = { "workingset_refault_anon", "workingset_refault_file", "swap", + "dmabuf", }; /* Universal VM events cgroup1 shows, original sort order */