Skip to content

[Deepin-Kernel-SIG] [linux 6.6.y] [Phytium] some phytium driver fixes found by GitHub Copilot - #2078

Open
Avenger-285714 wants to merge 5 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:phytium-6.6
Open

[Deepin-Kernel-SIG] [linux 6.6.y] [Phytium] some phytium driver fixes found by GitHub Copilot#2078
Avenger-285714 wants to merge 5 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:phytium-6.6

Conversation

@Avenger-285714

@Avenger-285714 Avenger-285714 commented Aug 12, 2026

Copy link
Copy Markdown
Member

Summary by Sourcery

Fix Phytium localbus and QSPI driver probing and resource handling.

Bug Fixes:

  • Prevent double-free/mismatched free by removing kfree on devm-managed phytium_lbc allocations.
  • Replace dynamically allocated reg-names array with a fixed-size stack array in phytium_lbc to avoid allocation and lifetime issues.
  • Add checks for missing ACPI memory resources and fail probing with ENODEV when they are absent in phytium_lbc.
  • Return ENODEV when neither a device tree node nor an ACPI companion is present for the Phytium localbus controller.
  • Initialize the resource pointer in phytium_qspi_probe to avoid potential use of an uninitialized variable.

@Avenger-285714
Avenger-285714 requested a balanced review from Copilot August 12, 2026 09:03
@sourcery-ai

sourcery-ai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR hardens Phytium platform drivers by replacing a dynamically allocated reg-names array with a fixed stack array, adding missing resource-null checks and error paths, and initializing a resource pointer to avoid potential misuse in the SPI QSPI probe path.

Sequence diagram for updated phytium_lbc_probe resource handling

sequenceDiagram
    participant P as platform_device
    participant D as device
    participant L as phytium_lbc
    participant R as resource

    D->>L: phytium_lbc_probe(pdev)
    alt dev.of_node
        D->>P: platform_get_resource_byname(pdev, IORESOURCE_MEM, localbus)
        P-->>R: resource(localbus)
        D->>D: devm_ioremap_resource(dev, res) / lbc->io_base
        alt IS_ERR(lbc->io_base)
            D-->>D: return PTR_ERR(lbc->io_base)
        end
        D->>P: platform_get_resource_byname(pdev, IORESOURCE_MEM, lbc_mm)
        P-->>R: resource(lbc_mm)
        D->>D: devm_ioremap_resource(dev, res) / lbc->mm_base
        alt IS_ERR(lbc->mm_base)
            D-->>D: return PTR_ERR(lbc->mm_base)
        end
    else has_acpi_companion(dev)
        D->>P: platform_get_resource(pdev, IORESOURCE_MEM, 0)
        alt res == NULL
            D-->>D: return -ENODEV
        else res != NULL
            D->>D: fwnode_property_read_string_array(dev->fwnode, reg-names, reg_names_array, 2)
            D->>D: res->name = reg_names_array[0]
            D->>D: devm_ioremap_resource(dev, res) / lbc->io_base
            alt IS_ERR(lbc->io_base)
                D-->>D: return PTR_ERR(lbc->io_base)
            end
        end
        D->>P: platform_get_resource(pdev, IORESOURCE_MEM, 1)
        alt res == NULL
            D-->>D: return -ENODEV
        else res != NULL
            D->>D: res->name = reg_names_array[1]
            D->>D: devm_ioremap_resource(dev, res) / lbc->mm_base
            alt IS_ERR(lbc->mm_base)
                D-->>D: return PTR_ERR(lbc->mm_base)
            end
        end
    else no of_node and no ACPI companion
        D->>D: dev_err(dev, no device tree node or ACPI companion found.)
        D-->>D: return -ENODEV
    end
Loading

File-Level Changes

Change Details Files
Make reg-names handling in the Phytium LBC probe safer and remove unnecessary manual frees.
  • Replace kcalloc-based dynamic allocation of reg-names with a fixed-size stack array of four const char* entries.
  • Remove manual kfree(lbc) calls in probe error paths that now rely on devm-managed allocations for cleanup.
  • Add error handling for missing device tree or ACPI companion by logging an error and returning -ENODEV.
