[bootlin/training-materials updates] master: Kernel slides: improve devm_ memory allocation example (6ab1b6d7)
Michael Opdenacker
michael.opdenacker at bootlin.com
Wed Oct 28 17:05:28 CET 2020
Repository : https://github.com/bootlin/training-materials
On branch : master
Link : https://github.com/bootlin/training-materials/commit/6ab1b6d7a788beddeec47b42a87f370e1ae949cd
>---------------------------------------------------------------
commit 6ab1b6d7a788beddeec47b42a87f370e1ae949cd
Author: Michael Opdenacker <michael.opdenacker at bootlin.com>
Date: Wed Oct 28 17:05:28 2020 +0100
Kernel slides: improve devm_ memory allocation example
Signed-off-by: Michael Opdenacker <michael.opdenacker at bootlin.com>
>---------------------------------------------------------------
6ab1b6d7a788beddeec47b42a87f370e1ae949cd
slides/kernel-frameworks2/kernel-frameworks2.tex | 34 +++++++++++++-----------
1 file changed, 18 insertions(+), 16 deletions(-)
diff --git a/slides/kernel-frameworks2/kernel-frameworks2.tex b/slides/kernel-frameworks2/kernel-frameworks2.tex
index d3bb567f..c684583f 100644
--- a/slides/kernel-frameworks2/kernel-frameworks2.tex
+++ b/slides/kernel-frameworks2/kernel-frameworks2.tex
@@ -198,7 +198,8 @@ Many more operations exist. All of them are optional.
\item Allows to extend the driver capabilities beyond the limited
read/write API.
\item For example: changing the speed of a serial port, setting
- video output format, querying a device serial number...
+ video output format, querying a device serial number... Used
+ extensively in the V4L2 (video) and Alsa (sound) driver frameworks.
\item \code{cmd} is a number identifying the operation to perform
\item \code{arg} is the optional argument passed as third argument
of the \code{ioctl()} system call. Can be an integer, an
@@ -411,26 +412,25 @@ static int xxxfb_probe (struct pci_dev *dev,
\begin{minted}{c}
int foo_probe(struct platform_device *pdev)
{
- foo_t *foo;
-
- foo = kmalloc(sizeof(struct foo_t),
- GFP_KERNEL);
+ foo_t *foo = kmalloc(sizeof(struct foo_t),
+ GFP_KERNEL);
+ /* Register to framework, store
+ * reference to framework structure in foo */
...
if (failure) {
kfree(foo);
return -EBUSY;
}
- ...
platform_set_drvdata(pdev, foo);
- pm_runtime_enable(&pdev->dev);
- pm_runtime_get_sync(&pdev->dev);
return 0;
}
void foo_remove(struct platform_device *pdev)
{
- pm_runtime_disable(&pdev->dev);
foo_t *foo = platform_get_drvdata(pdev);
+ /* Retrieve framework structure from foo
+ and unregister it */
+ ...
kfree(foo);
}
\end{minted}
@@ -441,23 +441,25 @@ void foo_remove(struct platform_device *pdev)
\begin{minted}{c}
int foo_probe(struct platform_device *pdev)
{
- foo_t *foo;
-
- foo = devm_kmalloc(&pdev->dev,
+ foo_t *foo = devm_kmalloc(&pdev->dev,
sizeof(struct foo_t),
GFP_KERNEL);
+ /* Register to framework, store
+ * reference to framework structure in foo */
...
if (failure)
return -EBUSY;
- ...
- pm_runtime_enable(&pdev->dev);
- pm_runtime_get_sync(&pdev->dev);
+ platform_set_drvdata(pdev, foo);
return 0;
}
void foo_remove(struct platform_device *pdev)
{
- pm_runtime_disable(&pdev->dev);
+ foo_t *foo = platform_get_drvdata(pdev);
+ /* Retrieve framework structure from foo
+ and unregister it */
+ ...
+ /* foo automatically freed */
}
\end{minted}
\end{block}
More information about the training-materials-updates
mailing list