From 18c8de3428e7a75130f5c52da1a57562eccd80fc Mon Sep 17 00:00:00 2001 From: jsteube Date: Tue, 22 Aug 2017 15:19:25 +0200 Subject: [PATCH] Use zero-copy for main password buffer using CL_MEM_ALLOC_HOST_PTR and hc_clEnqueueMapBuffer() --- src/opencl.c | 62 ++++++++++++++++++++++++++++++++++++++++++-------- src/wordlist.c | 18 +++++++-------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/opencl.c b/src/opencl.c index 6b1e3271c..e1a4babc0 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -975,7 +975,7 @@ int hc_clReleaseContext (hashcat_ctx_t *hashcat_ctx, cl_context context) return 0; } -int hc_clEnqueueMapBuffer (hashcat_ctx_t *hashcat_ctx, cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, cl_map_flags map_flags, size_t offset, size_t cb, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event, void **buf) +int hc_clEnqueueMapBuffer (hashcat_ctx_t *hashcat_ctx, cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_map, cl_map_flags map_flags, size_t offset, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event, void **buf) { opencl_ctx_t *opencl_ctx = hashcat_ctx->opencl_ctx; @@ -983,7 +983,7 @@ int hc_clEnqueueMapBuffer (hashcat_ctx_t *hashcat_ctx, cl_command_queue command_ cl_int CL_err; - *buf = ocl->clEnqueueMapBuffer (command_queue, buffer, blocking_read, map_flags, offset, cb, num_events_in_wait_list, event_wait_list, event, &CL_err); + *buf = ocl->clEnqueueMapBuffer (command_queue, buffer, blocking_map, map_flags, offset, size, num_events_in_wait_list, event_wait_list, event, &CL_err); if (CL_err != CL_SUCCESS) { @@ -1794,7 +1794,17 @@ int run_copy (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT) { - const int CL_rc = hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, pws_cnt * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL); + int CL_rc; + + CL_rc = hc_clEnqueueUnmapMemObject (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, device_param->pws_buf, 0, NULL, NULL); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clFinish (hashcat_ctx, device_param->command_queue); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clEnqueueMapBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, CL_MAP_WRITE, 0, device_param->size_pws, 0, NULL, NULL, (void **) &device_param->pws_buf); if (CL_rc == -1) return -1; } @@ -1856,7 +1866,17 @@ int run_copy (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const } } - const int CL_rc = hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, pws_cnt * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL); + int CL_rc; + + CL_rc = hc_clEnqueueUnmapMemObject (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, device_param->pws_buf, 0, NULL, NULL); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clFinish (hashcat_ctx, device_param->command_queue); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clEnqueueMapBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, CL_MAP_WRITE, 0, device_param->size_pws, 0, NULL, NULL, (void **) &device_param->pws_buf); if (CL_rc == -1) return -1; } @@ -1864,13 +1884,33 @@ int run_copy (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const { if (user_options->attack_mode == ATTACK_MODE_COMBI) { - const int CL_rc = hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, pws_cnt * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL); + int CL_rc; + + CL_rc = hc_clEnqueueUnmapMemObject (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, device_param->pws_buf, 0, NULL, NULL); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clFinish (hashcat_ctx, device_param->command_queue); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clEnqueueMapBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, CL_MAP_WRITE, 0, device_param->size_pws, 0, NULL, NULL, (void **) &device_param->pws_buf); if (CL_rc == -1) return -1; } else if (user_options->attack_mode == ATTACK_MODE_HYBRID1) { - const int CL_rc = hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, pws_cnt * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL); + int CL_rc; + + CL_rc = hc_clEnqueueUnmapMemObject (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, device_param->pws_buf, 0, NULL, NULL); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clFinish (hashcat_ctx, device_param->command_queue); + + if (CL_rc == -1) return -1; + + CL_rc = hc_clEnqueueMapBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, CL_MAP_WRITE, 0, device_param->size_pws, 0, NULL, NULL, (void **) &device_param->pws_buf); if (CL_rc == -1) return -1; } @@ -4791,7 +4831,8 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) * global buffers */ - CL_rc = hc_clCreateBuffer (hashcat_ctx, device_param->context, CL_MEM_READ_ONLY, size_pws, NULL, &device_param->d_pws_buf); if (CL_rc == -1) return -1; + CL_rc = hc_clCreateBuffer (hashcat_ctx, device_param->context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, + size_pws, NULL, &device_param->d_pws_buf); if (CL_rc == -1) return -1; CL_rc = hc_clCreateBuffer (hashcat_ctx, device_param->context, CL_MEM_READ_ONLY, size_pws_amp, NULL, &device_param->d_pws_amp_buf); if (CL_rc == -1) return -1; CL_rc = hc_clCreateBuffer (hashcat_ctx, device_param->context, CL_MEM_READ_WRITE, size_tmps, NULL, &device_param->d_tmps); if (CL_rc == -1) return -1; CL_rc = hc_clCreateBuffer (hashcat_ctx, device_param->context, CL_MEM_READ_WRITE, size_hooks, NULL, &device_param->d_hooks); if (CL_rc == -1) return -1; @@ -4885,9 +4926,9 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) * main host data */ - pw_t *pws_buf = (pw_t *) hcmalloc (size_pws); + CL_rc = hc_clEnqueueMapBuffer (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, CL_MAP_WRITE, 0, device_param->size_pws, 0, NULL, NULL, (void **) &device_param->pws_buf); - device_param->pws_buf = pws_buf; + if (CL_rc == -1) return -1; pw_t *combs_buf = (pw_t *) hccalloc (KERNEL_COMBS, sizeof (pw_t)); @@ -5499,7 +5540,8 @@ void opencl_session_destroy (hashcat_ctx_t *hashcat_ctx) if (device_param->skipped == true) continue; - hcfree (device_param->pws_buf); + hc_clEnqueueUnmapMemObject (hashcat_ctx, device_param->command_queue, device_param->d_pws_buf, device_param->pws_buf, 0, NULL, NULL); + hcfree (device_param->combs_buf); hcfree (device_param->hooks_buf); diff --git a/src/wordlist.c b/src/wordlist.c index 82395c964..60a5f59fb 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -240,8 +240,8 @@ void get_next_word (hashcat_ctx_t *hashcat_ctx, FILE *fd, char **out_buf, u32 *o void pw_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len) { - //if (device_param->pws_cnt < device_param->kernel_power) - //{ + if (device_param->pws_cnt < device_param->kernel_power) + { pw_t *pw = (pw_t *) device_param->pws_buf + device_param->pws_cnt; u8 *ptr = (u8 *) pw->i; @@ -253,13 +253,13 @@ void pw_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len pw->pw_len = pw_len; device_param->pws_cnt++; - //} - //else - //{ - // fprintf (stderr, "BUG pw_add()!!\n"); - // - // return; - //} + } + else + { + fprintf (stderr, "BUG pw_add()!!\n"); + + return; + } } int count_words (hashcat_ctx_t *hashcat_ctx, FILE *fd, const char *dictfile, u64 *result)