From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9504BC433ED for ; Mon, 12 Apr 2021 23:28:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0EE87610FC for ; Mon, 12 Apr 2021 23:28:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0EE87610FC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A00738D0006; Mon, 12 Apr 2021 19:28:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D8978D0001; Mon, 12 Apr 2021 19:28:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78F208D0006; Mon, 12 Apr 2021 19:28:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0187.hostedemail.com [216.40.44.187]) by kanga.kvack.org (Postfix) with ESMTP id 513258D0001 for ; Mon, 12 Apr 2021 19:28:09 -0400 (EDT) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 08510180ACF62 for ; Mon, 12 Apr 2021 23:28:09 +0000 (UTC) X-FDA: 78025305498.13.A3E9C4A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf15.hostedemail.com (Postfix) with ESMTP id EDADEA00039B for ; Mon, 12 Apr 2021 23:28:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618270088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BIlUvHrSpn5Lb9pAOjajK4yBauPRJvKURvgmPYRmI9U=; b=Yt9IRbFbt7DwrK+2hlpzC2J5HzaZ8KmxqbCivZoAV27owguh6G8aiU2wJtYXBthrPh4MAQ qq7IVGl6vz+nX95CofFHGBFsqOk3a2/eWP56aGqD3xXey16IA+3Ch2/iakSEun1EprvSik PJ7ZuOEjyvkoA7k6Gu4SrOCYZhkEshU= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-41-3hwPgYiUMY6pngy_tDVYcA-1; Mon, 12 Apr 2021 19:28:06 -0400 X-MC-Unique: 3hwPgYiUMY6pngy_tDVYcA-1 Received: by mail-qt1-f198.google.com with SMTP id t18so174479qtw.15 for ; Mon, 12 Apr 2021 16:28:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BIlUvHrSpn5Lb9pAOjajK4yBauPRJvKURvgmPYRmI9U=; b=Sjavp8zK/2y6ipBV/SNG5AE7RZERU0KoZ7XBlDJKqgpLVidOVFXyH1nVwqg8xJ9U+U DYQ1fhgeqFDMmgkZU+eKoM1AEp1UlUdyVnunQZJXW1o9EePDPKM0D4doGNF6PVGj+44S Tqruvvds3f9HrD5zGLWkbILRoeoR8XdfULUBRnoOjMRjcnPMhB3UVmqEapJAv/ZciRQn dQy6Yg5oKcccGQ6m5ezQQV8IxGs+qoFGbAWja3r+5XLbdNRXPZIqSkjTq6gx7YKZtqss jSznkSMVSMQTyWeValiVT11srFNXHr2hXUcsQ6VEizCVU9jPKtex59U5q0FE64EY4qsm bI4w== X-Gm-Message-State: AOAM532b+2vUci8RLuYJvZM8R1bs/pWDqnLcxMfmfETF0AGxnOWsKmZI mV/cmWJ79raQ65+kzguc5WL3UMnNLJ7TSJDGbXftU4Rvg5Ml7yJ9c4CbzMCZspVujy/00d5oN8y R2TPaQi3X82f4uyRY/zyhsBYhoLzWUinuQeJr/Ii35T2VYoM6fVnOAUYrmlVn X-Received: by 2002:a05:6214:20e9:: with SMTP id 9mr29562378qvk.36.1618270084860; Mon, 12 Apr 2021 16:28:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLcGWN5a0nCYPjy8ijR51JC6ki/iJj8NH+LPRfP33rK0sQmOdOvtDicXdxvh/zhCNhBza65g== X-Received: by 2002:a05:6214:20e9:: with SMTP id 9mr29562330qvk.36.1618270084107; Mon, 12 Apr 2021 16:28:04 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-88-174-93-75-154.dsl.bell.ca. [174.93.75.154]) by smtp.gmail.com with ESMTPSA id y29sm8958925qtm.13.2021.04.12.16.28.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 16:28:02 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrea Arcangeli , peterx@redhat.com, Andrew Morton , Mike Rapoport , Axel Rasmussen Subject: [PATCH v2 5/5] userfaultfd/selftests: Unify error handling Date: Mon, 12 Apr 2021 19:27:53 -0400 Message-Id: <20210412232753.1012412-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210412232753.1012412-1-peterx@redhat.com> References: <20210412232753.1012412-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="US-ASCII" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: EDADEA00039B X-Stat-Signature: 5nq857ycq9wzeck9otxafq7dnjkt4r5m Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf15; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618270086-556494 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce err()/_err() and replace all the different ways to fail the pro= gram, mostly "fprintf" and "perror" with tons of exit() calls. Always stop the = test program at any failure. Reviewed-by: Axel Rasmussen Signed-off-by: Peter Xu --- tools/testing/selftests/vm/userfaultfd.c | 556 ++++++++--------------- 1 file changed, 187 insertions(+), 369 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/sel= ftests/vm/userfaultfd.c index da2374bda5a3..6339aeaeeff8 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -140,11 +140,18 @@ static void usage(void) exit(1); } =20 -#define uffd_error(code, fmt, ...) = \ - do { = \ - fprintf(stderr, fmt, ##__VA_ARGS__); \ - fprintf(stderr, ": %" PRId64 "\n", (int64_t)(code)); \ - exit(1); \ +#define _err(fmt, ...) \ + do { \ + int ret =3D errno; \ + fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__); \ + fprintf(stderr, " (errno=3D%d, line=3D%d)\n", \ + ret, __LINE__); \ + } while (0) + +#define err(fmt, ...) \ + do { \ + _err(fmt, ##__VA_ARGS__); \ + exit(1); \ } while (0) =20 static void uffd_stats_reset(struct uffd_stats *uffd_stats, @@ -193,44 +200,28 @@ static void uffd_stats_report(struct uffd_stats *st= ats, int n_cpus) printf("\n"); } =20 -static int anon_release_pages(char *rel_area) +static void anon_release_pages(char *rel_area) { - int ret =3D 0; - - if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) { - perror("madvise"); - ret =3D 1; - } - - return ret; + if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) + err("madvise(MADV_DONTNEED) failed"); } =20 static void anon_allocate_area(void **alloc_area) { - if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) { - fprintf(stderr, "out of memory\n"); - *alloc_area =3D NULL; - } + if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) + err("posix_memalign() failed"); } =20 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long o= ffset) { } =20 -/* HugeTLB memory */ -static int hugetlb_release_pages(char *rel_area) +static void hugetlb_release_pages(char *rel_area) { - int ret =3D 0; - if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, - rel_area =3D=3D huge_fd_off0 ? 0 : - nr_pages * page_size, - nr_pages * page_size)) { - perror("fallocate"); - ret =3D 1; - } - - return ret; + rel_area =3D=3D huge_fd_off0 ? 0 : nr_pages * page_size, + nr_pages * page_size)) + err("fallocate() failed"); } =20 static void hugetlb_allocate_area(void **alloc_area) @@ -243,20 +234,16 @@ static void hugetlb_allocate_area(void **alloc_area= ) MAP_HUGETLB, huge_fd, *alloc_area =3D=3D area_src ? 0 : nr_pages * page_size); - if (*alloc_area =3D=3D MAP_FAILED) { - perror("mmap of hugetlbfs file failed"); - goto fail; - } + if (*alloc_area =3D=3D MAP_FAILED) + err("mmap of hugetlbfs file failed"); =20 if (map_shared) { area_alias =3D mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE= , MAP_SHARED | MAP_HUGETLB, huge_fd, *alloc_area =3D=3D area_src ? 0 : nr_pages * page_size); - if (area_alias =3D=3D MAP_FAILED) { - perror("mmap of hugetlb file alias failed"); - goto fail_munmap; - } + if (area_alias =3D=3D MAP_FAILED) + err("mmap of hugetlb file alias failed"); } =20 if (*alloc_area =3D=3D area_src) { @@ -267,16 +254,6 @@ static void hugetlb_allocate_area(void **alloc_area) } if (area_alias) *alloc_area_alias =3D area_alias; - - return; - -fail_munmap: - if (munmap(*alloc_area, nr_pages * page_size) < 0) { - perror("hugetlb munmap"); - exit(1); - } -fail: - *alloc_area =3D NULL; } =20 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned lon= g offset) @@ -292,33 +269,24 @@ static void hugetlb_alias_mapping(__u64 *start, siz= e_t len, unsigned long offset *start =3D (unsigned long) area_dst_alias + offset; } =20 -/* Shared memory */ -static int shmem_release_pages(char *rel_area) +static void shmem_release_pages(char *rel_area) { - int ret =3D 0; - - if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) { - perror("madvise"); - ret =3D 1; - } - - return ret; + if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) + err("madvise(MADV_REMOVE) failed"); } =20 static void shmem_allocate_area(void **alloc_area) { *alloc_area =3D mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE= , MAP_ANONYMOUS | MAP_SHARED, -1, 0); - if (*alloc_area =3D=3D MAP_FAILED) { - fprintf(stderr, "shared memory mmap failed\n"); - *alloc_area =3D NULL; - } + if (*alloc_area =3D=3D MAP_FAILED) + err("mmap of memfd failed"); } =20 struct uffd_test_ops { unsigned long expected_ioctls; void (*allocate_area)(void **alloc_area); - int (*release_pages)(char *rel_area); + void (*release_pages)(char *rel_area); void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset); }; =20 @@ -373,11 +341,8 @@ static void wp_range(int ufd, __u64 start, __u64 len= , bool wp) /* Undo write-protect, do wakeup after that */ prms.mode =3D wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0; =20 - if (ioctl(ufd, UFFDIO_WRITEPROTECT, &prms)) { - fprintf(stderr, "clear WP failed for address 0x%" PRIx64 "\n", - (uint64_t)start); - exit(1); - } + if (ioctl(ufd, UFFDIO_WRITEPROTECT, &prms)) + err("clear WP failed: address=3D0x%"PRIx64, (uint64_t)start); } =20 static void continue_range(int ufd, __u64 start, __u64 len) @@ -388,12 +353,9 @@ static void continue_range(int ufd, __u64 start, __u= 64 len) req.range.len =3D len; req.mode =3D 0; =20 - if (ioctl(ufd, UFFDIO_CONTINUE, &req)) { - fprintf(stderr, - "UFFDIO_CONTINUE failed for address 0x%" PRIx64 "\n", - (uint64_t)start); - exit(1); - } + if (ioctl(ufd, UFFDIO_CONTINUE, &req)) + err("UFFDIO_CONTINUE failed for address 0x%" PRIx64, + (uint64_t)start); } =20 static void *locking_thread(void *arg) @@ -412,10 +374,8 @@ static void *locking_thread(void *arg) seed +=3D cpu; bzero(&rand, sizeof(rand)); bzero(&randstate, sizeof(randstate)); - if (initstate_r(seed, randstate, sizeof(randstate), &rand)) { - fprintf(stderr, "srandom_r error\n"); - exit(1); - } + if (initstate_r(seed, randstate, sizeof(randstate), &rand)) + err("initstate_r failed"); } else { page_nr =3D -bounces; if (!(bounces & BOUNCE_RACINGFAULTS)) @@ -424,16 +384,12 @@ static void *locking_thread(void *arg) =20 while (!finished) { if (bounces & BOUNCE_RANDOM) { - if (random_r(&rand, &rand_nr)) { - fprintf(stderr, "random_r 1 error\n"); - exit(1); - } + if (random_r(&rand, &rand_nr)) + err("random_r failed"); page_nr =3D rand_nr; if (sizeof(page_nr) > sizeof(rand_nr)) { - if (random_r(&rand, &rand_nr)) { - fprintf(stderr, "random_r 2 error\n"); - exit(1); - } + if (random_r(&rand, &rand_nr)) + err("random_r failed"); page_nr |=3D (((unsigned long) rand_nr) << 16) << 16; } @@ -442,12 +398,9 @@ static void *locking_thread(void *arg) page_nr %=3D nr_pages; pthread_mutex_lock(area_mutex(area_dst, page_nr)); count =3D *area_count(area_dst, page_nr); - if (count !=3D count_verify[page_nr]) { - fprintf(stderr, - "page_nr %lu memory corruption %Lu %Lu\n", - page_nr, count, - count_verify[page_nr]); exit(1); - } + if (count !=3D count_verify[page_nr]) + err("page_nr %lu memory corruption %llu %llu", + page_nr, count, count_verify[page_nr]); count++; *area_count(area_dst, page_nr) =3D count_verify[page_nr] =3D count; pthread_mutex_unlock(area_mutex(area_dst, page_nr)); @@ -464,22 +417,21 @@ static void retry_copy_page(int ufd, struct uffdio_= copy *uffdio_copy, offset); if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) { /* real retval in ufdio_copy.copy */ - if (uffdio_copy->copy !=3D -EEXIST) { - uffd_error(uffdio_copy->copy, - "UFFDIO_COPY retry error"); - } - } else - uffd_error(uffdio_copy->copy, "UFFDIO_COPY retry unexpected"); + if (uffdio_copy->copy !=3D -EEXIST) + err("UFFDIO_COPY retry error: %"PRId64, + (int64_t)uffdio_copy->copy); + } else { + err("UFFDIO_COPY retry unexpected: %"PRId64, + (int64_t)uffdio_copy->copy); + } } =20 static int __copy_page(int ufd, unsigned long offset, bool retry) { struct uffdio_copy uffdio_copy; =20 - if (offset >=3D nr_pages * page_size) { - fprintf(stderr, "unexpected offset %lu\n", offset); - exit(1); - } + if (offset >=3D nr_pages * page_size) + err("unexpected offset %lu\n", offset); uffdio_copy.dst =3D (unsigned long) area_dst + offset; uffdio_copy.src =3D (unsigned long) area_src + offset; uffdio_copy.len =3D page_size; @@ -491,9 +443,10 @@ static int __copy_page(int ufd, unsigned long offset= , bool retry) if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) { /* real retval in ufdio_copy.copy */ if (uffdio_copy.copy !=3D -EEXIST) - uffd_error(uffdio_copy.copy, "UFFDIO_COPY error"); + err("UFFDIO_COPY error: %"PRId64, + (int64_t)uffdio_copy.copy); } else if (uffdio_copy.copy !=3D page_size) { - uffd_error(uffdio_copy.copy, "UFFDIO_COPY unexpected copy"); + err("UFFDIO_COPY error: %"PRId64, (int64_t)uffdio_copy.copy); } else { if (test_uffdio_copy_eexist && retry) { test_uffdio_copy_eexist =3D false; @@ -522,11 +475,10 @@ static int uffd_read_msg(int ufd, struct uffd_msg *= msg) if (ret < 0) { if (errno =3D=3D EAGAIN) return 1; - perror("blocking read error"); + err("blocking read error"); } else { - fprintf(stderr, "short read\n"); + err("short read"); } - exit(1); } =20 return 0; @@ -537,10 +489,8 @@ static void uffd_handle_page_fault(struct uffd_msg *= msg, { unsigned long offset; =20 - if (msg->event !=3D UFFD_EVENT_PAGEFAULT) { - fprintf(stderr, "unexpected msg event %u\n", msg->event); - exit(1); - } + if (msg->event !=3D UFFD_EVENT_PAGEFAULT) + err("unexpected msg event %u", msg->event); =20 if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP) { /* Write protect page faults */ @@ -571,10 +521,8 @@ static void uffd_handle_page_fault(struct uffd_msg *= msg, stats->minor_faults++; } else { /* Missing page faults */ - if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) { - fprintf(stderr, "unexpected write fault\n"); - exit(1); - } + if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) + err("unexpected write fault"); =20 offset =3D (char *)(unsigned long)msg->arg.pagefault.address - area_ds= t; offset &=3D ~(page_size-1); @@ -601,32 +549,20 @@ static void *uffd_poll_thread(void *arg) =20 for (;;) { ret =3D poll(pollfd, 2, -1); - if (!ret) { - fprintf(stderr, "poll error %d\n", ret); - exit(1); - } - if (ret < 0) { - perror("poll"); - exit(1); - } + if (ret <=3D 0) + err("poll error: %d", ret); if (pollfd[1].revents & POLLIN) { - if (read(pollfd[1].fd, &tmp_chr, 1) !=3D 1) { - fprintf(stderr, "read pipefd error\n"); - exit(1); - } + if (read(pollfd[1].fd, &tmp_chr, 1) !=3D 1) + err("read pipefd error"); break; } - if (!(pollfd[0].revents & POLLIN)) { - fprintf(stderr, "pollfd[0].revents %d\n", - pollfd[0].revents); - exit(1); - } + if (!(pollfd[0].revents & POLLIN)) + err("pollfd[0].revents %d", pollfd[0].revents); if (uffd_read_msg(uffd, &msg)) continue; switch (msg.event) { default: - fprintf(stderr, "unexpected msg event %u\n", - msg.event); exit(1); + err("unexpected msg event %u\n", msg.event); break; case UFFD_EVENT_PAGEFAULT: uffd_handle_page_fault(&msg, stats); @@ -640,10 +576,8 @@ static void *uffd_poll_thread(void *arg) uffd_reg.range.start =3D msg.arg.remove.start; uffd_reg.range.len =3D msg.arg.remove.end - msg.arg.remove.start; - if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range)) { - fprintf(stderr, "remove failure\n"); - exit(1); - } + if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range)) + err("remove failure"); break; case UFFD_EVENT_REMAP: area_dst =3D (char *)(unsigned long)msg.arg.remap.to; @@ -746,9 +680,7 @@ static int stress(struct uffd_stats *uffd_stats) * UFFDIO_COPY without writing zero pages into area_dst * because the background threads already completed). */ - if (uffd_test_ops->release_pages(area_src)) - return 1; - + uffd_test_ops->release_pages(area_src); =20 finished =3D 1; for (cpu =3D 0; cpu < nr_cpus; cpu++) @@ -758,10 +690,8 @@ static int stress(struct uffd_stats *uffd_stats) for (cpu =3D 0; cpu < nr_cpus; cpu++) { char c; if (bounces & BOUNCE_POLL) { - if (write(pipefd[cpu*2+1], &c, 1) !=3D 1) { - fprintf(stderr, "pipefd write error\n"); - return 1; - } + if (write(pipefd[cpu*2+1], &c, 1) !=3D 1) + err("pipefd write error"); if (pthread_join(uffd_threads[cpu], (void *)&uffd_stats[cpu])) return 1; @@ -861,10 +791,8 @@ static int faulting_process(int signal_test) memset(&act, 0, sizeof(act)); act.sa_sigaction =3D sighndl; act.sa_flags =3D SA_SIGINFO; - if (sigaction(SIGBUS, &act, 0)) { - perror("sigaction"); - return 1; - } + if (sigaction(SIGBUS, &act, 0)) + err("sigaction"); lastnr =3D (unsigned long)-1; } =20 @@ -874,10 +802,8 @@ static int faulting_process(int signal_test) =20 if (signal_test) { if (sigsetjmp(*sigbuf, 1) !=3D 0) { - if (steps =3D=3D 1 && nr =3D=3D lastnr) { - fprintf(stderr, "Signal repeated\n"); - return 1; - } + if (steps =3D=3D 1 && nr =3D=3D lastnr) + err("Signal repeated"); =20 lastnr =3D nr; if (signal_test =3D=3D 1) { @@ -902,12 +828,9 @@ static int faulting_process(int signal_test) } =20 count =3D *area_count(area_dst, nr); - if (count !=3D count_verify[nr]) { - fprintf(stderr, - "nr %lu memory corruption %Lu %Lu\n", - nr, count, - count_verify[nr]); - } + if (count !=3D count_verify[nr]) + err("nr %lu memory corruption %llu %llu\n", + nr, count, count_verify[nr]); /* * Trigger write protection if there is by writing * the same value back. @@ -923,18 +846,14 @@ static int faulting_process(int signal_test) =20 area_dst =3D mremap(area_dst, nr_pages * page_size, nr_pages * page_si= ze, MREMAP_MAYMOVE | MREMAP_FIXED, area_src); - if (area_dst =3D=3D MAP_FAILED) { - perror("mremap"); - exit(1); - } + if (area_dst =3D=3D MAP_FAILED) + err("mremap"); =20 for (; nr < nr_pages; nr++) { count =3D *area_count(area_dst, nr); if (count !=3D count_verify[nr]) { - fprintf(stderr, - "nr %lu memory corruption %Lu %Lu\n", - nr, count, - count_verify[nr]); exit(1); + err("nr %lu memory corruption %llu %llu\n", + nr, count, count_verify[nr]); } /* * Trigger write protection if there is by writing @@ -943,15 +862,11 @@ static int faulting_process(int signal_test) *area_count(area_dst, nr) =3D count; } =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 - for (nr =3D 0; nr < nr_pages; nr++) { - if (my_bcmp(area_dst + nr * page_size, zeropage, page_size)) { - fprintf(stderr, "nr %lu is not zero\n", nr); - exit(1); - } - } + for (nr =3D 0; nr < nr_pages; nr++) + if (my_bcmp(area_dst + nr * page_size, zeropage, page_size)) + err("nr %lu is not zero", nr); =20 return 0; } @@ -964,13 +879,12 @@ static void retry_uffdio_zeropage(int ufd, uffdio_zeropage->range.len, offset); if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) { - if (uffdio_zeropage->zeropage !=3D -EEXIST) { - uffd_error(uffdio_zeropage->zeropage, - "UFFDIO_ZEROPAGE retry error"); - } + if (uffdio_zeropage->zeropage !=3D -EEXIST) + err("UFFDIO_ZEROPAGE error: %"PRId64, + (int64_t)uffdio_zeropage->zeropage); } else { - uffd_error(uffdio_zeropage->zeropage, - "UFFDIO_ZEROPAGE retry unexpected"); + err("UFFDIO_ZEROPAGE error: %"PRId64, + (int64_t)uffdio_zeropage->zeropage); } } =20 @@ -983,10 +897,8 @@ static int __uffdio_zeropage(int ufd, unsigned long = offset, bool retry) =20 has_zeropage =3D uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAG= E); =20 - if (offset >=3D nr_pages * page_size) { - fprintf(stderr, "unexpected offset %lu\n", offset); - exit(1); - } + if (offset >=3D nr_pages * page_size) + err("unexpected offset %lu", offset); uffdio_zeropage.range.start =3D (unsigned long) area_dst + offset; uffdio_zeropage.range.len =3D page_size; uffdio_zeropage.mode =3D 0; @@ -994,14 +906,13 @@ static int __uffdio_zeropage(int ufd, unsigned long= offset, bool retry) res =3D uffdio_zeropage.zeropage; if (ret) { /* real retval in ufdio_zeropage.zeropage */ - if (has_zeropage) { - uffd_error(res, "UFFDIO_ZEROPAGE %s", - res =3D=3D -EEXIST ? "-EEXIST" : "error"); - } else if (res !=3D -EINVAL) - uffd_error(res, "UFFDIO_ZEROPAGE not -EINVAL"); + if (has_zeropage) + err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res); + else if (res !=3D -EINVAL) + err("UFFDIO_ZEROPAGE not -EINVAL"); } else if (has_zeropage) { if (res !=3D page_size) { - uffd_error(res, "UFFDIO_ZEROPAGE unexpected"); + err("UFFDIO_ZEROPAGE unexpected size"); } else { if (test_uffdio_zeropage_eexist && retry) { test_uffdio_zeropage_eexist =3D false; @@ -1011,7 +922,7 @@ static int __uffdio_zeropage(int ufd, unsigned long = offset, bool retry) return 1; } } else - uffd_error(res, "UFFDIO_ZEROPAGE succeeded"); + err("UFFDIO_ZEROPAGE succeeded"); =20 return 0; } @@ -1030,8 +941,7 @@ static int userfaultfd_zeropage_test(void) printf("testing UFFDIO_ZEROPAGE: "); fflush(stdout); =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 if (userfaultfd_open(0)) return 1; @@ -1040,25 +950,16 @@ static int userfaultfd_zeropage_test(void) uffdio_register.mode =3D UFFDIO_REGISTER_MODE_MISSING; if (test_uffdio_wp) uffdio_register.mode |=3D UFFDIO_REGISTER_MODE_WP; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure\n"); - exit(1); - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure"); =20 expected_ioctls =3D uffd_test_ops->expected_ioctls; - if ((uffdio_register.ioctls & expected_ioctls) !=3D - expected_ioctls) { - fprintf(stderr, - "unexpected missing ioctl for anon memory\n"); - exit(1); - } + if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) + err("unexpected missing ioctl for anon memory"); =20 - if (uffdio_zeropage(uffd, 0)) { - if (my_bcmp(area_dst, zeropage, page_size)) { - fprintf(stderr, "zeropage is not zero\n"); - exit(1); - } - } + if (uffdio_zeropage(uffd, 0)) + if (my_bcmp(area_dst, zeropage, page_size)) + err("zeropage is not zero"); =20 close(uffd); printf("done.\n"); @@ -1078,8 +979,7 @@ static int userfaultfd_events_test(void) printf("testing events (fork, remap, remove): "); fflush(stdout); =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 features =3D UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP | UFFD_FEATURE_EVENT_REMOVE; @@ -1092,41 +992,28 @@ static int userfaultfd_events_test(void) uffdio_register.mode =3D UFFDIO_REGISTER_MODE_MISSING; if (test_uffdio_wp) uffdio_register.mode |=3D UFFDIO_REGISTER_MODE_WP; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure\n"); - exit(1); - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure"); =20 expected_ioctls =3D uffd_test_ops->expected_ioctls; - if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) { - fprintf(stderr, "unexpected missing ioctl for anon memory\n"); - exit(1); - } + if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) + err("unexpected missing ioctl for anon memory"); =20 - if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) { - perror("uffd_poll_thread create"); - exit(1); - } + if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) + err("uffd_poll_thread create"); =20 pid =3D fork(); - if (pid < 0) { - perror("fork"); - exit(1); - } + if (pid < 0) + err("fork"); =20 if (!pid) exit(faulting_process(0)); =20 waitpid(pid, &err, 0); - if (err) { - fprintf(stderr, "faulting process failed\n"); - exit(1); - } - - if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) { - perror("pipe write"); - exit(1); - } + if (err) + err("faulting process failed"); + if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) + err("pipe write"); if (pthread_join(uffd_mon, NULL)) return 1; =20 @@ -1151,8 +1038,7 @@ static int userfaultfd_sig_test(void) printf("testing signal delivery: "); fflush(stdout); =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 features =3D UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS; if (userfaultfd_open(features)) @@ -1164,57 +1050,41 @@ static int userfaultfd_sig_test(void) uffdio_register.mode =3D UFFDIO_REGISTER_MODE_MISSING; if (test_uffdio_wp) uffdio_register.mode |=3D UFFDIO_REGISTER_MODE_WP; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure\n"); - exit(1); - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure"); =20 expected_ioctls =3D uffd_test_ops->expected_ioctls; - if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) { - fprintf(stderr, "unexpected missing ioctl for anon memory\n"); - exit(1); - } + if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) + err("unexpected missing ioctl for anon memory"); =20 - if (faulting_process(1)) { - fprintf(stderr, "faulting process failed\n"); - exit(1); - } + if (faulting_process(1)) + err("faulting process failed"); =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 - if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) { - perror("uffd_poll_thread create"); - exit(1); - } + if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) + err("uffd_poll_thread create"); =20 pid =3D fork(); - if (pid < 0) { - perror("fork"); - exit(1); - } + if (pid < 0) + err("fork"); =20 if (!pid) exit(faulting_process(2)); =20 waitpid(pid, &err, 0); - if (err) { - fprintf(stderr, "faulting process failed\n"); - exit(1); - } - - if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) { - perror("pipe write"); - exit(1); - } + if (err) + err("faulting process failed"); + if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) + err("pipe write"); if (pthread_join(uffd_mon, (void **)&userfaults)) return 1; =20 printf("done.\n"); if (userfaults) - fprintf(stderr, "Signal test failed, userfaults: %ld\n", - userfaults); + err("Signal test failed, userfaults: %ld", userfaults); close(uffd); + return userfaults !=3D 0; } =20 @@ -1236,8 +1106,7 @@ static int userfaultfd_minor_test(void) printf("testing minor faults: "); fflush(stdout); =20 - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 if (userfaultfd_open_ext(&features)) return 1; @@ -1251,17 +1120,13 @@ static int userfaultfd_minor_test(void) uffdio_register.range.start =3D (unsigned long)area_dst_alias; uffdio_register.range.len =3D nr_pages * page_size; uffdio_register.mode =3D UFFDIO_REGISTER_MODE_MINOR; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure\n"); - exit(1); - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure"); =20 expected_ioctls =3D uffd_test_ops->expected_ioctls; expected_ioctls |=3D 1 << _UFFDIO_CONTINUE; - if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) { - fprintf(stderr, "unexpected missing ioctl(s)\n"); - exit(1); - } + if ((uffdio_register.ioctls & expected_ioctls) !=3D expected_ioctls) + err("unexpected missing ioctl(s)"); =20 /* * After registering with UFFD, populate the non-UFFD-registered side o= f @@ -1272,10 +1137,8 @@ static int userfaultfd_minor_test(void) page_size); } =20 - if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) { - perror("uffd_poll_thread create"); - exit(1); - } + if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats)) + err("uffd_poll_thread create"); =20 /* * Read each of the pages back using the UFFD-registered mapping. We @@ -1284,26 +1147,19 @@ static int userfaultfd_minor_test(void) * page's contents, and then issuing a CONTINUE ioctl. */ =20 - if (posix_memalign(&expected_page, page_size, page_size)) { - fprintf(stderr, "out of memory\n"); - return 1; - } + if (posix_memalign(&expected_page, page_size, page_size)) + err("out of memory"); =20 for (p =3D 0; p < nr_pages; ++p) { expected_byte =3D ~((uint8_t)(p % ((uint8_t)-1))); memset(expected_page, expected_byte, page_size); if (my_bcmp(expected_page, area_dst_alias + (p * page_size), - page_size)) { - fprintf(stderr, - "unexpected page contents after minor fault\n"); - exit(1); - } + page_size)) + err("unexpected page contents after minor fault"); } =20 - if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) { - perror("pipe write"); - exit(1); - } + if (write(pipefd[1], &c, sizeof(c)) !=3D sizeof(c)) + err("pipe write"); if (pthread_join(uffd_mon, NULL)) return 1; =20 @@ -1321,7 +1177,6 @@ static int userfaultfd_stress(void) unsigned long nr; struct uffdio_register uffdio_register; unsigned long cpu; - int err; struct uffd_stats uffd_stats[nr_cpus]; =20 uffd_test_ops->allocate_area((void **)&area_src); @@ -1366,10 +1221,8 @@ static int userfaultfd_stress(void) } } =20 - if (posix_memalign(&area, page_size, page_size)) { - fprintf(stderr, "out of memory\n"); - return 1; - } + if (posix_memalign(&area, page_size, page_size)) + err("out of memory"); zeropage =3D area; bzero(zeropage, page_size); =20 @@ -1378,7 +1231,6 @@ static int userfaultfd_stress(void) pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 16*1024*1024); =20 - err =3D 0; while (bounces--) { unsigned long expected_ioctls; =20 @@ -1407,25 +1259,18 @@ static int userfaultfd_stress(void) uffdio_register.mode =3D UFFDIO_REGISTER_MODE_MISSING; if (test_uffdio_wp) uffdio_register.mode |=3D UFFDIO_REGISTER_MODE_WP; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure\n"); - return 1; - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure"); expected_ioctls =3D uffd_test_ops->expected_ioctls; if ((uffdio_register.ioctls & expected_ioctls) !=3D - expected_ioctls) { - fprintf(stderr, - "unexpected missing ioctl for anon memory\n"); - return 1; - } + expected_ioctls) + err("unexpected missing ioctl for anon memory"); =20 if (area_dst_alias) { uffdio_register.range.start =3D (unsigned long) area_dst_alias; - if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { - fprintf(stderr, "register failure alias\n"); - return 1; - } + if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) + err("register failure alias"); } =20 /* @@ -1452,8 +1297,7 @@ static int userfaultfd_stress(void) * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's * required to MADV_DONTNEED here. */ - if (uffd_test_ops->release_pages(area_dst)) - return 1; + uffd_test_ops->release_pages(area_dst); =20 uffd_stats_reset(uffd_stats, nr_cpus); =20 @@ -1467,33 +1311,22 @@ static int userfaultfd_stress(void) nr_pages * page_size, false); =20 /* unregister */ - if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) { - fprintf(stderr, "unregister failure\n"); - return 1; - } + if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) + err("unregister failure"); if (area_dst_alias) { uffdio_register.range.start =3D (unsigned long) area_dst; if (ioctl(uffd, UFFDIO_UNREGISTER, - &uffdio_register.range)) { - fprintf(stderr, "unregister failure alias\n"); - return 1; - } + &uffdio_register.range)) + err("unregister failure alias"); } =20 /* verification */ - if (bounces & BOUNCE_VERIFY) { - for (nr =3D 0; nr < nr_pages; nr++) { - if (*area_count(area_dst, nr) !=3D count_verify[nr]) { - fprintf(stderr, - "error area_count %Lu %Lu %lu\n", - *area_count(area_src, nr), - count_verify[nr], - nr); - err =3D 1; - bounces =3D 0; - } - } - } + if (bounces & BOUNCE_VERIFY) + for (nr =3D 0; nr < nr_pages; nr++) + if (*area_count(area_dst, nr) !=3D count_verify[nr]) + err("error area_count %llu %llu %lu\n", + *area_count(area_src, nr), + count_verify[nr], nr); =20 /* prepare next bounce */ tmp_area =3D area_src; @@ -1507,9 +1340,6 @@ static int userfaultfd_stress(void) uffd_stats_report(uffd_stats, nr_cpus); } =20 - if (err) - return err; - close(uffd); return userfaultfd_zeropage_test() || userfaultfd_sig_test() || userfaultfd_events_test() || userfaultfd_minor_test(); @@ -1560,7 +1390,7 @@ static void set_test_type(const char *type) test_type =3D TEST_SHMEM; uffd_test_ops =3D &shmem_uffd_test_ops; } else { - fprintf(stderr, "Unknown test type: %s\n", type); exit(1); + err("Unknown test type: %s", type); } =20 if (test_type =3D=3D TEST_HUGETLB) @@ -1568,15 +1398,11 @@ static void set_test_type(const char *type) else page_size =3D sysconf(_SC_PAGE_SIZE); =20 - if (!page_size) { - fprintf(stderr, "Unable to determine page size\n"); - exit(2); - } + if (!page_size) + err("Unable to determine page size"); if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * = 2 - > page_size) { - fprintf(stderr, "Impossible to run this test\n"); - exit(2); - } + > page_size) + err("Impossible to run this test"); } =20 static void sigalrm(int sig) @@ -1593,10 +1419,8 @@ int main(int argc, char **argv) if (argc < 4) usage(); =20 - if (signal(SIGALRM, sigalrm) =3D=3D SIG_ERR) { - fprintf(stderr, "failed to arm SIGALRM"); - exit(1); - } + if (signal(SIGALRM, sigalrm) =3D=3D SIG_ERR) + err("failed to arm SIGALRM"); alarm(ALARM_INTERVAL_SECS); =20 set_test_type(argv[1]); @@ -1605,13 +1429,13 @@ int main(int argc, char **argv) nr_pages_per_cpu =3D atol(argv[2]) * 1024*1024 / page_size / nr_cpus; if (!nr_pages_per_cpu) { - fprintf(stderr, "invalid MiB\n"); + _err("invalid MiB"); usage(); } =20 bounces =3D atoi(argv[3]); if (bounces <=3D 0) { - fprintf(stderr, "invalid bounces\n"); + _err("invalid bounces"); usage(); } nr_pages =3D nr_pages_per_cpu * nr_cpus; @@ -1620,16 +1444,10 @@ int main(int argc, char **argv) if (argc < 5) usage(); huge_fd =3D open(argv[4], O_CREAT | O_RDWR, 0755); - if (huge_fd < 0) { - fprintf(stderr, "Open of %s failed", argv[3]); - perror("open"); - exit(1); - } - if (ftruncate(huge_fd, 0)) { - fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]); - perror("ftruncate"); - exit(1); - } + if (huge_fd < 0) + err("Open of %s failed", argv[4]); + if (ftruncate(huge_fd, 0)) + err("ftruncate %s to size 0 failed", argv[4]); } printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n", nr_pages, nr_pages_per_cpu); --=20 2.26.2