drivers/am335xgpio: Release resources on error and when quitting 47/7047/8
authorSteve Marple <stevemarple@googlemail.com>
Wed, 22 Jun 2022 14:43:51 +0000 (15:43 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Mon, 15 Aug 2022 13:24:46 +0000 (13:24 +0000)
The /dev/mem file descriptor can be closed without invalidating the
mappings so close as soon as possible.

munmap() all memory, either on error or from quit.

Change-Id: I9466edd2f43791e64f2dce719957c67728f3ec06
Signed-off-by: Steve Marple <stevemarple@googlemail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7047
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
src/jtag/drivers/am335xgpio.c

index 5b68b6d6f1c15e17aa7f85d86ac1db5340825873..ae58b16402bd109f74f9bdd09b7f23c11bdca5b8 100644 (file)
@@ -377,6 +377,13 @@ static bool am335xgpio_swd_mode_possible(void)
        return true;
 }
 
+static void am335xgpio_munmap(void)
+{
+       for (unsigned int i = 0; i < AM335XGPIO_NUM_GPIO_CHIPS && am335xgpio_gpio_chip_mmap_addr[i] != MAP_FAILED; ++i)
+               if (munmap((void *)am335xgpio_gpio_chip_mmap_addr[i], sysconf(_SC_PAGE_SIZE)) < 0)
+                       LOG_ERROR("Cannot unmap GPIO memory for chip %d: %s", i, strerror(errno));
+}
+
 static int am335xgpio_init(void)
 {
        LOG_INFO("AM335x GPIO JTAG/SWD bitbang driver");
@@ -410,10 +417,12 @@ static int am335xgpio_init(void)
 
                if (am335xgpio_gpio_chip_mmap_addr[i] == MAP_FAILED) {
                        LOG_ERROR("mmap: %s", strerror(errno));
+                       am335xgpio_munmap();
                        close(dev_mem_fd);
                        return ERROR_JTAG_INIT_FAILED;
                }
        }
+       close(dev_mem_fd);
 
        /* Configure JTAG/SWD signals. Default directions and initial states are handled
         * by adapter.c and "adapter gpio" command.
@@ -476,6 +485,8 @@ static int am335xgpio_quit(void)
        restore_gpio(ADAPTER_GPIO_IDX_SRST);
        restore_gpio(ADAPTER_GPIO_IDX_LED);
 
+       am335xgpio_munmap();
+
        return ERROR_OK;
 }
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)