File: smsc_knem_component.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (224 lines) | stat: -rw-r--r-- 7,548 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2014-2017 Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2021      Google, Inc. All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include "opal/mca/smsc/base/base.h"
#include "opal/mca/smsc/knem/smsc_knem_internal.h"
#include "opal/util/show_help.h"

#include <fcntl.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static int mca_smsc_knem_component_register(void);
static int mca_smsc_knem_component_open(void);
static int mca_smsc_knem_component_close(void);
static int mca_smsc_knem_component_query(void);
static mca_smsc_module_t *mca_smsc_knem_component_enable(void);

static int mca_smsc_knem_fini(void);

#define MCA_SMSC_KNEM_DEFAULT_PRIORITY 23
static const int mca_smsc_knem_default_priority = MCA_SMSC_KNEM_DEFAULT_PRIORITY;

mca_smsc_knem_component_t mca_smsc_knem_component = {
    .super = {
        .smsc_version = {
            MCA_SMSC_DEFAULT_VERSION("knem"),
            .mca_open_component = mca_smsc_knem_component_open,
            .mca_close_component = mca_smsc_knem_component_close,
            .mca_register_component_params = mca_smsc_knem_component_register,
        },
        .priority = MCA_SMSC_KNEM_DEFAULT_PRIORITY,
        .query = mca_smsc_knem_component_query,
        .enable = mca_smsc_knem_component_enable,
    },
};

static int mca_smsc_knem_component_register(void)
{
    /* Currently disabling DMA mode by default; it's not clear that this is useful in all
     * applications and architectures. */
    mca_smsc_knem_component.dma_min = 0;
    (void) mca_base_component_var_register(
        &mca_smsc_knem_component.super.smsc_version, "dma_min",
        "Minimum message size (in bytes) to use the knem DMA mode; "
        "ignored if knem does not support DMA mode (0 = do not use the "
        "knem DMA mode, default: 0)",
        MCA_BASE_VAR_TYPE_UNSIGNED_INT, NULL, 0, 0, OPAL_INFO_LVL_9, MCA_BASE_VAR_SCOPE_READONLY,
        &mca_smsc_knem_component.dma_min);

    mca_smsc_base_register_default_params(&mca_smsc_knem_component.super,
                                          mca_smsc_knem_default_priority);
    return OPAL_SUCCESS;
}

static int mca_smsc_knem_component_open(void)
{
    /* nothing to do */
    return OPAL_SUCCESS;
}

static int mca_smsc_knem_component_close(void)
{
    return mca_smsc_knem_fini();
}

static int mca_smsc_knem_get_info(struct knem_cmd_info *knem_info)
{
    /* Only show the help message if this is the only component. */
    bool show_help = (1 == opal_list_get_size(&opal_smsc_base_framework.framework_components));

    /* Check that the ABI if kernel module running is the same
     * as what we were compiled against. */
    memset(knem_info, 0, sizeof(*knem_info));
    int rc = ioctl(mca_smsc_knem_component.knem_fd, KNEM_CMD_GET_INFO, knem_info);
    if (rc < 0) {
        if (show_help) {
            opal_show_help("help-smsc-knem.txt", "knem get ABI fail", true,
                           opal_process_info.nodename, errno, strerror(errno));
        }
        return OPAL_ERR_NOT_AVAILABLE;
    }

    if (KNEM_ABI_VERSION != knem_info->abi) {
        if (show_help) {
            opal_show_help("help-smsc-knem.txt", "knem ABI mismatch", true,
                           opal_process_info.nodename, KNEM_ABI_VERSION, knem_info->abi);
        }
        return OPAL_ERR_NOT_AVAILABLE;
    }

    return OPAL_SUCCESS;
}
static int mca_smsc_knem_reg(void *reg_data, void *base, size_t size,
                             mca_rcache_base_registration_t *reg)
{
    mca_smsc_knem_registration_handle_t *knem_reg = (mca_smsc_knem_registration_handle_t *) reg;
    struct knem_cmd_create_region knem_cr;
    struct knem_cmd_param_iovec knem_iov;

    knem_iov.base = (uintptr_t) base;
    knem_iov.len = size;

    knem_cr.iovec_array = (uintptr_t) &knem_iov;
    knem_cr.iovec_nr = 1;
    knem_cr.protection = 0;

    if (reg->access_flags & (MCA_RCACHE_ACCESS_LOCAL_WRITE | MCA_RCACHE_ACCESS_REMOTE_WRITE)) {
        knem_cr.protection |= PROT_WRITE;
    }

    if (reg->access_flags & MCA_RCACHE_ACCESS_REMOTE_READ) {
        knem_cr.protection |= PROT_READ;
    }

    /* We will explicitly destroy this cookie. Do not use the single-use flag here. */
    knem_cr.flags = 0;
    if (OPAL_UNLIKELY(ioctl(mca_smsc_knem_component.knem_fd, KNEM_CMD_CREATE_REGION, &knem_cr)
                      < 0)) {
        return OPAL_ERROR;
    }

    knem_reg->data.cookie = knem_cr.cookie;
    knem_reg->data.base_addr = (intptr_t) base;

    return OPAL_SUCCESS;
}

