Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve invalidation mechanism and harden security #60

Merged
merged 19 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a6b0d3e
Fixed the bug that causes unmap_mapping_range to not perform unmapping
pakmarkthub Jun 3, 2019
d394e2a
Fixed the bug allowing the child process the shared gdr_map with the…
pakmarkthub Jul 17, 2019
e82a4c5
Prevented sharing gdrcopy fd between processes
pakmarkthub Jul 17, 2019
835cc12
Separated mapping address space of Process A from Process B
pakmarkthub Jul 17, 2019
490a109
Added comments to gdrdrv.c
pakmarkthub Jun 5, 2019
094ea6b
Changed %p to %px in gdrdrv
pakmarkthub Jun 13, 2019
2d95c64
Added pid checking to gdrdrv_release
pakmarkthub Jun 13, 2019
e452d9c
Created gdr_generate_mr_handle and fixed the wrap-around bug
pakmarkthub Jun 13, 2019
df02139
Handled the mismatch of the address_space struct in different Linux k…
pakmarkthub Jun 14, 2019
34daeb2
Removed unused #include <linux/sched/mm.h> from gdrdrv/gdrdrv.c
pakmarkthub Jun 14, 2019
d1a266c
Fixed the bug that makes gdrdrv_pin_buffer eagerly fail when the assi…
pakmarkthub Jun 14, 2019
58575d5
Fixed the bug that adds mr to info->mr_list when creating the mr fail
pakmarkthub Jun 14, 2019
a25c4fe
Fixed comments and checked for info->lock in gdrdrv.c
pakmarkthub Jun 18, 2019
b93ad9c
gdr_close() now automatically cleans up resources
pakmarkthub Jul 17, 2019
11079f5
Removed deprecated gds_close comment
pakmarkthub Jun 18, 2019
00b947e
Dropped support for Linux kernel version <= 2.6.9, and fixed a typo
pakmarkthub Jul 15, 2019
9124188
Fixed the indentation issue in gdrapi.h
pakmarkthub Jul 17, 2019
d91d76b
Removed "// TODO: tear down stale partial mappings" because partial m…
pakmarkthub Jul 15, 2019
3786215
Changed child -> children and checked ret before assigning params.han…
pakmarkthub Jul 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions gdrapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <sys/ioctl.h>
#include <time.h>
#include <asm/types.h>
#include <sys/queue.h>

#include "gdrapi.h"
#include "gdrdrv.h"
Expand Down Expand Up @@ -100,8 +101,9 @@ static void gdr_msg(enum gdrcopy_msg_level lvl, const char* fmt, ...)
#define gdr_warn(FMT, ARGS...) gdr_msg(GDRCOPY_MSG_WARN, "WARN: " FMT, ## ARGS)
#define gdr_err(FMT, ARGS...) gdr_msg(GDRCOPY_MSG_ERROR, "ERR: " FMT, ## ARGS)

typedef struct {
typedef struct gdr_memh_t {
gdr_hnd_t handle;
LIST_ENTRY(gdr_memh_t) entries;
unsigned mapped:1;
unsigned wc_mapping:1;
} gdr_memh_t;
Expand All @@ -121,6 +123,7 @@ static gdr_mh_t from_memh(gdr_memh_t *memh) {

struct gdr {
int fd;
LIST_HEAD(memh_list, gdr_memh_t) memhs;
};

gdr_t gdr_open()
Expand All @@ -134,7 +137,7 @@ gdr_t gdr_open()
return NULL;
}

int fd = open(gdrinode, O_RDWR);
int fd = open(gdrinode, O_RDWR | O_CLOEXEC);
if (-1 == fd ) {
int ret = errno;
gdr_err("error opening driver (errno=%d/%s)\n", ret, strerror(ret));
Expand All @@ -143,14 +146,31 @@ gdr_t gdr_open()
}

g->fd = fd;
LIST_INIT(&g->memhs);

return g;
}

int gdr_close(gdr_t g)
{
int ret = 0;
int retcode = close(g->fd);
int retcode;
gdr_memh_t *mh, *next_mh;

mh = g->memhs.lh_first;
while (mh != NULL) {
// gdr_unpin_buffer frees mh, so we need to get the next one
// beforehand.
next_mh = mh->entries.le_next;
ret = gdr_unpin_buffer(g, from_memh(mh));
if (ret) {
gdr_err("error unpinning buffer inside gdr_close (errno=%d/%s)\n", ret, strerror(ret));
return ret;
}
mh = next_mh;
}

retcode = close(g->fd);
if (-1 == retcode) {
ret = errno;
gdr_err("error closing driver (errno=%d/%s)\n", ret, strerror(ret));
Expand Down Expand Up @@ -188,6 +208,7 @@ int gdr_pin_buffer(gdr_t g, unsigned long addr, size_t size, uint64_t p2p_token,
goto err;
}
mh->handle = params.handle;
LIST_INSERT_HEAD(&g->memhs, mh, entries);
*handle = from_memh(mh);
err:
return ret;
Expand All @@ -206,6 +227,8 @@ int gdr_unpin_buffer(gdr_t g, gdr_mh_t handle)
ret = errno;
gdr_err("ioctl error (errno=%d)\n", ret);
}
LIST_REMOVE(mh, entries);
free(mh);

return ret;
}
Expand Down
6 changes: 1 addition & 5 deletions gdrapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ gdr_t gdr_open();

// Destroy library state object, e.g. it closes the connection to kernel-mode
// driver.
//
// Note that altough BAR mappings of GPU memory are destroyed, user-space
// mappings are not. So therefore user code is responsible of calling
// gdr_unmap on all mappings before calling gdr_close.
int gdr_close(gdr_t g);

typedef struct gdr_mh_s {
unsigned long h;
unsigned long h;
} gdr_mh_t;

// Map device memory buffer on GPU BAR1, returning an handle.
Expand Down
Loading