cregit-Linux how code gets into the kernel

Release 4.11 drivers/edac/edac_mc.c

Directory: drivers/edac
/*
 * edac_mc kernel module
 * (C) 2005, 2006 Linux Networx (http://lnxi.com)
 * This file may be distributed under the terms of the
 * GNU General Public License.
 *
 * Written by Thayne Harbaugh
 * Based on work by Dan Hollis <goemon at anime dot net> and others.
 *      http://www.anime.net/~goemon/linux-ecc/
 *
 * Modified by Dave Peterson and Doug Thompson
 *
 */

#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/smp.h>
#include <linux/init.h>
#include <linux/sysctl.h>
#include <linux/highmem.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/ctype.h>
#include <linux/edac.h>
#include <linux/bitops.h>
#include <linux/uaccess.h>
#include <asm/page.h>
#include "edac_mc.h"
#include "edac_module.h"
#include <ras/ras_event.h>

#ifdef CONFIG_EDAC_ATOMIC_SCRUB
#include <asm/edac.h>
#else

#define edac_atomic_scrub(va, size) do { } while (0)
#endif

/* lock to memory controller's control array */
static DEFINE_MUTEX(mem_ctls_mutex);
static LIST_HEAD(mc_devices);

/*
 * Used to lock EDAC MC to just one module, avoiding two drivers e. g.
 *      apei/ghes and i7core_edac to be used at the same time.
 */

static void const *edac_mc_owner;


static struct bus_type mc_bus[EDAC_MAX_MCS];


unsigned edac_dimm_info_location(struct dimm_info *dimm, char *buf, unsigned len) { struct mem_ctl_info *mci = dimm->mci; int i, n, count = 0; char *p = buf; for (i = 0; i < mci->n_layers; i++) { n = snprintf(p, len, "%s %d ", edac_layer_name[mci->layers[i].type], dimm->location[i]); p += n; len -= n; count += n; if (!len) break; } return count; }

Contributors

PersonTokensPropCommitsCommitProp
Mauro Carvalho Chehab109100.00%1100.00%
Total109100.00%1100.00%

#ifdef CONFIG_EDAC_DEBUG
static void edac_mc_dump_channel(struct rank_info *chan) { edac_dbg(4, " channel->chan_idx = %d\n", chan->chan_idx); edac_dbg(4, " channel = %p\n", chan); edac_dbg(4, " channel->csrow = %p\n", chan->csrow); edac_dbg(4, " channel->dimm = %p\n", chan->dimm); }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson2139.62%222.22%
Mauro Carvalho Chehab1833.96%555.56%
Joe Perches1222.64%111.11%
Alan Cox23.77%111.11%
Total53100.00%9100.00%


static void edac_mc_dump_dimm(struct dimm_info *dimm, int number) { char location[80]; edac_dimm_info_location(dimm, location, sizeof(location)); edac_dbg(4, "%s%i: %smapped as virtual row %d, chan %d\n", dimm->mci->csbased ? "rank" : "dimm", number, location, dimm->csrow, dimm->cschannel); edac_dbg(4, " dimm = %p\n", dimm); edac_dbg(4, " dimm->label = '%s'\n", dimm->label); edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages); edac_dbg(4, " dimm->grain = %d\n", dimm->grain); edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages); }

Contributors

PersonTokensPropCommitsCommitProp
Mauro Carvalho Chehab8877.19%562.50%
Joe Perches1614.04%112.50%
Doug Thompson97.89%112.50%
Alan Cox10.88%112.50%
Total114100.00%8100.00%


static void edac_mc_dump_csrow(struct csrow_info *csrow) { edac_dbg(4, "csrow->csrow_idx = %d\n", csrow->csrow_idx); edac_dbg(4, " csrow = %p\n", csrow); edac_dbg(4, " csrow->first_page = 0x%lx\n", csrow->first_page); edac_dbg(4, " csrow->last_page = 0x%lx\n", csrow->last_page); edac_dbg(4, " csrow->page_mask = 0x%lx\n", csrow->page_mask); edac_dbg(4, " csrow->nr_channels = %d\n", csrow->nr_channels); edac_dbg(4, " csrow->channels = %p\n", csrow->channels); edac_dbg(4, " csrow->mci = %p\n", csrow->mci); }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson3435.05%233.33%
Dave Peterson2727.84%116.67%
Joe Perches2424.74%116.67%
Mauro Carvalho Chehab1010.31%116.67%
Alan Cox22.06%116.67%
Total97100.00%6100.00%


