Skip to content

Commit c2d466b

Browse files
keesaxboe
authored andcommitted
block: partitions: Replace pp_buf with struct seq_buf
In preparation for removing the strlcat API[1], replace the char *pp_buf with a struct seq_buf, which tracks the current write position and remaining space internally. This allows for: - Direct use of seq_buf_printf() in place of snprintf()+strlcat() pairs, eliminating local tmp buffers throughout. - Adjacent strlcat() calls that build strings piece-by-piece (e.g., strlcat("["); strlcat(name); strlcat("]")) to be collapsed into single seq_buf_printf() calls. - Simpler call sites: seq_buf_puts() takes only the buffer and string, with no need to pass PAGE_SIZE at every call. The backing buffer allocation is unchanged (__get_free_page), and the output path uses seq_buf_str() to NUL-terminate before passing to printk(). Link: KSPP/linux#370 [1] Cc: Andy Shevchenko <[email protected]> Cc: Josh Law <[email protected]> Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Josh Law <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 7b6d325 commit c2d466b

19 files changed

Lines changed: 106 additions & 157 deletions

File tree

block/partitions/acorn.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ adfs_partition(struct parsed_partitions *state, char *name, char *data,
4040
(le32_to_cpu(dr->disc_size) >> 9);
4141

4242
if (name) {
43-
strlcat(state->pp_buf, " [", PAGE_SIZE);
44-
strlcat(state->pp_buf, name, PAGE_SIZE);
45-
strlcat(state->pp_buf, "]", PAGE_SIZE);
43+
seq_buf_printf(&state->pp_buf, " [%s]", name);
4644
}
4745
put_partition(state, slot, first_sector, nr_sects);
4846
return dr;
@@ -78,14 +76,14 @@ static int riscix_partition(struct parsed_partitions *state,
7876
if (!rr)
7977
return -1;
8078

81-
strlcat(state->pp_buf, " [RISCiX]", PAGE_SIZE);
79+
seq_buf_puts(&state->pp_buf, " [RISCiX]");
8280

8381

8482
if (rr->magic == RISCIX_MAGIC) {
8583
unsigned long size = nr_sects > 2 ? 2 : nr_sects;
8684
int part;
8785

88-
strlcat(state->pp_buf, " <", PAGE_SIZE);
86+
seq_buf_puts(&state->pp_buf, " <");
8987

9088
put_partition(state, slot++, first_sect, size);
9189
for (part = 0; part < 8; part++) {
@@ -94,13 +92,11 @@ static int riscix_partition(struct parsed_partitions *state,
9492
put_partition(state, slot++,
9593
le32_to_cpu(rr->part[part].start),
9694
le32_to_cpu(rr->part[part].length));
97-
strlcat(state->pp_buf, "(", PAGE_SIZE);
98-
strlcat(state->pp_buf, rr->part[part].name, PAGE_SIZE);
99-
strlcat(state->pp_buf, ")", PAGE_SIZE);
95+
seq_buf_printf(&state->pp_buf, "(%s)", rr->part[part].name);
10096
}
10197
}
10298

103-
strlcat(state->pp_buf, " >\n", PAGE_SIZE);
99+
seq_buf_puts(&state->pp_buf, " >\n");
104100
} else {
105101
put_partition(state, slot++, first_sect, nr_sects);
106102
}
@@ -130,15 +126,15 @@ static int linux_partition(struct parsed_partitions *state,
130126
struct linux_part *linuxp;
131127
unsigned long size = nr_sects > 2 ? 2 : nr_sects;
132128

133-
strlcat(state->pp_buf, " [Linux]", PAGE_SIZE);
129+
seq_buf_puts(&state->pp_buf, " [Linux]");
134130

135131
put_partition(state, slot++, first_sect, size);
136132

137133
linuxp = read_part_sector(state, first_sect, &sect);
138134
if (!linuxp)
139135
return -1;
140136

141-
strlcat(state->pp_buf, " <", PAGE_SIZE);
137+
seq_buf_puts(&state->pp_buf, " <");
142138
while (linuxp->magic == cpu_to_le32(LINUX_NATIVE_MAGIC) ||
143139
linuxp->magic == cpu_to_le32(LINUX_SWAP_MAGIC)) {
144140
if (slot == state->limit)
@@ -148,7 +144,7 @@ static int linux_partition(struct parsed_partitions *state,
148144
le32_to_cpu(linuxp->nr_sects));
149145
linuxp ++;
150146
}
151-
strlcat(state->pp_buf, " >", PAGE_SIZE);
147+
seq_buf_puts(&state->pp_buf, " >");
152148

153149
put_dev_sector(sect);
154150
return slot;
@@ -293,7 +289,7 @@ int adfspart_check_ADFS(struct parsed_partitions *state)
293289
break;
294290
}
295291
}
296-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
292+
seq_buf_puts(&state->pp_buf, "\n");
297293
return 1;
298294
}
299295
#endif
@@ -366,7 +362,7 @@ int adfspart_check_ICS(struct parsed_partitions *state)
366362
return 0;
367363
}
368364

