[PATCH] LoongArch: Drop -ffreestanding from CFLAGS

WANG Xuerui posted 1 patch 2 years, 1 month ago
arch/loongarch/Makefile | 1 -
1 file changed, 1 deletion(-)
[PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by WANG Xuerui 2 years, 1 month ago
From: WANG Xuerui <git@xen0n.name>

As explained by Nick in the original issue: the kernel usually does a
good job of providing library helpers that have similar semantics as
their ordinary userspace libc equivalents, but -ffreestanding disables
such libcall optimization and other related features in the compiler,
which can lead to unexpected things such as CONFIG_FORTIFY_SOURCE not
working (!).

As it turns out to be the case, removing the flag does not impact the
LoongArch kernel's normal operation at all; so just remove it to
restore expected libcall semantics globally on this architecture.

Closes: https://github.com/ClangBuiltLinux/linux/issues/1897
Reported-by: Nathan Chancellor <nathan@kernel.org>
Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: WANG Xuerui <git@xen0n.name>
---
 arch/loongarch/Makefile | 1 -
 1 file changed, 1 deletion(-)

diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index b1e5db51b61c..db0d7210272d 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -83,7 +83,6 @@ KBUILD_CFLAGS_KERNEL		+= -fPIE
 LDFLAGS_vmlinux			+= -static -pie --no-dynamic-linker -z notext
 endif
 
-cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
 load-y		= 0x9000000000200000
-- 
2.40.0
Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by kernel test robot 2 years, 1 month ago
Hi WANG,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.5-rc4]
[cannot apply to next-20230804]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/WANG-Xuerui/LoongArch-Drop-ffreestanding-from-CFLAGS/20230804-205642
base:   linus/master
patch link:    https://lore.kernel.org/r/20230804125609.2054719-1-kernel%40xen0n.name
patch subject: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20230805/202308050433.0KAmYqQ2-lkp@intel.com/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230805/202308050433.0KAmYqQ2-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202308050433.0KAmYqQ2-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/debugobjects.h:6,
                    from include/linux/timer.h:8,
                    from include/linux/workqueue.h:9,
                    from drivers/media/usb/em28xx/em28xx.h:21,
                    from drivers/media/usb/em28xx/em28xx-audio.c:14:
   arch/loongarch/include/asm/percpu.h:20:4: error: #error compiler support for the model attribute is necessary when a recent assembler is used
      20 | #  error compiler support for the model attribute is necessary when a recent assembler is used
         |    ^~~~~
   drivers/media/usb/em28xx/em28xx-audio.c: In function 'em28xx_audio_init':
>> drivers/media/usb/em28xx/em28xx-audio.c:808:29: warning: variable 'devnr' set but not used [-Wunused-but-set-variable]
     808 |         static int          devnr;
         |                             ^~~~~
   drivers/media/usb/em28xx/em28xx-audio.c: At top level:
   drivers/media/usb/em28xx/em28xx-audio.c:47:12: warning: 'index' defined but not used [-Wunused-variable]
      47 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
         |            ^~~~~


vim +/devnr +808 drivers/media/usb/em28xx/em28xx-audio.c

966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  801  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  802  static int em28xx_audio_init(struct em28xx *dev)
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  803  {
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  804  	struct em28xx_audio *adev = &dev->adev;
c6d48134cb2682 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  805  	struct usb_device *udev = interface_to_usbdev(dev->intf);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  806  	struct snd_pcm      *pcm;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  807  	struct snd_card     *card;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12 @808  	static int          devnr;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  809  	int		    err;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  810  
c5874208ff78a5 drivers/media/usb/em28xx/em28xx-audio.c   Frank Schaefer        2014-09-13  811  	if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
9f90f5371f52b4 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-03-03  812  		/*
9f90f5371f52b4 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-03-03  813  		 * This device does not support the extension (in this case
9f90f5371f52b4 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-03-03  814  		 * the device is expecting the snd-usb-audio module or
9f90f5371f52b4 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-03-03  815  		 * doesn't have analog audio support at all)
9f90f5371f52b4 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-03-03  816  		 */
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  817  		return 0;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  818  	}
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  819  
29b05e22f5c68c drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  820  	dev_info(&dev->intf->dev, "Binding audio extension\n");
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  821  
47677e51e2a404 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-03-05  822  	kref_get(&dev->ref);
47677e51e2a404 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-03-05  823  
29b05e22f5c68c drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  824  	dev_info(&dev->intf->dev,
ce8591ff023ef8 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-10-20  825  		 "em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n");
29b05e22f5c68c drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  826  	dev_info(&dev->intf->dev,
ce8591ff023ef8 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-10-20  827  		 "em28xx-audio.c: Copyright (C) 2007-2016 Mauro Carvalho Chehab\n");
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  828  
29b05e22f5c68c drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  829  	err = snd_card_new(&dev->intf->dev, index[devnr], "Em28xx Audio",
e735688875208f drivers/media/usb/em28xx/em28xx-audio.c   Takashi Iwai          2014-01-29  830  			   THIS_MODULE, 0, &card);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  831  	if (err < 0)
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  832  		return err;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  833  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  834  	spin_lock_init(&adev->slock);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  835  	adev->sndcard = card;
c6d48134cb2682 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  836  	adev->udev = udev;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  837  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  838  	err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  839  	if (err < 0)
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  840  		goto card_free;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  841  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  842  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
2abb1b2daf88ee drivers/media/usb/em28xx/em28xx-audio.c   Takashi Iwai          2019-12-10  843  	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  844  	pcm->info_flags = 0;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  845  	pcm->private_data = dev;
cc1e6315e83db0 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-09-10  846  	strscpy(pcm->name, "Empia 28xx Capture", sizeof(pcm->name));
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  847  
cc1e6315e83db0 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-09-10  848  	strscpy(card->driver, "Em28xx-Audio", sizeof(card->driver));
cc1e6315e83db0 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-09-10  849  	strscpy(card->shortname, "Em28xx Audio", sizeof(card->shortname));
cc1e6315e83db0 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2018-09-10  850  	strscpy(card->longname, "Empia Em28xx Audio", sizeof(card->longname));
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  851  
a5c075cfd2386a drivers/media/usb/em28xx/em28xx-audio.c   Frank Schaefer        2014-03-24  852  	INIT_WORK(&adev->wq_trigger, audio_trigger);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  853  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  854  	if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  855  		em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  856  		em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  857  		em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  858  		em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  859  		em28xx_cvol_new(card, dev, "CD", AC97_CD);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  860  		em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  861  		em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  862  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  863  		em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  864  		em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  865  		em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  866  		em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  867  		em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  868  	}
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  869  
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  870  	err = em28xx_audio_urb_init(dev);
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  871  	if (err)
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  872  		goto card_free;
966f4163751b45 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  873  
6d79468dd85375 drivers/media/video/em28xx/em28xx-audio.c Mauro Carvalho Chehab 2008-01-05  874  	err = snd_card_register(card);
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  875  	if (err < 0)
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  876  		goto urb_free;
6d79468dd85375 drivers/media/video/em28xx/em28xx-audio.c Mauro Carvalho Chehab 2008-01-05  877  
29b05e22f5c68c drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2016-12-07  878  	dev_info(&dev->intf->dev, "Audio extension successfully initialized\n");
a52932b405f230 drivers/media/video/em28xx/em28xx-audio.c Markus Rechberger     2008-01-05  879  	return 0;
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  880  
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  881  urb_free:
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  882  	em28xx_audio_free_urb(dev);
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  883  
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  884  card_free:
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  885  	snd_card_free(card);
452f236fcf845b drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-14  886  	adev->sndcard = NULL;
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  887  
0cd03a0cb20c75 drivers/media/usb/em28xx/em28xx-audio.c   Mauro Carvalho Chehab 2014-01-12  888  	return err;
a52932b405f230 drivers/media/video/em28xx/em28xx-audio.c Markus Rechberger     2008-01-05  889  }
a52932b405f230 drivers/media/video/em28xx/em28xx-audio.c Markus Rechberger     2008-01-05  890  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by Huacai Chen 2 years, 1 month ago
Hi, Xuerui,

On Fri, Aug 4, 2023 at 8:56 PM WANG Xuerui <kernel@xen0n.name> wrote:
>
> From: WANG Xuerui <git@xen0n.name>
>
> As explained by Nick in the original issue: the kernel usually does a
> good job of providing library helpers that have similar semantics as
> their ordinary userspace libc equivalents, but -ffreestanding disables
> such libcall optimization and other related features in the compiler,
> which can lead to unexpected things such as CONFIG_FORTIFY_SOURCE not
> working (!).
>
> As it turns out to be the case, removing the flag does not impact the
> LoongArch kernel's normal operation at all; so just remove it to
> restore expected libcall semantics globally on this architecture.
>
Not all processors support unaligned access, so we need the
alternative mechanism to select memset/memcpy/memmove implementations.
If remove -ffreestanding, the builtin implementation cannot be used on
all hardware.

Huacai

> Closes: https://github.com/ClangBuiltLinux/linux/issues/1897
> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
> Signed-off-by: WANG Xuerui <git@xen0n.name>
> ---
>  arch/loongarch/Makefile | 1 -
>  1 file changed, 1 deletion(-)
>
> diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
> index b1e5db51b61c..db0d7210272d 100644
> --- a/arch/loongarch/Makefile
> +++ b/arch/loongarch/Makefile
> @@ -83,7 +83,6 @@ KBUILD_CFLAGS_KERNEL          += -fPIE
>  LDFLAGS_vmlinux                        += -static -pie --no-dynamic-linker -z notext
>  endif
>
> -cflags-y += -ffreestanding
>  cflags-y += $(call cc-option, -mno-check-zero-division)
>
>  load-y         = 0x9000000000200000
> --
> 2.40.0
>
Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by Nick Desaulniers 2 years, 1 month ago
On Fri, Aug 4, 2023 at 8:26 AM Huacai Chen <chenhuacai@kernel.org> wrote:
>
> Hi, Xuerui,
>
> On Fri, Aug 4, 2023 at 8:56 PM WANG Xuerui <kernel@xen0n.name> wrote:
> >
> > From: WANG Xuerui <git@xen0n.name>
> >
> > As explained by Nick in the original issue: the kernel usually does a
> > good job of providing library helpers that have similar semantics as
> > their ordinary userspace libc equivalents, but -ffreestanding disables
> > such libcall optimization and other related features in the compiler,
> > which can lead to unexpected things such as CONFIG_FORTIFY_SOURCE not
> > working (!).
> >
> > As it turns out to be the case, removing the flag does not impact the
> > LoongArch kernel's normal operation at all; so just remove it to
> > restore expected libcall semantics globally on this architecture.
> >
> Not all processors support unaligned access, so we need the
> alternative mechanism to select memset/memcpy/memmove implementations.
> If remove -ffreestanding, the builtin implementation cannot be used on
> all hardware.

That sounds like a compiler bug in that compiler's implementation of
string.h builtins then; it should default to the safest implementation
(aligned accesses) until instructed otherwise.  Have you filed a bug
against your compiler vendor for which compiler you observe that
behavior from?

At the very least, there should be a comment above the addition of
-ffreestanding justifying why it's being used, probably with a link to
the above bug report.

I would expect either -mcpu or perhaps some other -m flag to guide the
compiler when it is safe to emit memcpy (and friends) in terms of
unaligned access or not.

>
> Huacai
>
> > Closes: https://github.com/ClangBuiltLinux/linux/issues/1897
> > Reported-by: Nathan Chancellor <nathan@kernel.org>
> > Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
> > Signed-off-by: WANG Xuerui <git@xen0n.name>
> > ---
> >  arch/loongarch/Makefile | 1 -
> >  1 file changed, 1 deletion(-)
> >
> > diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
> > index b1e5db51b61c..db0d7210272d 100644
> > --- a/arch/loongarch/Makefile
> > +++ b/arch/loongarch/Makefile
> > @@ -83,7 +83,6 @@ KBUILD_CFLAGS_KERNEL          += -fPIE
> >  LDFLAGS_vmlinux                        += -static -pie --no-dynamic-linker -z notext
> >  endif
> >
> > -cflags-y += -ffreestanding
> >  cflags-y += $(call cc-option, -mno-check-zero-division)
> >
> >  load-y         = 0x9000000000200000
> > --
> > 2.40.0
> >
>