static void edac_mc_dump_mci(struct mem_ctl_info *mci) { edac_dbg(3, "\tmci = %p\n", mci); edac_dbg(3, "\tmci->mtype_cap = %lx\n", mci->mtype_cap); edac_dbg(3, "\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap); edac_dbg(3, "\tmci->edac_cap = %lx\n", mci->edac_cap); edac_dbg(4, "\tmci->edac_check = %p\n", mci->edac_check); edac_dbg(3, "\tmci->nr_csrows = %d, csrows = %p\n", mci->nr_csrows, mci->csrows); edac_dbg(3, "\tmci->nr_dimms = %d, dimms = %p\n", mci->tot_dimms, mci->dimms); edac_dbg(3, "\tdev = %p\n", mci->pdev); edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n", mci->mod_name, mci->ctl_name); edac_dbg(3, "\tpvt_info = %p\n\n", mci->pvt_info); }

Contributors

PersonTokensPropCommitsCommitProp
Alan Cox4332.82%114.29%
Doug Thompson3829.01%114.29%
Joe Perches3022.90%114.29%
Mauro Carvalho Chehab139.92%228.57%
Dave Peterson64.58%114.29%
Adrian Bunk10.76%114.29%
Total131100.00%7100.00%

#endif /* CONFIG_EDAC_DEBUG */ const char * const edac_mem_types[] = { [MEM_EMPTY] = "Empty csrow", [MEM_RESERVED] = "Reserved csrow type", [MEM_UNKNOWN] = "Unknown csrow type", [MEM_FPM] = "Fast page mode RAM", [MEM_EDO] = "Extended data out RAM", [MEM_BEDO] = "Burst Extended data out RAM", [MEM_SDR] = "Single data rate SDRAM", [MEM_RDR] = "Registered single data rate SDRAM", [MEM_DDR] = "Double data rate SDRAM", [MEM_RDDR] = "Registered Double data rate SDRAM", [MEM_RMBS] = "Rambus DRAM", [MEM_DDR2] = "Unbuffered DDR2 RAM", [MEM_FB_DDR2] = "Fully buffered DDR2", [MEM_RDDR2] = "Registered DDR2 RAM", [MEM_XDR] = "Rambus XDR", [MEM_DDR3] = "Unbuffered DDR3 RAM", [MEM_RDDR3] = "Registered DDR3 RAM", [MEM_LRDDR3] = "Load-Reduced DDR3 RAM", [MEM_DDR4] = "Unbuffered DDR4 RAM", [MEM_RDDR4] = "Registered DDR4 RAM", }; EXPORT_SYMBOL_GPL(edac_mem_types); /** * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation * @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 * 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 **p, unsigned size, int n_elems) { unsigned align, r; void *ptr = *p; *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. * As far as I know, this is a reasonable assumption. */ if (size > sizeof(long)) align = sizeof(long long); else if (size > sizeof(int)) align = sizeof(long); else if (size > sizeof(short)) align = sizeof(int); else if (size > sizeof(char)) align = sizeof(short); else return (char *)ptr; r = (unsigned long)p % align; if (r == 0) return (char *)ptr; *p += align - r; return (void *)(((unsigned long)ptr) + align - r); }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson10061.73%337.50%
Mauro Carvalho Chehab2716.67%112.50%
Alan Cox1710.49%112.50%
Dave Peterson138.02%225.00%
Chris Metcalf53.09%112.50%
Total162100.00%8100.00%


static void _edac_mc_free(struct mem_ctl_info *mci) { int i, chn, row; struct csrow_info *csr; const unsigned int tot_dimms = mci->tot_dimms; const unsigned int tot_channels = mci->num_cschannel; const unsigned int tot_csrows = mci->nr_csrows; if (mci->dimms) { for (i = 0; i < tot_dimms; i++) kfree(mci->dimms[i]); kfree(mci->dimms); } if (mci->csrows) { for (row = 0; row < tot_csrows; row++) { csr = mci->csrows[row]; if (csr) { if (csr->channels) { for (chn = 0; chn < tot_channels; chn++) kfree(csr->channels[chn]); kfree(csr->channels); } kfree(csr); } } kfree(mci->csrows); } kfree(mci); }

Contributors

