diff mbox series

[v2,2/8] s390/cio: introduce DMA pools to cio

Message ID 20190523162209.9543-3-mimu@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series s390: virtio: support protected virtualization | expand

Commit Message

Michael Mueller May 23, 2019, 4:22 p.m. UTC
From: Halil Pasic <pasic@linux.ibm.com>

To support protected virtualization cio will need to make sure the
memory used for communication with the hypervisor is DMA memory.

Let us introduce one global cio, and some tools for pools seated
at individual devices.

Our DMA pools are implemented as a gen_pool backed with DMA pages. The
idea is to avoid each allocation effectively wasting a page, as we
typically allocate much less than PAGE_SIZE.

Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
---
 arch/s390/Kconfig           |   1 +
 arch/s390/include/asm/cio.h |  11 +++++
 drivers/s390/cio/css.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)

Comments

Sebastian Ott May 25, 2019, 9:22 a.m. UTC | #1
On Thu, 23 May 2019, Michael Mueller wrote:
> +static void __init cio_dma_pool_init(void)
> +{
> +	/* No need to free up the resources: compiled in */
> +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);

This can return NULL.

> +/**
> + * Allocate dma memory from the css global pool. Intended for memory not
> + * specific to any single device within the css. The allocated memory
> + * is not guaranteed to be 31-bit addressable.
> + *
> + * Caution: Not suitable for early stuff like console.
> + *
> + */

drivers/s390/cio/css.c:1121: warning: Function parameter or member 'size' not described in 'cio_dma_zalloc'

Reviewed-by: Sebastian Ott <sebott@linux.ibm.com>
Cornelia Huck May 27, 2019, 6:57 a.m. UTC | #2
On Thu, 23 May 2019 18:22:03 +0200
Michael Mueller <mimu@linux.ibm.com> wrote:

> From: Halil Pasic <pasic@linux.ibm.com>
> 
> To support protected virtualization cio will need to make sure the
> memory used for communication with the hypervisor is DMA memory.
> 
> Let us introduce one global cio, and some tools for pools seated

"one global pool for cio"?

> at individual devices.
> 
> Our DMA pools are implemented as a gen_pool backed with DMA pages. The
> idea is to avoid each allocation effectively wasting a page, as we
> typically allocate much less than PAGE_SIZE.
> 
> Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
> ---
>  arch/s390/Kconfig           |   1 +
>  arch/s390/include/asm/cio.h |  11 +++++
>  drivers/s390/cio/css.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 122 insertions(+)
> 

(...)

> @@ -1018,6 +1024,109 @@ static struct notifier_block css_power_notifier = {
>  	.notifier_call = css_power_event,
>  };
>  
> +#define POOL_INIT_PAGES 1
> +static struct gen_pool *cio_dma_pool;
> +/* Currently cio supports only a single css */

This comment looks misplaced.

> +#define  CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
> +
> +
> +struct device *cio_get_dma_css_dev(void)
> +{
> +	return &channel_subsystems[0]->device;
> +}
> +
> +struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
> +{
> +	struct gen_pool *gp_dma;
> +	void *cpu_addr;
> +	dma_addr_t dma_addr;
> +	int i;
> +
> +	gp_dma = gen_pool_create(3, -1);
> +	if (!gp_dma)
> +		return NULL;
> +	for (i = 0; i < nr_pages; ++i) {
> +		cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
> +					      CIO_DMA_GFP);
> +		if (!cpu_addr)
> +			return gp_dma;

So, you may return here with no memory added to the pool at all (or
less than requested), but for the caller that is indistinguishable from
an allocation that went all right. May that be a problem?

> +		gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
> +				  dma_addr, PAGE_SIZE, -1);
> +	}
> +	return gp_dma;
> +}
> +

(...)

> +static void __init cio_dma_pool_init(void)
> +{
> +	/* No need to free up the resources: compiled in */
> +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);

Does it make sense to continue if you did not get a pool here? I don't
think that should happen unless things were really bad already?

