Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Albert Esteve" <aesteve@redhat.com>, "Tejun Heo" <tj@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutný" <mkoutny@suse.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Benjamin Gaignard" <benjamin.gaignard@collabora.com>,
	"Brian Starkey" <Brian.Starkey@arm.com>,
	"John Stultz" <jstultz@google.com>,
	"T.J. Mercier" <tjmercier@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Ondrej Mosnáček" <omosnacek@gmail.com>,
	"Jens Wiklander" <jenswi@kernel.org>,
	"Sumit Garg" <sumit.garg@kernel.org>,
	"Philipp Stanner" <phasta@kernel.org>
Cc: airlied@gmail.com, dri-devel@lists.freedesktop.org,
	linux-mm@kvack.org, linux-security-module@vger.kernel.org,
	selinux@vger.kernel.org, op-tee@lists.trustedfirmware.org,
	echanude@redhat.com, mripard@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v5 2/8] dma-heap: charge dma-buf memory via explicit memcg
Date: Wed, 18 Nov 2026 08:40:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-220468@kernel.org> (raw)
In-Reply-To: <20261117-v2_20230123_tjmercier_google_com-v5-2-1ce975f2a9ad@redhat.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: memcontrol: task_css() race with migration in
  get_obj_cgroup_from_fd()
--

commit 7e6b5f51c2c9a154a8386e2fbe69f7c3fe1c4a27
Author: Albert Esteve <aesteve@redhat.com>

dma-heap: charge dma-buf memory via explicit memcg

This commit adds a charge_fd field to struct dma_heap_allocation_data so
DMA_HEAP_IOCTL_ALLOC can resolve a target cgroup from a pidfd or cgroupfd
and charge system-heap dma-buf allocations to that cgroup's obj_cgroup.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 45e82cda7c441..adc0abcf80fae 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -3168,6 +3169,88 @@ struct obj_cgroup *get_obj_cgroup_from_folio(const struct folio *folio)
[ ... ]
> +struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
> +{
[ ... ]
> +	if (IS_ERR(css)) {
> +		if (PTR_ERR(css) != -EBADF)
> +			return ERR_CAST(css);
> +
> +		/* fd is a pidfd */
> +		task = pidfd_get_task(fd, &pidfd_flags);
> +		if (IS_ERR(task))
> +			return ERR_CAST(task);
> +
> +		rcu_read_lock();
> +		css = task_css(task, memory_cgrp_id);
> +		put_task_struct(task);
> +		if (!css || !css_tryget_online(css)) {
> +			rcu_read_unlock();\n> +			return ERR_PTR(-ENOENT);
> +		}
> +		rcu_read_unlock();
> +	}

[Severity: Low]
Can this one-shot task_css() and css_tryget_online() lookup spuriously fail
with -ENOENT?

CPU0                                  CPU1
get_obj_cgroup_from_fd()
  rcu_read_lock()
  css = task_css(task, ...)
                                      cgroup_migrate_execute()
                                        css_set_move_task(task, ...)
                                      cgroup_destroy_locked()
                                        kill_css_finish()
  css_tryget_online(css)  <-- false
  return ERR_PTR(-ENOENT)

Also, pidfd_get_task() on a process pidfd returns the thread-group leader;
if that leader has exited (PF_EXITING) in a multi-threaded process whose
remaining threads migrated and whose old cgroup was offlined, the leader
stays associated with the offline css even though get_obj_cgroup_from_memcg()
can walk up to its online ancestor.  Using task_get_css(task, memory_cgrp_id)
before put_task_struct(task) avoids both issues:

include/linux/cgroup.h:task_get_css() {
	rcu_read_lock();
	while (true) {
		css = task_css(task, subsys_id);
		/*
		 * Can't use css_tryget_online() here.  A task which has
		 * PF_EXITING set may stay associated with an offline css.
		 * If such task calls this function, css_tryget_online()
		 * will keep failing.
		 */
		if (likely(css_tryget(css)))
			break;
		cpu_relax();
	}
	rcu_read_unlock();
	return css;
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-v2_20230123_tjmercier_google_com-v5-0-1ce975f2a9ad@redhat.com?part=2

  reply	other threads:[~2026-11-18  8:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  8:39 [PATCH v5 0/8] memcg: dma-buf cgroup accounting and charge transfer Albert Esteve
2026-11-17  8:39 ` [PATCH v5 1/8] memcg: Add MEMCG_DMABUF accounting infrastructure Albert Esteve
2026-11-18  8:40   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 2/8] dma-heap: charge dma-buf memory via explicit memcg Albert Esteve
2026-11-18  8:40   ` sashiko-bot [this message]
2026-11-17  8:39 ` [PATCH v5 3/8] dma-buf: transfer objcg charges via DMA_BUF_IOCTL_XFER_CHARGE Albert Esteve
2026-11-18  8:41   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 4/8] lsm: Add memcg_charge_task LSM hook Albert Esteve
2026-11-17 22:54   ` Paul Moore
2026-11-18  8:41   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 5/8] lsm: Add memcg_charge_cgroup " Albert Esteve
2026-11-18  8:41   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 6/8] selinux: Restrict cross-cgroup memcg charging Albert Esteve
2026-11-18  8:41   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 7/8] selinux: Restrict memcg charging to a cgroupfd target Albert Esteve
2026-11-18  8:41   ` sashiko-bot
2026-11-17  8:39 ` [PATCH v5 8/8] selftests/dmabuf-heaps: Add dma-buf memcg accounting tests Albert Esteve
2026-11-18  8:41   ` sashiko-bot
2026-11-17 22:54 ` [PATCH v5 0/8] memcg: dma-buf cgroup accounting and charge transfer Paul Moore

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-220468@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Brian.Starkey@arm.com \
    --cc=aesteve@redhat.com \
    --cc=airlied@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=benjamin.gaignard@collabora.com \
    --cc=brauner@kernel.org \
    --cc=christian.koenig@amd.com \
    --cc=corbet@lwn.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=echanude@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=jenswi@kernel.org \
    --cc=jmorris@namei.org \
    --cc=jstultz@google.com \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=mripard@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=omosnacek@gmail.com \
    --cc=op-tee@lists.trustedfirmware.org \
    --cc=paul@paul-moore.com \
    --cc=phasta@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=selinux@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=stephen.smalley.work@gmail.com \
    --cc=sumit.garg@kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox