Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/fpu/xstate: Provide xstate_calculate_size()

Split out the size calculation from the paranoia check so it can be used
for recalculating buffer sizes when dynamically enabled features are
supported.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
[ tglx: Adopted to changed base code ]
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20211021225527.10184-4-chang.seok.bae@intel.com

authored by

Chang S. Bae and committed by
Borislav Petkov
84e4dccc 3aac3ebe

+28 -18
+28 -18
arch/x86/kernel/fpu/xstate.c
··· 549 549 return true; 550 550 } 551 551 552 + static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted) 553 + { 554 + unsigned int size = FXSAVE_SIZE + XSAVE_HDR_SIZE; 555 + int i; 556 + 557 + for_each_extended_xfeature(i, xfeatures) { 558 + /* Align from the end of the previous feature */ 559 + if (xfeature_is_aligned(i)) 560 + size = ALIGN(size, 64); 561 + /* 562 + * In compacted format the enabled features are packed, 563 + * i.e. disabled features do not occupy space. 564 + * 565 + * In non-compacted format the offsets are fixed and 566 + * disabled states still occupy space in the memory buffer. 567 + */ 568 + if (!compacted) 569 + size = xfeature_uncompacted_offset(i); 570 + /* 571 + * Add the feature size even for non-compacted format 572 + * to make the end result correct 573 + */ 574 + size += xfeature_size(i); 575 + } 576 + return size; 577 + } 578 + 552 579 /* 553 580 * This essentially double-checks what the cpu told us about 554 581 * how large the XSAVE buffer needs to be. We are recalculating ··· 602 575 XSTATE_WARN_ON(1); 603 576 return false; 604 577 } 605 - 606 - /* Align from the end of the previous feature */ 607 - if (xfeature_is_aligned(i)) 608 - size = ALIGN(size, 64); 609 - /* 610 - * In compacted format the enabled features are packed, 611 - * i.e. disabled features do not occupy space. 612 - * 613 - * In non-compacted format the offsets are fixed and 614 - * disabled states still occupy space in the memory buffer. 615 - */ 616 - if (!compacted) 617 - size = xfeature_uncompacted_offset(i); 618 - /* 619 - * Add the feature size even for non-compacted format 620 - * to make the end result correct 621 - */ 622 - size += xfeature_size(i); 623 578 } 579 + size = xstate_calculate_size(fpu_kernel_cfg.max_features, compacted); 624 580 XSTATE_WARN_ON(size != kernel_size); 625 581 return size == kernel_size; 626 582 }