> +}
> +
> +void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
> +			size_t size)
> +{
> +	dma_addr_t dma_addr;
> +	unsigned long addr;
> +	size_t chunk_size;
> +
> +	addr = gen_pool_alloc(gp_dma, size);
> +	while (!addr) {
> +		chunk_size = round_up(size, PAGE_SIZE);
> +		addr = (unsigned long) dma_alloc_coherent(dma_dev,
> +					 chunk_size, &dma_addr, CIO_DMA_GFP);
> +		if (!addr)
> +			return NULL;
> +		gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
> +		addr = gen_pool_alloc(gp_dma, size);
> +	}
> +	return (void *) addr;
> +}
> +
> +void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
> +{
> +	if (!cpu_addr)
> +		return;
> +	memset(cpu_addr, 0, size);
> +	gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
> +}
> +
> +/**
> + * Allocate dma memory from the css global pool. Intended for memory not
> + * specific to any single device within the css. The allocated memory
> + * is not guaranteed to be 31-bit addressable.
> + *
> + * Caution: Not suitable for early stuff like console.
> + *
> + */
> +void *cio_dma_zalloc(size_t size)
> +{
> +	return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);

Ok, that looks like the failure I mentioned above should be
accommodated by the code. Still, I think it's a bit odd.

> +}
Michael Mueller May 27, 2019, 11:26 a.m. UTC | #3
On 25.05.19 11:22, Sebastian Ott wrote:
> 
> On Thu, 23 May 2019, Michael Mueller wrote:
>> +static void __init cio_dma_pool_init(void)
>> +{
>> +	/* No need to free up the resources: compiled in */
>> +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
> 
> This can return NULL.

css_bus_init() will fail with -ENOMEM in v3

> 
>> +/**
>> + * Allocate dma memory from the css global pool. Intended for memory not
>> + * specific to any single device within the css. The allocated memory
>> + * is not guaranteed to be 31-bit addressable.
>> + *
>> + * Caution: Not suitable for early stuff like console.
>> + *
>> + */
> 
> drivers/s390/cio/css.c:1121: warning: Function parameter or member 'size' not described in 'cio_dma_zalloc'

will complete param description in v3

> 
> Reviewed-by: Sebastian Ott <sebott@linux.ibm.com>

Thanks!

> 

Michael
Halil Pasic May 27, 2019, 11:47 a.m. UTC | #4
On Mon, 27 May 2019 08:57:18 +0200
Cornelia Huck <cohuck@redhat.com> wrote:

> On Thu, 23 May 2019 18:22:03 +0200
> Michael Mueller <mimu@linux.ibm.com> wrote:
> 
> > From: Halil Pasic <pasic@linux.ibm.com>
> > 
> > To support protected virtualization cio will need to make sure the
> > memory used for communication with the hypervisor is DMA memory.
> > 
> > Let us introduce one global cio, and some tools for pools seated
> 
> "one global pool for cio"?
> 

Nod.

> > at individual devices.
> > 
> > Our DMA pools are implemented as a gen_pool backed with DMA pages. The
> > idea is to avoid each allocation effectively wasting a page, as we
> > typically allocate much less than PAGE_SIZE.
> > 
> > Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
> > ---
> >  arch/s390/Kconfig           |   1 +
> >  arch/s390/include/asm/cio.h |  11 +++++
> >  drivers/s390/cio/css.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 122 insertions(+)
> > 
> 
> (...)
> 
> > @@ -1018,6 +1024,109 @@ static struct notifier_block css_power_notifier = {
> >  	.notifier_call = css_power_event,
> >  };
> >  
> > +#define POOL_INIT_PAGES 1
> > +static struct gen_pool *cio_dma_pool;
> > +/* Currently cio supports only a single css */
> 
> This comment looks misplaced.

Right! Move to ...

> 
> > +#define  CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
> > +
> > +

... here?

> > +struct device *cio_get_dma_css_dev(void)
> > +{
> > +	return &channel_subsystems[0]->device;
> > +}
> > +
> > +struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
> > +{
> > +	struct gen_pool *gp_dma;
> > +	void *cpu_addr;
> > +	dma_addr_t dma_addr;
> > +	int i;
> > +
> > +	gp_dma = gen_pool_create(3, -1);
> > +	if (!gp_dma)
> > +		return NULL;
> > +	for (i = 0; i < nr_pages; ++i) {
> > +		cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
> > +					      CIO_DMA_GFP);
> > +		if (!cpu_addr)
> > +			return gp_dma;
> 
> So, you may return here with no memory added to the pool at all (or
> less than requested), but for the caller that is indistinguishable from
> an allocation that went all right. May that be a problem?
> 

I do not think it can cause a problem: cio_gp_dma_zalloc() is going to
try to allocate the memory required and put it in the pool. If that
fails as well, we return a NULL pointer like kmalloc(). So I think we
are clean.

> > +		gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
> > +				  dma_addr, PAGE_SIZE, -1);
> > +	}
> > +	return gp_dma;
> > +}
> > +
> 
> (...)
> 
> > +static void __init cio_dma_pool_init(void)
> > +{
> > +	/* No need to free up the resources: compiled in */
> > +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
> 
> Does it make sense to continue if you did not get a pool here? I don't
> think that should happen unless things were really bad already?
> 

I agree, this should not fail under any sane circumstances. I don't
think it makes sense to continue. Shall we simply call panic()?

> > +}
> > +
> > +void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
> > +			size_t size)
> > +{
> > +	dma_addr_t dma_addr;
> > +	unsigned long addr;
> > +	size_t chunk_size;
> > +
> > +	addr = gen_pool_alloc(gp_dma, size);
> > +	while (!addr) {
> > +		chunk_size = round_up(size, PAGE_SIZE);
> > +		addr = (unsigned long) dma_alloc_coherent(dma_dev,
> > +					 chunk_size, &dma_addr, CIO_DMA_GFP);
> > +		if (!addr)
> > +			return NULL;
> > +		gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
> > +		addr = gen_pool_alloc(gp_dma, size);
> > +	}
> > +	return (void *) addr;
> > +}
> > +
> > +void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
> > +{
> > +	if (!cpu_addr)
> > +		return;
> > +	memset(cpu_addr, 0, size);
> > +	gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
> > +}
> > +
> > +/**
> > + * Allocate dma memory from the css global pool. Intended for memory not
> > + * specific to any single device within the css. The allocated memory
> > + * is not guaranteed to be 31-bit addressable.
> > + *
> > + * Caution: Not suitable for early stuff like console.
> > + *
> > + */
> > +void *cio_dma_zalloc(size_t size)
> > +{
> > +	return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
> 
> Ok, that looks like the failure I mentioned above should be
> accommodated by the code. Still, I think it's a bit odd.
> 

I think the behavior is reasonable: if client code wants pre-allocate n
page sized chunks we pre-allocate as may as we can. If we can't
pre-allocate all n, it ain't necessarily bad. There is no guarantee we
will hit a wall in a non-recoverable fashion.

But if you insist, I can get rid of the pre-allocation or fail create and
do a rollback if it fails.

Thanks for having a look!

Regards,
Halil

> > +}
>
Michael Mueller May 27, 2019, noon UTC | #5
On 27.05.19 08:57, Cornelia Huck wrote:
> On Thu, 23 May 2019 18:22:03 +0200
> Michael Mueller <mimu@linux.ibm.com> wrote:
> 
>> From: Halil Pasic <pasic@linux.ibm.com>
>>
>> To support protected virtualization cio will need to make sure the
>> memory used for communication with the hypervisor is DMA memory.
>>
>> Let us introduce one global cio, and some tools for pools seated
> 
> "one global pool for cio"?

