Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Raag Jadav <raag.jadav@intel.com>
To: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: intel-xe@lists.freedesktop.org, Rodrigo Vivi <rodrigo.vivi@intel.com>
Subject: Re: [PATCH v4] drm/xe/gt_throttle: Avoid TOCTOU when monitoring reasons
Date: Mon, 3 Nov 2025 17:22:55 +0100	[thread overview]
Message-ID: <aQjW374EgQZH1_KZ@black.igk.intel.com> (raw)
In-Reply-To: <u5iaqtgrobvvzal4t7mjnspuacswc6ul5etk23toitrr7e4wdd@zkwdz2kvsfxf>

On Mon, Nov 03, 2025 at 09:20:40AM -0600, Lucas De Marchi wrote:
> On Sat, Nov 01, 2025 at 07:38:00AM +0100, Raag Jadav wrote:
> > On Fri, Oct 31, 2025 at 09:47:25AM -0700, Lucas De Marchi wrote:
> > > It's currently not possible to safely monitor if there's throttling
> > > happening and what are the reasons. The approach of reading the status
> > > and then reading the reasons is not reliable as by the time sysadmin
> > > reads the reason, the throttling could not be happening anymore.
> > > 
> > > Previous tentative to fix that[1] was breaking the ABI and potentially
> > > sysadmin's scripts. This takes a different approach of adding and
> > > documenting the additional attribute. It's still valuable, though
> > > redundant, to provide the simpler 0/1 interface.
> > > 
> > > In order to avoid userspace knowledge on the bitmask meaning and to be
> > > able to maintain the kernel side in sync with possible changes in
> > > future, just walk the attribute group and check what are the masks that
> > > match the value read.
> > > 
> > > [1] https://lore.kernel.org/intel-xe/20241025092238.167042-1-raag.jadav@intel.com/
> > 
> > ...
> > 
> > > +static const struct attribute_group *get_platform_throttle_group(struct xe_device *xe);
> > > +
> > > +static ssize_t reasons_show(struct kobject *kobj,
> > > +			    struct kobj_attribute *attr, char *buff)
> > > +{
> > > +	struct xe_gt *gt = throttle_to_gt(kobj);
> > > +	struct xe_device *xe = gt_to_xe(gt);
> > > +	const struct attribute_group *group;
> > > +	struct attribute **pother;
> > > +	ssize_t ret = 0;
> > > +	u32 reasons;
> > > +
> > > +	reasons = xe_gt_throttle_get_limit_reasons(gt);
> > > +	group = get_platform_throttle_group(xe);
> > > +
> > > +	for (pother = group->attrs; *pother; pother++) {
> > 
> > Shouldn't this entire loop be inside if (reasons), or did I miss something?
> 
> it could be an optimization, but should work regardless.

Yep, upto you.

> > > +		struct kobj_attribute *kattr = container_of(*pother, struct kobj_attribute, attr);
> > > +		struct throttle_attribute *other_ta = kobj_attribute_to_throttle(kattr);
> > > +
> > > +		if (other_ta->mask != U32_MAX && reasons & other_ta->mask)
> > 
> > With above in place this will be much simpler.
> 
> why would it be simpler? we would still skip the status attribute
> (conviniently defined with mask == U32_MAX the status attribute) and
> still do a & with mask

Hm, I had a misconception that we've indexed the attributes as per their
mask value. With that perhaps for_each_set_bit() would've been a great
alternative.

> > > +			ret += sysfs_emit_at(buff, ret, "%s ", (*pother)->name);
> > > +	}
> > > +
> > > +	if (ret)
> > > +		/* Drop extra space from last iteration above */
> > > +		ret--;
> > > +	else
> > > +		ret += sysfs_emit_at(buff, ret, "none");
> > 
> > I find the lack of '+' a bit more readable as it guarantees that nothing
> > else is inherited here, or perhaps it's my needless paranoia at work :(
> > Upto you.
> 
> Keeping the pattern is good IMO. If we'd change then it would be for a
> sysfs_emit().
> 
> 	if (!ret)
> 		return sysfs_emit(buff, "none\n");
> 
> 	ret--;
> 	ret += sysfs_emit_at(buff, ret, "\n");
> 
> 	return ret;

Either way works for me :)

Reviewed-by: Raag Jadav <raag.jadav@intel.com>

  reply	other threads:[~2025-11-03 16:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-31 16:47 [PATCH v4] drm/xe/gt_throttle: Avoid TOCTOU when monitoring reasons Lucas De Marchi
2025-10-31 16:52 ` ✗ CI.checkpatch: warning for " Patchwork
2025-10-31 16:54 ` ✓ CI.KUnit: success " Patchwork
2025-10-31 17:55 ` ✓ Xe.CI.BAT: " Patchwork
2025-11-01  6:08 ` ✓ Xe.CI.Full: " Patchwork
2025-11-01  6:38 ` [PATCH v4] " Raag Jadav
2025-11-03 15:20   ` Lucas De Marchi
2025-11-03 16:22     ` Raag Jadav [this message]
2025-11-04 22:19       ` Lucas De Marchi

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=aQjW374EgQZH1_KZ@black.igk.intel.com \
    --to=raag.jadav@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=lucas.demarchi@intel.com \
    --cc=rodrigo.vivi@intel.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