summaryrefslogtreecommitdiffstats
path: root/include/slice.h
Commit message (Collapse)AuthorAgeFilesLines
* common: bthread: schedule only in command contextAhmad Fatoum2021-06-251-2/+0
| | | | | | | | | | | | | | | | | | | | | Originally, I envisioned bthreads as replacement for pollers and workqueues. But even without preemption, having functions you call possibly accessing structures you are iterating over can corrupt memory. This problem exists with pollers as well, but because of their limited scope, it's harder to shoot your foot with them, as you don't keep implicit state between poller activations unlike bthreads, which maintain their stack across context switches. Limit bthread scope instead to be a replacement for workqueues. This still allows us to port some classes of state-machine-in-kthread kernel code, while avoding the aforementioned pitfalls. Cc: Jan Luebbe <j.luebbe@pengutronix.de> Cc: Sascha Hauer <s.hauer@pengutronix.de> Cc: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210622082617.18011-5-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* common: move workqueue handling from poller_call() to sched()Ahmad Fatoum2021-06-251-4/+2
| | | | | | | | | | | | | | | Workqueues are run out of poller_call, not because of a dependency, but because when they were added, poller_call was directly called from is_timeout. With the addition of bthreads, there is now a general resched() function that runs pollers and switches between bthreads. It makes sense to move workqueue handling there as well to keep scheduling matter contained in a single function. Do so. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210622082617.18011-4-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* common: introduce bthreads, co-operative barebox threadsAhmad Fatoum2021-03-221-8/+9
| | | | | | | | | | | | | | | | | | | | With the new setjmp/longjmp/initjmp support, we have all the architecture support in place to have suspendable green threads in barebox. These are expected to replace pollers and workqueues. For now we still have a differentiation between the main and secondary threads. The main thread is allowed I/O access unconditionally. If it's in a delay loop, a secondary thread running needs to be wary of not entering the same driver and doing hardware manipulation. We already have slices as mechanism to guard against this, but they aren't used as widely as needed. Preferably, in the end, threads will automatically yield until they can claim a resource (i.e. lock a mutex). Until we are there, take the same care when using bthreads as with pollers. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* fs: Warn when filesystem operations are called from a pollerSascha Hauer2020-08-191-0/+10
| | | | | | | | | Filesystem operations possibly call into arbitrary devices, so shouldn't be used from a poller. This patch sprinkles some WARN_ONCE() when this happens. One exception is when the file which is accessed is on ramfs which doesn't have any dependencies to devices. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* Add workqueuesSascha Hauer2020-08-131-0/+5
| | | | | | | | | | | | | | | | | | | Some code wants to run arbitrary code in the background, examples are fastboot and ratp. Currently this is implemented in pollers. The problem with this is that pollers are executed whenever is_timeout() is called which may happen anywhere in the code. With this we can never tell which resources are currently in use when the poller is executed. This adds a work queue interface which is specifically designed to trigger doing work in a context where it's safe to run arbitrary commands. Code in pollers can attach work to a work queue which is at that time only queued up. A new slice, the command slice, is added which by default is acquired. It is only released at places where the shell waits for input. When during this time pollers are executed the queued up works are done before running the registered pollers. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* Introduce slicesSascha Hauer2020-08-131-0/+31
slices, the barebox idea of locking barebox has pollers which execute code in the background whenever one of the delay functions (udelay, mdelay, ...) or is_timeout() are called. This introduces resource problems when some device triggers a poller by calling a delay function and then the poller code calls into the same device again. As an example consider a I2C GPIO expander which drives a LED which shall be used as a heartbeat LED: poller -> LED on/off -> GPIO high/low -> I2C transfer The I2C controller has a timeout loop using is_timeout() and thus can trigger a poller run. With this the following can happen during an unrelated I2C transfer: I2C transfer -> is_timeout() -> poller -> LED on/off -> GPIO high/low -> I2C transfer We end up with issuing an I2C transfer during another I2C transfer and things go downhill. Due to the lack of interrupts we can't do real locking in barebox. We use a mechanism called slices instead. A slice describes a resource to which other slices can be attached. Whenever a slice is needed it must be acquired. Acquiring a slice never fails, it just increases the acquired counter of the slice and its dependent slices. when a slice shall be used inside a poller it must first be tested if the slice is already in use. If it is, we can't do the operation on the slice now and must return and hope that we have more luck in the next poller call. slices can be attached other slices as dependencies. In the example above LED driver would add a dependency to the GPIO controller and the GPIO driver would add a dependency to the I2C bus: GPIO driver probe: slice_add(&gpio->slice, i2c_device_slice(i2cdev)); LED driver probe: slice_add(&led->slice, gpio_slice(gpio)); The GPIO code would call slice_acquire(&gpio->slice) before doing any operation on the GPIO chip providing this GPIO, likewise the I2C core would call slice_acquire(&i2cbus->slice) before doing an operation on this I2C bus. The heartbeat poller code would call slice_acquired(led_slice(led)) and only continue when the slice is not acquired. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>