| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * SPU core dump code |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * (C) Copyright 2006 IBM Corp. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Author: Dwayne Grant McConnell <decimal@us.ibm.com> |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 9 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 10 | | - * the Free Software Foundation; either version 2, or (at your option) |
|---|
| 11 | | - * any later version. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | | - * GNU General Public License for more details. |
|---|
| 17 | | - * |
|---|
| 18 | | - * You should have received a copy of the GNU General Public License |
|---|
| 19 | | - * along with this program; if not, write to the Free Software |
|---|
| 20 | | - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 21 | 8 | */ |
|---|
| 22 | 9 | |
|---|
| 23 | 10 | #include <linux/elf.h> |
|---|
| .. | .. |
|---|
| 33 | 20 | #include <linux/uaccess.h> |
|---|
| 34 | 21 | |
|---|
| 35 | 22 | #include "spufs.h" |
|---|
| 36 | | - |
|---|
| 37 | | -static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer, |
|---|
| 38 | | - size_t size, loff_t *off) |
|---|
| 39 | | -{ |
|---|
| 40 | | - u64 data; |
|---|
| 41 | | - int ret; |
|---|
| 42 | | - |
|---|
| 43 | | - if (spufs_coredump_read[num].read) |
|---|
| 44 | | - return spufs_coredump_read[num].read(ctx, buffer, size, off); |
|---|
| 45 | | - |
|---|
| 46 | | - data = spufs_coredump_read[num].get(ctx); |
|---|
| 47 | | - ret = snprintf(buffer, size, "0x%.16llx", data); |
|---|
| 48 | | - if (ret >= size) |
|---|
| 49 | | - return size; |
|---|
| 50 | | - return ++ret; /* count trailing NULL */ |
|---|
| 51 | | -} |
|---|
| 52 | 23 | |
|---|
| 53 | 24 | static int spufs_ctx_note_size(struct spu_context *ctx, int dfd) |
|---|
| 54 | 25 | { |
|---|
| .. | .. |
|---|
| 95 | 66 | */ |
|---|
| 96 | 67 | static struct spu_context *coredump_next_context(int *fd) |
|---|
| 97 | 68 | { |
|---|
| 69 | + struct spu_context *ctx; |
|---|
| 98 | 70 | struct file *file; |
|---|
| 99 | 71 | int n = iterate_fd(current->files, *fd, match_context, NULL); |
|---|
| 100 | 72 | if (!n) |
|---|
| 101 | 73 | return NULL; |
|---|
| 102 | 74 | *fd = n - 1; |
|---|
| 75 | + |
|---|
| 76 | + rcu_read_lock(); |
|---|
| 103 | 77 | file = fcheck(*fd); |
|---|
| 104 | | - return SPUFS_I(file_inode(file))->i_ctx; |
|---|
| 78 | + ctx = SPUFS_I(file_inode(file))->i_ctx; |
|---|
| 79 | + get_spu_context(ctx); |
|---|
| 80 | + rcu_read_unlock(); |
|---|
| 81 | + |
|---|
| 82 | + return ctx; |
|---|
| 105 | 83 | } |
|---|
| 106 | 84 | |
|---|
| 107 | 85 | int spufs_coredump_extra_notes_size(void) |
|---|
| .. | .. |
|---|
| 112 | 90 | fd = 0; |
|---|
| 113 | 91 | while ((ctx = coredump_next_context(&fd)) != NULL) { |
|---|
| 114 | 92 | rc = spu_acquire_saved(ctx); |
|---|
| 115 | | - if (rc) |
|---|
| 93 | + if (rc) { |
|---|
| 94 | + put_spu_context(ctx); |
|---|
| 116 | 95 | break; |
|---|
| 96 | + } |
|---|
| 97 | + |
|---|
| 117 | 98 | rc = spufs_ctx_note_size(ctx, fd); |
|---|
| 118 | 99 | spu_release_saved(ctx); |
|---|
| 119 | | - if (rc < 0) |
|---|
| 100 | + if (rc < 0) { |
|---|
| 101 | + put_spu_context(ctx); |
|---|
| 120 | 102 | break; |
|---|
| 103 | + } |
|---|
| 121 | 104 | |
|---|
| 122 | 105 | size += rc; |
|---|
| 123 | 106 | |
|---|
| 124 | 107 | /* start searching the next fd next time */ |
|---|
| 125 | 108 | fd++; |
|---|
| 109 | + put_spu_context(ctx); |
|---|
| 126 | 110 | } |
|---|
| 127 | 111 | |
|---|
| 128 | 112 | return size; |
|---|
| .. | .. |
|---|
| 131 | 115 | static int spufs_arch_write_note(struct spu_context *ctx, int i, |
|---|
| 132 | 116 | struct coredump_params *cprm, int dfd) |
|---|
| 133 | 117 | { |
|---|
| 134 | | - loff_t pos = 0; |
|---|
| 135 | | - int sz, rc, total = 0; |
|---|
| 136 | | - const int bufsz = PAGE_SIZE; |
|---|
| 137 | | - char *name; |
|---|
| 138 | | - char fullname[80], *buf; |
|---|
| 118 | + size_t sz = spufs_coredump_read[i].size; |
|---|
| 119 | + char fullname[80]; |
|---|
| 139 | 120 | struct elf_note en; |
|---|
| 140 | | - size_t skip; |
|---|
| 121 | + int ret; |
|---|
| 141 | 122 | |
|---|
| 142 | | - buf = (void *)get_zeroed_page(GFP_KERNEL); |
|---|
| 143 | | - if (!buf) |
|---|
| 144 | | - return -ENOMEM; |
|---|
| 145 | | - |
|---|
| 146 | | - name = spufs_coredump_read[i].name; |
|---|
| 147 | | - sz = spufs_coredump_read[i].size; |
|---|
| 148 | | - |
|---|
| 149 | | - sprintf(fullname, "SPU/%d/%s", dfd, name); |
|---|
| 123 | + sprintf(fullname, "SPU/%d/%s", dfd, spufs_coredump_read[i].name); |
|---|
| 150 | 124 | en.n_namesz = strlen(fullname) + 1; |
|---|
| 151 | 125 | en.n_descsz = sz; |
|---|
| 152 | 126 | en.n_type = NT_SPU; |
|---|
| 153 | 127 | |
|---|
| 154 | 128 | if (!dump_emit(cprm, &en, sizeof(en))) |
|---|
| 155 | | - goto Eio; |
|---|
| 156 | | - |
|---|
| 129 | + return -EIO; |
|---|
| 157 | 130 | if (!dump_emit(cprm, fullname, en.n_namesz)) |
|---|
| 158 | | - goto Eio; |
|---|
| 159 | | - |
|---|
| 131 | + return -EIO; |
|---|
| 160 | 132 | if (!dump_align(cprm, 4)) |
|---|
| 161 | | - goto Eio; |
|---|
| 133 | + return -EIO; |
|---|
| 162 | 134 | |
|---|
| 163 | | - do { |
|---|
| 164 | | - rc = do_coredump_read(i, ctx, buf, bufsz, &pos); |
|---|
| 165 | | - if (rc > 0) { |
|---|
| 166 | | - if (!dump_emit(cprm, buf, rc)) |
|---|
| 167 | | - goto Eio; |
|---|
| 168 | | - total += rc; |
|---|
| 169 | | - } |
|---|
| 170 | | - } while (rc == bufsz && total < sz); |
|---|
| 135 | + if (spufs_coredump_read[i].dump) { |
|---|
| 136 | + ret = spufs_coredump_read[i].dump(ctx, cprm); |
|---|
| 137 | + if (ret < 0) |
|---|
| 138 | + return ret; |
|---|
| 139 | + } else { |
|---|
| 140 | + char buf[32]; |
|---|
| 171 | 141 | |
|---|
| 172 | | - if (rc < 0) |
|---|
| 173 | | - goto out; |
|---|
| 142 | + ret = snprintf(buf, sizeof(buf), "0x%.16llx", |
|---|
| 143 | + spufs_coredump_read[i].get(ctx)); |
|---|
| 144 | + if (ret >= sizeof(buf)) |
|---|
| 145 | + return sizeof(buf); |
|---|
| 174 | 146 | |
|---|
| 175 | | - skip = roundup(cprm->pos - total + sz, 4) - cprm->pos; |
|---|
| 176 | | - if (!dump_skip(cprm, skip)) |
|---|
| 177 | | - goto Eio; |
|---|
| 147 | + /* count trailing the NULL: */ |
|---|
| 148 | + if (!dump_emit(cprm, buf, ret + 1)) |
|---|
| 149 | + return -EIO; |
|---|
| 150 | + } |
|---|
| 178 | 151 | |
|---|
| 179 | | - rc = 0; |
|---|
| 180 | | -out: |
|---|
| 181 | | - free_page((unsigned long)buf); |
|---|
| 182 | | - return rc; |
|---|
| 183 | | -Eio: |
|---|
| 184 | | - free_page((unsigned long)buf); |
|---|
| 185 | | - return -EIO; |
|---|
| 152 | + if (!dump_skip(cprm, roundup(cprm->pos - ret + sz, 4) - cprm->pos)) |
|---|
| 153 | + return -EIO; |
|---|
| 154 | + return 0; |
|---|
| 186 | 155 | } |
|---|
| 187 | 156 | |
|---|
| 188 | 157 | int spufs_coredump_extra_notes_write(struct coredump_params *cprm) |
|---|