drivers/mtd/maps/phytium_lbc.c
Harden ACPI resource lookup and naming in the Phytium LBC driver.
  • Add NULL checks for ACPI memory resources obtained via platform_get_resource before using them.
  • Use the reg-names array filled by fwnode_property_read_string_array to name each resource before ioremap.
  • Ensure failures in mapping IO or MM regions return appropriate error codes without leaking resources.
drivers/mtd/maps/phytium_lbc.c
Initialize the resource pointer in the Phytium QSPI probe to avoid undefined use.
  • Initialize struct resource *res to NULL at declaration in the QSPI probe function to prevent potential use of an uninitialized pointer in later code paths.
drivers/spi/spi-phytium-qspi.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from avenger-285714. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In the ACPI path of phytium_lbc_probe, the return value of fwnode_property_read_string_array() is ignored; consider checking it and failing probe if reg-names are missing so you don't assign NULL names to resources.
  • phytium_lbc_probe still relies on 'res' when computing lbc->mm_size; ensure that in all early-return branches (including the new ENODEV paths) 'res' is either valid or not used afterward to avoid any accidental use of an uninitialized/incorrect resource.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the ACPI path of phytium_lbc_probe, the return value of fwnode_property_read_string_array() is ignored; consider checking it and failing probe if reg-names are missing so you don't assign NULL names to resources.
- phytium_lbc_probe still relies on 'res' when computing lbc->mm_size; ensure that in all early-return branches (including the new ENODEV paths) 'res' is either valid or not used afterward to avoid any accidental use of an uninitialized/incorrect resource.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Avenger-285714
Avenger-285714 requested a review from opsiff August 12, 2026 09:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Phytium localbus and QSPI probe-time resource handling.

Changes:

  • Removes invalid frees of managed localbus allocations.
  • Validates localbus firmware and ACPI resources.
  • Initializes the QSPI resource pointer.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
drivers/spi/spi-phytium-qspi.c Initializes the probe resource pointer.
drivers/mtd/maps/phytium_lbc.c Improves allocation and resource error handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

struct device *dev = &pdev->dev;
struct spi_controller *ctrl;
struct resource *res;
struct resource *res = NULL;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除非固件有bug,不然没必要在这里做防御性修复

Clang warns that 'res' may be used uninitialized in
phytium_qspi_probe():

drivers/spi/spi-phytium-qspi.c:717:11: error: variable 'res' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
  717 |         else if (has_acpi_companion(dev)) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~
drivers/spi/spi-phytium-qspi.c:723:45: note: uninitialized use occurs here
  723 |         qspi->io_base = devm_ioremap_resource(dev, res);
      |                                                    ^~~
drivers/spi/spi-phytium-qspi.c:717:7: note: remove the 'if' if its condition is always true
  717 |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/spi/spi-phytium-qspi.c:688:22: note: initialize the variable 'res' to silence this warning
  688 |         struct resource *res;
      |                             ^
      |                              = NULL
1 error generated.

'res' is only assigned inside the 'if (dev->of_node)' /
'else if (has_acpi_companion(dev))' branches. When a device has
neither a DT node nor an ACPI companion, neither branch is taken and
'res' is passed uninitialized to devm_ioremap_resource(). GCC does
not report this, but with CONFIG_WERROR=y a clang build fails.

Initialize 'res' to NULL so that devm_ioremap_resource() handles the
missing-resource case gracefully (it returns -EINVAL for a NULL
resource) instead of consuming an uninitialized pointer.