changed in v3

> 
>> at individual devices.
>>
>> Our DMA pools are implemented as a gen_pool backed with DMA pages. The
>> idea is to avoid each allocation effectively wasting a page, as we
>> typically allocate much less than PAGE_SIZE.
>>
>> Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
>> ---
>>   arch/s390/Kconfig           |   1 +
>>   arch/s390/include/asm/cio.h |  11 +++++
>>   drivers/s390/cio/css.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 122 insertions(+)
>>
> 
> (...)
> 
>> @@ -1018,6 +1024,109 @@ static struct notifier_block css_power_notifier = {
>>   	.notifier_call = css_power_event,
>>   };
>>   
>> +#define POOL_INIT_PAGES 1
>> +static struct gen_pool *cio_dma_pool;
>> +/* Currently cio supports only a single css */
> 
> This comment looks misplaced.

gone in v3

> 
>> +#define  CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
>> +
>> +
>> +struct device *cio_get_dma_css_dev(void)
>> +{
>> +	return &channel_subsystems[0]->device;
>> +}
>> +
>> +struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
>> +{
>> +	struct gen_pool *gp_dma;
>> +	void *cpu_addr;
>> +	dma_addr_t dma_addr;
>> +	int i;
>> +
>> +	gp_dma = gen_pool_create(3, -1);
>> +	if (!gp_dma)
>> +		return NULL;
>> +	for (i = 0; i < nr_pages; ++i) {
>> +		cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
>> +					      CIO_DMA_GFP);
>> +		if (!cpu_addr)
>> +			return gp_dma;
> 
> So, you may return here with no memory added to the pool at all (or
> less than requested), but for the caller that is indistinguishable from
> an allocation that went all right. May that be a problem?

Halil,

can you pls. bring some light into the intention of this part of
the code. To me this seems to be odd as well!
Currently cio_gp_dma_create() might succeed with a successful
gen_pool_create() and an initially failing dma_alloc_coherent().

> 
>> +		gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
>> +				  dma_addr, PAGE_SIZE, -1);
>> +	}
>> +	return gp_dma;
>> +}
>> +
> 
> (...)
> 
>> +static void __init cio_dma_pool_init(void)
>> +{
>> +	/* No need to free up the resources: compiled in */
>> +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
> 
> Does it make sense to continue if you did not get a pool here? I don't
> think that should happen unless things were really bad already?

cio_gp_dma_create() will be evaluated and css_bus_init() will fail
in v3 in the NULL case.

> 
>> +}
>> +
>> +void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
>> +			size_t size)
>> +{
>> +	dma_addr_t dma_addr;
>> +	unsigned long addr;
>> +	size_t chunk_size;
>> +
>> +	addr = gen_pool_alloc(gp_dma, size);
>> +	while (!addr) {
>> +		chunk_size = round_up(size, PAGE_SIZE);
>> +		addr = (unsigned long) dma_alloc_coherent(dma_dev,
>> +					 chunk_size, &dma_addr, CIO_DMA_GFP);
>> +		if (!addr)
>> +			return NULL;
>> +		gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
>> +		addr = gen_pool_alloc(gp_dma, size);
>> +	}
>> +	return (void *) addr;
>> +}
>> +
>> +void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
>> +{
>> +	if (!cpu_addr)
>> +		return;
>> +	memset(cpu_addr, 0, size);
>> +	gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
>> +}
>> +
>> +/**
>> + * Allocate dma memory from the css global pool. Intended for memory not
>> + * specific to any single device within the css. The allocated memory
>> + * is not guaranteed to be 31-bit addressable.
>> + *
>> + * Caution: Not suitable for early stuff like console.
>> + *
>> + */
>> +void *cio_dma_zalloc(size_t size)
>> +{
>> +	return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
> 
> Ok, that looks like the failure I mentioned above should be
> accommodated by the code. Still, I think it's a bit odd.

This code will be reached in v3 only when cio_dma_pool is *not* NULL.

> 
>> +}
> 