369-
strlcat(state->pp_buf, " [ICS]", PAGE_SIZE);
365+
seq_buf_puts(&state->pp_buf, " [ICS]");
370366

371367
for (slot = 1, p = (const struct ics_part *)data; p->size; p++) {
372368
u32 start = le32_to_cpu(p->start);
@@ -400,7 +396,7 @@ int adfspart_check_ICS(struct parsed_partitions *state)
400396
}
401397

402398
put_dev_sector(sect);
403-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
399+
seq_buf_puts(&state->pp_buf, "\n");
404400
return 1;
405401
}
406402
#endif
@@ -460,7 +456,7 @@ int adfspart_check_POWERTEC(struct parsed_partitions *state)
460456
return 0;
461457
}
462458

463-
strlcat(state->pp_buf, " [POWERTEC]", PAGE_SIZE);
459+
seq_buf_puts(&state->pp_buf, " [POWERTEC]");
464460

465461
for (i = 0, p = (const struct ptec_part *)data; i < 12; i++, p++) {
466462
u32 start = le32_to_cpu(p->start);
@@ -471,7 +467,7 @@ int adfspart_check_POWERTEC(struct parsed_partitions *state)
471467
}
472468

473469
put_dev_sector(sect);
474-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
470+
seq_buf_puts(&state->pp_buf, "\n");
475471
return 1;
476472
}
477473
#endif
@@ -542,7 +538,7 @@ int adfspart_check_EESOX(struct parsed_partitions *state)
542538

543539
size = get_capacity(state->disk);
544540
put_partition(state, slot++, start, size - start);
545-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
541+
seq_buf_puts(&state->pp_buf, "\n");
546542
}
547543

548544
return i ? 1 : 0;

block/partitions/aix.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,22 @@ int aix_partition(struct parsed_partitions *state)
173173
if (d) {
174174
struct lvm_rec *p = (struct lvm_rec *)d;
175175
u16 lvm_version = be16_to_cpu(p->version);
176-
char tmp[64];
177176

178177
if (lvm_version == 1) {
179178
int pp_size_log2 = be16_to_cpu(p->pp_size);
180179

181180
pp_bytes_size = 1 << pp_size_log2;
182181
pp_blocks_size = pp_bytes_size / 512;
183-
snprintf(tmp, sizeof(tmp),
184-
" AIX LVM header version %u found\n",
185-
lvm_version);
182+
seq_buf_printf(&state->pp_buf,
183+
" AIX LVM header version %u found\n",
184+
lvm_version);
186185
vgda_len = be32_to_cpu(p->vgda_len);
187186
vgda_sector = be32_to_cpu(p->vgda_psn[0]);
188187
} else {
189-
snprintf(tmp, sizeof(tmp),
190-
" unsupported AIX LVM version %d found\n",
191-
lvm_version);
188+
seq_buf_printf(&state->pp_buf,
189+
" unsupported AIX LVM version %d found\n",
190+
lvm_version);
192191
}
193-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
194192
put_dev_sector(sect);
195193
}
196194
if (vgda_sector && (d = read_part_sector(state, vgda_sector, &sect))) {
@@ -251,14 +249,11 @@ int aix_partition(struct parsed_partitions *state)
251249
continue;
252250
}
253251
if (lp_ix == lvip[lv_ix].pps_per_lv) {
254-
char tmp[70];
255-
256252
put_partition(state, lv_ix + 1,
257253
(i + 1 - lp_ix) * pp_blocks_size + psn_part1,
258254
lvip[lv_ix].pps_per_lv * pp_blocks_size);
259-
snprintf(tmp, sizeof(tmp), " <%s>\n",
260-
n[lv_ix].name);
261-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
255+
seq_buf_printf(&state->pp_buf, " <%s>\n",
256+
n[lv_ix].name);
262257
lvip[lv_ix].lv_is_contiguous = 1;
263258
ret = 1;
264259
next_lp_ix = 1;

block/partitions/amiga.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,8 @@ int amiga_partition(struct parsed_partitions *state)
8181
/* blksize is blocks per 512 byte standard block */
8282
blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
8383

84-
{
85-
char tmp[7 + 10 + 1 + 1];
86-
87-
/* Be more informative */
88-
snprintf(tmp, sizeof(tmp), " RDSK (%d)", blksize * 512);
89-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
90-
}
84+
/* Be more informative */
85+
seq_buf_printf(&state->pp_buf, " RDSK (%d)", blksize * 512);
9186
blk = be32_to_cpu(rdb->rdb_PartitionList);
9287
put_dev_sector(sect);
9388
for (part = 1; (s32) blk>0 && part<=16; part++, put_dev_sector(sect)) {
@@ -179,27 +174,27 @@ int amiga_partition(struct parsed_partitions *state)
179174
{
180175
/* Be even more informative to aid mounting */
181176
char dostype[4];
182-
char tmp[42];
183177

184178
__be32 *dt = (__be32 *)dostype;
185179
*dt = pb->pb_Environment[16];
186180
if (dostype[3] < ' ')
187-
snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
188-
dostype[0], dostype[1],
189-
dostype[2], dostype[3] + '@' );
181+
seq_buf_printf(&state->pp_buf,
182+
" (%c%c%c^%c)",
183+
dostype[0], dostype[1],
184+
dostype[2],
185+
dostype[3] + '@');
190186
else
191-
snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
192-
dostype[0], dostype[1],
193-
dostype[2], dostype[3]);
194-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
195-
snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
196-
be32_to_cpu(pb->pb_Environment[6]),
197-
be32_to_cpu(pb->pb_Environment[4]));
198-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
187+
seq_buf_printf(&state->pp_buf,
188+
" (%c%c%c%c)",
189+
dostype[0], dostype[1],
190+
dostype[2], dostype[3]);
191+
seq_buf_printf(&state->pp_buf, "(res %d spb %d)",
192+
be32_to_cpu(pb->pb_Environment[6]),
193+
be32_to_cpu(pb->pb_Environment[4]));
199194
}
200195
res = 1;
201196
}
202-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
197+
seq_buf_puts(&state->pp_buf, "\n");
203198