Fixes: 8b02928 ("arm64: phytium: UEFI mode acpi table support for qspi/spi driver")
Assisted-by: GitHub Copilot:deepseek-v4-flash
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
Fix a clang build error (fatal because CONFIG_WERROR=y) when compiling
for arm64:

  drivers/mtd/maps/phytium_lbc.c:439:13: error: variable 'res' is used
  uninitialized whenever 'if' condition is false
  [-Werror,-Wsometimes-uninitialized]
    439 |         } else if (has_acpi_companion(dev)) {
        |                    ^~~~~~~~~~~~~~~~~~~~~~~
  drivers/mtd/maps/phytium_lbc.c:460:31: note: uninitialized use occurs here
    460 |         lbc->mm_size = resource_size(res);
        |                                      ^~~
  drivers/mtd/maps/phytium_lbc.c:439:9: note: remove the 'if' if its
  condition is always true
    439 |         } else if (has_acpi_companion(dev)) {
        |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  drivers/mtd/maps/phytium_lbc.c:406:22: note: initialize the variable
  'res' to silence this warning
    406 |         struct resource *res;
        |                             ^
        |                              = NULL
  1 error generated.

In phytium_lbc_probe(), 'res' is only assigned inside the
"if (dev->of_node)" and "else if (has_acpi_companion(dev))" branches, but
it is dereferenced unconditionally afterwards by:

        lbc->mm_size = resource_size(res);

When the probed platform device has neither a device tree node nor an
ACPI companion, 'res' is never assigned, so resource_size() dereferences
an uninitialized pointer. This is undefined behavior at runtime and is
also reported by clang's -Wsometimes-uninitialized, which is promoted to
a hard error by CONFIG_WERROR=y.

Reject devices with neither a device tree node nor an ACPI companion by
returning -ENODEV before 'res' is used. This guarantees 'res' is always
initialized when resource_size() is reached. No manual cleanup is needed
on this path because 'lbc' is allocated with devm_kzalloc() and is freed
automatically by devres when the probe fails.

Fixes: 64b35ad ("lbc: phytium: Add uefi support for localbus controller driver")
Assisted-by: GitHub Copilot:deepseek-v4-flash
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
phytium_lbc_probe() allocates lbc with devm_kzalloc() but then calls
kfree(lbc) on five error paths.  devm_kzalloc() returns a pointer
into the middle of the devres object (the devres header plus
ARCH_DMA_MINALIGN padding sits in front of it), so kfree(lbc) is an
interior-pointer free that corrupts the slab freelist; when the
probe then fails, devres_release_all() frees the very same object
again at its real base address - a double free.  The production
defconfig enables neither KASAN nor SLUB debugging, so on real
firmware-error paths this is silent heap corruption.

Simply drop the kfree() calls: devres releases the memory
automatically when the probe fails.  Spotted while addressing the
reg_names_array leak flagged by code review in the same function.

Fixes: 64b35ad ("lbc: phytium: Add uefi support for localbus controller driver")
Reported-by: GitHub Copilot:Code Review model
Assisted-by: Kimi Code:K3
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
reg_names_array is kcalloc()'d unconditionally before the
firmware-type branch, but it is only ever used in the ACPI branch
and never freed anywhere: every successful probe leaks the 32-byte
array (on the OF path it is not even used).  The early return for
devices with neither an OF node nor an ACPI companion, added later
by commit b04ff35 ("mtd: maps: phytium_lbc: fix uninitialized
use of 'res' in probe"), leaks it as well.

The string pointers filled in by fwnode_property_read_string_array()
are owned by the firmware side (ACPI documents that callers must not
free them; OF points into the unflattened device tree), and
devm_ioremap_resource() duplicates the resource name, so the array
itself does not need to outlive the assignments.  Replace the heap
allocation with a stack array, which also removes the unchecked-NULL
kcalloc failure mode.

Fixes: 64b35ad ("lbc: phytium: Add uefi support for localbus controller driver")
Reported-by: GitHub Copilot:Code Review model
Assisted-by: Kimi Code:K3
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
The ACPI branch dereferences the struct resource returned by
platform_get_resource() ("res->name = ...") without checking it,
oopsing on a NULL pointer when the platform device does not carry
the expected memory resources.  The OF branch needs no such check
because devm_ioremap_resource() itself rejects a NULL resource.

Return -ENODEV when either resource is missing.

Fixes: 64b35ad ("lbc: phytium: Add uefi support for localbus controller driver")
Reported-by: GitHub Copilot:Code Review model
Assisted-by: Kimi Code:K3
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
@Avenger-285714

Copy link
Copy Markdown
Member Author

Hey - I've left some high level feedback:

  • In the ACPI path of phytium_lbc_probe, the return value of fwnode_property_read_string_array() is ignored; consider checking it and failing probe if reg-names are missing so you don't assign NULL names to resources.
  • phytium_lbc_probe still relies on 'res' when computing lbc->mm_size; ensure that in all early-return branches (including the new ENODEV paths) 'res' is either valid or not used afterward to avoid any accidental use of an uninitialized/incorrect resource.

胡说八道

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants