cregit-Linux how code gets into the kernel

Release 4.15 drivers/char/tpm/tpm-chip.c

Directory: drivers/char/tpm
/*
 * Copyright (C) 2004 IBM Corporation
 * Copyright (C) 2014 Intel Corporation
 *
 * Authors:
 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 *
 * TPM chip management routines.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 */

#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/freezer.h>
#include <linux/major.h>
#include "tpm.h"
#include "tpm_eventlog.h"


DEFINE_IDR(dev_nums_idr);
static DEFINE_MUTEX(idr_lock);


struct class *tpm_class;

struct class *tpmrm_class;

dev_t tpm_devt;

/**
 * tpm_try_get_ops() - Get a ref to the tpm_chip
 * @chip: Chip to ref
 *
 * The caller must already have some kind of locking to ensure that chip is
 * valid. This function will lock the chip so that the ops member can be
 * accessed safely. The locking prevents tpm_chip_unregister from
 * completing, so it should not be held for long periods.
 *
 * Returns -ERRNO if the chip could not be got.
 */

int tpm_try_get_ops(struct tpm_chip *chip) { int rc = -EIO; get_device(&chip->dev); down_read(&chip->ops_sem); if (!chip->ops) goto out_lock; return 0; out_lock: up_read(&chip->ops_sem); put_device(&chip->dev); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe66100.00%1100.00%
Total66100.00%1100.00%

EXPORT_SYMBOL_GPL(tpm_try_get_ops); /** * tpm_put_ops() - Release a ref to the tpm_chip * @chip: Chip to put * * This is the opposite pair to tpm_try_get_ops(). After this returns chip may * be kfree'd. */
void tpm_put_ops(struct tpm_chip *chip) { up_read(&chip->ops_sem); put_device(&chip->dev); }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe26100.00%1100.00%
Total26100.00%1100.00%

EXPORT_SYMBOL_GPL(tpm_put_ops); /** * tpm_chip_find_get() - return tpm_chip for a given chip number * @chip_num: id to find * * The return'd chip has been tpm_try_get_ops'd and must be released via * tpm_put_ops */
struct tpm_chip *tpm_chip_find_get(int chip_num) { struct tpm_chip *chip, *res = NULL; int chip_prev; mutex_lock(&idr_lock); if (chip_num == TPM_ANY_NUM) { chip_num = 0; do { chip_prev = chip_num; chip = idr_get_next(&dev_nums_idr, &chip_num); if (chip && !tpm_try_get_ops(chip)) { res = chip; break; } } while (chip_prev != chip_num); } else { chip = idr_find(&dev_nums_idr, chip_num); if (chip && !tpm_try_get_ops(chip)) res = chip; } mutex_unlock(&idr_lock); return res; }

Contributors

PersonTokensPropCommitsCommitProp
Stefan Berger7865.55%125.00%
Jarkko Sakkinen3831.93%125.00%
Jason Gunthorpe21.68%125.00%
Matthew Wilcox10.84%125.00%
Total119100.00%4100.00%

/** * tpm_dev_release() - free chip memory and the device number * @dev: the character device for the TPM chip * * This is used as the release function for the character device. */
static void tpm_dev_release(struct device *dev) { struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); mutex_lock(&idr_lock); idr_remove(&dev_nums_idr, chip->dev_num); mutex_unlock(&idr_lock); kfree(chip->log.bios_event_log); kfree(chip->work_space.context_buf); kfree(chip->work_space.session_buf); kfree(chip); }

Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen5467.50%350.00%
James Bottomley911.25%116.67%
Nayna Jain911.25%116.67%
Stefan Berger810.00%116.67%
Total80100.00%6100.00%


static void tpm_devs_release(struct device *dev) { struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); /* release the master device reference */ put_device(&chip->dev); }

Contributors

PersonTokensPropCommitsCommitProp
James Bottomley35100.00%1100.00%
Total35100.00%1100.00%

/** * tpm_class_shutdown() - prepare the TPM device for loss of power. * @dev: device to which the chip is associated. * * Issues a TPM2_Shutdown command prior to loss of power, as required by the * TPM 2.0 spec. * Then, calls bus- and device- specific shutdown code. * * XXX: This codepath relies on the fact that sysfs is not enabled for * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2 * has sysfs support enabled before TPM sysfs's implicit locking is fixed. */
static int tpm_class_shutdown(struct device *dev) { struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); if (chip->flags & TPM_CHIP_FLAG_TPM2) { down_write(&chip->ops_sem); tpm2_shutdown(chip, TPM2_SU_CLEAR); chip->ops = NULL; up_write(&chip->ops_sem); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Josh Zimmerman68100.00%1100.00%
Total68100.00%1100.00%

/** * tpm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: device to which the chip is associated * At this point pdev mst be initialized, but does not have to * be registered * @ops: struct tpm_class_ops instance * * Allocates a new struct tpm_chip instance and assigns a free * device number for it. Must be paired with put_device(&chip->dev). */
struct tpm_chip *tpm_chip_alloc(struct device *pdev, const struct tpm_class_ops *ops) { struct tpm_chip *chip; int rc; chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) return ERR_PTR(-ENOMEM); mutex_init(&chip->tpm_mutex); init_rwsem(&chip->ops_sem); chip->ops = ops; mutex_lock(&idr_lock); rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL); mutex_unlock(&idr_lock); if (rc < 0) { dev_err(pdev, "No available tpm device numbers\n"); kfree(chip); return ERR_PTR(rc); } chip->dev_num = rc; device_initialize(&chip->dev); device_initialize(&chip->devs); chip->dev.class = tpm_class; chip->dev.class->shutdown_pre = tpm_class_shutdown; chip->dev.release = tpm_dev_release; chip->dev.parent = pdev; chip->dev.groups = chip->groups; chip->devs.parent = pdev; chip->devs.class = tpmrm_class; chip->devs.release = tpm_devs_release; /* get extra reference on main device to hold on * behalf of devs. This holds the chip structure * while cdevs is in use. The corresponding put * is in the tpm_devs_release (TPM2 only) */ if (chip->flags & TPM_CHIP_FLAG_TPM2) get_device(&chip->dev); if (chip->dev_num == 0) chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); else chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); chip->devs.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); if (rc) goto out; rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); if (rc) goto out; if (!pdev) chip->flags |= TPM_CHIP_FLAG_VIRTUAL; cdev_init(&chip->cdev, &tpm_fops); cdev_init(&chip->cdevs, &tpmrm_fops); chip->cdev.owner = THIS_MODULE; chip->cdevs.owner = THIS_MODULE; chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); if (!chip->work_space.context_buf) { rc = -ENOMEM; goto out; } chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); if (!chip->work_space.session_buf) { rc = -ENOMEM; goto out; } chip->locality = -1; return chip; out: put_device(&chip->devs); put_device(&chip->dev); return ERR_PTR(rc); }

Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen23848.77%735.00%
James Bottomley14229.10%210.00%
Jason Gunthorpe5411.07%420.00%
Stefan Berger408.20%420.00%
Josh Zimmerman91.84%15.00%
Tomas Winkler40.82%15.00%
Michal Suchanek10.20%15.00%
Total488100.00%20100.00%

EXPORT_SYMBOL_GPL(tpm_chip_alloc); /** * tpmm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: parent device to which the chip is associated * @ops: struct tpm_class_ops instance * * Same as tpm_chip_alloc except devm is used to do the put_device */
struct tpm_chip *tpmm_chip_alloc(struct device *pdev, const struct tpm_class_ops *ops) { struct tpm_chip *chip; int rc; chip = tpm_chip_alloc(pdev, ops); if (IS_ERR(chip)) return chip; rc = devm_add_action_or_reset(pdev, (void (*)(void *)) put_device, &chip->dev); if (rc) return ERR_PTR(rc); dev_set_drvdata(pdev, chip); return chip; }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe5561.80%233.33%
Jarkko Sakkinen3337.08%350.00%
Sudip Mukherjee11.12%116.67%
Total89100.00%6100.00%

EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
static int tpm_add_char_device(struct tpm_chip *chip) { int rc; rc = cdev_device_add(&chip->cdev, &chip->dev); if (rc) { dev_err(&chip->dev, "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", dev_name(&chip->dev), MAJOR(chip->dev.devt), MINOR(chip->dev.devt), rc); return rc; } if (chip->flags & TPM_CHIP_FLAG_TPM2) { rc = cdev_device_add(&chip->cdevs, &chip->devs); if (rc) { dev_err(&chip->devs, "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", dev_name(&chip->devs), MAJOR(chip->devs.devt), MINOR(chip->devs.devt), rc); return rc; } } /* Make the chip available. */ mutex_lock(&idr_lock); idr_replace(&dev_nums_idr, chip, chip->dev_num); mutex_unlock(&idr_lock); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen7240.91%337.50%
James Bottomley6738.07%112.50%
Stefan Berger2514.20%112.50%
Jason Gunthorpe52.84%112.50%
Linus Torvalds52.84%112.50%
Logan Gunthorpe21.14%112.50%
Total176100.00%8100.00%


static void tpm_del_char_device(struct tpm_chip *chip) { cdev_device_del(&chip->cdev, &chip->dev); /* Make the chip unavailable. */ mutex_lock(&idr_lock); idr_replace(&dev_nums_idr, NULL, chip->dev_num); mutex_unlock(&idr_lock); /* Make the driver uncallable. */ down_write(&chip->ops_sem); if (chip->flags & TPM_CHIP_FLAG_TPM2) tpm2_shutdown(chip, TPM2_SU_CLEAR); chip->ops = NULL; up_write(&chip->ops_sem); }

Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen3135.63%457.14%
Stefan Berger3135.63%114.29%
Jason Gunthorpe2326.44%114.29%
Logan Gunthorpe22.30%114.29%
Total87100.00%7100.00%


static void tpm_del_legacy_sysfs(struct tpm_chip *chip) { struct attribute **i; if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) return; sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); for (i = chip->groups[0]->attrs; *i != NULL; ++i) sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe7589.29%125.00%
Jarkko Sakkinen55.95%250.00%
Stefan Berger44.76%125.00%
Total84100.00%4100.00%