204199
rdb_done:
205200
return res;

block/partitions/atari.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int atari_partition(struct parsed_partitions *state)
7070
}
7171

7272
pi = &rs->part[0];
73-
strlcat(state->pp_buf, " AHDI", PAGE_SIZE);
73+
seq_buf_puts(&state->pp_buf, " AHDI");
7474
for (slot = 1; pi < &rs->part[4] && slot < state->limit; slot++, pi++) {
7575
struct rootsector *xrs;
7676
Sector sect2;
@@ -89,7 +89,7 @@ int atari_partition(struct parsed_partitions *state)
8989
#ifdef ICD_PARTS
9090
part_fmt = 1;
9191
#endif
92-
strlcat(state->pp_buf, " XGM<", PAGE_SIZE);
92+
seq_buf_puts(&state->pp_buf, " XGM<");
9393
partsect = extensect = be32_to_cpu(pi->st);
9494
while (1) {
9595
xrs = read_part_sector(state, partsect, &sect2);
@@ -128,14 +128,14 @@ int atari_partition(struct parsed_partitions *state)
128128
break;
129129
}
130130
}
131-
strlcat(state->pp_buf, " >", PAGE_SIZE);
131+
seq_buf_puts(&state->pp_buf, " >");
132132
}
133133
#ifdef ICD_PARTS
134134
if ( part_fmt!=1 ) { /* no extended partitions -> test ICD-format */
135135
pi = &rs->icdpart[0];
136136
/* sanity check: no ICD format if first partition invalid */
137137
if (OK_id(pi->id)) {
138-
strlcat(state->pp_buf, " ICD<", PAGE_SIZE);
138+
seq_buf_puts(&state->pp_buf, " ICD<");
139139
for (; pi < &rs->icdpart[8] && slot < state->limit; slot++, pi++) {
140140
/* accept only GEM,BGM,RAW,LNX,SWP partitions */
141141
if (!((pi->flg & 1) && OK_id(pi->id)))
@@ -144,13 +144,13 @@ int atari_partition(struct parsed_partitions *state)
144144
be32_to_cpu(pi->st),
145145
be32_to_cpu(pi->siz));
146146
}
147-
strlcat(state->pp_buf, " >", PAGE_SIZE);
147+
seq_buf_puts(&state->pp_buf, " >");
148148
}
149149
}
150150
#endif
151151
put_dev_sector(sect);
152152

