diff mbox

[API-NEXT,PATCHv2] linux-gen: _ishm: unlinking files asap for cleaner termination

Message ID 1481045132-61171-1-git-send-email-christophe.milard@linaro.org
State Accepted
Commit fe182fb0a97c1989747ae96b401a10d34c878480
Headers show

Commit Message

Christophe Milard Dec. 6, 2016, 5:25 p.m. UTC
_ishm now unlinks the created files as soon as possible, hence reducing
the chance to see left-overs, if ODP terminates abnormaly.
This does not provide 100% guaranty: if we are unlucky enough,
ODP may be killed between open() and unlink().
Also this method excludes exported files (flag _ODP_ISHM_EXPORT),
whose names shall be seen in the file system.

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

---

 Since V1: 
	-flag size reduced to 1 bit (maxim)
	-typo fix (Maxim)

 platform/linux-generic/_ishm.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

-- 
2.7.4

Comments

Christophe Milard Dec. 20, 2016, 4:43 p.m. UTC | #1
Ping.
I think this patch would ease cleaning up the Huge pages. If reviewed, it
is candidate for "next" and master.
Christophe.

On 6 December 2016 at 18:25, Christophe Milard <christophe.milard@linaro.org
> wrote:


> _ishm now unlinks the created files as soon as possible, hence reducing

> the chance to see left-overs, if ODP terminates abnormaly.

> This does not provide 100% guaranty: if we are unlucky enough,

> ODP may be killed between open() and unlink().

> Also this method excludes exported files (flag _ODP_ISHM_EXPORT),

> whose names shall be seen in the file system.

>

> Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

> ---

>

>  Since V1:

>         -flag size reduced to 1 bit (maxim)

>         -typo fix (Maxim)

>

>  platform/linux-generic/_ishm.c | 14 ++++++++++++--

>  1 file changed, 12 insertions(+), 2 deletions(-)

>

> diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_

> ishm.c

> index 449e357..efdb7bc 100644

> --- a/platform/linux-generic/_ishm.c

> +++ b/platform/linux-generic/_ishm.c

> @@ -71,6 +71,7 @@

>  #include <sys/types.h>

>  #include <inttypes.h>

>  #include <sys/wait.h>

> +#include <libgen.h>

>

>  /*

>   * Maximum number of internal shared memory blocks.

> @@ -159,6 +160,7 @@ typedef struct ishm_block {

>         char exptname[ISHM_FILENAME_MAXLEN]; /* name of the export file

>    */

>         uint32_t user_flags;     /* any flags the user want to remember.

>   */

>         uint32_t flags;          /* block creation flags.

>  */

> +       uint32_t external_fd:1;  /* block FD was externally provided

>   */

>         uint64_t user_len;       /* length, as requested at reserve time.

>  */

>         void *start;             /* only valid if _ODP_ISHM_SINGLE_VA is

> set*/

>         uint64_t len;            /* length. multiple of page size. 0 if

> free*/

> @@ -452,15 +454,17 @@ static int create_file(int block_index, huge_flag_t

> huge, uint64_t len,

>                 ODP_ERR("ftruncate failed: fd=%d, err=%s.\n",

>                         fd, strerror(errno));

>                 close(fd);

> +               unlink(filename);

>                 return -1;

>         }

>

> -       strncpy(new_block->filename, filename, ISHM_FILENAME_MAXLEN - 1);

>

>         /* if _ODP_ISHM_EXPORT is set, create a description file for

>          * external ref:

>          */

>         if (flags & _ODP_ISHM_EXPORT) {

> +               strncpy(new_block->filename, filename,

> +                       ISHM_FILENAME_MAXLEN - 1);

>                 snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,

>                          ISHM_EXPTNAME_FORMAT,

>                          odp_global_data.main_pid,

> @@ -483,6 +487,8 @@ static int create_file(int block_index, huge_flag_t

> huge, uint64_t len,

>                 }

>         } else {

>                 new_block->exptname[0] = 0;

> +               /* remove the file from the filesystem, keeping its fd

> open */

> +               unlink(filename);

>         }

>

>         return fd;

> @@ -814,6 +820,9 @@ int _odp_ishm_reserve(const char *name, uint64_t size,

> int fd,

>                         return -1;

>                 }