Michael
Cornelia Huck May 27, 2019, 12:06 p.m. UTC | #6
On Mon, 27 May 2019 13:47:55 +0200
Halil Pasic <pasic@linux.ibm.com> wrote:

> On Mon, 27 May 2019 08:57:18 +0200
> Cornelia Huck <cohuck@redhat.com> wrote:
> 
> > On Thu, 23 May 2019 18:22:03 +0200
> > Michael Mueller <mimu@linux.ibm.com> wrote:
> >   
> > > From: Halil Pasic <pasic@linux.ibm.com>
> > > 
> > > To support protected virtualization cio will need to make sure the
> > > memory used for communication with the hypervisor is DMA memory.
> > > 
> > > Let us introduce one global cio, and some tools for pools seated  
> > 
> > "one global pool for cio"?
> >   
> 
> Nod.
> 
> > > at individual devices.
> > > 
> > > Our DMA pools are implemented as a gen_pool backed with DMA pages. The
> > > idea is to avoid each allocation effectively wasting a page, as we
> > > typically allocate much less than PAGE_SIZE.
> > > 
> > > Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
> > > ---
> > >  arch/s390/Kconfig           |   1 +
> > >  arch/s390/include/asm/cio.h |  11 +++++
> > >  drivers/s390/cio/css.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
> > >  3 files changed, 122 insertions(+)
> > >   
> > 
> > (...)
> >   
> > > @@ -1018,6 +1024,109 @@ static struct notifier_block css_power_notifier = {
> > >  	.notifier_call = css_power_event,
> > >  };
> > >  
> > > +#define POOL_INIT_PAGES 1
> > > +static struct gen_pool *cio_dma_pool;
> > > +/* Currently cio supports only a single css */  
> > 
> > This comment looks misplaced.  
> 
> Right! Move to ...
> 
> >   
> > > +#define  CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
> > > +
> > > +  
> 
> ... here?

