From mboxrd@z Thu Jan 1 00:00:00 1970 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 7570FC433EF for ; Mon, 20 Jun 2022 04:44:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD2BA6B0071; Mon, 20 Jun 2022 00:44:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D84336B0073; Mon, 20 Jun 2022 00:44:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C49EB8D0001; Mon, 20 Jun 2022 00:44:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B4EDA6B0071 for ; Mon, 20 Jun 2022 00:44:16 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 80F5633A for ; Mon, 20 Jun 2022 04:44:16 +0000 (UTC) X-FDA: 79597372512.27.666EE6A Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf09.hostedemail.com (Postfix) with ESMTP id C9B141400AF for ; Mon, 20 Jun 2022 04:44:15 +0000 (UTC) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-166-e-ku6nOkPGGi772ICmkLdQ-1; Mon, 20 Jun 2022 05:44:13 +0100 X-MC-Unique: e-ku6nOkPGGi772ICmkLdQ-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Mon, 20 Jun 2022 05:44:10 +0100 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.036; Mon, 20 Jun 2022 05:44:10 +0100 From: David Laight To: 'Kent Overstreet' , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , "pmladek@suse.com" CC: "rostedt@goodmis.org" , "enozhatsky@chromium.org" , "linux@rasmusvillemoes.dk" , "willy@infradead.org" Subject: RE: [PATCH v4 01/34] lib/printbuf: New data structure for printing strings Thread-Topic: [PATCH v4 01/34] lib/printbuf: New data structure for printing strings Thread-Index: AQHYhD6p9ckAsWkCSk+0B0i5FtmPu61XsWog Date: Mon, 20 Jun 2022 04:44:10 +0000 Message-ID: References: <20220620004233.3805-1-kent.overstreet@gmail.com> <20220620004233.3805-2-kent.overstreet@gmail.com> In-Reply-To: <20220620004233.3805-2-kent.overstreet@gmail.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf09.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655700256; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yAGGbrhJctsx+4Me6rg+3soNLMSrL/m3BR7DRxTeRAU=; b=knOfQwLtQAEhUGeL2RDiV6TBxRrLCpkdDMLK3Mfu8Jy5OBHUrkwmtrkDp8LNRpg8vph80h VX5tiRTuUCOQ0nUEVt6Ox5MiiAZyEa3wxDJHvHHxJlarBvH8h8bv2qNdIKbL1YFPDERsoG jEAswtdB4AY7kmTXQpbeSLfe5VSeG1k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655700256; a=rsa-sha256; cv=none; b=NYOHEnFp5GORtyc+6gn/lZERfHg7260mqzIVGDyH/nfEMg6yN8AWZm3IIINgd2T1nJj8Ft 0CeRYOIi9FuoMcOwQgSiCWjvRcqBCkI50HOtZyhrlv22BAg0jON/pm4Imc9R+IX7cgahEE y259G2BRdmEIbx0Lwq0G42mC3sgquLM= X-Stat-Signature: 35infa3zrtdggd45ijuot16ayghjheku X-Rspamd-Queue-Id: C9B141400AF X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf09.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com X-Rspamd-Server: rspam10 X-HE-Tag: 1655700255-732421 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: From: Kent Overstreet > Sent: 20 June 2022 01:42 >=20 > This adds printbufs: a printbuf points to a char * buffer and knows the > size of the output buffer as well as the current output position. >=20 > Future patches will be adding more features to printbuf, but initially > printbufs are targeted at refactoring and improving our existing code in > lib/vsprintf.c - so this initial printbuf patch has the features > required for that. >=20 > Signed-off-by: Kent Overstreet > Reviewed-by: Matthew Wilcox (Oracle) > --- > include/linux/printbuf.h | 122 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 122 insertions(+) > create mode 100644 include/linux/printbuf.h >=20 > diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h > new file mode 100644 > index 0000000000..8186c447ca > --- /dev/null > +++ b/include/linux/printbuf.h > @@ -0,0 +1,122 @@ > +/* SPDX-License-Identifier: LGPL-2.1+ */ > +/* Copyright (C) 2022 Kent Overstreet */ > + > +#ifndef _LINUX_PRINTBUF_H > +#define _LINUX_PRINTBUF_H > + > +#include > +#include > + > +/* > + * Printbufs: String buffer for outputting (printing) to, for vsnprintf > + */ > + > +struct printbuf { > +=09char=09=09=09*buf; > +=09unsigned=09=09size; > +=09unsigned=09=09pos; No naked unsigneds. > +}; > + > +/* > + * Returns size remaining of output buffer: > + */ > +static inline unsigned printbuf_remaining_size(struct printbuf *out) > +{ > +=09return out->pos < out->size ? out->size - out->pos : 0; > +} > + > +/* > + * Returns number of characters we can print to the output buffer - i.e. > + * excluding the terminating nul: > + */ > +static inline unsigned printbuf_remaining(struct printbuf *out) > +{ > +=09return out->pos < out->size ? out->size - out->pos - 1 : 0; > +} Those two are so similar mistakes will be make. You can also just return negatives when the buffer has overlowed and get the callers to test < or <=3D as required. I also wonder it is necessary to count the total length when the buffer isn't long enough? Unless there is a real pressing need for it I'd not bother. Setting pos =3D=3D size (after writing the '\0') allows overflow be detected without most of the dangers. > + > +static inline unsigned printbuf_written(struct printbuf *out) > +{ > +=09return min(out->pos, out->size); That excludes the '\0' for short buffers but includes it for overlong ones. > +} > + > +/* > + * Returns true if output was truncated: > + */ > +static inline bool printbuf_overflowed(struct printbuf *out) > +{ > +=09return out->pos >=3D out->size; > +} > + > +static inline void printbuf_nul_terminate(struct printbuf *out) > +{ > +=09if (out->pos < out->size) > +=09=09out->buf[out->pos] =3D 0; > +=09else if (out->size) > +=09=09out->buf[out->size - 1] =3D 0; > +} > + > +static inline void __prt_char(struct printbuf *out, char c) > +{ > +=09if (printbuf_remaining(out)) > +=09=09out->buf[out->pos] =3D c; At this point it is (should be) always safe to add the '\0'. Doing so would save the extra conditionals later on. > +=09out->pos++; > +} > + > +static inline void prt_char(struct printbuf *out, char c) > +{ > +=09__prt_char(out, c); > +=09printbuf_nul_terminate(out); > +} > + > +static inline void __prt_chars(struct printbuf *out, char c, unsigned n) > +{ > +=09unsigned i, can_print =3D min(n, printbuf_remaining(out)); > + > +=09for (i =3D 0; i < can_print; i++) > +=09=09out->buf[out->pos++] =3D c; > +=09out->pos +=3D n - can_print; > +} > + > +static inline void prt_chars(struct printbuf *out, char c, unsigned n) > +{ > +=09__prt_chars(out, c, n); > +=09printbuf_nul_terminate(out); > +} > + > +static inline void prt_bytes(struct printbuf *out, const void *b, unsign= ed n) > +{ > +=09unsigned i, can_print =3D min(n, printbuf_remaining(out)); > + > +=09for (i =3D 0; i < can_print; i++) > +=09=09out->buf[out->pos++] =3D ((char *) b)[i]; > +=09out->pos +=3D n - can_print; > + > +=09printbuf_nul_terminate(out); jeepers - that can be written so much better. Something like: =09unsigned int i, pos =3D out->pos; =09int space =3D pos - out->size - 1; =09char *tgt =3D out->buf + pos; =09const char *src =3D b; =09out->pos =3D pos + n; =09if (space <=3D 0) =09=09return; =09if (n > space) =09=09n =3D space; =09for (i =3D 0; i < n; i++) =09=09tgt[i] =3D src[i]; =09tgt[1] =3D 0; > +} > + > +static inline void prt_str(struct printbuf *out, const char *str) > +{ > +=09prt_bytes(out, str, strlen(str)); Do you really need to call strlen() and then process the buffer byte by byte? =09David > +} > + > +static inline void prt_hex_byte(struct printbuf *out, u8 byte) > +{ > +=09__prt_char(out, hex_asc_hi(byte)); > +=09__prt_char(out, hex_asc_lo(byte)); > +=09printbuf_nul_terminate(out); > +} > + > +static inline void prt_hex_byte_upper(struct printbuf *out, u8 byte) > +{ > +=09__prt_char(out, hex_asc_upper_hi(byte)); > +=09__prt_char(out, hex_asc_upper_lo(byte)); > +=09printbuf_nul_terminate(out); > +} > + > +#define PRINTBUF_EXTERN(_buf, _size)=09=09=09\ > +((struct printbuf) {=09=09=09=09=09\ > +=09.buf=09=3D _buf,=09=09=09=09=09\ > +=09.size=09=3D _size,=09=09=09=09\ > +}) > + > +#endif /* _LINUX_PRINTBUF_H */ > -- > 2.36.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)