// SPDX-License-Identifier: GPL-2.0+ /* * Provides user-space access to the SSAM EC via the /dev/surface/aggregator * misc device. Intended for debugging and development. * * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
*/
/* Validate notifier target category. */ if (!ssh_rqid_is_event(rqid)) return -EINVAL;
mutex_lock(&client->notifier_lock);
/* Check if the notifier has already been registered. */ if (client->notifier[event]) {
mutex_unlock(&client->notifier_lock); return -EEXIST;
}
/* Allocate new notifier. */
nf = kzalloc(sizeof(*nf), GFP_KERNEL); if (!nf) {
mutex_unlock(&client->notifier_lock); return -ENOMEM;
}
/* * Create a dummy notifier with the minimal required fields for * observer registration. Note that we can skip fully specifying event * and registry here as we do not need any matching and use silent * registration, which does not enable the corresponding event.
*/
nf->client = client;
nf->nf.base.fn = ssam_cdev_notifier;
nf->nf.base.priority = priority;
nf->nf.event.id.target_category = tc;
nf->nf.event.mask = 0; /* Do not do any matching. */
nf->nf.flags = SSAM_EVENT_NOTIFIER_OBSERVER;
/* Register notifier. */
status = ssam_notifier_register(client->cdev->ctrl, &nf->nf); if (status)
kfree(nf); else
client->notifier[event] = nf;
staticvoid ssam_cdev_notifier_unregister_all(struct ssam_cdev_client *client)
{ int i;
down_read(&client->cdev->lock);
/* * This function may be used during shutdown, thus we need to test for * cdev->ctrl instead of the SSAM_CDEV_DEVICE_SHUTDOWN_BIT bit.
*/ if (client->cdev->ctrl) { for (i = 0; i < SSH_NUM_EVENTS; i++)
ssam_cdev_notifier_unregister(client, i + 1);
} else { int count = 0;
/* * Device has been shut down. Any notifier remaining is a bug, * so warn about that as this would otherwise hardly be * noticeable. Nevertheless, free them as well.
*/
mutex_lock(&client->notifier_lock); for (i = 0; i < SSH_NUM_EVENTS; i++) {
count += !!(client->notifier[i]);
kfree(client->notifier[i]);
client->notifier[i] = NULL;
}
mutex_unlock(&client->notifier_lock);
/* Get request payload from user-space. */ if (spec.length) { if (!plddata) {
ret = -EINVAL; goto out;
}
/* * Note: spec.length is limited to U16_MAX bytes via struct * ssam_cdev_request. This is slightly larger than the * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the * underlying protocol (note that nothing remotely this size * should ever be allocated in any normal case). This size is * validated later in ssam_request_do_sync(), for allocation * the bound imposed by u16 should be enough.
*/
spec.payload = kzalloc(spec.length, GFP_KERNEL); if (!spec.payload) {
ret = -ENOMEM; goto out;
}
if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
ret = -EFAULT; goto out;
}
}
/* Allocate response buffer. */ if (rsp.capacity) { if (!rspdata) {
ret = -EINVAL; goto out;
}
/* * Note: rsp.capacity is limited to U16_MAX bytes via struct * ssam_cdev_request. This is slightly larger than the * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the * underlying protocol (note that nothing remotely this size * should ever be allocated in any normal case). In later use, * this capacity does not have to be strictly bounded, as it * is only used as an output buffer to be written to. For * allocation the bound imposed by u16 should be enough.
*/
rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL); if (!rsp.pointer) {
ret = -ENOMEM; goto out;
}
}
/* Perform request. */
status = ssam_request_do_sync(client->cdev->ctrl, &spec, &rsp); if (status) goto out;
/* Copy response to user-space. */ if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
ret = -EFAULT;
out: /* Always try to set response-length and status. */
tmp = put_user(rsp.length, &r->response.length); if (tmp)
ret = tmp;
tmp = put_user(status, &r->status); if (tmp)
ret = tmp;
if (down_read_killable(&cdev->lock)) return -ERESTARTSYS;
/* Make sure we're not shut down. */ if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
up_read(&cdev->lock); return -ENODEV;
}
do { /* Check availability, wait if necessary. */ if (kfifo_is_empty(&client->buffer)) {
up_read(&cdev->lock);
if (file->f_flags & O_NONBLOCK) return -EAGAIN;
status = wait_event_interruptible(client->waitq,
!kfifo_is_empty(&client->buffer) ||
test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT,
&cdev->flags)); if (status < 0) return status;
if (down_read_killable(&cdev->lock)) return -ERESTARTSYS;
/* Need to check that we're not shut down again. */ if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
up_read(&cdev->lock); return -ENODEV;
}
}
/* Try to read from FIFO. */ if (mutex_lock_interruptible(&client->read_lock)) {
up_read(&cdev->lock); return -ERESTARTSYS;
}
status = kfifo_to_user(&client->buffer, buf, count, &copied);
mutex_unlock(&client->read_lock);
if (status < 0) {
up_read(&cdev->lock); return status;
}
/* We might not have gotten anything, check this here. */ if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
up_read(&cdev->lock); return -EAGAIN;
}
} while (copied == 0);
/* * Mark device as shut-down. Prevent new clients from being added and * new operations from being executed.
*/
set_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags);
down_write(&cdev->client_lock);
/* Remove all notifiers registered by us. */
list_for_each_entry(client, &cdev->client_list, node) {
ssam_cdev_notifier_unregister_all(client);
}
/* Wake up async clients. */
list_for_each_entry(client, &cdev->client_list, node) {
kill_fasync(&client->fasync, SIGIO, POLL_HUP);
}
/* Wake up blocking clients. */
list_for_each_entry(client, &cdev->client_list, node) {
wake_up_interruptible(&client->waitq);
}
up_write(&cdev->client_lock);
/* * The controller is only guaranteed to be valid for as long as the * driver is bound. Remove controller so that any lingering open files * cannot access it any more after we're gone.
*/
down_write(&cdev->lock);
cdev->ctrl = NULL;
cdev->dev = NULL;
up_write(&cdev->lock);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.