Yes :)

> 
> > > +struct device *cio_get_dma_css_dev(void)
> > > +{
> > > +	return &channel_subsystems[0]->device;
> > > +}
> > > +
> > > +struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
> > > +{
> > > +	struct gen_pool *gp_dma;
> > > +	void *cpu_addr;
> > > +	dma_addr_t dma_addr;
> > > +	int i;
> > > +
> > > +	gp_dma = gen_pool_create(3, -1);
> > > +	if (!gp_dma)
> > > +		return NULL;
> > > +	for (i = 0; i < nr_pages; ++i) {
> > > +		cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
> > > +					      CIO_DMA_GFP);
> > > +		if (!cpu_addr)
> > > +			return gp_dma;  
> > 
> > So, you may return here with no memory added to the pool at all (or
> > less than requested), but for the caller that is indistinguishable from
> > an allocation that went all right. May that be a problem?
> >   
> 
> I do not think it can cause a problem: cio_gp_dma_zalloc() is going to
> try to allocate the memory required and put it in the pool. If that
> fails as well, we return a NULL pointer like kmalloc(). So I think we
> are clean.
> 
> > > +		gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
> > > +				  dma_addr, PAGE_SIZE, -1);
> > > +	}
> > > +	return gp_dma;
> > > +}
> > > +  
> > 
> > (...)
> >   
> > > +static void __init cio_dma_pool_init(void)
> > > +{
> > > +	/* No need to free up the resources: compiled in */
> > > +	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);  
> > 
> > Does it make sense to continue if you did not get a pool here? I don't
> > think that should happen unless things were really bad already?
> >   
> 
> I agree, this should not fail under any sane circumstances. I don't
> think it makes sense to continue. Shall we simply call panic()?

Can we continue without the common I/O layer? Probably not. It might
really be an 'oh crap, let's panic' situation.

