Commit 93e4fe64 authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab

edac: rewrite edac_align_ptr()

The edac_align_ptr() function is used to prepare data for a single
memory allocation kzalloc() call. It counts how many bytes are needed
by some data structure.

Using it as-is is not that trivial, as the quantity of memory elements
reserved is not there, but, instead, it is on a next call.

In order to avoid mistakes when using it, move the number of allocated
elements into it, making easier to use it.
Reviewed-by: default avatarBorislav Petkov <bp@amd64.org>
Cc: Aristeu Rozanski <arozansk@redhat.com>
Cc: Doug Thompson <norsk5@yahoo.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent a895bf8b
...@@ -79,7 +79,7 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info( ...@@ -79,7 +79,7 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info(
unsigned total_size; unsigned total_size;
unsigned count; unsigned count;
unsigned instance, block, attr; unsigned instance, block, attr;
void *pvt; void *pvt, *p;
int err; int err;
debugf4("%s() instances=%d blocks=%d\n", debugf4("%s() instances=%d blocks=%d\n",
...@@ -92,35 +92,30 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info( ...@@ -92,35 +92,30 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info(
* to be at least as stringent as what the compiler would * to be at least as stringent as what the compiler would
* provide if we could simply hardcode everything into a single struct. * provide if we could simply hardcode everything into a single struct.
*/ */
dev_ctl = (struct edac_device_ctl_info *)NULL; p = NULL;
dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
/* Calc the 'end' offset past end of ONE ctl_info structure /* Calc the 'end' offset past end of ONE ctl_info structure
* which will become the start of the 'instance' array * which will become the start of the 'instance' array
*/ */
dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst)); dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
/* Calc the 'end' offset past the instance array within the ctl_info /* Calc the 'end' offset past the instance array within the ctl_info
* which will become the start of the block array * which will become the start of the block array
*/ */
dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk)); count = nr_instances * nr_blocks;
dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
/* Calc the 'end' offset past the dev_blk array /* Calc the 'end' offset past the dev_blk array
* which will become the start of the attrib array, if any. * which will become the start of the attrib array, if any.
*/ */
count = nr_instances * nr_blocks; /* calc how many nr_attrib we need */
dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib)); if (nr_attrib > 0)
/* Check for case of when an attribute array is specified */
if (nr_attrib > 0) {
/* calc how many nr_attrib we need */
count *= nr_attrib; count *= nr_attrib;
dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
/* Calc the 'end' offset past the attributes array */ /* Calc the 'end' offset past the attributes array */
pvt = edac_align_ptr(&dev_attrib[count], sz_private); pvt = edac_align_ptr(&p, sz_private, 1);
} else {
/* no attribute array specificed */
pvt = edac_align_ptr(dev_attrib, sz_private);
}
/* 'pvt' now points to where the private data area is. /* 'pvt' now points to where the private data area is.
* At this point 'pvt' (like dev_inst,dev_blk and dev_attrib) * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
......
...@@ -101,18 +101,37 @@ const char *edac_mem_types[] = { ...@@ -101,18 +101,37 @@ const char *edac_mem_types[] = {
}; };
EXPORT_SYMBOL_GPL(edac_mem_types); EXPORT_SYMBOL_GPL(edac_mem_types);
/* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'. /**
* Adjust 'ptr' so that its alignment is at least as stringent as what the * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation
* compiler would provide for X and return the aligned result. * @p: pointer to a pointer with the memory offset to be used. At
* return, this will be incremented to point to the next offset
* @size: Size of the data structure to be reserved
* @n_elems: Number of elements that should be reserved
* *
* If 'size' is a constant, the compiler will optimize this whole function * If 'size' is a constant, the compiler will optimize this whole function
* down to either a no-op or the addition of a constant to the value of 'ptr'. * down to either a no-op or the addition of a constant to the value of '*p'.
*
* The 'p' pointer is absolutely needed to keep the proper advancing
* further in memory to the proper offsets when allocating the struct along
* with its embedded structs, as edac_device_alloc_ctl_info() does it
* above, for example.
*
* At return, the pointer 'p' will be incremented to be used on a next call
* to this function.
*/ */
void *edac_align_ptr(void *ptr, unsigned size) void *edac_align_ptr(void **p, unsigned size, int n_elems)
{ {
unsigned align, r; unsigned align, r;
void *ptr = *p;
/* Here we assume that the alignment of a "long long" is the most *p += size * n_elems;
/*
* 'p' can possibly be an unaligned item X such that sizeof(X) is
* 'size'. Adjust 'p' so that its alignment is at least as
* stringent as what the compiler would provide for X and return
* the aligned result.
* Here we assume that the alignment of a "long long" is the most
* stringent alignment that the compiler will ever provide by default. * stringent alignment that the compiler will ever provide by default.
* As far as I know, this is a reasonable assumption. * As far as I know, this is a reasonable assumption.
*/ */
...@@ -132,6 +151,8 @@ void *edac_align_ptr(void *ptr, unsigned size) ...@@ -132,6 +151,8 @@ void *edac_align_ptr(void *ptr, unsigned size)
if (r == 0) if (r == 0)
return (char *)ptr; return (char *)ptr;
*p += align - r;
return (void *)(((unsigned long)ptr) + align - r); return (void *)(((unsigned long)ptr) + align - r);
} }
...@@ -154,6 +175,7 @@ void *edac_align_ptr(void *ptr, unsigned size) ...@@ -154,6 +175,7 @@ void *edac_align_ptr(void *ptr, unsigned size)
struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows, struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
unsigned nr_chans, int edac_index) unsigned nr_chans, int edac_index)
{ {
void *ptr = NULL;
struct mem_ctl_info *mci; struct mem_ctl_info *mci;
struct csrow_info *csi, *csrow; struct csrow_info *csi, *csrow;
struct rank_info *chi, *chp, *chan; struct rank_info *chi, *chp, *chan;
...@@ -168,11 +190,11 @@ struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows, ...@@ -168,11 +190,11 @@ struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
* stringent as what the compiler would provide if we could simply * stringent as what the compiler would provide if we could simply
* hardcode everything into a single struct. * hardcode everything into a single struct.
*/ */
mci = (struct mem_ctl_info *)0; mci = edac_align_ptr(&ptr, sizeof(*mci), 1);
csi = edac_align_ptr(&mci[1], sizeof(*csi)); csi = edac_align_ptr(&ptr, sizeof(*csi), nr_csrows);
chi = edac_align_ptr(&csi[nr_csrows], sizeof(*chi)); chi = edac_align_ptr(&ptr, sizeof(*chi), nr_csrows * nr_chans);
dimm = edac_align_ptr(&chi[nr_chans * nr_csrows], sizeof(*dimm)); dimm = edac_align_ptr(&ptr, sizeof(*dimm), nr_csrows * nr_chans);
pvt = edac_align_ptr(&dimm[nr_chans * nr_csrows], sz_pvt); pvt = edac_align_ptr(&ptr, sz_pvt, 1);
size = ((unsigned long)pvt) + sz_pvt; size = ((unsigned long)pvt) + sz_pvt;
mci = kzalloc(size, GFP_KERNEL); mci = kzalloc(size, GFP_KERNEL);
......
...@@ -50,7 +50,7 @@ extern void edac_device_reset_delay_period(struct edac_device_ctl_info ...@@ -50,7 +50,7 @@ extern void edac_device_reset_delay_period(struct edac_device_ctl_info
*edac_dev, unsigned long value); *edac_dev, unsigned long value);
extern void edac_mc_reset_delay_period(int value); extern void edac_mc_reset_delay_period(int value);
extern void *edac_align_ptr(void *ptr, unsigned size); extern void *edac_align_ptr(void **p, unsigned size, int n_elems);
/* /*
* EDAC PCI functions * EDAC PCI functions
......
...@@ -42,13 +42,13 @@ struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt, ...@@ -42,13 +42,13 @@ struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
const char *edac_pci_name) const char *edac_pci_name)
{ {
struct edac_pci_ctl_info *pci; struct edac_pci_ctl_info *pci;
void *pvt; void *p = NULL, *pvt;
unsigned int size; unsigned int size;
debugf1("%s()\n", __func__); debugf1("%s()\n", __func__);
pci = (struct edac_pci_ctl_info *)0; pci = edac_align_ptr(&p, sizeof(*pci), 1);
pvt = edac_align_ptr(&pci[1], sz_pvt); pvt = edac_align_ptr(&p, 1, sz_pvt);
size = ((unsigned long)pvt) + sz_pvt; size = ((unsigned long)pvt) + sz_pvt;
/* Alloc the needed control struct memory */ /* Alloc the needed control struct memory */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment