From: Basharath Hussain Khaja <basharath@couthit.com>
To: Rob Herring <robh@kernel.org>
Cc: basharath <basharath@couthit.com>,
mpe@ellerman.id.au,
thomas weissschuh <thomas.weissschuh@linutronix.de>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, saravanak@google.com,
danishanwar <danishanwar@ti.com>, krishna <krishna@couthit.com>,
mohan <mohan@couthit.com>, parvathi <parvathi@couthit.com>,
pmohan <pmohan@couthit.com>
Subject: Re: [PATCH] of: address: Unify resource bounds overflow checking
Date: Fri, 17 Jan 2025 12:23:53 +0530 (IST) [thread overview]
Message-ID: <2089813158.341858.1737096833060.JavaMail.zimbra@couthit.local> (raw)
In-Reply-To: <CAL_JsqLLGW_o9i6a5wcUj=Z=4nL-GhzHwAQMFtQkb9OSHuSgTA@mail.gmail.com>
>> >> Thomas Weißschuh <thomas.weissschuh@linutronix.de> writes:
>> >> > The members "start" and "end" of struct resource are of type
>> >> > "resource_size_t" which can be 32bit wide.
>> >> > Values read from OF however are always 64bit wide.
>> >> >
>> >> > Refactor the diff overflow checks into a helper function.
>> >> > Also extend the checks to validate each calculation step.
>> >> >
>> >> > Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
>> >> > ---
>> >> > drivers/of/address.c | 45 ++++++++++++++++++++++++++-------------------
>> >> > 1 file changed, 26 insertions(+), 19 deletions(-)
>> >> >
>> >> > diff --git a/drivers/of/address.c b/drivers/of/address.c
>> >> > index 7e59283a4472..df854bb427ce 100644
>> >> > --- a/drivers/of/address.c
>> >> > +++ b/drivers/of/address.c
>> >> > @@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32
>> >> > *range, int na, int ns,
>> >> >
>> >> > #endif /* CONFIG_PCI */
>> >> >
>> >> > +static int __of_address_resource_bounds(struct resource *r, u64 start, u64
>> >> > size)
>> >> > +{
>> >> > + u64 end = start;
>> >> > +
>> >> > + if (overflows_type(start, r->start))
>> >> > + return -EOVERFLOW;
>> >> > + if (size == 0)
>> >> > + return -EOVERFLOW;
>> >> > + if (check_add_overflow(end, size - 1, &end))
>> >> > + return -EOVERFLOW;
>> >> > + if (overflows_type(end, r->end))
>> >> > + return -EOVERFLOW;
>> >>
>> >> This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource
>> >> that's zero sized, which used to succeed but now fails due to the size
>> >> check above.
>> >>
>> >> The diff below fixes it for me.
>> >
>> > I fixed it up with your change.
>>
>>
>> This commit is breaking Ethernet functionality on the TI AM57xx platform due to
>> zero byte SRAM block size allocation during initialization. Prior to this
>> patch, zero byte block sizes were handled properly.
>
> What driver and where exactly?
We found an issue while developing the driver [1] and more specifically in [2] (lines 313-327), but it looks like this is a generic issue which can block 1 byte of memory, when a zero size request has been initiated for the reserved region.
static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
{
u64 end = start;
if (overflows_type(start, r->start))
return -EOVERFLOW;
if (size && check_add_overflow(end, size - 1, &end))
return -EOVERFLOW;
if (overflows_type(end, r->end))
return -EOVERFLOW;
r->start = start;
r->end = end;
return 0;
}
Though we have the start address handling already in place above, we do see an issue with the end address, because there is an unconditional +1 afterwards in resource_size() API below which is responsible for reserving the extra byte
static inline resource_size_t resource_size(const struct resource *res)
{
return res->end - res->start + 1;
}
We have 4 ways of fixing it.
Option 1: Modify the function to handle the size zero case
diff --git a/drivers/of/address.c b/drivers/of/address.c
index c1f1c810e810..8db6ae9a12b8 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -204,6 +204,12 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
if (overflows_type(start, r->start))
return -EOVERFLOW;
+ if (!size) {
+ r->start = start;
+ r->end = end - 1;
+
+ return 0;
+ }
if (size && check_add_overflow(end, size - 1, &end))
return -EOVERFLOW;
if (overflows_type(end, r->end))
This seems to be the simplest solution.
Option 2: Handle in resource_size().
static inline resource_size_t resource_size(const struct resource *res)
{
return (res->end == res->start) ? 0 : (res->end - res->start + 1);
}
There are plenty of places where we are using this API and there is an assumption that the end address should always be start + size -1. We are a bit unsure about the side effects of this change.
Option 3: Handle in sram_reserve_region().
We can avoid calling the resource_size() API and handle size zero as a special case. We are a bit unsure about the side effects of this change as well.
Option 4: Handle this in dts [2] with non zero size. Estimate the approximate size and update that value in dts file with extra buffer. However, as indicated in [2] in lines 313-327, the size is not known apriori and the actual size is only known in runtime. So if we set some size for this buffer, then this will always be blocked and may or may not be used subsequently.
[1] https://lore.kernel.org/all/20250109105600.41297-1-basharath@couthit.com/
[2] https://github.com/torvalds/linux/blob/master/arch/arm/boot/dts/ti/omap/dra7.dtsi
Thanks & Best Regards,
Basharath
next prev parent reply other threads:[~2025-01-17 6:54 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-06 12:25 Thomas Weißschuh
2024-09-06 21:34 ` Rob Herring (Arm)
2024-09-13 13:15 ` Michael Ellerman
2024-09-13 18:56 ` Rob Herring
2024-09-13 23:10 ` Michael Ellerman
2025-01-08 14:04 ` Basharath Hussain Khaja
2025-01-08 22:43 ` Rob Herring
2025-01-17 6:53 ` Basharath Hussain Khaja [this message]
2025-01-17 13:06 ` Thomas Weißschuh
2025-01-20 10:09 ` Basharath Hussain Khaja
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=2089813158.341858.1737096833060.JavaMail.zimbra@couthit.local \
--to=basharath@couthit.com \
--cc=danishanwar@ti.com \
--cc=devicetree@vger.kernel.org \
--cc=krishna@couthit.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mohan@couthit.com \
--cc=mpe@ellerman.id.au \
--cc=parvathi@couthit.com \
--cc=pmohan@couthit.com \
--cc=robh@kernel.org \
--cc=saravanak@google.com \
--cc=thomas.weissschuh@linutronix.de \
/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®