> 
> > > +}
> > > +
> > > +void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
> > > +			size_t size)
> > > +{
> > > +	dma_addr_t dma_addr;
> > > +	unsigned long addr;
> > > +	size_t chunk_size;
> > > +
> > > +	addr = gen_pool_alloc(gp_dma, size);
> > > +	while (!addr) {
> > > +		chunk_size = round_up(size, PAGE_SIZE);
> > > +		addr = (unsigned long) dma_alloc_coherent(dma_dev,
> > > +					 chunk_size, &dma_addr, CIO_DMA_GFP);
> > > +		if (!addr)
> > > +			return NULL;
> > > +		gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
> > > +		addr = gen_pool_alloc(gp_dma, size);
> > > +	}
> > > +	return (void *) addr;
> > > +}
> > > +
> > > +void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
> > > +{
> > > +	if (!cpu_addr)
> > > +		return;
> > > +	memset(cpu_addr, 0, size);
> > > +	gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
> > > +}
> > > +
> > > +/**
> > > + * Allocate dma memory from the css global pool. Intended for memory not
> > > + * specific to any single device within the css. The allocated memory
> > > + * is not guaranteed to be 31-bit addressable.
> > > + *
> > > + * Caution: Not suitable for early stuff like console.
> > > + *
> > > + */
> > > +void *cio_dma_zalloc(size_t size)
> > > +{
> > > +	return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);  
> > 
> > Ok, that looks like the failure I mentioned above should be
> > accommodated by the code. Still, I think it's a bit odd.
> >   
> 
> I think the behavior is reasonable: if client code wants pre-allocate n
> page sized chunks we pre-allocate as may as we can. If we can't
> pre-allocate all n, it ain't necessarily bad. There is no guarantee we
> will hit a wall in a non-recoverable fashion.

It's not necessarily broken, but there are two things that feel a bit
weird to me:
- The caller doesn't know if the requested pre-allocation worked or not.
- If we can't get memory in this early init phase, is it likely that we
  can get memory later on?

> 
> But if you insist, I can get rid of the pre-allocation or fail create and
> do a rollback if it fails.
> 
> Thanks for having a look!
> 
> Regards,
> Halil
> 
> > > +}  
> >   
>
diff mbox series

Patch

diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index 88d8355b7bf7..2a245b56db8b 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -191,6 +191,7 @@  config S390
 	select ARCH_HAS_SCALED_CPUTIME
 	select HAVE_NMI
 	select SWIOTLB
+	select GENERIC_ALLOCATOR
 
 
 config SCHED_OMIT_FRAME_POINTER
diff --git a/arch/s390/include/asm/cio.h b/arch/s390/include/asm/cio.h
index 1727180e8ca1..43c007d2775a 100644
--- a/arch/s390/include/asm/cio.h
+++ b/arch/s390/include/asm/cio.h
@@ -328,6 +328,17 @@  static inline u8 pathmask_to_pos(u8 mask)
 void channel_subsystem_reinit(void);
 extern void css_schedule_reprobe(void);
 
+extern void *cio_dma_zalloc(size_t size);
+extern void cio_dma_free(void *cpu_addr, size_t size);
+extern struct device *cio_get_dma_css_dev(void);
+
+struct gen_pool;
+void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
+			size_t size);
+void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size);
+void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev);
+struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages);
+
 /* Function from drivers/s390/cio/chsc.c */
 int chsc_sstpc(void *page, unsigned int op, u16 ctrl, u64 *clock_delta);
 int chsc_sstpi(void *page, void *result, size_t size);
diff --git a/drivers/s390/cio/css.c b/drivers/s390/cio/css.c
index aea502922646..789f6ecdbbcc 100644
--- a/drivers/s390/cio/css.c
+++ b/drivers/s390/cio/css.c
@@ -20,6 +20,8 @@ 
 #include <linux/reboot.h>
 #include <linux/suspend.h>
 #include <linux/proc_fs.h>
+#include <linux/genalloc.h>
+#include <linux/dma-mapping.h>
 #include <asm/isc.h>
 #include <asm/crw.h>
 
