Skip to contentSkip to conversation

How to POST an OOB Kernel semaphore to an “in band” User space thread in DOVETAIL/XENOMAI 4

DISCUSSION [email protected] 2 participants 2 messages Jul 18, 2022 – Jul 19, 2022

lore
Share & tools
lore
#0 lore raw

We would like to know how to POST an OOB Kernel semaphore to an “in band” User space thread. We are currently using PREEMPT-RT up/down semaphores in and in band IOCTL function with the User space thread making the IOCTL calls to the up/down.

See the example code below: NOTE everything below this is PREEMPT-RT

THIS CODE IS IN KERNEL SPACE:

// define which file operations are supported
static const struct file_operations rtpmac_fops =
{
  .open  = rtpmac_open,
  .release = rtpmac_release,
  .unlocked_ioctl = rtpmac_ioctl,
  .mmap = rtpmac_mmap
};

static long rtpmac_ioctl(struct file* file, unsigned int cmd, unsigned long arg) {
  switch (cmd)
  {
    case IOCTL_RTINT_CPLC_SEM_POST:
      if(RtiCplc_sem.count == 0)
         up(&RtiCplc_sem);
      break;
    case IOCTL_RTINT_CPLC_SEM_WAIT:
      down(&RtiCplc_sem);
      break;

    // other calls from another User In Band thread }
}

This is to create the Semaphore
sema_init(&RtiCplc_sem, 0);

This is how we are creating PREEMPT-RT ISR
request_irq(phaseirq, (irq_handler_t)&phaseintr_handler, IRQF_NO_THREAD, "UBUS_Phase", &phasecookie);

This is the post from OOB Kernel side ISR
if(RtiCplc_sem.count == 0)
     up(&RtiCplc_sem);

THIS CODE IS IN USERSPACE:
This how we create the "in band" User space thread:

pthread_attr_init(&rtithread_attr); pthread_attr_setinheritsched(&rtithread_attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&rtithread_attr, SCHED_FIFO); rtithread_param.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
if (pthread_attr_setschedparam(&rtithread_attr, &rtithread_param) == 0) rtithread_status = pthread_create(&rtithread_thread_id, &rtithread_attr, &pRtiThread, NULL);

#This is the User space thread IOCTL call
while (rti_thread_run)
{
    ioctl(iortfd, IOCTL_RTINT_CPLC_SEM_WAIT,NULL, 0); // This is an in band Kernel ioctl call }

# This is the current DoveTail OOB ISR
request_irq(phaseirq, (irq_handler_t)&phaseintr_handler, IRQF_OOB, "UBUS_Phase", &phasecookie);

Using the code above we are trying to figure out how to the same using DOVETAIL/XENOMAI 4

NOTE: We are currently using Kernel version 5.15.0
uname -a
Linux ppmac64 5.15.0 #19 SMP PREEMPT IRQPIPE Tue Jun 28 17:00:39 PDT 2022 aarch64 aarch64 aarch64 GNU/Linux

Please provide any feedback it would be greatly appreciated.

Thank you.

#1 lore raw
Quoted 4 lines · "[email protected]" <[email protected]> writes:

We would like to know how to POST an OOB Kernel semaphore to an “in band” User space thread. We are currently using PREEMPT-RT up/down semaphores in and in band IOCTL function with the User space thread making the IOCTL calls to the up/down.

See the example code below: NOTE everything below this is PREEMPT-RT

I'm unsure why you would want to synchronize an in-band thread on an oob event; is losing the real-time guarantee during such transition ok in your use case?

Anyway, the common way to trigger in-band actions from an oob event with Dovetail is by using the regular irq_work() service [1], with the oob irq handler calling irq_work_queue() on some irq_work descriptor initialized with an in-band handler. That handler would be allowed to post a common kernel semaphore for instance, like your preempt-rt based code does.

e.g.

static void inband_handler(struct irq_work *irq_work)

{
        up(&sem);
}

static DEFINE_IRQ_WORK(inband_work, inband_handler);

static irqreturn_t irq_handler(unsigned int irq, void *dev_id)

{
        irq_work_queue(&inband_work);
}

static long ioctl_handler(struct file *filp, unsigned int cmd, unsigned long arg)

{
        ...
        down(&sem);
        ...
}

void at_init(void)

{
        request_irq(irq, irq_handler, IRQF_OOB, "irq_to_foo", dev_id);
}

The inband handler is running in interrupt context kernel-wise, on the lower execution stage though. If you need to call kernel services which might sleep from such a handler, you would have to chain the irq_work with a work_struct, calling these services from the latter. EVL provides such a combo off-the-shelf via the evl_work construct (see evl_init_work()). Calling up() directly from the inband IRQ handler is just fine though.

[1] https://evlproject.org/dovetail/pipeline/pipeline_inject/#irq-work

Signature · 1 line
Philippe.