"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-pkey.c" (15 Mar 2019, 3347 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-pkey.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.49_vs_0.09.50.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #define PAGES_TO_EXERCISE   (8)
   28 
   29 #if defined(HAVE_PKEY_MPROTECT)
   30 
   31 /*
   32  *  stress_pkey()
   33  *  stress pkeys
   34  */
   35 static int stress_pkey(const args_t *args)
   36 {
   37     uint8_t *pages;
   38     int rc = EXIT_SUCCESS;
   39     const size_t page_size = args->page_size;
   40     const size_t pages_size = page_size * PAGES_TO_EXERCISE;
   41 
   42     pages = mmap(NULL, pages_size, PROT_READ | PROT_WRITE,
   43         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   44     if (pages == MAP_FAILED) {
   45         pr_inf("%s: cannot allocate a page, errno=%d (%s)\n",
   46             args->name, errno, strerror(errno));
   47         return EXIT_NO_RESOURCE;
   48     }
   49 
   50     do {
   51         int pkey, ret;
   52         const int page_num = (mwc8() % PAGES_TO_EXERCISE);
   53         const size_t page_offset = page_num * args->page_size;
   54         uint8_t *page = pages + page_offset;
   55 
   56         pkey = shim_pkey_alloc(0, 0);
   57         if (pkey < 0) {
   58             /*
   59              *  Can't allocate, perhaps we don't have any, or
   60              *  the system does not provide support, or the syscall
   61              *  was not available. All is not lost, as we can
   62              *  perform pkey_mprotect on a -ve pkey, this should
   63              *  fall back and perform the standard mprotect call.
   64              */
   65             pkey = -1;
   66         }
   67 
   68         ret = shim_pkey_mprotect(page, page_size, PROT_NONE, pkey);
   69         if (ret < 0) {
   70             if (errno == ENOSYS) {
   71                 pr_inf("%s: pkey system calls not implemented, skipping\n",
   72                     args->name);
   73                 rc = EXIT_NOT_IMPLEMENTED;
   74                 break;
   75             }
   76         }
   77         (void)shim_pkey_mprotect(page, page_size, PROT_READ, pkey);
   78         (void)shim_pkey_mprotect(page, page_size, PROT_WRITE, pkey);
   79         (void)shim_pkey_mprotect(page, page_size, PROT_READ | PROT_WRITE, pkey);
   80 
   81         (void)shim_pkey_mprotect(page, page_size, PROT_EXEC, pkey);
   82         (void)shim_pkey_mprotect(page, page_size, PROT_READ | PROT_EXEC, pkey);
   83         (void)shim_pkey_mprotect(page, page_size, PROT_WRITE | PROT_EXEC, pkey);
   84         (void)shim_pkey_mprotect(page, page_size, PROT_READ | PROT_WRITE | PROT_EXEC, pkey);
   85 
   86         if (pkey >= 0) {
   87             int rights;
   88 
   89             rights = shim_pkey_get(pkey);
   90             if (rights > -1) 
   91                 (void)shim_pkey_set(pkey, rights);
   92             (void)shim_pkey_free(pkey);
   93         }
   94         inc_counter(args);
   95     } while (keep_stressing());
   96 
   97     (void)munmap(pages, pages_size);
   98     return rc;
   99 }
  100 
  101 stressor_info_t stress_pkey_info = {
  102     .stressor = stress_pkey,
  103     .class = CLASS_CPU
  104 };
  105 #else
  106 stressor_info_t stress_pkey_info = {
  107     .stressor = stress_not_implemented,
  108     .class = CLASS_CPU
  109 };
  110 #endif