@@ -224,6 +226,8 @@  struct subchannel *css_alloc_subchannel(struct subchannel_id schid,
 	INIT_WORK(&sch->todo_work, css_sch_todo);
 	sch->dev.release = &css_subchannel_release;
 	device_initialize(&sch->dev);
+	sch->dev.coherent_dma_mask = DMA_BIT_MASK(31);
+	sch->dev.dma_mask = &sch->dev.coherent_dma_mask;
 	return sch;
 
 err:
@@ -899,6 +903,8 @@  static int __init setup_css(int nr)
 	dev_set_name(&css->device, "css%x", nr);
 	css->device.groups = cssdev_attr_groups;
 	css->device.release = channel_subsystem_release;
+	css->device.coherent_dma_mask = DMA_BIT_MASK(64);
+	css->device.dma_mask = &css->device.coherent_dma_mask;
 
 	mutex_init(&css->mutex);
 	css->cssid = chsc_get_cssid(nr);
@@ -1018,6 +1024,109 @@  static struct notifier_block css_power_notifier = {
 	.notifier_call = css_power_event,
 };
 
+#define POOL_INIT_PAGES 1
+static struct gen_pool *cio_dma_pool;
+/* Currently cio supports only a single css */
+#define  CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO)
+
+
+struct device *cio_get_dma_css_dev(void)
+{
+	return &channel_subsystems[0]->device;
+}
+
+struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages)
+{
+	struct gen_pool *gp_dma;
+	void *cpu_addr;
+	dma_addr_t dma_addr;
+	int i;
+
+	gp_dma = gen_pool_create(3, -1);
+	if (!gp_dma)
+		return NULL;
+	for (i = 0; i < nr_pages; ++i) {
+		cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr,
+					      CIO_DMA_GFP);
+		if (!cpu_addr)
+			return gp_dma;
+		gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr,
+				  dma_addr, PAGE_SIZE, -1);
+	}
+	return gp_dma;
+}
+
+static void __gp_dma_free_dma(struct gen_pool *pool,
+			      struct gen_pool_chunk *chunk, void *data)
+{
+	size_t chunk_size = chunk->end_addr - chunk->start_addr + 1;
+
+	dma_free_coherent((struct device *) data, chunk_size,
+			 (void *) chunk->start_addr,
+			 (dma_addr_t) chunk->phys_addr);
+}
+
+void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev)
+{
+	if (!gp_dma)
+		return;
+	/* this is qite ugly but no better idea */
+	gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev);
+	gen_pool_destroy(gp_dma);
+}
+
+static void __init cio_dma_pool_init(void)
+{
+	/* No need to free up the resources: compiled in */
+	cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1);
+}
+
+void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev,
+			size_t size)
+{
+	dma_addr_t dma_addr;
+	unsigned long addr;
+	size_t chunk_size;
+
+	addr = gen_pool_alloc(gp_dma, size);
+	while (!addr) {
+		chunk_size = round_up(size, PAGE_SIZE);
+		addr = (unsigned long) dma_alloc_coherent(dma_dev,
+					 chunk_size, &dma_addr, CIO_DMA_GFP);
+		if (!addr)
+			return NULL;
+		gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1);
+		addr = gen_pool_alloc(gp_dma, size);
+	}
+	return (void *) addr;
+}
+
+void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size)
+{
+	if (!cpu_addr)
+		return;
+	memset(cpu_addr, 0, size);
+	gen_pool_free(gp_dma, (unsigned long) cpu_addr, size);
+}
+
+/**
+ * Allocate dma memory from the css global pool. Intended for memory not
+ * specific to any single device within the css. The allocated memory
+ * is not guaranteed to be 31-bit addressable.
+ *
+ * Caution: Not suitable for early stuff like console.
+ *
+ */
+void *cio_dma_zalloc(size_t size)
+{
+	return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size);
+}
+
+void cio_dma_free(void *cpu_addr, size_t size)
+{
+	cio_gp_dma_free(cio_dma_pool, cpu_addr, size);
+}
+
 /*
  * Now that the driver core is running, we can setup our channel subsystem.
  * The struct subchannel's are created during probing.
@@ -1063,6 +1172,7 @@  static int __init css_bus_init(void)
 		unregister_reboot_notifier(&css_reboot_notifier);
 		goto out_unregister;
 	}
+	cio_dma_pool_init();
 	css_init_done = 1;
 
 	/* Enable default isc for I/O subchannels. */