mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Jinseob Kim <kimjinseob88@gmail.com>
Cc: linux-iio@vger.kernel.org, dlechner@baylibre.com,
	nuno.sa@analog.com, andy@kernel.org,
	linux-kernel@vger.kernel.org, rdunlap@infradead.org,
	joshua.crofts1@gmail.com, u.kleine-koenig@baylibre.com,
	julianbraha@gmail.com, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, grondon@gmail.com,
	devicetree@vger.kernel.org, corbet@lwn.net,
	skhan@linuxfoundation.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH v8 4/5] iio: osf: add authenticated stream parser
Date: Fri, 21 Aug 2026 03:47:48 +0100	[thread overview]
Message-ID: <20260821034748.5049aca0@jic23-huawei> (raw)
In-Reply-To: <20260820050608.5440-5-kimjinseob88@gmail.com>

On Thu, 20 Aug 2026 14:06:07 +0900
Jinseob Kim <kimjinseob88@gmail.com> wrote:

> Add a UART byte-stream parser for Open Sensor Fusion frames.
> 
> The parser searches for the OSF0 wire magic, keeps partial frames
> buffered, checks header length and payload bounds, and passes complete
> candidate frames to a registered frame callback.
> 
> Candidates rejected before authentication drop only the current head
> byte before resynchronizing, so a corrupted unauthenticated payload length
> cannot make the parser skip later valid frames. CRC-valid authenticated
> frames are consumed in full and classified as handled, ignored, or
> rejected.
> 
> Use a direct callback member with an opaque context and keep explicit
> statistics for authenticated outcomes and framing failures.
> 
> Signed-off-by: Jinseob Kim <kimjinseob88@gmail.com>

One really trivial thing inline.  Please check to see if other areas
of code alignment would benefit from a bit of a reorg.

> diff --git a/drivers/iio/opensensorfusion/osf_stream.c b/drivers/iio/opensensorfusion/osf_stream.c
> new file mode 100644
> index 000000000000..7ce45e631648
> --- /dev/null
> +++ b/drivers/iio/opensensorfusion/osf_stream.c
...

> +
> +static int osf_stream_process(struct osf_stream *stream)
> +{
> +	size_t discarded;
> +	size_t frame_len;
> +	u32 payload_len;
> +	int frame_result;
> +	int first_err = 0;
> +
> +	while (stream->len) {
> +		discarded = osf_stream_discard_to_magic(stream);
> +		if (discarded) {
> +			stream->stats.bad_magic_resyncs++;
> +			stream->stats.dropped_bytes += discarded;
> +			if (!first_err)
> +				first_err = -EPROTO;
> +		}
> +
> +		if (!stream->len)
> +			break;
> +
> +		if (stream->len < OSF_FRAME_HEADER_LEN)
> +			break;
> +
> +		if (get_unaligned_le16(stream->buf + 6) != OSF_FRAME_HEADER_LEN) {
> +			stream->stats.dropped_bytes++;
> +			osf_stream_drop_invalid_head(stream);
> +			if (!first_err)
> +				first_err = -EPROTO;
> +			continue;
> +		}
> +
> +		payload_len = get_unaligned_le32(stream->buf + 10);
> +		if (payload_len > OSF_STREAM_MAX_PAYLOAD_LEN) {
> +			stream->stats.dropped_bytes++;
> +			osf_stream_drop_invalid_head(stream);
> +			if (!first_err)
> +				first_err = -EMSGSIZE;
> +			continue;
> +		}
> +
> +		frame_len = OSF_FRAME_HEADER_LEN + payload_len + OSF_FRAME_CRC_LEN;
> +		if (stream->len < frame_len)
> +			break;
> +
> +		frame_result = stream->receive_frame(stream->frame_context,
> +					     stream->buf, frame_len);

Align after (

> +		if (frame_result < 0) {
> +			if (frame_result == -EBADMSG)
> +				stream->stats.bad_crc_frames++;
> +
> +			/*
> +			 * Decoding failed before the frame was authenticated;
> +			 * payload_len is still untrusted. Drop only the current
> +			 * head and resynchronize.
> +			 */
> +			stream->stats.dropped_bytes++;
> +			osf_stream_drop_invalid_head(stream);
> +			if (!first_err)
> +				first_err = frame_result;
> +			continue;
> +		}
> +
> +		/* Count exactly one outcome for every authenticated frame. */
> +		stream->stats.authenticated_frames++;
> +		switch (frame_result) {
> +		case OSF_STREAM_FRAME_HANDLED:
> +			stream->stats.handled_frames++;
> +			break;
> +		case OSF_STREAM_FRAME_IGNORED:
> +			stream->stats.ignored_frames++;
> +			break;
> +		case OSF_STREAM_FRAME_REJECTED:
> +			stream->stats.rejected_frames++;
> +			break;
> +		default:
> +			/*
> +			 * Preserve the authenticated boundary without scanning the
> +			 * payload for another magic value.
> +			 */
> +			stream->stats.rejected_frames++;
> +			if (!first_err)
> +				first_err = -EPROTO;
> +			break;
> +		}
> +		osf_stream_discard(stream, frame_len);
> +	}
> +
> +	return first_err;
> +}

  reply	other threads:[~2026-08-21  2:47 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-20  5:06 [PATCH v8 0/5] iio: add Open Sensor Fusion IIO driver Jinseob Kim
2026-08-20  5:06 ` [PATCH v8 1/5] dt-bindings: iio: add Open Sensor Fusion device Jinseob Kim
2026-08-20  5:06 ` [PATCH v8 2/5] Documentation: iio: add Open Sensor Fusion driver overview Jinseob Kim
2026-08-20  5:06 ` [PATCH v8 3/5] iio: osf: add protocol decoding Jinseob Kim
2026-08-20  5:06 ` [PATCH v8 4/5] iio: osf: add authenticated stream parser Jinseob Kim
2026-08-21  2:47   ` Jonathan Cameron [this message]
2026-08-20  5:06 ` [PATCH v8 5/5] iio: osf: add UART IIO driver Jinseob Kim
2026-08-21  3:03   ` Jonathan Cameron
2026-08-21  2:33 ` [PATCH v8 0/5] iio: add Open Sensor Fusion " Jonathan Cameron

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=20260821034748.5049aca0@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=grondon@gmail.com \
    --cc=joshua.crofts1@gmail.com \
    --cc=julianbraha@gmail.com \
    --cc=kimjinseob88@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=rdunlap@infradead.org \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=u.kleine-koenig@baylibre.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

all inboxes | Powered by JetHome®