Keyrings Archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: keyrings@vger.kernel.org,
	Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>,
	Peter Huewe <peterhuewe@gmx.de>, Jason Gunthorpe <jgg@ziepe.ca>,
	James Bottomley <James.Bottomley@hansenpartnership.com>,
	Mimi Zohar <zohar@linux.ibm.com>,
	David Howells <dhowells@redhat.com>,
	Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"open list:TPM DEVICE DRIVER" <linux-integrity@vger.kernel.org>,
	"open list:SECURITY SUBSYSTEM"
	<linux-security-module@vger.kernel.org>
Subject: Re: [PATCH v2] tpm: Cleanup class for tpm_buf
Date: Sat, 28 Jun 2025 05:14:53 +0300	[thread overview]
Message-ID: <aF9QHdH9k8x1mVjy@kernel.org> (raw)
In-Reply-To: <20250626101935.1007898-1-jarkko@kernel.org>

On Thu, Jun 26, 2025 at 01:19:33PM +0300, Jarkko Sakkinen wrote:
> From: Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>
> 
> Create a cleanup class for struct tpm_buf using DEFINE_CLASS(), which will
> guarantee that the heap allocated memory will be freed automatically for
> the transient instances of this structure, when they go out of scope.
> 
> Wrap this all into help macro CLASS_TPM_BUF().
> 
> A TPM buffer can now be declared trivially:
> 
>     CLASS_TPM_BUF(buf, buf_size);

Right, so learning this while doing and probably DEFINE_CLASS() would be
a bad idea :-) There's a better fit in cleanup.h: DEFINE_FREE() and
__free().

Given thatintroduction of tpm_buf_alloc() wipes out tpm_buf_destroy(),
we don't need to create any new wrappers with DEFINE_FREE() as
linux/slab.h has kfree() covered already.

This leads up to "one step backwards" solution i.e., explicitly call
tpm_buf_alloc() and implictly destroy (null checks are left out from
examples):

	struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();
	/* 
	 * I dropped buf_size as it will be gone in v3 as requested by
	 * James earlier.
	 */

	 /* Or: */
	struct tpm_buf *buf2 __free(kfree) = NULL;

	/* ... */
	buf2 = tpm_buf_alloc();

OFF-TOPIC: while doing this patch I noticed maybe 3-5 location where
we have do this:

1. Init something that is more complex to rollback than rolling back
   tpm_buf (which is kfree).
2. Init tpm_buf.
3. After this guaranteed success.

Only reason for doing the rollback for the "more complex to rollback
thing" is that stupid placement of tpm_buf_init(). There's no additional
conditionally failing steps after it.

I need to relocate these code blocks and do a reorders as split patches
and place them to the head of the patch set.

This was mostly reminder for myself :-) 

BR, Jarkko

      parent reply	other threads:[~2025-06-28  2:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-26 10:19 [PATCH v2] tpm: Cleanup class for tpm_buf Jarkko Sakkinen
2025-06-26 14:50 ` James Bottomley
2025-06-26 18:11   ` Jarkko Sakkinen
2025-06-26 22:35     ` Jarkko Sakkinen
2025-06-28  2:14 ` Jarkko Sakkinen [this message]

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=aF9QHdH9k8x1mVjy@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=dhowells@redhat.com \
    --cc=jarkko.sakkinen@opinsys.com \
    --cc=jgg@ziepe.ca \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=peterhuewe@gmx.de \
    --cc=serge@hallyn.com \
    --cc=zohar@linux.ibm.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).