On Thu, Sep 26, 2019 at 11:06:22PM +0800, Yu, Jin wrote:
> > >
> > > +static int
> > > +mem_create(const char *name, unsigned int flags) { #ifdef
> > > +MEMFD_SUPPORTED
> > > + return memfd_create(name, flags);
> > > +#else
> > > + RTE_LOG(ERR, VHOST_CONFIG,
> > > +         "doesn't support memfd--name:%s and flag:%x\n",
> > > +         name, flags);
> > 
> > You probably don't want to always print an error when memfd isn't available.
> I saw below error message from ci/Intel-compilation. 
> ../lib/librte_vhost/vhost_user.c:1202:24: error: unused parameter ‘name’ 
> [-Werror=unused-parameter]
>  mem_create(const char *name, unsigned int flags)
>                         ^~~~
> ../lib/librte_vhost/vhost_user.c:1202:43: error: unused parameter ‘flags’ 
> [-Werror=unused-parameter]
>  mem_create(const char *name, unsigned int flags)
> 
> That's why I add this print.Should I ignore this?

Looks better to do it like this (compile test only):

static void *
inflight_mem_alloc(const char *name, size_t size, int *fd)
{
        char fname[20] = "/tmp/memfd-XXXXXX";
        int mfd = -1;
        void *addr;

#ifdef MEMFD_SUPPORTED
        mfd = memfd_create(name, MFD_CLOEXEC);
#else
        RTE_SET_USED(name);
#endif
        if (mfd == -1) {
                mfd = mkstemp(fname);
                if (mfd == -1) {
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "failed to create memory file for inflight 
buffer\n");
                        return NULL;
                }
                unlink(fname);
        }

        if (ftruncate(mfd, size) == -1) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "failed to truncate memory file for inflight buffer\n");
                close(mfd);
                return NULL;
        }

        addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
        if (addr == MAP_FAILED) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "failed to map memory file for inflight buffer\n");
                close(mfd);
                return NULL;
        }

        *fd = mfd;
        return addr;
}

And you don't need to define CLOEXEC by yourself.


> Thanks.
> 
> > 
> > > + return -1;
> > > +#endif
> > > +}
> > > +
> > > +static void *
> > > +inflight_mem_alloc(const char *name, size_t size, int *fd) {
> > > + void *ptr;
> > > + int mfd = -1;
> > > + char fname[20] = "/tmp/memfd-XXXXXX";
> > > +
> > > + *fd = -1;
> > > + mfd = mem_create(name, CLOEXEC);
> > > + if (mfd != -1) {
> > > +         if (ftruncate(mfd, size) == -1) {
> > > +                 RTE_LOG(ERR, VHOST_CONFIG,
> > > +                         "ftruncate fail for alloc inflight buffer\n");
> > > +                 close(mfd);
> > > +                 return NULL;
> > > +         }
> > > + } else {
> > > +         mfd = mkstemp(fname);
> > > +         unlink(fname);
> > > +
> > > +         if (mfd == -1) {
> > > +                 RTE_LOG(ERR, VHOST_CONFIG,
> > > +                         "mkstemp fail for alloc inflight buffer\n");
> > > +                 return NULL;
> > > +         }
> > > +
> > > +         if (ftruncate(mfd, size) == -1) {
> > > +                 RTE_LOG(ERR, VHOST_CONFIG,
> > > +                         "ftruncate fail for alloc inflight buffer\n");
> > > +                 close(mfd);
> > > +                 return NULL;
> > > +         }
> > > + }
> > > +
> > > + ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> > > + if (ptr == MAP_FAILED) {
> > > +         RTE_LOG(ERR, VHOST_CONFIG,
> > > +                 "mmap fail for alloc inflight buffer\n");
> > > +         close(mfd);
> > > +         return NULL;
> > > + }
> > > +
> > > + *fd = mfd;
> > > + return ptr;
> > > +}

Reply via email to