/* For compatibility with legacy sysfs paths we provide symlinks from the * parent dev directory to selected names within the tpm chip directory. Old * kernel versions created these files directly under the parent. */
static int tpm_add_legacy_sysfs(struct tpm_chip *chip) { struct attribute **i; int rc; if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) return 0; rc = __compat_only_sysfs_link_entry_to_kobj( &chip->dev.parent->kobj, &chip->dev.kobj, "ppi"); if (rc && rc != -ENOENT) return rc; /* All the names from tpm-sysfs */ for (i = chip->groups[0]->attrs; *i != NULL; ++i) { rc = __compat_only_sysfs_link_entry_to_kobj( &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name); if (rc) { tpm_del_legacy_sysfs(chip); return rc; } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe6949.64%225.00%
Jarkko Sakkinen6647.48%562.50%
Stefan Berger42.88%112.50%
Total139100.00%8100.00%

/* * tpm_chip_register() - create a character device for the TPM chip * @chip: TPM chip to use. * * Creates a character device for the TPM chip and adds sysfs attributes for * the device. As the last step this function adds the chip to the list of TPM * chips available for in-kernel use. * * This function should be only called after the chip initialization is * complete. */
int tpm_chip_register(struct tpm_chip *chip) { int rc; if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) { if (chip->flags & TPM_CHIP_FLAG_TPM2) rc = tpm2_auto_startup(chip); else rc = tpm1_auto_startup(chip); if (rc) return rc; } tpm_sysfs_add_device(chip); rc = tpm_bios_log_setup(chip); if (rc != 0 && rc != -ENODEV) return rc; tpm_add_ppi(chip); rc = tpm_add_char_device(chip); if (rc) { tpm_bios_log_teardown(chip); return rc; } rc = tpm_add_legacy_sysfs(chip); if (rc) { tpm_chip_unregister(chip); return rc; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Jason Gunthorpe11487.02%350.00%
Jarkko Sakkinen1511.45%233.33%
Nayna Jain21.53%116.67%
Total131100.00%6100.00%

EXPORT_SYMBOL_GPL(tpm_chip_register); /* * tpm_chip_unregister() - release the TPM driver * @chip: TPM chip to use. * * Takes the chip first away from the list of available TPM chips and then * cleans up all the resources reserved by tpm_chip_register(). * * Once this function returns the driver call backs in 'op's will not be * running and will no longer start. * * NOTE: This function should be only called before deinitializing chip * resources. */
void tpm_chip_unregister(struct tpm_chip *chip) { tpm_del_legacy_sysfs(chip); tpm_bios_log_teardown(chip); if (chip->flags & TPM_CHIP_FLAG_TPM2) cdev_device_del(&chip->cdevs, &chip->devs); tpm_del_char_device(chip); }

Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen2452.17%457.14%
James Bottomley1941.30%114.29%
Linus Torvalds24.35%114.29%
Jason Gunthorpe12.17%114.29%
Total46100.00%7100.00%

EXPORT_SYMBOL_GPL(tpm_chip_unregister);

Overall Contributors

PersonTokensPropCommitsCommitProp
Jarkko Sakkinen63236.68%1642.11%
Jason Gunthorpe51229.72%821.05%
James Bottomley27716.08%25.26%
Stefan Berger19411.26%410.53%
Josh Zimmerman784.53%12.63%
Nayna Jain110.64%12.63%
Linus Torvalds70.41%12.63%
Logan Gunthorpe40.23%12.63%
Tomas Winkler40.23%12.63%
Matthew Wilcox20.12%12.63%
Sudip Mukherjee10.06%12.63%
Michal Suchanek10.06%12.63%
Total1723100.00%38100.00%
Directory: drivers/char/tpm
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.