Re: [PATCH v4 1/3] lib: logic_pio: Use logical PIO low-level accessors for !CONFIG_INDIRECT_PIO

From: John Garry
Date: Thu Jun 13 2019 - 11:26:37 EST

On 13/06/2019 14:58, Bjorn Helgaas wrote:
On Tue, Jun 11, 2019 at 10:12:52PM +0800, John Garry wrote:
Another thought here:

if (addr < MMIO_UPPER_LIMIT) { \
ret = read##bw(PCI_IOBASE + addr); \
} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
- struct logic_pio_hwaddr *entry = find_io_range(addr); \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ size_t sz = sizeof(type); \
- if (entry && entry->ops) \
- ret = entry->ops->in(entry->hostdata, \
- addr, sizeof(type)); \
+ if (range && range->ops) \
+ ret = range->ops->in(range->hostdata, addr, sz);\
else \

Hi Bjorn,

Could this be simplified a little by requiring callers to set
range->ops for LOGIC_PIO_INDIRECT ranges *before* calling
logic_pio_register_range()? E.g.,

range = devm_kzalloc(...);
range->flags = LOGIC_PIO_INDIRECT;
range->ops = &hisi_lpc_ops;


logic_pio_register_range(struct logic_pio_hwaddr *new_range)
if (new_range->flags == LOGIC_PIO_INDIRECT && !new_range->ops)
return -EINVAL;

Then maybe you wouldn't need to check range->ops in the accessors.

I think I know the reason why it was done this way.

So currently there is no method to unregister a logical PIO region (the old code leaked ranges as well). As such, if hisi_lpc_probe() fails after we register the logical PIO range, there would be a range registered but no actual host backing it. So we set the ops at the point at which the probe cannot fail to avoid a potential problem.

And now I realise that there is a bug in the code - range is allocated with devm_kzalloc and is passed to logic_pio_register_range(). As such, if the hisi_lpc_probe() goes on to fail, then this memory would be free'd and we have an issue.

PCI code should be ok as it uses kzalloc().

The simplest solution is to not change the logical PIO API to allocate this memory itself, but rather make hisi_lpc_probe() use kzalloc(). And, if we go this way, we can use your idea to set the ops.

I'll spin a separate patch for this.