PersonTokensPropCommitsCommitProp
Shaun Ruffell181100.00%1100.00%
Total181100.00%1100.00%


struct mem_ctl_info *edac_mc_alloc(unsigned mc_num, unsigned n_layers, struct edac_mc_layer *layers, unsigned sz_pvt) { struct mem_ctl_info *mci; struct edac_mc_layer *layer; struct csrow_info *csr; struct rank_info *chan; struct dimm_info *dimm; u32 *ce_per_layer[EDAC_MAX_LAYERS], *ue_per_layer[EDAC_MAX_LAYERS]; unsigned pos[EDAC_MAX_LAYERS]; unsigned size, tot_dimms = 1, count = 1; unsigned tot_csrows = 1, tot_channels = 1, tot_errcount = 0; void *pvt, *p, *ptr = NULL; int i, j, row, chn, n, len, off; bool per_rank = false; BUG_ON(n_layers > EDAC_MAX_LAYERS || n_layers == 0); /* * Calculate the total amount of dimms and csrows/cschannels while * in the old API emulation mode */ for (i = 0; i < n_layers; i++) { tot_dimms *= layers[i].size; if (layers[i].is_virt_csrow) tot_csrows *= layers[i].size; else tot_channels *= layers[i].size; if (layers[i].type == EDAC_MC_LAYER_CHIP_SELECT) per_rank = true; } /* Figure out the offsets of the various items from the start of an mc * structure. We want the alignment of each item to be at least as * stringent as what the compiler would provide if we could simply * hardcode everything into a single struct. */ mci = edac_align_ptr(&ptr, sizeof(*mci), 1); layer = edac_align_ptr(&ptr, sizeof(*layer), n_layers); for (i = 0; i < n_layers; i++) { count *= layers[i].size; edac_dbg(4, "errcount layer %d size %d\n", i, count); ce_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count); ue_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count); tot_errcount += 2 * count; } edac_dbg(4, "allocating %d error counters\n", tot_errcount); pvt = edac_align_ptr(&ptr, sz_pvt, 1); size = ((unsigned long)pvt) + sz_pvt; edac_dbg(1, "allocating %u bytes for mci data (%d %s, %d csrows/channels)\n", size, tot_dimms, per_rank ? "ranks" : "dimms", tot_csrows * tot_channels); mci = kzalloc(size, GFP_KERNEL); if (mci == NULL) return NULL; /* Adjust pointers so they point within the memory we just allocated * rather than an imaginary chunk of memory located at address 0. */ layer = (struct edac_mc_layer *)(((char *)mci) + ((unsigned long)layer)); for (i = 0; i < n_layers; i++) { mci->ce_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ce_per_layer[i])); mci->ue_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ue_per_layer[i])); } pvt = sz_pvt ? (((char *)mci) + ((unsigned long)pvt)) : NULL; /* setup index and various internal pointers */ mci->mc_idx = mc_num; mci->tot_dimms = tot_dimms; mci->pvt_info = pvt; mci->n_layers = n_layers; mci->layers = layer; memcpy(mci->layers, layers, sizeof(*layer) * n_layers); mci->nr_csrows = tot_csrows; mci->num_cschannel = tot_channels; mci->csbased = per_rank; /* * Alocate and fill the csrow/channels structs */ mci->csrows = kcalloc(tot_csrows, sizeof(*mci->csrows), GFP_KERNEL); if (!mci->csrows) goto error; for (row = 0; row < tot_csrows; row++) { csr = kzalloc(sizeof(**mci->csrows), GFP_KERNEL); if (!csr) goto error; mci->csrows[row] = csr; csr->csrow_idx = row; csr->mci = mci; csr->nr_channels = tot_channels; csr->channels = kcalloc(tot_channels, sizeof(*csr->channels), GFP_KERNEL); if (!csr->channels) goto error; for (chn = 0; chn < tot_channels; chn++) { chan = kzalloc(sizeof(**csr->channels), GFP_KERNEL); if (!chan) goto error; csr->channels[chn] = chan; chan->chan_idx = chn; chan->csrow = csr; } } /* * Allocate and fill the dimm structs */ mci->dimms = kcalloc(tot_dimms, sizeof(*mci->dimms), GFP_KERNEL); if (!mci->dimms) goto error; memset(&pos, 0, sizeof(pos)); row = 0; chn = 0; for (i = 0; i < tot_dimms; i++) { chan = mci->csrows[row]->channels[chn]; off = EDAC_DIMM_OFF(layer, n_layers, pos[0], pos[1], pos[2]); if (off < 0 || off >= tot_dimms) { edac_mc_printk(mci, KERN_ERR, "EDAC core bug: EDAC_DIMM_OFF is trying to do an illegal data access\n"); goto error; } dimm = kzalloc(sizeof(**mci->dimms), GFP_KERNEL); if (!dimm) goto error; mci->dimms[off] = dimm; dimm->mci = mci; /* * Copy DIMM location and initialize it. */ len = sizeof(dimm->label); p = dimm->label; n = snprintf(p, len, "mc#%u", mc_num); p += n; len -= n; for (j = 0; j < n_layers; j++) { n = snprintf(p, len, "%s#%u", edac_layer_name[layers[j].type], pos[j]); p += n; len -= n; dimm->location[j] = pos[j]; if (len <= 0) break; } /* Link it to the csrows old API data */ chan->dimm = dimm; dimm->csrow = row; dimm->cschannel = chn; /* Increment csrow location */ if (layers[0].is_virt_csrow) { chn++; if (chn == tot_channels) { chn = 0; row++; } } else { row++; if (row == tot_csrows) { row = 0; chn++; } } /* Increment dimm location */ for (j = n_layers - 1; j >= 0; j--) { pos[j]++; if (pos[j] < layers[j].size) break; pos[j] = 0; } } mci->op_state = OP_ALLOC; return mci; error: _edac_mc_free(mci); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Mauro Carvalho Chehab94781.29%1050.00%
Doug Thompson13511.59%420.00%
Alan Cox564.81%15.00%
Joe Perches121.03%210.00%
Dan Carpenter80.69%15.00%
Dave Jiang60.52%15.00%
Shaun Ruffell10.09%15.00%
Total1165100.00%20100.00%