>                 new_block->huge = EXTERNAL;

> +               new_block->external_fd = 1;

> +       } else {

> +               new_block->external_fd = 0;

>         }

>

>         /* Otherwise, Try first huge pages when possible and needed: */

> @@ -865,8 +874,9 @@ int _odp_ishm_reserve(const char *name, uint64_t size,

> int fd,

>

>         /* if neither huge pages or normal pages works, we cannot proceed:

> */

>         if ((fd < 0) || (addr == NULL) || (len == 0)) {

> -               if ((new_block->filename[0]) && (fd >= 0))

> +               if ((!new_block->external_fd) && (fd >= 0))

>                         close(fd);

> +               delete_file(new_block);

>                 odp_spinlock_unlock(&ishm_tbl->lock);

>                 ODP_ERR("_ishm_reserve failed.\n");

>                 return -1;

> --

> 2.7.4

>

>
Maxim Uvarov Dec. 22, 2016, 1:05 p.m. UTC | #2
Merged,
Maxim.

On 12/20/16 19:43, Christophe Milard wrote:
> Ping.

> I think this patch would ease cleaning up the Huge pages. If reviewed,

> it is candidate for "next" and master.

> Christophe.

> 

> On 6 December 2016 at 18:25, Christophe Milard

> <christophe.milard@linaro.org <mailto:christophe.milard@linaro.org>> wrote:

> 

>     _ishm now unlinks the created files as soon as possible, hence reducing

>     the chance to see left-overs, if ODP terminates abnormaly.

>     This does not provide 100% guaranty: if we are unlucky enough,

>     ODP may be killed between open() and unlink().

>     Also this method excludes exported files (flag _ODP_ISHM_EXPORT),

>     whose names shall be seen in the file system.

> 

>     Signed-off-by: Christophe Milard <christophe.milard@linaro.org

>     <mailto:christophe.milard@linaro.org>>

>     ---

> 

>      Since V1:

>             -flag size reduced to 1 bit (maxim)

>             -typo fix (Maxim)

> 

>      platform/linux-generic/_ishm.c | 14 ++++++++++++--

>      1 file changed, 12 insertions(+), 2 deletions(-)

> 

>     diff --git a/platform/linux-generic/_ishm.c

>     b/platform/linux-generic/_ishm.c

>     index 449e357..efdb7bc 100644

>     --- a/platform/linux-generic/_ishm.c

>     +++ b/platform/linux-generic/_ishm.c

>     @@ -71,6 +71,7 @@

>      #include <sys/types.h>

>      #include <inttypes.h>

>      #include <sys/wait.h>

>     +#include <libgen.h>

> 

>      /*

>       * Maximum number of internal shared memory blocks.

>     @@ -159,6 +160,7 @@ typedef struct ishm_block {

>             char exptname[ISHM_FILENAME_MAXLEN]; /* name of the export

>     file     */

>             uint32_t user_flags;     /* any flags the user want to

>     remember.    */

>             uint32_t flags;          /* block creation flags.           

>            */

>     +       uint32_t external_fd:1;  /* block FD was externally

>     provided        */

>             uint64_t user_len;       /* length, as requested at reserve

>     time.   */

>             void *start;             /* only valid if

>     _ODP_ISHM_SINGLE_VA is set*/

>             uint64_t len;            /* length. multiple of page size. 0

>     if free*/

>     @@ -452,15 +454,17 @@ static int create_file(int block_index,

>     huge_flag_t huge, uint64_t len,

>                     ODP_ERR("ftruncate failed: fd=%d, err=%s.\n",

>                             fd, strerror(errno));

>                     close(fd);

>     +               unlink(filename);

>                     return -1;

>             }

> 

>     -       strncpy(new_block->filename, filename, ISHM_FILENAME_MAXLEN

>     - 1);

> 

>             /* if _ODP_ISHM_EXPORT is set, create a description file for

>              * external ref:

>              */

>             if (flags & _ODP_ISHM_EXPORT) {

>     +               strncpy(new_block->filename, filename,

>     +                       ISHM_FILENAME_MAXLEN - 1);

>                     snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,

>                              ISHM_EXPTNAME_FORMAT,

>                              odp_global_data.main_pid,

>     @@ -483,6 +487,8 @@ static int create_file(int block_index,

>     huge_flag_t huge, uint64_t len,

>                     }

>             } else {

>                     new_block->exptname[0] = 0;

>     +               /* remove the file from the filesystem, keeping its

>     fd open */

>     +               unlink(filename);

>             }

> 

>             return fd;

>     @@ -814,6 +820,9 @@ int _odp_ishm_reserve(const char *name, uint64_t

>     size, int fd,

>                             return -1;

>                     }

>                     new_block->huge = EXTERNAL;

>     +               new_block->external_fd = 1;

>     +       } else {

>     +               new_block->external_fd = 0;

>             }

> 

>             /* Otherwise, Try first huge pages when possible and needed: */

>     @@ -865,8 +874,9 @@ int _odp_ishm_reserve(const char *name, uint64_t

>     size, int fd,

> 

>             /* if neither huge pages or normal pages works, we cannot

>     proceed: */

>             if ((fd < 0) || (addr == NULL) || (len == 0)) {

>     -               if ((new_block->filename[0]) && (fd >= 0))

>     +               if ((!new_block->external_fd) && (fd >= 0))

>                             close(fd);

>     +               delete_file(new_block);

>                     odp_spinlock_unlock(&ishm_tbl->lock);

>                     ODP_ERR("_ishm_reserve failed.\n");

>                     return -1;

>     --

>     2.7.4

> 

>
diff mbox

Patch

diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c
index 449e357..efdb7bc 100644
--- a/platform/linux-generic/_ishm.c
+++ b/platform/linux-generic/_ishm.c
@@ -71,6 +71,7 @@ 
 #include <sys/types.h>
 #include <inttypes.h>
 #include <sys/wait.h>
+#include <libgen.h>
 
 /*
  * Maximum number of internal shared memory blocks.
@@ -159,6 +160,7 @@  typedef struct ishm_block {
 	char exptname[ISHM_FILENAME_MAXLEN]; /* name of the export file     */
 	uint32_t user_flags;     /* any flags the user want to remember.    */
 	uint32_t flags;          /* block creation flags.                   */
+	uint32_t external_fd:1;  /* block FD was externally provided        */
 	uint64_t user_len;	 /* length, as requested at reserve time.   */
 	void *start;		 /* only valid if _ODP_ISHM_SINGLE_VA is set*/
 	uint64_t len;		 /* length. multiple of page size. 0 if free*/
@@ -452,15 +454,17 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 		ODP_ERR("ftruncate failed: fd=%d, err=%s.\n",
 			fd, strerror(errno));
 		close(fd);
+		unlink(filename);
 		return -1;
 	}
 
-	strncpy(new_block->filename, filename, ISHM_FILENAME_MAXLEN - 1);
 
 	/* if _ODP_ISHM_EXPORT is set, create a description file for
 	 * external ref:
 	 */
 	if (flags & _ODP_ISHM_EXPORT) {
+		strncpy(new_block->filename, filename,
+			ISHM_FILENAME_MAXLEN - 1);
 		snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,
 			 ISHM_EXPTNAME_FORMAT,
 			 odp_global_data.main_pid,
@@ -483,6 +487,8 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 		}
 	} else {
 		new_block->exptname[0] = 0;
+		/* remove the file from the filesystem, keeping its fd open */
+		unlink(filename);
 	}
 
 	return fd;
@@ -814,6 +820,9 @@  int _odp_ishm_reserve(const char *name, uint64_t size, int fd,
 			return -1;
 		}
 		new_block->huge = EXTERNAL;
+		new_block->external_fd = 1;
+	} else {
+		new_block->external_fd = 0;
 	}
 
 	/* Otherwise, Try first huge pages when possible and needed: */
@@ -865,8 +874,9 @@  int _odp_ishm_reserve(const char *name, uint64_t size, int fd,
 
 	/* if neither huge pages or normal pages works, we cannot proceed: */
 	if ((fd < 0) || (addr == NULL) || (len == 0)) {
-		if ((new_block->filename[0]) && (fd >= 0))
+		if ((!new_block->external_fd) && (fd >= 0))
 			close(fd);
+		delete_file(new_block);
 		odp_spinlock_unlock(&ishm_tbl->lock);
 		ODP_ERR("_ishm_reserve failed.\n");
 		return -1;