aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2012-04-16 09:18:12 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2012-05-28 18:10:59 -0400
commit93e4fe64ece4eccf0ff4ac69bceb389290b8ab7c (patch)
tree5794c15f0d2c47568e49379b56e9a8d139532050
parenta895bf8b1e1ea4c032a8fa8a09475a2ce09fe77a (diff)
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: Borislav Petkov <bp@amd64.org> Cc: Aristeu Rozanski <arozansk@redhat.com> Cc: Doug Thompson <norsk5@yahoo.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r--drivers/edac/edac_device.c27
-rw-r--r--drivers/edac/edac_mc.c44
-rw-r--r--drivers/edac/edac_module.h2
-rw-r--r--drivers/edac/edac_pci.c6
4 files changed, 48 insertions, 31 deletions
diff --git a/drivers/edac/edac_device.c b/drivers/edac/edac_device.c
index 4b154593343..cb397d9437d 100644
--- a/drivers/edac/edac_device.c
+++ b/drivers/edac/edac_device.c
@@ -79,7 +79,7 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info(
79 unsigned total_size; 79 unsigned total_size;
80 unsigned count; 80 unsigned count;
81 unsigned instance, block, attr; 81 unsigned instance, block, attr;
82 void *pvt; 82 void *pvt, *p;
83 int err; 83 int err;
84 84
85 debugf4("%s() instances=%d blocks=%d\n", 85 debugf4("%s() instances=%d blocks=%d\n",
@@ -92,35 +92,30 @@ struct edac_device_ctl_info *edac_device_alloc_ctl_info(
92 * to be at least as stringent as what the compiler would 92 * to be at least as stringent as what the compiler would
93 * provide if we could simply hardcode everything into a single struct. 93 * provide if we could simply hardcode everything into a single struct.
94 */ 94 */
95 dev_ctl = (struct edac_device_ctl_info *)NULL; 95 p = NULL;
96 dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
96 97
97 /* Calc the 'end' offset past end of ONE ctl_info structure 98 /* Calc the 'end' offset past end of ONE ctl_info structure
98 * which will become the start of the 'instance' array 99 * which will become the start of the 'instance' array
99 */ 100 */
100 dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst)); 101 dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
101 102
102 /* Calc the 'end' offset past the instance array within the ctl_info 103 /* Calc the 'end' offset past the instance array within the ctl_info
103 * which will become the start of the block array 104 * which will become the start of the block array
104 */ 105 */
105 dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk)); 106 count = nr_instances * nr_blocks;
107 dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
106 108
107 /* Calc the 'end' offset past the dev_blk array 109 /* Calc the 'end' offset past the dev_blk array
108 * which will become the start of the attrib array, if any. 110 * which will become the start of the attrib array, if any.
109 */ 111 */
110 count = nr_instances * nr_blocks; 112 /* calc how many nr_attrib we need */
111 dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib)); 113 if (nr_attrib > 0)
112
113 /* Check for case of when an attribute array is specified */
114 if (nr_attrib > 0) {
115 /* calc how many nr_attrib we need */
116 count *= nr_attrib; 114 count *= nr_attrib;
115 dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
117 116
118 /* Calc the 'end' offset past the attributes array */ 117 /* Calc the 'end' offset past the attributes array */
119 pvt = edac_align_ptr(&dev_attrib[count], sz_private); 118 pvt = edac_align_ptr(&p, sz_private, 1);
120 } else {
121 /* no attribute array specificed */
122 pvt = edac_align_ptr(dev_attrib, sz_private);
123 }
124 119
125 /* 'pvt' now points to where the private data area is. 120 /* 'pvt' now points to where the private data area is.
126 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib) 121 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
index 072aa81b4a7..ff8c0020649 100644
--- a/drivers/edac/edac_mc.c
+++ b/drivers/edac/edac_mc.c
@@ -101,18 +101,37 @@ const char *edac_mem_types[] = {
101}; 101};
102EXPORT_SYMBOL_GPL(edac_mem_types); 102EXPORT_SYMBOL_GPL(edac_mem_types);
103 103
104/* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'. 104/**
105 * Adjust 'ptr' so that its alignment is at least as stringent as what the 105 * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation
106 * compiler would provide for X and return the aligned result. 106 * @p: pointer to a pointer with the memory offset to be used. At
107 * return, this will be incremented to point to the next offset
108 * @size: Size of the data structure to be reserved
109 * @n_elems: Number of elements that should be reserved
107 * 110 *
108 * If 'size' is a constant, the compiler will optimize this whole function 111 * If 'size' is a constant, the compiler will optimize this whole function
109 * down to either a no-op or the addition of a constant to the value of 'ptr'. 112 * down to either a no-op or the addition of a constant to the value of '*p'.
113 *
114 * The 'p' pointer is absolutely needed to keep the proper advancing
115 * further in memory to the proper offsets when allocating the struct along
116 * with its embedded structs, as edac_device_alloc_ctl_info() does it
117 * above, for example.
118 *
119 * At return, the pointer 'p' will be incremented to be used on a next call
120 * to this function.
110 */ 121 */
111void *edac_align_ptr(void *ptr, unsigned size) 122void *edac_align_ptr(void **p, unsigned size, int n_elems)
112{ 123{
113 unsigned align, r; 124 unsigned align, r;
125 void *ptr = *p;
114 126
115 /* Here we assume that the alignment of a "long long" is the most 127 *p += size * n_elems;
128
129 /*
130 * 'p' can possibly be an unaligned item X such that sizeof(X) is
131 * 'size'. Adjust 'p' so that its alignment is at least as
132 * stringent as what the compiler would provide for X and return
133 * the aligned result.
134 * Here we assume that the alignment of a "long long" is the most
116 * stringent alignment that the compiler will ever provide by default. 135 * stringent alignment that the compiler will ever provide by default.
117 * As far as I know, this is a reasonable assumption. 136 * As far as I know, this is a reasonable assumption.
118 */ 137 */
@@ -132,6 +151,8 @@ void *edac_align_ptr(void *ptr, unsigned size)
132 if (r == 0) 151 if (r == 0)
133 return (char *)ptr; 152 return (char *)ptr;
134 153
154 *p += align - r;
155
135 return (void *)(((unsigned long)ptr) + align - r); 156 return (void *)(((unsigned long)ptr) + align - r);
136} 157}
137 158
@@ -154,6 +175,7 @@ void *edac_align_ptr(void *ptr, unsigned size)
154struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows, 175struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
155 unsigned nr_chans, int edac_index) 176 unsigned nr_chans, int edac_index)
156{ 177{
178 void *ptr = NULL;
157 struct mem_ctl_info *mci; 179 struct mem_ctl_info *mci;
158 struct csrow_info *csi, *csrow; 180 struct csrow_info *csi, *csrow;
159 struct rank_info *chi, *chp, *chan; 181 struct rank_info *chi, *chp, *chan;
@@ -168,11 +190,11 @@ struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
168 * stringent as what the compiler would provide if we could simply 190 * stringent as what the compiler would provide if we could simply
169 * hardcode everything into a single struct. 191 * hardcode everything into a single struct.
170 */ 192 */
171 mci = (struct mem_ctl_info *)0; 193 mci = edac_align_ptr(&ptr, sizeof(*mci), 1);
172 csi = edac_align_ptr(&mci[1], sizeof(*csi)); 194 csi = edac_align_ptr(&ptr, sizeof(*csi), nr_csrows);
173 chi = edac_align_ptr(&csi[nr_csrows], sizeof(*chi)); 195 chi = edac_align_ptr(&ptr, sizeof(*chi), nr_csrows * nr_chans);
174 dimm = edac_align_ptr(&chi[nr_chans * nr_csrows], sizeof(*dimm)); 196 dimm = edac_align_ptr(&ptr, sizeof(*dimm), nr_csrows * nr_chans);
175 pvt = edac_align_ptr(&dimm[nr_chans * nr_csrows], sz_pvt); 197 pvt = edac_align_ptr(&ptr, sz_pvt, 1);
176 size = ((unsigned long)pvt) + sz_pvt; 198 size = ((unsigned long)pvt) + sz_pvt;
177 199
178 mci = kzalloc(size, GFP_KERNEL); 200 mci = kzalloc(size, GFP_KERNEL);
diff --git a/drivers/edac/edac_module.h b/drivers/edac/edac_module.h
index 00f81b47a51..0ea7d14cb93 100644
--- a/drivers/edac/edac_module.h
+++ b/drivers/edac/edac_module.h
@@ -50,7 +50,7 @@ extern void edac_device_reset_delay_period(struct edac_device_ctl_info
50 *edac_dev, unsigned long value); 50 *edac_dev, unsigned long value);
51extern void edac_mc_reset_delay_period(int value); 51extern void edac_mc_reset_delay_period(int value);
52 52
53extern void *edac_align_ptr(void *ptr, unsigned size); 53extern void *edac_align_ptr(void **p, unsigned size, int n_elems);
54 54
55/* 55/*
56 * EDAC PCI functions 56 * EDAC PCI functions
diff --git a/drivers/edac/edac_pci.c b/drivers/edac/edac_pci.c
index 63af1c5673d..f1ac8664988 100644
--- a/drivers/edac/edac_pci.c
+++ b/drivers/edac/edac_pci.c
@@ -42,13 +42,13 @@ struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
42 const char *edac_pci_name) 42 const char *edac_pci_name)
43{ 43{
44 struct edac_pci_ctl_info *pci; 44 struct edac_pci_ctl_info *pci;
45 void *pvt; 45 void *p = NULL, *pvt;
46 unsigned int size; 46 unsigned int size;
47 47
48 debugf1("%s()\n", __func__); 48 debugf1("%s()\n", __func__);
49 49
50 pci = (struct edac_pci_ctl_info *)0; 50 pci = edac_align_ptr(&p, sizeof(*pci), 1);
51 pvt = edac_align_ptr(&pci[1], sz_pvt); 51 pvt = edac_align_ptr(&p, 1, sz_pvt);
52 size = ((unsigned long)pvt) + sz_pvt; 52 size = ((unsigned long)pvt) + sz_pvt;
53 53
54 /* Alloc the needed control struct memory */ 54 /* Alloc the needed control struct memory */