153-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
153+
seq_buf_puts(&state->pp_buf, "\n");
154154

155155
return 1;
156156
}

block/partitions/check.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/pagemap.h>
33
#include <linux/blkdev.h>
4+
#include <linux/seq_buf.h>
45
#include "../blk.h"
56

67
/*
@@ -20,7 +21,7 @@ struct parsed_partitions {
2021
int next;
2122
int limit;
2223
bool access_beyond_eod;
23-
char *pp_buf;
24+
struct seq_buf pp_buf;
2425
};
2526

2627
typedef struct {
@@ -37,12 +38,9 @@ static inline void
3738
put_partition(struct parsed_partitions *p, int n, sector_t from, sector_t size)
3839
{
3940
if (n < p->limit) {
40-
char tmp[1 + BDEVNAME_SIZE + 10 + 1];
41-
4241
p->parts[n].from = from;
4342
p->parts[n].size = size;
44-
snprintf(tmp, sizeof(tmp), " %s%d", p->name, n);
45-
strlcat(p->pp_buf, tmp, PAGE_SIZE);
43+
seq_buf_printf(&p->pp_buf, " %s%d", p->name, n);
4644
}
4745
}
4846

block/partitions/cmdline.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ static int add_part(int slot, struct cmdline_subpart *subpart,
229229
struct parsed_partitions *state)
230230
{
231231
struct partition_meta_info *info;
232-
char tmp[sizeof(info->volname) + 4];
233232

234233
if (slot >= state->limit)
235234
return 1;
@@ -244,8 +243,7 @@ static int add_part(int slot, struct cmdline_subpart *subpart,
244243

245244
strscpy(info->volname, subpart->name, sizeof(info->volname));
246245

247-
snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
248-
strlcat(state->pp_buf, tmp, PAGE_SIZE);
246+
seq_buf_printf(&state->pp_buf, "(%s)", info->volname);
249247

250248
state->parts[slot].has_info = true;
251249

@@ -379,7 +377,7 @@ int cmdline_partition(struct parsed_partitions *state)
379377
cmdline_parts_set(parts, disk_size, state);
380378
cmdline_parts_verifier(1, state);
381379

382-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
380+
seq_buf_puts(&state->pp_buf, "\n");
383381

384382
return 1;
385383
}

block/partitions/core.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
123123
state = allocate_partitions(hd);
124124
if (!state)
125125
return NULL;
126-
state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
127-
if (!state->pp_buf) {
126+
state->pp_buf.buffer = (char *)__get_free_page(GFP_KERNEL);
127+
if (!state->pp_buf.buffer) {
128128
free_partitions(state);
129129
return NULL;
130130
}
131-
state->pp_buf[0] = '\0';
131+
seq_buf_init(&state->pp_buf, state->pp_buf.buffer, PAGE_SIZE);
132132

133133
state->disk = hd;
134134
strscpy(state->name, hd->disk_name);
135-
snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
135+
seq_buf_printf(&state->pp_buf, " %s:", state->name);
136136
if (isdigit(state->name[strlen(state->name)-1]))
137137
sprintf(state->name, "p");
138138

@@ -151,9 +151,9 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
151151

152152
}
153153
if (res > 0) {
154-
printk(KERN_INFO "%s", state->pp_buf);
154+
printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf));
155155

156-
free_page((unsigned long)state->pp_buf);
156+
free_page((unsigned long)state->pp_buf.buffer);
157157
return state;
158158
}
159159
if (state->access_beyond_eod)
@@ -164,12 +164,12 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
164164
if (err)
165165
res = err;
166166
if (res) {
167-
strlcat(state->pp_buf,
168-
" unable to read partition table\n", PAGE_SIZE);
169-
printk(KERN_INFO "%s", state->pp_buf);
167+
seq_buf_puts(&state->pp_buf,
168+
" unable to read partition table\n");
169+
printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf));
170170
}
171171

172-
free_page((unsigned long)state->pp_buf);
172+
free_page((unsigned long)state->pp_buf.buffer);
173173
free_partitions(state);
174174
return ERR_PTR(res);
175175
}

block/partitions/efi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,6 @@ int efi_partition(struct parsed_partitions *state)
751751
}
752752
kfree(ptes);
753753
kfree(gpt);
754-
strlcat(state->pp_buf, "\n", PAGE_SIZE);
754+
seq_buf_puts(&state->pp_buf, "\n");
755755
return 1;
756756
}

0 commit comments

Comments
 (0)