static int mca_smsc_knem_dereg(void *reg_data, mca_rcache_base_registration_t *reg)
{
    mca_smsc_knem_registration_handle_t *knem_reg = (mca_smsc_knem_registration_handle_t *) reg;

    /* NTH: explicitly ignore the return code. Don't care about this cookie anymore anyway. */
    (void) ioctl(mca_smsc_knem_component.knem_fd, KNEM_CMD_DESTROY_REGION, &knem_reg->data.cookie);

    return OPAL_SUCCESS;
}

static int mca_smsc_knem_fini(void)
{
    if (-1 != mca_smsc_knem_component.knem_fd) {
        close(mca_smsc_knem_component.knem_fd);
        mca_smsc_knem_component.knem_fd = -1;
    }

    if (mca_smsc_knem_module.rcache) {
        (void) mca_rcache_base_module_destroy(mca_smsc_knem_module.rcache);
        mca_smsc_knem_module.rcache = NULL;
    }

    return OPAL_SUCCESS;
}

static int mca_smsc_knem_component_query(void)
{
    struct knem_cmd_info knem_info;
    int rc;

    /* Open the knem device.  Try to print a helpful message if we
       fail to open it. */
    mca_smsc_knem_component.knem_fd = open("/dev/knem", O_RDWR);
    if (mca_smsc_knem_component.knem_fd < 0) {
        if (EACCES == errno) {
            struct stat sbuf;
            if (0 != stat("/dev/knem", &sbuf)) {
                sbuf.st_mode = 0;
            }
            opal_show_help("help-smsc-knem.txt", "knem permission denied", true,
                           opal_process_info.nodename, sbuf.st_mode);
        } else {
            opal_show_help("help-smsc-knem.txt", "knem fail open", true, opal_process_info.nodename,
                           errno, strerror(errno));
        }

        return OPAL_ERR_NOT_AVAILABLE;
    }

    rc = mca_smsc_knem_get_info(&knem_info);
    if (OPAL_SUCCESS != rc) {
        mca_smsc_knem_fini();
        return rc;
    }

    if (!(mca_smsc_knem_component.dma_min && (knem_info.features & KNEM_FEATURE_DMA))) {
        /* disable DMA */
        mca_smsc_knem_component.dma_min = UINT_MAX;
    }

    return OPAL_SUCCESS;
}

static mca_smsc_module_t *mca_smsc_knem_component_enable(void)
{
    if (0 > mca_smsc_knem_component.super.priority) {
        return NULL;
    }

    mca_rcache_base_resources_t rcache_resources = {.cache_name = "smsc_knem",
                                                    .reg_data = NULL,
                                                    .sizeof_reg = sizeof(
                                                        mca_smsc_knem_registration_handle_t),
                                                    .register_mem = mca_smsc_knem_reg,
                                                    .deregister_mem = mca_smsc_knem_dereg};

    mca_smsc_knem_module.rcache = mca_rcache_base_module_create("grdma", NULL, &rcache_resources);
    if (NULL == mca_smsc_knem_module.rcache) {
        return NULL;
    }

    return &mca_smsc_knem_module.super;
}