public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Arseniy Krasnov <avkrasnov@sberdevices.ru>
Cc: Liang Yang <liang.yang@amlogic.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Neil Armstrong <neil.armstrong@linaro.org>,
	Kevin Hilman <khilman@baylibre.com>,
	Jerome Brunet <jbrunet@baylibre.com>,
	Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
	<oxffffaa@gmail.com>, <kernel@sberdevices.ru>,
	<linux-mtd@lists.infradead.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-amlogic@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] mtd: rawnand: meson: check buffer length
Date: Thu, 8 Jun 2023 08:10:40 +0200	[thread overview]
Message-ID: <20230608081040.78e74eb1@xps-13> (raw)
In-Reply-To: <3a9470ed-d7ad-6cae-0d58-732399590272@sberdevices.ru>

Hi Arseniy,

avkrasnov@sberdevices.ru wrote on Thu, 8 Jun 2023 00:17:35 +0300:

> Hi again Miquel, Liang!
> 
> What do You think about this patch?

I really appreciate all the effort you are putting into this but
please consider giving me a little bit of air as well, I'm already
spending a lot of time reviewing all your patches. Please mind a ping
is not relevant before a couple of weeks in general.

In this case I had it in my "to apply" list but actually looking at it
again I have a minor comment below.

> 
> Thanks, Arseniy
> 
> On 06.06.2023 19:29, Arseniy Krasnov wrote:
> > Sorry, here is changelog:
> > v1 -> v2:
> > * Move checks from 'switch/case' which executes commands in 'meson_nfc_exec_op()' to a special
> >   separated function 'meson_nfc_check_op()' which is called before commands processing.
> > 
> > On 06.06.2023 13:16, Arseniy Krasnov wrote:  
> >> Meson NAND controller has limited buffer length, so check it before
> >> command execution to avoid length trim. Also check MTD write size on
> >> chip attach.
> >>
> >> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
> >> ---
> >>  drivers/mtd/nand/raw/meson_nand.c | 47 +++++++++++++++++++++++++++----
> >>  1 file changed, 42 insertions(+), 5 deletions(-)
> >>
> >> diff --git a/drivers/mtd/nand/raw/meson_nand.c b/drivers/mtd/nand/raw/meson_nand.c
> >> index 23a73268421b..db6b18753071 100644
> >> --- a/drivers/mtd/nand/raw/meson_nand.c
> >> +++ b/drivers/mtd/nand/raw/meson_nand.c
> >> @@ -111,6 +111,8 @@
> >>  
> >>  #define PER_INFO_BYTE		8
> >>  
> >> +#define NFC_CMD_RAW_LEN	GENMASK(13, 0)
> >> +
> >>  struct meson_nfc_nand_chip {
> >>  	struct list_head node;
> >>  	struct nand_chip nand;
> >> @@ -284,7 +286,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
> >>  
> >>  	if (raw) {
> >>  		len = mtd->writesize + mtd->oobsize;
> >> -		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);
> >> +		cmd = len | scrambler | DMA_DIR(dir);
> >>  		writel(cmd, nfc->reg_base + NFC_REG_CMD);
> >>  		return;
> >>  	}
> >> @@ -573,7 +575,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
> >>  	if (ret)
> >>  		goto out;
> >>  
> >> -	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
> >> +	cmd = NFC_CMD_N2M | len;
> >>  	writel(cmd, nfc->reg_base + NFC_REG_CMD);
> >>  
> >>  	meson_nfc_drain_cmd(nfc);
> >> @@ -597,7 +599,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
> >>  	if (ret)
> >>  		return ret;
> >>  
> >> -	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
> >> +	cmd = NFC_CMD_M2N | len;
> >>  	writel(cmd, nfc->reg_base + NFC_REG_CMD);
> >>  
> >>  	meson_nfc_drain_cmd(nfc);
> >> @@ -1007,6 +1009,31 @@ meson_nand_op_put_dma_safe_output_buf(const struct nand_op_instr *instr,
> >>  		kfree(buf);
> >>  }
> >>  
> >> +static int meson_nfc_check_op(struct nand_chip *chip,
> >> +			      const struct nand_operation *op)
> >> +{
> >> +	int op_id;
> >> +
> >> +	for (op_id = 0; op_id < op->ninstrs; op_id++) {
> >> +		const struct nand_op_instr *instr;
> >> +
> >> +		instr = &op->instrs[op_id];
> >> +
> >> +		switch (instr->type) {
> >> +		case NAND_OP_DATA_IN_INSTR:
> >> +		case NAND_OP_DATA_OUT_INSTR:
> >> +			if (instr->ctx.data.len > NFC_CMD_RAW_LEN)
> >> +				return -ENOTSUPP;
> >> +
> >> +			break;
> >> +		default:
> >> +			break;
> >> +		}
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >>  static int meson_nfc_exec_op(struct nand_chip *nand,
> >>  			     const struct nand_operation *op, bool check_only)
> >>  {
> >> @@ -1015,10 +1042,12 @@ static int meson_nfc_exec_op(struct nand_chip *nand,
> >>  	const struct nand_op_instr *instr = NULL;
> >>  	void *buf;
> >>  	u32 op_id, delay_idle, cmd;
> >> +	int err;
> >>  	int i;
> >>  
> >> -	if (check_only)
> >> -		return 0;
> >> +	err = meson_nfc_check_op(nand, op);
> >> +	if (err || check_only)
> >> +		return err;

For the sake of readability it is really important that we keep using
standard constructions, so don't mix orthogonal checks and please use
something like:

	err = check();
	if (err)
		return err;

	if (check_only)
		return 0;

> >>  
> >>  	meson_nfc_select_chip(nand, op->cs);
> >>  	for (op_id = 0; op_id < op->ninstrs; op_id++) {
> >> @@ -1293,6 +1322,7 @@ static int meson_nand_attach_chip(struct nand_chip *nand)
> >>  	struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
> >>  	struct mtd_info *mtd = nand_to_mtd(nand);
> >>  	int nsectors = mtd->writesize / 1024;
> >> +	int raw_writesize;
> >>  	int ret;
> >>  
> >>  	if (!mtd->name) {
> >> @@ -1304,6 +1334,13 @@ static int meson_nand_attach_chip(struct nand_chip *nand)
> >>  			return -ENOMEM;
> >>  	}
> >>  
> >> +	raw_writesize = mtd->writesize + mtd->oobsize;
> >> +	if (raw_writesize > NFC_CMD_RAW_LEN) {
> >> +		dev_err(nfc->dev, "too big write size in raw mode: %d > %ld\n",
> >> +			raw_writesize, NFC_CMD_RAW_LEN);
> >> +		return -EINVAL;
> >> +	}
> >> +
> >>  	if (nand->bbt_options & NAND_BBT_USE_FLASH)
> >>  		nand->bbt_options |= NAND_BBT_NO_OOB;
> >>    


Thanks,
Miquèl

  reply	other threads:[~2023-06-08  6:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-06 10:16 [PATCH v2] mtd: rawnand: meson: check buffer length Arseniy Krasnov
2023-06-06 16:29 ` Arseniy Krasnov
2023-06-07 21:17   ` Arseniy Krasnov
2023-06-08  6:10     ` Miquel Raynal [this message]
2023-06-08  6:08       ` Arseniy Krasnov
2023-06-08  6:42     ` Liang Yang
2023-06-08  6:54       ` Miquel Raynal
2023-06-08  7:12         ` Liang Yang
2023-06-08 12:37           ` Liang Yang
2023-06-08 13:21             ` Miquel Raynal
2023-06-09  7:59               ` Liang Yang
2023-06-09 10:55                 ` Arseniy Krasnov

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=20230608081040.78e74eb1@xps-13 \
    --to=miquel.raynal@bootlin.com \
    --cc=avkrasnov@sberdevices.ru \
    --cc=jbrunet@baylibre.com \
    --cc=kernel@sberdevices.ru \
    --cc=khilman@baylibre.com \
    --cc=liang.yang@amlogic.com \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=neil.armstrong@linaro.org \
    --cc=oxffffaa@gmail.com \
    --cc=richard@nod.at \
    --cc=vigneshr@ti.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