Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp167675pxb; Fri, 17 Sep 2021 22:38:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1yzo11S8BMcEycUItweYcnAmPWDA2DZNYnTKsN83p3cmqrcxuIxhAKCtk8zcf8W5UHy0E X-Received: by 2002:a17:906:d8a4:: with SMTP id qc4mr16818561ejb.323.1631943502901; Fri, 17 Sep 2021 22:38:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631943502; cv=none; d=google.com; s=arc-20160816; b=f/XNiPUXSMTy5X1gwfDVS+w+MmX84kPkodB7oW5RRi8DKwsz03vJU7L7OBwrPHL3vp m0yXiVHiWWmjFmgFDOexAX0vl5iuP6jdRU5+ChNSTt+TXvJymxhGQhTI62s8dkRLcoOc wBcQnEfl1hvwRCNzl0lYZejNpxaOKzQ0bkq5AxwKdENHQvW7f0pswnER/3CxyFnIBRV1 NkYwV8m5rpRcKPH8wShG41PwNfElQSldZjQkH3mfArMiktXzeGLASL6fBKU+pOLe+FKS dUzCOaEzk4ISIEDH9LQFJmInfRDb1MxRFTca6Gh6+JT3rR71NM6FxdrdYacSPRNNIuel vAUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=OS94uZTmA0qnszl1fwVMo+RQL+/vTVqvtH0x+p48wY0=; b=WeR1Au0zleciuqjAI08PVPuRRrVJw/0pSr8RjKX01iKvOze+Tc5FC8jICbBbbechLI iqaGSxBbHQxTe/t4ZCo5znAfEz3ito1SNw1V2R6S2I8kojUdEtJiPfTRBoyhNzwAY8sC yIMtojrnb7Hk8DS+rFMuE9Ux6Pj6PtorYe35S3FI9eNz3Xgc6FHXplVPxqvij4qI366g 9wsFMjViTBZdVcrzWdihgN5vKvhAlPRRGl8NiXDZ8ueisvpt7JbQNVwHIQz8EyX1qPsr eW3yqEs8s4GVx3YEalh66l81JCGiCcQwEnM/1xjRVZWT8+1XAmObdxPRZsIH6bhZKfYi FfWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bRsPWtlh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v18si2738033ejh.182.2021.09.17.22.37.59; Fri, 17 Sep 2021 22:38:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bRsPWtlh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230105AbhIQP0K (ORCPT + 99 others); Fri, 17 Sep 2021 11:26:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344307AbhIQPY0 (ORCPT ); Fri, 17 Sep 2021 11:24:26 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36035C0613C1 for ; Fri, 17 Sep 2021 08:22:40 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id lb1-20020a17090b4a4100b001993f863df2so7618059pjb.5 for ; Fri, 17 Sep 2021 08:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=OS94uZTmA0qnszl1fwVMo+RQL+/vTVqvtH0x+p48wY0=; b=bRsPWtlhXzo4Tc/8or9LqnXhztTrk9NmxMapoTxWY0Ypjm1gL3iusGWCPx1hXm6hXA OnJWj3AfeV/AyOHx5exXC8jx9r5SrprlQOmDZ4fXNd1uKmS3VPuHRNL+hiI+Z8GF4XPi WK/2g61WY2v+O6shCYTiop8X+p9M/F/J96rA8m3g8Ut+moKVIOQoEjGLOImm2cOJAt7M f5crjbQisPGL3RZe3I/3BFsuR2bOn0bHvJN95jx9lTXBpcsUvy5TCJbdsn1eUZwqGKRR MXAOf8+zN8rXXGS6FxxD2Hn+G8vBOwu4E/XhZs8fEPSUn5SEgVF7Nbf60iAD89hmPRX4 Go7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=OS94uZTmA0qnszl1fwVMo+RQL+/vTVqvtH0x+p48wY0=; b=Z4wjrYgmhpvClKzY9t8+330Fsxji5JKckcyJhyM3DERwxf6aJxxMah5oCPv3ikoxrw OYOFUB4DTHN3Pk84pgYE5d408aDN18pFbnnv77E6S4pa7fg4mz+HYudQa12hVX388U7n Ie/0UVKMIm5YfmEPVIXqyb/AH8ODab8AjzERJh6sMonctjn7eaUbUJmXfsJbYh+HNUpm GRLK6wRiJRpJ4bUsL61XlSDWDo4Rk/oI1OjaqAt9nCmWssVOGYZH7lMyH8c327790oSZ 7nAIVxy24aMlDT80CS/Js/pyb7PbXfxjRmmWbdVyptYIpK+O1J7Oowy1KCsNb7VTMZGI jnOA== X-Gm-Message-State: AOAM533Wsr2a6qvIT3b6WXXrMnH6nhC9sVXn5ww5jwtLrs+vhBRFetPS 2JJOijzB/6bRr75bWo9czo2Fyg== X-Received: by 2002:a17:902:a3ca:b0:13c:a7be:1767 with SMTP id q10-20020a170902a3ca00b0013ca7be1767mr6481323plb.88.1631892159539; Fri, 17 Sep 2021 08:22:39 -0700 (PDT) Received: from p14s (S0106889e681aac74.cg.shawcable.net. [68.147.0.187]) by smtp.gmail.com with ESMTPSA id 23sm6472340pfp.206.2021.09.17.08.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Sep 2021 08:22:38 -0700 (PDT) Date: Fri, 17 Sep 2021 09:22:36 -0600 From: Mathieu Poirier To: Shengjiu Wang Cc: Shengjiu Wang , Ohad Ben Cohen , Bjorn Andersson , Rob Herring , Shawn Guo , Sascha Hauer , Sascha Hauer , Fabio Estevam , Daniel Baluta , NXP Linux Team , "open list:REMOTE PROCESSOR (REMOTEPROC) SUBSYSTEM" , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , "moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE" , linux-kernel Subject: Re: [PATCH v4 3/4] remoteproc: imx_dsp_rproc: Add remoteproc driver for DSP on i.MX Message-ID: <20210917152236.GA1878943@p14s> References: <1631092255-25150-1-git-send-email-shengjiu.wang@nxp.com> <1631092255-25150-4-git-send-email-shengjiu.wang@nxp.com> <20210915161624.GA1770838@p14s> <20210916165957.GA1825273@p14s> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 17, 2021 at 05:44:44PM +0800, Shengjiu Wang wrote: > On Fri, Sep 17, 2021 at 1:20 PM Shengjiu Wang wrote: > > > > On Fri, Sep 17, 2021 at 1:00 AM Mathieu Poirier > > wrote: > > > > > > [...] > > > > > > > > > + > > > > > > +/** > > > > > > + * imx_dsp_rproc_elf_load_segments() - load firmware segments to memory > > > > > > + * @rproc: remote processor which will be booted using these fw segments > > > > > > + * @fw: the ELF firmware image > > > > > > + * > > > > > > + * This function specially checks if memsz is zero or not, otherwise it > > > > > > + * is mostly same as rproc_elf_load_segments(). > > > > > > + */ > > > > > > +static int imx_dsp_rproc_elf_load_segments(struct rproc *rproc, > > > > > > + const struct firmware *fw) > > > > > > +{ > > > > > > + struct device *dev = &rproc->dev; > > > > > > + u8 class = fw_elf_get_class(fw); > > > > > > + u32 elf_phdr_get_size = elf_size_of_phdr(class); > > > > > > + const u8 *elf_data = fw->data; > > > > > > + const void *ehdr, *phdr; > > > > > > + int i, ret = 0; > > > > > > + u16 phnum; > > > > > > + > > > > > > + ehdr = elf_data; > > > > > > + phnum = elf_hdr_get_e_phnum(class, ehdr); > > > > > > + phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr); > > > > > > + > > > > > > + /* go through the available ELF segments */ > > > > > > + for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) { > > > > > > + u64 da = elf_phdr_get_p_paddr(class, phdr); > > > > > > + u64 memsz = elf_phdr_get_p_memsz(class, phdr); > > > > > > + u64 filesz = elf_phdr_get_p_filesz(class, phdr); > > > > > > + u64 offset = elf_phdr_get_p_offset(class, phdr); > > > > > > + u32 type = elf_phdr_get_p_type(class, phdr); > > > > > > + void *ptr; > > > > > > + bool is_iomem; > > > > > > + > > > > > > + if (type != PT_LOAD || !memsz) > > > > > > > > > > You did a really good job with adding comments but this part is undocumented... > > > > > If I read this correctly you need to check for !memsz because some part of > > > > > the program segment may have a header but its memsz is zero, in which case it can > > > > > be safely skipped. So why is that segment in the image to start with, and why > > > > > is it marked PT_LOAD if it is not needed? This is very puzzling... > > > > > > > > Actually I have added comments in the header of this function. > > > > > > Indeed there is a mention of memsz in the function's header but it doesn't > > > mention _why_ this is needed, and that is what I'm looking for. > > > > > > > > > > > memsz= 0 with PT_LOAD issue, I have asked the toolchain's vendor, > > > > they said that this case is allowed by elf spec... > > > > > > > > And in the "pru_rproc.c" and "mtk_scp.c", seems they met same problem > > > > they also check the filesz in their internal xxx_elf_load_segments() function. > > > > > > In both cases they are skipping PT_LOAD sections where "filesz" is '0', which > > > makes sense because we don't know how many bytes to copy. But here you are > > > skipping over a PT_LOAD section with a potentially valid filesz, and that is the > > > part I don't understand. > > > > Ok, I can use filesz instead. For my case, filesz = memsz = 0, > > it is the same result I want. If that is the case then rproc_elf_load_segments() should work, i.e it won't copy anything. If rproc_elf_load_segments() doesn't work for you then there are corner cases you haven't told me about. > > > > The reason why I use "memsz '' is because there is "if (filesz > memsz) " > > check after this, if memsz is zero, then "filesz" should be zero too, other > > values are not allowed. > > But I still think checking "!memsz" is better than filesz, because > memsz > filesz is allowed (filesz = 0), the code below can be executed. > filesz > memsz is not allowed. > > What do you think? I don't see a need to add a custom implementation for things that _may_ happen. If using the default rproc_elf_load_segments() works than go with that. We can deal with problems if/when there is a need for it. > > Best regards > Wang shengjiu > > > > > > > > > > > > > > > > > > > > > > > > > + continue; > > > > > > + > > > > > > + dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n", > > > > > > + type, da, memsz, filesz); > > > > > > + > > > > > > + if (filesz > memsz) { > > > > > > + dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n", > > > > > > + filesz, memsz); > > > > > > + ret = -EINVAL; > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + if (offset + filesz > fw->size) { > > > > > > + dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n", > > > > > > + offset + filesz, fw->size); > > > > > > + ret = -EINVAL; > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + if (!rproc_u64_fit_in_size_t(memsz)) { > > > > > > + dev_err(dev, "size (%llx) does not fit in size_t type\n", > > > > > > + memsz); > > > > > > + ret = -EOVERFLOW; > > > > > > + break; > > > > > > + } > > > > > > + > > > > > > + /* grab the kernel address for this device address */ > > > > > > + ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem); > > > > > > > > > > rproc_da_to_va(rproc, da, memsz, NULL); > > > > > > > > yes, will update it. > > > > > > > > > > > > > > More comments to follow later today or tomorrow. > > > > > > > > Thanks. > > > > > > > > Best regards > > > > Wang Shengjiu