From: Hanjun Guo <hanjun.guo@linaro.org>
To: Grant Likely <grant.likely@linaro.org>,
"Rafael J. Wysocki" <rjw@rjwysocki.net>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will.deacon@arm.com>,
Russell King - ARM Linux <linux@arm.linux.org.uk>
Cc: Mark Rutland <mark.rutland@arm.com>,
Matthew Garrett <mjg59@srcf.ucam.org>,
linaro-kernel@lists.linaro.org, linux-acpi@vger.kernel.org,
patches@linaro.org, linux-kernel@vger.kernel.org,
Rob Herring <rob.herring@calxeda.com>,
linaro-acpi@lists.linaro.org, Olof Johansson <olof@lixom.net>,
Bjorn Helgaas <bhelgaas@google.com>,
linux-arm-kernel@lists.infradead.org
Subject: Re: [RFC part2 PATCH 4/9] ARM64 / ACPI: Use Parked Address in GIC structure for spin table SMP initialisation
Date: Wed, 11 Dec 2013 15:02:45 +0800 [thread overview]
Message-ID: <52A80E15.50404@linaro.org> (raw)
In-Reply-To: <20131210130330.65087C40A27@trevor.secretlab.ca>
On 2013-12-10 21:03, Grant Likely wrote:
[...]
>> +/* Parked Address in ACPI GIC structure can be used as cpu release addr */
>> +int acpi_get_parked_address_with_gic_id(u32 gic_id, u64 *parked_address)
>> +{
>> + struct acpi_table_header *table_header = NULL;
>> + struct acpi_subtable_header *entry;
>> + int err = 0;
>> + unsigned long table_end;
>> + acpi_size tbl_size;
>> + struct acpi_madt_generic_interrupt *processor = NULL;
>> +
>> + if (!parked_address)
>> + return -EINVAL;
>> +
>> + acpi_get_table_with_size(ACPI_SIG_MADT, 0, &table_header, &tbl_size);
>> + if (!table_header) {
>> + pr_warn(PREFIX "MADT table not present\n");
>> + return -ENODEV;
>> + }
>> +
>> + table_end = (unsigned long)table_header + table_header->length;
>> +
>> + /* Parse all entries looking for a match. */
>> + entry = (struct acpi_subtable_header *)
>> + ((unsigned long)table_header + sizeof(struct acpi_table_madt));
>> +
>> + while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
>> + table_end) {
>> + if (entry->type != ACPI_MADT_TYPE_GENERIC_INTERRUPT
>> + || BAD_MADT_ENTRY(entry, table_end))
>> + continue;
>> +
>> + processor = (struct acpi_madt_generic_interrupt *)entry;
>> +
>> + if (processor->gic_id == gic_id) {
>> + *parked_address = processor->parked_address;
>> + goto out;
>> + }
>> +
>> + entry = (struct acpi_subtable_header *)
>> + ((unsigned long)entry + entry->length);
>
> All of the casting in this table looks suspicious. If you have to resort
> to casting, then the variable types are very likely wrong.
>
> In the case immediately above, it seems that the entry size doesn't
> necessarily equal the acpi_subtable_header size, in which case you
> should cast the values to a void* instead of an unsigned long. That
> would mean you can do this:
>
> entry = ((void*)entry) + entry->length;
>
> In fact, if I were writing the code, I would have two variables; the
> iterator pointer as a void* and a header pointer as a struct
> acpi_subtable_header*. Like so:
>
> void *entry, *table_end;
> struct acpi_subtable_header *header;
>
> entry = ((void*)table_header) + sizeof(struct acpi_table_madt);
> table_end = ((void*)table_header) + table_header->length;
> while (entry + sizeof(*header)) < table_end) {
> header = entry;
>
> if (header->type != ACPI_MADT_TYPE_GENERIC_INTERRUPT ||
> BAD_MADT_ENTRY(entry, table_end))
> continue;
> processor = entry;
>
> if (processor->gic_id == gic_id) {
> *parked_address = processor->parked_address;
> goto out;
> }
>
> entry += header->length;
> }
>
> See? Much cleaner code.
Aha, much much cleaner, thanks for the guidance, will rework my patch
and test it.
Hanjun
WARNING: multiple messages have this Message-ID (diff)
From: hanjun.guo@linaro.org (Hanjun Guo)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC part2 PATCH 4/9] ARM64 / ACPI: Use Parked Address in GIC structure for spin table SMP initialisation
Date: Wed, 11 Dec 2013 15:02:45 +0800 [thread overview]
Message-ID: <52A80E15.50404@linaro.org> (raw)
In-Reply-To: <20131210130330.65087C40A27@trevor.secretlab.ca>
On 2013-12-10 21:03, Grant Likely wrote:
[...]
>> +/* Parked Address in ACPI GIC structure can be used as cpu release addr */
>> +int acpi_get_parked_address_with_gic_id(u32 gic_id, u64 *parked_address)
>> +{
>> + struct acpi_table_header *table_header = NULL;
>> + struct acpi_subtable_header *entry;
>> + int err = 0;
>> + unsigned long table_end;
>> + acpi_size tbl_size;
>> + struct acpi_madt_generic_interrupt *processor = NULL;
>> +
>> + if (!parked_address)
>> + return -EINVAL;
>> +
>> + acpi_get_table_with_size(ACPI_SIG_MADT, 0, &table_header, &tbl_size);
>> + if (!table_header) {
>> + pr_warn(PREFIX "MADT table not present\n");
>> + return -ENODEV;
>> + }
>> +
>> + table_end = (unsigned long)table_header + table_header->length;
>> +
>> + /* Parse all entries looking for a match. */
>> + entry = (struct acpi_subtable_header *)
>> + ((unsigned long)table_header + sizeof(struct acpi_table_madt));
>> +
>> + while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
>> + table_end) {
>> + if (entry->type != ACPI_MADT_TYPE_GENERIC_INTERRUPT
>> + || BAD_MADT_ENTRY(entry, table_end))
>> + continue;
>> +
>> + processor = (struct acpi_madt_generic_interrupt *)entry;
>> +
>> + if (processor->gic_id == gic_id) {
>> + *parked_address = processor->parked_address;
>> + goto out;
>> + }
>> +
>> + entry = (struct acpi_subtable_header *)
>> + ((unsigned long)entry + entry->length);
>
> All of the casting in this table looks suspicious. If you have to resort
> to casting, then the variable types are very likely wrong.
>
> In the case immediately above, it seems that the entry size doesn't
> necessarily equal the acpi_subtable_header size, in which case you
> should cast the values to a void* instead of an unsigned long. That
> would mean you can do this:
>
> entry = ((void*)entry) + entry->length;
>
> In fact, if I were writing the code, I would have two variables; the
> iterator pointer as a void* and a header pointer as a struct
> acpi_subtable_header*. Like so:
>
> void *entry, *table_end;
> struct acpi_subtable_header *header;
>
> entry = ((void*)table_header) + sizeof(struct acpi_table_madt);
> table_end = ((void*)table_header) + table_header->length;
> while (entry + sizeof(*header)) < table_end) {
> header = entry;
>
> if (header->type != ACPI_MADT_TYPE_GENERIC_INTERRUPT ||
> BAD_MADT_ENTRY(entry, table_end))
> continue;
> processor = entry;
>
> if (processor->gic_id == gic_id) {
> *parked_address = processor->parked_address;
> goto out;
> }
>
> entry += header->length;
> }
>
> See? Much cleaner code.
Aha, much much cleaner, thanks for the guidance, will rework my patch
and test it.
Hanjun
next prev parent reply other threads:[~2013-12-11 7:03 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1385999094-3152-1-git-send-email-hanjun.guo@linaro.org>
[not found] ` < 1385999094-3152-3-git-send-email-hanjun.guo@linaro.org>
[not found] ` <1385999094-3152-3-git-send-email-hanjun.guo@linaro.org>
2013-12-10 12:53 ` [RFC part2 PATCH 2/9] ARM64 / ACPI: Prefill cpu possible/present maps and map logical cpu id to APIC id Grant Likely
2013-12-10 12:53 ` Grant Likely
2013-12-10 12:53 ` Grant Likely
2013-12-10 15:07 ` Hanjun Guo
2013-12-10 15:07 ` Hanjun Guo
[not found] ` < 1385999094-3152-5-git-send-email-hanjun.guo@linaro.org>
[not found] ` <1385999094-3152-5-git-send-email-hanjun.guo@linaro.org>
2013-12-10 13:03 ` [RFC part2 PATCH 4/9] ARM64 / ACPI: Use Parked Address in GIC structure for spin table SMP initialisation Grant Likely
2013-12-10 13:03 ` Grant Likely
2013-12-10 13:03 ` Grant Likely
2013-12-11 7:02 ` Hanjun Guo [this message]
2013-12-11 7:02 ` Hanjun Guo
[not found] ` < 1385999094-3152-9-git-send-email-hanjun.guo@linaro.org>
[not found] ` <1385999094-3152-9-git-send-email-hanjun.guo@linaro.org>
2013-12-10 13:05 ` [RFC part2 PATCH 8/9] ACPI / ARM64: Update acpi_register_gsi to register with the core IRQ subsystem Grant Likely
2013-12-10 13:05 ` Grant Likely
2013-12-10 13:05 ` Grant Likely
2013-12-11 5:23 ` Arnd Bergmann
2013-12-11 5:23 ` Arnd Bergmann
2014-01-20 14:36 ` Grant Likely
2014-01-20 14:36 ` Grant Likely
2013-12-03 16:39 [RFC part2 PATCH 0/9] Using ACPI MADT table to initialise SMP and GIC Hanjun Guo
2013-12-03 16:39 ` [RFC part2 PATCH 4/9] ARM64 / ACPI: Use Parked Address in GIC structure for spin table SMP initialisation Hanjun Guo
2013-12-03 16:39 ` Hanjun Guo
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=52A80E15.50404@linaro.org \
--to=hanjun.guo@linaro.org \
--cc=bhelgaas@google.com \
--cc=catalin.marinas@arm.com \
--cc=grant.likely@linaro.org \
--cc=linaro-acpi@lists.linaro.org \
--cc=linaro-kernel@lists.linaro.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=mark.rutland@arm.com \
--cc=mjg59@srcf.ucam.org \
--cc=olof@lixom.net \
--cc=patches@linaro.org \
--cc=rjw@rjwysocki.net \
--cc=rob.herring@calxeda.com \
--cc=will.deacon@arm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.