EXPORT_SYMBOL_GPL(edac_mc_alloc);
void edac_mc_free(struct mem_ctl_info *mci) { edac_dbg(1, "\n"); /* If we're not yet registered with sysfs free only what was allocated * in edac_mc_alloc(). */ if (!device_is_registered(&mci->dev)) { _edac_mc_free(mci); return; } /* the mci instance is freed here, when the sysfs object is dropped */ edac_unregister_sysfs(mci); }

Contributors

PersonTokensPropCommitsCommitProp
Shaun Ruffell2046.51%116.67%
Mauro Carvalho Chehab2046.51%466.67%
Joe Perches36.98%116.67%
Total43100.00%6100.00%

EXPORT_SYMBOL_GPL(edac_mc_free);
bool edac_has_mcs(void) { bool ret; mutex_lock(&mem_ctls_mutex); ret = list_empty(&mc_devices); mutex_unlock(&mem_ctls_mutex); return !ret; }

Contributors

PersonTokensPropCommitsCommitProp
Yazen Ghannam34100.00%1100.00%
Total34100.00%1100.00%

EXPORT_SYMBOL_GPL(edac_has_mcs); /* Caller must hold mem_ctls_mutex */
static struct mem_ctl_info *__find_mci_by_dev(struct device *dev) { struct mem_ctl_info *mci; struct list_head *item; edac_dbg(3, "\n"); list_for_each(item, &mc_devices) { mci = list_entry(item, struct mem_ctl_info, link); if (mci->pdev == dev) return mci; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson3351.56%116.67%
Alan Cox2539.06%116.67%
Joe Perches23.12%116.67%
Borislav Petkov23.12%116.67%
Mauro Carvalho Chehab23.12%233.33%
Total64100.00%6100.00%

/** * find_mci_by_dev * * scan list of controllers looking for the one that manages * the 'dev' device * @dev: pointer to a struct device related with the MCI */
struct mem_ctl_info *find_mci_by_dev(struct device *dev) { struct mem_ctl_info *ret; mutex_lock(&mem_ctls_mutex); ret = __find_mci_by_dev(dev); mutex_unlock(&mem_ctls_mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Borislav Petkov39100.00%1100.00%
Total39100.00%1100.00%

EXPORT_SYMBOL_GPL(find_mci_by_dev); /* * handler for EDAC to check if NMI type handler has asserted interrupt */
static int edac_mc_assert_error_check_and_clear(void) { int old_state; if (edac_op_state == EDAC_OPSTATE_POLL) return 1; old_state = edac_err_assert; edac_err_assert = 0; return old_state; }

Contributors

PersonTokensPropCommitsCommitProp
Dave Jiang31100.00%2100.00%
Total31100.00%2100.00%

/* * edac_mc_workq_function * performs the operation scheduled by a workq request */
static void edac_mc_workq_function(struct work_struct *work_req) { struct delayed_work *d_work = to_delayed_work(work_req); struct mem_ctl_info *mci = to_edac_mem_ctl_work(d_work); mutex_lock(&mem_ctls_mutex); if (mci->op_state != OP_RUNNING_POLL) { mutex_unlock(&mem_ctls_mutex); return; } if (edac_mc_assert_error_check_and_clear()) mci->edac_check(mci); mutex_unlock(&mem_ctls_mutex); /* Queue ourselves again. */ edac_queue_work(&mci->work, msecs_to_jiffies(edac_mc_get_poll_msec())); }

Contributors

PersonTokensPropCommitsCommitProp
Dave Jiang6574.71%233.33%
Doug Thompson1517.24%116.67%
Borislav Petkov44.60%233.33%
Jean Delvare33.45%116.67%
Total87100.00%6100.00%

/* * edac_mc_reset_delay_period(unsigned long value) * * user space has updated our poll period value, need to * reset our workq delays */
void edac_mc_reset_delay_period(unsigned long value) { struct mem_ctl_info *mci; struct list_head *item; mutex_lock(&mem_ctls_mutex); list_for_each(item, &mc_devices) { mci = list_entry(item, struct mem_ctl_info, link); if (mci->op_state == OP_RUNNING_POLL) edac_mod_work(&mci->work, value); } mutex_unlock(&mem_ctls_mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson3652.17%120.00%
Dave Jiang1927.54%120.00%
Nicholas Krause811.59%120.00%
Borislav Petkov68.70%240.00%
Total69100.00%5100.00%

/* Return 0 on success, 1 on failure. * Before calling this function, caller must * assign a unique value to mci->mc_idx. * * locking model: * * called with the mem_ctls_mutex lock held */
static int add_mc_to_global_list(struct mem_ctl_info *mci) { struct list_head *item, *insert_before; struct mem_ctl_info *p; insert_before = &mc_devices; p = __find_mci_by_dev(mci->pdev); if (unlikely(p != NULL)) goto fail0; list_for_each(item, &mc_devices) { p = list_entry(item, struct mem_ctl_info, link); if (p->mc_idx >= mci->mc_idx) { if (unlikely(p->mc_idx == mci->mc_idx)) goto fail1; insert_before = item; break; } } list_add_tail_rcu(&mci->link, insert_before); atomic_inc(&edac_handlers); return 0; fail0: edac_printk(KERN_WARNING, EDAC_MC, "%s (%s) %s %s already assigned %d\n", dev_name(p->pdev), edac_dev_name(mci), p->mod_name, p->ctl_name, p->mc_idx); return 1; fail1: edac_printk(KERN_WARNING, EDAC_MC, "bug in low-level driver: attempt to assign\n" " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__); return 1; }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson15686.19%330.00%
Alan Cox116.08%110.00%
Dave Jiang73.87%220.00%
Kay Sievers31.66%110.00%
Mauro Carvalho Chehab21.10%110.00%
Borislav Petkov10.55%110.00%
Stephen Rothwell10.55%110.00%
Total181100.00%10100.00%


static int del_mc_from_global_list(struct mem_ctl_info *mci) { int handlers = atomic_dec_return(&edac_handlers); list_del_rcu(&mci->link); /* these are for safe removal of devices from global list while * NMI handlers may be traversing list */ synchronize_rcu(); INIT_LIST_HEAD(&mci->link); return handlers; }

Contributors

PersonTokensPropCommitsCommitProp
Doug Thompson1534.88%120.00%
Mauro Carvalho Chehab920.93%120.00%
Alan Cox920.93%120.00%
Lai Jiangshan613.95%120.00%
Dave Jiang49.30%120.00%
Total43100.00%5100.00%


struct mem_ctl_info *edac_mc_find(int idx) { struct mem_ctl_info *mci = NULL; struct list_head *item; mutex_lock(&mem_ctls_mutex); list_for_each(item, &mc_devices) { mci = list_entry(item, struct mem_ctl_info, link); if (mci->mc_idx >= idx) { if (mci->mc_idx == idx) { goto unlock; }