-- 
Thanks,
~Nick Desaulniers
Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by Xi Ruoyao 2 years, 1 month ago
On Fri, 2023-08-04 at 08:46 -0700, Nick Desaulniers wrote:
> > Not all processors support unaligned access, so we need the
> > alternative mechanism to select memset/memcpy/memmove implementations.
> > If remove -ffreestanding, the builtin implementation cannot be used on
> > all hardware.
> 
> That sounds like a compiler bug in that compiler's implementation of
> string.h builtins then; it should default to the safest implementation
> (aligned accesses) until instructed otherwise.  Have you filed a bug
> against your compiler vendor for which compiler you observe that
> behavior from?

AFAIK there is no such bug in GCC.

But GCC indeed has a bug about builtin expansion: it generates really
stupid code for __builtin_memcpy and friends.  See
https://gcc.gnu.org/PR109465.  The bug is fixed for GCC 14, but GCC 14
won't be released soon.

> At the very least, there should be a comment above the addition of
> -ffreestanding justifying why it's being used, probably with a link to
> the above bug report.
> 
> I would expect either -mcpu or perhaps some other -m flag to guide the
> compiler when it is safe to emit memcpy (and friends) in terms of
> unaligned access or not.

It's controlled by -m{no-,}strict-align.  LoongArch kernel defaults to -
mstrict-align, with this the compiler should not generate unaligned
access.  -mno-strict-align is hidden behind CONFIG_EXPERT (FWIW I
personally dislike the decision to hide it).

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University
Re: [PATCH] LoongArch: Drop -ffreestanding from CFLAGS
Posted by Nick Desaulniers 2 years, 1 month ago
On Fri, Aug 4, 2023 at 8:56 AM Xi Ruoyao <xry111@xry111.site> wrote:
>
> On Fri, 2023-08-04 at 08:46 -0700, Nick Desaulniers wrote:
> > > Not all processors support unaligned access, so we need the
> > > alternative mechanism to select memset/memcpy/memmove implementations.
> > > If remove -ffreestanding, the builtin implementation cannot be used on
> > > all hardware.
> >
> > That sounds like a compiler bug in that compiler's implementation of
> > string.h builtins then; it should default to the safest implementation
> > (aligned accesses) until instructed otherwise.  Have you filed a bug
> > against your compiler vendor for which compiler you observe that
> > behavior from?
>
> AFAIK there is no such bug in GCC.
>
> But GCC indeed has a bug about builtin expansion: it generates really
> stupid code for __builtin_memcpy and friends.  See
> https://gcc.gnu.org/PR109465.  The bug is fixed for GCC 14, but GCC 14
> won't be released soon.

Perhaps -fno-builtin-* flags may be of help here, and more precise an
incision than the blunt -ffreestanding.

>
> > At the very least, there should be a comment above the addition of
> > -ffreestanding justifying why it's being used, probably with a link to
> > the above bug report.
> >
> > I would expect either -mcpu or perhaps some other -m flag to guide the
> > compiler when it is safe to emit memcpy (and friends) in terms of
> > unaligned access or not.
>
> It's controlled by -m{no-,}strict-align.  LoongArch kernel defaults to -
> mstrict-align, with this the compiler should not generate unaligned
> access.  -mno-strict-align is hidden behind CONFIG_EXPERT (FWIW I
> personally dislike the decision to hide it).

If GCC generates unaligned accesses in its implementation of
__builtin_memcpy when -mstrict-align is set, then that is a bug in GCC
and should be reported.

Clang users should not have to pay for such mistakes.

>
> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University



-- 
Thanks,
~Nick Desaulniers