From b66210f3016d0b66e09206fca1b781ef9dfbe60c Mon Sep 17 00:00:00 2001 From: Srinivas Ramana Date: Mon, 22 Aug 2016 11:05:06 +0530 Subject: [PATCH 01/16] defconfig: msm: Add support to build msmcobalt 32-bit Add kernel defconfig to build 32-bit kernel for msmcobalt. Change-Id: I9b4c22a6f54367f58b482fbccfaa9e37d70667f4 Signed-off-by: Srinivas Ramana --- arch/arm/configs/msmcortex_defconfig | 561 +++++++++++++++++++++++++++ 1 file changed, 561 insertions(+) create mode 100644 arch/arm/configs/msmcortex_defconfig diff --git a/arch/arm/configs/msmcortex_defconfig b/arch/arm/configs/msmcortex_defconfig new file mode 100644 index 000000000000..013bb87ecbbe --- /dev/null +++ b/arch/arm/configs/msmcortex_defconfig @@ -0,0 +1,561 @@ +# CONFIG_LOCALVERSION_AUTO is not set +CONFIG_AUDIT=y +CONFIG_NO_HZ=y +CONFIG_HIGH_RES_TIMERS=y +CONFIG_IRQ_TIME_ACCOUNTING=y +CONFIG_RCU_EXPERT=y +CONFIG_IKCONFIG=y +CONFIG_IKCONFIG_PROC=y +CONFIG_CGROUPS=y +CONFIG_CGROUP_DEBUG=y +CONFIG_CGROUP_FREEZER=y +CONFIG_CGROUP_CPUACCT=y +CONFIG_CGROUP_SCHED=y +CONFIG_RT_GROUP_SCHED=y +CONFIG_SCHED_HMP=y +CONFIG_NAMESPACES=y +# CONFIG_UTS_NS is not set +# CONFIG_PID_NS is not set +CONFIG_BLK_DEV_INITRD=y +# CONFIG_RD_XZ is not set +# CONFIG_RD_LZO is not set +# CONFIG_RD_LZ4 is not set +CONFIG_CC_OPTIMIZE_FOR_SIZE=y +CONFIG_KALLSYMS_ALL=y +# CONFIG_MEMBARRIER is not set +CONFIG_EMBEDDED=y +# CONFIG_COMPAT_BRK is not set +CONFIG_PROFILING=y +CONFIG_CC_STACKPROTECTOR_REGULAR=y +CONFIG_MODULES=y +CONFIG_MODULE_UNLOAD=y +CONFIG_MODULE_FORCE_UNLOAD=y +CONFIG_MODVERSIONS=y +CONFIG_MODULE_SIG=y +CONFIG_MODULE_SIG_SHA512=y +# CONFIG_BLK_DEV_BSG is not set +CONFIG_PARTITION_ADVANCED=y +# CONFIG_IOSCHED_DEADLINE is not set +CONFIG_ARCH_QCOM=y +CONFIG_ARCH_MSMFALCON=y +CONFIG_SMP=y +CONFIG_SCHED_MC=y +CONFIG_NR_CPUS=8 +CONFIG_ARM_PSCI=y +CONFIG_PREEMPT=y +CONFIG_AEABI=y +CONFIG_HIGHMEM=y +# CONFIG_HIGHPTE is not set +CONFIG_CLEANCACHE=y +CONFIG_CMA=y +CONFIG_ZSMALLOC=y +CONFIG_SECCOMP=y +CONFIG_BUILD_ARM_APPENDED_DTB_IMAGE=y +CONFIG_SCHED_FREQ_INPUT=y +# CONFIG_CPU_FREQ_STAT is not set +CONFIG_CPU_FREQ_GOV_POWERSAVE=y +CONFIG_CPU_FREQ_GOV_USERSPACE=y +CONFIG_CPU_FREQ_GOV_ONDEMAND=y +CONFIG_CPU_FREQ_GOV_INTERACTIVE=y +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y +CONFIG_CPU_IDLE=y +CONFIG_VFP=y +CONFIG_NEON=y +CONFIG_KERNEL_MODE_NEON=y +# CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS is not set +CONFIG_PM_AUTOSLEEP=y +CONFIG_PM_WAKELOCKS=y +CONFIG_PM_WAKELOCKS_LIMIT=0 +# CONFIG_PM_WAKELOCKS_GC is not set +CONFIG_PM_DEBUG=y +CONFIG_NET=y +CONFIG_PACKET=y +CONFIG_UNIX=y +CONFIG_XFRM_USER=y +CONFIG_XFRM_STATISTICS=y +CONFIG_NET_KEY=y +CONFIG_INET=y +CONFIG_IP_ADVANCED_ROUTER=y +CONFIG_IP_MULTIPLE_TABLES=y +CONFIG_IP_ROUTE_VERBOSE=y +CONFIG_IP_PNP=y +CONFIG_IP_PNP_DHCP=y +CONFIG_INET_AH=y +CONFIG_INET_ESP=y +CONFIG_INET_IPCOMP=y +# CONFIG_INET_LRO is not set +CONFIG_IPV6_ROUTER_PREF=y +CONFIG_IPV6_ROUTE_INFO=y +CONFIG_IPV6_OPTIMISTIC_DAD=y +CONFIG_INET6_AH=y +CONFIG_INET6_ESP=y +CONFIG_INET6_IPCOMP=y +CONFIG_IPV6_MIP6=y +CONFIG_IPV6_MULTIPLE_TABLES=y +CONFIG_IPV6_SUBTREES=y +CONFIG_NETFILTER=y +CONFIG_NF_CONNTRACK=y +CONFIG_NF_CONNTRACK_SECMARK=y +CONFIG_NF_CONNTRACK_EVENTS=y +CONFIG_NF_CT_PROTO_DCCP=y +CONFIG_NF_CT_PROTO_SCTP=y +CONFIG_NF_CT_PROTO_UDPLITE=y +CONFIG_NF_CONNTRACK_AMANDA=y +CONFIG_NF_CONNTRACK_FTP=y +CONFIG_NF_CONNTRACK_H323=y +CONFIG_NF_CONNTRACK_IRC=y +CONFIG_NF_CONNTRACK_NETBIOS_NS=y +CONFIG_NF_CONNTRACK_PPTP=y +CONFIG_NF_CONNTRACK_SANE=y +CONFIG_NF_CONNTRACK_TFTP=y +CONFIG_NF_CT_NETLINK=y +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=y +CONFIG_NETFILTER_XT_TARGET_CONNMARK=y +CONFIG_NETFILTER_XT_TARGET_CONNSECMARK=y +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=y +CONFIG_NETFILTER_XT_TARGET_HARDIDLETIMER=y +CONFIG_NETFILTER_XT_TARGET_LOG=y +CONFIG_NETFILTER_XT_TARGET_MARK=y +CONFIG_NETFILTER_XT_TARGET_NFLOG=y +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=y +CONFIG_NETFILTER_XT_TARGET_NOTRACK=y +CONFIG_NETFILTER_XT_TARGET_TEE=y +CONFIG_NETFILTER_XT_TARGET_TPROXY=y +CONFIG_NETFILTER_XT_TARGET_TRACE=y +CONFIG_NETFILTER_XT_TARGET_SECMARK=y +CONFIG_NETFILTER_XT_TARGET_TCPMSS=y +CONFIG_NETFILTER_XT_MATCH_COMMENT=y +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=y +CONFIG_NETFILTER_XT_MATCH_CONNMARK=y +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=y +CONFIG_NETFILTER_XT_MATCH_DSCP=y +CONFIG_NETFILTER_XT_MATCH_ESP=y +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=y +CONFIG_NETFILTER_XT_MATCH_HELPER=y +CONFIG_NETFILTER_XT_MATCH_IPRANGE=y +# CONFIG_NETFILTER_XT_MATCH_L2TP is not set +CONFIG_NETFILTER_XT_MATCH_LENGTH=y +CONFIG_NETFILTER_XT_MATCH_LIMIT=y +CONFIG_NETFILTER_XT_MATCH_MAC=y +CONFIG_NETFILTER_XT_MATCH_MARK=y +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=y +CONFIG_NETFILTER_XT_MATCH_POLICY=y +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=y +CONFIG_NETFILTER_XT_MATCH_QTAGUID=y +CONFIG_NETFILTER_XT_MATCH_QUOTA=y +CONFIG_NETFILTER_XT_MATCH_QUOTA2=y +CONFIG_NETFILTER_XT_MATCH_SOCKET=y +CONFIG_NETFILTER_XT_MATCH_STATE=y +CONFIG_NETFILTER_XT_MATCH_STATISTIC=y +CONFIG_NETFILTER_XT_MATCH_STRING=y +CONFIG_NETFILTER_XT_MATCH_TIME=y +CONFIG_NETFILTER_XT_MATCH_U32=y +CONFIG_NF_CONNTRACK_IPV4=y +CONFIG_IP_NF_IPTABLES=y +CONFIG_IP_NF_MATCH_AH=y +CONFIG_IP_NF_MATCH_ECN=y +CONFIG_IP_NF_MATCH_TTL=y +CONFIG_IP_NF_FILTER=y +CONFIG_IP_NF_TARGET_REJECT=y +CONFIG_IP_NF_NAT=y +CONFIG_IP_NF_TARGET_MASQUERADE=y +CONFIG_IP_NF_TARGET_NETMAP=y +CONFIG_IP_NF_TARGET_REDIRECT=y +CONFIG_IP_NF_MANGLE=y +CONFIG_IP_NF_RAW=y +CONFIG_IP_NF_SECURITY=y +CONFIG_IP_NF_ARPTABLES=y +CONFIG_IP_NF_ARPFILTER=y +CONFIG_IP_NF_ARP_MANGLE=y +CONFIG_NF_CONNTRACK_IPV6=y +CONFIG_IP6_NF_IPTABLES=y +CONFIG_IP6_NF_FILTER=y +CONFIG_IP6_NF_TARGET_REJECT=y +CONFIG_IP6_NF_MANGLE=y +CONFIG_IP6_NF_RAW=y +CONFIG_BRIDGE_NF_EBTABLES=y +CONFIG_BRIDGE_EBT_BROUTE=y +CONFIG_L2TP=y +CONFIG_L2TP_DEBUGFS=y +CONFIG_L2TP_V3=y +CONFIG_L2TP_IP=y +CONFIG_L2TP_ETH=y +CONFIG_BRIDGE=y +CONFIG_NET_SCHED=y +CONFIG_NET_SCH_HTB=y +CONFIG_NET_SCH_PRIO=y +CONFIG_NET_SCH_MULTIQ=y +CONFIG_NET_SCH_INGRESS=y +CONFIG_NET_CLS_FW=y +CONFIG_NET_CLS_U32=y +CONFIG_CLS_U32_MARK=y +CONFIG_NET_CLS_FLOW=y +CONFIG_NET_EMATCH=y +CONFIG_NET_EMATCH_CMP=y +CONFIG_NET_EMATCH_NBYTE=y +CONFIG_NET_EMATCH_U32=y +CONFIG_NET_EMATCH_META=y +CONFIG_NET_EMATCH_TEXT=y +CONFIG_NET_CLS_ACT=y +CONFIG_NET_ACT_GACT=y +CONFIG_NET_ACT_MIRRED=y +CONFIG_NET_ACT_SKBEDIT=y +CONFIG_DNS_RESOLVER=y +CONFIG_RMNET_DATA=y +CONFIG_RMNET_DATA_FC=y +CONFIG_RMNET_DATA_DEBUG_PKT=y +CONFIG_SOCKEV_NLMCAST=y +CONFIG_BT=y +CONFIG_MSM_BT_POWER=y +CONFIG_BTFM_SLIM=y +CONFIG_BTFM_SLIM_WCN3990=y +CONFIG_CFG80211=y +CONFIG_CFG80211_INTERNAL_REGDB=y +# CONFIG_CFG80211_CRDA_SUPPORT is not set +CONFIG_RFKILL=y +CONFIG_NFC_NQ=y +CONFIG_IPC_ROUTER=y +CONFIG_IPC_ROUTER_SECURITY=y +CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y +CONFIG_DMA_CMA=y +CONFIG_CMA_SIZE_MBYTES=40 +CONFIG_ZRAM=y +CONFIG_BLK_DEV_LOOP=y +CONFIG_BLK_DEV_RAM=y +CONFIG_BLK_DEV_RAM_SIZE=8192 +CONFIG_UID_CPUTIME=y +CONFIG_MSM_ULTRASOUND=y +CONFIG_SCSI=y +CONFIG_BLK_DEV_SD=y +CONFIG_CHR_DEV_SG=y +CONFIG_CHR_DEV_SCH=y +CONFIG_SCSI_CONSTANTS=y +CONFIG_SCSI_LOGGING=y +CONFIG_SCSI_SCAN_ASYNC=y +CONFIG_SCSI_UFSHCD=y +CONFIG_SCSI_UFSHCD_PLATFORM=y +CONFIG_SCSI_UFS_QCOM=y +CONFIG_MD=y +CONFIG_BLK_DEV_DM=y +CONFIG_DM_UEVENT=y +CONFIG_DM_VERITY=y +CONFIG_NETDEVICES=y +CONFIG_BONDING=y +CONFIG_DUMMY=y +CONFIG_TUN=y +CONFIG_RNDIS_IPA=y +CONFIG_PHYLIB=y +CONFIG_PPP=y +CONFIG_PPP_BSDCOMP=y +CONFIG_PPP_DEFLATE=y +CONFIG_PPP_MPPE=y +CONFIG_PPPOLAC=y +CONFIG_PPPOPNS=y +CONFIG_WCNSS_MEM_PRE_ALLOC=y +CONFIG_ATH_CARDS=y +CONFIG_CLD_LL_CORE=y +CONFIG_QPNP_POWER_ON=y +CONFIG_INPUT_EVDEV=y +CONFIG_INPUT_KEYRESET=y +CONFIG_KEYBOARD_GPIO=y +# CONFIG_INPUT_MOUSE is not set +CONFIG_INPUT_JOYSTICK=y +CONFIG_INPUT_TOUCHSCREEN=y +CONFIG_TOUCHSCREEN_SYNAPTICS_DSX_CORE_v21=y +CONFIG_TOUCHSCREEN_SYNAPTICS_DSX_RMI_DEV_v21=y +CONFIG_TOUCHSCREEN_SYNAPTICS_DSX_FW_UPDATE_v21=y +CONFIG_INPUT_MISC=y +CONFIG_INPUT_HBTP_INPUT=y +CONFIG_INPUT_KEYCHORD=y +CONFIG_INPUT_UINPUT=y +CONFIG_INPUT_GPIO=y +# CONFIG_SERIO_SERPORT is not set +# CONFIG_VT is not set +# CONFIG_LEGACY_PTYS is not set +CONFIG_SERIAL_MSM=y +CONFIG_SERIAL_MSM_CONSOLE=y +CONFIG_SERIAL_MSM_HS=y +CONFIG_SERIAL_MSM_SMD=y +CONFIG_HW_RANDOM=y +CONFIG_HW_RANDOM_MSM_LEGACY=y +CONFIG_MSM_ADSPRPC=y +CONFIG_I2C_CHARDEV=y +CONFIG_I2C_MSM_V2=y +CONFIG_SLIMBUS=y +CONFIG_SLIMBUS_MSM_NGD=y +CONFIG_SOUNDWIRE=y +CONFIG_SPI=y +CONFIG_SPI_QUP=y +CONFIG_SPI_SPIDEV=y +CONFIG_SPMI=y +CONFIG_PINCTRL_MSMCOBALT=y +CONFIG_PINCTRL_MSMFALCON=y +CONFIG_GPIO_SYSFS=y +CONFIG_GPIO_QPNP_PIN=y +CONFIG_APSS_CORE_EA=y +CONFIG_MSM_APM=y +CONFIG_QPNP_SMBCHARGER=y +CONFIG_SMB135X_CHARGER=y +CONFIG_SMB1351_USB_CHARGER=y +CONFIG_MSM_BCL_CTL=y +CONFIG_MSM_BCL_PERIPHERAL_CTL=y +CONFIG_QPNP_SMB2=y +CONFIG_SMB138X_CHARGER=y +CONFIG_SENSORS_QPNP_ADC_VOLTAGE=y +CONFIG_THERMAL=y +CONFIG_MFD_SPMI_PMIC=y +CONFIG_MFD_I2C_PMIC=y +CONFIG_MSM_CDC_PINCTRL=y +CONFIG_MSM_CDC_SUPPLY=y +CONFIG_REGULATOR=y +CONFIG_REGULATOR_FIXED_VOLTAGE=y +CONFIG_REGULATOR_RPM_SMD=y +CONFIG_REGULATOR_QPNP=y +CONFIG_REGULATOR_QPNP_LABIBB=y +CONFIG_REGULATOR_SPM=y +CONFIG_REGULATOR_CPR3_HMSS=y +CONFIG_REGULATOR_CPR3_MMSS=y +CONFIG_REGULATOR_CPRH_KBSS=y +CONFIG_REGULATOR_MEM_ACC=y +CONFIG_REGULATOR_PROXY_CONSUMER=y +CONFIG_REGULATOR_STUB=y +CONFIG_MEDIA_SUPPORT=y +CONFIG_MEDIA_CAMERA_SUPPORT=y +CONFIG_MEDIA_CONTROLLER=y +CONFIG_VIDEO_V4L2_SUBDEV_API=y +CONFIG_VIDEO_ADV_DEBUG=y +CONFIG_VIDEO_FIXED_MINOR_RANGES=y +CONFIG_V4L_PLATFORM_DRIVERS=y +CONFIG_MSM_CAMERA=y +CONFIG_MSM_CAMERA_DEBUG=y +CONFIG_MSM_SDE_ROTATOR=y +CONFIG_QCOM_KGSL=y +CONFIG_FB=y +CONFIG_FB_VIRTUAL=y +CONFIG_LOGO=y +# CONFIG_LOGO_LINUX_MONO is not set +# CONFIG_LOGO_LINUX_VGA16 is not set +CONFIG_SOUND=y +CONFIG_SND=y +CONFIG_SND_DYNAMIC_MINORS=y +CONFIG_SND_SOC=y +CONFIG_UHID=y +CONFIG_HID_APPLE=y +CONFIG_HID_MICROSOFT=y +CONFIG_USB=y +CONFIG_USB_XHCI_HCD=y +CONFIG_USB_EHCI_HCD=y +CONFIG_USB_EHCI_HCD_PLATFORM=y +CONFIG_USB_OHCI_HCD=y +CONFIG_USB_OHCI_HCD_PLATFORM=y +CONFIG_USB_STORAGE=y +CONFIG_USB_DWC3=y +CONFIG_USB_ISP1760=y +CONFIG_USB_ISP1760_HOST_ROLE=y +CONFIG_USB_OTG_WAKELOCK=y +CONFIG_NOP_USB_XCEIV=y +CONFIG_USB_MSM_SSPHY_QMP=y +CONFIG_MSM_QUSB_PHY=y +CONFIG_DUAL_ROLE_USB_INTF=y +CONFIG_USB_GADGET=y +CONFIG_USB_GADGET_VBUS_DRAW=500 +CONFIG_USB_CONFIGFS=y +CONFIG_USB_CONFIGFS_MASS_STORAGE=y +CONFIG_USB_CONFIGFS_F_FS=y +CONFIG_USB_CONFIGFS_F_MTP=y +CONFIG_USB_CONFIGFS_F_PTP=y +CONFIG_USB_CONFIGFS_F_ACC=y +CONFIG_USB_CONFIGFS_UEVENT=y +CONFIG_USB_CONFIGFS_F_HID=y +CONFIG_USB_CONFIGFS_F_DIAG=y +CONFIG_USB_CONFIGFS_F_GSI=y +CONFIG_USB_CONFIGFS_F_CDEV=y +CONFIG_USB_CONFIGFS_F_QDSS=y +CONFIG_MMC=y +CONFIG_MMC_PERF_PROFILING=y +CONFIG_MMC_PARANOID_SD_INIT=y +CONFIG_MMC_CLKGATE=y +CONFIG_MMC_BLOCK_MINORS=32 +CONFIG_MMC_TEST=y +CONFIG_MMC_SDHCI=y +CONFIG_MMC_SDHCI_PLTFM=y +CONFIG_MMC_SDHCI_MSM=y +CONFIG_NEW_LEDS=y +CONFIG_LEDS_CLASS=y +CONFIG_LEDS_QPNP=y +CONFIG_LEDS_QPNP_FLASH_V2=y +CONFIG_LEDS_QPNP_WLED=y +CONFIG_LEDS_TRIGGERS=y +CONFIG_LEDS_TRIGGER_HEARTBEAT=y +CONFIG_LEDS_TRIGGER_CPU=y +CONFIG_SWITCH=y +CONFIG_EDAC=y +CONFIG_EDAC_MM_EDAC=y +CONFIG_RTC_CLASS=y +CONFIG_RTC_DRV_QPNP=y +CONFIG_DMADEVICES=y +CONFIG_QCOM_SPS_DMA=y +CONFIG_UIO=y +CONFIG_UIO_MSM_SHAREDMEM=y +CONFIG_STAGING=y +CONFIG_ASHMEM=y +CONFIG_ANDROID_TIMED_GPIO=y +CONFIG_ANDROID_LOW_MEMORY_KILLER=y +CONFIG_SYNC=y +CONFIG_ION=y +CONFIG_ION_MSM=y +CONFIG_QPNP_REVID=y +CONFIG_QPNP_COINCELL=y +CONFIG_SPS=y +CONFIG_SPS_SUPPORT_NDP_BAM=y +CONFIG_IPA=y +CONFIG_RMNET_IPA=y +CONFIG_GSI=y +CONFIG_IPA3=y +CONFIG_RMNET_IPA3=y +CONFIG_GPIO_USB_DETECT=y +CONFIG_USB_BAM=y +CONFIG_REMOTE_SPINLOCK_MSM=y +CONFIG_ARM_SMMU=y +CONFIG_QCOM_COMMON_LOG=y +CONFIG_MSM_SMEM=y +CONFIG_QPNP_HAPTIC=y +CONFIG_MSM_SMD=y +CONFIG_MSM_SMD_DEBUG=y +CONFIG_MSM_GLINK=y +CONFIG_MSM_GLINK_LOOPBACK_SERVER=y +CONFIG_MSM_GLINK_SMD_XPRT=y +CONFIG_MSM_GLINK_SMEM_NATIVE_XPRT=y +CONFIG_MSM_SPCOM=y +CONFIG_MSM_SMEM_LOGGING=y +CONFIG_MSM_SMP2P=y +CONFIG_MSM_SMP2P_TEST=y +CONFIG_MSM_QMI_INTERFACE=y +CONFIG_QCOM_BUS_SCALING=y +CONFIG_MSM_SERVICE_LOCATOR=y +CONFIG_QCOM_DCC=y +CONFIG_MSM_IPC_ROUTER_SMD_XPRT=y +CONFIG_MSM_SYSMON_GLINK_COMM=y +CONFIG_MSM_IPC_ROUTER_GLINK_XPRT=y +CONFIG_MSM_GLINK_PKT=y +CONFIG_MSM_SPM=y +CONFIG_QCOM_SCM=y +CONFIG_QCOM_WATCHDOG_V2=y +CONFIG_QCOM_MEMORY_DUMP_V2=y +CONFIG_ICNSS=y +CONFIG_MSM_GLADIATOR_ERP_V2=y +CONFIG_PANIC_ON_GLADIATOR_ERROR_V2=y +CONFIG_MSM_GLADIATOR_HANG_DETECT=y +CONFIG_MSM_CORE_HANG_DETECT=y +CONFIG_MSM_RUN_QUEUE_STATS=y +CONFIG_MSM_BOOT_STATS=y +CONFIG_QCOM_CPUSS_DUMP=y +CONFIG_MSM_QDSP6_APRV2_GLINK=y +CONFIG_MSM_PERFORMANCE=y +CONFIG_MSM_SUBSYSTEM_RESTART=y +CONFIG_MSM_PIL=y +CONFIG_MSM_PIL_SSR_GENERIC=y +CONFIG_MSM_PIL_MSS_QDSP6V5=y +CONFIG_TRACER_PKT=y +CONFIG_QCOM_FORCE_WDOG_BITE_ON_PANIC=y +CONFIG_MSM_MPM_OF=y +CONFIG_MSM_EVENT_TIMER=y +CONFIG_MSM_CORE_CTL_HELPER=y +CONFIG_MSM_SERVICE_NOTIFIER=y +CONFIG_MEM_SHARE_QMI_SERVICE=y +CONFIG_QCOM_BIMC_BWMON=y +CONFIG_DEVFREQ_GOV_QCOM_BW_HWMON=y +CONFIG_QCOM_DEVFREQ_DEVBW=y +CONFIG_EXTCON=y +CONFIG_IIO=y +CONFIG_QCOM_RRADC=y +CONFIG_PWM=y +CONFIG_PWM_QPNP=y +CONFIG_ARM_GIC_V3_ACL=y +CONFIG_ANDROID=y +CONFIG_ANDROID_BINDER_IPC=y +CONFIG_MSM_TZ_LOG=y +CONFIG_SENSORS_SSC=y +CONFIG_EXT2_FS=y +CONFIG_EXT2_FS_XATTR=y +CONFIG_EXT3_FS=y +CONFIG_EXT4_FS_SECURITY=y +CONFIG_FUSE_FS=y +CONFIG_MSDOS_FS=y +CONFIG_VFAT_FS=y +CONFIG_TMPFS=y +CONFIG_TMPFS_POSIX_ACL=y +CONFIG_ECRYPT_FS=y +CONFIG_ECRYPT_FS_MESSAGING=y +# CONFIG_NETWORK_FILESYSTEMS is not set +CONFIG_NLS_CODEPAGE_437=y +CONFIG_NLS_ISO8859_1=y +CONFIG_PRINTK_TIME=y +CONFIG_DYNAMIC_DEBUG=y +CONFIG_DEBUG_INFO=y +CONFIG_PAGE_OWNER=y +CONFIG_PAGE_OWNER_ENABLE_DEFAULT=y +CONFIG_MAGIC_SYSRQ=y +CONFIG_SLUB_DEBUG_PANIC_ON=y +CONFIG_DEBUG_OBJECTS=y +CONFIG_DEBUG_OBJECTS_FREE=y +CONFIG_DEBUG_OBJECTS_TIMERS=y +CONFIG_DEBUG_OBJECTS_WORK=y +CONFIG_DEBUG_OBJECTS_RCU_HEAD=y +CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER=y +CONFIG_SLUB_DEBUG_ON=y +CONFIG_DEBUG_KMEMLEAK=y +CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=4000 +CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF=y +CONFIG_DEBUG_STACK_USAGE=y +CONFIG_DEBUG_MEMORY_INIT=y +CONFIG_LOCKUP_DETECTOR=y +CONFIG_WQ_WATCHDOG=y +CONFIG_PANIC_TIMEOUT=5 +CONFIG_PANIC_ON_SCHED_BUG=y +CONFIG_PANIC_ON_RT_THROTTLING=y +CONFIG_SCHEDSTATS=y +CONFIG_SCHED_STACK_END_CHECK=y +CONFIG_TIMER_STATS=y +# CONFIG_DEBUG_PREEMPT is not set +CONFIG_DEBUG_SPINLOCK=y +CONFIG_DEBUG_MUTEXES=y +CONFIG_DEBUG_ATOMIC_SLEEP=y +CONFIG_DEBUG_LIST=y +CONFIG_FAULT_INJECTION=y +CONFIG_FAIL_PAGE_ALLOC=y +CONFIG_UFS_FAULT_INJECTION=y +CONFIG_FAULT_INJECTION_DEBUG_FS=y +CONFIG_FAULT_INJECTION_STACKTRACE_FILTER=y +CONFIG_IPC_LOGGING=y +CONFIG_QCOM_RTB=y +CONFIG_FUNCTION_TRACER=y +CONFIG_TRACER_SNAPSHOT=y +CONFIG_BLK_DEV_IO_TRACE=y +CONFIG_CPU_FREQ_SWITCH_PROFILER=y +CONFIG_MEMTEST=y +CONFIG_PANIC_ON_DATA_CORRUPTION=y +CONFIG_PID_IN_CONTEXTIDR=y +CONFIG_DEBUG_SET_MODULE_RONX=y +CONFIG_CORESIGHT=y +CONFIG_CORESIGHT_REMOTE_ETM=y +CONFIG_CORESIGHT_REMOTE_ETM_DEFAULT_ENABLE=0 +CONFIG_CORESIGHT_CTI=y +CONFIG_CORESIGHT_TPDA=y +CONFIG_CORESIGHT_TPDM=y +CONFIG_CORESIGHT_QPDI=y +CONFIG_CORESIGHT_SOURCE_DUMMY=y +CONFIG_SECURITY=y +CONFIG_SECURITY_SELINUX=y +CONFIG_SECURITY_SMACK=y +CONFIG_CRYPTO_ECHAINIV=y +CONFIG_CRYPTO_XCBC=y +CONFIG_CRYPTO_MD4=y +CONFIG_CRYPTO_TWOFISH=y +CONFIG_CRYPTO_ANSI_CPRNG=y +CONFIG_CRYPTO_DEV_QCRYPTO=y +CONFIG_CRYPTO_DEV_QCOM_MSM_QCE=y +CONFIG_CRYPTO_DEV_QCEDEV=y +CONFIG_CRYPTO_DEV_OTA_CRYPTO=y +CONFIG_CRYPTO_DEV_QCE=y +CONFIG_XZ_DEC=y From b855596ec10f4c35655becc049496efff41888a8 Mon Sep 17 00:00:00 2001 From: Srinivas Ramana Date: Fri, 19 Aug 2016 14:59:35 +0530 Subject: [PATCH 02/16] defconfig: msm: falcon: get up to date with msmcobalt defconfig Get msmfalcon defconfigs up to date with msmcobalt defconfigs, in order to bring in all the latest changes. Change-Id: Ia670dab963bd337bf3aa395e15f4ec8e8f213e1c Signed-off-by: Srinivas Ramana --- arch/arm/configs/msmfalcon_defconfig | 1 - arch/arm64/configs/msmfalcon-perf_defconfig | 15 ++++++++++++--- arch/arm64/configs/msmfalcon_defconfig | 11 ++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/arch/arm/configs/msmfalcon_defconfig b/arch/arm/configs/msmfalcon_defconfig index 9a3ffc5e555f..013bb87ecbbe 100644 --- a/arch/arm/configs/msmfalcon_defconfig +++ b/arch/arm/configs/msmfalcon_defconfig @@ -223,7 +223,6 @@ CONFIG_ZRAM=y CONFIG_BLK_DEV_LOOP=y CONFIG_BLK_DEV_RAM=y CONFIG_BLK_DEV_RAM_SIZE=8192 -CONFIG_UID_STAT=y CONFIG_UID_CPUTIME=y CONFIG_MSM_ULTRASOUND=y CONFIG_SCSI=y diff --git a/arch/arm64/configs/msmfalcon-perf_defconfig b/arch/arm64/configs/msmfalcon-perf_defconfig index a09e952927f7..40a054eca640 100644 --- a/arch/arm64/configs/msmfalcon-perf_defconfig +++ b/arch/arm64/configs/msmfalcon-perf_defconfig @@ -11,6 +11,7 @@ CONFIG_IKCONFIG=y CONFIG_IKCONFIG_PROC=y CONFIG_LOG_CPU_MAX_BUF_SHIFT=17 CONFIG_CGROUP_FREEZER=y +CONFIG_CPUSETS=y CONFIG_CGROUP_CPUACCT=y CONFIG_RT_GROUP_SCHED=y CONFIG_SCHED_HMP=y @@ -51,6 +52,7 @@ CONFIG_PREEMPT=y CONFIG_HZ_100=y CONFIG_ARM64_REG_REBALANCE_ON_CTX_SW=y CONFIG_CMA=y +CONFIG_CMA_DEBUGFS=y CONFIG_ZSMALLOC=y CONFIG_BALANCE_ANON_FILE_RECLAIM=y CONFIG_FORCE_ALLOC_FROM_DMA_ZONE=y @@ -238,8 +240,11 @@ CONFIG_SCSI_SCAN_ASYNC=y CONFIG_SCSI_UFSHCD=y CONFIG_SCSI_UFSHCD_PLATFORM=y CONFIG_SCSI_UFS_QCOM=y +CONFIG_SCSI_UFS_QCOM_ICE=y CONFIG_MD=y CONFIG_BLK_DEV_DM=y +CONFIG_DM_CRYPT=y +CONFIG_DM_REQ_CRYPT=y CONFIG_DM_UEVENT=y CONFIG_DM_VERITY=y CONFIG_NETDEVICES=y @@ -399,6 +404,7 @@ CONFIG_HID_ELECOM=y CONFIG_HID_MAGICMOUSE=y CONFIG_HID_MICROSOFT=y CONFIG_HID_MULTITOUCH=y +CONFIG_HID_PLANTRONICS=y CONFIG_USB=y CONFIG_USB_ANNOUNCE_NEW_DEVICES=y CONFIG_USB_XHCI_HCD=y @@ -468,6 +474,7 @@ CONFIG_GSI=y CONFIG_IPA3=y CONFIG_RMNET_IPA3=y CONFIG_GPIO_USB_DETECT=y +CONFIG_SEEMP_CORE=y CONFIG_USB_BAM=y CONFIG_MSM_MDSS_PLL=y CONFIG_REMOTE_SPINLOCK_MSM=y @@ -491,6 +498,7 @@ CONFIG_MSM_SMP2P_TEST=y CONFIG_MSM_QMI_INTERFACE=y CONFIG_MSM_RPM_SMD=y CONFIG_QCOM_BUS_SCALING=y +CONFIG_MSM_SERVICE_LOCATOR=y CONFIG_MSM_IPC_ROUTER_SMD_XPRT=y CONFIG_MSM_SYSMON_GLINK_COMM=y CONFIG_MSM_IPC_ROUTER_GLINK_XPRT=y @@ -513,8 +521,10 @@ CONFIG_TRACER_PKT=y CONFIG_QCOM_FORCE_WDOG_BITE_ON_PANIC=y CONFIG_MSM_MPM_OF=y CONFIG_MSM_EVENT_TIMER=y +CONFIG_MSM_AVTIMER=y CONFIG_MSM_CORE_CTL_HELPER=y CONFIG_QCOM_REMOTEQDSS=y +CONFIG_MSM_SERVICE_NOTIFIER=y CONFIG_MSM_RPM_RBCPR_STATS_V2_LOG=y CONFIG_MSM_RPM_LOG=y CONFIG_MSM_RPM_STATS_LOG=y @@ -532,6 +542,7 @@ CONFIG_IIO=y CONFIG_QCOM_RRADC=y CONFIG_PWM=y CONFIG_PWM_QPNP=y +CONFIG_ARM_GIC_V3_ACL=y CONFIG_ANDROID=y CONFIG_ANDROID_BINDER_IPC=y CONFIG_SENSORS_SSC=y @@ -561,10 +572,8 @@ CONFIG_DEBUG_SET_MODULE_RONX=y CONFIG_DEBUG_RODATA=y CONFIG_DEBUG_ALIGN_RODATA=y CONFIG_CORESIGHT=y +CONFIG_CORESIGHT_LINKS_AND_SINKS=y CONFIG_CORESIGHT_LINK_AND_SINK_TMC=y -CONFIG_CORESIGHT_SOURCE_ETM4X=y -CONFIG_CORESIGHT_REMOTE_ETM=y -CONFIG_CORESIGHT_REMOTE_ETM_DEFAULT_ENABLE=0 CONFIG_CORESIGHT_QCOM_REPLICATOR=y CONFIG_CORESIGHT_STM=y CONFIG_CORESIGHT_HWEVENT=y diff --git a/arch/arm64/configs/msmfalcon_defconfig b/arch/arm64/configs/msmfalcon_defconfig index cfc2c30ca91f..0ccb7e243ec0 100644 --- a/arch/arm64/configs/msmfalcon_defconfig +++ b/arch/arm64/configs/msmfalcon_defconfig @@ -11,6 +11,7 @@ CONFIG_LOG_CPU_MAX_BUF_SHIFT=17 CONFIG_CGROUPS=y CONFIG_CGROUP_DEBUG=y CONFIG_CGROUP_FREEZER=y +CONFIG_CPUSETS=y CONFIG_CGROUP_CPUACCT=y CONFIG_CGROUP_SCHED=y CONFIG_RT_GROUP_SCHED=y @@ -52,6 +53,7 @@ CONFIG_PREEMPT=y CONFIG_HZ_100=y CONFIG_CLEANCACHE=y CONFIG_CMA=y +CONFIG_CMA_DEBUGFS=y CONFIG_ZSMALLOC=y CONFIG_SECCOMP=y CONFIG_ARMV8_DEPRECATED=y @@ -240,8 +242,11 @@ CONFIG_SCSI_SCAN_ASYNC=y CONFIG_SCSI_UFSHCD=y CONFIG_SCSI_UFSHCD_PLATFORM=y CONFIG_SCSI_UFS_QCOM=y +CONFIG_SCSI_UFS_QCOM_ICE=y CONFIG_MD=y CONFIG_BLK_DEV_DM=y +CONFIG_DM_CRYPT=y +CONFIG_DM_REQ_CRYPT=y CONFIG_DM_UEVENT=y CONFIG_DM_VERITY=y CONFIG_NETDEVICES=y @@ -402,6 +407,7 @@ CONFIG_HID_ELECOM=y CONFIG_HID_MAGICMOUSE=y CONFIG_HID_MICROSOFT=y CONFIG_HID_MULTITOUCH=y +CONFIG_HID_PLANTRONICS=y CONFIG_USB=y CONFIG_USB_XHCI_HCD=y CONFIG_USB_EHCI_HCD=y @@ -478,6 +484,7 @@ CONFIG_GSI=y CONFIG_IPA3=y CONFIG_RMNET_IPA3=y CONFIG_GPIO_USB_DETECT=y +CONFIG_SEEMP_CORE=y CONFIG_USB_BAM=y CONFIG_MSM_MDSS_PLL=y CONFIG_REMOTE_SPINLOCK_MSM=y @@ -533,6 +540,7 @@ CONFIG_TRACER_PKT=y CONFIG_QCOM_FORCE_WDOG_BITE_ON_PANIC=y CONFIG_MSM_MPM_OF=y CONFIG_MSM_EVENT_TIMER=y +CONFIG_MSM_AVTIMER=y CONFIG_MSM_CORE_CTL_HELPER=y CONFIG_QCOM_REMOTEQDSS=y CONFIG_MSM_SERVICE_NOTIFIER=y @@ -616,7 +624,8 @@ CONFIG_IPC_LOGGING=y CONFIG_QCOM_RTB=y CONFIG_QCOM_RTB_SEPARATE_CPUS=y CONFIG_FUNCTION_TRACER=y -CONFIG_TRACER_SNAPSHOT=y +CONFIG_IRQSOFF_TRACER=y +CONFIG_PREEMPT_TRACER=y CONFIG_BLK_DEV_IO_TRACE=y CONFIG_CPU_FREQ_SWITCH_PROFILER=y CONFIG_MEMTEST=y From ef1e55638d7d37fdee298ff8821b359052ef17ce Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Mon, 25 Jul 2016 17:10:46 -0700 Subject: [PATCH 03/16] sched: Remove unused migration notifier code. Migration notifiers were created to aid the CPU-boost driver manage CPU frequencies when tasks migrate from one CPU to another. Over time with the evolution of scheduler guided frequency, the scheduler now directly manages load when tasks migrate. Consequently the CPU-boost driver no longer makes use of this information. Remove unused code pertaining to this feature. Change-Id: I3529e4356e15e342a5fcfbcf3654396752a1d7cd Signed-off-by: Syed Rameez Mustafa --- include/linux/sched/sysctl.h | 1 - kernel/sched/core.c | 59 ------------------------------------ kernel/sched/fair.c | 38 ----------------------- kernel/sched/sched.h | 10 ------ kernel/sysctl.c | 7 ----- 5 files changed, 115 deletions(-) diff --git a/include/linux/sched/sysctl.h b/include/linux/sched/sysctl.h index 3e5fd5619367..4ed8c818f6d5 100644 --- a/include/linux/sched/sysctl.h +++ b/include/linux/sched/sysctl.h @@ -40,7 +40,6 @@ extern unsigned int sysctl_sched_min_granularity; extern unsigned int sysctl_sched_wakeup_granularity; extern unsigned int sysctl_sched_child_runs_first; extern unsigned int sysctl_sched_wake_to_idle; -extern unsigned int sysctl_sched_wakeup_load_threshold; extern unsigned int sysctl_sched_window_stats_policy; extern unsigned int sysctl_sched_ravg_hist_size; extern unsigned int sysctl_sched_cpu_high_irqload; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 25afcb8a1402..645db3ef9cbb 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -100,7 +100,6 @@ const char *task_event_names[] = {"PUT_PREV_TASK", "PICK_NEXT_TASK", const char *migrate_type_names[] = {"GROUP_TO_RQ", "RQ_TO_GROUP", "RQ_TO_RQ", "GROUP_TO_GROUP"}; -ATOMIC_NOTIFIER_HEAD(migration_notifier_head); ATOMIC_NOTIFIER_HEAD(load_alert_notifier_head); DEFINE_MUTEX(sched_domains_mutex); @@ -4333,7 +4332,6 @@ static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_ static void notify_migration(int src_cpu, int dest_cpu, bool src_cpu_dead, struct task_struct *p) { - struct migration_notify_data mnd; bool check_groups; rcu_read_lock(); @@ -4348,14 +4346,6 @@ static void notify_migration(int src_cpu, int dest_cpu, bool src_cpu_dead, } else { check_for_freq_change(cpu_rq(dest_cpu), true, check_groups); } - - if (task_notify_on_migrate(p)) { - mnd.src_cpu = src_cpu; - mnd.dest_cpu = dest_cpu; - mnd.load = pct_task_load(p); - atomic_notifier_call_chain(&migration_notifier_head, 0, - (void *)&mnd); - } } /* @@ -5181,8 +5171,6 @@ static void ttwu_queue(struct task_struct *p, int cpu) raw_spin_unlock(&rq->lock); } -__read_mostly unsigned int sysctl_sched_wakeup_load_threshold = 110; - /** * try_to_wake_up - wake up a thread * @p: the thread to be awakened @@ -5203,8 +5191,6 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) { unsigned long flags; int cpu, src_cpu, success = 0; - int notify = 0; - struct migration_notify_data mnd; #ifdef CONFIG_SMP unsigned int old_load; struct rq *rq; @@ -5309,31 +5295,9 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) ttwu_queue(p, cpu); stat: ttwu_stat(p, cpu, wake_flags); - - if (task_notify_on_migrate(p)) { - mnd.src_cpu = src_cpu; - mnd.dest_cpu = cpu; - mnd.load = pct_task_load(p); - - /* - * Call the migration notifier with mnd for foreground task - * migrations as well as for wakeups if their load is above - * sysctl_sched_wakeup_load_threshold. This would prompt the - * cpu-boost to boost the CPU frequency on wake up of a heavy - * weight foreground task - */ - if ((src_cpu != cpu) || (mnd.load > - sysctl_sched_wakeup_load_threshold)) - notify = 1; - } - out: raw_spin_unlock_irqrestore(&p->pi_lock, flags); - if (notify) - atomic_notifier_call_chain(&migration_notifier_head, - 0, (void *)&mnd); - if (freq_notif_allowed) { if (!same_freq_domain(src_cpu, cpu)) { check_for_freq_change(cpu_rq(cpu), @@ -11793,24 +11757,6 @@ static void cpu_cgroup_attach(struct cgroup_taskset *tset) sched_move_task(task); } -static u64 cpu_notify_on_migrate_read_u64(struct cgroup_subsys_state *css, - struct cftype *cft) -{ - struct task_group *tg = css_tg(css); - - return tg->notify_on_migrate; -} - -static int cpu_notify_on_migrate_write_u64(struct cgroup_subsys_state *css, - struct cftype *cft, u64 notify) -{ - struct task_group *tg = css_tg(css); - - tg->notify_on_migrate = (notify > 0); - - return 0; -} - #ifdef CONFIG_SCHED_HMP static u64 cpu_upmigrate_discourage_read_u64(struct cgroup_subsys_state *css, @@ -12135,11 +12081,6 @@ static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, #endif /* CONFIG_RT_GROUP_SCHED */ static struct cftype cpu_files[] = { - { - .name = "notify_on_migrate", - .read_u64 = cpu_notify_on_migrate_read_u64, - .write_u64 = cpu_notify_on_migrate_write_u64, - }, #ifdef CONFIG_SCHED_HMP { .name = "upmigrate_discourage", diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 584cd048c24b..18b859c44574 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7579,9 +7579,6 @@ struct lb_env { struct list_head tasks; }; -static DEFINE_PER_CPU(bool, dbs_boost_needed); -static DEFINE_PER_CPU(int, dbs_boost_load_moved); - /* * Is this task likely cache-hot: */ @@ -7822,7 +7819,6 @@ static struct task_struct *detach_one_task(struct lb_env *env) * inside detach_tasks(). */ schedstat_inc(env->sd, lb_gained[env->idle]); - per_cpu(dbs_boost_load_moved, env->dst_cpu) += pct_task_load(p); return p; } @@ -7895,7 +7891,6 @@ redo: detached++; env->imbalance -= load; - per_cpu(dbs_boost_load_moved, env->dst_cpu) += pct_task_load(p); #ifdef CONFIG_PREEMPT /* @@ -7949,8 +7944,6 @@ static void attach_task(struct rq *rq, struct task_struct *p) activate_task(rq, p, 0); p->on_rq = TASK_ON_RQ_QUEUED; check_preempt_curr(rq, p, 0); - if (task_notify_on_migrate(p)) - per_cpu(dbs_boost_needed, task_cpu(p)) = true; } /* @@ -9241,7 +9234,6 @@ static int load_balance(int this_cpu, struct rq *this_rq, cpumask_copy(cpus, cpu_active_mask); - per_cpu(dbs_boost_load_moved, this_cpu) = 0; schedstat_inc(sd, lb_count[idle]); redo: @@ -9436,20 +9428,6 @@ no_move: } } else { sd->nr_balance_failed = 0; - if (per_cpu(dbs_boost_needed, this_cpu)) { - struct migration_notify_data mnd; - - mnd.src_cpu = cpu_of(busiest); - mnd.dest_cpu = this_cpu; - mnd.load = per_cpu(dbs_boost_load_moved, this_cpu); - if (mnd.load > 100) - mnd.load = 100; - atomic_notifier_call_chain(&migration_notifier_head, - 0, (void *)&mnd); - per_cpu(dbs_boost_needed, this_cpu) = false; - per_cpu(dbs_boost_load_moved, this_cpu) = 0; - - } /* Assumes one 'busiest' cpu that we pulled tasks from */ if (!same_freq_domain(this_cpu, cpu_of(busiest))) { @@ -9681,8 +9659,6 @@ static int active_load_balance_cpu_stop(void *data) raw_spin_lock_irq(&busiest_rq->lock); - per_cpu(dbs_boost_load_moved, target_cpu) = 0; - /* make sure the requested cpu hasn't gone down in the meantime */ if (unlikely(busiest_cpu != smp_processor_id() || !busiest_rq->active_balance)) @@ -9765,20 +9741,6 @@ out_unlock: check_for_freq_change(target_rq, true, false); } - if (per_cpu(dbs_boost_needed, target_cpu)) { - struct migration_notify_data mnd; - - mnd.src_cpu = cpu_of(busiest_rq); - mnd.dest_cpu = target_cpu; - mnd.load = per_cpu(dbs_boost_load_moved, target_cpu); - if (mnd.load > 100) - mnd.load = 100; - atomic_notifier_call_chain(&migration_notifier_head, - 0, (void *)&mnd); - - per_cpu(dbs_boost_needed, target_cpu) = false; - per_cpu(dbs_boost_load_moved, target_cpu) = 0; - } return 0; } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index b9566cf3ad37..3209b13207ea 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -254,7 +254,6 @@ struct cfs_bandwidth { struct task_group { struct cgroup_subsys_state css; - bool notify_on_migrate; #ifdef CONFIG_SCHED_HMP bool upmigrate_discouraged; #endif @@ -1496,11 +1495,6 @@ static inline struct task_group *task_group(struct task_struct *p) return p->sched_task_group; } -static inline bool task_notify_on_migrate(struct task_struct *p) -{ - return task_group(p)->notify_on_migrate; -} - /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { @@ -1526,10 +1520,6 @@ static inline struct task_group *task_group(struct task_struct *p) { return NULL; } -static inline bool task_notify_on_migrate(struct task_struct *p) -{ - return false; -} #endif /* CONFIG_CGROUP_SCHED */ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 81fbed978da3..2e77841e4eda 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -292,13 +292,6 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = proc_dointvec, }, - { - .procname = "sched_wakeup_load_threshold", - .data = &sysctl_sched_wakeup_load_threshold, - .maxlen = sizeof(unsigned int), - .mode = 0644, - .proc_handler = proc_dointvec, - }, #ifdef CONFIG_SCHED_FREQ_INPUT { .procname = "sched_freq_inc_notify", From e97839440656ab1cae408d36cda646221165c934 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Thu, 28 Jul 2016 10:53:01 -0700 Subject: [PATCH 04/16] sched: Remove unused PELT extensions for HMP scheduling PELT extensions for HMP have never been used since the early days of the HMP scheduler. Furthermore, changes to PELT itself in newer kernel versions render some of the code redundant or incorrect. These extensions have not been tested for a long time and are practically dead code. Remove it so that future upgrades become easier. Change-Id: I029f327406ca00b2370c93134158b61dda3b81e3 Signed-off-by: Syed Rameez Mustafa --- include/linux/sched.h | 3 -- kernel/sched/core.c | 22 +++++++------ kernel/sched/debug.c | 3 -- kernel/sched/fair.c | 75 +------------------------------------------ kernel/sched/sched.h | 13 ++------ 5 files changed, 15 insertions(+), 101 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 06dd540192c7..5e644bdd107c 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1232,9 +1232,6 @@ struct sched_avg { u64 last_update_time, load_sum; u32 util_sum, period_contrib; unsigned long load_avg, util_avg; -#ifdef CONFIG_SCHED_HMP - u32 runnable_avg_sum_scaled; -#endif }; #ifdef CONFIG_SCHEDSTATS diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 645db3ef9cbb..b599a01c4f53 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1801,9 +1801,6 @@ __read_mostly unsigned int sysctl_sched_pred_alert_freq = 10 * 1024 * 1024; #endif /* CONFIG_SCHED_FREQ_INPUT */ -/* 1 -> use PELT based load stats, 0 -> use window-based load stats */ -unsigned int __read_mostly sched_use_pelt; - /* * Maximum possible frequency across all cpus. Task demand and cpu * capacity (cpu_power) metrics are scaled in reference to it. @@ -1911,11 +1908,17 @@ static inline int exiting_task(struct task_struct *p) static int __init set_sched_ravg_window(char *str) { - get_option(&str, &sched_ravg_window); + unsigned int window_size; - sched_use_pelt = (sched_ravg_window < MIN_SCHED_RAVG_WINDOW || - sched_ravg_window > MAX_SCHED_RAVG_WINDOW); + get_option(&str, &window_size); + if (window_size < MIN_SCHED_RAVG_WINDOW || + window_size > MAX_SCHED_RAVG_WINDOW) { + WARN_ON(1); + return -EINVAL; + } + + sched_ravg_window = window_size; return 0; } @@ -2949,7 +2952,7 @@ static void update_task_ravg(struct task_struct *p, struct rq *rq, int event, u64 wallclock, u64 irqtime) { - if (sched_use_pelt || !rq->window_start || sched_disable_window_stats) + if (!rq->window_start || sched_disable_window_stats) return; lockdep_assert_held(&rq->lock); @@ -3430,9 +3433,8 @@ int sched_set_window(u64 window_start, unsigned int window_size) s64 ws; unsigned long flags; - if (sched_use_pelt || - (window_size * TICK_NSEC < MIN_SCHED_RAVG_WINDOW)) - return -EINVAL; + if (window_size * TICK_NSEC < MIN_SCHED_RAVG_WINDOW) + return -EINVAL; mutex_lock(&policy_mutex); diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index d1c0ef4bf07d..fc9878eee5df 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -417,10 +417,8 @@ static void sched_debug_header(struct seq_file *m) P(sched_upmigrate); P(sched_downmigrate); P(sched_init_task_load_windows); - P(sched_init_task_load_pelt); P(min_capacity); P(max_capacity); - P(sched_use_pelt); P(sched_ravg_window); #endif #undef PN @@ -644,7 +642,6 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m) __P(load_avg); #ifdef CONFIG_SCHED_HMP P(ravg.demand); - P(se.avg.runnable_avg_sum_scaled); #endif #endif diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 18b859c44574..034c9414cabd 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2577,9 +2577,6 @@ static u32 __compute_runnable_contrib(u64 n) return contrib + runnable_avg_yN_sum[n]; } -static void add_to_scaled_stat(int cpu, struct sched_avg *sa, u64 delta); -static inline void decay_scaled_stat(struct sched_avg *sa, u64 periods); - struct cpu_pwr_stats __weak *get_cpu_pwr_stats(void) { return NULL; @@ -2594,15 +2591,11 @@ enum sched_boost_type { #ifdef CONFIG_SCHED_HMP /* Initial task load. Newly created tasks are assigned this load. */ -unsigned int __read_mostly sched_init_task_load_pelt; unsigned int __read_mostly sched_init_task_load_windows; unsigned int __read_mostly sysctl_sched_init_task_load_pct = 15; unsigned int max_task_load(void) { - if (sched_use_pelt) - return LOAD_AVG_MAX; - return sched_ravg_window; } @@ -2723,11 +2716,6 @@ void set_hmp_defaults(void) sched_major_task_runtime = mult_frac(sched_ravg_window, MAJOR_TASK_PCT, 100); #endif - - sched_init_task_load_pelt = - div64_u64((u64)sysctl_sched_init_task_load_pct * - (u64)LOAD_AVG_MAX, 100); - sched_init_task_load_windows = div64_u64((u64)sysctl_sched_init_task_load_pct * (u64)sched_ravg_window, 100); @@ -4323,7 +4311,6 @@ __update_load_avg(u64 now, int cpu, struct sched_avg *sa, u32 contrib; unsigned int delta_w, scaled_delta_w, decayed = 0; unsigned long scale_freq, scale_cpu; - struct sched_entity *se = NULL; delta = now - sa->last_update_time; /* @@ -4344,12 +4331,6 @@ __update_load_avg(u64 now, int cpu, struct sched_avg *sa, return 0; sa->last_update_time = now; - if (sched_use_pelt && cfs_rq && weight) { - se = container_of(sa, struct sched_entity, avg); - if (entity_is_task(se) && se->on_rq) - dec_hmp_sched_stats_fair(rq_of(cfs_rq), task_of(se)); - } - scale_freq = arch_scale_freq_capacity(NULL, cpu); scale_cpu = arch_scale_cpu_capacity(NULL, cpu); @@ -4370,7 +4351,6 @@ __update_load_avg(u64 now, int cpu, struct sched_avg *sa, scaled_delta_w = cap_scale(delta_w, scale_freq); if (weight) { sa->load_sum += weight * scaled_delta_w; - add_to_scaled_stat(cpu, sa, delta_w); if (cfs_rq) { cfs_rq->runnable_load_sum += weight * scaled_delta_w; @@ -4397,7 +4377,6 @@ __update_load_avg(u64 now, int cpu, struct sched_avg *sa, contrib = cap_scale(contrib, scale_freq); if (weight) { sa->load_sum += weight * contrib; - add_to_scaled_stat(cpu, sa, contrib); if (cfs_rq) cfs_rq->runnable_load_sum += weight * contrib; } @@ -4409,14 +4388,10 @@ __update_load_avg(u64 now, int cpu, struct sched_avg *sa, scaled_delta = cap_scale(delta, scale_freq); if (weight) { sa->load_sum += weight * scaled_delta; - add_to_scaled_stat(cpu, sa, delta); if (cfs_rq) cfs_rq->runnable_load_sum += weight * scaled_delta; } - if (se && entity_is_task(se) && se->on_rq) - inc_hmp_sched_stats_fair(rq_of(cfs_rq), task_of(se)); - if (running) sa->util_sum += scaled_delta * scale_cpu; @@ -4676,7 +4651,6 @@ void init_new_task_load(struct task_struct *p) { int i; u32 init_load_windows = sched_init_task_load_windows; - u32 init_load_pelt = sched_init_task_load_pelt; u32 init_load_pct = current->init_load_pct; p->init_load_pct = 0; @@ -4685,18 +4659,14 @@ void init_new_task_load(struct task_struct *p) memset(&p->ravg, 0, sizeof(struct ravg)); p->cpu_cycles = 0; - if (init_load_pct) { - init_load_pelt = div64_u64((u64)init_load_pct * - (u64)LOAD_AVG_MAX, 100); + if (init_load_pct) init_load_windows = div64_u64((u64)init_load_pct * (u64)sched_ravg_window, 100); - } p->ravg.demand = init_load_windows; clear_ravg_pred_demand(); for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) p->ravg.sum_history[i] = init_load_windows; - p->se.avg.runnable_avg_sum_scaled = init_load_pelt; } #else /* CONFIG_SCHED_HMP */ @@ -4719,40 +4689,6 @@ unsigned int pct_task_load(struct task_struct *p) return load; } -/* - * Add scaled version of 'delta' to runnable_avg_sum_scaled - * 'delta' is scaled in reference to "best" cpu - */ -static inline void -add_to_scaled_stat(int cpu, struct sched_avg *sa, u64 delta) -{ - int cur_freq = cpu_cur_freq(cpu); - u64 scaled_delta; - int sf; - - if (!sched_enable_hmp) - return; - - if (unlikely(cur_freq > max_possible_freq)) - cur_freq = max_possible_freq; - - scaled_delta = div64_u64(delta * cur_freq, max_possible_freq); - sf = (cpu_efficiency(cpu) * 1024) / max_possible_efficiency; - scaled_delta *= sf; - scaled_delta >>= 10; - sa->runnable_avg_sum_scaled += scaled_delta; -} - -static inline void decay_scaled_stat(struct sched_avg *sa, u64 periods) -{ - if (!sched_enable_hmp) - return; - - sa->runnable_avg_sum_scaled = - decay_load(sa->runnable_avg_sum_scaled, - periods); -} - #ifdef CONFIG_CFS_BANDWIDTH static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) @@ -4814,15 +4750,6 @@ static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, #else /* CONFIG_SCHED_HMP */ -static inline void -add_to_scaled_stat(int cpu, struct sched_avg *sa, u64 delta) -{ -} - -static inline void decay_scaled_stat(struct sched_avg *sa, u64 periods) -{ -} - static inline void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { } static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 3209b13207ea..28e2cb22e313 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1047,7 +1047,6 @@ extern void init_new_task_load(struct task_struct *p); extern struct mutex policy_mutex; extern unsigned int sched_ravg_window; -extern unsigned int sched_use_pelt; extern unsigned int sched_disable_window_stats; extern unsigned int sched_enable_hmp; extern unsigned int max_possible_freq; @@ -1062,7 +1061,6 @@ extern unsigned int max_possible_capacity; extern unsigned int min_max_possible_capacity; extern unsigned int sched_upmigrate; extern unsigned int sched_downmigrate; -extern unsigned int sched_init_task_load_pelt; extern unsigned int sched_init_task_load_windows; extern unsigned int up_down_migrate_scale_factor; extern unsigned int sysctl_sched_restrict_cluster_spill; @@ -1179,9 +1177,6 @@ static inline u64 scale_load_to_cpu(u64 task_load, int cpu) static inline unsigned int task_load(struct task_struct *p) { - if (sched_use_pelt) - return p->se.avg.runnable_avg_sum_scaled; - return p->ravg.demand; } @@ -1202,8 +1197,7 @@ inc_cumulative_runnable_avg(struct hmp_sched_stats *stats, if (!sched_enable_hmp || sched_disable_window_stats) return; - task_load = sched_use_pelt ? p->se.avg.runnable_avg_sum_scaled : - (sched_disable_window_stats ? 0 : p->ravg.demand); + task_load = sched_disable_window_stats ? 0 : p->ravg.demand; stats->cumulative_runnable_avg += task_load; set_pred_demands_sum(stats, stats->pred_demands_sum + @@ -1219,8 +1213,7 @@ dec_cumulative_runnable_avg(struct hmp_sched_stats *stats, if (!sched_enable_hmp || sched_disable_window_stats) return; - task_load = sched_use_pelt ? p->se.avg.runnable_avg_sum_scaled : - (sched_disable_window_stats ? 0 : p->ravg.demand); + task_load = sched_disable_window_stats ? 0 : p->ravg.demand; stats->cumulative_runnable_avg -= task_load; @@ -1287,8 +1280,6 @@ struct related_thread_group *task_related_thread_group(struct task_struct *p) #else /* CONFIG_SCHED_HMP */ -#define sched_use_pelt 0 - struct hmp_sched_stats; struct related_thread_group; From e2b9b4a39560077bf20712564a8de920431f1c62 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Thu, 28 Jul 2016 11:22:08 -0700 Subject: [PATCH 05/16] sched: Move CPU cstate tracking under CONFIG_SCHED_HMP While tracking C-states makes sense under CONFIG_SMP as well, cstate information is currently unused under CONFIG_SMP. Move it under CONFIG_SCHED_HMP for now since that is the only place it is relevant at the moment. Change-Id: Ifc5812cfe14ebf2b4d447100dcd87f02ab29ff7a Signed-off-by: Syed Rameez Mustafa --- include/linux/sched.h | 36 +++++++++++++++++++----------------- kernel/sched/core.c | 41 +++++++++++++++++++++-------------------- kernel/sched/sched.h | 2 +- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 5e644bdd107c..c326611099d7 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2346,10 +2346,6 @@ extern void do_set_cpus_allowed(struct task_struct *p, extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask); -extern void sched_set_cpu_cstate(int cpu, int cstate, - int wakeup_energy, int wakeup_latency); -extern void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, - int wakeup_energy, int wakeup_latency); #else static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) @@ -2362,15 +2358,6 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p, return -EINVAL; return 0; } -static inline void -sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) -{ -} - -static inline void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, - int dstate, int wakeup_energy, int wakeup_latency) -{ -} #endif extern int sched_set_wake_up_idle(struct task_struct *p, int wake_up_idle); @@ -2388,9 +2375,14 @@ extern unsigned int sched_get_static_cpu_pwr_cost(int cpu); extern int sched_set_static_cluster_pwr_cost(int cpu, unsigned int cost); extern unsigned int sched_get_static_cluster_pwr_cost(int cpu); extern int sched_update_freq_max_load(const cpumask_t *cpumask); -extern void sched_update_cpu_freq_min_max(const cpumask_t *cpus, u32 fmin, u32 - fmax); -#else +extern void sched_update_cpu_freq_min_max(const cpumask_t *cpus, + u32 fmin, u32 fmax); +extern void sched_set_cpu_cstate(int cpu, int cstate, + int wakeup_energy, int wakeup_latency); +extern void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, + int wakeup_energy, int wakeup_latency); + +#else /* CONFIG_SCHED_HMP */ static inline int sched_set_boost(int enable) { return -EINVAL; @@ -2403,7 +2395,17 @@ static inline int sched_update_freq_max_load(const cpumask_t *cpumask) static inline void sched_update_cpu_freq_min_max(const cpumask_t *cpus, u32 fmin, u32 fmax) { } -#endif + +static inline void +sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) +{ +} + +static inline void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, + int dstate, int wakeup_energy, int wakeup_latency) +{ +} +#endif /* CONFIG_SCHED_HMP */ #ifdef CONFIG_NO_HZ_COMMON void calc_load_enter_idle(void); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b599a01c4f53..82a2fe73345e 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -774,24 +774,6 @@ void sched_avg_update(struct rq *rq) } } -/* - * Note C-state for (idle) cpus. - * - * @cstate = cstate index, 0 -> active state - * @wakeup_energy = energy spent in waking up cpu - * @wakeup_latency = latency to wakeup from cstate - * - */ -void -sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) -{ - struct rq *rq = cpu_rq(cpu); - - rq->cstate = cstate; /* C1, C2 etc */ - rq->wakeup_energy = wakeup_energy; - rq->wakeup_latency = wakeup_latency; -} - #endif /* CONFIG_SMP */ #ifdef CONFIG_SCHED_HMP @@ -850,6 +832,24 @@ static inline void set_task_last_switch_out(struct task_struct *p, p->last_switch_out_ts = wallclock; } +/* + * Note C-state for (idle) cpus. + * + * @cstate = cstate index, 0 -> active state + * @wakeup_energy = energy spent in waking up cpu + * @wakeup_latency = latency to wakeup from cstate + * + */ +void +sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) +{ + struct rq *rq = cpu_rq(cpu); + + rq->cstate = cstate; /* C1, C2 etc */ + rq->wakeup_energy = wakeup_energy; + rq->wakeup_latency = wakeup_latency; +} + /* * Note D-state for (idle) cluster. * @@ -10941,6 +10941,9 @@ void __init sched_init(void) rq->static_cpu_pwr_cost = 0; rq->cc.cycles = SCHED_MIN_FREQ; rq->cc.time = 1; + rq->cstate = 0; + rq->wakeup_latency = 0; + rq->wakeup_energy = 0; /* * All cpus part of same cluster by default. This avoids the @@ -10959,8 +10962,6 @@ void __init sched_init(void) #endif #endif rq->max_idle_balance_cost = sysctl_sched_migration_cost; - rq->cstate = 0; - rq->wakeup_latency = 0; INIT_LIST_HEAD(&rq->cfs_tasks); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 28e2cb22e313..b45c88735ea6 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -735,7 +735,6 @@ struct rq { u64 age_stamp; u64 idle_stamp; u64 avg_idle; - int cstate, wakeup_latency, wakeup_energy; /* This is used to determine avg_idle's max value */ u64 max_idle_balance_cost; @@ -746,6 +745,7 @@ struct rq { struct cpumask freq_domain_cpumask; struct hmp_sched_stats hmp_stats; + int cstate, wakeup_latency, wakeup_energy; u64 window_start; unsigned long hmp_flags; From 9b6911e89aef49ea96ae64f8fe75317227d9f04f Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Thu, 28 Jul 2016 19:02:09 -0700 Subject: [PATCH 06/16] Documentation: scheduler: Rename sched-zone.txt to sched-hmp.txt The QHMP version of the scheduler is no longer available from this kernel vesion onwards. sched-zone.txt should be used instead for the upgraded zone scheduler. Remove the original sched-hmp.txt and reuse the name for sched-zone.txt. Change-Id: I9b033101b903dc49b8683025602d2af395501daa Signed-off-by: Syed Rameez Mustafa --- Documentation/scheduler/sched-hmp.txt | 712 +++++------- Documentation/scheduler/sched-zone.txt | 1437 ------------------------ 2 files changed, 305 insertions(+), 1844 deletions(-) delete mode 100644 Documentation/scheduler/sched-zone.txt diff --git a/Documentation/scheduler/sched-hmp.txt b/Documentation/scheduler/sched-hmp.txt index 5e72fdd18fc1..5a84f0524481 100644 --- a/Documentation/scheduler/sched-hmp.txt +++ b/Documentation/scheduler/sched-hmp.txt @@ -18,20 +18,24 @@ CONTENTS 4. CPU Power 5. HMP Scheduler 5.1 Classification of Tasks and CPUs - 5.2 Task Wakeup and select_best_cpu() + 5.2 select_best_cpu() + 5.2.1 sched_boost + 5.2.2 task_will_fit() + 5.2.3 Tunables affecting select_best_cpu() + 5.2.4 Wakeup Logic 5.3 Scheduler Tick 5.4 Load Balancer 5.5 Real Time Tasks 5.6 Task packing 6. Frequency Guidance 6.1 Per-CPU Window-Based Stats - 6.1 Per-task Window-Based Stats + 6.2 Per-task Window-Based Stats 6.3 Effect of various task events 7. Tunables 8. HMP Scheduler Trace Points 8.1 sched_enq_deq_task 8.2 sched_task_load - 8.3 sched_cpu_load + 8.3 sched_cpu_load_* 8.4 sched_update_task_ravg 8.5 sched_update_history 8.6 sched_reset_all_windows_stats @@ -123,7 +127,7 @@ since v3.7, has some perceived shortcomings when used to place tasks on HMP systems or provide recommendations on CPU frequency. Per-entity load tracking does not make a distinction between the ramp up -vs. ramp down time of task load. It also decays task load without exception when +vs ramp down time of task load. It also decays task load without exception when a task sleeps. As an example, a cpu bound task at its peak load (LOAD_AVG_MAX or 47742) can see its load decay to 0 after a sleep of just 213ms! A cpu-bound task running on a performance-efficient cpu could thus get re-classified as not @@ -520,21 +524,6 @@ available to the HMP scheduler: Combined with tunable parameters, this information can be used to classify both tasks and CPUs to aid in the placement of tasks. -- small task - - Small tasks are tasks that have relatively little CPU - demand. Normally it is desirable to wake a task on an idle CPU to - minimize the latency for it to execute - this may mean waking the - idle CPU up out of a deep power-saving state. For small tasks - however this may not be the case. Because a small task is expected - to run for very little time, it may be better to put it on a CPU - which is not idle, but lightly loaded. - - The small task threshold is set by the value - /proc/sys/kenrel/sched_small_task. This value is a percentage. If the - task consumes this much or less of the minimum CPU in the system, the - task is considered "small." - - big task A big task is one that exerts a CPU demand too high for a particular @@ -561,7 +550,7 @@ both tasks and CPUs to aid in the placement of tasks. particular CPU, that CPU will be considered too small for the task. The task will thus be seen as a "big" task on the cpu and will reflect in nr_big_tasks statistics maintained for that cpu. Note that certain tasks (whose nice - value exceeds sched_upmigrate_min_nice value or those that belong to a cgroup + value exceeds SCHED_UPMIGRATE_MIN_NICE value or those that belong to a cgroup whose upmigrate_discourage flag is set) will never be classified as big tasks despite their high demand. @@ -583,51 +572,23 @@ both tasks and CPUs to aid in the placement of tasks. between the upmigrate and downmigrate, it is clipped to a value such that the difference between the modified and the original thresholds is same. -- mostly_idle - - The "mostly_idle" classification applies to CPUs. This - classification attempts to answer the following question: if a task - is put on this CPU, is it likely to be able to run with low contention for - bandwidth? One possible way to answer this question would be to just check - whether the CPU is idle or not. That may be too conservative however. The CPU - may be currently executing a very small task and could become idle soon. Since - the scheduler is tracking the demand of each task it can make an educated - guess as to whether a CPU will become idle in the near future. - - There are three tunable parameters which are used to determine whether - a CPU is mostly idle: - - /sys/devices/system/cpu/cpuX/sched_mostly_idle_nr_run - /sys/devices/system/cpu/cpuX/sched_mostly_idle_load - /sys/devices/system/cpu/cpuX/sched_mostly_idle_freq - - Note that these tunables are per-cpu. If a CPU does not have more than - sched_mostly_idle_nr_run runnable tasks and is not more than - sched_mostly_idle_load percent busy, it is considered mostly idle. - Additionally if a cpu's sched_mostly_idle_freq is non-zero and its current - frequency is less than threshold, then scheduler will attempt to pack - tasks on the most power-efficient cpu in the cluster. - - spill threshold - The spill threshold determines how much task load the scheduler - should put on a CPU before considering that CPU busy and putting the - load elsewhere. This allows a configurable level of task packing within - one or more CPUs in the system. How aggressively should the scheduler - attempt to fill CPUs with task demand before utilizing other CPUs? + Tasks will normally be placed on lowest power-cost cluster where they can fit. + This could result in power-efficient cluster becoming overcrowded when there + are "too" many low-demand tasks. Spill threshold provides a spill over + criteria, wherein low-demand task are allowed to be placed on idle or + busy cpus in high-performance cluster. - These two tunable parameters together define the spill threshold. + Scheduler will avoid placing a task on a cpu if it can result in cpu exceeding + its spill threshold, which is defined by two tunables: - /proc/sys/kernel/sched_spill_nr_run - /proc/sys/kernel/sched_spill_load + /proc/sys/kernel/sched_spill_nr_run (default: 10) + /proc/sys/kernel/sched_spill_load (default : 100%) - If placing a task on a CPU would cause it to have more than - sched_spill_nr_run runnable tasks, or would cause the CPU to be more - than sched_spill_load percent busy, the scheduler will interpret that as - causing the CPU to cross its spill threshold. Spill threshold is only - considered when having to consider whether a task, which can fit in - a power-efficient cpu, should spill over to a high-performance CPU because - the aggregate load of power-efficient cpus exceed their spill threshold. + A cpu is considered to be above its spill level if it already has 10 tasks or + if the sum of task load (scaled in reference to given cpu) and + rq->cumulative_runnable_avg exceeds 'sched_spill_load'. - power band @@ -646,82 +607,154 @@ both tasks and CPUs to aid in the placement of tasks. be in a different "band" and it is selected, despite perhaps having a higher current task load. -*** 5.2 Task Wakeup and select_best_cpu() +*** 5.2 select_best_cpu() -CPU placement of a waking task is the single most important decision -made by the HMP scheduler. This section will describe the call flow -and algorithm used in detail. +CPU placement decisions for a task at its wakeup or creation time are the +most important decisions made by the HMP scheduler. This section will describe +the call flow and algorithm used in detail. -The primary entry point for a task wakeup operation is -try_to_wake_up(), located in kernel/sched/core.c. This function relies -on select_task_rq() to determine the target CPU for the waking -task. For fair-class (SCHED_OTHER) tasks, that request will be routed -to select_task_rq_fair() in kernel/sched/fair.c. As part of these -scheduler extensions a hook has been inserted into the top of that -function. If HMP scheduling is enabled the normal scheduling behavior -will be replaced by a call to select_best_cpu(). This function, -select_best_cpu(), represents the heart of the HMP scheduling -algorithm described in this document. +The primary entry point for a task wakeup operation is try_to_wake_up(), +located in kernel/sched/core.c. This function relies on select_task_rq() to +determine the target CPU for the waking task. For fair-class (SCHED_OTHER) +tasks, that request will be routed to select_task_rq_fair() in +kernel/sched/fair.c. As part of these scheduler extensions a hook has been +inserted into the top of that function. If HMP scheduling is enabled the normal +scheduling behavior will be replaced by a call to select_best_cpu(). This +function, select_best_cpu(), represents the heart of the HMP scheduling +algorithm described in this document. Note that select_best_cpu() is also +invoked for a task being created. -The behavior of select_best_cpu() differs depending on whether the -task being placed is a small task or not and the value of the sched_prefer_idle -tunable. +The behavior of select_best_cpu() depends on several factors such as boost +setting, choice of several tunables and on task demand. ---- Wakeup Logic a Non-Small Task "p" +**** 5.2.1 Boost -The order of CPU preference for a non-small task when sched_prefer_idle = 1 is -the following: +The task placement policy changes signifincantly when scheduler boost is in +effect. When boost is in effect the scheduler ignores the power cost of +placing tasks on CPUs. Instead it figures out the load on each CPU and then +places task on the least loaded CPU. If the load of two or more CPUs is the +same (generally when CPUs are idle) the task prefers to go highest capacity +CPU in the system. - 1. The shallowest-cstate idle CPU in the lowest-power cluster which can fit - the task. Where there is a tie of two CPUs with the same load, the CPU with - the lowest power cost is chosen. +A further enhancement during boost is the scheduler' early detection feature. +While boost is in effect the scheduler checks for the precence of tasks that +have been runnable for over some period of time within the tick. For such +tasks the scheduler informs the governor of imminent need for high frequency. +If there exists a task on the runqueue at the tick that has been runnable +for greater than SCHED_EARLY_DETECTION_DURATION amount of time, it notifies +the governor with a fabricated load of the full window at the highest +frequency. The fabricated load is maintained until the task is no longer +runnable or until the next tick. - 2. The least-loaded CPU the task is allowed to run on in the lowest power band - where the task will fit and where the placement will not result in cpu - exceeding spill level. When there is a tie of two CPUs at same load, the - CPU with the lowest power cost is chosen. +Boost can be set via either /proc/sys/kernel/sched_boost or by invoking +kernel API sched_set_boost(). - 3. The least-loaded mostly idle CPU that the task is allowed to run on where - the task won't fit (since there was no CPU where the task would fit). + int sched_set_boost(int enable); - 4. The CPU which the task last ran on. +Once turned on, boost will remain in effect until it is explicitly turned off. +To allow for boost to be controlled by multiple external entities (application +or kernel module) at same time, boost setting is reference counted. This means +that two applications can turn on boost and the effect of boost is eliminated +only after both applications have turned off boost. boost_refcount variable +represents this reference count. -The order of CPU preference for a non-small task when sched_prefer_idle = 0 -is the following: +**** 5.2.2 task_will_fit() - 1. The least-loaded non-idle mostly idle CPU the task is allowed to run on in - the lowest power band where the task will fit. When there is a tie of two - CPUs at same load, the CPU with the lowest power cost is chosen. +The overall goal of select_best_cpu() is to place a task on the least power +cluster where it can "fit" i.e where its cpu usage shall be below the capacity +offered by cluster. Criteria for a task to be considered as fitting in a cluster +is: - 2. The shallowest-cstate idle CPU in the lowest-power cluster which can fit - the task. Where there is a tie of two CPUs with the same load, the CPU with - the lowest power cost is chosen. + i) A low-priority task, whose nice value is greater than + SCHED_UPMIGRATE_MIN_NICE or whose cgroup has its + upmigrate_discourage flag set, is considered to be fitting in all clusters, + irrespective of their capacity and task's cpu demand. - 3. The least-loaded CPU the task is allowed to run on in the lowest power band - where the task will fit and where the placement will not result in the CPU - exceeding spill level. When there is a tie of two CPUs at the same load, - the CPU with the lowest power cost is chosen. + ii) All tasks are considered to fit in highest capacity cluster. - 4. The least-loaded mostly idle CPU that the task is allowed to run on where - the task won't fit (since there was no CPU where the task would fit). + iii) Task demand scaled in reference to the given cluster should be less than a + threshold. See section on load_scale_factor to know more about how task + demand is scaled in reference to a given cpu (cluster). The threshold used + is normally sched_upmigrate. Its possible for a task's demand to exceed + sched_upmigrate threshold in reference to a cluster when its upmigrated to + higher capacity cluster. To prevent it from coming back immediately to + lower capacity cluster, the task is not considered to "fit" on its earlier + cluster until its demand has dropped below sched_downmigrate in reference + to that earlier cluster. sched_downmigrate thus provides for some + hysteresis control. - 5. The CPU which the task last ran on. ---- Wakeup Logic a Small Task "p" +**** 5.2.3 Factors affecting select_best_cpu() -The order of CPU preference for a small task is the following: +Behavior of select_best_cpu() is further controlled by several tunables and +synchronous nature of wakeup. - 1. The lowest-power CPU, if it is not idle but is mostly idle. +a. /proc/sys/kernel/sched_cpu_high_irqload + A cpu whose irq load is greater than this threshold will not be + considered eligible for placement. This threshold value in expressed in + nanoseconds scale, with default threshold being 10000000 (10ms). See + notes on sched_cpu_high_irqload tunable to understand how irq load on a + cpu is measured. - 2. A non-idle CPU in the lowest power band which is mostly idle. The first - such CPU found is selected. +b. Synchronous nature of wakeup + Synchronous wakeup is a hint to scheduler that the task issuing wakeup + (i.e task currently running on cpu where wakeup is being processed by + scheduler) will "soon" relinquish CPU. A simple example is two tasks + communicating with each other using a pipe structure. When reader task + blocks waiting for data, its woken by writer task after it has written + data to pipe. Writer task usually blocks waiting for reader task to + consume data in pipe (which may not have any more room for writes). - 3. An idle CPU in the lowest power band that is in the least shallow C-state. + Synchronous wakeup is accounted for by adjusting load of a cpu to not + include load of currently running task. As a result, a cpu that has only + one runnable task and which is currently processing synchronous wakeup + will be considered idle. - 4. The least busy CPU in the lowest power band where adding the task will not - result in exceeding the spill threshold. +c. PF_WAKE_UP_IDLE + Any task with this flag set will be woken up to an idle cpu (if one is + available) independent of sched_prefer_idle flag setting, its demand and + synchronous nature of wakeup. Similarly idle cpu is preferred during + wakeup for any task that does not have this flag set but is being woken + by a task with PF_WAKE_UP_IDLE flag set. For simplicity, we will use the + term "PF_WAKE_UP_IDLE wakeup" to signify wakeups involving a task with + PF_WAKE_UP_IDLE flag set. - 5. The most power-efficient CPU outside of the lowest power band. +d. /proc/sys/kernel/sched_select_prev_cpu_us + This threshold controls whether task placement goes through fast path or + not. If task's wakeup time since last sleep is short there are high + chances that it's better to place the task on its previous CPU. This + reduces task placement latency, cache miss and number of migrations. + Default value of sched_select_prev_cpu_us is 2000 (2ms). This can be + turned off by setting it to 0. + +**** 5.2.4 Wakeup Logic for Task "p" + +Wakeup task placement logic is as follows: + +1) Eliminate CPUs with high irq load based on sched_cpu_high_irqload tunable. + +2) Eliminate CPUs where either the task does not fit or CPUs where placement +will result in exceeding the spill threshold tunables. CPUs elimiated at this +stage will be considered as backup choices incase none of the CPUs get past +this stage. + +3) Find out and return the least power CPU that satisfies all conditions above. + +4) If two or more CPUs are projected to have the same power, break ties in the +following preference order: + a) The CPU is the task's previous CPU. + b) The CPU is in the same cluster as the task's previous CPU. + c) The CPU has the least load + +The placement logic described above does not apply when PF_WAKE_UP_IDLE is set +for either the waker task or the wakee task. Instead the scheduler chooses the +most power efficient idle CPU. + +5) If no CPU is found after step 2, resort to backup CPU selection logic +whereby the CPU with highest amount of spare capacity is selected. + +6) If none of the CPUs have any spare capacity, return the task's previous +CPU. *** 5.3 Scheduler Tick @@ -739,8 +772,10 @@ to be migrated. Possible reasons for migrating task could be: a) A big task is running on a power-efficient cpu and a high-performance cpu is available (idle) to service it -b) The task is not a small task and a more power-efficient cpu is available to -service the task +b) A task is starving on a CPU with high irq load. + +c) A task with upmigration discouraged is running on a performance cluster. +See notes on 'cpu.upmigrate_discourage'. In case the test for migration turns out positive (which is expected to be rare event), a candidate cpu is identified for task migration. To avoid multiple task @@ -764,19 +799,11 @@ balance code serve these goals: 3. Allow idle high-performance cpu to pick up big tasks from power-efficient cpu -4. Allow a cpu with lower power rating to pick up load from another cpu with - higher power rating. Power rating of cpus is provided by an - architecture-specific driver, described in Sec 4 - -5. Allow small-task packing. Normally a cpu with more than one task would kick - an idle cpu in tickless state and have it pull task from it. That is - undesirable when, say, a cpu has couple of small tasks. - *** 5.5 Real Time Tasks Minimal changes introduced in treatment of real-time tasks by HMP scheduler -aims at preferring scheduling of real-time tasks on cpus with low -power-rating. +aims at preferring scheduling of real-time tasks on cpus with low load on +a power efficient cluster. Prior to HMP scheduler, the fast-path cpu selection for placing a real-time task (at wakeup) is its previous cpu, provided the currently running task on its @@ -789,67 +816,15 @@ that real-time tasks often execute for very short intervals and thus the focus is to place them on a cpu where they can be run immediately. HMP scheduler brings in a change which avoids fast-path and always resorts to -slow-path. Further cpu with lowest power-rating from candidate list of cpus is -chosen as cpu for placing waking real-time task. - -*** 5.6 Task packing - -Task packing is letting one cpu take up more than one task in an attempt to -improve power (and in some cases performance). Power benefit is derived by -avoiding wakeup cost for idle cpus from their deep sleep states. For example, -consider a system with one cpu busy while other cpus are idle and in deep -sleep state. A small task in this situation needs to be placed on a suitable -cpu. Placing the small task on the busy cpu will likely not hurt its -performance (it is after all a low-demand task) while helping gain on power -because we avoid the cost associated with waking idle cpu from deep sleep -state. - -Task packing can have good or bad implications for power and performance. - -a. Power implications - -As described in the small task wakeup example, task packing can be beneficial -for power. However, the adverse impact on power can arise when packing on one -cpu can increase its busy time and hence result in frequency raise. - -b. Performance implications - -The most obvious negative impact on performance because of packing is -increased scheduling latencies for tasks that can occur. Positive impact on -performance from packing has also been seen. This arises from the fact -that a waking task, when woken to busy cpu because of packing, will incur very -low latency to run immediately, when compared to being woken to a idle cpu in -deep sleep state. In later case, task has to wait for cpu to exit sleep state, -considerable enough in some cases to hurt performance. - -Packing thus is a delicate matter to play with! The following parameters control -packing behavior. - -- sched_small_task - This parameter specifies demand threshold below which a task will be -classified as "small". As described in Sec 5.2 ("Task Wakeup and -select_best_cpu()"), for small tasks wakeups, a busy cpu is prefered as target -rather than idle cpu. - -- mostly_idle_load and mostly_idle_nr_run - -These are per-cpu parameters that define mostly_idle thresholds for a cpu. A cpu -whose load < mostly_idle_load AND whose nr_running is < mostly_idle_nr_run is -classified as mostly_idle. See further description of "mostly_idle" thresholds -in Sec 5. - -- mostly_idle_freq - -This is a per-cpu parameter. If non-zero for a cpu which is part of a cluster -and cluster current frequency is less than this threshold, then scheduler will -pack all tasks on a single cpu in cluster. The cpu chosen is the first most -power-efficient cpu found while scanning cluster's online cpus. +slow-path. Further cpu with lowest load in a power efficient cluster from +candidate list of cpus is chosen as cpu for placing waking real-time task. - PF_WAKE_UP_IDLE - Any task that has this flag set in its 'task_struct.flags' field will be -always woken to idle cpu. Further any task woken by such tasks will be also -placed on idle cpu. PF_WAKE_UP_IDLE flag is inherited by children of a task. -It can be modified for a task in two ways: + +Idle cpu is preferred for any waking task that has this flag set in its +'task_struct.flags' field. Further idle cpu is preferred for any task woken by +such tasks. PF_WAKE_UP_IDLE flag of a task is inherited by it's children. It can +be modified for a task in two ways: > kernel-space interface set_wake_up_idle() needs to be called in the context of a task @@ -859,18 +834,6 @@ It can be modified for a task in two ways: /proc/[pid]/sched_wake_up_idle file needs to be written to for setting or clearing PF_WAKE_UP_IDLE flag for a given task -For some low band of frequency, spread of task on all available cpus can be -groslly power-inefficient. As an example, consider two tasks that each need -500MHz. Packing them on one cpu could lead to 1GHz. In spread case, we incur -cost of two cpus running at 500MHz, while in packed case, we incur the cost of -one cpu running at 1GHz. Based on the silicon characteristics, where leakage -power can be dominant factor, former can be worse on power rather than latter. -Running at slow frequency (in spread case) can actually makes it worse on -leakage power (especially if 500MHz and 1GHz share the same voltage point). -sched_mostly_idle_freq is set based on silicon characteristics and can provide -a winning argument for both power and performance. - - ===================== 6. FREQUENCY GUIDANCE ===================== @@ -895,7 +858,7 @@ get_cpu_iowait_time_us() APIs. This API is invoked by governor at initialization time or whenever window size is changed. 'window_size' argument (in jiffy units) indicates the size of window to be used. The first window of size - 'window_size' is set to beging at jiffy 'window_start' + 'window_size' is set to begin at jiffy 'window_start' -EINVAL is returned if per-entity load tracking is in use rather than window-based load tracking, otherwise a success value of 0 @@ -916,7 +879,7 @@ In addition to the per-task window-based demand, the HMP scheduler extensions also track the aggregate demand seen on each CPU. This is done using the same windows that the task demand is tracked with (which is in turn set by the governor when frequency guidance is in -use). There are two quantities maintained for each CPU by the HMP scheduler: +use). There are four quantities maintained for each CPU by the HMP scheduler: curr_runnable_sum: aggregate demand from all tasks which executed during the current (not yet completed) window @@ -924,6 +887,12 @@ use). There are two quantities maintained for each CPU by the HMP scheduler: prev_runnable_sum: aggregate demand from all tasks which executed during the most recent completed window + nt_curr_runnable_sum: aggregate demand from all 'new' tasks which executed + during the current (not yet completed) window + + nt_prev_runnable_sum: aggregate demand from all 'new' tasks which executed + during the most recent completed window. + When the scheduler is updating a task's window-based stats it also updates these values. Like per-task window-based demand these quantities are normalized against the max possible frequency and max @@ -931,7 +900,11 @@ efficiency (instructions per cycle) in the system. If an update occurs and a window rollover is observed, curr_runnable_sum is copied into prev_runnable_sum before being reset to 0. The sched_get_busy() API returns prev_runnable_sum, scaled to the efficiency and fmax of given -CPU. +CPU. The same applies to nt_curr_runnable_sum and nt_prev_runnable_sum. + +A 'new' task is defined as a task whose number of active windows since fork is +less than sysctl_sched_new_task_windows. An active window is defined as a window +where a task was observed to be runnable. *** 6.2 Per-task window-based stats @@ -943,8 +916,11 @@ curr_window - represents cpu demand of task in its most recently tracked prev_window - represents cpu demand of task in the window prior to the one being tracked by curr_window +The above counters are resued for nt_curr_runnable_sum and +nt_prev_runnable_sum. + "cpu demand" of a task includes its execution time and can also include its -wait time. 'sched_freq_account_wait_time' tunable controls whether task's wait +wait time. 'SCHED_FREQ_ACCOUNT_WAIT_TIME' controls whether task's wait time is included in its 'curr_window' and 'prev_window' counters or not. Needless to say, curr_runnable_sum counter of a cpu is derived from curr_window @@ -958,7 +934,7 @@ PICK_NEXT_TASK This represents beginning of execution for a task. Provided the task refers to a non-idle task, a portion of task's wait time that corresponds to the current window being tracked on a cpu is added to - task's curr_window counter, provided sched_freq_account_wait_time is + task's curr_window counter, provided SCHED_FREQ_ACCOUNT_WAIT_TIME is set. The same quantum is also added to cpu's curr_runnable_sum counter. The remaining portion, which corresponds to task's wait time in previous window is added to task's prev_window and cpu's prev_runnable_sum @@ -993,13 +969,12 @@ TASK_MIGRATE this event reflects actions taken under PICK_NEXT_TASK (i.e its wait time is added to task's curr/prev_window counters as well as src_cpu's curr/prev_runnable_sum counters, provided - sched_freq_account_wait_time tunable is non-zero). After that update, + SCHED_FREQ_ACCOUNT_WAIT_TIME is non-zero). After that update, src_cpu's curr_runnable_sum is reduced by task's curr_window value and dst_cpu's curr_runnable_sum is increased by task's curr_window - value, provided sched_migration_fixup = 1. Similarly, src_cpu's - prev_runnable_sum is reduced by task's prev_window value and dst_cpu's - prev_runnable_sum is increased by task's prev_window value, - provided sched_migration_fixup = 1 + value. Similarly, src_cpu's prev_runnable_sum is reduced by task's + prev_window value and dst_cpu's prev_runnable_sum is increased by + task's prev_window value. IRQ_UPDATE This event signifies end of execution of an interrupt handler. This @@ -1016,32 +991,7 @@ IRQ_UPDATE 7. TUNABLES =========== -*** 7.1 sched_mostly_idle_nr_run - -Appears at: /sys/devices/system/cpu/cpuX/sched_mostly_idle_nr_run - -Default value: 3 - -If a CPU has this many runnable tasks (or less), it is considered -"mostly idle." A mostly idle CPU is a preferred destination for a -waking task. To be mostly idle a CPU must not have -more than sched_mostly_idle_nr_run runnable tasks and must not be more -than sched_mostly_idle_load percent busy. - -*** 7.2 sched_mostly_idle_load - -Appears at: /sys/devices/system/cpu/cpuX/sched_mostly_idle_load - -Default value: 20 - -This tunable is a percentage. If a CPU is busier than this, it cannot -be considered "mostly idle." A mostly idle CPU is a preferred -destination for a waking task. To be mostly idle a CPU must not have -more than sched_mostly_idle_nr_run runnable tasks and must not be more -than sched_mostly_idle_load percent busy. - - -*** 7.3 sched_spill_load +*** 7.1 sched_spill_load Appears at: /proc/sys/kernel/sched_spill_load @@ -1051,20 +1001,20 @@ CPU selection criteria for fair-sched class tasks is the lowest power cpu where they can fit. When the most power-efficient cpu where a task can fit is overloaded (aggregate demand of tasks currently queued on it exceeds sched_spill_load), a task can be placed on a higher-performance cpu, even though -the task strictly doesn't need one. This applies to non-small tasks. +the task strictly doesn't need one. -*** 7.4 sched_spill_nr_run +*** 7.2 sched_spill_nr_run Appears at: /proc/sys/kernel/sched_spill_nr_run Default value: 10 The intent of this tunable is similar to sched_spill_load, except it applies to -nr_running count of a cpu. A non-small task can spill over to a -higher-performance cpu when the most power-efficient cpu where it can normally -fit has more tasks than sched_spill_nr_run. +nr_running count of a cpu. A task can spill over to a higher-performance cpu +when the most power-efficient cpu where it can normally fit has more tasks than +sched_spill_nr_run. -*** 7.5 sched_upmigrate +*** 7.3 sched_upmigrate Appears at: /proc/sys/kernel/sched_upmigrate @@ -1074,30 +1024,7 @@ This tunable is a percentage. If a task consumes more than this much of a CPU, the CPU is considered too small for the task and the scheduler will try to find a bigger CPU to place the task on. -*** 7.6 sched_downmigrate - -Appears at: /proc/sys/kernel/sched_downmigrate - -Default value: 60 - -This tunable is a percentage. It exists to control hysteresis. Lets say a task -migrated to a high-performance cpu when it crossed 80% demand on a -power-efficient cpu. We don't let it come back to a power-efficient cpu until -its demand *in reference to the power-efficient cpu* drops less than 60% -(sched_down_migrate). - -*** 7.7 sched_small_task - -Appears at: /proc/sys/kernel/sched_small_task - -Default value: 10 - -This tunable is a percentage. If a task consumes this much or less of -the minimum capacity CPU in the system, it is considered a "small -task." The scheduler will not attempt to find an idle CPU for small -tasks - they may be woken up on busy CPUs. - -*** 7.8 sched_init_task_load +*** 7.4 sched_init_task_load Appears at: /proc/sys/kernel/sched_init_task_load @@ -1109,33 +1036,7 @@ historical load value to assign to it. This tunable specifies the initial load value for newly created tasks. Also see Sec 2.8 on per-task 'initial task load' attribute. -*** 7.9 sched_upmigrate_min_nice - -Appears at: /proc/sys/kernel/sched_upmigrate_min_nice - -Default value: 15 - -A task whose nice value is greater than this tunable value will never -be considered as a "big" task (it will not be allowed to run on a -high-performance CPU). - -See also notes on 'cpu.upmigrate_discourage' tunable. - -*** 7.10 sched_enable_power_aware - -Appears at: /proc/sys/kernel/sched_enable_power_aware - -Default value: 0 - -Controls whether or not per-CPU power values are used in determining -task placement. If this is disabled, tasks are simply placed on the -smallest capacity CPU that will adequately meet the task's needs as -determined by the task load tracking mechanism. If this is enabled, -after a set of CPUs are determined which will meet the task's -performance needs, a CPU is selected which is reported to have the -lowest power consumption at that time. - -*** 7.11 sched_ravg_hist_size +*** 7.5 sched_ravg_hist_size Appears at: /proc/sys/kernel/sched_ravg_hist_size @@ -1144,7 +1045,7 @@ Default value: 5 This tunable controls the number of samples used from task's sum_history[] array for determination of its demand. -*** 7.12 sched_window_stats_policy +*** 7.6 sched_window_stats_policy Appears at: /proc/sys/kernel/sched_window_stats_policy @@ -1160,10 +1061,10 @@ Possible values for this tunable are: 1: Use the maximum value of first M samples found in task's cpu demand history (sum_history[] array), where M = sysctl_sched_ravg_hist_size 2: Use the maximum of (the most recent window sample, average of first M - samples), where M = syctl_sched_ravg_hist_size + samples), where M = sysctl_sched_ravg_hist_size 3. Use average of first M samples, where M = sysctl_sched_ravg_hist_size -*** 7.13 sched_ravg_window +*** 7.7 sched_ravg_window Appears at: kernel command line argument @@ -1174,7 +1075,7 @@ tracking. By default each window is 10ms long. This quantity must currently be set at boot time on the kernel command line (or the default value of 10ms can be used). -*** 7.14 RAVG_HIST_SIZE +*** 7.8 RAVG_HIST_SIZE Appears at: compile time only (see RAVG_HIST_SIZE in include/linux/sched.h) @@ -1185,35 +1086,7 @@ tracking mechanism maintains per task. If default values are used for both this and sched_ravg_window then a total of 50ms of task history would be maintained in 5 10ms windows. -*** 7.15 sched_account_wait_time - -Appears at: /proc/sys/kernel/sched_account_wait_time - -Default value: 1 - -This controls whether a task's wait time is accounted as its demand for cpu -and thus the values found in its sum, sum_history[] and demand attributes. - -*** 7.16 sched_freq_account_wait_time - -Appears at: /proc/sys/kernel/sched_freq_account_wait_time - -Default value: 0 - -This controls whether a task's wait time is accounted in its curr_window and -prev_window attributes and thus in a cpu's curr_runnable_sum and -prev_runnable_sum counters. - -*** 7.17 sched_migration_fixup - -Appears at: /proc/sys/kernel/sched_migration_fixup - -Default value: 1 - -This controls whether a cpu's busy time counters are adjusted during task -migration. - -*** 7.18 sched_freq_inc_notify +*** 7.9 sched_freq_inc_notify Appears at: /proc/sys/kernel/sched_freq_inc_notify @@ -1225,7 +1098,7 @@ exceeds sched_freq_inc_notify, where freq_required is the frequency calculated by scheduler to meet current task demand. Note that sched_freq_inc_notify is specified in kHz units. -*** 7.19 sched_freq_dec_notify +*** 7.10 sched_freq_dec_notify Appears at: /proc/sys/kernel/sched_freq_dec_notify @@ -1238,30 +1111,7 @@ exceeds sched_freq_dec_notify, where freq_required is the frequency calculated by scheduler to meet current task demand. Note that sched_freq_dec_notify is specified in kHz units. -** 7.20 sched_heavy_task - -Appears at: /proc/sys/kernel/sched_heavy_task - -Default value: 0 - -This tunable can be used to specify a demand value for tasks above which task -are classified as "heavy" tasks. Task's ravg.demand attribute is used for this -comparison. Scheduler will request a raise in cpu frequency when heavy tasks -wakeup after at least one window of sleep, where window size is defined by -sched_ravg_window. Value 0 will disable this feature. - -** 7.21 sched_mostly_idle_freq - -Appears at: /sys/devices/system/cpu/cpuX/sched_mostly_idle_freq - -Default value: 0 - -This tunable is intended to achieve task packing behavior based on cluster -frequency. Hence it is strongly advised to have all cpus in a cluster have the -same value for mostly_idle_freq. For more details, see section on "Task -packing" (sec 5.6). - -*** 7.22 sched_cpu_high_irqload +*** 7.11 sched_cpu_high_irqload Appears at: /proc/sys/kernel/sched_cpu_high_irqload @@ -1269,47 +1119,17 @@ Default value: 10000000 (10ms) The scheduler keeps a decaying average of the amount of irq and softirq activity seen on each CPU within a ten millisecond window. Note that this "irqload" -(reported in the sched_cpu_load tracepoint) will be higher than the typical load +(reported in the sched_cpu_load_* tracepoint) will be higher than the typical load in a single window since every time the window rolls over, the value is decayed by some fraction and then added to the irq/softirq time spent in the next window. When the irqload on a CPU exceeds the value of this tunable, the CPU is no -longer eligible to be seen as mostly idle. This will affect the task placement -logic described above, causing the scheduler to try and steer tasks away from +longer eligible for placement. This will affect the task placement logic +described above, causing the scheduler to try and steer tasks away from the CPU. -** 7.23 sched_prefer_idle - -Appears at: /sys/devices/system/cpu/cpuX/sched_prefer_idle - -Default value: 1 - -Non-small tasks will prefer to wake up on idle CPUs if this tunable is set to 1. -If the tunable is set to 0, non-small tasks will prefer to wake up on mostly -idle CPUs which are not completely idle, increasing task packing behavior. - -** 7.24 sched_min_runtime - -Appears at: /proc/sys/kernel/sched_min_runtime - -Default value: 0 (0 ms) - -This tunable helps avouid frequent migration of task on account of -energy-awareness. During scheduler tick, a check is made (in migration_needed()) -whether the running task needs to be migrated to a "better" cpu, which could -either offer better performance or power. When deciding to migrate task on -account of power, we want to avoid "frequent" migration of task (say every -tick), which could be add more overhead for comparatively little gains. A task's -'run_start' attribute is set when it starts running on a cpu. This information -is used in migration_needed() to avoid "frequent" migrations. Once a task has -been associated with a cpu (in either running or runnable state) for more than -'sched_min_vruntime' ns, it is considered eligible for migration in tick path on -account of energy awareness reasons. -The same logic also applies to the load balancer path to avoid frequent -migrations due to energy awareness. - -** 7.25 cpu.upmigrate_discourage +*** 7.12 cpu.upmigrate_discourage Default value : 0 @@ -1322,8 +1142,87 @@ Setting this flag to 1 discourages upmigration for all tasks of a cgroup. High demand tasks of such a cgroup will never be classified as big tasks and hence not upmigrated. Any task of the cgroup is allowed to upmigrate only under overcommitted scenario. See notes on sched_spill_nr_run and sched_spill_load for -how overcommitment threshold is defined and also notes on -'sched_upmigrate_min_nice' tunable. +how overcommitment threshold is defined. + +*** 7.13 sched_static_cpu_pwr_cost + +Default value: 0 + +Appears at /sys/devices/system/cpu/cpu/sched_static_cpu_pwr_cost + +This is the power cost associated with bringing an idle CPU out of low power +mode. It ignores the actual C-state that a CPU may be in and assumes the +worst case power cost of the highest C-state. It is means of biasing task +placement away from idle CPUs when necessary. It can be defined per CPU, +however, a more appropriate usage to define the same value for every CPU +within a cluster and possibly have differing value between clusters as +needed. + + +*** 7.14 sched_static_cluster_pwr_cost + +Default value: 0 + +Appears at /sys/devices/system/cpu/cpu/sched_static_cluster_pwr_cost + +This is the power cost associated with bringing an idle cluster out of low +power mode. It ignores the actual D-state that a cluster may be in and assumes +the worst case power cost of the highest D-state. It is means of biasing task +placement away from idle clusters when necessary. + +*** 7.15 sched_restrict_cluster_spill + +Default value: 0 + +Appears at /proc/sys/kernel/sched_restrict_cluster_spill + +This tunable can be used to restrict tasks spilling to the higher capacity +(higher power) cluster. When this tunable is enabled, + +- Restrict the higher capacity cluster pulling tasks from the lower capacity +cluster in the load balance path. The restriction is lifted if all of the CPUS +in the lower capacity cluster are above spill. The power cost is used to break +the ties if the capacity of clusters are same for applying this restriction. + +- The current CPU selection algorithm for RT tasks looks for the least loaded +CPU across all clusters. When this tunable is enabled, the RT tasks are +restricted to the lowest possible power cluster. + + +*** 7.16 sched_downmigrate + +Appears at: /proc/sys/kernel/sched_downmigrate + +Default value: 60 + +This tunable is a percentage. It exists to control hysteresis. Lets say a task +migrated to a high-performance cpu when it crossed 80% demand on a +power-efficient cpu. We don't let it come back to a power-efficient cpu until +its demand *in reference to the power-efficient cpu* drops less than 60% +(sched_downmigrate). + + +*** 7.17 sched_small_wakee_task_load + +Appears at: /proc/sys/kernel/sched_small_wakee_task_load + +Default value: 10 + +This tunable is a percentage. Configure the maximum demand of small wakee task. +Sync wakee tasks which have demand less than sched_small_wakee_task_load are +categorized as small wakee tasks. Scheduler places small wakee tasks on the +waker's cluster. + + +*** 7.18 sched_big_waker_task_load + +Appears at: /proc/sys/kernel/sched_big_waker_task_load + +Default value: 25 + +This tunable is a percentage. Configure the minimum demand of big sync waker +task. Scheduler places small wakee tasks woken up by big sync waker on the +waker's cluster. ========================= 8. HMP SCHEDULER TRACE POINTS @@ -1333,7 +1232,7 @@ how overcommitment threshold is defined and also notes on Logged when a task is either enqueued or dequeued on a CPU's run queue. - -0 [004] d.h4 12700.711665: sched_enq_deq_task: cpu=4 enqueue comm=powertop pid=13227 prio=120 nr_running=1 cpu_load=0 rt_nr_running=0 affine=ff sum_scaled=0 period=48237 demand=13364423 + -0 [004] d.h4 12700.711665: sched_enq_deq_task: cpu=4 enqueue comm=powertop pid=13227 prio=120 nr_running=1 cpu_load=0 rt_nr_running=0 affine=ff demand=13364423 - cpu: the CPU that the task is being enqueued on to or dequeued off of - enqueue/dequeue: whether this was an enqueue or dequeue event @@ -1346,9 +1245,6 @@ Logged when a task is either enqueued or dequeued on a CPU's run queue. - rt_nr_running: number of real-time processes running on this CPU - affine: CPU affinity mask in hex for this task (so ff is a task eligible to run on CPUs 0-7) -- sum_scaled: PELT-based task demand scaled by cpu frequency and efficiency (ns) -- period: PELT-based decaying average of the period (1024us, ~1ms) that the - "sum_scaled" is relative to - demand: window-based task demand computed based on selected policy (recent, max, or average) (ns) @@ -1356,36 +1252,33 @@ Logged when a task is either enqueued or dequeued on a CPU's run queue. Logged when selecting the best CPU to run the task (select_best_cpu()). -<...>-2907 [002] d.s3 66.841363: sched_task_load: 32 (kworker/u16:1): sum=319, sum_scaled=69, period=47541 demand=192442 small=1 boost=0 reason=0 +sched_task_load: 4004 (adbd): demand=698425 boost=0 reason=0 sync=0 need_idle=0 best_cpu=0 latency=103177 -- sum: PELT-based task demand (not normalized for CPU frequency) (ns) -- sum_scaled: PELT-based task demand scaled by cpu frequency and efficiency (ns) -- period: PELT-based decaying average of the period (1024us, ~1ms) that the - "sum_scaled" is relative to - demand: window-based task demand computed based on selected policy (recent, max, or average) (ns) -- small: whether the task is considered small - boost: whether boost is in effect - reason: reason we are picking a new CPU: 0: no migration - selecting a CPU for a wakeup or new task wakeup 1: move to big CPU (migration) - 2: move to littlte CPU (migration) - 3: move to power efficient CPU (migration) + 2: move to little CPU (migration) + 3: move to low irq load CPU (migration) +- sync: is the nature synchronous in nature +- need_idle: is an idle CPU required for this task based on PF_WAKE_UP_IDLE +- best_cpu: The CPU selected by the select_best_cpu() function for placement +- latency: The execution time of the function select_best_cpu() -*** 8.3 sched_cpu_load +*** 8.3 sched_cpu_load_* Logged when selecting the best CPU to run a task (select_best_cpu() for fair class tasks, find_lowest_rq_hmp() for RT tasks) and load balancing (update_sg_lb_stats()). --0 [004] d.h3 12700.711541: sched_cpu_load: cpu 0 idle 1 mostly_idle 1 nr_run 0 nr_big 0 nr_small 0 lsf 1945 capacity 1045 cr_avg 0 irqload 4456 fcur 199200 fmax 940800 power_cost 1045 cstate 1 temp 73 +-0 [004] d.h3 12700.711541: sched_cpu_load_*: cpu 0 idle 1 nr_run 0 nr_big 0 lsf 1119 capacity 1024 cr_avg 0 irqload 3301121 fcur 729600 fmax 1459200 power_cost 5 cstate 2 temp 38 - cpu: the CPU being described - idle: boolean indicating whether the CPU is idle -- mostly_idle: boolean indicating whether the CPU is mostly idle - nr_run: number of tasks running on CPU - nr_big: number of BIG tasks running on CPU -- nr_small: number of small tasks running on CPU - lsf: load scale factor - multiply normalized load by this factor to determine how much load task will exert on CPU - capacity: capacity of CPU (based on max possible frequency and efficiency) @@ -1452,7 +1345,7 @@ update_history() from update_task_ravg(). The first call would record activity in completed window 1 and second call would record activity for windows 2 and 3 together (samples will be 2 in second call). --0 [004] d.h4 12700.711489: sched_update_history: 13227 (powertop): runtime 13364423 samples 1 event TASK_WAKE demand 13364423 (hist: 13364423 9871252 2236009 6162476 10282078) cpu 4 nr_big 0 nr_small 0 +-0 [004] d.h4 12700.711489: sched_update_history: 13227 (powertop): runtime 13364423 samples 1 event TASK_WAKE demand 13364423 (hist: 13364423 9871252 2236009 6162476 10282078) cpu 4 nr_big 0 - runtime: task cpu demand in recently completed window(s). This value is scaled to max_possible_freq and max_possible_efficiency. This value is pushed into @@ -1469,7 +1362,6 @@ together (samples will be 2 in second call). listed first - cpu: CPU the task is associated with - nr_big: number of big tasks on the CPU -- nr_small: Number of small tasks on the CPU *** 8.6 sched_reset_all_windows_stats @@ -1479,10 +1371,7 @@ cpus are being reset. Changes to below attributes result in such a reset: * sched_ravg_window (See Sec 2) * sched_window_stats_policy (See Sec 2.4) -* sched_account_wait_time (See Sec 7.15) * sched_ravg_hist_size (See Sec 7.11) -* sched_migration_fixup (See Sec 7.17) -* sched_freq_account_wait_time (See Sec 7.16) -0 [004] d.h4 12700.711489: sched_reset_all_windows_stats: time_taken 1123 window_start 0 window_size 0 reason POLICY_CHANGE old_val 0 new_val 1 @@ -1498,24 +1387,33 @@ cpus are being reset. Changes to below attributes result in such a reset: Logged when CONFIG_SCHED_FREQ_INPUT feature is enabled and a task is migrating to another cpu. --0 [004] d.h4 12700.711489: sched_migration_update_sum: cpu 0: cs XXX ps YYY pid 1234 +-0 [000] d..8 5020.404137: sched_migration_update_sum: cpu 0: cs 471278 ps 902463 nt_cs 0 nt_ps 0 pid 2645 - cpu: cpu, away from which or to which, task is migrating - cs: curr_runnable_sum of cpu (ns). See Sec 6.1 for more details of this counter. - ps: prev_runnable_sum of cpu (ns). See Sec 6.1 for more details of this counter. +- nt_cs: nt_curr_runnable_sum of cpu (ns). See Sec 6.1 for more details of + this counter. +- nt_ps: nt_prev_runnable_sum of cpu (ns). See Sec 6.1 for more details of + this counter - pid: PID of migrating task *** 8.8 sched_get_busy Logged when scheduler is returning busy time statistics for a cpu. --0 [004] d.h4 12700.711489: sched_get_busy: cpu 0 load XXX +<...>-4331 [003] d.s3 313.700108: sched_get_busy: cpu 3 load 19076 new_task_load 0 early 0 + - cpu: cpu, for which busy time statistic (prev_runnable_sum) is being returned (ns) - load: corresponds to prev_runnable_sum (ns), scaled to fmax of cpu +- new_task_load: corresponds to nt_prev_runnable_sum to fmax of cpu +- early: A flag indicating whether the scheduler is passing regular load or early detection load + 0 - regular load + 1 - early detection load *** 8.9 sched_freq_alert diff --git a/Documentation/scheduler/sched-zone.txt b/Documentation/scheduler/sched-zone.txt deleted file mode 100644 index 5a84f0524481..000000000000 --- a/Documentation/scheduler/sched-zone.txt +++ /dev/null @@ -1,1437 +0,0 @@ -CONTENTS - -1. Introduction - 1.1 Heterogeneous Systems - 1.2 CPU Frequency Guidance -2. Window-Based Load Tracking Scheme - 2.1 Synchronized Windows - 2.2 struct ravg - 2.3 Scaling Load Statistics - 2.4 sched_window_stats_policy - 2.5 Task Events - 2.6 update_task_ravg() - 2.7 update_history() - 2.8 Per-task 'initial task load' -3. CPU Capacity - 3.1 Load scale factor - 3.2 CPU Power -4. CPU Power -5. HMP Scheduler - 5.1 Classification of Tasks and CPUs - 5.2 select_best_cpu() - 5.2.1 sched_boost - 5.2.2 task_will_fit() - 5.2.3 Tunables affecting select_best_cpu() - 5.2.4 Wakeup Logic - 5.3 Scheduler Tick - 5.4 Load Balancer - 5.5 Real Time Tasks - 5.6 Task packing -6. Frequency Guidance - 6.1 Per-CPU Window-Based Stats - 6.2 Per-task Window-Based Stats - 6.3 Effect of various task events -7. Tunables -8. HMP Scheduler Trace Points - 8.1 sched_enq_deq_task - 8.2 sched_task_load - 8.3 sched_cpu_load_* - 8.4 sched_update_task_ravg - 8.5 sched_update_history - 8.6 sched_reset_all_windows_stats - 8.7 sched_migration_update_sum - 8.8 sched_get_busy - 8.9 sched_freq_alert - 8.10 sched_set_boost - -=============== -1. INTRODUCTION -=============== - -Scheduler extensions described in this document serves two goals: - -1) handle heterogeneous multi-processor (HMP) systems -2) guide cpufreq governor on proactive changes to cpu frequency - -*** 1.1 Heterogeneous systems - -Heterogeneous systems have cpus that differ with regard to their performance and -power characteristics. Some cpus could offer peak performance better than -others, although at cost of consuming more power. We shall refer such cpus as -"high performance" or "performance efficient" cpus. Other cpus that offer lesser -peak performance are referred to as "power efficient". - -In this situation the scheduler is tasked with the responsibility of assigning -tasks to run on the right cpus where their performance requirements can be met -at the least expense of power. - -Achieving that goal is made complicated by the fact that the scheduler has -little clue about performance requirements of tasks and how they may change by -running on power or performance efficient cpus! One simplifying assumption here -could be that a task's desire for more performance is expressed by its cpu -utilization. A task demanding high cpu utilization on a power-efficient cpu -would likely improve in its performance by running on a performance-efficient -cpu. This idea forms the basis for HMP-related scheduler extensions. - -Key inputs required by the HMP scheduler for its task placement decisions are: - -a) task load - this reflects cpu utilization or demand of tasks -b) CPU capacity - this reflects peak performance offered by cpus -c) CPU power - this reflects power or energy cost of cpus - -Once all 3 pieces of information are available, the HMP scheduler can place -tasks on the lowest power cpus where their demand can be satisfied. - -*** 1.2 CPU Frequency guidance - -A somewhat separate but related goal of the scheduler extensions described here -is to provide guidance to the cpufreq governor on the need to change cpu -frequency. Most governors that control cpu frequency work on a reactive basis. -CPU utilization is sampled at regular intervals, based on which the need to -change frequency is determined. Higher utilization leads to a frequency increase -and vice-versa. There are several problems with this approach that scheduler -can help resolve. - -a) latency - - Reactive nature introduces latency for cpus to ramp up to desired speed - which can hurt application performance. This is inevitable as cpufreq - governors can only track cpu utilization as a whole and not tasks which - are driving that demand. Scheduler can however keep track of individual - task demand and can alert the governor on changing task activity. For - example, request raise in frequency when tasks activity is increasing on - a cpu because of wakeup or migration or request frequency to be lowered - when task activity is decreasing because of sleep/exit or migration. - -b) part-picture - - Most governors track utilization of each CPU independently. When a task - migrates from one cpu to another the task's execution time is split - across the two cpus. The governor can fail to see the full picture of - task demand in this case and thus the need for increasing frequency, - affecting the task's performance. Scheduler can keep track of task - migrations, fix up busy time upon migration and report per-cpu busy time - to the governor that reflects task demand accurately. - -The rest of this document explains key enhancements made to the scheduler to -accomplish both of the aforementioned goals. - -==================================== -2. WINDOW-BASED LOAD TRACKING SCHEME -==================================== - -As mentioned in the introduction section, knowledge of the CPU demand exerted by -a task is a prerequisite to knowing where to best place the task in an HMP -system. The per-entity load tracking (PELT) scheme, present in Linux kernel -since v3.7, has some perceived shortcomings when used to place tasks on HMP -systems or provide recommendations on CPU frequency. - -Per-entity load tracking does not make a distinction between the ramp up -vs ramp down time of task load. It also decays task load without exception when -a task sleeps. As an example, a cpu bound task at its peak load (LOAD_AVG_MAX or -47742) can see its load decay to 0 after a sleep of just 213ms! A cpu-bound task -running on a performance-efficient cpu could thus get re-classified as not -requiring such a cpu after a short sleep. In the case of mobile workloads, tasks -could go to sleep due to a lack of user input. When they wakeup it is very -likely their cpu utilization pattern repeats. Resetting their load across sleep -and incurring latency to reclassify them as requiring a high performance cpu can -hurt application performance. - -The window-based load tracking scheme described in this document avoids these -drawbacks. It keeps track of N windows of execution for every task. Windows -where a task had no activity are ignored and not recorded. N can be tuned at -compile time (RAVG_HIST_SIZE defined in include/linux/sched.h) or at runtime -(/proc/sys/kernel/sched_ravg_hist_size). The window size, W, is common for all -tasks and currently defaults to 10ms ('sched_ravg_window' defined in -kernel/sched/core.c). The window size can be tuned at boot time via the -sched_ravg_window=W argument to kernel. Alternately it can be tuned after boot -via tunables provided by the interactive governor. More on this later. - -Based on the N samples available per-task, a per-task "demand" attribute is -calculated which represents the cpu demand of that task. The demand attribute is -used to classify tasks as to whether or not they need a performance-efficient -CPU and also serves to provide inputs on frequency to the cpufreq governor. More -on this later. The 'sched_window_stats_policy' tunable (defined in -kernel/sched/core.c) controls how the demand field for a task is derived from -its N past samples. - -*** 2.1 Synchronized windows - -Windows of observation for task activity are synchronized across cpus. This -greatly aids in the scheduler's frequency guidance feature. Scheduler currently -relies on a synchronized clock (sched_clock()) for this feature to work. It may -be possible to extend this feature to work on systems having an unsynchronized -sched_clock(). - -struct rq { - - .. - - u64 window_start; - - .. -}; - -The 'window_start' attribute represents the time when current window began on a -cpu. It is updated when key task events such as wakeup or context-switch call -update_task_ravg() to record task activity. The window_start value is expected -to be the same for all cpus, although it could be behind on some cpus where it -has not yet been updated because update_task_ravg() has not been recently -called. For example, when a cpu is idle for a long time its window_start could -be stale. The window_start value for such cpus is rolled forward upon -occurrence of a task event resulting in a call to update_task_ravg(). - -*** 2.2 struct ravg - -The ravg struct contains information tracked per-task. - -struct ravg { - u64 mark_start; - u32 sum, demand; - u32 sum_history[RAVG_HIST_SIZE]; -#ifdef CONFIG_SCHED_FREQ_INPUT - u32 curr_window, prev_window; -#endif -}; - -struct task_struct { - - .. - - struct ravg ravg; - - .. -}; - -sum_history[] - stores cpu utilization samples from N previous windows - where task had activity - -sum - stores cpu utilization of the task in its most recently - tracked window. Once the corresponding window terminates, - 'sum' will be pushed into the sum_history[] array and is then - reset to 0. It is possible that the window corresponding to - sum is not the current window being tracked on a cpu. For - example, a task could go to sleep in window X and wakeup in - window Y (Y > X). In this case, sum would correspond to the - task's activity seen in window X. When update_task_ravg() is - called during the task's wakeup event it will be seen that - window X has elapsed. The sum value will be pushed to - 'sum_history[]' array before being reset to 0. - -demand - represents task's cpu demand and is derived from the - elements in sum_history[]. The section on - 'sched_window_stats_policy' provides more details on how - 'demand' is derived from elements in sum_history[] array - -mark_start - records timestamp of the beginning of the most recent task - event. See section on 'Task events' for possible events that - update 'mark_start' - -curr_window - this is described in the section on 'Frequency guidance' - -prev_window - this is described in the section on 'Frequency guidance' - - -*** 2.3 Scaling load statistics - -Time required for a task to complete its work (and hence its load) depends on, -among various other factors, cpu frequency and its efficiency. In a HMP system, -some cpus are more performance efficient than others. Performance efficiency of -a cpu can be described by its "instructions-per-cycle" (IPC) attribute. History -of task execution could involve task having run at different frequencies and on -cpus with different IPC attributes. To avoid ambiguity of how task load relates -to the frequency and IPC of cpus on which a task has run, task load is captured -in a scaled form, with scaling being done in reference to an "ideal" cpu that -has best possible IPC and frequency. Such an "ideal" cpu, having the best -possible frequency and IPC, may or may not exist in system. - -As an example, consider a HMP system, with two types of cpus, A53 and A57. A53 -has IPC count of 1024 and can run at maximum frequency of 1 GHz, while A57 has -IPC count of 2048 and can run at maximum frequency of 2 GHz. Ideal cpu in this -case is A57 running at 2 GHz. - -A unit of work that takes 100ms to finish on A53 running at 100MHz would get -done in 10ms on A53 running at 1GHz, in 5 ms running on A57 at 1 GHz and 2.5ms -on A57 running at 2 GHz. Thus a load of 100ms can be expressed as 2.5ms in -reference to ideal cpu of A57 running at 2 GHz. - -In order to understand how much load a task will consume on a given cpu, its -scaled load needs to be multiplied by a factor (load scale factor). In above -example, scaled load of 2.5ms needs to be multiplied by a factor of 4 in order -to estimate the load of task on A53 running at 1 GHz. - -/proc/sched_debug provides IPC attribute and load scale factor for every cpu. - -In summary, task load information stored in a task's sum_history[] array is -scaled for both frequency and efficiency. If a task runs for X ms, then the -value stored in its 'sum' field is derived as: - - X_s = X * (f_cur / max_possible_freq) * - (efficiency / max_possible_efficiency) - -where: - -X = cpu utilization that needs to be accounted -X_s = Scaled derivative of X -f_cur = current frequency of the cpu where the task was - running -max_possible_freq = maximum possible frequency (across all cpus) -efficiency = instructions per cycle (IPC) of cpu where task was - running -max_possible_efficiency = maximum IPC offered by any cpu in system - - -*** 2.4 sched_window_stats_policy - -sched_window_stats_policy controls how the 'demand' attribute for a task is -derived from elements in its 'sum_history[]' array. - -WINDOW_STATS_RECENT (0) - demand = recent - -WINDOW_STATS_MAX (1) - demand = max - -WINDOW_STATS_MAX_RECENT_AVG (2) - demand = maximum(average, recent) - -WINDOW_STATS_AVG (3) - demand = average - -where: - M = history size specified by - /proc/sys/kernel/sched_ravg_hist_size - average = average of first M samples found in the sum_history[] array - max = maximum value of first M samples found in the sum_history[] - array - recent = most recent sample (sum_history[0]) - demand = demand attribute found in 'struct ravg' - -This policy can be changed at runtime via -/proc/sys/kernel/sched_window_stats_policy. For example, the command -below would select WINDOW_STATS_USE_MAX policy - -echo 1 > /proc/sys/kernel/sched_window_stats_policy - -*** 2.5 Task events - -A number of events results in the window-based stats of a task being -updated. These are: - -PICK_NEXT_TASK - the task is about to start running on a cpu -PUT_PREV_TASK - the task stopped running on a cpu -TASK_WAKE - the task is waking from sleep -TASK_MIGRATE - the task is migrating from one cpu to another -TASK_UPDATE - this event is invoked on a currently running task to - update the task's window-stats and also the cpu's - window-stats such as 'window_start' -IRQ_UPDATE - event to record the busy time spent by an idle cpu - processing interrupts - -*** 2.6 update_task_ravg() - -update_task_ravg() is called to mark the beginning of an event for a task or a -cpu. It serves to accomplish these functions: - -a. Update a cpu's window_start value -b. Update a task's window-stats (sum, sum_history[], demand and mark_start) - -In addition update_task_ravg() updates the busy time information for the given -cpu, which is used for frequency guidance. This is described further in section -6. - -*** 2.7 update_history() - -update_history() is called on a task to record its activity in an elapsed -window. 'sum', which represents task's cpu demand in its elapsed window is -pushed onto sum_history[] array and its 'demand' attribute is updated based on -the sched_window_stats_policy in effect. - -*** 2.8 Initial task load attribute for a task (init_load_pct) - -In some cases, it may be desirable for children of a task to be assigned a -"high" load so that they can start running on best capacity cluster. By default, -newly created tasks are assigned a load defined by tunable sched_init_task_load -(Sec 7.8). Some specialized tasks may need a higher value than the global -default for their child tasks. This will let child tasks run on cpus with best -capacity. This is accomplished by setting the 'initial task load' attribute -(init_load_pct) for a task. Child tasks starting load (ravg.demand and -ravg.sum_history[]) is initialized from their parent's 'initial task load' -attribute. Note that child task's 'initial task load' attribute itself will be 0 -by default (i.e it is not inherited from parent). - -A task's 'initial task load' attribute can be set in two ways: - -**** /proc interface - -/proc/[pid]/sched_init_task_load can be written to for setting a task's 'initial -task load' attribute. A numeric value between 0 - 100 (in percent scale) is -accepted for task's 'initial task load' attribute. - -Reading /proc/[pid]/sched_init_task_load returns the 'initial task load' -attribute for the given task. - -**** kernel API - -Following kernel APIs are provided to set or retrieve a given task's 'initial -task load' attribute: - -int sched_set_init_task_load(struct task_struct *p, int init_load_pct); -int sched_get_init_task_load(struct task_struct *p); - - -=============== -3. CPU CAPACITY -=============== - -CPU capacity reflects peak performance offered by a cpu. It is defined both by -maximum frequency at which cpu can run and its efficiency attribute. Capacity of -a cpu is defined in reference to "least" performing cpu such that "least" -performing cpu has capacity of 1024. - - capacity = 1024 * (fmax_cur * / min_max_freq) * - (efficiency / min_possible_efficiency) - -where: - - fmax_cur = maximum frequency at which cpu is currently - allowed to run at - efficiency = IPC of cpu - min_max_freq = max frequency at which "least" performing cpu - can run - min_possible_efficiency = IPC of "least" performing cpu - -'fmax_cur' reflects the fact that a cpu may be constrained at runtime to run at -a maximum frequency less than what is supported. This may be a constraint placed -by user or drivers such as thermal that intends to reduce temperature of a cpu -by restricting its maximum frequency. - -'max_possible_capacity' reflects the maximum capacity of a cpu based on the -maximum frequency it supports. - -max_possible_capacity = 1024 * (fmax * / min_max_freq) * - (efficiency / min_possible_efficiency) - -where: - fmax = maximum frequency supported by a cpu - -/proc/sched_debug lists capacity and maximum_capacity information for a cpu. - -In the example HMP system quoted in Sec 2.3, "least" performing CPU is A53 and -thus min_max_freq = 1GHz and min_possible_efficiency = 1024. - -Capacity of A57 = 1024 * (2GHz / 1GHz) * (2048 / 1024) = 4096 -Capacity of A53 = 1024 * (1GHz / 1GHz) * (1024 / 1024) = 1024 - -Capacity of A57 when constrained to run at maximum frequency of 500MHz can be -calculated as: - -Capacity of A57 = 1024 * (500MHz / 1GHz) * (2048 / 1024) = 1024 - -*** 3.1 load_scale_factor - -'lsf' or load scale factor attribute of a cpu is used to estimate load of a task -on that cpu when running at its fmax_cur frequency. 'lsf' is defined in -reference to "best" performing cpu such that it's lsf is 1024. 'lsf' for a cpu -is defined as: - - lsf = 1024 * (max_possible_freq / fmax_cur) * - (max_possible_efficiency / ipc) - -where: - fmax_cur = maximum frequency at which cpu is currently - allowed to run at - ipc = IPC of cpu - max_possible_freq = max frequency at which "best" performing cpu - can run - max_possible_efficiency = IPC of "best" performing cpu - -In the example HMP system quoted in Sec 2.3, "best" performing CPU is A57 and -thus max_possible_freq = 2 GHz, max_possible_efficiency = 2048 - -lsf of A57 = 1024 * (2GHz / 2GHz) * (2048 / 2048) = 1024 -lsf of A53 = 1024 * (2GHz / 1 GHz) * (2048 / 1024) = 4096 - -lsf of A57 constrained to run at maximum frequency of 500MHz can be calculated -as: - -lsf of A57 = 1024 * (2GHz / 500Mhz) * (2048 / 2048) = 4096 - -To estimate load of a task on a given cpu running at its fmax_cur: - - load = scaled_load * lsf / 1024 - -A task with scaled load of 20% would thus be estimated to consume 80% bandwidth -of A53 running at 1GHz. The same task with scaled load of 20% would be estimated -to consume 160% bandwidth on A53 constrained to run at maximum frequency of -500MHz. - -load_scale_factor, thus, is very useful to estimate load of a task on a given -cpu and thus to decide whether it can fit in a cpu or not. - -*** 3.2 cpu_power - -A metric 'cpu_power' related to 'capacity' is also listed in /proc/sched_debug. -'cpu_power' is ideally same for all cpus (1024) when they are idle and running -at the same frequency. 'cpu_power' of a cpu can be scaled down from its ideal -value to reflect reduced frequency it is operating at and also to reflect the -amount of cpu bandwidth consumed by real-time tasks executing on it. -'cpu_power' metric is used by scheduler to decide task load distribution among -cpus. CPUs with low 'cpu_power' will be assigned less task load compared to cpus -with higher 'cpu_power' - -============ -4. CPU POWER -============ - -The HMP scheduler extensions currently depend on an architecture-specific driver -to provide runtime information on cpu power. In the absence of an -architecture-specific driver, the scheduler will resort to using the -max_possible_capacity metric of a cpu as a measure of its power. - -================ -5. HMP SCHEDULER -================ - -For normal (SCHED_OTHER/fair class) tasks there are three paths in the -scheduler which these HMP extensions affect. The task wakeup path, the -load balancer, and the scheduler tick are each modified. - -Real-time and stop-class tasks are served by different code -paths. These will be discussed separately. - -Prior to delving further into the algorithm and implementation however -some definitions are required. - -*** 5.1 Classification of Tasks and CPUs - -With the extensions described thus far, the following information is -available to the HMP scheduler: - -- per-task CPU demand information from either Per-Entity Load Tracking - (PELT) or the window-based algorithm described above - -- a power value for each frequency supported by each CPU via the API - described in section 4 - -- current CPU frequency, maximum CPU frequency (may be throttled by at - runtime due to thermal conditions), maximum possible CPU frequency supported - by hardware - -- data previously maintained within the scheduler such as the number - of currently runnable tasks on each CPU - -Combined with tunable parameters, this information can be used to classify -both tasks and CPUs to aid in the placement of tasks. - -- big task - - A big task is one that exerts a CPU demand too high for a particular - CPU to satisfy. The scheduler will attempt to find a CPU with more - capacity for such a task. - - The definition of "big" is specific to a task *and* a CPU. A task - may be considered big on one CPU in the system and not big on - another if the first CPU has less capacity than the second. - - What task demand is "too high" for a particular CPU? One obvious - answer would be a task demand which, as measured by PELT or - window-based load tracking, matches or exceeds the capacity of that - CPU. A task which runs on a CPU for a long time, for example, might - meet this criteria as it would report 100% demand of that CPU. It - may be desirable however to classify tasks which use less than 100% - of a particular CPU as big so that the task has some "headroom" to grow - without its CPU bandwidth getting capped and its performance requirements - not being met. This task demand is therefore a tunable parameter: - - /proc/sys/kernel/sched_upmigrate - - This value is a percentage. If a task consumes more than this much of a - particular CPU, that CPU will be considered too small for the task. The task - will thus be seen as a "big" task on the cpu and will reflect in nr_big_tasks - statistics maintained for that cpu. Note that certain tasks (whose nice - value exceeds SCHED_UPMIGRATE_MIN_NICE value or those that belong to a cgroup - whose upmigrate_discourage flag is set) will never be classified as big tasks - despite their high demand. - - As the load scale factor is calculated against current fmax, it gets boosted - when a lower capacity CPU is restricted to run at lower fmax. The task - demand is inflated in this scenario and the task upmigrates early to the - maximum capacity CPU. Hence this threshold is auto-adjusted by a factor - equal to max_possible_frequency/current_frequency of a lower capacity CPU. - This adjustment happens only when the lower capacity CPU frequency is - restricted. The same adjustment is applied to the downmigrate threshold - as well. - - When the frequency restriction is relaxed, the previous values are restored. - sched_up_down_migrate_auto_update macro defined in kernel/sched/core.c - controls this auto-adjustment behavior and it is enabled by default. - - If the adjusted upmigrate threshold exceeds the window size, it is clipped to - the window size. If the adjusted downmigrate threshold decreases the difference - between the upmigrate and downmigrate, it is clipped to a value such that the - difference between the modified and the original thresholds is same. - -- spill threshold - - Tasks will normally be placed on lowest power-cost cluster where they can fit. - This could result in power-efficient cluster becoming overcrowded when there - are "too" many low-demand tasks. Spill threshold provides a spill over - criteria, wherein low-demand task are allowed to be placed on idle or - busy cpus in high-performance cluster. - - Scheduler will avoid placing a task on a cpu if it can result in cpu exceeding - its spill threshold, which is defined by two tunables: - - /proc/sys/kernel/sched_spill_nr_run (default: 10) - /proc/sys/kernel/sched_spill_load (default : 100%) - - A cpu is considered to be above its spill level if it already has 10 tasks or - if the sum of task load (scaled in reference to given cpu) and - rq->cumulative_runnable_avg exceeds 'sched_spill_load'. - -- power band - - The scheduler may be faced with a tradeoff between power and performance when - placing a task. If the scheduler sees two CPUs which can accommodate a task: - - CPU 1, power cost of 20, load of 10 - CPU 2, power cost of 10, load of 15 - - It is not clear what the right choice of CPU is. The HMP scheduler - offers the sched_powerband_limit tunable to determine how this - situation should be handled. When the power delta between two CPUs - is less than sched_powerband_limit_pct, load will be prioritized as - the deciding factor as to which CPU is selected. If the power delta - between two CPUs exceeds that, the lower power CPU is considered to - be in a different "band" and it is selected, despite perhaps having - a higher current task load. - -*** 5.2 select_best_cpu() - -CPU placement decisions for a task at its wakeup or creation time are the -most important decisions made by the HMP scheduler. This section will describe -the call flow and algorithm used in detail. - -The primary entry point for a task wakeup operation is try_to_wake_up(), -located in kernel/sched/core.c. This function relies on select_task_rq() to -determine the target CPU for the waking task. For fair-class (SCHED_OTHER) -tasks, that request will be routed to select_task_rq_fair() in -kernel/sched/fair.c. As part of these scheduler extensions a hook has been -inserted into the top of that function. If HMP scheduling is enabled the normal -scheduling behavior will be replaced by a call to select_best_cpu(). This -function, select_best_cpu(), represents the heart of the HMP scheduling -algorithm described in this document. Note that select_best_cpu() is also -invoked for a task being created. - -The behavior of select_best_cpu() depends on several factors such as boost -setting, choice of several tunables and on task demand. - -**** 5.2.1 Boost - -The task placement policy changes signifincantly when scheduler boost is in -effect. When boost is in effect the scheduler ignores the power cost of -placing tasks on CPUs. Instead it figures out the load on each CPU and then -places task on the least loaded CPU. If the load of two or more CPUs is the -same (generally when CPUs are idle) the task prefers to go highest capacity -CPU in the system. - -A further enhancement during boost is the scheduler' early detection feature. -While boost is in effect the scheduler checks for the precence of tasks that -have been runnable for over some period of time within the tick. For such -tasks the scheduler informs the governor of imminent need for high frequency. -If there exists a task on the runqueue at the tick that has been runnable -for greater than SCHED_EARLY_DETECTION_DURATION amount of time, it notifies -the governor with a fabricated load of the full window at the highest -frequency. The fabricated load is maintained until the task is no longer -runnable or until the next tick. - -Boost can be set via either /proc/sys/kernel/sched_boost or by invoking -kernel API sched_set_boost(). - - int sched_set_boost(int enable); - -Once turned on, boost will remain in effect until it is explicitly turned off. -To allow for boost to be controlled by multiple external entities (application -or kernel module) at same time, boost setting is reference counted. This means -that two applications can turn on boost and the effect of boost is eliminated -only after both applications have turned off boost. boost_refcount variable -represents this reference count. - -**** 5.2.2 task_will_fit() - -The overall goal of select_best_cpu() is to place a task on the least power -cluster where it can "fit" i.e where its cpu usage shall be below the capacity -offered by cluster. Criteria for a task to be considered as fitting in a cluster -is: - - i) A low-priority task, whose nice value is greater than - SCHED_UPMIGRATE_MIN_NICE or whose cgroup has its - upmigrate_discourage flag set, is considered to be fitting in all clusters, - irrespective of their capacity and task's cpu demand. - - ii) All tasks are considered to fit in highest capacity cluster. - - iii) Task demand scaled in reference to the given cluster should be less than a - threshold. See section on load_scale_factor to know more about how task - demand is scaled in reference to a given cpu (cluster). The threshold used - is normally sched_upmigrate. Its possible for a task's demand to exceed - sched_upmigrate threshold in reference to a cluster when its upmigrated to - higher capacity cluster. To prevent it from coming back immediately to - lower capacity cluster, the task is not considered to "fit" on its earlier - cluster until its demand has dropped below sched_downmigrate in reference - to that earlier cluster. sched_downmigrate thus provides for some - hysteresis control. - - -**** 5.2.3 Factors affecting select_best_cpu() - -Behavior of select_best_cpu() is further controlled by several tunables and -synchronous nature of wakeup. - -a. /proc/sys/kernel/sched_cpu_high_irqload - A cpu whose irq load is greater than this threshold will not be - considered eligible for placement. This threshold value in expressed in - nanoseconds scale, with default threshold being 10000000 (10ms). See - notes on sched_cpu_high_irqload tunable to understand how irq load on a - cpu is measured. - -b. Synchronous nature of wakeup - Synchronous wakeup is a hint to scheduler that the task issuing wakeup - (i.e task currently running on cpu where wakeup is being processed by - scheduler) will "soon" relinquish CPU. A simple example is two tasks - communicating with each other using a pipe structure. When reader task - blocks waiting for data, its woken by writer task after it has written - data to pipe. Writer task usually blocks waiting for reader task to - consume data in pipe (which may not have any more room for writes). - - Synchronous wakeup is accounted for by adjusting load of a cpu to not - include load of currently running task. As a result, a cpu that has only - one runnable task and which is currently processing synchronous wakeup - will be considered idle. - -c. PF_WAKE_UP_IDLE - Any task with this flag set will be woken up to an idle cpu (if one is - available) independent of sched_prefer_idle flag setting, its demand and - synchronous nature of wakeup. Similarly idle cpu is preferred during - wakeup for any task that does not have this flag set but is being woken - by a task with PF_WAKE_UP_IDLE flag set. For simplicity, we will use the - term "PF_WAKE_UP_IDLE wakeup" to signify wakeups involving a task with - PF_WAKE_UP_IDLE flag set. - -d. /proc/sys/kernel/sched_select_prev_cpu_us - This threshold controls whether task placement goes through fast path or - not. If task's wakeup time since last sleep is short there are high - chances that it's better to place the task on its previous CPU. This - reduces task placement latency, cache miss and number of migrations. - Default value of sched_select_prev_cpu_us is 2000 (2ms). This can be - turned off by setting it to 0. - -**** 5.2.4 Wakeup Logic for Task "p" - -Wakeup task placement logic is as follows: - -1) Eliminate CPUs with high irq load based on sched_cpu_high_irqload tunable. - -2) Eliminate CPUs where either the task does not fit or CPUs where placement -will result in exceeding the spill threshold tunables. CPUs elimiated at this -stage will be considered as backup choices incase none of the CPUs get past -this stage. - -3) Find out and return the least power CPU that satisfies all conditions above. - -4) If two or more CPUs are projected to have the same power, break ties in the -following preference order: - a) The CPU is the task's previous CPU. - b) The CPU is in the same cluster as the task's previous CPU. - c) The CPU has the least load - -The placement logic described above does not apply when PF_WAKE_UP_IDLE is set -for either the waker task or the wakee task. Instead the scheduler chooses the -most power efficient idle CPU. - -5) If no CPU is found after step 2, resort to backup CPU selection logic -whereby the CPU with highest amount of spare capacity is selected. - -6) If none of the CPUs have any spare capacity, return the task's previous -CPU. - -*** 5.3 Scheduler Tick - -Every CPU is interrupted periodically to let kernel update various statistics -and possibly preempt the currently running task in favor of a waiting task. This -periodicity, determined by CONFIG_HZ value, is set at 10ms. There are various -optimizations by which a CPU however can skip taking these interrupts (ticks). -A cpu going idle for considerable time in one such case. - -HMP scheduler extensions brings in a change in processing of tick -(scheduler_tick()) that can result in task migration. In case the currently -running task on a cpu belongs to fair_sched class, a check is made if it needs -to be migrated. Possible reasons for migrating task could be: - -a) A big task is running on a power-efficient cpu and a high-performance cpu is -available (idle) to service it - -b) A task is starving on a CPU with high irq load. - -c) A task with upmigration discouraged is running on a performance cluster. -See notes on 'cpu.upmigrate_discourage'. - -In case the test for migration turns out positive (which is expected to be rare -event), a candidate cpu is identified for task migration. To avoid multiple task -migrations to the same candidate cpu(s), identification of candidate cpu is -serialized via global spinlock (migration_lock). - -*** 5.4 Load Balancer - -Load balance is a key functionality of scheduler that strives to distribute task -across available cpus in a "fair" manner. Most of the complexity associated with -this feature involves balancing fair_sched class tasks. Changes made to load -balance code serve these goals: - -1. Restrict flow of tasks from power-efficient cpus to high-performance cpu. - Provide a spill-over threshold, defined in terms of number of tasks - (sched_spill_nr_run) and cpu demand (sched_spill_load), beyond which tasks - can spill over from power-efficient cpu to high-performance cpus. - -2. Allow idle power-efficient cpus to pick up extra load from over-loaded - performance-efficient cpu - -3. Allow idle high-performance cpu to pick up big tasks from power-efficient cpu - -*** 5.5 Real Time Tasks - -Minimal changes introduced in treatment of real-time tasks by HMP scheduler -aims at preferring scheduling of real-time tasks on cpus with low load on -a power efficient cluster. - -Prior to HMP scheduler, the fast-path cpu selection for placing a real-time task -(at wakeup) is its previous cpu, provided the currently running task on its -previous cpu is not a real-time task or a real-time task with lower priority. -Failing this, cpu selection in slow-path involves building a list of candidate -cpus where the waking real-time task will be of highest priority and thus can be -run immediately. The first cpu from this candidate list is chosen for the waking -real-time task. Much of the premise for this simple approach is the assumption -that real-time tasks often execute for very short intervals and thus the focus -is to place them on a cpu where they can be run immediately. - -HMP scheduler brings in a change which avoids fast-path and always resorts to -slow-path. Further cpu with lowest load in a power efficient cluster from -candidate list of cpus is chosen as cpu for placing waking real-time task. - -- PF_WAKE_UP_IDLE - -Idle cpu is preferred for any waking task that has this flag set in its -'task_struct.flags' field. Further idle cpu is preferred for any task woken by -such tasks. PF_WAKE_UP_IDLE flag of a task is inherited by it's children. It can -be modified for a task in two ways: - - > kernel-space interface - set_wake_up_idle() needs to be called in the context of a task - to set or clear its PF_WAKE_UP_IDLE flag. - - > user-space interface - /proc/[pid]/sched_wake_up_idle file needs to be written to for - setting or clearing PF_WAKE_UP_IDLE flag for a given task - -===================== -6. FREQUENCY GUIDANCE -===================== - -As mentioned in the introduction section the scheduler is in a unique -position to assist with the determination of CPU frequency. Because -the scheduler now maintains an estimate of per-task CPU demand, task -activity can be tracked, aggregated and provided to the CPUfreq -governor as a replacement for simple CPU busy time. CONFIG_SCHED_FREQ_INPUT -kernel configuration variable needs to be enabled for this feature to be active. - -Two of the most popular CPUfreq governors, interactive and ondemand, -utilize a window-based approach for measuring CPU busy time. This -works well with the window-based load tracking scheme previously -described. The following APIs are provided to allow the CPUfreq -governor to query busy time from the scheduler instead of using the -basic CPU busy time value derived via get_cpu_idle_time_us() and -get_cpu_iowait_time_us() APIs. - - int sched_set_window(u64 window_start, unsigned int window_size) - - This API is invoked by governor at initialization time or whenever - window size is changed. 'window_size' argument (in jiffy units) - indicates the size of window to be used. The first window of size - 'window_size' is set to begin at jiffy 'window_start' - - -EINVAL is returned if per-entity load tracking is in use rather - than window-based load tracking, otherwise a success value of 0 - is returned. - - int sched_get_busy(int cpu) - - Returns the busy time for the given CPU in the most recent - complete window. The value returned is microseconds of busy - time at fmax of given CPU. - -The values returned by sched_get_busy() take a bit of explanation, -both in what they mean and also how they are derived. - -*** 6.1 Per-CPU Window-Based Stats - -In addition to the per-task window-based demand, the HMP scheduler -extensions also track the aggregate demand seen on each CPU. This is -done using the same windows that the task demand is tracked with -(which is in turn set by the governor when frequency guidance is in -use). There are four quantities maintained for each CPU by the HMP scheduler: - - curr_runnable_sum: aggregate demand from all tasks which executed during - the current (not yet completed) window - - prev_runnable_sum: aggregate demand from all tasks which executed during - the most recent completed window - - nt_curr_runnable_sum: aggregate demand from all 'new' tasks which executed - during the current (not yet completed) window - - nt_prev_runnable_sum: aggregate demand from all 'new' tasks which executed - during the most recent completed window. - -When the scheduler is updating a task's window-based stats it also -updates these values. Like per-task window-based demand these -quantities are normalized against the max possible frequency and max -efficiency (instructions per cycle) in the system. If an update occurs -and a window rollover is observed, curr_runnable_sum is copied into -prev_runnable_sum before being reset to 0. The sched_get_busy() API -returns prev_runnable_sum, scaled to the efficiency and fmax of given -CPU. The same applies to nt_curr_runnable_sum and nt_prev_runnable_sum. - -A 'new' task is defined as a task whose number of active windows since fork is -less than sysctl_sched_new_task_windows. An active window is defined as a window -where a task was observed to be runnable. - -*** 6.2 Per-task window-based stats - -Corresponding to curr_runnable_sum and prev_runnable_sum, two counters are -maintained per-task - -curr_window - represents cpu demand of task in its most recently tracked - window -prev_window - represents cpu demand of task in the window prior to the one - being tracked by curr_window - -The above counters are resued for nt_curr_runnable_sum and -nt_prev_runnable_sum. - -"cpu demand" of a task includes its execution time and can also include its -wait time. 'SCHED_FREQ_ACCOUNT_WAIT_TIME' controls whether task's wait -time is included in its 'curr_window' and 'prev_window' counters or not. - -Needless to say, curr_runnable_sum counter of a cpu is derived from curr_window -counter of various tasks that ran on it in its most recent window. - -*** 6.3 Effect of various task events - -We now consider various events and how they affect above mentioned counters. - -PICK_NEXT_TASK - This represents beginning of execution for a task. Provided the task - refers to a non-idle task, a portion of task's wait time that - corresponds to the current window being tracked on a cpu is added to - task's curr_window counter, provided SCHED_FREQ_ACCOUNT_WAIT_TIME is - set. The same quantum is also added to cpu's curr_runnable_sum counter. - The remaining portion, which corresponds to task's wait time in previous - window is added to task's prev_window and cpu's prev_runnable_sum - counters. - -PUT_PREV_TASK - This represents end of execution of a time-slice for a task, where the - task could refer to a cpu's idle task also. In case the task is non-idle - or (in case of task being idle with cpu having non-zero rq->nr_iowait - count and sched_io_is_busy =1), a portion of task's execution time, that - corresponds to current window being tracked on a cpu is added to task's - curr_window_counter and also to cpu's curr_runnable_sum counter. Portion - of task's execution that corresponds to the previous window is added to - task's prev_window and cpu's prev_runnable_sum counters. - -TASK_UPDATE - This event is called on a cpu's currently running task and hence - behaves effectively as PUT_PREV_TASK. Task continues executing after - this event, until PUT_PREV_TASK event occurs on the task (during - context switch). - -TASK_WAKE - This event signifies a task waking from sleep. Since many windows - could have elapsed since the task went to sleep, its curr_window - and prev_window are updated to reflect task's demand in the most - recent and its previous window that is being tracked on a cpu. - -TASK_MIGRATE - This event signifies task migration across cpus. It is invoked on the - task prior to being moved. Thus at the time of this event, the task - can be considered to be in "waiting" state on src_cpu. In that way - this event reflects actions taken under PICK_NEXT_TASK (i.e its - wait time is added to task's curr/prev_window counters as well - as src_cpu's curr/prev_runnable_sum counters, provided - SCHED_FREQ_ACCOUNT_WAIT_TIME is non-zero). After that update, - src_cpu's curr_runnable_sum is reduced by task's curr_window value - and dst_cpu's curr_runnable_sum is increased by task's curr_window - value. Similarly, src_cpu's prev_runnable_sum is reduced by task's - prev_window value and dst_cpu's prev_runnable_sum is increased by - task's prev_window value. - -IRQ_UPDATE - This event signifies end of execution of an interrupt handler. This - event results in update of cpu's busy time counters, curr_runnable_sum - and prev_runnable_sum, provided cpu was idle. - When sched_io_is_busy = 0, only the interrupt handling time is added - to cpu's curr_runnable_sum and prev_runnable_sum counters. When - sched_io_is_busy = 1, the event mirrors actions taken under - TASK_UPDATED event i.e time since last accounting of idle task's cpu - usage is added to cpu's curr_runnable_sum and prev_runnable_sum - counters. - -=========== -7. TUNABLES -=========== - -*** 7.1 sched_spill_load - -Appears at: /proc/sys/kernel/sched_spill_load - -Default value: 100 - -CPU selection criteria for fair-sched class tasks is the lowest power cpu where -they can fit. When the most power-efficient cpu where a task can fit is -overloaded (aggregate demand of tasks currently queued on it exceeds -sched_spill_load), a task can be placed on a higher-performance cpu, even though -the task strictly doesn't need one. - -*** 7.2 sched_spill_nr_run - -Appears at: /proc/sys/kernel/sched_spill_nr_run - -Default value: 10 - -The intent of this tunable is similar to sched_spill_load, except it applies to -nr_running count of a cpu. A task can spill over to a higher-performance cpu -when the most power-efficient cpu where it can normally fit has more tasks than -sched_spill_nr_run. - -*** 7.3 sched_upmigrate - -Appears at: /proc/sys/kernel/sched_upmigrate - -Default value: 80 - -This tunable is a percentage. If a task consumes more than this much -of a CPU, the CPU is considered too small for the task and the -scheduler will try to find a bigger CPU to place the task on. - -*** 7.4 sched_init_task_load - -Appears at: /proc/sys/kernel/sched_init_task_load - -Default value: 15 - -This tunable is a percentage. When a task is first created it has no -history, so the task load tracking mechanism cannot determine a -historical load value to assign to it. This tunable specifies the -initial load value for newly created tasks. Also see Sec 2.8 on per-task -'initial task load' attribute. - -*** 7.5 sched_ravg_hist_size - -Appears at: /proc/sys/kernel/sched_ravg_hist_size - -Default value: 5 - -This tunable controls the number of samples used from task's sum_history[] -array for determination of its demand. - -*** 7.6 sched_window_stats_policy - -Appears at: /proc/sys/kernel/sched_window_stats_policy - -Default value: 2 - -This tunable controls the policy in how window-based load tracking -calculates an overall demand value based on the windows of CPU -utilization it has collected for a task. - -Possible values for this tunable are: -0: Just use the most recent window sample of task activity when calculating - task demand. -1: Use the maximum value of first M samples found in task's cpu demand - history (sum_history[] array), where M = sysctl_sched_ravg_hist_size -2: Use the maximum of (the most recent window sample, average of first M - samples), where M = sysctl_sched_ravg_hist_size -3. Use average of first M samples, where M = sysctl_sched_ravg_hist_size - -*** 7.7 sched_ravg_window - -Appears at: kernel command line argument - -Default value: 10000000 (10ms, units of tunable are nanoseconds) - -This specifies the duration of each window in window-based load -tracking. By default each window is 10ms long. This quantity must -currently be set at boot time on the kernel command line (or the -default value of 10ms can be used). - -*** 7.8 RAVG_HIST_SIZE - -Appears at: compile time only (see RAVG_HIST_SIZE in include/linux/sched.h) - -Default value: 5 - -This macro specifies the number of windows the window-based load -tracking mechanism maintains per task. If default values are used for -both this and sched_ravg_window then a total of 50ms of task history -would be maintained in 5 10ms windows. - -*** 7.9 sched_freq_inc_notify - -Appears at: /proc/sys/kernel/sched_freq_inc_notify - -Default value: 10 * 1024 * 1024 (10 Ghz) - -When scheduler detects that cur_freq of a cluster is insufficient to meet -demand, it sends notification to governor, provided (freq_required - cur_freq) -exceeds sched_freq_inc_notify, where freq_required is the frequency calculated -by scheduler to meet current task demand. Note that sched_freq_inc_notify is -specified in kHz units. - -*** 7.10 sched_freq_dec_notify - -Appears at: /proc/sys/kernel/sched_freq_dec_notify - -Default value: 10 * 1024 * 1024 (10 Ghz) - -When scheduler detects that cur_freq of a cluster is far greater than what is -needed to serve current task demand, it will send notification to governor. -More specifically, notification is sent when (cur_freq - freq_required) -exceeds sched_freq_dec_notify, where freq_required is the frequency calculated -by scheduler to meet current task demand. Note that sched_freq_dec_notify is -specified in kHz units. - -*** 7.11 sched_cpu_high_irqload - -Appears at: /proc/sys/kernel/sched_cpu_high_irqload - -Default value: 10000000 (10ms) - -The scheduler keeps a decaying average of the amount of irq and softirq activity -seen on each CPU within a ten millisecond window. Note that this "irqload" -(reported in the sched_cpu_load_* tracepoint) will be higher than the typical load -in a single window since every time the window rolls over, the value is decayed -by some fraction and then added to the irq/softirq time spent in the next -window. - -When the irqload on a CPU exceeds the value of this tunable, the CPU is no -longer eligible for placement. This will affect the task placement logic -described above, causing the scheduler to try and steer tasks away from -the CPU. - -*** 7.12 cpu.upmigrate_discourage - -Default value : 0 - -This is a cgroup attribute supported by the cpu resource controller. It normally -appears at [root_cpu]/[name1]/../[name2]/cpu.upmigrate_discourage. Here -"root_cpu" is the mount point for cgroup (cpu resource control) filesystem -and name1, name2 etc are names of cgroups that form a hierarchy. - -Setting this flag to 1 discourages upmigration for all tasks of a cgroup. High -demand tasks of such a cgroup will never be classified as big tasks and hence -not upmigrated. Any task of the cgroup is allowed to upmigrate only under -overcommitted scenario. See notes on sched_spill_nr_run and sched_spill_load for -how overcommitment threshold is defined. - -*** 7.13 sched_static_cpu_pwr_cost - -Default value: 0 - -Appears at /sys/devices/system/cpu/cpu/sched_static_cpu_pwr_cost - -This is the power cost associated with bringing an idle CPU out of low power -mode. It ignores the actual C-state that a CPU may be in and assumes the -worst case power cost of the highest C-state. It is means of biasing task -placement away from idle CPUs when necessary. It can be defined per CPU, -however, a more appropriate usage to define the same value for every CPU -within a cluster and possibly have differing value between clusters as -needed. - - -*** 7.14 sched_static_cluster_pwr_cost - -Default value: 0 - -Appears at /sys/devices/system/cpu/cpu/sched_static_cluster_pwr_cost - -This is the power cost associated with bringing an idle cluster out of low -power mode. It ignores the actual D-state that a cluster may be in and assumes -the worst case power cost of the highest D-state. It is means of biasing task -placement away from idle clusters when necessary. - -*** 7.15 sched_restrict_cluster_spill - -Default value: 0 - -Appears at /proc/sys/kernel/sched_restrict_cluster_spill - -This tunable can be used to restrict tasks spilling to the higher capacity -(higher power) cluster. When this tunable is enabled, - -- Restrict the higher capacity cluster pulling tasks from the lower capacity -cluster in the load balance path. The restriction is lifted if all of the CPUS -in the lower capacity cluster are above spill. The power cost is used to break -the ties if the capacity of clusters are same for applying this restriction. - -- The current CPU selection algorithm for RT tasks looks for the least loaded -CPU across all clusters. When this tunable is enabled, the RT tasks are -restricted to the lowest possible power cluster. - - -*** 7.16 sched_downmigrate - -Appears at: /proc/sys/kernel/sched_downmigrate - -Default value: 60 - -This tunable is a percentage. It exists to control hysteresis. Lets say a task -migrated to a high-performance cpu when it crossed 80% demand on a -power-efficient cpu. We don't let it come back to a power-efficient cpu until -its demand *in reference to the power-efficient cpu* drops less than 60% -(sched_downmigrate). - - -*** 7.17 sched_small_wakee_task_load - -Appears at: /proc/sys/kernel/sched_small_wakee_task_load - -Default value: 10 - -This tunable is a percentage. Configure the maximum demand of small wakee task. -Sync wakee tasks which have demand less than sched_small_wakee_task_load are -categorized as small wakee tasks. Scheduler places small wakee tasks on the -waker's cluster. - - -*** 7.18 sched_big_waker_task_load - -Appears at: /proc/sys/kernel/sched_big_waker_task_load - -Default value: 25 - -This tunable is a percentage. Configure the minimum demand of big sync waker -task. Scheduler places small wakee tasks woken up by big sync waker on the -waker's cluster. - -========================= -8. HMP SCHEDULER TRACE POINTS -========================= - -*** 8.1 sched_enq_deq_task - -Logged when a task is either enqueued or dequeued on a CPU's run queue. - - -0 [004] d.h4 12700.711665: sched_enq_deq_task: cpu=4 enqueue comm=powertop pid=13227 prio=120 nr_running=1 cpu_load=0 rt_nr_running=0 affine=ff demand=13364423 - -- cpu: the CPU that the task is being enqueued on to or dequeued off of -- enqueue/dequeue: whether this was an enqueue or dequeue event -- comm: name of task -- pid: PID of task -- prio: priority of task -- nr_running: number of runnable tasks on this CPU -- cpu_load: current priority-weighted load on the CPU (note, this is *not* - the same as CPU utilization or a metric tracked by PELT/window-based tracking) -- rt_nr_running: number of real-time processes running on this CPU -- affine: CPU affinity mask in hex for this task (so ff is a task eligible to - run on CPUs 0-7) -- demand: window-based task demand computed based on selected policy (recent, - max, or average) (ns) - -*** 8.2 sched_task_load - -Logged when selecting the best CPU to run the task (select_best_cpu()). - -sched_task_load: 4004 (adbd): demand=698425 boost=0 reason=0 sync=0 need_idle=0 best_cpu=0 latency=103177 - -- demand: window-based task demand computed based on selected policy (recent, - max, or average) (ns) -- boost: whether boost is in effect -- reason: reason we are picking a new CPU: - 0: no migration - selecting a CPU for a wakeup or new task wakeup - 1: move to big CPU (migration) - 2: move to little CPU (migration) - 3: move to low irq load CPU (migration) -- sync: is the nature synchronous in nature -- need_idle: is an idle CPU required for this task based on PF_WAKE_UP_IDLE -- best_cpu: The CPU selected by the select_best_cpu() function for placement -- latency: The execution time of the function select_best_cpu() - -*** 8.3 sched_cpu_load_* - -Logged when selecting the best CPU to run a task (select_best_cpu() for fair -class tasks, find_lowest_rq_hmp() for RT tasks) and load balancing -(update_sg_lb_stats()). - --0 [004] d.h3 12700.711541: sched_cpu_load_*: cpu 0 idle 1 nr_run 0 nr_big 0 lsf 1119 capacity 1024 cr_avg 0 irqload 3301121 fcur 729600 fmax 1459200 power_cost 5 cstate 2 temp 38 - -- cpu: the CPU being described -- idle: boolean indicating whether the CPU is idle -- nr_run: number of tasks running on CPU -- nr_big: number of BIG tasks running on CPU -- lsf: load scale factor - multiply normalized load by this factor to determine - how much load task will exert on CPU -- capacity: capacity of CPU (based on max possible frequency and efficiency) -- cr_avg: cumulative runnable average, instantaneous sum of the demand (either - PELT or window-based) of all the runnable task on a CPU (ns) -- irqload: decaying average of irq activity on CPU (ns) -- fcur: current CPU frequency (Khz) -- fmax: max CPU frequency (but not maximum _possible_ frequency) (KHz) -- power_cost: cost of running this CPU at the current frequency -- cstate: current cstate of CPU -- temp: current temperature of the CPU - -The power_cost value above differs in how it is calculated depending on the -callsite of this tracepoint. The select_best_cpu() call to this tracepoint -finds the minimum frequency required to satisfy the existing load on the CPU -as well as the task being placed, and returns the power cost of that frequency. -The load balance and real time task placement paths used a fixed frequency -(highest frequency common to all CPUs for load balancing, minimum -frequency of the CPU for real time task placement). - -*** 8.4 sched_update_task_ravg - -Logged when window-based stats are updated for a task. The update may happen -for a variety of reasons, see section 2.5, "Task Events." - --0 [004] d.h4 12700.711513: sched_update_task_ravg: wc 12700711473496 ws 12700691772135 delta 19701361 event TASK_WAKE cpu 4 cur_freq 199200 cur_pid 0 task 13227 (powertop) ms 12640648272532 delta 60063200964 demand 13364423 sum 0 irqtime 0 cs 0 ps 495018 cur_window 0 prev_window 0 - -- wc: wallclock, output of sched_clock(), monotonically increasing time since - boot (will roll over in 585 years) (ns) -- ws: window start, time when the current window started (ns) -- delta: time since the window started (wc - ws) (ns) -- event: What event caused this trace event to occur (see section 2.5 for more - details) -- cpu: which CPU the task is running on -- cur_freq: CPU's current frequency in KHz -- curr_pid: PID of the current running task (current) -- task: PID and name of task being updated -- ms: mark start - timestamp of the beginning of a segment of task activity, - either sleeping or runnable/running (ns) -- delta: time since last event within the window (wc - ms) (ns) -- demand: task demand computed based on selected policy (recent, max, or - average) (ns) -- sum: the task's run time during current window scaled by frequency and - efficiency (ns) -- irqtime: length of interrupt activity (ns). A non-zero irqtime is seen - when an idle cpu handles interrupts, the time for which needs to be - accounted as cpu busy time -- cs: curr_runnable_sum of cpu (ns). See section 6.1 for more details of this - counter. -- ps: prev_runnable_sum of cpu (ns). See section 6.1 for more details of this - counter. -- cur_window: cpu demand of task in its most recently tracked window (ns) -- prev_window: cpu demand of task in the window prior to the one being tracked - by cur_window - -*** 8.5 sched_update_history - -Logged when update_task_ravg() is accounting task activity into one or -more windows that have completed. This may occur more than once for a -single call into update_task_ravg(). A task that ran for 24ms spanning -four 10ms windows (the last 2ms of window 1, all of windows 2 and 3, -and the first 2ms of window 4) would result in two calls into -update_history() from update_task_ravg(). The first call would record activity -in completed window 1 and second call would record activity for windows 2 and 3 -together (samples will be 2 in second call). - --0 [004] d.h4 12700.711489: sched_update_history: 13227 (powertop): runtime 13364423 samples 1 event TASK_WAKE demand 13364423 (hist: 13364423 9871252 2236009 6162476 10282078) cpu 4 nr_big 0 - -- runtime: task cpu demand in recently completed window(s). This value is scaled - to max_possible_freq and max_possible_efficiency. This value is pushed into - task's demand history array. The number of windows to which runtime applies is - provided by samples field. -- samples: Number of samples (windows), each having value of runtime, that is - recorded in task's demand history array. -- event: What event caused this trace event to occur (see section 2.5 for more - details) - PUT_PREV_TASK, PICK_NEXT_TASK, TASK_WAKE, TASK_MIGRATE, - TASK_UPDATE -- demand: task demand computed based on selected policy (recent, max, or - average) (ns) -- hist: last 5 windows of history for the task with the most recent window - listed first -- cpu: CPU the task is associated with -- nr_big: number of big tasks on the CPU - -*** 8.6 sched_reset_all_windows_stats - -Logged when key parameters controlling window-based statistics collection are -changed. This event signifies that all window-based statistics for tasks and -cpus are being reset. Changes to below attributes result in such a reset: - -* sched_ravg_window (See Sec 2) -* sched_window_stats_policy (See Sec 2.4) -* sched_ravg_hist_size (See Sec 7.11) - --0 [004] d.h4 12700.711489: sched_reset_all_windows_stats: time_taken 1123 window_start 0 window_size 0 reason POLICY_CHANGE old_val 0 new_val 1 - -- time_taken: time taken for the reset function to complete (ns) -- window_start: Beginning of first window following change to window size (ns) -- window_size: Size of window. Non-zero if window-size is changing (in ticks) -- reason: Reason for reset of statistics. -- old_val: Old value of variable, change of which is triggering reset -- new_val: New value of variable, change of which is triggering reset - -*** 8.7 sched_migration_update_sum - -Logged when CONFIG_SCHED_FREQ_INPUT feature is enabled and a task is migrating -to another cpu. - --0 [000] d..8 5020.404137: sched_migration_update_sum: cpu 0: cs 471278 ps 902463 nt_cs 0 nt_ps 0 pid 2645 - -- cpu: cpu, away from which or to which, task is migrating -- cs: curr_runnable_sum of cpu (ns). See Sec 6.1 for more details of this - counter. -- ps: prev_runnable_sum of cpu (ns). See Sec 6.1 for more details of this - counter. -- nt_cs: nt_curr_runnable_sum of cpu (ns). See Sec 6.1 for more details of - this counter. -- nt_ps: nt_prev_runnable_sum of cpu (ns). See Sec 6.1 for more details of - this counter -- pid: PID of migrating task - -*** 8.8 sched_get_busy - -Logged when scheduler is returning busy time statistics for a cpu. - -<...>-4331 [003] d.s3 313.700108: sched_get_busy: cpu 3 load 19076 new_task_load 0 early 0 - - -- cpu: cpu, for which busy time statistic (prev_runnable_sum) is being - returned (ns) -- load: corresponds to prev_runnable_sum (ns), scaled to fmax of cpu -- new_task_load: corresponds to nt_prev_runnable_sum to fmax of cpu -- early: A flag indicating whether the scheduler is passing regular load or early detection load - 0 - regular load - 1 - early detection load - -*** 8.9 sched_freq_alert - -Logged when scheduler is alerting cpufreq governor about need to change -frequency - --0 [004] d.h4 12700.711489: sched_freq_alert: cpu 0 old_load=XXX new_load=YYY - -- cpu: cpu in cluster that has highest load (prev_runnable_sum) -- old_load: cpu busy time last reported to governor. This is load scaled in - reference to max_possible_freq and max_possible_efficiency. -- new_load: recent cpu busy time. This is load scaled in - reference to max_possible_freq and max_possible_efficiency. - -*** 8.10 sched_set_boost - -Logged when boost settings are being changed - --0 [004] d.h4 12700.711489: sched_set_boost: ref_count=1 - -- ref_count: A non-zero value indicates boost is in effect From 62f2600ce97a18db675289ae471286267d7d268e Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Thu, 28 Jul 2016 19:18:08 -0700 Subject: [PATCH 07/16] sched: Remove all existence of CONFIG_SCHED_FREQ_INPUT CONFIG_SCHED_FREQ_INPUT was created to keep parts of the scheduler dealing with frequency separate from other parts of the scheduler that deal with task placement. However, overtime the two features have become intricately linked whereby SCHED_FREQ_INPUT cannot be turned on without having SCHED_HMP turned on as well. Given this complex inter-dependency and the fact that all old, existing and future targets use both config options, remove this unnecessary feature separation. It will aid in making kernel upgrades a lot simpler and faster. Change-Id: Ia20e40d8a088d50909cc28f5be758fa3e9a4af6f Signed-off-by: Syed Rameez Mustafa --- Documentation/scheduler/sched-hmp.txt | 9 +- drivers/cpufreq/Kconfig | 10 -- include/linux/sched.h | 53 +++--- include/linux/sched/sysctl.h | 16 +- include/trace/events/sched.h | 56 ++---- kernel/sched/core.c | 78 +-------- kernel/sched/fair.c | 28 +-- kernel/sched/sched.h | 236 +++++++++++--------------- kernel/sysctl.c | 6 +- 9 files changed, 147 insertions(+), 345 deletions(-) diff --git a/Documentation/scheduler/sched-hmp.txt b/Documentation/scheduler/sched-hmp.txt index 5a84f0524481..22449aec5558 100644 --- a/Documentation/scheduler/sched-hmp.txt +++ b/Documentation/scheduler/sched-hmp.txt @@ -189,9 +189,6 @@ struct ravg { u64 mark_start; u32 sum, demand; u32 sum_history[RAVG_HIST_SIZE]; -#ifdef CONFIG_SCHED_FREQ_INPUT - u32 curr_window, prev_window; -#endif }; struct task_struct { @@ -842,8 +839,7 @@ As mentioned in the introduction section the scheduler is in a unique position to assist with the determination of CPU frequency. Because the scheduler now maintains an estimate of per-task CPU demand, task activity can be tracked, aggregated and provided to the CPUfreq -governor as a replacement for simple CPU busy time. CONFIG_SCHED_FREQ_INPUT -kernel configuration variable needs to be enabled for this feature to be active. +governor as a replacement for simple CPU busy time. Two of the most popular CPUfreq governors, interactive and ondemand, utilize a window-based approach for measuring CPU busy time. This @@ -1384,8 +1380,7 @@ cpus are being reset. Changes to below attributes result in such a reset: *** 8.7 sched_migration_update_sum -Logged when CONFIG_SCHED_FREQ_INPUT feature is enabled and a task is migrating -to another cpu. +Logged when a task is migrating to another cpu. -0 [000] d..8 5020.404137: sched_migration_update_sum: cpu 0: cs 471278 ps 902463 nt_cs 0 nt_ps 0 pid 2645 diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig index c8674404dbf3..4c5c65e1e5d0 100644 --- a/drivers/cpufreq/Kconfig +++ b/drivers/cpufreq/Kconfig @@ -25,16 +25,6 @@ config CPU_FREQ_BOOST_SW bool depends on THERMAL -config SCHED_FREQ_INPUT - bool "Scheduler inputs to cpufreq governor" - depends on SCHED_HMP - help - This option enables support for scheduler based CPU utilization - calculations which may then be used by any cpufreq governor. The - scheduler keeps track of "recent" cpu demand of tasks, which can - help determine need for changing frequency well in advance of what - a governor would have been able to detect on its own. - config CPU_FREQ_STAT tristate "CPU frequency translation statistics" default y diff --git a/include/linux/sched.h b/include/linux/sched.h index c326611099d7..d7f67d0b9644 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1305,12 +1305,10 @@ struct ravg { u64 mark_start; u32 sum, demand; u32 sum_history[RAVG_HIST_SIZE_MAX]; -#ifdef CONFIG_SCHED_FREQ_INPUT u32 curr_window, prev_window; u16 active_windows; u32 pred_demand; u8 busy_buckets[NUM_BUSY_BUCKETS]; -#endif }; struct sched_entity { @@ -2152,32 +2150,6 @@ static inline cputime_t task_gtime(struct task_struct *t) extern void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st); extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st); -struct sched_load { - unsigned long prev_load; - unsigned long new_task_load; - unsigned long predicted_load; -}; - -#if defined(CONFIG_SCHED_FREQ_INPUT) -extern int sched_set_window(u64 window_start, unsigned int window_size); -extern unsigned long sched_get_busy(int cpu); -extern void sched_get_cpus_busy(struct sched_load *busy, - const struct cpumask *query_cpus); -extern void sched_set_io_is_busy(int val); -#else -static inline int sched_set_window(u64 window_start, unsigned int window_size) -{ - return -EINVAL; -} -static inline unsigned long sched_get_busy(int cpu) -{ - return 0; -} -static inline void sched_get_cpus_busy(struct sched_load *busy, - const struct cpumask *query_cpus) {}; -static inline void sched_set_io_is_busy(int val) {}; -#endif - /* * Per process flags */ @@ -2360,13 +2332,23 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p, } #endif +struct sched_load { + unsigned long prev_load; + unsigned long new_task_load; + unsigned long predicted_load; +}; + extern int sched_set_wake_up_idle(struct task_struct *p, int wake_up_idle); extern u32 sched_get_wake_up_idle(struct task_struct *p); extern int sched_set_group_id(struct task_struct *p, unsigned int group_id); extern unsigned int sched_get_group_id(struct task_struct *p); #ifdef CONFIG_SCHED_HMP - +extern int sched_set_window(u64 window_start, unsigned int window_size); +extern unsigned long sched_get_busy(int cpu); +extern void sched_get_cpus_busy(struct sched_load *busy, + const struct cpumask *query_cpus); +extern void sched_set_io_is_busy(int val); extern int sched_set_boost(int enable); extern int sched_set_init_task_load(struct task_struct *p, int init_load_pct); extern u32 sched_get_init_task_load(struct task_struct *p); @@ -2383,6 +2365,19 @@ extern void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, int wakeup_energy, int wakeup_latency); #else /* CONFIG_SCHED_HMP */ +static inline int sched_set_window(u64 window_start, unsigned int window_size) +{ + return -EINVAL; +} +static inline unsigned long sched_get_busy(int cpu) +{ + return 0; +} +static inline void sched_get_cpus_busy(struct sched_load *busy, + const struct cpumask *query_cpus) {}; + +static inline void sched_set_io_is_busy(int val) {}; + static inline int sched_set_boost(int enable) { return -EINVAL; diff --git a/include/linux/sched/sysctl.h b/include/linux/sched/sysctl.h index 4ed8c818f6d5..c60075de415b 100644 --- a/include/linux/sched/sysctl.h +++ b/include/linux/sched/sysctl.h @@ -40,20 +40,14 @@ extern unsigned int sysctl_sched_min_granularity; extern unsigned int sysctl_sched_wakeup_granularity; extern unsigned int sysctl_sched_child_runs_first; extern unsigned int sysctl_sched_wake_to_idle; + +#ifdef CONFIG_SCHED_HMP +extern int sysctl_sched_freq_inc_notify; +extern int sysctl_sched_freq_dec_notify; extern unsigned int sysctl_sched_window_stats_policy; extern unsigned int sysctl_sched_ravg_hist_size; extern unsigned int sysctl_sched_cpu_high_irqload; - -#if defined(CONFIG_SCHED_FREQ_INPUT) || defined(CONFIG_SCHED_HMP) extern unsigned int sysctl_sched_init_task_load_pct; -#endif - -#ifdef CONFIG_SCHED_FREQ_INPUT -extern int sysctl_sched_freq_inc_notify; -extern int sysctl_sched_freq_dec_notify; -#endif - -#ifdef CONFIG_SCHED_HMP extern unsigned int sysctl_sched_spill_nr_run; extern unsigned int sysctl_sched_spill_load_pct; extern unsigned int sysctl_sched_upmigrate_pct; @@ -65,11 +59,9 @@ extern unsigned int sysctl_sched_big_waker_task_load_pct; extern unsigned int sysctl_sched_select_prev_cpu_us; extern unsigned int sysctl_sched_enable_colocation; extern unsigned int sysctl_sched_restrict_cluster_spill; -#if defined(CONFIG_SCHED_FREQ_INPUT) extern unsigned int sysctl_sched_new_task_windows; extern unsigned int sysctl_sched_pred_alert_freq; extern unsigned int sysctl_sched_freq_aggregate; -#endif #else /* CONFIG_SCHED_HMP */ diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index 2c69b5a3c2f4..8367eba91a2e 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -76,9 +76,7 @@ TRACE_EVENT(sched_enq_deq_task, __field(unsigned int, cpus_allowed ) #ifdef CONFIG_SCHED_HMP __field(unsigned int, demand ) -#ifdef CONFIG_SCHED_FREQ_INPUT __field(unsigned int, pred_demand ) -#endif #endif ), @@ -94,18 +92,13 @@ TRACE_EVENT(sched_enq_deq_task, __entry->cpus_allowed = cpus_allowed; #ifdef CONFIG_SCHED_HMP __entry->demand = p->ravg.demand; -#ifdef CONFIG_SCHED_FREQ_INPUT __entry->pred_demand = p->ravg.pred_demand; -#endif #endif ), TP_printk("cpu=%d %s comm=%s pid=%d prio=%d nr_running=%u cpu_load=%lu rt_nr_running=%u affine=%x" #ifdef CONFIG_SCHED_HMP - " demand=%u" -#ifdef CONFIG_SCHED_FREQ_INPUT - " pred_demand=%u" -#endif + " demand=%u pred_demand=%u" #endif , __entry->cpu, __entry->enqueue ? "enqueue" : "dequeue", @@ -113,10 +106,7 @@ TRACE_EVENT(sched_enq_deq_task, __entry->prio, __entry->nr_running, __entry->cpu_load, __entry->rt_nr_running, __entry->cpus_allowed #ifdef CONFIG_SCHED_HMP - , __entry->demand -#ifdef CONFIG_SCHED_FREQ_INPUT - , __entry->pred_demand -#endif + , __entry->demand, __entry->pred_demand #endif ) ); @@ -291,7 +281,6 @@ TRACE_EVENT(sched_update_task_ravg, __field(unsigned int, demand ) __field(unsigned int, sum ) __field( int, cpu ) -#ifdef CONFIG_SCHED_FREQ_INPUT __field(unsigned int, pred_demand ) __field( u64, rq_cs ) __field( u64, rq_ps ) @@ -304,7 +293,6 @@ TRACE_EVENT(sched_update_task_ravg, __field( u64, nt_cs ) __field( u64, nt_ps ) __field( u32, active_windows ) -#endif ), TP_fast_assign( @@ -322,7 +310,6 @@ TRACE_EVENT(sched_update_task_ravg, __entry->demand = p->ravg.demand; __entry->sum = p->ravg.sum; __entry->irqtime = irqtime; -#ifdef CONFIG_SCHED_FREQ_INPUT __entry->pred_demand = p->ravg.pred_demand; __entry->rq_cs = rq->curr_runnable_sum; __entry->rq_ps = rq->prev_runnable_sum; @@ -335,28 +322,19 @@ TRACE_EVENT(sched_update_task_ravg, __entry->nt_cs = rq->nt_curr_runnable_sum; __entry->nt_ps = rq->nt_prev_runnable_sum; __entry->active_windows = p->ravg.active_windows; -#endif ), - TP_printk("wc %llu ws %llu delta %llu event %s cpu %d cur_freq %u cur_pid %d task %d (%s) ms %llu delta %llu demand %u sum %u irqtime %llu" -#ifdef CONFIG_SCHED_FREQ_INPUT - " pred_demand %u rq_cs %llu rq_ps %llu cur_window %u prev_window %u nt_cs %llu nt_ps %llu active_wins %u grp_cs %lld grp_ps %lld, grp_nt_cs %llu, grp_nt_ps: %llu" -#endif + TP_printk("wc %llu ws %llu delta %llu event %s cpu %d cur_freq %u cur_pid %d task %d (%s) ms %llu delta %llu demand %u sum %u irqtime %llu pred_demand %u rq_cs %llu rq_ps %llu cur_window %u prev_window %u nt_cs %llu nt_ps %llu active_wins %u grp_cs %lld grp_ps %lld, grp_nt_cs %llu, grp_nt_ps: %llu" , __entry->wallclock, __entry->win_start, __entry->delta, task_event_names[__entry->evt], __entry->cpu, __entry->cur_freq, __entry->cur_pid, __entry->pid, __entry->comm, __entry->mark_start, __entry->delta_m, __entry->demand, - __entry->sum, __entry->irqtime -#ifdef CONFIG_SCHED_FREQ_INPUT - , __entry->pred_demand, __entry->rq_cs, __entry->rq_ps, - __entry->curr_window, __entry->prev_window, - __entry->nt_cs, __entry->nt_ps, - __entry->active_windows, - __entry->grp_cs, __entry->grp_ps, - __entry->grp_nt_cs, __entry->grp_nt_ps -#endif - ) + __entry->sum, __entry->irqtime, __entry->pred_demand, + __entry->rq_cs, __entry->rq_ps, __entry->curr_window, + __entry->prev_window, __entry->nt_cs, __entry->nt_ps, + __entry->active_windows, __entry->grp_cs, + __entry->grp_ps, __entry->grp_nt_cs, __entry->grp_nt_ps) ); TRACE_EVENT(sched_get_task_cpu_cycles, @@ -402,9 +380,7 @@ TRACE_EVENT(sched_update_history, __field( int, samples ) __field(enum task_event, evt ) __field(unsigned int, demand ) -#ifdef CONFIG_SCHED_FREQ_INPUT __field(unsigned int, pred_demand ) -#endif __array( u32, hist, RAVG_HIST_SIZE_MAX) __field(unsigned int, nr_big_tasks ) __field( int, cpu ) @@ -417,27 +393,19 @@ TRACE_EVENT(sched_update_history, __entry->samples = samples; __entry->evt = evt; __entry->demand = p->ravg.demand; -#ifdef CONFIG_SCHED_FREQ_INPUT __entry->pred_demand = p->ravg.pred_demand; -#endif memcpy(__entry->hist, p->ravg.sum_history, RAVG_HIST_SIZE_MAX * sizeof(u32)); __entry->nr_big_tasks = rq->hmp_stats.nr_big_tasks; __entry->cpu = rq->cpu; ), - TP_printk("%d (%s): runtime %u samples %d event %s demand %u" -#ifdef CONFIG_SCHED_FREQ_INPUT - " pred_demand %u" -#endif + TP_printk("%d (%s): runtime %u samples %d event %s demand %u pred_demand %u" " (hist: %u %u %u %u %u) cpu %d nr_big %u", __entry->pid, __entry->comm, __entry->runtime, __entry->samples, task_event_names[__entry->evt], - __entry->demand, -#ifdef CONFIG_SCHED_FREQ_INPUT - __entry->pred_demand, -#endif + __entry->demand, __entry->pred_demand, __entry->hist[0], __entry->hist[1], __entry->hist[2], __entry->hist[3], __entry->hist[4], __entry->cpu, __entry->nr_big_tasks) @@ -476,8 +444,6 @@ TRACE_EVENT(sched_reset_all_window_stats, __entry->old_val, __entry->new_val) ); -#ifdef CONFIG_SCHED_FREQ_INPUT - TRACE_EVENT(sched_update_pred_demand, TP_PROTO(struct rq *rq, struct task_struct *p, u32 runtime, int pct, @@ -637,8 +603,6 @@ TRACE_EVENT(sched_freq_alert, __entry->old_pred, __entry->new_pred) ); -#endif /* CONFIG_SCHED_FREQ_INPUT */ - #endif /* CONFIG_SCHED_HMP */ /* diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 82a2fe73345e..8d7c13af8c8e 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1777,8 +1777,6 @@ __read_mostly unsigned int sysctl_sched_cpu_high_irqload = (10 * NSEC_PER_MSEC); unsigned int __read_mostly sysctl_sched_enable_colocation = 1; -#ifdef CONFIG_SCHED_FREQ_INPUT - __read_mostly unsigned int sysctl_sched_new_task_windows = 5; #define SCHED_FREQ_ACCOUNT_WAIT_TIME 0 @@ -1799,8 +1797,6 @@ static __read_mostly unsigned int sched_io_is_busy; __read_mostly unsigned int sysctl_sched_pred_alert_freq = 10 * 1024 * 1024; -#endif /* CONFIG_SCHED_FREQ_INPUT */ - /* * Maximum possible frequency across all cpus. Task demand and cpu * capacity (cpu_power) metrics are scaled in reference to it. @@ -1838,9 +1834,7 @@ unsigned int __read_mostly sched_disable_window_stats; * in a window, it's considered to be generating majority of workload * for this window. Prediction could be adjusted for such tasks. */ -#ifdef CONFIG_SCHED_FREQ_INPUT __read_mostly unsigned int sched_major_task_runtime = 10000000; -#endif static unsigned int sync_cpu; @@ -1953,8 +1947,6 @@ static inline u64 scale_exec_time(u64 delta, struct rq *rq) return delta; } -#ifdef CONFIG_SCHED_FREQ_INPUT - static inline int cpu_is_waiting_on_io(struct rq *rq) { if (!sched_io_is_busy) @@ -2671,28 +2663,6 @@ static inline u32 predict_and_update_buckets(struct rq *rq, return pred_demand; } -#define assign_ravg_pred_demand(x) (p->ravg.pred_demand = x) - -#else /* CONFIG_SCHED_FREQ_INPUT */ - -static inline void -update_task_pred_demand(struct rq *rq, struct task_struct *p, int event) -{ -} - -static inline void update_cpu_busy_time(struct task_struct *p, struct rq *rq, - int event, u64 wallclock, u64 irqtime) -{ -} - -static inline u32 predict_and_update_buckets(struct rq *rq, - struct task_struct *p, u32 runtime) -{ - return 0; -} -#define assign_ravg_pred_demand(x) - -#endif /* CONFIG_SCHED_FREQ_INPUT */ static void update_task_cpu_cycles(struct task_struct *p, int cpu) { @@ -2831,7 +2801,7 @@ static void update_history(struct rq *rq, struct task_struct *p, pred_demand); p->ravg.demand = demand; - assign_ravg_pred_demand(pred_demand); + p->ravg.pred_demand = pred_demand; done: trace_sched_update_history(rq, p, runtime, samples, event); @@ -3074,10 +3044,8 @@ static inline void set_window_start(struct rq *rq) raw_spin_unlock(&rq->lock); double_rq_lock(rq, sync_rq); rq->window_start = cpu_rq(sync_cpu)->window_start; -#ifdef CONFIG_SCHED_FREQ_INPUT rq->curr_runnable_sum = rq->prev_runnable_sum = 0; rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; -#endif raw_spin_unlock(&sync_rq->lock); } @@ -3222,10 +3190,8 @@ void reset_all_window_stats(u64 window_start, unsigned int window_size) if (window_start) rq->window_start = window_start; -#ifdef CONFIG_SCHED_FREQ_INPUT rq->curr_runnable_sum = rq->prev_runnable_sum = 0; rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; -#endif reset_cpu_hmp_stats(cpu, 1); } @@ -3240,7 +3206,6 @@ void reset_all_window_stats(u64 window_start, unsigned int window_size) new = sysctl_sched_ravg_hist_size; sched_ravg_hist_size = sysctl_sched_ravg_hist_size; } -#ifdef CONFIG_SCHED_FREQ_INPUT else if (sched_freq_aggregate != sysctl_sched_freq_aggregate) { reason = FREQ_AGGREGATE_CHANGE; @@ -3248,7 +3213,6 @@ void reset_all_window_stats(u64 window_start, unsigned int window_size) new = sysctl_sched_freq_aggregate; sched_freq_aggregate = sysctl_sched_freq_aggregate; } -#endif for_each_possible_cpu(cpu) { struct rq *rq = cpu_rq(cpu); @@ -3263,8 +3227,6 @@ void reset_all_window_stats(u64 window_start, unsigned int window_size) sched_ktime_clock() - start_ts, reason, old, new); } -#ifdef CONFIG_SCHED_FREQ_INPUT - static inline void sync_window_start(struct rq *rq, struct group_cpu_time *cpu_time); @@ -3585,12 +3547,6 @@ done: double_rq_unlock(src_rq, dest_rq); } -#else - -static inline void fixup_busy_time(struct task_struct *p, int new_cpu) { } - -#endif /* CONFIG_SCHED_FREQ_INPUT */ - #define sched_up_down_migrate_auto_update 1 static void check_for_up_down_migrate_update(const struct cpumask *cpus) { @@ -3663,8 +3619,6 @@ static void set_preferred_cluster(struct related_thread_group *grp) #define ADD_TASK 0 #define REM_TASK 1 -#ifdef CONFIG_SCHED_FREQ_INPUT - static void update_task_ravg(struct task_struct *p, struct rq *rq, int event, u64 wallclock, u64 irqtime); @@ -3825,34 +3779,6 @@ _group_cpu_time(struct related_thread_group *grp, int cpu) return grp ? per_cpu_ptr(grp->cpu_time, cpu) : NULL; } -#else /* CONFIG_SCHED_FREQ_INPUT */ - -static inline void free_group_cputime(struct related_thread_group *grp) { } - -static inline int alloc_group_cputime(struct related_thread_group *grp) -{ - return 0; -} - -static inline void transfer_busy_time(struct rq *rq, - struct related_thread_group *grp, struct task_struct *p, int event) -{ -} - -static struct group_cpu_time * -task_group_cpu_time(struct task_struct *p, int cpu) -{ - return NULL; -} - -static inline struct group_cpu_time * -_group_cpu_time(struct related_thread_group *grp, int cpu) -{ - return NULL; -} - -#endif - struct related_thread_group *alloc_related_thread_group(int group_id) { struct related_thread_group *grp; @@ -10951,7 +10877,6 @@ void __init sched_init(void) * like select_best_cpu() */ rq->cluster = &init_cluster; -#ifdef CONFIG_SCHED_FREQ_INPUT rq->curr_runnable_sum = rq->prev_runnable_sum = 0; rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; rq->old_busy_time = 0; @@ -10959,7 +10884,6 @@ void __init sched_init(void) rq->old_busy_time_group = 0; rq->notifier_sent = 0; rq->hmp_stats.pred_demands_sum = 0; -#endif #endif rq->max_idle_balance_cost = sysctl_sched_migration_cost; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 034c9414cabd..0dba600dd28f 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2712,10 +2712,9 @@ void set_hmp_defaults(void) update_up_down_migrate(); -#ifdef CONFIG_SCHED_FREQ_INPUT sched_major_task_runtime = mult_frac(sched_ravg_window, MAJOR_TASK_PCT, 100); -#endif + sched_init_task_load_windows = div64_u64((u64)sysctl_sched_init_task_load_pct * (u64)sched_ravg_window, 100); @@ -3652,7 +3651,7 @@ static void reset_hmp_stats(struct hmp_sched_stats *stats, int reset_cra) stats->nr_big_tasks = 0; if (reset_cra) { stats->cumulative_runnable_avg = 0; - set_pred_demands_sum(stats, 0); + stats->pred_demands_sum = 0; } } @@ -3928,7 +3927,6 @@ void post_big_task_count_change(const struct cpumask *cpus) DEFINE_MUTEX(policy_mutex); -#ifdef CONFIG_SCHED_FREQ_INPUT static inline int invalid_value_freq_input(unsigned int *data) { if (data == &sysctl_sched_freq_aggregate) @@ -3936,12 +3934,6 @@ static inline int invalid_value_freq_input(unsigned int *data) return 0; } -#else -static inline int invalid_value_freq_input(unsigned int *data) -{ - return 0; -} -#endif static inline int invalid_value(unsigned int *data) { @@ -4641,12 +4633,6 @@ dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } #ifdef CONFIG_SCHED_HMP -#ifdef CONFIG_SCHED_FREQ_INPUT -#define clear_ravg_pred_demand() (p->ravg.pred_demand = 0) -#else -#define clear_ravg_pred_demand() -#endif - void init_new_task_load(struct task_struct *p) { int i; @@ -4664,7 +4650,7 @@ void init_new_task_load(struct task_struct *p) (u64)sched_ravg_window, 100); p->ravg.demand = init_load_windows; - clear_ravg_pred_demand(); + p->ravg.pred_demand = 0; for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) p->ravg.sum_history[i] = init_load_windows; } @@ -4695,7 +4681,7 @@ static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { cfs_rq->hmp_stats.nr_big_tasks = 0; cfs_rq->hmp_stats.cumulative_runnable_avg = 0; - set_pred_demands_sum(&cfs_rq->hmp_stats, 0); + cfs_rq->hmp_stats.pred_demands_sum = 0; } static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, @@ -4720,8 +4706,7 @@ static void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, stats->nr_big_tasks += cfs_rq->hmp_stats.nr_big_tasks; stats->cumulative_runnable_avg += cfs_rq->hmp_stats.cumulative_runnable_avg; - set_pred_demands_sum(stats, stats->pred_demands_sum + - cfs_rq->hmp_stats.pred_demands_sum); + stats->pred_demands_sum += cfs_rq->hmp_stats.pred_demands_sum; } static void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, @@ -4730,8 +4715,7 @@ static void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, stats->nr_big_tasks -= cfs_rq->hmp_stats.nr_big_tasks; stats->cumulative_runnable_avg -= cfs_rq->hmp_stats.cumulative_runnable_avg; - set_pred_demands_sum(stats, stats->pred_demands_sum - - cfs_rq->hmp_stats.pred_demands_sum); + stats->pred_demands_sum -= cfs_rq->hmp_stats.pred_demands_sum; BUG_ON(stats->nr_big_tasks < 0 || (s64)stats->cumulative_runnable_avg < 0); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index b45c88735ea6..414c46d20f41 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -366,9 +366,7 @@ struct cfs_bandwidth { }; struct hmp_sched_stats { int nr_big_tasks; u64 cumulative_runnable_avg; -#ifdef CONFIG_SCHED_FREQ_INPUT u64 pred_demands_sum; -#endif }; struct sched_cluster { @@ -409,16 +407,12 @@ struct related_thread_group { struct sched_cluster *preferred_cluster; struct rcu_head rcu; u64 last_update; -#ifdef CONFIG_SCHED_FREQ_INPUT struct group_cpu_time __percpu *cpu_time; /* one per cluster */ -#endif }; struct migration_sum_data { struct rq *src_rq, *dst_rq; -#ifdef CONFIG_SCHED_FREQ_INPUT struct group_cpu_time *src_cpu_time, *dst_cpu_time; -#endif }; extern struct list_head cluster_head; @@ -435,7 +429,7 @@ struct cpu_cycle { #define for_each_sched_cluster(cluster) \ list_for_each_entry_rcu(cluster, &cluster_head, list) -#endif +#endif /* CONFIG_SCHED_HMP */ /* CFS-related fields in a runqueue */ struct cfs_rq { @@ -755,15 +749,9 @@ struct rq { unsigned int static_cpu_pwr_cost; struct task_struct *ed_task; struct cpu_cycle cc; - -#ifdef CONFIG_SCHED_FREQ_INPUT u64 old_busy_time, old_busy_time_group; int notifier_sent; u64 old_estimated_time; -#endif -#endif - -#ifdef CONFIG_SCHED_FREQ_INPUT u64 curr_runnable_sum; u64 prev_runnable_sum; u64 nt_curr_runnable_sum; @@ -1045,6 +1033,8 @@ extern void init_new_task_load(struct task_struct *p); #define WINDOW_STATS_AVG 3 #define WINDOW_STATS_INVALID_POLICY 4 +#define MAJOR_TASK_PCT 85 + extern struct mutex policy_mutex; extern unsigned int sched_ravg_window; extern unsigned int sched_disable_window_stats; @@ -1065,11 +1055,7 @@ extern unsigned int sched_init_task_load_windows; extern unsigned int up_down_migrate_scale_factor; extern unsigned int sysctl_sched_restrict_cluster_spill; extern unsigned int sched_pred_alert_load; - -#ifdef CONFIG_SCHED_FREQ_INPUT -#define MAJOR_TASK_PCT 85 extern unsigned int sched_major_task_runtime; -#endif extern void reset_cpu_hmp_stats(int cpu, int reset_cra); extern unsigned int max_task_load(void); @@ -1180,14 +1166,6 @@ static inline unsigned int task_load(struct task_struct *p) return p->ravg.demand; } -#ifdef CONFIG_SCHED_FREQ_INPUT -#define set_pred_demands_sum(stats, x) ((stats)->pred_demands_sum = (x)) -#define verify_pred_demands_sum(stat) BUG_ON((s64)(stat)->pred_demands_sum < 0) -#else -#define set_pred_demands_sum(stats, x) -#define verify_pred_demands_sum(stat) -#endif - static inline void inc_cumulative_runnable_avg(struct hmp_sched_stats *stats, struct task_struct *p) @@ -1200,13 +1178,12 @@ inc_cumulative_runnable_avg(struct hmp_sched_stats *stats, task_load = sched_disable_window_stats ? 0 : p->ravg.demand; stats->cumulative_runnable_avg += task_load; - set_pred_demands_sum(stats, stats->pred_demands_sum + - p->ravg.pred_demand); + stats->pred_demands_sum += p->ravg.pred_demand; } static inline void dec_cumulative_runnable_avg(struct hmp_sched_stats *stats, - struct task_struct *p) + struct task_struct *p) { u32 task_load; @@ -1219,9 +1196,8 @@ dec_cumulative_runnable_avg(struct hmp_sched_stats *stats, BUG_ON((s64)stats->cumulative_runnable_avg < 0); - set_pred_demands_sum(stats, stats->pred_demands_sum - - p->ravg.pred_demand); - verify_pred_demands_sum(stats); + stats->pred_demands_sum -= p->ravg.pred_demand; + BUG_ON((s64)stats->pred_demands_sum < 0); } static inline void @@ -1235,12 +1211,10 @@ fixup_cumulative_runnable_avg(struct hmp_sched_stats *stats, stats->cumulative_runnable_avg += task_load_delta; BUG_ON((s64)stats->cumulative_runnable_avg < 0); - set_pred_demands_sum(stats, stats->pred_demands_sum + - pred_demand_delta); - verify_pred_demands_sum(stats); + stats->pred_demands_sum += pred_demand_delta; + BUG_ON((s64)stats->pred_demands_sum < 0); } - #define pct_to_real(tunable) \ (div64_u64((u64)tunable * (u64)max_task_load(), 100)) @@ -1278,6 +1252,87 @@ struct related_thread_group *task_related_thread_group(struct task_struct *p) return rcu_dereference(p->grp); } +#define PRED_DEMAND_DELTA ((s64)new_pred_demand - p->ravg.pred_demand) + +extern void +check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups); + +struct group_cpu_time { + u64 curr_runnable_sum; + u64 prev_runnable_sum; + u64 nt_curr_runnable_sum; + u64 nt_prev_runnable_sum; + u64 window_start; +}; + +/* Is frequency of two cpus synchronized with each other? */ +static inline int same_freq_domain(int src_cpu, int dst_cpu) +{ + struct rq *rq = cpu_rq(src_cpu); + + if (src_cpu == dst_cpu) + return 1; + + return cpumask_test_cpu(dst_cpu, &rq->freq_domain_cpumask); +} + +#define BOOST_KICK 0 +#define CPU_RESERVED 1 + +static inline int is_reserved(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + return test_bit(CPU_RESERVED, &rq->hmp_flags); +} + +static inline int mark_reserved(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + /* Name boost_flags as hmp_flags? */ + return test_and_set_bit(CPU_RESERVED, &rq->hmp_flags); +} + +static inline void clear_reserved(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + clear_bit(CPU_RESERVED, &rq->hmp_flags); +} + +static inline u64 cpu_cravg_sync(int cpu, int sync) +{ + struct rq *rq = cpu_rq(cpu); + u64 load; + + load = rq->hmp_stats.cumulative_runnable_avg; + + /* + * If load is being checked in a sync wakeup environment, + * we may want to discount the load of the currently running + * task. + */ + if (sync && cpu == smp_processor_id()) { + if (load > rq->curr->ravg.demand) + load -= rq->curr->ravg.demand; + else + load = 0; + } + + return load; +} + +extern void check_for_migration(struct rq *rq, struct task_struct *p); +extern void pre_big_task_count_change(const struct cpumask *cpus); +extern void post_big_task_count_change(const struct cpumask *cpus); +extern void set_hmp_defaults(void); +extern int power_delta_exceeded(unsigned int cpu_cost, unsigned int base_cost); +extern unsigned int power_cost(int cpu, u64 demand); +extern void reset_all_window_stats(u64 window_start, unsigned int window_size); +extern void boost_kick(int cpu); +extern int sched_boost(void); + #else /* CONFIG_SCHED_HMP */ struct hmp_sched_stats; @@ -1340,41 +1395,8 @@ static inline int update_preferred_cluster(struct related_thread_group *grp, return 0; } -#endif /* CONFIG_SCHED_HMP */ - -/* - * Returns the rq capacity of any rq in a group. This does not play - * well with groups where rq capacity can change independently. - */ -#define group_rq_capacity(group) cpu_capacity(group_first_cpu(group)) - -#ifdef CONFIG_SCHED_FREQ_INPUT -#define PRED_DEMAND_DELTA ((s64)new_pred_demand - p->ravg.pred_demand) - -extern void -check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups); - -struct group_cpu_time { - u64 curr_runnable_sum; - u64 prev_runnable_sum; - u64 nt_curr_runnable_sum; - u64 nt_prev_runnable_sum; - u64 window_start; -}; - -/* Is frequency of two cpus synchronized with each other? */ -static inline int same_freq_domain(int src_cpu, int dst_cpu) -{ - struct rq *rq = cpu_rq(src_cpu); - - if (src_cpu == dst_cpu) - return 1; - - return cpumask_test_cpu(dst_cpu, &rq->freq_domain_cpumask); -} - -#else /* CONFIG_SCHED_FREQ_INPUT */ - +#define sched_enable_hmp 0 +#define sched_freq_legacy_mode 1 #define sched_migration_fixup 0 #define PRED_DEMAND_DELTA (0) @@ -1386,72 +1408,6 @@ static inline int same_freq_domain(int src_cpu, int dst_cpu) return 1; } -#endif /* CONFIG_SCHED_FREQ_INPUT */ - -#ifdef CONFIG_SCHED_HMP - -#define BOOST_KICK 0 -#define CPU_RESERVED 1 - -static inline int is_reserved(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - return test_bit(CPU_RESERVED, &rq->hmp_flags); -} - -static inline int mark_reserved(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - /* Name boost_flags as hmp_flags? */ - return test_and_set_bit(CPU_RESERVED, &rq->hmp_flags); -} - -static inline void clear_reserved(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - clear_bit(CPU_RESERVED, &rq->hmp_flags); -} - -static inline u64 cpu_cravg_sync(int cpu, int sync) -{ - struct rq *rq = cpu_rq(cpu); - u64 load; - - load = rq->hmp_stats.cumulative_runnable_avg; - - /* - * If load is being checked in a sync wakeup environment, - * we may want to discount the load of the currently running - * task. - */ - if (sync && cpu == smp_processor_id()) { - if (load > rq->curr->ravg.demand) - load -= rq->curr->ravg.demand; - else - load = 0; - } - - return load; -} - -extern void check_for_migration(struct rq *rq, struct task_struct *p); -extern void pre_big_task_count_change(const struct cpumask *cpus); -extern void post_big_task_count_change(const struct cpumask *cpus); -extern void set_hmp_defaults(void); -extern int power_delta_exceeded(unsigned int cpu_cost, unsigned int base_cost); -extern unsigned int power_cost(int cpu, u64 demand); -extern void reset_all_window_stats(u64 window_start, unsigned int window_size); -extern void boost_kick(int cpu); -extern int sched_boost(void); - -#else /* CONFIG_SCHED_HMP */ - -#define sched_enable_hmp 0 -#define sched_freq_legacy_mode 1 - static inline void check_for_migration(struct rq *rq, struct task_struct *p) { } static inline void pre_big_task_count_change(void) { } static inline void post_big_task_count_change(void) { } @@ -1464,7 +1420,13 @@ static inline void clear_reserved(int cpu) { } #define trace_sched_cpu_load_cgroup(...) #define trace_sched_cpu_load_wakeup(...) -#endif /* CONFIG_SCHED_HMP */ +#endif /* CONFIG_SCHED_HMP */ + +/* + * Returns the rq capacity of any rq in a group. This does not play + * well with groups where rq capacity can change independently. + */ +#define group_rq_capacity(group) cpu_capacity(group_first_cpu(group)) #ifdef CONFIG_CGROUP_SCHED diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 2e77841e4eda..8b9ca50dc53f 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -292,7 +292,7 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = proc_dointvec, }, -#ifdef CONFIG_SCHED_FREQ_INPUT +#ifdef CONFIG_SCHED_HMP { .procname = "sched_freq_inc_notify", .data = &sysctl_sched_freq_inc_notify, @@ -309,8 +309,6 @@ static struct ctl_table kern_table[] = { .proc_handler = proc_dointvec_minmax, .extra1 = &zero, }, -#endif -#ifdef CONFIG_SCHED_HMP { .procname = "sched_cpu_high_irqload", .data = &sysctl_sched_cpu_high_irqload, @@ -407,7 +405,6 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = sched_hmp_proc_update_handler, }, -#ifdef CONFIG_SCHED_FREQ_INPUT { .procname = "sched_new_task_windows", .data = &sysctl_sched_new_task_windows, @@ -430,7 +427,6 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = sched_window_update_handler, }, -#endif { .procname = "sched_boost", .data = &sysctl_sched_boost, From f81e5286fbd9636488a78c9c3a736ca1a2e3194b Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Wed, 10 Aug 2016 11:52:45 -0700 Subject: [PATCH 08/16] defconfig: Remove CONFIG_SCHED_FREQ_INPUT from all defconfigs The config option has been removed. Update all defconfigs to reflect that. Change-Id: Ia1aa0405e8d7c7c48fe8023691b57fba00c6a22b Signed-off-by: Syed Rameez Mustafa --- arch/arm/configs/msmcortex_defconfig | 1 - arch/arm/configs/msmfalcon_defconfig | 1 - arch/arm64/configs/msm-perf_defconfig | 1 - arch/arm64/configs/msm_defconfig | 1 - arch/arm64/configs/msmcortex-perf_defconfig | 1 - arch/arm64/configs/msmcortex_defconfig | 1 - arch/arm64/configs/msmfalcon-perf_defconfig | 1 - arch/arm64/configs/msmfalcon_defconfig | 1 - 8 files changed, 8 deletions(-) diff --git a/arch/arm/configs/msmcortex_defconfig b/arch/arm/configs/msmcortex_defconfig index 013bb87ecbbe..1e6a1c66ed82 100644 --- a/arch/arm/configs/msmcortex_defconfig +++ b/arch/arm/configs/msmcortex_defconfig @@ -51,7 +51,6 @@ CONFIG_CMA=y CONFIG_ZSMALLOC=y CONFIG_SECCOMP=y CONFIG_BUILD_ARM_APPENDED_DTB_IMAGE=y -CONFIG_SCHED_FREQ_INPUT=y # CONFIG_CPU_FREQ_STAT is not set CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y diff --git a/arch/arm/configs/msmfalcon_defconfig b/arch/arm/configs/msmfalcon_defconfig index 013bb87ecbbe..1e6a1c66ed82 100644 --- a/arch/arm/configs/msmfalcon_defconfig +++ b/arch/arm/configs/msmfalcon_defconfig @@ -51,7 +51,6 @@ CONFIG_CMA=y CONFIG_ZSMALLOC=y CONFIG_SECCOMP=y CONFIG_BUILD_ARM_APPENDED_DTB_IMAGE=y -CONFIG_SCHED_FREQ_INPUT=y # CONFIG_CPU_FREQ_STAT is not set CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y diff --git a/arch/arm64/configs/msm-perf_defconfig b/arch/arm64/configs/msm-perf_defconfig index 9ded3e3d5ff4..d7e78430fe7f 100644 --- a/arch/arm64/configs/msm-perf_defconfig +++ b/arch/arm64/configs/msm-perf_defconfig @@ -64,7 +64,6 @@ CONFIG_PM_WAKELOCKS=y CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y CONFIG_CPU_FREQ_GOV_ONDEMAND=y diff --git a/arch/arm64/configs/msm_defconfig b/arch/arm64/configs/msm_defconfig index f4b10dcd45cb..9d4228923595 100644 --- a/arch/arm64/configs/msm_defconfig +++ b/arch/arm64/configs/msm_defconfig @@ -64,7 +64,6 @@ CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_PM_DEBUG=y CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y CONFIG_CPU_FREQ_GOV_ONDEMAND=y diff --git a/arch/arm64/configs/msmcortex-perf_defconfig b/arch/arm64/configs/msmcortex-perf_defconfig index 40a054eca640..f2eafd610cac 100644 --- a/arch/arm64/configs/msmcortex-perf_defconfig +++ b/arch/arm64/configs/msmcortex-perf_defconfig @@ -70,7 +70,6 @@ CONFIG_PM_WAKELOCKS=y CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y CONFIG_CPU_FREQ_GOV_ONDEMAND=y diff --git a/arch/arm64/configs/msmcortex_defconfig b/arch/arm64/configs/msmcortex_defconfig index 0ccb7e243ec0..3742fe210dc2 100644 --- a/arch/arm64/configs/msmcortex_defconfig +++ b/arch/arm64/configs/msmcortex_defconfig @@ -69,7 +69,6 @@ CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_PM_DEBUG=y CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y # CONFIG_CPU_FREQ_STAT is not set CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y diff --git a/arch/arm64/configs/msmfalcon-perf_defconfig b/arch/arm64/configs/msmfalcon-perf_defconfig index 40a054eca640..f2eafd610cac 100644 --- a/arch/arm64/configs/msmfalcon-perf_defconfig +++ b/arch/arm64/configs/msmfalcon-perf_defconfig @@ -70,7 +70,6 @@ CONFIG_PM_WAKELOCKS=y CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y CONFIG_CPU_FREQ_GOV_ONDEMAND=y diff --git a/arch/arm64/configs/msmfalcon_defconfig b/arch/arm64/configs/msmfalcon_defconfig index 0ccb7e243ec0..3742fe210dc2 100644 --- a/arch/arm64/configs/msmfalcon_defconfig +++ b/arch/arm64/configs/msmfalcon_defconfig @@ -69,7 +69,6 @@ CONFIG_PM_WAKELOCKS_LIMIT=0 # CONFIG_PM_WAKELOCKS_GC is not set CONFIG_PM_DEBUG=y CONFIG_CPU_FREQ=y -CONFIG_SCHED_FREQ_INPUT=y # CONFIG_CPU_FREQ_STAT is not set CONFIG_CPU_FREQ_GOV_POWERSAVE=y CONFIG_CPU_FREQ_GOV_USERSPACE=y From b01a93838d1ff0caf8057f852c437f95e798ccc6 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Fri, 29 Jul 2016 15:56:29 -0700 Subject: [PATCH 09/16] sched: Fix compile issues for !CONFIG_SCHED_HMP Fix compile issues observed when CONFIG_SCHED_HMP is not turned on. There are still targets that may want that config option turned off. Change-Id: I29e69356da8d003d13d8cd3927a0b166cc1ef95e Signed-off-by: Syed Rameez Mustafa --- kernel/sched/core.c | 2 +- kernel/sched/fair.c | 5 ++--- kernel/sched/sched.h | 10 ++++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 8d7c13af8c8e..f217924c10f2 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4263,7 +4263,7 @@ static void notify_migration(int src_cpu, int dest_cpu, bool src_cpu_dead, bool check_groups; rcu_read_lock(); - check_groups = rcu_access_pointer(p->grp) != NULL; + check_groups = task_in_related_thread_group(p); rcu_read_unlock(); if (!same_freq_domain(src_cpu, dest_cpu)) { diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 0dba600dd28f..a4f3af6fc175 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4182,8 +4182,7 @@ unsigned int cpu_temp(int cpu) struct cpu_select_env; struct sched_cluster; -static inline int task_will_fit(struct task_struct *p, int cpu, - enum sched_boost_type boost_type) +static inline int task_will_fit(struct task_struct *p, int cpu) { return 1; } @@ -7700,7 +7699,7 @@ static void detach_task(struct task_struct *p, struct lb_env *env) deactivate_task(env->src_rq, p, 0); double_lock_balance(env->src_rq, env->dst_rq); set_task_cpu(p, env->dst_cpu); - if (rcu_access_pointer(p->grp)) + if (task_in_related_thread_group(p)) env->flags |= LBF_MOVED_RELATED_THREAD_GROUP_TASK; double_unlock_balance(env->src_rq, env->dst_rq); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 414c46d20f41..af1d33ad82e6 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1246,6 +1246,11 @@ static inline int sched_cpu_high_irqload(int cpu) return sched_irqload(cpu) >= sysctl_sched_cpu_high_irqload; } +static inline bool task_in_related_thread_group(struct task_struct *p) +{ + return !!(rcu_access_pointer(p->grp) != NULL); +} + static inline struct related_thread_group *task_related_thread_group(struct task_struct *p) { @@ -1381,6 +1386,11 @@ static inline int sched_cpu_high_irqload(int cpu) { return 0; } static inline void set_preferred_cluster(struct related_thread_group *grp) { } +static inline bool task_in_related_thread_group(struct task_struct *p) +{ + return false; +} + static inline struct related_thread_group *task_related_thread_group(struct task_struct *p) { From 7663fb1d6e7f417c1127f8010eddf080b4fc6a24 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Thu, 28 Jul 2016 19:45:56 -0700 Subject: [PATCH 10/16] sched: Consolidate CONFIG_SCHED_HMP sections in various files Code sections found either CONFIG_SCHED_HMP or !CONFIG_SCHED_HMP have become quite fragmented over time. Some of these fragmented sections are necessary because of the code dependencies. Others fragmented sections can easily be consolidated. Do so in order to make kernel upgrades a lot simpler. Change-Id: I6be476834ce70274aec5a52fd9455b5f0065af87 Signed-off-by: Syed Rameez Mustafa --- kernel/sched/core.c | 113 +++++++++----------- kernel/sched/debug.c | 2 - kernel/sched/fair.c | 242 +++++++++++++++++++++---------------------- 3 files changed, 164 insertions(+), 193 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index f217924c10f2..20b42f8d6f67 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1591,7 +1591,6 @@ static void add_cluster(const struct cpumask *cpus, struct list_head *head) num_clusters++; } -#ifdef CONFIG_SMP static void update_cluster_topology(void) { struct cpumask cpus = *cpu_possible_mask; @@ -1616,7 +1615,6 @@ static void update_cluster_topology(void) */ move_list(&cluster_head, &new_head, false); } -#endif static void init_clusters(void) { @@ -1722,32 +1720,6 @@ unsigned int sched_get_static_cluster_pwr_cost(int cpu) return cpu_rq(cpu)->cluster->static_cluster_pwr_cost; } -#else /* CONFIG_SCHED_HMP */ - -static inline int got_boost_kick(void) -{ - return 0; -} - -static inline void clear_boost_kick(int cpu) { } - -static inline void clear_hmp_request(int cpu) { } - -int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) -{ - return 0; -} - -#ifdef CONFIG_SMP -static void update_cluster_topology(void) { } -#endif - -#endif /* CONFIG_SCHED_HMP */ - -#define SCHED_MIN_FREQ 1 - -#if defined(CONFIG_SCHED_HMP) - /* * sched_window_stats_policy and sched_ravg_hist_size have a 'sysctl' copy * associated with them. This is required for atomic update of those variables @@ -4166,9 +4138,50 @@ static inline int update_preferred_cluster(struct related_thread_group *grp, return 0; } -#else /* CONFIG_SCHED_HMP */ +static bool early_detection_notify(struct rq *rq, u64 wallclock) +{ + struct task_struct *p; + int loop_max = 10; + + if (!sched_boost() || !rq->cfs.h_nr_running) + return 0; + + rq->ed_task = NULL; + list_for_each_entry(p, &rq->cfs_tasks, se.group_node) { + if (!loop_max) + break; + + if (wallclock - p->last_wake_ts >= EARLY_DETECTION_DURATION) { + rq->ed_task = p; + return 1; + } + + loop_max--; + } + + return 0; +} + +#else /* CONFIG_SCHED_HMP */ static inline void fixup_busy_time(struct task_struct *p, int new_cpu) { } +static inline void clear_boost_kick(int cpu) { } +static inline void clear_hmp_request(int cpu) { } +static inline void mark_task_starting(struct task_struct *p) {} +static inline void set_window_start(struct rq *rq) {} +static inline void migrate_sync_cpu(int cpu) {} + +static inline int got_boost_kick(void) +{ + return 0; +} + +int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) +{ + return 0; +} + +static inline void update_cluster_topology(void) { } static void update_task_ravg(struct task_struct *p, struct rq *rq, @@ -4176,11 +4189,10 @@ update_task_ravg(struct task_struct *p, struct rq *rq, { } -static inline void mark_task_starting(struct task_struct *p) {} - -static inline void set_window_start(struct rq *rq) {} - -static inline void migrate_sync_cpu(int cpu) {} +static bool early_detection_notify(struct rq *rq, u64 wallclock) +{ + return 0; +} #endif /* CONFIG_SCHED_HMP */ @@ -6128,37 +6140,6 @@ unsigned long long task_sched_runtime(struct task_struct *p) return ns; } -#ifdef CONFIG_SCHED_HMP -static bool early_detection_notify(struct rq *rq, u64 wallclock) -{ - struct task_struct *p; - int loop_max = 10; - - if (!sched_boost() || !rq->cfs.h_nr_running) - return 0; - - rq->ed_task = NULL; - list_for_each_entry(p, &rq->cfs_tasks, se.group_node) { - if (!loop_max) - break; - - if (wallclock - p->last_wake_ts >= EARLY_DETECTION_DURATION) { - rq->ed_task = p; - return 1; - } - - loop_max--; - } - - return 0; -} -#else /* CONFIG_SCHED_HMP */ -static bool early_detection_notify(struct rq *rq, u64 wallclock) -{ - return 0; -} -#endif /* CONFIG_SCHED_HMP */ - /* * This function gets called by the timer code, with HZ frequency. * We call it with interrupts disabled. @@ -10865,7 +10846,7 @@ void __init sched_init(void) rq->avg_irqload = 0; rq->irqload_ts = 0; rq->static_cpu_pwr_cost = 0; - rq->cc.cycles = SCHED_MIN_FREQ; + rq->cc.cycles = 1; rq->cc.time = 1; rq->cstate = 0; rq->wakeup_latency = 0; diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index fc9878eee5df..b6dc131f36a6 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -327,8 +327,6 @@ do { \ P(cluster->cur_freq); P(cluster->max_freq); P(cluster->exec_scale_factor); -#endif -#ifdef CONFIG_SCHED_HMP P(hmp_stats.nr_big_tasks); SEQ_printf(m, " .%-30s: %llu\n", "hmp_stats.cumulative_runnable_avg", rq->hmp_stats.cumulative_runnable_avg); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a4f3af6fc175..4806ec37035d 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4177,6 +4177,95 @@ unsigned int cpu_temp(int cpu) return 0; } +void init_new_task_load(struct task_struct *p) +{ + int i; + u32 init_load_windows = sched_init_task_load_windows; + u32 init_load_pct = current->init_load_pct; + + p->init_load_pct = 0; + rcu_assign_pointer(p->grp, NULL); + INIT_LIST_HEAD(&p->grp_list); + memset(&p->ravg, 0, sizeof(struct ravg)); + p->cpu_cycles = 0; + + if (init_load_pct) + init_load_windows = div64_u64((u64)init_load_pct * + (u64)sched_ravg_window, 100); + + p->ravg.demand = init_load_windows; + p->ravg.pred_demand = 0; + for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) + p->ravg.sum_history[i] = init_load_windows; +} + +/* Return task demand in percentage scale */ +unsigned int pct_task_load(struct task_struct *p) +{ + unsigned int load; + + load = div64_u64((u64)task_load(p) * 100, (u64)max_task_load()); + + return load; +} + +#ifdef CONFIG_CFS_BANDWIDTH + +static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) +{ + cfs_rq->hmp_stats.nr_big_tasks = 0; + cfs_rq->hmp_stats.cumulative_runnable_avg = 0; + cfs_rq->hmp_stats.pred_demands_sum = 0; +} + +static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) +{ + inc_nr_big_task(&cfs_rq->hmp_stats, p); + if (change_cra) + inc_cumulative_runnable_avg(&cfs_rq->hmp_stats, p); +} + +static void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) +{ + dec_nr_big_task(&cfs_rq->hmp_stats, p); + if (change_cra) + dec_cumulative_runnable_avg(&cfs_rq->hmp_stats, p); +} + +static void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, + struct cfs_rq *cfs_rq) +{ + stats->nr_big_tasks += cfs_rq->hmp_stats.nr_big_tasks; + stats->cumulative_runnable_avg += + cfs_rq->hmp_stats.cumulative_runnable_avg; + stats->pred_demands_sum += cfs_rq->hmp_stats.pred_demands_sum; +} + +static void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, + struct cfs_rq *cfs_rq) +{ + stats->nr_big_tasks -= cfs_rq->hmp_stats.nr_big_tasks; + stats->cumulative_runnable_avg -= + cfs_rq->hmp_stats.cumulative_runnable_avg; + stats->pred_demands_sum -= cfs_rq->hmp_stats.pred_demands_sum; + + BUG_ON(stats->nr_big_tasks < 0 || + (s64)stats->cumulative_runnable_avg < 0); + verify_pred_demands_sum(stats); +} + +#else /* CONFIG_CFS_BANDWIDTH */ + +static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +#endif /* CONFIG_CFS_BANDWIDTH */ + #else /* CONFIG_SCHED_HMP */ struct cpu_select_env; @@ -4256,10 +4345,28 @@ static inline struct sched_cluster *rq_cluster(struct rq *rq) return NULL; } +void init_new_task_load(struct task_struct *p) { } + +static inline void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { } + +static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +static inline void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, + struct cfs_rq *cfs_rq) +{ +} + +static inline void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, + struct cfs_rq *cfs_rq) +{ +} + #endif /* CONFIG_SCHED_HMP */ - - #if (SCHED_LOAD_SHIFT - SCHED_LOAD_RESOLUTION) != 10 || SCHED_CAPACITY_SHIFT != 10 #error "load tracking assumes 2^10 as unit" #endif @@ -4628,131 +4735,16 @@ inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } static inline void dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } +static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, + struct task_struct *p, int change_cra) { } + +void init_new_task_load(struct task_struct *p) { } + #endif /* CONFIG_SMP */ -#ifdef CONFIG_SCHED_HMP - -void init_new_task_load(struct task_struct *p) -{ - int i; - u32 init_load_windows = sched_init_task_load_windows; - u32 init_load_pct = current->init_load_pct; - - p->init_load_pct = 0; - rcu_assign_pointer(p->grp, NULL); - INIT_LIST_HEAD(&p->grp_list); - memset(&p->ravg, 0, sizeof(struct ravg)); - p->cpu_cycles = 0; - - if (init_load_pct) - init_load_windows = div64_u64((u64)init_load_pct * - (u64)sched_ravg_window, 100); - - p->ravg.demand = init_load_windows; - p->ravg.pred_demand = 0; - for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) - p->ravg.sum_history[i] = init_load_windows; -} - -#else /* CONFIG_SCHED_HMP */ - -void init_new_task_load(struct task_struct *p) -{ -} - -#endif /* CONFIG_SCHED_HMP */ - -#ifdef CONFIG_SCHED_HMP - -/* Return task demand in percentage scale */ -unsigned int pct_task_load(struct task_struct *p) -{ - unsigned int load; - - load = div64_u64((u64)task_load(p) * 100, (u64)max_task_load()); - - return load; -} - -#ifdef CONFIG_CFS_BANDWIDTH - -static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) -{ - cfs_rq->hmp_stats.nr_big_tasks = 0; - cfs_rq->hmp_stats.cumulative_runnable_avg = 0; - cfs_rq->hmp_stats.pred_demands_sum = 0; -} - -static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) -{ - inc_nr_big_task(&cfs_rq->hmp_stats, p); - if (change_cra) - inc_cumulative_runnable_avg(&cfs_rq->hmp_stats, p); -} - -static void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) -{ - dec_nr_big_task(&cfs_rq->hmp_stats, p); - if (change_cra) - dec_cumulative_runnable_avg(&cfs_rq->hmp_stats, p); -} - -static void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, - struct cfs_rq *cfs_rq) -{ - stats->nr_big_tasks += cfs_rq->hmp_stats.nr_big_tasks; - stats->cumulative_runnable_avg += - cfs_rq->hmp_stats.cumulative_runnable_avg; - stats->pred_demands_sum += cfs_rq->hmp_stats.pred_demands_sum; -} - -static void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, - struct cfs_rq *cfs_rq) -{ - stats->nr_big_tasks -= cfs_rq->hmp_stats.nr_big_tasks; - stats->cumulative_runnable_avg -= - cfs_rq->hmp_stats.cumulative_runnable_avg; - stats->pred_demands_sum -= cfs_rq->hmp_stats.pred_demands_sum; - - BUG_ON(stats->nr_big_tasks < 0 || - (s64)stats->cumulative_runnable_avg < 0); - verify_pred_demands_sum(stats); -} - -#else /* CONFIG_CFS_BANDWIDTH */ - -static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) { } - -static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) { } - -#endif /* CONFIG_CFS_BANDWIDTH */ - -#else /* CONFIG_SCHED_HMP */ - -static inline void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { } - -static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) { } - -static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, - struct task_struct *p, int change_cra) { } - -static inline void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, - struct cfs_rq *cfs_rq) -{ -} - -static inline void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats, - struct cfs_rq *cfs_rq) -{ -} - -#endif /* CONFIG_SCHED_HMP */ - static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) { #ifdef CONFIG_SCHEDSTATS From 9095a09ab1d2bd5b1ab123d4714003ee77aa4fa1 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Mon, 1 Aug 2016 17:48:21 -0700 Subject: [PATCH 11/16] sched: Move most HMP specific code to a separate file. Most code pertaining to CONFIG_SCHED_HMP has been moved to a separate file "hmp.c" in order to facilitate kernel upgrades. Fewer changes in the original scheduler files means fewer conflicts. Some parts of code, however, could not be moved to the separate file either because of dependencies with other non-HMP code or because the changes are specific only to the scheduling classes where the code resides. Change-Id: Ib067ac75e5a494008dcb3c67586b622c1b3962ce Signed-off-by: Syed Rameez Mustafa --- include/linux/sched.h | 28 +- kernel/sched/Makefile | 1 + kernel/sched/core.c | 3214 +-------------------------------- kernel/sched/fair.c | 894 +--------- kernel/sched/hmp.c | 3898 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 181 +- 6 files changed, 4142 insertions(+), 4074 deletions(-) create mode 100644 kernel/sched/hmp.c diff --git a/include/linux/sched.h b/include/linux/sched.h index d7f67d0b9644..74b2a11b1d1c 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2340,8 +2340,10 @@ struct sched_load { extern int sched_set_wake_up_idle(struct task_struct *p, int wake_up_idle); extern u32 sched_get_wake_up_idle(struct task_struct *p); -extern int sched_set_group_id(struct task_struct *p, unsigned int group_id); -extern unsigned int sched_get_group_id(struct task_struct *p); + +struct cpu_cycle_counter_cb { + u64 (*get_cpu_cycle_counter)(int cpu); +}; #ifdef CONFIG_SCHED_HMP extern int sched_set_window(u64 window_start, unsigned int window_size); @@ -2363,8 +2365,23 @@ extern void sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency); extern void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, int wakeup_energy, int wakeup_latency); +extern int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb); +extern u64 sched_ktime_clock(void); +extern int sched_set_group_id(struct task_struct *p, unsigned int group_id); +extern unsigned int sched_get_group_id(struct task_struct *p); #else /* CONFIG_SCHED_HMP */ +static inline u64 sched_ktime_clock(void) +{ + return 0; +} + +static inline int +register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) +{ + return 0; +} + static inline int sched_set_window(u64 window_start, unsigned int window_size) { return -EINVAL; @@ -2435,8 +2452,6 @@ extern u64 local_clock(void); extern u64 running_clock(void); extern u64 sched_clock_cpu(int cpu); -extern u64 sched_ktime_clock(void); - extern void sched_clock_init(void); extern int sched_clock_initialized(void); @@ -3373,9 +3388,4 @@ static inline unsigned long rlimit_max(unsigned int limit) return task_rlimit_max(current, limit); } -struct cpu_cycle_counter_cb { - u64 (*get_cpu_cycle_counter)(int cpu); -}; -int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb); - #endif diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile index 846c15156616..1f159743ebfc 100644 --- a/kernel/sched/Makefile +++ b/kernel/sched/Makefile @@ -15,6 +15,7 @@ obj-y += core.o loadavg.o clock.o cputime.o obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o obj-y += wait.o completion.o idle.o sched_avg.o obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o +obj-$(CONFIG_SCHED_HMP) += hmp.o obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o obj-$(CONFIG_SCHEDSTATS) += stats.o obj-$(CONFIG_SCHED_DEBUG) += debug.o diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 20b42f8d6f67..6836851d416f 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -74,9 +74,6 @@ #include #include #include -#include -#include -#include #include #include @@ -776,200 +773,6 @@ void sched_avg_update(struct rq *rq) #endif /* CONFIG_SMP */ -#ifdef CONFIG_SCHED_HMP - -static ktime_t ktime_last; -static bool sched_ktime_suspended; - -static bool use_cycle_counter; -static struct cpu_cycle_counter_cb cpu_cycle_counter_cb; - -u64 sched_ktime_clock(void) -{ - if (unlikely(sched_ktime_suspended)) - return ktime_to_ns(ktime_last); - return ktime_get_ns(); -} - -static void sched_resume(void) -{ - sched_ktime_suspended = false; -} - -static int sched_suspend(void) -{ - ktime_last = ktime_get(); - sched_ktime_suspended = true; - return 0; -} - -static struct syscore_ops sched_syscore_ops = { - .resume = sched_resume, - .suspend = sched_suspend -}; - -static int __init sched_init_ops(void) -{ - register_syscore_ops(&sched_syscore_ops); - return 0; -} -late_initcall(sched_init_ops); - -static inline void clear_ed_task(struct task_struct *p, struct rq *rq) -{ - if (p == rq->ed_task) - rq->ed_task = NULL; -} - -static inline void set_task_last_wake(struct task_struct *p, u64 wallclock) -{ - p->last_wake_ts = wallclock; -} - -static inline void set_task_last_switch_out(struct task_struct *p, - u64 wallclock) -{ - p->last_switch_out_ts = wallclock; -} - -/* - * Note C-state for (idle) cpus. - * - * @cstate = cstate index, 0 -> active state - * @wakeup_energy = energy spent in waking up cpu - * @wakeup_latency = latency to wakeup from cstate - * - */ -void -sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) -{ - struct rq *rq = cpu_rq(cpu); - - rq->cstate = cstate; /* C1, C2 etc */ - rq->wakeup_energy = wakeup_energy; - rq->wakeup_latency = wakeup_latency; -} - -/* - * Note D-state for (idle) cluster. - * - * @dstate = dstate index, 0 -> active state - * @wakeup_energy = energy spent in waking up cluster - * @wakeup_latency = latency to wakeup from cluster - * - */ -void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, - int wakeup_energy, int wakeup_latency) -{ - struct sched_cluster *cluster = - cpu_rq(cpumask_first(cluster_cpus))->cluster; - cluster->dstate = dstate; - cluster->dstate_wakeup_energy = wakeup_energy; - cluster->dstate_wakeup_latency = wakeup_latency; -} - -u32 __weak get_freq_max_load(int cpu, u32 freq) -{ - /* 100% by default */ - return 100; -} - -DEFINE_PER_CPU(struct freq_max_load *, freq_max_load); -static DEFINE_SPINLOCK(freq_max_load_lock); - -int sched_update_freq_max_load(const cpumask_t *cpumask) -{ - int i, cpu, ret; - unsigned int freq; - struct cpu_pstate_pwr *costs; - struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); - struct freq_max_load *max_load, *old_max_load; - struct freq_max_load_entry *entry; - u64 max_demand_capacity, max_demand; - unsigned long flags; - u32 hfreq; - int hpct; - - if (!per_cpu_info) - return 0; - - spin_lock_irqsave(&freq_max_load_lock, flags); - max_demand_capacity = div64_u64(max_task_load(), max_possible_capacity); - for_each_cpu(cpu, cpumask) { - if (!per_cpu_info[cpu].ptable) { - ret = -EINVAL; - goto fail; - } - - old_max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); - - /* - * allocate len + 1 and leave the last power cost as 0 for - * power_cost() can stop iterating index when - * per_cpu_info[cpu].len > len of max_load due to race between - * cpu power stats update and get_cpu_pwr_stats(). - */ - max_load = kzalloc(sizeof(struct freq_max_load) + - sizeof(struct freq_max_load_entry) * - (per_cpu_info[cpu].len + 1), GFP_ATOMIC); - if (unlikely(!max_load)) { - ret = -ENOMEM; - goto fail; - } - - max_load->length = per_cpu_info[cpu].len; - - max_demand = max_demand_capacity * - cpu_max_possible_capacity(cpu); - - i = 0; - costs = per_cpu_info[cpu].ptable; - while (costs[i].freq) { - entry = &max_load->freqs[i]; - freq = costs[i].freq; - hpct = get_freq_max_load(cpu, freq); - if (hpct <= 0 && hpct > 100) - hpct = 100; - hfreq = div64_u64((u64)freq * hpct, 100); - entry->hdemand = - div64_u64(max_demand * hfreq, - cpu_max_possible_freq(cpu)); - i++; - } - - rcu_assign_pointer(per_cpu(freq_max_load, cpu), max_load); - if (old_max_load) - kfree_rcu(old_max_load, rcu); - } - - spin_unlock_irqrestore(&freq_max_load_lock, flags); - return 0; - -fail: - for_each_cpu(cpu, cpumask) { - max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); - if (max_load) { - rcu_assign_pointer(per_cpu(freq_max_load, cpu), NULL); - kfree_rcu(max_load, rcu); - } - } - - spin_unlock_irqrestore(&freq_max_load_lock, flags); - return ret; -} - -#else /* CONFIG_SCHED_HMP */ -u64 sched_ktime_clock(void) -{ - return 0; -} - -static inline void clear_ed_task(struct task_struct *p, struct rq *rq) {} -static inline void set_task_last_wake(struct task_struct *p, u64 wallclock) {} -static inline void set_task_last_switch_out(struct task_struct *p, - u64 wallclock) {} -#endif /* CONFIG_SCHED_HMP */ - #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) /* @@ -1258,2944 +1061,6 @@ void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) rq_clock_skip_update(rq, true); } -#ifdef CONFIG_SCHED_HMP -unsigned int max_possible_efficiency = 1; -unsigned int min_possible_efficiency = UINT_MAX; - -unsigned long __weak arch_get_cpu_efficiency(int cpu) -{ - return SCHED_LOAD_SCALE; -} - -/* Keep track of max/min capacity possible across CPUs "currently" */ -static void __update_min_max_capacity(void) -{ - int i; - int max_cap = 0, min_cap = INT_MAX; - - for_each_online_cpu(i) { - max_cap = max(max_cap, cpu_capacity(i)); - min_cap = min(min_cap, cpu_capacity(i)); - } - - max_capacity = max_cap; - min_capacity = min_cap; -} - -static void update_min_max_capacity(void) -{ - unsigned long flags; - int i; - - local_irq_save(flags); - for_each_possible_cpu(i) - raw_spin_lock(&cpu_rq(i)->lock); - - __update_min_max_capacity(); - - for_each_possible_cpu(i) - raw_spin_unlock(&cpu_rq(i)->lock); - local_irq_restore(flags); -} - -/* - * Return 'capacity' of a cpu in reference to "least" efficient cpu, such that - * least efficient cpu gets capacity of 1024 - */ -static unsigned long -capacity_scale_cpu_efficiency(struct sched_cluster *cluster) -{ - return (1024 * cluster->efficiency) / min_possible_efficiency; -} - -/* - * Return 'capacity' of a cpu in reference to cpu with lowest max_freq - * (min_max_freq), such that one with lowest max_freq gets capacity of 1024. - */ -static unsigned long capacity_scale_cpu_freq(struct sched_cluster *cluster) -{ - return (1024 * cluster_max_freq(cluster)) / min_max_freq; -} - -/* - * Return load_scale_factor of a cpu in reference to "most" efficient cpu, so - * that "most" efficient cpu gets a load_scale_factor of 1 - */ -static inline unsigned long -load_scale_cpu_efficiency(struct sched_cluster *cluster) -{ - return DIV_ROUND_UP(1024 * max_possible_efficiency, - cluster->efficiency); -} - -/* - * Return load_scale_factor of a cpu in reference to cpu with best max_freq - * (max_possible_freq), so that one with best max_freq gets a load_scale_factor - * of 1. - */ -static inline unsigned long load_scale_cpu_freq(struct sched_cluster *cluster) -{ - return DIV_ROUND_UP(1024 * max_possible_freq, - cluster_max_freq(cluster)); -} - -static int compute_capacity(struct sched_cluster *cluster) -{ - int capacity = 1024; - - capacity *= capacity_scale_cpu_efficiency(cluster); - capacity >>= 10; - - capacity *= capacity_scale_cpu_freq(cluster); - capacity >>= 10; - - return capacity; -} - -static int compute_max_possible_capacity(struct sched_cluster *cluster) -{ - int capacity = 1024; - - capacity *= capacity_scale_cpu_efficiency(cluster); - capacity >>= 10; - - capacity *= (1024 * cluster->max_possible_freq) / min_max_freq; - capacity >>= 10; - - return capacity; -} - -static int compute_load_scale_factor(struct sched_cluster *cluster) -{ - int load_scale = 1024; - - /* - * load_scale_factor accounts for the fact that task load - * is in reference to "best" performing cpu. Task's load will need to be - * scaled (up) by a factor to determine suitability to be placed on a - * (little) cpu. - */ - load_scale *= load_scale_cpu_efficiency(cluster); - load_scale >>= 10; - - load_scale *= load_scale_cpu_freq(cluster); - load_scale >>= 10; - - return load_scale; -} - -struct list_head cluster_head; -static DEFINE_MUTEX(cluster_lock); -static cpumask_t all_cluster_cpus = CPU_MASK_NONE; -DECLARE_BITMAP(all_cluster_ids, NR_CPUS); -struct sched_cluster *sched_cluster[NR_CPUS]; -int num_clusters; - -static struct sched_cluster init_cluster = { - .list = LIST_HEAD_INIT(init_cluster.list), - .id = 0, - .max_power_cost = 1, - .min_power_cost = 1, - .capacity = 1024, - .max_possible_capacity = 1024, - .efficiency = 1, - .load_scale_factor = 1024, - .cur_freq = 1, - .max_freq = 1, - .max_mitigated_freq = UINT_MAX, - .min_freq = 1, - .max_possible_freq = 1, - .dstate = 0, - .dstate_wakeup_energy = 0, - .dstate_wakeup_latency = 0, - .exec_scale_factor = 1024, -}; - -void update_all_clusters_stats(void) -{ - struct sched_cluster *cluster; - u64 highest_mpc = 0, lowest_mpc = U64_MAX; - - pre_big_task_count_change(cpu_possible_mask); - - for_each_sched_cluster(cluster) { - u64 mpc; - - cluster->capacity = compute_capacity(cluster); - mpc = cluster->max_possible_capacity = - compute_max_possible_capacity(cluster); - cluster->load_scale_factor = compute_load_scale_factor(cluster); - - cluster->exec_scale_factor = - DIV_ROUND_UP(cluster->efficiency * 1024, - max_possible_efficiency); - - if (mpc > highest_mpc) - highest_mpc = mpc; - - if (mpc < lowest_mpc) - lowest_mpc = mpc; - } - - max_possible_capacity = highest_mpc; - min_max_possible_capacity = lowest_mpc; - - __update_min_max_capacity(); - sched_update_freq_max_load(cpu_possible_mask); - post_big_task_count_change(cpu_possible_mask); -} - -static void assign_cluster_ids(struct list_head *head) -{ - struct sched_cluster *cluster; - int pos = 0; - - list_for_each_entry(cluster, head, list) { - cluster->id = pos; - sched_cluster[pos++] = cluster; - } -} - -static void -move_list(struct list_head *dst, struct list_head *src, bool sync_rcu) -{ - struct list_head *first, *last; - - first = src->next; - last = src->prev; - - if (sync_rcu) { - INIT_LIST_HEAD_RCU(src); - synchronize_rcu(); - } - - first->prev = dst; - dst->prev = last; - last->next = dst; - - /* Ensure list sanity before making the head visible to all CPUs. */ - smp_mb(); - dst->next = first; -} - -static int -compare_clusters(void *priv, struct list_head *a, struct list_head *b) -{ - struct sched_cluster *cluster1, *cluster2; - int ret; - - cluster1 = container_of(a, struct sched_cluster, list); - cluster2 = container_of(b, struct sched_cluster, list); - - ret = cluster1->max_power_cost > cluster2->max_power_cost || - (cluster1->max_power_cost == cluster2->max_power_cost && - cluster1->max_possible_capacity < - cluster2->max_possible_capacity); - - return ret; -} - -static void sort_clusters(void) -{ - struct sched_cluster *cluster; - struct list_head new_head; - - INIT_LIST_HEAD(&new_head); - - for_each_sched_cluster(cluster) { - cluster->max_power_cost = power_cost(cluster_first_cpu(cluster), - max_task_load()); - cluster->min_power_cost = power_cost(cluster_first_cpu(cluster), - 0); - } - - move_list(&new_head, &cluster_head, true); - - list_sort(NULL, &new_head, compare_clusters); - assign_cluster_ids(&new_head); - - /* - * Ensure cluster ids are visible to all CPUs before making - * cluster_head visible. - */ - move_list(&cluster_head, &new_head, false); -} - -static void -insert_cluster(struct sched_cluster *cluster, struct list_head *head) -{ - struct sched_cluster *tmp; - struct list_head *iter = head; - - list_for_each_entry(tmp, head, list) { - if (cluster->max_power_cost < tmp->max_power_cost) - break; - iter = &tmp->list; - } - - list_add(&cluster->list, iter); -} - -static struct sched_cluster *alloc_new_cluster(const struct cpumask *cpus) -{ - struct sched_cluster *cluster = NULL; - - cluster = kzalloc(sizeof(struct sched_cluster), GFP_ATOMIC); - if (!cluster) { - __WARN_printf("Cluster allocation failed. \ - Possible bad scheduling\n"); - return NULL; - } - - INIT_LIST_HEAD(&cluster->list); - cluster->max_power_cost = 1; - cluster->min_power_cost = 1; - cluster->capacity = 1024; - cluster->max_possible_capacity = 1024; - cluster->efficiency = 1; - cluster->load_scale_factor = 1024; - cluster->cur_freq = 1; - cluster->max_freq = 1; - cluster->max_mitigated_freq = UINT_MAX; - cluster->min_freq = 1; - cluster->max_possible_freq = 1; - cluster->dstate = 0; - cluster->dstate_wakeup_energy = 0; - cluster->dstate_wakeup_latency = 0; - cluster->freq_init_done = false; - - cluster->cpus = *cpus; - cluster->efficiency = arch_get_cpu_efficiency(cpumask_first(cpus)); - - if (cluster->efficiency > max_possible_efficiency) - max_possible_efficiency = cluster->efficiency; - if (cluster->efficiency < min_possible_efficiency) - min_possible_efficiency = cluster->efficiency; - - return cluster; -} - -static void add_cluster(const struct cpumask *cpus, struct list_head *head) -{ - struct sched_cluster *cluster = alloc_new_cluster(cpus); - int i; - - if (!cluster) - return; - - for_each_cpu(i, cpus) - cpu_rq(i)->cluster = cluster; - - insert_cluster(cluster, head); - set_bit(num_clusters, all_cluster_ids); - num_clusters++; -} - -static void update_cluster_topology(void) -{ - struct cpumask cpus = *cpu_possible_mask; - const struct cpumask *cluster_cpus; - struct list_head new_head; - int i; - - INIT_LIST_HEAD(&new_head); - - for_each_cpu(i, &cpus) { - cluster_cpus = cpu_coregroup_mask(i); - cpumask_or(&all_cluster_cpus, &all_cluster_cpus, cluster_cpus); - cpumask_andnot(&cpus, &cpus, cluster_cpus); - add_cluster(cluster_cpus, &new_head); - } - - assign_cluster_ids(&new_head); - - /* - * Ensure cluster ids are visible to all CPUs before making - * cluster_head visible. - */ - move_list(&cluster_head, &new_head, false); -} - -static void init_clusters(void) -{ - bitmap_clear(all_cluster_ids, 0, NR_CPUS); - init_cluster.cpus = *cpu_possible_mask; - INIT_LIST_HEAD(&cluster_head); -} - -int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) -{ - mutex_lock(&cluster_lock); - if (!cb->get_cpu_cycle_counter) { - mutex_unlock(&cluster_lock); - return -EINVAL; - } - - cpu_cycle_counter_cb = *cb; - use_cycle_counter = true; - mutex_unlock(&cluster_lock); - - return 0; -} - -static int __init set_sched_enable_hmp(char *str) -{ - int enable_hmp = 0; - - get_option(&str, &enable_hmp); - - sched_enable_hmp = !!enable_hmp; - - return 0; -} - -early_param("sched_enable_hmp", set_sched_enable_hmp); - -static inline int got_boost_kick(void) -{ - int cpu = smp_processor_id(); - struct rq *rq = cpu_rq(cpu); - - return test_bit(BOOST_KICK, &rq->hmp_flags); -} - -static inline void clear_boost_kick(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - clear_bit(BOOST_KICK, &rq->hmp_flags); -} - -void boost_kick(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - if (!test_and_set_bit(BOOST_KICK, &rq->hmp_flags)) - smp_send_reschedule(cpu); -} - -/* Clear any HMP scheduler related requests pending from or on cpu */ -static inline void clear_hmp_request(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - unsigned long flags; - - clear_boost_kick(cpu); - clear_reserved(cpu); - if (rq->push_task) { - raw_spin_lock_irqsave(&rq->lock, flags); - if (rq->push_task) { - clear_reserved(rq->push_cpu); - put_task_struct(rq->push_task); - rq->push_task = NULL; - } - rq->active_balance = 0; - raw_spin_unlock_irqrestore(&rq->lock, flags); - } -} - -int sched_set_static_cpu_pwr_cost(int cpu, unsigned int cost) -{ - struct rq *rq = cpu_rq(cpu); - - rq->static_cpu_pwr_cost = cost; - return 0; -} - -unsigned int sched_get_static_cpu_pwr_cost(int cpu) -{ - return cpu_rq(cpu)->static_cpu_pwr_cost; -} - -int sched_set_static_cluster_pwr_cost(int cpu, unsigned int cost) -{ - struct sched_cluster *cluster = cpu_rq(cpu)->cluster; - - cluster->static_cluster_pwr_cost = cost; - return 0; -} - -unsigned int sched_get_static_cluster_pwr_cost(int cpu) -{ - return cpu_rq(cpu)->cluster->static_cluster_pwr_cost; -} - -/* - * sched_window_stats_policy and sched_ravg_hist_size have a 'sysctl' copy - * associated with them. This is required for atomic update of those variables - * when being modifed via sysctl interface. - * - * IMPORTANT: Initialize both copies to same value!! - */ - -/* - * Tasks that are runnable continuously for a period greather than - * EARLY_DETECTION_DURATION can be flagged early as potential - * high load tasks. - */ -#define EARLY_DETECTION_DURATION 9500000 - -static __read_mostly unsigned int sched_ravg_hist_size = 5; -__read_mostly unsigned int sysctl_sched_ravg_hist_size = 5; - -static __read_mostly unsigned int sched_window_stats_policy = - WINDOW_STATS_MAX_RECENT_AVG; -__read_mostly unsigned int sysctl_sched_window_stats_policy = - WINDOW_STATS_MAX_RECENT_AVG; - -#define SCHED_ACCOUNT_WAIT_TIME 1 - -__read_mostly unsigned int sysctl_sched_cpu_high_irqload = (10 * NSEC_PER_MSEC); - -unsigned int __read_mostly sysctl_sched_enable_colocation = 1; - -__read_mostly unsigned int sysctl_sched_new_task_windows = 5; - -#define SCHED_FREQ_ACCOUNT_WAIT_TIME 0 - -/* - * For increase, send notification if - * freq_required - cur_freq > sysctl_sched_freq_inc_notify - */ -__read_mostly int sysctl_sched_freq_inc_notify = 10 * 1024 * 1024; /* + 10GHz */ - -/* - * For decrease, send notification if - * cur_freq - freq_required > sysctl_sched_freq_dec_notify - */ -__read_mostly int sysctl_sched_freq_dec_notify = 10 * 1024 * 1024; /* - 10GHz */ - -static __read_mostly unsigned int sched_io_is_busy; - -__read_mostly unsigned int sysctl_sched_pred_alert_freq = 10 * 1024 * 1024; - -/* - * Maximum possible frequency across all cpus. Task demand and cpu - * capacity (cpu_power) metrics are scaled in reference to it. - */ -unsigned int max_possible_freq = 1; - -/* - * Minimum possible max_freq across all cpus. This will be same as - * max_possible_freq on homogeneous systems and could be different from - * max_possible_freq on heterogenous systems. min_max_freq is used to derive - * capacity (cpu_power) of cpus. - */ -unsigned int min_max_freq = 1; - -unsigned int max_capacity = 1024; /* max(rq->capacity) */ -unsigned int min_capacity = 1024; /* min(rq->capacity) */ -unsigned int max_possible_capacity = 1024; /* max(rq->max_possible_capacity) */ -unsigned int -min_max_possible_capacity = 1024; /* min(rq->max_possible_capacity) */ - -/* Window size (in ns) */ -__read_mostly unsigned int sched_ravg_window = 10000000; - -/* Min window size (in ns) = 10ms */ -#define MIN_SCHED_RAVG_WINDOW 10000000 - -/* Max window size (in ns) = 1s */ -#define MAX_SCHED_RAVG_WINDOW 1000000000 - -/* Temporarily disable window-stats activity on all cpus */ -unsigned int __read_mostly sched_disable_window_stats; - -/* - * Major task runtime. If a task runs for more than sched_major_task_runtime - * in a window, it's considered to be generating majority of workload - * for this window. Prediction could be adjusted for such tasks. - */ -__read_mostly unsigned int sched_major_task_runtime = 10000000; - -static unsigned int sync_cpu; - -static LIST_HEAD(related_thread_groups); -static DEFINE_RWLOCK(related_thread_group_lock); - -#define for_each_related_thread_group(grp) \ - list_for_each_entry(grp, &related_thread_groups, list) - -/* - * Demand aggregation for frequency purpose: - * - * 'sched_freq_aggregate' controls aggregation of cpu demand of related threads - * for frequency determination purpose. This aggregation is done per-cluster. - * - * CPU demand of tasks from various related groups is aggregated per-cluster and - * added to the "max_busy_cpu" in that cluster, where max_busy_cpu is determined - * by just rq->prev_runnable_sum. - * - * Some examples follow, which assume: - * Cluster0 = CPU0-3, Cluster1 = CPU4-7 - * One related thread group A that has tasks A0, A1, A2 - * - * A->cpu_time[X].curr/prev_sum = counters in which cpu execution stats of - * tasks belonging to group A are accumulated when they run on cpu X. - * - * CX->curr/prev_sum = counters in which cpu execution stats of all tasks - * not belonging to group A are accumulated when they run on cpu X - * - * Lets say the stats for window M was as below: - * - * C0->prev_sum = 1ms, A->cpu_time[0].prev_sum = 5ms - * Task A0 ran 5ms on CPU0 - * Task B0 ran 1ms on CPU0 - * - * C1->prev_sum = 5ms, A->cpu_time[1].prev_sum = 6ms - * Task A1 ran 4ms on CPU1 - * Task A2 ran 2ms on CPU1 - * Task B1 ran 5ms on CPU1 - * - * C2->prev_sum = 0ms, A->cpu_time[2].prev_sum = 0 - * CPU2 idle - * - * C3->prev_sum = 0ms, A->cpu_time[3].prev_sum = 0 - * CPU3 idle - * - * In this case, CPU1 was most busy going by just its prev_sum counter. Demand - * from all group A tasks are added to CPU1. IOW, at end of window M, cpu busy - * time reported to governor will be: - * - * - * C0 busy time = 1ms - * C1 busy time = 5 + 5 + 6 = 16ms - * - */ -static __read_mostly unsigned int sched_freq_aggregate; -__read_mostly unsigned int sysctl_sched_freq_aggregate; - -#define EXITING_TASK_MARKER 0xdeaddead - -static inline int exiting_task(struct task_struct *p) -{ - return (p->ravg.sum_history[0] == EXITING_TASK_MARKER); -} - -static int __init set_sched_ravg_window(char *str) -{ - unsigned int window_size; - - get_option(&str, &window_size); - - if (window_size < MIN_SCHED_RAVG_WINDOW || - window_size > MAX_SCHED_RAVG_WINDOW) { - WARN_ON(1); - return -EINVAL; - } - - sched_ravg_window = window_size; - return 0; -} - -early_param("sched_ravg_window", set_sched_ravg_window); - -static inline void -update_window_start(struct rq *rq, u64 wallclock) -{ - s64 delta; - int nr_windows; - - delta = wallclock - rq->window_start; - BUG_ON(delta < 0); - if (delta < sched_ravg_window) - return; - - nr_windows = div64_u64(delta, sched_ravg_window); - rq->window_start += (u64)nr_windows * (u64)sched_ravg_window; -} - -#define DIV64_U64_ROUNDUP(X, Y) div64_u64((X) + (Y - 1), Y) - -static inline u64 scale_exec_time(u64 delta, struct rq *rq) -{ - u32 freq; - - freq = cpu_cycles_to_freq(rq->cc.cycles, rq->cc.time); - delta = DIV64_U64_ROUNDUP(delta * freq, max_possible_freq); - delta *= rq->cluster->exec_scale_factor; - delta >>= 10; - - return delta; -} - -static inline int cpu_is_waiting_on_io(struct rq *rq) -{ - if (!sched_io_is_busy) - return 0; - - return atomic_read(&rq->nr_iowait); -} - -/* Does freq_required sufficiently exceed or fall behind cur_freq? */ -static inline int -nearly_same_freq(unsigned int cur_freq, unsigned int freq_required) -{ - int delta = freq_required - cur_freq; - - if (freq_required > cur_freq) - return delta < sysctl_sched_freq_inc_notify; - - delta = -delta; - - return delta < sysctl_sched_freq_dec_notify; -} - -/* Convert busy time to frequency equivalent */ -static inline unsigned int load_to_freq(struct rq *rq, u64 load) -{ - unsigned int freq; - - load = scale_load_to_cpu(load, cpu_of(rq)); - load *= 128; - load = div64_u64(load, max_task_load()); - - freq = load * cpu_max_possible_freq(cpu_of(rq)); - freq /= 128; - - return freq; -} - -static inline struct group_cpu_time * -_group_cpu_time(struct related_thread_group *grp, int cpu); - -/* - * Return load from all related group in given cpu. - * Caller must ensure that related_thread_group_lock is held. - */ -static void _group_load_in_cpu(int cpu, u64 *grp_load, u64 *new_grp_load) -{ - struct related_thread_group *grp; - - for_each_related_thread_group(grp) { - struct group_cpu_time *cpu_time; - - cpu_time = _group_cpu_time(grp, cpu); - *grp_load += cpu_time->prev_runnable_sum; - if (new_grp_load) - *new_grp_load += cpu_time->nt_prev_runnable_sum; - } -} - -/* - * Return load from all related groups in given frequency domain. - * Caller must ensure that related_thread_group_lock is held. - */ -static void group_load_in_freq_domain(struct cpumask *cpus, - u64 *grp_load, u64 *new_grp_load) -{ - struct related_thread_group *grp; - int j; - - for_each_related_thread_group(grp) { - for_each_cpu(j, cpus) { - struct group_cpu_time *cpu_time; - - cpu_time = _group_cpu_time(grp, j); - *grp_load += cpu_time->prev_runnable_sum; - *new_grp_load += cpu_time->nt_prev_runnable_sum; - } - } -} - -/* - * Should scheduler alert governor for changing frequency? - * - * @check_pred - evaluate frequency based on the predictive demand - * @check_groups - add load from all related groups on given cpu - * - * check_groups is set to 1 if a "related" task movement/wakeup is triggering - * the notification check. To avoid "re-aggregation" of demand in such cases, - * we check whether the migrated/woken tasks demand (along with demand from - * existing tasks on the cpu) can be met on target cpu - * - */ - -static int send_notification(struct rq *rq, int check_pred, int check_groups) -{ - unsigned int cur_freq, freq_required; - unsigned long flags; - int rc = 0; - u64 group_load = 0, new_load = 0; - - if (!sched_enable_hmp) - return 0; - - if (check_pred) { - u64 prev = rq->old_busy_time; - u64 predicted = rq->hmp_stats.pred_demands_sum; - - if (rq->cluster->cur_freq == cpu_max_freq(cpu_of(rq))) - return 0; - - prev = max(prev, rq->old_estimated_time); - if (prev > predicted) - return 0; - - cur_freq = load_to_freq(rq, prev); - freq_required = load_to_freq(rq, predicted); - - if (freq_required < cur_freq + sysctl_sched_pred_alert_freq) - return 0; - } else { - read_lock(&related_thread_group_lock); - /* - * Protect from concurrent update of rq->prev_runnable_sum and - * group cpu load - */ - raw_spin_lock_irqsave(&rq->lock, flags); - if (check_groups) - _group_load_in_cpu(cpu_of(rq), &group_load, NULL); - - new_load = rq->prev_runnable_sum + group_load; - - raw_spin_unlock_irqrestore(&rq->lock, flags); - read_unlock(&related_thread_group_lock); - - cur_freq = load_to_freq(rq, rq->old_busy_time); - freq_required = load_to_freq(rq, new_load); - - if (nearly_same_freq(cur_freq, freq_required)) - return 0; - } - - raw_spin_lock_irqsave(&rq->lock, flags); - if (!rq->notifier_sent) { - rq->notifier_sent = 1; - rc = 1; - trace_sched_freq_alert(cpu_of(rq), check_pred, check_groups, rq, - new_load); - } - raw_spin_unlock_irqrestore(&rq->lock, flags); - - return rc; -} - -/* Alert governor if there is a need to change frequency */ -void check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups) -{ - int cpu = cpu_of(rq); - - if (!send_notification(rq, check_pred, check_groups)) - return; - - atomic_notifier_call_chain( - &load_alert_notifier_head, 0, - (void *)(long)cpu); -} - -static int account_busy_for_cpu_time(struct rq *rq, struct task_struct *p, - u64 irqtime, int event) -{ - if (is_idle_task(p)) { - /* TASK_WAKE && TASK_MIGRATE is not possible on idle task! */ - if (event == PICK_NEXT_TASK) - return 0; - - /* PUT_PREV_TASK, TASK_UPDATE && IRQ_UPDATE are left */ - return irqtime || cpu_is_waiting_on_io(rq); - } - - if (event == TASK_WAKE) - return 0; - - if (event == PUT_PREV_TASK || event == IRQ_UPDATE) - return 1; - - /* - * TASK_UPDATE can be called on sleeping task, when its moved between - * related groups - */ - if (event == TASK_UPDATE) { - if (rq->curr == p) - return 1; - - return p->on_rq ? SCHED_FREQ_ACCOUNT_WAIT_TIME : 0; - } - - /* TASK_MIGRATE, PICK_NEXT_TASK left */ - return SCHED_FREQ_ACCOUNT_WAIT_TIME; -} - -static inline bool is_new_task(struct task_struct *p) -{ - return p->ravg.active_windows < sysctl_sched_new_task_windows; -} - -#define INC_STEP 8 -#define DEC_STEP 2 -#define CONSISTENT_THRES 16 -#define INC_STEP_BIG 16 -/* - * bucket_increase - update the count of all buckets - * - * @buckets: array of buckets tracking busy time of a task - * @idx: the index of bucket to be incremented - * - * Each time a complete window finishes, count of bucket that runtime - * falls in (@idx) is incremented. Counts of all other buckets are - * decayed. The rate of increase and decay could be different based - * on current count in the bucket. - */ -static inline void bucket_increase(u8 *buckets, int idx) -{ - int i, step; - - for (i = 0; i < NUM_BUSY_BUCKETS; i++) { - if (idx != i) { - if (buckets[i] > DEC_STEP) - buckets[i] -= DEC_STEP; - else - buckets[i] = 0; - } else { - step = buckets[i] >= CONSISTENT_THRES ? - INC_STEP_BIG : INC_STEP; - if (buckets[i] > U8_MAX - step) - buckets[i] = U8_MAX; - else - buckets[i] += step; - } - } -} - -static inline int busy_to_bucket(u32 normalized_rt) -{ - int bidx; - - bidx = mult_frac(normalized_rt, NUM_BUSY_BUCKETS, max_task_load()); - bidx = min(bidx, NUM_BUSY_BUCKETS - 1); - - /* - * Combine lowest two buckets. The lowest frequency falls into - * 2nd bucket and thus keep predicting lowest bucket is not - * useful. - */ - if (!bidx) - bidx++; - - return bidx; -} - -static inline u64 -scale_load_to_freq(u64 load, unsigned int src_freq, unsigned int dst_freq) -{ - return div64_u64(load * (u64)src_freq, (u64)dst_freq); -} - -#define HEAVY_TASK_SKIP 2 -#define HEAVY_TASK_SKIP_LIMIT 4 -/* - * get_pred_busy - calculate predicted demand for a task on runqueue - * - * @rq: runqueue of task p - * @p: task whose prediction is being updated - * @start: starting bucket. returned prediction should not be lower than - * this bucket. - * @runtime: runtime of the task. returned prediction should not be lower - * than this runtime. - * Note: @start can be derived from @runtime. It's passed in only to - * avoid duplicated calculation in some cases. - * - * A new predicted busy time is returned for task @p based on @runtime - * passed in. The function searches through buckets that represent busy - * time equal to or bigger than @runtime and attempts to find the bucket to - * to use for prediction. Once found, it searches through historical busy - * time and returns the latest that falls into the bucket. If no such busy - * time exists, it returns the medium of that bucket. - */ -static u32 get_pred_busy(struct rq *rq, struct task_struct *p, - int start, u32 runtime) -{ - int i; - u8 *buckets = p->ravg.busy_buckets; - u32 *hist = p->ravg.sum_history; - u32 dmin, dmax; - u64 cur_freq_runtime = 0; - int first = NUM_BUSY_BUCKETS, final, skip_to; - u32 ret = runtime; - - /* skip prediction for new tasks due to lack of history */ - if (unlikely(is_new_task(p))) - goto out; - - /* find minimal bucket index to pick */ - for (i = start; i < NUM_BUSY_BUCKETS; i++) { - if (buckets[i]) { - first = i; - break; - } - } - /* if no higher buckets are filled, predict runtime */ - if (first >= NUM_BUSY_BUCKETS) - goto out; - - /* compute the bucket for prediction */ - final = first; - if (first < HEAVY_TASK_SKIP_LIMIT) { - /* compute runtime at current CPU frequency */ - cur_freq_runtime = mult_frac(runtime, max_possible_efficiency, - rq->cluster->efficiency); - cur_freq_runtime = scale_load_to_freq(cur_freq_runtime, - max_possible_freq, rq->cluster->cur_freq); - /* - * if the task runs for majority of the window, try to - * pick higher buckets. - */ - if (cur_freq_runtime >= sched_major_task_runtime) { - int next = NUM_BUSY_BUCKETS; - /* - * if there is a higher bucket that's consistently - * hit, don't jump beyond that. - */ - for (i = start + 1; i <= HEAVY_TASK_SKIP_LIMIT && - i < NUM_BUSY_BUCKETS; i++) { - if (buckets[i] > CONSISTENT_THRES) { - next = i; - break; - } - } - skip_to = min(next, start + HEAVY_TASK_SKIP); - /* don't jump beyond HEAVY_TASK_SKIP_LIMIT */ - skip_to = min(HEAVY_TASK_SKIP_LIMIT, skip_to); - /* don't go below first non-empty bucket, if any */ - final = max(first, skip_to); - } - } - - /* determine demand range for the predicted bucket */ - if (final < 2) { - /* lowest two buckets are combined */ - dmin = 0; - final = 1; - } else { - dmin = mult_frac(final, max_task_load(), NUM_BUSY_BUCKETS); - } - dmax = mult_frac(final + 1, max_task_load(), NUM_BUSY_BUCKETS); - - /* - * search through runtime history and return first runtime that falls - * into the range of predicted bucket. - */ - for (i = 0; i < sched_ravg_hist_size; i++) { - if (hist[i] >= dmin && hist[i] < dmax) { - ret = hist[i]; - break; - } - } - /* no historical runtime within bucket found, use average of the bin */ - if (ret < dmin) - ret = (dmin + dmax) / 2; - /* - * when updating in middle of a window, runtime could be higher - * than all recorded history. Always predict at least runtime. - */ - ret = max(runtime, ret); -out: - trace_sched_update_pred_demand(rq, p, runtime, - mult_frac((unsigned int)cur_freq_runtime, 100, - sched_ravg_window), ret); - return ret; -} - -static inline u32 calc_pred_demand(struct rq *rq, struct task_struct *p) -{ - if (p->ravg.pred_demand >= p->ravg.curr_window) - return p->ravg.pred_demand; - - return get_pred_busy(rq, p, busy_to_bucket(p->ravg.curr_window), - p->ravg.curr_window); -} - -/* - * predictive demand of a task is calculated at the window roll-over. - * if the task current window busy time exceeds the predicted - * demand, update it here to reflect the task needs. - */ -void update_task_pred_demand(struct rq *rq, struct task_struct *p, int event) -{ - u32 new, old; - - if (is_idle_task(p) || exiting_task(p)) - return; - - if (event != PUT_PREV_TASK && event != TASK_UPDATE && - (!SCHED_FREQ_ACCOUNT_WAIT_TIME || - (event != TASK_MIGRATE && - event != PICK_NEXT_TASK))) - return; - - /* - * TASK_UPDATE can be called on sleeping task, when its moved between - * related groups - */ - if (event == TASK_UPDATE) { - if (!p->on_rq && !SCHED_FREQ_ACCOUNT_WAIT_TIME) - return; - } - - new = calc_pred_demand(rq, p); - old = p->ravg.pred_demand; - - if (old >= new) - return; - - if (task_on_rq_queued(p) && (!task_has_dl_policy(p) || - !p->dl.dl_throttled)) - p->sched_class->fixup_hmp_sched_stats(rq, p, - p->ravg.demand, - new); - - p->ravg.pred_demand = new; -} - -/* - * Account cpu activity in its busy time counters (rq->curr/prev_runnable_sum) - */ -static void update_cpu_busy_time(struct task_struct *p, struct rq *rq, - int event, u64 wallclock, u64 irqtime) -{ - int new_window, full_window = 0; - int p_is_curr_task = (p == rq->curr); - u64 mark_start = p->ravg.mark_start; - u64 window_start = rq->window_start; - u32 window_size = sched_ravg_window; - u64 delta; - u64 *curr_runnable_sum = &rq->curr_runnable_sum; - u64 *prev_runnable_sum = &rq->prev_runnable_sum; - u64 *nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; - u64 *nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; - int flip_counters = 0; - int prev_sum_reset = 0; - bool new_task; - struct related_thread_group *grp; - - new_window = mark_start < window_start; - if (new_window) { - full_window = (window_start - mark_start) >= window_size; - if (p->ravg.active_windows < USHRT_MAX) - p->ravg.active_windows++; - } - - new_task = is_new_task(p); - - grp = p->grp; - if (grp && sched_freq_aggregate) { - /* cpu_time protected by rq_lock */ - struct group_cpu_time *cpu_time = - _group_cpu_time(grp, cpu_of(rq)); - - curr_runnable_sum = &cpu_time->curr_runnable_sum; - prev_runnable_sum = &cpu_time->prev_runnable_sum; - - nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; - nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; - - if (cpu_time->window_start != rq->window_start) { - int nr_windows; - - delta = rq->window_start - cpu_time->window_start; - nr_windows = div64_u64(delta, window_size); - if (nr_windows > 1) - prev_sum_reset = 1; - - cpu_time->window_start = rq->window_start; - flip_counters = 1; - } - - if (p_is_curr_task && new_window) { - u64 curr_sum = rq->curr_runnable_sum; - u64 nt_curr_sum = rq->nt_curr_runnable_sum; - - if (full_window) - curr_sum = nt_curr_sum = 0; - - rq->prev_runnable_sum = curr_sum; - rq->nt_prev_runnable_sum = nt_curr_sum; - - rq->curr_runnable_sum = 0; - rq->nt_curr_runnable_sum = 0; - } - } else { - if (p_is_curr_task && new_window) { - flip_counters = 1; - if (full_window) - prev_sum_reset = 1; - } - } - - /* Handle per-task window rollover. We don't care about the idle - * task or exiting tasks. */ - if (new_window && !is_idle_task(p) && !exiting_task(p)) { - u32 curr_window = 0; - - if (!full_window) - curr_window = p->ravg.curr_window; - - p->ravg.prev_window = curr_window; - p->ravg.curr_window = 0; - } - - if (flip_counters) { - u64 curr_sum = *curr_runnable_sum; - u64 nt_curr_sum = *nt_curr_runnable_sum; - - if (prev_sum_reset) - curr_sum = nt_curr_sum = 0; - - *prev_runnable_sum = curr_sum; - *nt_prev_runnable_sum = nt_curr_sum; - - *curr_runnable_sum = 0; - *nt_curr_runnable_sum = 0; - } - - if (!account_busy_for_cpu_time(rq, p, irqtime, event)) { - /* account_busy_for_cpu_time() = 0, so no update to the - * task's current window needs to be made. This could be - * for example - * - * - a wakeup event on a task within the current - * window (!new_window below, no action required), - * - switching to a new task from idle (PICK_NEXT_TASK) - * in a new window where irqtime is 0 and we aren't - * waiting on IO */ - - if (!new_window) - return; - - /* A new window has started. The RQ demand must be rolled - * over if p is the current task. */ - if (p_is_curr_task) { - /* p is idle task */ - BUG_ON(p != rq->idle); - } - - return; - } - - if (!new_window) { - /* account_busy_for_cpu_time() = 1 so busy time needs - * to be accounted to the current window. No rollover - * since we didn't start a new window. An example of this is - * when a task starts execution and then sleeps within the - * same window. */ - - if (!irqtime || !is_idle_task(p) || cpu_is_waiting_on_io(rq)) - delta = wallclock - mark_start; - else - delta = irqtime; - delta = scale_exec_time(delta, rq); - *curr_runnable_sum += delta; - if (new_task) - *nt_curr_runnable_sum += delta; - - if (!is_idle_task(p) && !exiting_task(p)) - p->ravg.curr_window += delta; - - return; - } - - if (!p_is_curr_task) { - /* account_busy_for_cpu_time() = 1 so busy time needs - * to be accounted to the current window. A new window - * has also started, but p is not the current task, so the - * window is not rolled over - just split up and account - * as necessary into curr and prev. The window is only - * rolled over when a new window is processed for the current - * task. - * - * Irqtime can't be accounted by a task that isn't the - * currently running task. */ - - if (!full_window) { - /* A full window hasn't elapsed, account partial - * contribution to previous completed window. */ - delta = scale_exec_time(window_start - mark_start, rq); - if (!exiting_task(p)) - p->ravg.prev_window += delta; - } else { - /* Since at least one full window has elapsed, - * the contribution to the previous window is the - * full window (window_size). */ - delta = scale_exec_time(window_size, rq); - if (!exiting_task(p)) - p->ravg.prev_window = delta; - } - - *prev_runnable_sum += delta; - if (new_task) - *nt_prev_runnable_sum += delta; - - /* Account piece of busy time in the current window. */ - delta = scale_exec_time(wallclock - window_start, rq); - *curr_runnable_sum += delta; - if (new_task) - *nt_curr_runnable_sum += delta; - - if (!exiting_task(p)) - p->ravg.curr_window = delta; - - return; - } - - if (!irqtime || !is_idle_task(p) || cpu_is_waiting_on_io(rq)) { - /* account_busy_for_cpu_time() = 1 so busy time needs - * to be accounted to the current window. A new window - * has started and p is the current task so rollover is - * needed. If any of these three above conditions are true - * then this busy time can't be accounted as irqtime. - * - * Busy time for the idle task or exiting tasks need not - * be accounted. - * - * An example of this would be a task that starts execution - * and then sleeps once a new window has begun. */ - - if (!full_window) { - /* A full window hasn't elapsed, account partial - * contribution to previous completed window. */ - delta = scale_exec_time(window_start - mark_start, rq); - if (!is_idle_task(p) && !exiting_task(p)) - p->ravg.prev_window += delta; - } else { - /* Since at least one full window has elapsed, - * the contribution to the previous window is the - * full window (window_size). */ - delta = scale_exec_time(window_size, rq); - if (!is_idle_task(p) && !exiting_task(p)) - p->ravg.prev_window = delta; - } - - /* Rollover is done here by overwriting the values in - * prev_runnable_sum and curr_runnable_sum. */ - *prev_runnable_sum += delta; - if (new_task) - *nt_prev_runnable_sum += delta; - - /* Account piece of busy time in the current window. */ - delta = scale_exec_time(wallclock - window_start, rq); - *curr_runnable_sum += delta; - if (new_task) - *nt_curr_runnable_sum += delta; - - if (!is_idle_task(p) && !exiting_task(p)) - p->ravg.curr_window = delta; - - return; - } - - if (irqtime) { - /* account_busy_for_cpu_time() = 1 so busy time needs - * to be accounted to the current window. A new window - * has started and p is the current task so rollover is - * needed. The current task must be the idle task because - * irqtime is not accounted for any other task. - * - * Irqtime will be accounted each time we process IRQ activity - * after a period of idleness, so we know the IRQ busy time - * started at wallclock - irqtime. */ - - BUG_ON(!is_idle_task(p)); - mark_start = wallclock - irqtime; - - /* Roll window over. If IRQ busy time was just in the current - * window then that is all that need be accounted. */ - if (mark_start > window_start) { - *curr_runnable_sum = scale_exec_time(irqtime, rq); - return; - } - - /* The IRQ busy time spanned multiple windows. Process the - * busy time preceding the current window start first. */ - delta = window_start - mark_start; - if (delta > window_size) - delta = window_size; - delta = scale_exec_time(delta, rq); - *prev_runnable_sum += delta; - - /* Process the remaining IRQ busy time in the current window. */ - delta = wallclock - window_start; - rq->curr_runnable_sum = scale_exec_time(delta, rq); - - return; - } - - BUG(); -} - -static inline u32 predict_and_update_buckets(struct rq *rq, - struct task_struct *p, u32 runtime) { - - int bidx; - u32 pred_demand; - - bidx = busy_to_bucket(runtime); - pred_demand = get_pred_busy(rq, p, bidx, runtime); - bucket_increase(p->ravg.busy_buckets, bidx); - - return pred_demand; -} - -static void update_task_cpu_cycles(struct task_struct *p, int cpu) -{ - if (use_cycle_counter) - p->cpu_cycles = cpu_cycle_counter_cb.get_cpu_cycle_counter(cpu); -} - -static void -update_task_rq_cpu_cycles(struct task_struct *p, struct rq *rq, int event, - u64 wallclock, u64 irqtime) -{ - u64 cur_cycles; - int cpu = cpu_of(rq); - - lockdep_assert_held(&rq->lock); - - if (!use_cycle_counter) { - rq->cc.cycles = cpu_cur_freq(cpu); - rq->cc.time = 1; - return; - } - - cur_cycles = cpu_cycle_counter_cb.get_cpu_cycle_counter(cpu); - - /* - * If current task is idle task and irqtime == 0 CPU was - * indeed idle and probably its cycle counter was not - * increasing. We still need estimatied CPU frequency - * for IO wait time accounting. Use the previously - * calculated frequency in such a case. - */ - if (!is_idle_task(rq->curr) || irqtime) { - if (unlikely(cur_cycles < p->cpu_cycles)) - rq->cc.cycles = cur_cycles + (U64_MAX - p->cpu_cycles); - else - rq->cc.cycles = cur_cycles - p->cpu_cycles; - rq->cc.cycles = rq->cc.cycles * NSEC_PER_MSEC; - - if (event == IRQ_UPDATE && is_idle_task(p)) - /* - * Time between mark_start of idle task and IRQ handler - * entry time is CPU cycle counter stall period. - * Upon IRQ handler entry sched_account_irqstart() - * replenishes idle task's cpu cycle counter so - * rq->cc.cycles now represents increased cycles during - * IRQ handler rather than time between idle entry and - * IRQ exit. Thus use irqtime as time delta. - */ - rq->cc.time = irqtime; - else - rq->cc.time = wallclock - p->ravg.mark_start; - BUG_ON((s64)rq->cc.time < 0); - } - - p->cpu_cycles = cur_cycles; - - trace_sched_get_task_cpu_cycles(cpu, event, rq->cc.cycles, rq->cc.time); -} - -static int account_busy_for_task_demand(struct task_struct *p, int event) -{ - /* No need to bother updating task demand for exiting tasks - * or the idle task. */ - if (exiting_task(p) || is_idle_task(p)) - return 0; - - /* When a task is waking up it is completing a segment of non-busy - * time. Likewise, if wait time is not treated as busy time, then - * when a task begins to run or is migrated, it is not running and - * is completing a segment of non-busy time. */ - if (event == TASK_WAKE || (!SCHED_ACCOUNT_WAIT_TIME && - (event == PICK_NEXT_TASK || event == TASK_MIGRATE))) - return 0; - - return 1; -} - -/* - * Called when new window is starting for a task, to record cpu usage over - * recently concluded window(s). Normally 'samples' should be 1. It can be > 1 - * when, say, a real-time task runs without preemption for several windows at a - * stretch. - */ -static void update_history(struct rq *rq, struct task_struct *p, - u32 runtime, int samples, int event) -{ - u32 *hist = &p->ravg.sum_history[0]; - int ridx, widx; - u32 max = 0, avg, demand, pred_demand; - u64 sum = 0; - - /* Ignore windows where task had no activity */ - if (!runtime || is_idle_task(p) || exiting_task(p) || !samples) - goto done; - - /* Push new 'runtime' value onto stack */ - widx = sched_ravg_hist_size - 1; - ridx = widx - samples; - for (; ridx >= 0; --widx, --ridx) { - hist[widx] = hist[ridx]; - sum += hist[widx]; - if (hist[widx] > max) - max = hist[widx]; - } - - for (widx = 0; widx < samples && widx < sched_ravg_hist_size; widx++) { - hist[widx] = runtime; - sum += hist[widx]; - if (hist[widx] > max) - max = hist[widx]; - } - - p->ravg.sum = 0; - - if (sched_window_stats_policy == WINDOW_STATS_RECENT) { - demand = runtime; - } else if (sched_window_stats_policy == WINDOW_STATS_MAX) { - demand = max; - } else { - avg = div64_u64(sum, sched_ravg_hist_size); - if (sched_window_stats_policy == WINDOW_STATS_AVG) - demand = avg; - else - demand = max(avg, runtime); - } - pred_demand = predict_and_update_buckets(rq, p, runtime); - - /* - * A throttled deadline sched class task gets dequeued without - * changing p->on_rq. Since the dequeue decrements hmp stats - * avoid decrementing it here again. - */ - if (task_on_rq_queued(p) && (!task_has_dl_policy(p) || - !p->dl.dl_throttled)) - p->sched_class->fixup_hmp_sched_stats(rq, p, demand, - pred_demand); - - p->ravg.demand = demand; - p->ravg.pred_demand = pred_demand; - -done: - trace_sched_update_history(rq, p, runtime, samples, event); -} - -static void add_to_task_demand(struct rq *rq, struct task_struct *p, u64 delta) -{ - delta = scale_exec_time(delta, rq); - p->ravg.sum += delta; - if (unlikely(p->ravg.sum > sched_ravg_window)) - p->ravg.sum = sched_ravg_window; -} - -/* - * Account cpu demand of task and/or update task's cpu demand history - * - * ms = p->ravg.mark_start; - * wc = wallclock - * ws = rq->window_start - * - * Three possibilities: - * - * a) Task event is contained within one window. - * window_start < mark_start < wallclock - * - * ws ms wc - * | | | - * V V V - * |---------------| - * - * In this case, p->ravg.sum is updated *iff* event is appropriate - * (ex: event == PUT_PREV_TASK) - * - * b) Task event spans two windows. - * mark_start < window_start < wallclock - * - * ms ws wc - * | | | - * V V V - * -----|------------------- - * - * In this case, p->ravg.sum is updated with (ws - ms) *iff* event - * is appropriate, then a new window sample is recorded followed - * by p->ravg.sum being set to (wc - ws) *iff* event is appropriate. - * - * c) Task event spans more than two windows. - * - * ms ws_tmp ws wc - * | | | | - * V V V V - * ---|-------|-------|-------|-------|------ - * | | - * |<------ nr_full_windows ------>| - * - * In this case, p->ravg.sum is updated with (ws_tmp - ms) first *iff* - * event is appropriate, window sample of p->ravg.sum is recorded, - * 'nr_full_window' samples of window_size is also recorded *iff* - * event is appropriate and finally p->ravg.sum is set to (wc - ws) - * *iff* event is appropriate. - * - * IMPORTANT : Leave p->ravg.mark_start unchanged, as update_cpu_busy_time() - * depends on it! - */ -static void update_task_demand(struct task_struct *p, struct rq *rq, - int event, u64 wallclock) -{ - u64 mark_start = p->ravg.mark_start; - u64 delta, window_start = rq->window_start; - int new_window, nr_full_windows; - u32 window_size = sched_ravg_window; - - new_window = mark_start < window_start; - if (!account_busy_for_task_demand(p, event)) { - if (new_window) - /* If the time accounted isn't being accounted as - * busy time, and a new window started, only the - * previous window need be closed out with the - * pre-existing demand. Multiple windows may have - * elapsed, but since empty windows are dropped, - * it is not necessary to account those. */ - update_history(rq, p, p->ravg.sum, 1, event); - return; - } - - if (!new_window) { - /* The simple case - busy time contained within the existing - * window. */ - add_to_task_demand(rq, p, wallclock - mark_start); - return; - } - - /* Busy time spans at least two windows. Temporarily rewind - * window_start to first window boundary after mark_start. */ - delta = window_start - mark_start; - nr_full_windows = div64_u64(delta, window_size); - window_start -= (u64)nr_full_windows * (u64)window_size; - - /* Process (window_start - mark_start) first */ - add_to_task_demand(rq, p, window_start - mark_start); - - /* Push new sample(s) into task's demand history */ - update_history(rq, p, p->ravg.sum, 1, event); - if (nr_full_windows) - update_history(rq, p, scale_exec_time(window_size, rq), - nr_full_windows, event); - - /* Roll window_start back to current to process any remainder - * in current window. */ - window_start += (u64)nr_full_windows * (u64)window_size; - - /* Process (wallclock - window_start) next */ - mark_start = window_start; - add_to_task_demand(rq, p, wallclock - mark_start); -} - -/* Reflect task activity on its demand and cpu's busy time statistics */ -static void -update_task_ravg(struct task_struct *p, struct rq *rq, int event, - u64 wallclock, u64 irqtime) -{ - if (!rq->window_start || sched_disable_window_stats) - return; - - lockdep_assert_held(&rq->lock); - - update_window_start(rq, wallclock); - - if (!p->ravg.mark_start) { - update_task_cpu_cycles(p, cpu_of(rq)); - goto done; - } - - update_task_rq_cpu_cycles(p, rq, event, wallclock, irqtime); - update_task_demand(p, rq, event, wallclock); - update_cpu_busy_time(p, rq, event, wallclock, irqtime); - update_task_pred_demand(rq, p, event); -done: - trace_sched_update_task_ravg(p, rq, event, wallclock, irqtime, - rq->cc.cycles, rq->cc.time, - _group_cpu_time(p->grp, cpu_of(rq))); - - p->ravg.mark_start = wallclock; -} - -void sched_account_irqtime(int cpu, struct task_struct *curr, - u64 delta, u64 wallclock) -{ - struct rq *rq = cpu_rq(cpu); - unsigned long flags, nr_windows; - u64 cur_jiffies_ts; - - raw_spin_lock_irqsave(&rq->lock, flags); - - /* - * cputime (wallclock) uses sched_clock so use the same here for - * consistency. - */ - delta += sched_clock() - wallclock; - cur_jiffies_ts = get_jiffies_64(); - - if (is_idle_task(curr)) - update_task_ravg(curr, rq, IRQ_UPDATE, sched_ktime_clock(), - delta); - - nr_windows = cur_jiffies_ts - rq->irqload_ts; - - if (nr_windows) { - if (nr_windows < 10) { - /* Decay CPU's irqload by 3/4 for each window. */ - rq->avg_irqload *= (3 * nr_windows); - rq->avg_irqload = div64_u64(rq->avg_irqload, - 4 * nr_windows); - } else { - rq->avg_irqload = 0; - } - rq->avg_irqload += rq->cur_irqload; - rq->cur_irqload = 0; - } - - rq->cur_irqload += delta; - rq->irqload_ts = cur_jiffies_ts; - raw_spin_unlock_irqrestore(&rq->lock, flags); -} - -void sched_account_irqstart(int cpu, struct task_struct *curr, u64 wallclock) -{ - struct rq *rq = cpu_rq(cpu); - - if (!rq->window_start || sched_disable_window_stats) - return; - - if (is_idle_task(curr)) { - /* We're here without rq->lock held, IRQ disabled */ - raw_spin_lock(&rq->lock); - update_task_cpu_cycles(curr, cpu); - raw_spin_unlock(&rq->lock); - } -} - -static void reset_task_stats(struct task_struct *p) -{ - u32 sum = 0; - - if (exiting_task(p)) - sum = EXITING_TASK_MARKER; - - memset(&p->ravg, 0, sizeof(struct ravg)); - /* Retain EXITING_TASK marker */ - p->ravg.sum_history[0] = sum; -} - -static inline void mark_task_starting(struct task_struct *p) -{ - u64 wallclock; - struct rq *rq = task_rq(p); - - if (!rq->window_start || sched_disable_window_stats) { - reset_task_stats(p); - return; - } - - wallclock = sched_ktime_clock(); - p->ravg.mark_start = p->last_wake_ts = wallclock; - p->last_cpu_selected_ts = wallclock; - p->last_switch_out_ts = 0; - update_task_cpu_cycles(p, cpu_of(rq)); -} - -static inline void set_window_start(struct rq *rq) -{ - int cpu = cpu_of(rq); - struct rq *sync_rq = cpu_rq(sync_cpu); - - if (rq->window_start || !sched_enable_hmp) - return; - - if (cpu == sync_cpu) { - rq->window_start = sched_ktime_clock(); - } else { - raw_spin_unlock(&rq->lock); - double_rq_lock(rq, sync_rq); - rq->window_start = cpu_rq(sync_cpu)->window_start; - rq->curr_runnable_sum = rq->prev_runnable_sum = 0; - rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; - raw_spin_unlock(&sync_rq->lock); - } - - rq->curr->ravg.mark_start = rq->window_start; -} - -static inline void migrate_sync_cpu(int cpu) -{ - if (cpu == sync_cpu) - sync_cpu = smp_processor_id(); -} - -static void reset_all_task_stats(void) -{ - struct task_struct *g, *p; - - read_lock(&tasklist_lock); - do_each_thread(g, p) { - reset_task_stats(p); - } while_each_thread(g, p); - read_unlock(&tasklist_lock); -} - -/* - * sched_exit() - Set EXITING_TASK_MARKER in task's ravg.demand field - * - * Stop accounting (exiting) task's future cpu usage - * - * We need this so that reset_all_windows_stats() can function correctly. - * reset_all_window_stats() depends on do_each_thread/for_each_thread task - * iterators to reset *all* task's statistics. Exiting tasks however become - * invisible to those iterators. sched_exit() is called on a exiting task prior - * to being removed from task_list, which will let reset_all_window_stats() - * function correctly. - */ -void sched_exit(struct task_struct *p) -{ - unsigned long flags; - int cpu = get_cpu(); - struct rq *rq = cpu_rq(cpu); - u64 wallclock; - - sched_set_group_id(p, 0); - - raw_spin_lock_irqsave(&rq->lock, flags); - /* rq->curr == p */ - wallclock = sched_ktime_clock(); - update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0); - dequeue_task(rq, p, 0); - reset_task_stats(p); - p->ravg.mark_start = wallclock; - p->ravg.sum_history[0] = EXITING_TASK_MARKER; - enqueue_task(rq, p, 0); - clear_ed_task(p, rq); - raw_spin_unlock_irqrestore(&rq->lock, flags); - - put_cpu(); -} - -static void disable_window_stats(void) -{ - unsigned long flags; - int i; - - local_irq_save(flags); - for_each_possible_cpu(i) - raw_spin_lock(&cpu_rq(i)->lock); - - sched_disable_window_stats = 1; - - for_each_possible_cpu(i) - raw_spin_unlock(&cpu_rq(i)->lock); - - local_irq_restore(flags); -} - -/* Called with all cpu's rq->lock held */ -static void enable_window_stats(void) -{ - sched_disable_window_stats = 0; - -} - -enum reset_reason_code { - WINDOW_CHANGE, - POLICY_CHANGE, - HIST_SIZE_CHANGE, - FREQ_AGGREGATE_CHANGE, -}; - -const char *sched_window_reset_reasons[] = { - "WINDOW_CHANGE", - "POLICY_CHANGE", - "HIST_SIZE_CHANGE", -}; - -/* Called with IRQs enabled */ -void reset_all_window_stats(u64 window_start, unsigned int window_size) -{ - int cpu; - unsigned long flags; - u64 start_ts = sched_ktime_clock(); - int reason = WINDOW_CHANGE; - unsigned int old = 0, new = 0; - struct related_thread_group *grp; - - disable_window_stats(); - - reset_all_task_stats(); - - local_irq_save(flags); - - read_lock(&related_thread_group_lock); - - for_each_possible_cpu(cpu) { - struct rq *rq = cpu_rq(cpu); - raw_spin_lock(&rq->lock); - } - - list_for_each_entry(grp, &related_thread_groups, list) { - int j; - - for_each_possible_cpu(j) { - struct group_cpu_time *cpu_time; - /* Protected by rq lock */ - cpu_time = _group_cpu_time(grp, j); - memset(cpu_time, 0, sizeof(struct group_cpu_time)); - if (window_start) - cpu_time->window_start = window_start; - } - } - - if (window_size) { - sched_ravg_window = window_size * TICK_NSEC; - set_hmp_defaults(); - } - - enable_window_stats(); - - for_each_possible_cpu(cpu) { - struct rq *rq = cpu_rq(cpu); - - if (window_start) - rq->window_start = window_start; - rq->curr_runnable_sum = rq->prev_runnable_sum = 0; - rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; - reset_cpu_hmp_stats(cpu, 1); - } - - if (sched_window_stats_policy != sysctl_sched_window_stats_policy) { - reason = POLICY_CHANGE; - old = sched_window_stats_policy; - new = sysctl_sched_window_stats_policy; - sched_window_stats_policy = sysctl_sched_window_stats_policy; - } else if (sched_ravg_hist_size != sysctl_sched_ravg_hist_size) { - reason = HIST_SIZE_CHANGE; - old = sched_ravg_hist_size; - new = sysctl_sched_ravg_hist_size; - sched_ravg_hist_size = sysctl_sched_ravg_hist_size; - } - else if (sched_freq_aggregate != - sysctl_sched_freq_aggregate) { - reason = FREQ_AGGREGATE_CHANGE; - old = sched_freq_aggregate; - new = sysctl_sched_freq_aggregate; - sched_freq_aggregate = sysctl_sched_freq_aggregate; - } - - for_each_possible_cpu(cpu) { - struct rq *rq = cpu_rq(cpu); - raw_spin_unlock(&rq->lock); - } - - read_unlock(&related_thread_group_lock); - - local_irq_restore(flags); - - trace_sched_reset_all_window_stats(window_start, window_size, - sched_ktime_clock() - start_ts, reason, old, new); -} - -static inline void -sync_window_start(struct rq *rq, struct group_cpu_time *cpu_time); - -void sched_get_cpus_busy(struct sched_load *busy, - const struct cpumask *query_cpus) -{ - unsigned long flags; - struct rq *rq; - const int cpus = cpumask_weight(query_cpus); - u64 load[cpus], group_load[cpus]; - u64 nload[cpus], ngload[cpus]; - u64 pload[cpus]; - unsigned int cur_freq[cpus], max_freq[cpus]; - int notifier_sent[cpus]; - int early_detection[cpus]; - int cpu, i = 0; - unsigned int window_size; - u64 max_prev_sum = 0; - int max_busy_cpu = cpumask_first(query_cpus); - struct related_thread_group *grp; - - if (unlikely(cpus == 0)) - return; - - /* - * This function could be called in timer context, and the - * current task may have been executing for a long time. Ensure - * that the window stats are current by doing an update. - */ - read_lock(&related_thread_group_lock); - - local_irq_save(flags); - for_each_cpu(cpu, query_cpus) - raw_spin_lock(&cpu_rq(cpu)->lock); - - window_size = sched_ravg_window; - - for_each_cpu(cpu, query_cpus) { - rq = cpu_rq(cpu); - - update_task_ravg(rq->curr, rq, TASK_UPDATE, sched_ktime_clock(), - 0); - cur_freq[i] = cpu_cycles_to_freq(rq->cc.cycles, rq->cc.time); - - load[i] = rq->old_busy_time = rq->prev_runnable_sum; - nload[i] = rq->nt_prev_runnable_sum; - pload[i] = rq->hmp_stats.pred_demands_sum; - rq->old_estimated_time = pload[i]; - - if (load[i] > max_prev_sum) { - max_prev_sum = load[i]; - max_busy_cpu = cpu; - } - - notifier_sent[i] = rq->notifier_sent; - early_detection[i] = (rq->ed_task != NULL); - rq->notifier_sent = 0; - cur_freq[i] = cpu_cur_freq(cpu); - max_freq[i] = cpu_max_freq(cpu); - i++; - } - - for_each_related_thread_group(grp) { - for_each_cpu(cpu, query_cpus) { - /* Protected by rq_lock */ - struct group_cpu_time *cpu_time = - _group_cpu_time(grp, cpu); - sync_window_start(cpu_rq(cpu), cpu_time); - } - } - - i = 0; - for_each_cpu(cpu, query_cpus) { - group_load[i] = 0; - ngload[i] = 0; - - if (early_detection[i]) - goto skip_early; - - rq = cpu_rq(cpu); - if (!notifier_sent[i]) { - if (cpu == max_busy_cpu) - group_load_in_freq_domain( - &rq->freq_domain_cpumask, - &group_load[i], &ngload[i]); - } else { - _group_load_in_cpu(cpu, &group_load[i], &ngload[i]); - } - - load[i] += group_load[i]; - nload[i] += ngload[i]; - /* - * Scale load in reference to cluster max_possible_freq. - * - * Note that scale_load_to_cpu() scales load in reference to - * the cluster max_freq. - */ - load[i] = scale_load_to_cpu(load[i], cpu); - nload[i] = scale_load_to_cpu(nload[i], cpu); - pload[i] = scale_load_to_cpu(pload[i], cpu); -skip_early: - i++; - } - - for_each_cpu(cpu, query_cpus) - raw_spin_unlock(&(cpu_rq(cpu))->lock); - local_irq_restore(flags); - - read_unlock(&related_thread_group_lock); - - i = 0; - for_each_cpu(cpu, query_cpus) { - rq = cpu_rq(cpu); - - if (early_detection[i]) { - busy[i].prev_load = div64_u64(sched_ravg_window, - NSEC_PER_USEC); - busy[i].new_task_load = 0; - goto exit_early; - } - - if (!notifier_sent[i]) { - load[i] = scale_load_to_freq(load[i], max_freq[i], - cur_freq[i]); - nload[i] = scale_load_to_freq(nload[i], max_freq[i], - cur_freq[i]); - if (load[i] > window_size) - load[i] = window_size; - if (nload[i] > window_size) - nload[i] = window_size; - - load[i] = scale_load_to_freq(load[i], cur_freq[i], - cpu_max_possible_freq(cpu)); - nload[i] = scale_load_to_freq(nload[i], cur_freq[i], - cpu_max_possible_freq(cpu)); - } else { - load[i] = scale_load_to_freq(load[i], max_freq[i], - cpu_max_possible_freq(cpu)); - nload[i] = scale_load_to_freq(nload[i], max_freq[i], - cpu_max_possible_freq(cpu)); - } - pload[i] = scale_load_to_freq(pload[i], max_freq[i], - rq->cluster->max_possible_freq); - - busy[i].prev_load = div64_u64(load[i], NSEC_PER_USEC); - busy[i].new_task_load = div64_u64(nload[i], NSEC_PER_USEC); - busy[i].predicted_load = div64_u64(pload[i], NSEC_PER_USEC); - -exit_early: - trace_sched_get_busy(cpu, busy[i].prev_load, - busy[i].new_task_load, - busy[i].predicted_load, - early_detection[i]); - i++; - } -} - -void sched_set_io_is_busy(int val) -{ - sched_io_is_busy = val; -} - -int sched_set_window(u64 window_start, unsigned int window_size) -{ - u64 now, cur_jiffies, jiffy_ktime_ns; - s64 ws; - unsigned long flags; - - if (window_size * TICK_NSEC < MIN_SCHED_RAVG_WINDOW) - return -EINVAL; - - mutex_lock(&policy_mutex); - - /* - * Get a consistent view of ktime, jiffies, and the time - * since the last jiffy (based on last_jiffies_update). - */ - local_irq_save(flags); - cur_jiffies = jiffy_to_ktime_ns(&now, &jiffy_ktime_ns); - local_irq_restore(flags); - - /* translate window_start from jiffies to nanoseconds */ - ws = (window_start - cur_jiffies); /* jiffy difference */ - ws *= TICK_NSEC; - ws += jiffy_ktime_ns; - - /* roll back calculated window start so that it is in - * the past (window stats must have a current window) */ - while (ws > now) - ws -= (window_size * TICK_NSEC); - - BUG_ON(sched_ktime_clock() < ws); - - reset_all_window_stats(ws, window_size); - - sched_update_freq_max_load(cpu_possible_mask); - - mutex_unlock(&policy_mutex); - - return 0; -} - -static void fixup_busy_time(struct task_struct *p, int new_cpu) -{ - struct rq *src_rq = task_rq(p); - struct rq *dest_rq = cpu_rq(new_cpu); - u64 wallclock; - u64 *src_curr_runnable_sum, *dst_curr_runnable_sum; - u64 *src_prev_runnable_sum, *dst_prev_runnable_sum; - u64 *src_nt_curr_runnable_sum, *dst_nt_curr_runnable_sum; - u64 *src_nt_prev_runnable_sum, *dst_nt_prev_runnable_sum; - int migrate_type; - struct migration_sum_data d; - bool new_task; - struct related_thread_group *grp; - - if (!sched_enable_hmp || (!p->on_rq && p->state != TASK_WAKING)) - return; - - if (exiting_task(p)) { - clear_ed_task(p, src_rq); - return; - } - - if (p->state == TASK_WAKING) - double_rq_lock(src_rq, dest_rq); - - if (sched_disable_window_stats) - goto done; - - wallclock = sched_ktime_clock(); - - update_task_ravg(task_rq(p)->curr, task_rq(p), - TASK_UPDATE, - wallclock, 0); - update_task_ravg(dest_rq->curr, dest_rq, - TASK_UPDATE, wallclock, 0); - - update_task_ravg(p, task_rq(p), TASK_MIGRATE, - wallclock, 0); - - update_task_cpu_cycles(p, new_cpu); - - new_task = is_new_task(p); - /* Protected by rq_lock */ - grp = p->grp; - if (grp && sched_freq_aggregate) { - struct group_cpu_time *cpu_time; - - migrate_type = GROUP_TO_GROUP; - /* Protected by rq_lock */ - cpu_time = _group_cpu_time(grp, cpu_of(src_rq)); - d.src_rq = NULL; - d.src_cpu_time = cpu_time; - src_curr_runnable_sum = &cpu_time->curr_runnable_sum; - src_prev_runnable_sum = &cpu_time->prev_runnable_sum; - src_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; - src_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; - - /* Protected by rq_lock */ - cpu_time = _group_cpu_time(grp, cpu_of(dest_rq)); - d.dst_rq = NULL; - d.dst_cpu_time = cpu_time; - dst_curr_runnable_sum = &cpu_time->curr_runnable_sum; - dst_prev_runnable_sum = &cpu_time->prev_runnable_sum; - dst_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; - dst_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; - sync_window_start(dest_rq, cpu_time); - } else { - migrate_type = RQ_TO_RQ; - d.src_rq = src_rq; - d.src_cpu_time = NULL; - d.dst_rq = dest_rq; - d.dst_cpu_time = NULL; - src_curr_runnable_sum = &src_rq->curr_runnable_sum; - src_prev_runnable_sum = &src_rq->prev_runnable_sum; - src_nt_curr_runnable_sum = &src_rq->nt_curr_runnable_sum; - src_nt_prev_runnable_sum = &src_rq->nt_prev_runnable_sum; - - dst_curr_runnable_sum = &dest_rq->curr_runnable_sum; - dst_prev_runnable_sum = &dest_rq->prev_runnable_sum; - dst_nt_curr_runnable_sum = &dest_rq->nt_curr_runnable_sum; - dst_nt_prev_runnable_sum = &dest_rq->nt_prev_runnable_sum; - } - - if (p->ravg.curr_window) { - *src_curr_runnable_sum -= p->ravg.curr_window; - *dst_curr_runnable_sum += p->ravg.curr_window; - if (new_task) { - *src_nt_curr_runnable_sum -= p->ravg.curr_window; - *dst_nt_curr_runnable_sum += p->ravg.curr_window; - } - } - - if (p->ravg.prev_window) { - *src_prev_runnable_sum -= p->ravg.prev_window; - *dst_prev_runnable_sum += p->ravg.prev_window; - if (new_task) { - *src_nt_prev_runnable_sum -= p->ravg.prev_window; - *dst_nt_prev_runnable_sum += p->ravg.prev_window; - } - } - - if (p == src_rq->ed_task) { - src_rq->ed_task = NULL; - if (!dest_rq->ed_task) - dest_rq->ed_task = p; - } - - trace_sched_migration_update_sum(p, migrate_type, &d); - BUG_ON((s64)*src_prev_runnable_sum < 0); - BUG_ON((s64)*src_curr_runnable_sum < 0); - BUG_ON((s64)*src_nt_prev_runnable_sum < 0); - BUG_ON((s64)*src_nt_curr_runnable_sum < 0); - -done: - if (p->state == TASK_WAKING) - double_rq_unlock(src_rq, dest_rq); -} - -#define sched_up_down_migrate_auto_update 1 -static void check_for_up_down_migrate_update(const struct cpumask *cpus) -{ - int i = cpumask_first(cpus); - - if (!sched_up_down_migrate_auto_update) - return; - - if (cpu_max_possible_capacity(i) == max_possible_capacity) - return; - - if (cpu_max_possible_freq(i) == cpu_max_freq(i)) - up_down_migrate_scale_factor = 1024; - else - up_down_migrate_scale_factor = (1024 * - cpu_max_possible_freq(i)) / cpu_max_freq(i); - - update_up_down_migrate(); -} - -/* Return cluster which can offer required capacity for group */ -static struct sched_cluster * -best_cluster(struct related_thread_group *grp, u64 total_demand) -{ - struct sched_cluster *cluster = NULL; - - for_each_sched_cluster(cluster) { - if (group_will_fit(cluster, grp, total_demand)) - return cluster; - } - - return NULL; -} - -static void _set_preferred_cluster(struct related_thread_group *grp) -{ - struct task_struct *p; - u64 combined_demand = 0; - - if (!sysctl_sched_enable_colocation) { - grp->last_update = sched_ktime_clock(); - grp->preferred_cluster = NULL; - return; - } - - /* - * wakeup of two or more related tasks could race with each other and - * could result in multiple calls to _set_preferred_cluster being issued - * at same time. Avoid overhead in such cases of rechecking preferred - * cluster - */ - if (sched_ktime_clock() - grp->last_update < sched_ravg_window / 10) - return; - - list_for_each_entry(p, &grp->tasks, grp_list) - combined_demand += p->ravg.demand; - - grp->preferred_cluster = best_cluster(grp, combined_demand); - grp->last_update = sched_ktime_clock(); - trace_sched_set_preferred_cluster(grp, combined_demand); -} - -static void set_preferred_cluster(struct related_thread_group *grp) -{ - raw_spin_lock(&grp->lock); - _set_preferred_cluster(grp); - raw_spin_unlock(&grp->lock); -} - -#define ADD_TASK 0 -#define REM_TASK 1 - -static void -update_task_ravg(struct task_struct *p, struct rq *rq, - int event, u64 wallclock, u64 irqtime); - -static inline void free_group_cputime(struct related_thread_group *grp) -{ - free_percpu(grp->cpu_time); -} - -static int alloc_group_cputime(struct related_thread_group *grp) -{ - int i; - struct group_cpu_time *cpu_time; - int cpu = raw_smp_processor_id(); - struct rq *rq = cpu_rq(cpu); - u64 window_start = rq->window_start; - - grp->cpu_time = alloc_percpu(struct group_cpu_time); - if (!grp->cpu_time) - return -ENOMEM; - - for_each_possible_cpu(i) { - cpu_time = per_cpu_ptr(grp->cpu_time, i); - memset(cpu_time, 0, sizeof(struct group_cpu_time)); - cpu_time->window_start = window_start; - } - - return 0; -} - -/* - * A group's window_start may be behind. When moving it forward, flip prev/curr - * counters. When moving forward > 1 window, prev counter is set to 0 - */ -static inline void -sync_window_start(struct rq *rq, struct group_cpu_time *cpu_time) -{ - u64 delta; - int nr_windows; - u64 curr_sum = cpu_time->curr_runnable_sum; - u64 nt_curr_sum = cpu_time->nt_curr_runnable_sum; - - delta = rq->window_start - cpu_time->window_start; - if (!delta) - return; - - nr_windows = div64_u64(delta, sched_ravg_window); - if (nr_windows > 1) - curr_sum = nt_curr_sum = 0; - - cpu_time->prev_runnable_sum = curr_sum; - cpu_time->curr_runnable_sum = 0; - - cpu_time->nt_prev_runnable_sum = nt_curr_sum; - cpu_time->nt_curr_runnable_sum = 0; - - cpu_time->window_start = rq->window_start; -} - -/* - * Task's cpu usage is accounted in: - * rq->curr/prev_runnable_sum, when its ->grp is NULL - * grp->cpu_time[cpu]->curr/prev_runnable_sum, when its ->grp is !NULL - * - * Transfer task's cpu usage between those counters when transitioning between - * groups - */ -static void transfer_busy_time(struct rq *rq, struct related_thread_group *grp, - struct task_struct *p, int event) -{ - u64 wallclock; - struct group_cpu_time *cpu_time; - u64 *src_curr_runnable_sum, *dst_curr_runnable_sum; - u64 *src_prev_runnable_sum, *dst_prev_runnable_sum; - u64 *src_nt_curr_runnable_sum, *dst_nt_curr_runnable_sum; - u64 *src_nt_prev_runnable_sum, *dst_nt_prev_runnable_sum; - struct migration_sum_data d; - int migrate_type; - - if (!sched_freq_aggregate) - return; - - wallclock = sched_ktime_clock(); - - update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0); - update_task_ravg(p, rq, TASK_UPDATE, wallclock, 0); - - /* cpu_time protected by related_thread_group_lock, grp->lock rq_lock */ - cpu_time = _group_cpu_time(grp, cpu_of(rq)); - if (event == ADD_TASK) { - sync_window_start(rq, cpu_time); - migrate_type = RQ_TO_GROUP; - d.src_rq = rq; - d.src_cpu_time = NULL; - d.dst_rq = NULL; - d.dst_cpu_time = cpu_time; - src_curr_runnable_sum = &rq->curr_runnable_sum; - dst_curr_runnable_sum = &cpu_time->curr_runnable_sum; - src_prev_runnable_sum = &rq->prev_runnable_sum; - dst_prev_runnable_sum = &cpu_time->prev_runnable_sum; - - src_nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; - dst_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; - src_nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; - dst_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; - } else if (event == REM_TASK) { - migrate_type = GROUP_TO_RQ; - d.src_rq = NULL; - d.src_cpu_time = cpu_time; - d.dst_rq = rq; - d.dst_cpu_time = NULL; - - /* - * In case of REM_TASK, cpu_time->window_start would be - * uptodate, because of the update_task_ravg() we called - * above on the moving task. Hence no need for - * sync_window_start() - */ - src_curr_runnable_sum = &cpu_time->curr_runnable_sum; - dst_curr_runnable_sum = &rq->curr_runnable_sum; - src_prev_runnable_sum = &cpu_time->prev_runnable_sum; - dst_prev_runnable_sum = &rq->prev_runnable_sum; - - src_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; - dst_nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; - src_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; - dst_nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; - } - - *src_curr_runnable_sum -= p->ravg.curr_window; - *dst_curr_runnable_sum += p->ravg.curr_window; - - *src_prev_runnable_sum -= p->ravg.prev_window; - *dst_prev_runnable_sum += p->ravg.prev_window; - - if (is_new_task(p)) { - *src_nt_curr_runnable_sum -= p->ravg.curr_window; - *dst_nt_curr_runnable_sum += p->ravg.curr_window; - *src_nt_prev_runnable_sum -= p->ravg.prev_window; - *dst_nt_prev_runnable_sum += p->ravg.prev_window; - } - - trace_sched_migration_update_sum(p, migrate_type, &d); - - BUG_ON((s64)*src_curr_runnable_sum < 0); - BUG_ON((s64)*src_prev_runnable_sum < 0); -} - -static inline struct group_cpu_time * -task_group_cpu_time(struct task_struct *p, int cpu) -{ - return _group_cpu_time(rcu_dereference(p->grp), cpu); -} - -static inline struct group_cpu_time * -_group_cpu_time(struct related_thread_group *grp, int cpu) -{ - return grp ? per_cpu_ptr(grp->cpu_time, cpu) : NULL; -} - -struct related_thread_group *alloc_related_thread_group(int group_id) -{ - struct related_thread_group *grp; - - grp = kzalloc(sizeof(*grp), GFP_KERNEL); - if (!grp) - return ERR_PTR(-ENOMEM); - - if (alloc_group_cputime(grp)) { - kfree(grp); - return ERR_PTR(-ENOMEM); - } - - grp->id = group_id; - INIT_LIST_HEAD(&grp->tasks); - INIT_LIST_HEAD(&grp->list); - raw_spin_lock_init(&grp->lock); - - return grp; -} - -struct related_thread_group *lookup_related_thread_group(unsigned int group_id) -{ - struct related_thread_group *grp; - - list_for_each_entry(grp, &related_thread_groups, list) { - if (grp->id == group_id) - return grp; - } - - return NULL; -} - -/* See comments before preferred_cluster() */ -static void free_related_thread_group(struct rcu_head *rcu) -{ - struct related_thread_group *grp = container_of(rcu, struct - related_thread_group, rcu); - - free_group_cputime(grp); - kfree(grp); -} - -static void remove_task_from_group(struct task_struct *p) -{ - struct related_thread_group *grp = p->grp; - struct rq *rq; - int empty_group = 1; - - raw_spin_lock(&grp->lock); - - rq = __task_rq_lock(p); - transfer_busy_time(rq, p->grp, p, REM_TASK); - list_del_init(&p->grp_list); - rcu_assign_pointer(p->grp, NULL); - __task_rq_unlock(rq); - - if (!list_empty(&grp->tasks)) { - empty_group = 0; - _set_preferred_cluster(grp); - } - - raw_spin_unlock(&grp->lock); - - if (empty_group) { - list_del(&grp->list); - call_rcu(&grp->rcu, free_related_thread_group); - } -} - -static int -add_task_to_group(struct task_struct *p, struct related_thread_group *grp) -{ - struct rq *rq; - - raw_spin_lock(&grp->lock); - - /* - * Change p->grp under rq->lock. Will prevent races with read-side - * reference of p->grp in various hot-paths - */ - rq = __task_rq_lock(p); - transfer_busy_time(rq, grp, p, ADD_TASK); - list_add(&p->grp_list, &grp->tasks); - rcu_assign_pointer(p->grp, grp); - __task_rq_unlock(rq); - - _set_preferred_cluster(grp); - - raw_spin_unlock(&grp->lock); - - return 0; -} - -int sched_set_group_id(struct task_struct *p, unsigned int group_id) -{ - int rc = 0, destroy = 0; - unsigned long flags; - struct related_thread_group *grp = NULL, *new = NULL; - -redo: - raw_spin_lock_irqsave(&p->pi_lock, flags); - - if ((current != p && p->flags & PF_EXITING) || - (!p->grp && !group_id) || - (p->grp && p->grp->id == group_id)) - goto done; - - write_lock(&related_thread_group_lock); - - if (!group_id) { - remove_task_from_group(p); - write_unlock(&related_thread_group_lock); - goto done; - } - - if (p->grp && p->grp->id != group_id) - remove_task_from_group(p); - - grp = lookup_related_thread_group(group_id); - if (!grp && !new) { - /* New group */ - write_unlock(&related_thread_group_lock); - raw_spin_unlock_irqrestore(&p->pi_lock, flags); - new = alloc_related_thread_group(group_id); - if (IS_ERR(new)) - return -ENOMEM; - destroy = 1; - /* Rerun checks (like task exiting), since we dropped pi_lock */ - goto redo; - } else if (!grp && new) { - /* New group - use object allocated before */ - destroy = 0; - list_add(&new->list, &related_thread_groups); - grp = new; - } - - BUG_ON(!grp); - rc = add_task_to_group(p, grp); - write_unlock(&related_thread_group_lock); -done: - raw_spin_unlock_irqrestore(&p->pi_lock, flags); - - if (new && destroy) { - free_group_cputime(new); - kfree(new); - } - - return rc; -} - -unsigned int sched_get_group_id(struct task_struct *p) -{ - unsigned int group_id; - struct related_thread_group *grp; - - rcu_read_lock(); - grp = task_related_thread_group(p); - group_id = grp ? grp->id : 0; - rcu_read_unlock(); - - return group_id; -} - -static void update_cpu_cluster_capacity(const cpumask_t *cpus) -{ - int i; - struct sched_cluster *cluster; - struct cpumask cpumask; - - cpumask_copy(&cpumask, cpus); - pre_big_task_count_change(cpu_possible_mask); - - for_each_cpu(i, &cpumask) { - cluster = cpu_rq(i)->cluster; - cpumask_andnot(&cpumask, &cpumask, &cluster->cpus); - - cluster->capacity = compute_capacity(cluster); - cluster->load_scale_factor = compute_load_scale_factor(cluster); - - /* 'cpus' can contain cpumask more than one cluster */ - check_for_up_down_migrate_update(&cluster->cpus); - } - - __update_min_max_capacity(); - - post_big_task_count_change(cpu_possible_mask); -} - -static DEFINE_SPINLOCK(cpu_freq_min_max_lock); -void sched_update_cpu_freq_min_max(const cpumask_t *cpus, u32 fmin, u32 fmax) -{ - struct cpumask cpumask; - struct sched_cluster *cluster; - int i, update_capacity = 0; - unsigned long flags; - - spin_lock_irqsave(&cpu_freq_min_max_lock, flags); - cpumask_copy(&cpumask, cpus); - for_each_cpu(i, &cpumask) { - cluster = cpu_rq(i)->cluster; - cpumask_andnot(&cpumask, &cpumask, &cluster->cpus); - - update_capacity += (cluster->max_mitigated_freq != fmax); - cluster->max_mitigated_freq = fmax; - } - spin_unlock_irqrestore(&cpu_freq_min_max_lock, flags); - - if (update_capacity) - update_cpu_cluster_capacity(cpus); -} - -static int cpufreq_notifier_policy(struct notifier_block *nb, - unsigned long val, void *data) -{ - struct cpufreq_policy *policy = (struct cpufreq_policy *)data; - struct sched_cluster *cluster = NULL; - struct cpumask policy_cluster = *policy->related_cpus; - unsigned int orig_max_freq = 0; - int i, j, update_capacity = 0; - - if (val != CPUFREQ_NOTIFY && val != CPUFREQ_REMOVE_POLICY && - val != CPUFREQ_CREATE_POLICY) - return 0; - - if (val == CPUFREQ_REMOVE_POLICY || val == CPUFREQ_CREATE_POLICY) { - update_min_max_capacity(); - return 0; - } - - max_possible_freq = max(max_possible_freq, policy->cpuinfo.max_freq); - if (min_max_freq == 1) - min_max_freq = UINT_MAX; - min_max_freq = min(min_max_freq, policy->cpuinfo.max_freq); - BUG_ON(!min_max_freq); - BUG_ON(!policy->max); - - for_each_cpu(i, &policy_cluster) { - cluster = cpu_rq(i)->cluster; - cpumask_andnot(&policy_cluster, &policy_cluster, - &cluster->cpus); - - orig_max_freq = cluster->max_freq; - cluster->min_freq = policy->min; - cluster->max_freq = policy->max; - cluster->cur_freq = policy->cur; - - if (!cluster->freq_init_done) { - mutex_lock(&cluster_lock); - for_each_cpu(j, &cluster->cpus) - cpumask_copy(&cpu_rq(j)->freq_domain_cpumask, - policy->related_cpus); - cluster->max_possible_freq = policy->cpuinfo.max_freq; - cluster->max_possible_capacity = - compute_max_possible_capacity(cluster); - cluster->freq_init_done = true; - - sort_clusters(); - update_all_clusters_stats(); - mutex_unlock(&cluster_lock); - continue; - } - - update_capacity += (orig_max_freq != cluster->max_freq); - } - - if (update_capacity) - update_cpu_cluster_capacity(policy->related_cpus); - - return 0; -} - -static int cpufreq_notifier_trans(struct notifier_block *nb, - unsigned long val, void *data) -{ - struct cpufreq_freqs *freq = (struct cpufreq_freqs *)data; - unsigned int cpu = freq->cpu, new_freq = freq->new; - unsigned long flags; - struct sched_cluster *cluster; - struct cpumask policy_cpus = cpu_rq(cpu)->freq_domain_cpumask; - int i, j; - - if (val != CPUFREQ_POSTCHANGE) - return 0; - - BUG_ON(!new_freq); - - if (cpu_cur_freq(cpu) == new_freq) - return 0; - - for_each_cpu(i, &policy_cpus) { - cluster = cpu_rq(i)->cluster; - - for_each_cpu(j, &cluster->cpus) { - struct rq *rq = cpu_rq(j); - - raw_spin_lock_irqsave(&rq->lock, flags); - update_task_ravg(rq->curr, rq, TASK_UPDATE, - sched_ktime_clock(), 0); - raw_spin_unlock_irqrestore(&rq->lock, flags); - } - - cluster->cur_freq = new_freq; - cpumask_andnot(&policy_cpus, &policy_cpus, &cluster->cpus); - } - - return 0; -} - -static int pwr_stats_ready_notifier(struct notifier_block *nb, - unsigned long cpu, void *data) -{ - cpumask_t mask = CPU_MASK_NONE; - - cpumask_set_cpu(cpu, &mask); - sched_update_freq_max_load(&mask); - - mutex_lock(&cluster_lock); - sort_clusters(); - mutex_unlock(&cluster_lock); - - return 0; -} - -static struct notifier_block notifier_policy_block = { - .notifier_call = cpufreq_notifier_policy -}; - -static struct notifier_block notifier_trans_block = { - .notifier_call = cpufreq_notifier_trans -}; - -static struct notifier_block notifier_pwr_stats_ready = { - .notifier_call = pwr_stats_ready_notifier -}; - -int __weak register_cpu_pwr_stats_ready_notifier(struct notifier_block *nb) -{ - return -EINVAL; -} - -static int register_sched_callback(void) -{ - int ret; - - if (!sched_enable_hmp) - return 0; - - ret = cpufreq_register_notifier(¬ifier_policy_block, - CPUFREQ_POLICY_NOTIFIER); - - if (!ret) - ret = cpufreq_register_notifier(¬ifier_trans_block, - CPUFREQ_TRANSITION_NOTIFIER); - - register_cpu_pwr_stats_ready_notifier(¬ifier_pwr_stats_ready); - - return 0; -} - -/* - * cpufreq callbacks can be registered at core_initcall or later time. - * Any registration done prior to that is "forgotten" by cpufreq. See - * initialization of variable init_cpufreq_transition_notifier_list_called - * for further information. - */ -core_initcall(register_sched_callback); - -static inline int update_preferred_cluster(struct related_thread_group *grp, - struct task_struct *p, u32 old_load) -{ - u32 new_load = task_load(p); - - if (!grp) - return 0; - - /* - * Update if task's load has changed significantly or a complete window - * has passed since we last updated preference - */ - if (abs(new_load - old_load) > sched_ravg_window / 4 || - sched_ktime_clock() - grp->last_update > sched_ravg_window) - return 1; - - return 0; -} - -static bool early_detection_notify(struct rq *rq, u64 wallclock) -{ - struct task_struct *p; - int loop_max = 10; - - if (!sched_boost() || !rq->cfs.h_nr_running) - return 0; - - rq->ed_task = NULL; - list_for_each_entry(p, &rq->cfs_tasks, se.group_node) { - if (!loop_max) - break; - - if (wallclock - p->last_wake_ts >= EARLY_DETECTION_DURATION) { - rq->ed_task = p; - return 1; - } - - loop_max--; - } - - return 0; -} - -#else /* CONFIG_SCHED_HMP */ - -static inline void fixup_busy_time(struct task_struct *p, int new_cpu) { } -static inline void clear_boost_kick(int cpu) { } -static inline void clear_hmp_request(int cpu) { } -static inline void mark_task_starting(struct task_struct *p) {} -static inline void set_window_start(struct rq *rq) {} -static inline void migrate_sync_cpu(int cpu) {} - -static inline int got_boost_kick(void) -{ - return 0; -} - -int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) -{ - return 0; -} - -static inline void update_cluster_topology(void) { } - -static void -update_task_ravg(struct task_struct *p, struct rq *rq, - int event, u64 wallclock, u64 irqtime) -{ -} - -static bool early_detection_notify(struct rq *rq, u64 wallclock) -{ - return 0; -} - -#endif /* CONFIG_SCHED_HMP */ - #ifdef CONFIG_SMP /* * This is how migration works: @@ -5370,6 +2235,44 @@ void __dl_clear_params(struct task_struct *p) dl_se->dl_yielded = 0; } +#ifdef CONFIG_SCHED_HMP +/* + * sched_exit() - Set EXITING_TASK_MARKER in task's ravg.demand field + * + * Stop accounting (exiting) task's future cpu usage + * + * We need this so that reset_all_windows_stats() can function correctly. + * reset_all_window_stats() depends on do_each_thread/for_each_thread task + * iterators to reset *all* task's statistics. Exiting tasks however become + * invisible to those iterators. sched_exit() is called on a exiting task prior + * to being removed from task_list, which will let reset_all_window_stats() + * function correctly. + */ +void sched_exit(struct task_struct *p) +{ + unsigned long flags; + int cpu = get_cpu(); + struct rq *rq = cpu_rq(cpu); + u64 wallclock; + + sched_set_group_id(p, 0); + + raw_spin_lock_irqsave(&rq->lock, flags); + /* rq->curr == p */ + wallclock = sched_ktime_clock(); + update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0); + dequeue_task(rq, p, 0); + reset_task_stats(p); + p->ravg.mark_start = wallclock; + p->ravg.sum_history[0] = EXITING_TASK_MARKER; + enqueue_task(rq, p, 0); + clear_ed_task(p, rq); + raw_spin_unlock_irqrestore(&rq->lock, flags); + + put_cpu(); +} +#endif /* CONFIG_SCHED_HMP */ + /* * Perform scheduler related setup for a newly forked process p. * p is forked by current. @@ -11591,7 +8494,7 @@ int sched_rr_handler(struct ctl_table *table, int write, #ifdef CONFIG_CGROUP_SCHED -static inline struct task_group *css_tg(struct cgroup_subsys_state *css) +inline struct task_group *css_tg(struct cgroup_subsys_state *css) { return css ? container_of(css, struct task_group, css) : NULL; } @@ -11665,45 +8568,6 @@ static void cpu_cgroup_attach(struct cgroup_taskset *tset) sched_move_task(task); } -#ifdef CONFIG_SCHED_HMP - -static u64 cpu_upmigrate_discourage_read_u64(struct cgroup_subsys_state *css, - struct cftype *cft) -{ - struct task_group *tg = css_tg(css); - - return tg->upmigrate_discouraged; -} - -static int cpu_upmigrate_discourage_write_u64(struct cgroup_subsys_state *css, - struct cftype *cft, u64 upmigrate_discourage) -{ - struct task_group *tg = css_tg(css); - int discourage = upmigrate_discourage > 0; - - if (tg->upmigrate_discouraged == discourage) - return 0; - - /* - * Revisit big-task classification for tasks of this cgroup. It would - * have been efficient to walk tasks of just this cgroup in running - * state, but we don't have easy means to do that. Walk all tasks in - * running state on all cpus instead and re-visit their big task - * classification. - */ - get_online_cpus(); - pre_big_task_count_change(cpu_online_mask); - - tg->upmigrate_discouraged = discourage; - - post_big_task_count_change(cpu_online_mask); - put_online_cpus(); - - return 0; -} - -#endif /* CONFIG_SCHED_HMP */ - #ifdef CONFIG_FAIR_GROUP_SCHED static int cpu_shares_write_u64(struct cgroup_subsys_state *css, struct cftype *cftype, u64 shareval) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 4806ec37035d..30f32f07e54f 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2577,438 +2577,8 @@ static u32 __compute_runnable_contrib(u64 n) return contrib + runnable_avg_yN_sum[n]; } -struct cpu_pwr_stats __weak *get_cpu_pwr_stats(void) -{ - return NULL; -} - -enum sched_boost_type { - SCHED_BOOST_NONE, - SCHED_BOOST_ON_BIG, - SCHED_BOOST_ON_ALL, -}; - #ifdef CONFIG_SCHED_HMP -/* Initial task load. Newly created tasks are assigned this load. */ -unsigned int __read_mostly sched_init_task_load_windows; -unsigned int __read_mostly sysctl_sched_init_task_load_pct = 15; - -unsigned int max_task_load(void) -{ - return sched_ravg_window; -} - -/* Use this knob to turn on or off HMP-aware task placement logic */ -unsigned int __read_mostly sched_enable_hmp = 0; - -/* A cpu can no longer accomodate more tasks if: - * - * rq->nr_running > sysctl_sched_spill_nr_run || - * rq->hmp_stats.cumulative_runnable_avg > sched_spill_load - */ -unsigned int __read_mostly sysctl_sched_spill_nr_run = 10; - -/* - * Place sync wakee tasks those have less than configured demand to the waker's - * cluster. - */ -unsigned int __read_mostly sched_small_wakee_task_load; -unsigned int __read_mostly sysctl_sched_small_wakee_task_load_pct = 10; - -unsigned int __read_mostly sched_big_waker_task_load; -unsigned int __read_mostly sysctl_sched_big_waker_task_load_pct = 25; - -/* - * CPUs with load greater than the sched_spill_load_threshold are not - * eligible for task placement. When all CPUs in a cluster achieve a - * load higher than this level, tasks becomes eligible for inter - * cluster migration. - */ -unsigned int __read_mostly sched_spill_load; -unsigned int __read_mostly sysctl_sched_spill_load_pct = 100; - -/* - * Tasks whose bandwidth consumption on a cpu is more than - * sched_upmigrate are considered "big" tasks. Big tasks will be - * considered for "up" migration, i.e migrating to a cpu with better - * capacity. - */ -unsigned int __read_mostly sched_upmigrate; -unsigned int __read_mostly sysctl_sched_upmigrate_pct = 80; - -/* - * Big tasks, once migrated, will need to drop their bandwidth - * consumption to less than sched_downmigrate before they are "down" - * migrated. - */ -unsigned int __read_mostly sched_downmigrate; -unsigned int __read_mostly sysctl_sched_downmigrate_pct = 60; - -#define SCHED_UPMIGRATE_MIN_NICE 15 - -/* - * The load scale factor of a CPU gets boosted when its max frequency - * is restricted due to which the tasks are migrating to higher capacity - * CPUs early. The sched_upmigrate threshold is auto-upgraded by - * rq->max_possible_freq/rq->max_freq of a lower capacity CPU. - */ -unsigned int up_down_migrate_scale_factor = 1024; - -/* - * Scheduler boost is a mechanism to temporarily place tasks on CPUs - * with higher capacity than those where a task would have normally - * ended up with their load characteristics. Any entity enabling - * boost is responsible for disabling it as well. - */ -unsigned int sysctl_sched_boost; - -/* - * Scheduler selects and places task to its previous CPU if sleep time is - * less than sysctl_sched_select_prev_cpu_us. - */ -static unsigned int __read_mostly -sched_short_sleep_task_threshold = 2000 * NSEC_PER_USEC; -unsigned int __read_mostly sysctl_sched_select_prev_cpu_us = 2000; - -static unsigned int __read_mostly -sched_long_cpu_selection_threshold = 100 * NSEC_PER_MSEC; - -unsigned int __read_mostly sysctl_sched_restrict_cluster_spill; - -void update_up_down_migrate(void) -{ - unsigned int up_migrate = pct_to_real(sysctl_sched_upmigrate_pct); - unsigned int down_migrate = pct_to_real(sysctl_sched_downmigrate_pct); - unsigned int delta; - - if (up_down_migrate_scale_factor == 1024) - goto done; - - delta = up_migrate - down_migrate; - - up_migrate /= NSEC_PER_USEC; - up_migrate *= up_down_migrate_scale_factor; - up_migrate >>= 10; - up_migrate *= NSEC_PER_USEC; - - up_migrate = min(up_migrate, sched_ravg_window); - - down_migrate /= NSEC_PER_USEC; - down_migrate *= up_down_migrate_scale_factor; - down_migrate >>= 10; - down_migrate *= NSEC_PER_USEC; - - down_migrate = min(down_migrate, up_migrate - delta); -done: - sched_upmigrate = up_migrate; - sched_downmigrate = down_migrate; -} - -void set_hmp_defaults(void) -{ - sched_spill_load = - pct_to_real(sysctl_sched_spill_load_pct); - - update_up_down_migrate(); - - sched_major_task_runtime = - mult_frac(sched_ravg_window, MAJOR_TASK_PCT, 100); - - sched_init_task_load_windows = - div64_u64((u64)sysctl_sched_init_task_load_pct * - (u64)sched_ravg_window, 100); - - sched_short_sleep_task_threshold = sysctl_sched_select_prev_cpu_us * - NSEC_PER_USEC; - - sched_small_wakee_task_load = - div64_u64((u64)sysctl_sched_small_wakee_task_load_pct * - (u64)sched_ravg_window, 100); - - sched_big_waker_task_load = - div64_u64((u64)sysctl_sched_big_waker_task_load_pct * - (u64)sched_ravg_window, 100); -} - -u32 sched_get_init_task_load(struct task_struct *p) -{ - return p->init_load_pct; -} - -int sched_set_init_task_load(struct task_struct *p, int init_load_pct) -{ - if (init_load_pct < 0 || init_load_pct > 100) - return -EINVAL; - - p->init_load_pct = init_load_pct; - - return 0; -} - -#ifdef CONFIG_CGROUP_SCHED - -static inline int upmigrate_discouraged(struct task_struct *p) -{ - return task_group(p)->upmigrate_discouraged; -} - -#else - -static inline int upmigrate_discouraged(struct task_struct *p) -{ - return 0; -} - -#endif - -/* Is a task "big" on its current cpu */ -static inline int __is_big_task(struct task_struct *p, u64 scaled_load) -{ - int nice = task_nice(p); - - if (nice > SCHED_UPMIGRATE_MIN_NICE || upmigrate_discouraged(p)) - return 0; - - return scaled_load > sched_upmigrate; -} - -static inline int is_big_task(struct task_struct *p) -{ - return __is_big_task(p, scale_load_to_cpu(task_load(p), task_cpu(p))); -} - -static inline u64 cpu_load(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - - return scale_load_to_cpu(rq->hmp_stats.cumulative_runnable_avg, cpu); -} - -static inline u64 cpu_load_sync(int cpu, int sync) -{ - return scale_load_to_cpu(cpu_cravg_sync(cpu, sync), cpu); -} - -static int boost_refcount; -static DEFINE_SPINLOCK(boost_lock); -static DEFINE_MUTEX(boost_mutex); - -static void boost_kick_cpus(void) -{ - int i; - - for_each_online_cpu(i) { - if (cpu_capacity(i) != max_capacity) - boost_kick(i); - } -} - -int sched_boost(void) -{ - return boost_refcount > 0; -} - -int sched_set_boost(int enable) -{ - unsigned long flags; - int ret = 0; - int old_refcount; - - if (!sched_enable_hmp) - return -EINVAL; - - spin_lock_irqsave(&boost_lock, flags); - - old_refcount = boost_refcount; - - if (enable == 1) { - boost_refcount++; - } else if (!enable) { - if (boost_refcount >= 1) - boost_refcount--; - else - ret = -EINVAL; - } else { - ret = -EINVAL; - } - - if (!old_refcount && boost_refcount) - boost_kick_cpus(); - - trace_sched_set_boost(boost_refcount); - spin_unlock_irqrestore(&boost_lock, flags); - - return ret; -} - -int sched_boost_handler(struct ctl_table *table, int write, - void __user *buffer, size_t *lenp, - loff_t *ppos) -{ - int ret; - - mutex_lock(&boost_mutex); - if (!write) - sysctl_sched_boost = sched_boost(); - - ret = proc_dointvec(table, write, buffer, lenp, ppos); - if (ret || !write) - goto done; - - ret = (sysctl_sched_boost <= 1) ? - sched_set_boost(sysctl_sched_boost) : -EINVAL; - -done: - mutex_unlock(&boost_mutex); - return ret; -} - -/* - * Task will fit on a cpu if it's bandwidth consumption on that cpu - * will be less than sched_upmigrate. A big task that was previously - * "up" migrated will be considered fitting on "little" cpu if its - * bandwidth consumption on "little" cpu will be less than - * sched_downmigrate. This will help avoid frequenty migrations for - * tasks with load close to the upmigrate threshold - */ - -static int task_load_will_fit(struct task_struct *p, u64 task_load, int cpu, - enum sched_boost_type boost_type) -{ - int upmigrate; - - if (cpu_capacity(cpu) == max_capacity) - return 1; - - if (boost_type != SCHED_BOOST_ON_BIG) { - if (task_nice(p) > SCHED_UPMIGRATE_MIN_NICE || - upmigrate_discouraged(p)) - return 1; - - upmigrate = sched_upmigrate; - if (cpu_capacity(task_cpu(p)) > cpu_capacity(cpu)) - upmigrate = sched_downmigrate; - - if (task_load < upmigrate) - return 1; - } - - return 0; -} - -static enum sched_boost_type sched_boost_type(void) -{ - if (sched_boost()) { - if (min_possible_efficiency != max_possible_efficiency) - return SCHED_BOOST_ON_BIG; - else - return SCHED_BOOST_ON_ALL; - } - return SCHED_BOOST_NONE; -} - -static int task_will_fit(struct task_struct *p, int cpu) -{ - u64 tload = scale_load_to_cpu(task_load(p), cpu); - - return task_load_will_fit(p, tload, cpu, sched_boost_type()); -} - -int group_will_fit(struct sched_cluster *cluster, - struct related_thread_group *grp, u64 demand) -{ - int cpu = cluster_first_cpu(cluster); - int prev_capacity = 0; - unsigned int threshold = sched_upmigrate; - u64 load; - - if (cluster->capacity == max_capacity) - return 1; - - if (grp->preferred_cluster) - prev_capacity = grp->preferred_cluster->capacity; - - if (cluster->capacity < prev_capacity) - threshold = sched_downmigrate; - - load = scale_load_to_cpu(demand, cpu); - if (load < threshold) - return 1; - - return 0; -} - -/* - * Return the cost of running task p on CPU cpu. This function - * currently assumes that task p is the only task which will run on - * the CPU. - */ -unsigned int power_cost(int cpu, u64 demand) -{ - int first, mid, last; - struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); - struct cpu_pstate_pwr *costs; - struct freq_max_load *max_load; - int total_static_pwr_cost = 0; - struct rq *rq = cpu_rq(cpu); - unsigned int pc; - - if (!per_cpu_info || !per_cpu_info[cpu].ptable) - /* When power aware scheduling is not in use, or CPU - * power data is not available, just use the CPU - * capacity as a rough stand-in for real CPU power - * numbers, assuming bigger CPUs are more power - * hungry. */ - return cpu_max_possible_capacity(cpu); - - rcu_read_lock(); - max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); - if (!max_load) { - pc = cpu_max_possible_capacity(cpu); - goto unlock; - } - - costs = per_cpu_info[cpu].ptable; - - if (demand <= max_load->freqs[0].hdemand) { - pc = costs[0].power; - goto unlock; - } else if (demand > max_load->freqs[max_load->length - 1].hdemand) { - pc = costs[max_load->length - 1].power; - goto unlock; - } - - first = 0; - last = max_load->length - 1; - mid = (last - first) >> 1; - while (1) { - if (demand <= max_load->freqs[mid].hdemand) - last = mid; - else - first = mid; - - if (last - first == 1) - break; - mid = first + ((last - first) >> 1); - } - - pc = costs[last].power; - -unlock: - rcu_read_unlock(); - - if (idle_cpu(cpu) && rq->cstate) { - total_static_pwr_cost += rq->static_cpu_pwr_cost; - if (rq->cluster->dstate) - total_static_pwr_cost += - rq->cluster->static_cluster_pwr_cost; - } - - return pc + total_static_pwr_cost; - -} - struct cpu_select_env { struct task_struct *p; struct related_thread_group *rtg; @@ -3034,45 +2604,7 @@ struct cluster_cpu_stats { s64 highest_spare_capacity; }; -#define UP_MIGRATION 1 -#define DOWN_MIGRATION 2 -#define IRQLOAD_MIGRATION 3 - -/* - * Invoked from three places: - * 1) try_to_wake_up() -> ... -> select_best_cpu() - * 2) scheduler_tick() -> ... -> migration_needed() -> select_best_cpu() - * 3) can_migrate_task() - * - * Its safe to de-reference p->grp in first case (since p->pi_lock is held) - * but not in other cases. p->grp is hence freed after a RCU grace period and - * accessed under rcu_read_lock() - */ -static inline int -preferred_cluster(struct sched_cluster *cluster, struct task_struct *p) -{ - struct related_thread_group *grp; - int rc = 0; - - rcu_read_lock(); - - grp = task_related_thread_group(p); - if (!grp || !sysctl_sched_enable_colocation) - rc = 1; - else - rc = (grp->preferred_cluster == cluster); - - rcu_read_unlock(); - return rc; -} - -static inline struct sched_cluster *rq_cluster(struct rq *rq) -{ - return rq->cluster; -} - -static int -spill_threshold_crossed(struct cpu_select_env *env, struct rq *rq) +static int spill_threshold_crossed(struct cpu_select_env *env, struct rq *rq) { u64 total_load; @@ -3363,7 +2895,7 @@ static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, } } } -#endif +#endif /* CONFIG_SCHED_HMP_CSTATE_AWARE */ static void update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, struct cpu_select_env *env) @@ -3608,54 +3140,6 @@ out: return target; } -static void -inc_nr_big_task(struct hmp_sched_stats *stats, struct task_struct *p) -{ - if (!sched_enable_hmp || sched_disable_window_stats) - return; - - if (is_big_task(p)) - stats->nr_big_tasks++; -} - -static void -dec_nr_big_task(struct hmp_sched_stats *stats, struct task_struct *p) -{ - if (!sched_enable_hmp || sched_disable_window_stats) - return; - - if (is_big_task(p)) - stats->nr_big_tasks--; - - BUG_ON(stats->nr_big_tasks < 0); -} - -static void -inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) -{ - inc_nr_big_task(&rq->hmp_stats, p); - if (change_cra) - inc_cumulative_runnable_avg(&rq->hmp_stats, p); -} - -static void -dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) -{ - dec_nr_big_task(&rq->hmp_stats, p); - if (change_cra) - dec_cumulative_runnable_avg(&rq->hmp_stats, p); -} - -static void reset_hmp_stats(struct hmp_sched_stats *stats, int reset_cra) -{ - stats->nr_big_tasks = 0; - if (reset_cra) { - stats->cumulative_runnable_avg = 0; - stats->pred_demands_sum = 0; - } -} - - #ifdef CONFIG_CFS_BANDWIDTH static inline struct task_group *next_task_group(struct task_group *tg) @@ -3670,7 +3154,7 @@ static inline struct task_group *next_task_group(struct task_group *tg) for (tg = container_of(&task_groups, struct task_group, list); \ ((tg = next_task_group(tg)) && (cfs_rq = tg->cfs_rq[cpu]));) -static void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) +void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) { struct task_group *tg; struct cfs_rq *cfs_rq; @@ -3683,66 +3167,6 @@ static void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) rcu_read_unlock(); } -#else /* CONFIG_CFS_BANDWIDTH */ - -static inline void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) { } - -#endif /* CONFIG_CFS_BANDWIDTH */ - -/* - * Return total number of tasks "eligible" to run on highest capacity cpu - * - * This is simply nr_big_tasks for cpus which are not of max_capacity and - * nr_running for cpus of max_capacity - */ -unsigned int nr_eligible_big_tasks(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - int nr_big = rq->hmp_stats.nr_big_tasks; - int nr = rq->nr_running; - - if (cpu_max_possible_capacity(cpu) != max_possible_capacity) - return nr_big; - - return nr; -} - -/* - * reset_cpu_hmp_stats - reset HMP stats for a cpu - * nr_big_tasks - * cumulative_runnable_avg (iff reset_cra is true) - */ -void reset_cpu_hmp_stats(int cpu, int reset_cra) -{ - reset_cfs_rq_hmp_stats(cpu, reset_cra); - reset_hmp_stats(&cpu_rq(cpu)->hmp_stats, reset_cra); -} - -static void -fixup_nr_big_tasks(struct hmp_sched_stats *stats, struct task_struct *p, - s64 delta) -{ - u64 new_task_load; - u64 old_task_load; - - if (!sched_enable_hmp || sched_disable_window_stats) - return; - - old_task_load = scale_load_to_cpu(task_load(p), task_cpu(p)); - new_task_load = scale_load_to_cpu(delta + task_load(p), task_cpu(p)); - - if (__is_big_task(p, old_task_load) && !__is_big_task(p, new_task_load)) - stats->nr_big_tasks--; - else if (!__is_big_task(p, old_task_load) && - __is_big_task(p, new_task_load)) - stats->nr_big_tasks++; - - BUG_ON(stats->nr_big_tasks < 0); -} - - -#ifdef CONFIG_CFS_BANDWIDTH - static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq); static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, @@ -3751,8 +3175,8 @@ static void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, struct task_struct *p, int change_cra); /* Add task's contribution to a cpu' HMP statistics */ -static void -_inc_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p, int change_cra) +void _inc_hmp_sched_stats_fair(struct rq *rq, + struct task_struct *p, int change_cra) { struct cfs_rq *cfs_rq; struct sched_entity *se = &p->se; @@ -3844,6 +3268,8 @@ static int task_will_be_throttled(struct task_struct *p); #else /* CONFIG_CFS_BANDWIDTH */ +inline void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) { } + static void inc_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p) { @@ -3874,180 +3300,14 @@ static inline int task_will_be_throttled(struct task_struct *p) return 0; } -static void -_inc_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p, int change_cra) +void _inc_hmp_sched_stats_fair(struct rq *rq, + struct task_struct *p, int change_cra) { inc_nr_big_task(&rq->hmp_stats, p); } #endif /* CONFIG_CFS_BANDWIDTH */ -/* - * Walk runqueue of cpu and re-initialize 'nr_big_tasks' counters. - */ -static void update_nr_big_tasks(int cpu) -{ - struct rq *rq = cpu_rq(cpu); - struct task_struct *p; - - /* Do not reset cumulative_runnable_avg */ - reset_cpu_hmp_stats(cpu, 0); - - list_for_each_entry(p, &rq->cfs_tasks, se.group_node) - _inc_hmp_sched_stats_fair(rq, p, 0); -} - -/* Disable interrupts and grab runqueue lock of all cpus listed in @cpus */ -void pre_big_task_count_change(const struct cpumask *cpus) -{ - int i; - - local_irq_disable(); - - for_each_cpu(i, cpus) - raw_spin_lock(&cpu_rq(i)->lock); -} - -/* - * Reinitialize 'nr_big_tasks' counters on all affected cpus - */ -void post_big_task_count_change(const struct cpumask *cpus) -{ - int i; - - /* Assumes local_irq_disable() keeps online cpumap stable */ - for_each_cpu(i, cpus) - update_nr_big_tasks(i); - - for_each_cpu(i, cpus) - raw_spin_unlock(&cpu_rq(i)->lock); - - local_irq_enable(); -} - -DEFINE_MUTEX(policy_mutex); - -static inline int invalid_value_freq_input(unsigned int *data) -{ - if (data == &sysctl_sched_freq_aggregate) - return !(*data == 0 || *data == 1); - - return 0; -} - -static inline int invalid_value(unsigned int *data) -{ - unsigned int val = *data; - - if (data == &sysctl_sched_ravg_hist_size) - return (val < 2 || val > RAVG_HIST_SIZE_MAX); - - if (data == &sysctl_sched_window_stats_policy) - return val >= WINDOW_STATS_INVALID_POLICY; - - return invalid_value_freq_input(data); -} - -/* - * Handle "atomic" update of sysctl_sched_window_stats_policy, - * sysctl_sched_ravg_hist_size and sched_freq_legacy_mode variables. - */ -int sched_window_update_handler(struct ctl_table *table, int write, - void __user *buffer, size_t *lenp, - loff_t *ppos) -{ - int ret; - unsigned int *data = (unsigned int *)table->data; - unsigned int old_val; - - if (!sched_enable_hmp) - return -EINVAL; - - mutex_lock(&policy_mutex); - - old_val = *data; - - ret = proc_dointvec(table, write, buffer, lenp, ppos); - if (ret || !write || (write && (old_val == *data))) - goto done; - - if (invalid_value(data)) { - *data = old_val; - ret = -EINVAL; - goto done; - } - - reset_all_window_stats(0, 0); - -done: - mutex_unlock(&policy_mutex); - - return ret; -} - -/* - * Convert percentage value into absolute form. This will avoid div() operation - * in fast path, to convert task load in percentage scale. - */ -int sched_hmp_proc_update_handler(struct ctl_table *table, int write, - void __user *buffer, size_t *lenp, - loff_t *ppos) -{ - int ret; - unsigned int old_val; - unsigned int *data = (unsigned int *)table->data; - int update_min_nice = 0; - - mutex_lock(&policy_mutex); - - old_val = *data; - - ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); - - if (ret || !write || !sched_enable_hmp) - goto done; - - if (write && (old_val == *data)) - goto done; - - if (data != &sysctl_sched_select_prev_cpu_us) { - /* - * all tunables other than sched_select_prev_cpu_us are - * in percentage. - */ - if (sysctl_sched_downmigrate_pct > - sysctl_sched_upmigrate_pct || *data > 100) { - *data = old_val; - ret = -EINVAL; - goto done; - } - } - - /* - * Big task tunable change will need to re-classify tasks on - * runqueue as big and set their counters appropriately. - * sysctl interface affects secondary variables (*_pct), which is then - * "atomically" carried over to the primary variables. Atomic change - * includes taking runqueue lock of all online cpus and re-initiatizing - * their big counter values based on changed criteria. - */ - if ((data == &sysctl_sched_upmigrate_pct || update_min_nice)) { - get_online_cpus(); - pre_big_task_count_change(cpu_online_mask); - } - - set_hmp_defaults(); - - if ((data == &sysctl_sched_upmigrate_pct || update_min_nice)) { - post_big_task_count_change(cpu_online_mask); - put_online_cpus(); - } - -done: - mutex_unlock(&policy_mutex); - return ret; -} - /* * Reset balance_interval at all sched_domain levels of given cpu, so that it * honors kick. @@ -4111,8 +3371,6 @@ static inline int migration_needed(struct task_struct *p, int cpu) return 0; } -static DEFINE_RAW_SPINLOCK(migration_lock); - static inline int kick_active_balance(struct rq *rq, struct task_struct *p, int new_cpu) { @@ -4133,6 +3391,8 @@ kick_active_balance(struct rq *rq, struct task_struct *p, int new_cpu) return rc; } +static DEFINE_RAW_SPINLOCK(migration_lock); + /* * Check if currently running task should be migrated to a better cpu. * @@ -4163,52 +3423,6 @@ void check_for_migration(struct rq *rq, struct task_struct *p) &rq->active_balance_work); } -static inline int nr_big_tasks(struct rq *rq) -{ - return rq->hmp_stats.nr_big_tasks; -} - -unsigned int cpu_temp(int cpu) -{ - struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); - if (per_cpu_info) - return per_cpu_info[cpu].temp; - else - return 0; -} - -void init_new_task_load(struct task_struct *p) -{ - int i; - u32 init_load_windows = sched_init_task_load_windows; - u32 init_load_pct = current->init_load_pct; - - p->init_load_pct = 0; - rcu_assign_pointer(p->grp, NULL); - INIT_LIST_HEAD(&p->grp_list); - memset(&p->ravg, 0, sizeof(struct ravg)); - p->cpu_cycles = 0; - - if (init_load_pct) - init_load_windows = div64_u64((u64)init_load_pct * - (u64)sched_ravg_window, 100); - - p->ravg.demand = init_load_windows; - p->ravg.pred_demand = 0; - for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) - p->ravg.sum_history[i] = init_load_windows; -} - -/* Return task demand in percentage scale */ -unsigned int pct_task_load(struct task_struct *p) -{ - unsigned int load; - - load = div64_u64((u64)task_load(p) * 100, (u64)max_task_load()); - - return load; -} - #ifdef CONFIG_CFS_BANDWIDTH static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) @@ -4268,85 +3482,6 @@ static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, #else /* CONFIG_SCHED_HMP */ -struct cpu_select_env; -struct sched_cluster; - -static inline int task_will_fit(struct task_struct *p, int cpu) -{ - return 1; -} - -static inline int select_best_cpu(struct task_struct *p, int target, - int reason, int sync) -{ - return 0; -} - -unsigned int power_cost(int cpu, u64 demand) -{ - return SCHED_CAPACITY_SCALE; -} - -static inline int -spill_threshold_crossed(struct cpu_select_env *env, struct rq *rq) -{ - return 0; -} - -static inline int sched_boost(void) -{ - return 0; -} - -static inline int is_big_task(struct task_struct *p) -{ - return 0; -} - -static inline int nr_big_tasks(struct rq *rq) -{ - return 0; -} - -static inline int is_cpu_throttling_imminent(int cpu) -{ - return 0; -} - -static inline int is_task_migration_throttled(struct task_struct *p) -{ - return 0; -} - -unsigned int cpu_temp(int cpu) -{ - return 0; -} - -static inline void -inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } -static inline void -dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } - -static inline void -inc_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p) { } - -static inline void -dec_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p) { } - -static inline int -preferred_cluster(struct sched_cluster *cluster, struct task_struct *p) -{ - return 1; -} - -static inline struct sched_cluster *rq_cluster(struct rq *rq) -{ - return NULL; -} - -void init_new_task_load(struct task_struct *p) { } - static inline void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { } static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, @@ -4730,19 +3865,12 @@ static inline int idle_balance(struct rq *rq) return 0; } -static inline void -inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } -static inline void -dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } - static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, struct task_struct *p, int change_cra) { } static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq, struct task_struct *p, int change_cra) { } -void init_new_task_load(struct task_struct *p) { } - #endif /* CONFIG_SMP */ static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) diff --git a/kernel/sched/hmp.c b/kernel/sched/hmp.c new file mode 100644 index 000000000000..84cce75c6e50 --- /dev/null +++ b/kernel/sched/hmp.c @@ -0,0 +1,3898 @@ +/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Implementation credits: Srivatsa Vaddagiri, Steve Muckle + * Syed Rameez Mustafa, Olav haugan, Joonwoo Park, Pavan Kumar Kondeti + * and Vikram Mulukutla + */ + +#include +#include +#include + +#include "sched.h" + +#include + +static ktime_t ktime_last; +static bool sched_ktime_suspended; + +static bool use_cycle_counter; +static struct cpu_cycle_counter_cb cpu_cycle_counter_cb; + +u64 sched_ktime_clock(void) +{ + if (unlikely(sched_ktime_suspended)) + return ktime_to_ns(ktime_last); + return ktime_get_ns(); +} + +static void sched_resume(void) +{ + sched_ktime_suspended = false; +} + +static int sched_suspend(void) +{ + ktime_last = ktime_get(); + sched_ktime_suspended = true; + return 0; +} + +static struct syscore_ops sched_syscore_ops = { + .resume = sched_resume, + .suspend = sched_suspend +}; + +static int __init sched_init_ops(void) +{ + register_syscore_ops(&sched_syscore_ops); + return 0; +} +late_initcall(sched_init_ops); + +inline void clear_ed_task(struct task_struct *p, struct rq *rq) +{ + if (p == rq->ed_task) + rq->ed_task = NULL; +} + +inline void set_task_last_wake(struct task_struct *p, u64 wallclock) +{ + p->last_wake_ts = wallclock; +} + +inline void set_task_last_switch_out(struct task_struct *p, u64 wallclock) +{ + p->last_switch_out_ts = wallclock; +} + +/* + * Note C-state for (idle) cpus. + * + * @cstate = cstate index, 0 -> active state + * @wakeup_energy = energy spent in waking up cpu + * @wakeup_latency = latency to wakeup from cstate + * + */ +void +sched_set_cpu_cstate(int cpu, int cstate, int wakeup_energy, int wakeup_latency) +{ + struct rq *rq = cpu_rq(cpu); + + rq->cstate = cstate; /* C1, C2 etc */ + rq->wakeup_energy = wakeup_energy; + rq->wakeup_latency = wakeup_latency; +} + +/* + * Note D-state for (idle) cluster. + * + * @dstate = dstate index, 0 -> active state + * @wakeup_energy = energy spent in waking up cluster + * @wakeup_latency = latency to wakeup from cluster + * + */ +void sched_set_cluster_dstate(const cpumask_t *cluster_cpus, int dstate, + int wakeup_energy, int wakeup_latency) +{ + struct sched_cluster *cluster = + cpu_rq(cpumask_first(cluster_cpus))->cluster; + cluster->dstate = dstate; + cluster->dstate_wakeup_energy = wakeup_energy; + cluster->dstate_wakeup_latency = wakeup_latency; +} + +u32 __weak get_freq_max_load(int cpu, u32 freq) +{ + /* 100% by default */ + return 100; +} + +DEFINE_PER_CPU(struct freq_max_load *, freq_max_load); +static DEFINE_SPINLOCK(freq_max_load_lock); + +struct cpu_pwr_stats __weak *get_cpu_pwr_stats(void) +{ + return NULL; +} + +int sched_update_freq_max_load(const cpumask_t *cpumask) +{ + int i, cpu, ret; + unsigned int freq; + struct cpu_pstate_pwr *costs; + struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); + struct freq_max_load *max_load, *old_max_load; + struct freq_max_load_entry *entry; + u64 max_demand_capacity, max_demand; + unsigned long flags; + u32 hfreq; + int hpct; + + if (!per_cpu_info) + return 0; + + spin_lock_irqsave(&freq_max_load_lock, flags); + max_demand_capacity = div64_u64(max_task_load(), max_possible_capacity); + for_each_cpu(cpu, cpumask) { + if (!per_cpu_info[cpu].ptable) { + ret = -EINVAL; + goto fail; + } + + old_max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); + + /* + * allocate len + 1 and leave the last power cost as 0 for + * power_cost() can stop iterating index when + * per_cpu_info[cpu].len > len of max_load due to race between + * cpu power stats update and get_cpu_pwr_stats(). + */ + max_load = kzalloc(sizeof(struct freq_max_load) + + sizeof(struct freq_max_load_entry) * + (per_cpu_info[cpu].len + 1), GFP_ATOMIC); + if (unlikely(!max_load)) { + ret = -ENOMEM; + goto fail; + } + + max_load->length = per_cpu_info[cpu].len; + + max_demand = max_demand_capacity * + cpu_max_possible_capacity(cpu); + + i = 0; + costs = per_cpu_info[cpu].ptable; + while (costs[i].freq) { + entry = &max_load->freqs[i]; + freq = costs[i].freq; + hpct = get_freq_max_load(cpu, freq); + if (hpct <= 0 && hpct > 100) + hpct = 100; + hfreq = div64_u64((u64)freq * hpct, 100); + entry->hdemand = + div64_u64(max_demand * hfreq, + cpu_max_possible_freq(cpu)); + i++; + } + + rcu_assign_pointer(per_cpu(freq_max_load, cpu), max_load); + if (old_max_load) + kfree_rcu(old_max_load, rcu); + } + + spin_unlock_irqrestore(&freq_max_load_lock, flags); + return 0; + +fail: + for_each_cpu(cpu, cpumask) { + max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); + if (max_load) { + rcu_assign_pointer(per_cpu(freq_max_load, cpu), NULL); + kfree_rcu(max_load, rcu); + } + } + + spin_unlock_irqrestore(&freq_max_load_lock, flags); + return ret; +} + +unsigned int max_possible_efficiency = 1; +unsigned int min_possible_efficiency = UINT_MAX; + +unsigned long __weak arch_get_cpu_efficiency(int cpu) +{ + return SCHED_LOAD_SCALE; +} + +/* Keep track of max/min capacity possible across CPUs "currently" */ +static void __update_min_max_capacity(void) +{ + int i; + int max_cap = 0, min_cap = INT_MAX; + + for_each_online_cpu(i) { + max_cap = max(max_cap, cpu_capacity(i)); + min_cap = min(min_cap, cpu_capacity(i)); + } + + max_capacity = max_cap; + min_capacity = min_cap; +} + +static void update_min_max_capacity(void) +{ + unsigned long flags; + int i; + + local_irq_save(flags); + for_each_possible_cpu(i) + raw_spin_lock(&cpu_rq(i)->lock); + + __update_min_max_capacity(); + + for_each_possible_cpu(i) + raw_spin_unlock(&cpu_rq(i)->lock); + local_irq_restore(flags); +} + +/* + * Return 'capacity' of a cpu in reference to "least" efficient cpu, such that + * least efficient cpu gets capacity of 1024 + */ +static unsigned long +capacity_scale_cpu_efficiency(struct sched_cluster *cluster) +{ + return (1024 * cluster->efficiency) / min_possible_efficiency; +} + +/* + * Return 'capacity' of a cpu in reference to cpu with lowest max_freq + * (min_max_freq), such that one with lowest max_freq gets capacity of 1024. + */ +static unsigned long capacity_scale_cpu_freq(struct sched_cluster *cluster) +{ + return (1024 * cluster_max_freq(cluster)) / min_max_freq; +} + +/* + * Return load_scale_factor of a cpu in reference to "most" efficient cpu, so + * that "most" efficient cpu gets a load_scale_factor of 1 + */ +static inline unsigned long +load_scale_cpu_efficiency(struct sched_cluster *cluster) +{ + return DIV_ROUND_UP(1024 * max_possible_efficiency, + cluster->efficiency); +} + +/* + * Return load_scale_factor of a cpu in reference to cpu with best max_freq + * (max_possible_freq), so that one with best max_freq gets a load_scale_factor + * of 1. + */ +static inline unsigned long load_scale_cpu_freq(struct sched_cluster *cluster) +{ + return DIV_ROUND_UP(1024 * max_possible_freq, + cluster_max_freq(cluster)); +} + +static int compute_capacity(struct sched_cluster *cluster) +{ + int capacity = 1024; + + capacity *= capacity_scale_cpu_efficiency(cluster); + capacity >>= 10; + + capacity *= capacity_scale_cpu_freq(cluster); + capacity >>= 10; + + return capacity; +} + +static int compute_max_possible_capacity(struct sched_cluster *cluster) +{ + int capacity = 1024; + + capacity *= capacity_scale_cpu_efficiency(cluster); + capacity >>= 10; + + capacity *= (1024 * cluster->max_possible_freq) / min_max_freq; + capacity >>= 10; + + return capacity; +} + +static int compute_load_scale_factor(struct sched_cluster *cluster) +{ + int load_scale = 1024; + + /* + * load_scale_factor accounts for the fact that task load + * is in reference to "best" performing cpu. Task's load will need to be + * scaled (up) by a factor to determine suitability to be placed on a + * (little) cpu. + */ + load_scale *= load_scale_cpu_efficiency(cluster); + load_scale >>= 10; + + load_scale *= load_scale_cpu_freq(cluster); + load_scale >>= 10; + + return load_scale; +} + +struct list_head cluster_head; +static DEFINE_MUTEX(cluster_lock); +static cpumask_t all_cluster_cpus = CPU_MASK_NONE; +DECLARE_BITMAP(all_cluster_ids, NR_CPUS); +struct sched_cluster *sched_cluster[NR_CPUS]; +int num_clusters; + +struct sched_cluster init_cluster = { + .list = LIST_HEAD_INIT(init_cluster.list), + .id = 0, + .max_power_cost = 1, + .min_power_cost = 1, + .capacity = 1024, + .max_possible_capacity = 1024, + .efficiency = 1, + .load_scale_factor = 1024, + .cur_freq = 1, + .max_freq = 1, + .max_mitigated_freq = UINT_MAX, + .min_freq = 1, + .max_possible_freq = 1, + .dstate = 0, + .dstate_wakeup_energy = 0, + .dstate_wakeup_latency = 0, + .exec_scale_factor = 1024, +}; + +static void update_all_clusters_stats(void) +{ + struct sched_cluster *cluster; + u64 highest_mpc = 0, lowest_mpc = U64_MAX; + + pre_big_task_count_change(cpu_possible_mask); + + for_each_sched_cluster(cluster) { + u64 mpc; + + cluster->capacity = compute_capacity(cluster); + mpc = cluster->max_possible_capacity = + compute_max_possible_capacity(cluster); + cluster->load_scale_factor = compute_load_scale_factor(cluster); + + cluster->exec_scale_factor = + DIV_ROUND_UP(cluster->efficiency * 1024, + max_possible_efficiency); + + if (mpc > highest_mpc) + highest_mpc = mpc; + + if (mpc < lowest_mpc) + lowest_mpc = mpc; + } + + max_possible_capacity = highest_mpc; + min_max_possible_capacity = lowest_mpc; + + __update_min_max_capacity(); + sched_update_freq_max_load(cpu_possible_mask); + post_big_task_count_change(cpu_possible_mask); +} + +static void assign_cluster_ids(struct list_head *head) +{ + struct sched_cluster *cluster; + int pos = 0; + + list_for_each_entry(cluster, head, list) { + cluster->id = pos; + sched_cluster[pos++] = cluster; + } +} + +static void +move_list(struct list_head *dst, struct list_head *src, bool sync_rcu) +{ + struct list_head *first, *last; + + first = src->next; + last = src->prev; + + if (sync_rcu) { + INIT_LIST_HEAD_RCU(src); + synchronize_rcu(); + } + + first->prev = dst; + dst->prev = last; + last->next = dst; + + /* Ensure list sanity before making the head visible to all CPUs. */ + smp_mb(); + dst->next = first; +} + +static int +compare_clusters(void *priv, struct list_head *a, struct list_head *b) +{ + struct sched_cluster *cluster1, *cluster2; + int ret; + + cluster1 = container_of(a, struct sched_cluster, list); + cluster2 = container_of(b, struct sched_cluster, list); + + ret = cluster1->max_power_cost > cluster2->max_power_cost || + (cluster1->max_power_cost == cluster2->max_power_cost && + cluster1->max_possible_capacity < + cluster2->max_possible_capacity); + + return ret; +} + +static void sort_clusters(void) +{ + struct sched_cluster *cluster; + struct list_head new_head; + + INIT_LIST_HEAD(&new_head); + + for_each_sched_cluster(cluster) { + cluster->max_power_cost = power_cost(cluster_first_cpu(cluster), + max_task_load()); + cluster->min_power_cost = power_cost(cluster_first_cpu(cluster), + 0); + } + + move_list(&new_head, &cluster_head, true); + + list_sort(NULL, &new_head, compare_clusters); + assign_cluster_ids(&new_head); + + /* + * Ensure cluster ids are visible to all CPUs before making + * cluster_head visible. + */ + move_list(&cluster_head, &new_head, false); +} + +static void +insert_cluster(struct sched_cluster *cluster, struct list_head *head) +{ + struct sched_cluster *tmp; + struct list_head *iter = head; + + list_for_each_entry(tmp, head, list) { + if (cluster->max_power_cost < tmp->max_power_cost) + break; + iter = &tmp->list; + } + + list_add(&cluster->list, iter); +} + +static struct sched_cluster *alloc_new_cluster(const struct cpumask *cpus) +{ + struct sched_cluster *cluster = NULL; + + cluster = kzalloc(sizeof(struct sched_cluster), GFP_ATOMIC); + if (!cluster) { + __WARN_printf("Cluster allocation failed. \ + Possible bad scheduling\n"); + return NULL; + } + + INIT_LIST_HEAD(&cluster->list); + cluster->max_power_cost = 1; + cluster->min_power_cost = 1; + cluster->capacity = 1024; + cluster->max_possible_capacity = 1024; + cluster->efficiency = 1; + cluster->load_scale_factor = 1024; + cluster->cur_freq = 1; + cluster->max_freq = 1; + cluster->max_mitigated_freq = UINT_MAX; + cluster->min_freq = 1; + cluster->max_possible_freq = 1; + cluster->dstate = 0; + cluster->dstate_wakeup_energy = 0; + cluster->dstate_wakeup_latency = 0; + cluster->freq_init_done = false; + + cluster->cpus = *cpus; + cluster->efficiency = arch_get_cpu_efficiency(cpumask_first(cpus)); + + if (cluster->efficiency > max_possible_efficiency) + max_possible_efficiency = cluster->efficiency; + if (cluster->efficiency < min_possible_efficiency) + min_possible_efficiency = cluster->efficiency; + + return cluster; +} + +static void add_cluster(const struct cpumask *cpus, struct list_head *head) +{ + struct sched_cluster *cluster = alloc_new_cluster(cpus); + int i; + + if (!cluster) + return; + + for_each_cpu(i, cpus) + cpu_rq(i)->cluster = cluster; + + insert_cluster(cluster, head); + set_bit(num_clusters, all_cluster_ids); + num_clusters++; +} + +void update_cluster_topology(void) +{ + struct cpumask cpus = *cpu_possible_mask; + const struct cpumask *cluster_cpus; + struct list_head new_head; + int i; + + INIT_LIST_HEAD(&new_head); + + for_each_cpu(i, &cpus) { + cluster_cpus = cpu_coregroup_mask(i); + cpumask_or(&all_cluster_cpus, &all_cluster_cpus, cluster_cpus); + cpumask_andnot(&cpus, &cpus, cluster_cpus); + add_cluster(cluster_cpus, &new_head); + } + + assign_cluster_ids(&new_head); + + /* + * Ensure cluster ids are visible to all CPUs before making + * cluster_head visible. + */ + move_list(&cluster_head, &new_head, false); +} + +void init_clusters(void) +{ + bitmap_clear(all_cluster_ids, 0, NR_CPUS); + init_cluster.cpus = *cpu_possible_mask; + INIT_LIST_HEAD(&cluster_head); +} + +int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb) +{ + mutex_lock(&cluster_lock); + if (!cb->get_cpu_cycle_counter) { + mutex_unlock(&cluster_lock); + return -EINVAL; + } + + cpu_cycle_counter_cb = *cb; + use_cycle_counter = true; + mutex_unlock(&cluster_lock); + + return 0; +} + +int __init set_sched_enable_hmp(char *str) +{ + int enable_hmp = 0; + + get_option(&str, &enable_hmp); + + sched_enable_hmp = !!enable_hmp; + + return 0; +} + +early_param("sched_enable_hmp", set_sched_enable_hmp); + +int got_boost_kick(void) +{ + int cpu = smp_processor_id(); + struct rq *rq = cpu_rq(cpu); + + return test_bit(BOOST_KICK, &rq->hmp_flags); +} + +inline void clear_boost_kick(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + clear_bit(BOOST_KICK, &rq->hmp_flags); +} + +inline void boost_kick(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + if (!test_and_set_bit(BOOST_KICK, &rq->hmp_flags)) + smp_send_reschedule(cpu); +} + +/* Clear any HMP scheduler related requests pending from or on cpu */ +void clear_hmp_request(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + unsigned long flags; + + clear_boost_kick(cpu); + clear_reserved(cpu); + if (rq->push_task) { + raw_spin_lock_irqsave(&rq->lock, flags); + if (rq->push_task) { + clear_reserved(rq->push_cpu); + put_task_struct(rq->push_task); + rq->push_task = NULL; + } + rq->active_balance = 0; + raw_spin_unlock_irqrestore(&rq->lock, flags); + } +} + +int sched_set_static_cpu_pwr_cost(int cpu, unsigned int cost) +{ + struct rq *rq = cpu_rq(cpu); + + rq->static_cpu_pwr_cost = cost; + return 0; +} + +unsigned int sched_get_static_cpu_pwr_cost(int cpu) +{ + return cpu_rq(cpu)->static_cpu_pwr_cost; +} + +int sched_set_static_cluster_pwr_cost(int cpu, unsigned int cost) +{ + struct sched_cluster *cluster = cpu_rq(cpu)->cluster; + + cluster->static_cluster_pwr_cost = cost; + return 0; +} + +unsigned int sched_get_static_cluster_pwr_cost(int cpu) +{ + return cpu_rq(cpu)->cluster->static_cluster_pwr_cost; +} + +/* + * sched_window_stats_policy and sched_ravg_hist_size have a 'sysctl' copy + * associated with them. This is required for atomic update of those variables + * when being modifed via sysctl interface. + * + * IMPORTANT: Initialize both copies to same value!! + */ + +/* + * Tasks that are runnable continuously for a period greather than + * EARLY_DETECTION_DURATION can be flagged early as potential + * high load tasks. + */ +#define EARLY_DETECTION_DURATION 9500000 + +static __read_mostly unsigned int sched_ravg_hist_size = 5; +__read_mostly unsigned int sysctl_sched_ravg_hist_size = 5; + +static __read_mostly unsigned int sched_window_stats_policy = + WINDOW_STATS_MAX_RECENT_AVG; +__read_mostly unsigned int sysctl_sched_window_stats_policy = + WINDOW_STATS_MAX_RECENT_AVG; + +#define SCHED_ACCOUNT_WAIT_TIME 1 + +__read_mostly unsigned int sysctl_sched_cpu_high_irqload = (10 * NSEC_PER_MSEC); + +unsigned int __read_mostly sysctl_sched_enable_colocation = 1; + +__read_mostly unsigned int sysctl_sched_new_task_windows = 5; + +#define SCHED_FREQ_ACCOUNT_WAIT_TIME 0 + +/* + * For increase, send notification if + * freq_required - cur_freq > sysctl_sched_freq_inc_notify + */ +__read_mostly int sysctl_sched_freq_inc_notify = 10 * 1024 * 1024; /* + 10GHz */ + +/* + * For decrease, send notification if + * cur_freq - freq_required > sysctl_sched_freq_dec_notify + */ +__read_mostly int sysctl_sched_freq_dec_notify = 10 * 1024 * 1024; /* - 10GHz */ + +static __read_mostly unsigned int sched_io_is_busy; + +__read_mostly unsigned int sysctl_sched_pred_alert_freq = 10 * 1024 * 1024; + +/* + * Maximum possible frequency across all cpus. Task demand and cpu + * capacity (cpu_power) metrics are scaled in reference to it. + */ +unsigned int max_possible_freq = 1; + +/* + * Minimum possible max_freq across all cpus. This will be same as + * max_possible_freq on homogeneous systems and could be different from + * max_possible_freq on heterogenous systems. min_max_freq is used to derive + * capacity (cpu_power) of cpus. + */ +unsigned int min_max_freq = 1; + +unsigned int max_capacity = 1024; /* max(rq->capacity) */ +unsigned int min_capacity = 1024; /* min(rq->capacity) */ +unsigned int max_possible_capacity = 1024; /* max(rq->max_possible_capacity) */ +unsigned int +min_max_possible_capacity = 1024; /* min(rq->max_possible_capacity) */ + +/* Window size (in ns) */ +__read_mostly unsigned int sched_ravg_window = 10000000; + +/* Min window size (in ns) = 10ms */ +#define MIN_SCHED_RAVG_WINDOW 10000000 + +/* Max window size (in ns) = 1s */ +#define MAX_SCHED_RAVG_WINDOW 1000000000 + +/* Temporarily disable window-stats activity on all cpus */ +unsigned int __read_mostly sched_disable_window_stats; + +/* + * Major task runtime. If a task runs for more than sched_major_task_runtime + * in a window, it's considered to be generating majority of workload + * for this window. Prediction could be adjusted for such tasks. + */ +__read_mostly unsigned int sched_major_task_runtime = 10000000; + +static unsigned int sync_cpu; + +static LIST_HEAD(related_thread_groups); +static DEFINE_RWLOCK(related_thread_group_lock); + +#define for_each_related_thread_group(grp) \ + list_for_each_entry(grp, &related_thread_groups, list) + +/* + * Demand aggregation for frequency purpose: + * + * 'sched_freq_aggregate' controls aggregation of cpu demand of related threads + * for frequency determination purpose. This aggregation is done per-cluster. + * + * CPU demand of tasks from various related groups is aggregated per-cluster and + * added to the "max_busy_cpu" in that cluster, where max_busy_cpu is determined + * by just rq->prev_runnable_sum. + * + * Some examples follow, which assume: + * Cluster0 = CPU0-3, Cluster1 = CPU4-7 + * One related thread group A that has tasks A0, A1, A2 + * + * A->cpu_time[X].curr/prev_sum = counters in which cpu execution stats of + * tasks belonging to group A are accumulated when they run on cpu X. + * + * CX->curr/prev_sum = counters in which cpu execution stats of all tasks + * not belonging to group A are accumulated when they run on cpu X + * + * Lets say the stats for window M was as below: + * + * C0->prev_sum = 1ms, A->cpu_time[0].prev_sum = 5ms + * Task A0 ran 5ms on CPU0 + * Task B0 ran 1ms on CPU0 + * + * C1->prev_sum = 5ms, A->cpu_time[1].prev_sum = 6ms + * Task A1 ran 4ms on CPU1 + * Task A2 ran 2ms on CPU1 + * Task B1 ran 5ms on CPU1 + * + * C2->prev_sum = 0ms, A->cpu_time[2].prev_sum = 0 + * CPU2 idle + * + * C3->prev_sum = 0ms, A->cpu_time[3].prev_sum = 0 + * CPU3 idle + * + * In this case, CPU1 was most busy going by just its prev_sum counter. Demand + * from all group A tasks are added to CPU1. IOW, at end of window M, cpu busy + * time reported to governor will be: + * + * + * C0 busy time = 1ms + * C1 busy time = 5 + 5 + 6 = 16ms + * + */ +static __read_mostly unsigned int sched_freq_aggregate; +__read_mostly unsigned int sysctl_sched_freq_aggregate; + +/* Initial task load. Newly created tasks are assigned this load. */ +unsigned int __read_mostly sched_init_task_load_windows; +unsigned int __read_mostly sysctl_sched_init_task_load_pct = 15; + +unsigned int max_task_load(void) +{ + return sched_ravg_window; +} + +/* Use this knob to turn on or off HMP-aware task placement logic */ +unsigned int __read_mostly sched_enable_hmp; + +/* + * Scheduler boost is a mechanism to temporarily place tasks on CPUs + * with higher capacity than those where a task would have normally + * ended up with their load characteristics. Any entity enabling + * boost is responsible for disabling it as well. + */ +unsigned int sysctl_sched_boost; + +/* A cpu can no longer accommodate more tasks if: + * + * rq->nr_running > sysctl_sched_spill_nr_run || + * rq->hmp_stats.cumulative_runnable_avg > sched_spill_load + */ +unsigned int __read_mostly sysctl_sched_spill_nr_run = 10; + +/* + * Place sync wakee tasks those have less than configured demand to the waker's + * cluster. + */ +unsigned int __read_mostly sched_small_wakee_task_load; +unsigned int __read_mostly sysctl_sched_small_wakee_task_load_pct = 10; + +unsigned int __read_mostly sched_big_waker_task_load; +unsigned int __read_mostly sysctl_sched_big_waker_task_load_pct = 25; + +/* + * CPUs with load greater than the sched_spill_load_threshold are not + * eligible for task placement. When all CPUs in a cluster achieve a + * load higher than this level, tasks becomes eligible for inter + * cluster migration. + */ +unsigned int __read_mostly sched_spill_load; +unsigned int __read_mostly sysctl_sched_spill_load_pct = 100; + +/* + * Tasks whose bandwidth consumption on a cpu is more than + * sched_upmigrate are considered "big" tasks. Big tasks will be + * considered for "up" migration, i.e migrating to a cpu with better + * capacity. + */ +unsigned int __read_mostly sched_upmigrate; +unsigned int __read_mostly sysctl_sched_upmigrate_pct = 80; + +/* + * Big tasks, once migrated, will need to drop their bandwidth + * consumption to less than sched_downmigrate before they are "down" + * migrated. + */ +unsigned int __read_mostly sched_downmigrate; +unsigned int __read_mostly sysctl_sched_downmigrate_pct = 60; + +/* + * The load scale factor of a CPU gets boosted when its max frequency + * is restricted due to which the tasks are migrating to higher capacity + * CPUs early. The sched_upmigrate threshold is auto-upgraded by + * rq->max_possible_freq/rq->max_freq of a lower capacity CPU. + */ +unsigned int up_down_migrate_scale_factor = 1024; + +/* + * Scheduler selects and places task to its previous CPU if sleep time is + * less than sysctl_sched_select_prev_cpu_us. + */ +unsigned int __read_mostly +sched_short_sleep_task_threshold = 2000 * NSEC_PER_USEC; + +unsigned int __read_mostly sysctl_sched_select_prev_cpu_us = 2000; + +unsigned int __read_mostly +sched_long_cpu_selection_threshold = 100 * NSEC_PER_MSEC; + +unsigned int __read_mostly sysctl_sched_restrict_cluster_spill; + +void update_up_down_migrate(void) +{ + unsigned int up_migrate = pct_to_real(sysctl_sched_upmigrate_pct); + unsigned int down_migrate = pct_to_real(sysctl_sched_downmigrate_pct); + unsigned int delta; + + if (up_down_migrate_scale_factor == 1024) + goto done; + + delta = up_migrate - down_migrate; + + up_migrate /= NSEC_PER_USEC; + up_migrate *= up_down_migrate_scale_factor; + up_migrate >>= 10; + up_migrate *= NSEC_PER_USEC; + + up_migrate = min(up_migrate, sched_ravg_window); + + down_migrate /= NSEC_PER_USEC; + down_migrate *= up_down_migrate_scale_factor; + down_migrate >>= 10; + down_migrate *= NSEC_PER_USEC; + + down_migrate = min(down_migrate, up_migrate - delta); +done: + sched_upmigrate = up_migrate; + sched_downmigrate = down_migrate; +} + +void set_hmp_defaults(void) +{ + sched_spill_load = + pct_to_real(sysctl_sched_spill_load_pct); + + update_up_down_migrate(); + + sched_major_task_runtime = + mult_frac(sched_ravg_window, MAJOR_TASK_PCT, 100); + + sched_init_task_load_windows = + div64_u64((u64)sysctl_sched_init_task_load_pct * + (u64)sched_ravg_window, 100); + + sched_short_sleep_task_threshold = sysctl_sched_select_prev_cpu_us * + NSEC_PER_USEC; + + sched_small_wakee_task_load = + div64_u64((u64)sysctl_sched_small_wakee_task_load_pct * + (u64)sched_ravg_window, 100); + + sched_big_waker_task_load = + div64_u64((u64)sysctl_sched_big_waker_task_load_pct * + (u64)sched_ravg_window, 100); +} + +u32 sched_get_init_task_load(struct task_struct *p) +{ + return p->init_load_pct; +} + +int sched_set_init_task_load(struct task_struct *p, int init_load_pct) +{ + if (init_load_pct < 0 || init_load_pct > 100) + return -EINVAL; + + p->init_load_pct = init_load_pct; + + return 0; +} + +#ifdef CONFIG_CGROUP_SCHED + +int upmigrate_discouraged(struct task_struct *p) +{ + return task_group(p)->upmigrate_discouraged; +} + +#else + +static inline int upmigrate_discouraged(struct task_struct *p) +{ + return 0; +} + +#endif + +/* Is a task "big" on its current cpu */ +static inline int __is_big_task(struct task_struct *p, u64 scaled_load) +{ + int nice = task_nice(p); + + if (nice > SCHED_UPMIGRATE_MIN_NICE || upmigrate_discouraged(p)) + return 0; + + return scaled_load > sched_upmigrate; +} + +int is_big_task(struct task_struct *p) +{ + return __is_big_task(p, scale_load_to_cpu(task_load(p), task_cpu(p))); +} + +u64 cpu_load(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + return scale_load_to_cpu(rq->hmp_stats.cumulative_runnable_avg, cpu); +} + +u64 cpu_load_sync(int cpu, int sync) +{ + return scale_load_to_cpu(cpu_cravg_sync(cpu, sync), cpu); +} + +static int boost_refcount; +static DEFINE_SPINLOCK(boost_lock); +static DEFINE_MUTEX(boost_mutex); + +static void boost_kick_cpus(void) +{ + int i; + + for_each_online_cpu(i) { + if (cpu_capacity(i) != max_capacity) + boost_kick(i); + } +} + +int sched_boost(void) +{ + return boost_refcount > 0; +} + +int sched_set_boost(int enable) +{ + unsigned long flags; + int ret = 0; + int old_refcount; + + if (!sched_enable_hmp) + return -EINVAL; + + spin_lock_irqsave(&boost_lock, flags); + + old_refcount = boost_refcount; + + if (enable == 1) { + boost_refcount++; + } else if (!enable) { + if (boost_refcount >= 1) + boost_refcount--; + else + ret = -EINVAL; + } else { + ret = -EINVAL; + } + + if (!old_refcount && boost_refcount) + boost_kick_cpus(); + + trace_sched_set_boost(boost_refcount); + spin_unlock_irqrestore(&boost_lock, flags); + + return ret; +} + +int sched_boost_handler(struct ctl_table *table, int write, + void __user *buffer, size_t *lenp, + loff_t *ppos) +{ + int ret; + + mutex_lock(&boost_mutex); + if (!write) + sysctl_sched_boost = sched_boost(); + + ret = proc_dointvec(table, write, buffer, lenp, ppos); + if (ret || !write) + goto done; + + ret = (sysctl_sched_boost <= 1) ? + sched_set_boost(sysctl_sched_boost) : -EINVAL; + +done: + mutex_unlock(&boost_mutex); + return ret; +} + +/* + * Task will fit on a cpu if it's bandwidth consumption on that cpu + * will be less than sched_upmigrate. A big task that was previously + * "up" migrated will be considered fitting on "little" cpu if its + * bandwidth consumption on "little" cpu will be less than + * sched_downmigrate. This will help avoid frequenty migrations for + * tasks with load close to the upmigrate threshold + */ +int task_load_will_fit(struct task_struct *p, u64 task_load, int cpu, + enum sched_boost_type boost_type) +{ + int upmigrate; + + if (cpu_capacity(cpu) == max_capacity) + return 1; + + if (boost_type != SCHED_BOOST_ON_BIG) { + if (task_nice(p) > SCHED_UPMIGRATE_MIN_NICE || + upmigrate_discouraged(p)) + return 1; + + upmigrate = sched_upmigrate; + if (cpu_capacity(task_cpu(p)) > cpu_capacity(cpu)) + upmigrate = sched_downmigrate; + + if (task_load < upmigrate) + return 1; + } + + return 0; +} + +enum sched_boost_type sched_boost_type(void) +{ + if (sched_boost()) { + if (min_possible_efficiency != max_possible_efficiency) + return SCHED_BOOST_ON_BIG; + else + return SCHED_BOOST_ON_ALL; + } + return SCHED_BOOST_NONE; +} + +int task_will_fit(struct task_struct *p, int cpu) +{ + u64 tload = scale_load_to_cpu(task_load(p), cpu); + + return task_load_will_fit(p, tload, cpu, sched_boost_type()); +} + +int group_will_fit(struct sched_cluster *cluster, + struct related_thread_group *grp, u64 demand) +{ + int cpu = cluster_first_cpu(cluster); + int prev_capacity = 0; + unsigned int threshold = sched_upmigrate; + u64 load; + + if (cluster->capacity == max_capacity) + return 1; + + if (grp->preferred_cluster) + prev_capacity = grp->preferred_cluster->capacity; + + if (cluster->capacity < prev_capacity) + threshold = sched_downmigrate; + + load = scale_load_to_cpu(demand, cpu); + if (load < threshold) + return 1; + + return 0; +} + +/* + * Return the cost of running task p on CPU cpu. This function + * currently assumes that task p is the only task which will run on + * the CPU. + */ +unsigned int power_cost(int cpu, u64 demand) +{ + int first, mid, last; + struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); + struct cpu_pstate_pwr *costs; + struct freq_max_load *max_load; + int total_static_pwr_cost = 0; + struct rq *rq = cpu_rq(cpu); + unsigned int pc; + + if (!per_cpu_info || !per_cpu_info[cpu].ptable) + /* + * When power aware scheduling is not in use, or CPU + * power data is not available, just use the CPU + * capacity as a rough stand-in for real CPU power + * numbers, assuming bigger CPUs are more power + * hungry. + */ + return cpu_max_possible_capacity(cpu); + + rcu_read_lock(); + max_load = rcu_dereference(per_cpu(freq_max_load, cpu)); + if (!max_load) { + pc = cpu_max_possible_capacity(cpu); + goto unlock; + } + + costs = per_cpu_info[cpu].ptable; + + if (demand <= max_load->freqs[0].hdemand) { + pc = costs[0].power; + goto unlock; + } else if (demand > max_load->freqs[max_load->length - 1].hdemand) { + pc = costs[max_load->length - 1].power; + goto unlock; + } + + first = 0; + last = max_load->length - 1; + mid = (last - first) >> 1; + while (1) { + if (demand <= max_load->freqs[mid].hdemand) + last = mid; + else + first = mid; + + if (last - first == 1) + break; + mid = first + ((last - first) >> 1); + } + + pc = costs[last].power; + +unlock: + rcu_read_unlock(); + + if (idle_cpu(cpu) && rq->cstate) { + total_static_pwr_cost += rq->static_cpu_pwr_cost; + if (rq->cluster->dstate) + total_static_pwr_cost += + rq->cluster->static_cluster_pwr_cost; + } + + return pc + total_static_pwr_cost; + +} + +void inc_nr_big_task(struct hmp_sched_stats *stats, struct task_struct *p) +{ + if (!sched_enable_hmp || sched_disable_window_stats) + return; + + if (is_big_task(p)) + stats->nr_big_tasks++; +} + +void dec_nr_big_task(struct hmp_sched_stats *stats, struct task_struct *p) +{ + if (!sched_enable_hmp || sched_disable_window_stats) + return; + + if (is_big_task(p)) + stats->nr_big_tasks--; + + BUG_ON(stats->nr_big_tasks < 0); +} + +void inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) +{ + inc_nr_big_task(&rq->hmp_stats, p); + if (change_cra) + inc_cumulative_runnable_avg(&rq->hmp_stats, p); +} + +void dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) +{ + dec_nr_big_task(&rq->hmp_stats, p); + if (change_cra) + dec_cumulative_runnable_avg(&rq->hmp_stats, p); +} + +static void reset_hmp_stats(struct hmp_sched_stats *stats, int reset_cra) +{ + stats->nr_big_tasks = 0; + if (reset_cra) { + stats->cumulative_runnable_avg = 0; + stats->pred_demands_sum = 0; + } +} + +/* + * Invoked from three places: + * 1) try_to_wake_up() -> ... -> select_best_cpu() + * 2) scheduler_tick() -> ... -> migration_needed() -> select_best_cpu() + * 3) can_migrate_task() + * + * Its safe to de-reference p->grp in first case (since p->pi_lock is held) + * but not in other cases. p->grp is hence freed after a RCU grace period and + * accessed under rcu_read_lock() + */ +int preferred_cluster(struct sched_cluster *cluster, struct task_struct *p) +{ + struct related_thread_group *grp; + int rc = 0; + + rcu_read_lock(); + + grp = task_related_thread_group(p); + if (!grp || !sysctl_sched_enable_colocation) + rc = 1; + else + rc = (grp->preferred_cluster == cluster); + + rcu_read_unlock(); + return rc; +} + +struct sched_cluster *rq_cluster(struct rq *rq) +{ + return rq->cluster; +} + +/* + * reset_cpu_hmp_stats - reset HMP stats for a cpu + * nr_big_tasks + * cumulative_runnable_avg (iff reset_cra is true) + */ +void reset_cpu_hmp_stats(int cpu, int reset_cra) +{ + reset_cfs_rq_hmp_stats(cpu, reset_cra); + reset_hmp_stats(&cpu_rq(cpu)->hmp_stats, reset_cra); +} + +void fixup_nr_big_tasks(struct hmp_sched_stats *stats, + struct task_struct *p, s64 delta) +{ + u64 new_task_load; + u64 old_task_load; + + if (!sched_enable_hmp || sched_disable_window_stats) + return; + + old_task_load = scale_load_to_cpu(task_load(p), task_cpu(p)); + new_task_load = scale_load_to_cpu(delta + task_load(p), task_cpu(p)); + + if (__is_big_task(p, old_task_load) && !__is_big_task(p, new_task_load)) + stats->nr_big_tasks--; + else if (!__is_big_task(p, old_task_load) && + __is_big_task(p, new_task_load)) + stats->nr_big_tasks++; + + BUG_ON(stats->nr_big_tasks < 0); +} + +/* + * Walk runqueue of cpu and re-initialize 'nr_big_tasks' counters. + */ +static void update_nr_big_tasks(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + struct task_struct *p; + + /* Do not reset cumulative_runnable_avg */ + reset_cpu_hmp_stats(cpu, 0); + + list_for_each_entry(p, &rq->cfs_tasks, se.group_node) + _inc_hmp_sched_stats_fair(rq, p, 0); +} + +/* Disable interrupts and grab runqueue lock of all cpus listed in @cpus */ +void pre_big_task_count_change(const struct cpumask *cpus) +{ + int i; + + local_irq_disable(); + + for_each_cpu(i, cpus) + raw_spin_lock(&cpu_rq(i)->lock); +} + +/* + * Reinitialize 'nr_big_tasks' counters on all affected cpus + */ +void post_big_task_count_change(const struct cpumask *cpus) +{ + int i; + + /* Assumes local_irq_disable() keeps online cpumap stable */ + for_each_cpu(i, cpus) + update_nr_big_tasks(i); + + for_each_cpu(i, cpus) + raw_spin_unlock(&cpu_rq(i)->lock); + + local_irq_enable(); +} + +DEFINE_MUTEX(policy_mutex); + +static inline int invalid_value_freq_input(unsigned int *data) +{ + if (data == &sysctl_sched_freq_aggregate) + return !(*data == 0 || *data == 1); + + return 0; +} + +static inline int invalid_value(unsigned int *data) +{ + unsigned int val = *data; + + if (data == &sysctl_sched_ravg_hist_size) + return (val < 2 || val > RAVG_HIST_SIZE_MAX); + + if (data == &sysctl_sched_window_stats_policy) + return val >= WINDOW_STATS_INVALID_POLICY; + + return invalid_value_freq_input(data); +} + +/* + * Handle "atomic" update of sysctl_sched_window_stats_policy, + * sysctl_sched_ravg_hist_size and sched_freq_legacy_mode variables. + */ +int sched_window_update_handler(struct ctl_table *table, int write, + void __user *buffer, size_t *lenp, + loff_t *ppos) +{ + int ret; + unsigned int *data = (unsigned int *)table->data; + unsigned int old_val; + + if (!sched_enable_hmp) + return -EINVAL; + + mutex_lock(&policy_mutex); + + old_val = *data; + + ret = proc_dointvec(table, write, buffer, lenp, ppos); + if (ret || !write || (write && (old_val == *data))) + goto done; + + if (invalid_value(data)) { + *data = old_val; + ret = -EINVAL; + goto done; + } + + reset_all_window_stats(0, 0); + +done: + mutex_unlock(&policy_mutex); + + return ret; +} + +/* + * Convert percentage value into absolute form. This will avoid div() operation + * in fast path, to convert task load in percentage scale. + */ +int sched_hmp_proc_update_handler(struct ctl_table *table, int write, + void __user *buffer, size_t *lenp, + loff_t *ppos) +{ + int ret; + unsigned int old_val; + unsigned int *data = (unsigned int *)table->data; + int update_min_nice = 0; + + mutex_lock(&policy_mutex); + + old_val = *data; + + ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); + + if (ret || !write || !sched_enable_hmp) + goto done; + + if (write && (old_val == *data)) + goto done; + + if (data != &sysctl_sched_select_prev_cpu_us) { + /* + * all tunables other than sched_select_prev_cpu_us are + * in percentage. + */ + if (sysctl_sched_downmigrate_pct > + sysctl_sched_upmigrate_pct || *data > 100) { + *data = old_val; + ret = -EINVAL; + goto done; + } + } + + /* + * Big task tunable change will need to re-classify tasks on + * runqueue as big and set their counters appropriately. + * sysctl interface affects secondary variables (*_pct), which is then + * "atomically" carried over to the primary variables. Atomic change + * includes taking runqueue lock of all online cpus and re-initiatizing + * their big counter values based on changed criteria. + */ + if ((data == &sysctl_sched_upmigrate_pct || update_min_nice)) { + get_online_cpus(); + pre_big_task_count_change(cpu_online_mask); + } + + set_hmp_defaults(); + + if ((data == &sysctl_sched_upmigrate_pct || update_min_nice)) { + post_big_task_count_change(cpu_online_mask); + put_online_cpus(); + } + +done: + mutex_unlock(&policy_mutex); + return ret; +} + +inline int nr_big_tasks(struct rq *rq) +{ + return rq->hmp_stats.nr_big_tasks; +} + +unsigned int cpu_temp(int cpu) +{ + struct cpu_pwr_stats *per_cpu_info = get_cpu_pwr_stats(); + + if (per_cpu_info) + return per_cpu_info[cpu].temp; + else + return 0; +} + +void init_new_task_load(struct task_struct *p) +{ + int i; + u32 init_load_windows = sched_init_task_load_windows; + u32 init_load_pct = current->init_load_pct; + + p->init_load_pct = 0; + rcu_assign_pointer(p->grp, NULL); + INIT_LIST_HEAD(&p->grp_list); + memset(&p->ravg, 0, sizeof(struct ravg)); + p->cpu_cycles = 0; + + if (init_load_pct) + init_load_windows = div64_u64((u64)init_load_pct * + (u64)sched_ravg_window, 100); + + p->ravg.demand = init_load_windows; + p->ravg.pred_demand = 0; + for (i = 0; i < RAVG_HIST_SIZE_MAX; ++i) + p->ravg.sum_history[i] = init_load_windows; +} + +/* Return task demand in percentage scale */ +unsigned int pct_task_load(struct task_struct *p) +{ + unsigned int load; + + load = div64_u64((u64)task_load(p) * 100, (u64)max_task_load()); + + return load; +} + +/* + * Return total number of tasks "eligible" to run on highest capacity cpu + * + * This is simply nr_big_tasks for cpus which are not of max_capacity and + * nr_running for cpus of max_capacity + */ +unsigned int nr_eligible_big_tasks(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + int nr_big = rq->hmp_stats.nr_big_tasks; + int nr = rq->nr_running; + + if (cpu_max_possible_capacity(cpu) != max_possible_capacity) + return nr_big; + + return nr; +} + +static inline int exiting_task(struct task_struct *p) +{ + return (p->ravg.sum_history[0] == EXITING_TASK_MARKER); +} + +static int __init set_sched_ravg_window(char *str) +{ + unsigned int window_size; + + get_option(&str, &window_size); + + if (window_size < MIN_SCHED_RAVG_WINDOW || + window_size > MAX_SCHED_RAVG_WINDOW) { + WARN_ON(1); + return -EINVAL; + } + + sched_ravg_window = window_size; + return 0; +} + +early_param("sched_ravg_window", set_sched_ravg_window); + +static inline void +update_window_start(struct rq *rq, u64 wallclock) +{ + s64 delta; + int nr_windows; + + delta = wallclock - rq->window_start; + BUG_ON(delta < 0); + if (delta < sched_ravg_window) + return; + + nr_windows = div64_u64(delta, sched_ravg_window); + rq->window_start += (u64)nr_windows * (u64)sched_ravg_window; +} + +#define DIV64_U64_ROUNDUP(X, Y) div64_u64((X) + (Y - 1), Y) + +static inline u64 scale_exec_time(u64 delta, struct rq *rq) +{ + u32 freq; + + freq = cpu_cycles_to_freq(rq->cc.cycles, rq->cc.time); + delta = DIV64_U64_ROUNDUP(delta * freq, max_possible_freq); + delta *= rq->cluster->exec_scale_factor; + delta >>= 10; + + return delta; +} + +static inline int cpu_is_waiting_on_io(struct rq *rq) +{ + if (!sched_io_is_busy) + return 0; + + return atomic_read(&rq->nr_iowait); +} + +/* Does freq_required sufficiently exceed or fall behind cur_freq? */ +static inline int +nearly_same_freq(unsigned int cur_freq, unsigned int freq_required) +{ + int delta = freq_required - cur_freq; + + if (freq_required > cur_freq) + return delta < sysctl_sched_freq_inc_notify; + + delta = -delta; + + return delta < sysctl_sched_freq_dec_notify; +} + +/* Convert busy time to frequency equivalent */ +static inline unsigned int load_to_freq(struct rq *rq, u64 load) +{ + unsigned int freq; + + load = scale_load_to_cpu(load, cpu_of(rq)); + load *= 128; + load = div64_u64(load, max_task_load()); + + freq = load * cpu_max_possible_freq(cpu_of(rq)); + freq /= 128; + + return freq; +} + +static inline struct group_cpu_time * +_group_cpu_time(struct related_thread_group *grp, int cpu); + +/* + * Return load from all related group in given cpu. + * Caller must ensure that related_thread_group_lock is held. + */ +static void _group_load_in_cpu(int cpu, u64 *grp_load, u64 *new_grp_load) +{ + struct related_thread_group *grp; + + for_each_related_thread_group(grp) { + struct group_cpu_time *cpu_time; + + cpu_time = _group_cpu_time(grp, cpu); + *grp_load += cpu_time->prev_runnable_sum; + if (new_grp_load) + *new_grp_load += cpu_time->nt_prev_runnable_sum; + } +} + +/* + * Return load from all related groups in given frequency domain. + * Caller must ensure that related_thread_group_lock is held. + */ +static void group_load_in_freq_domain(struct cpumask *cpus, + u64 *grp_load, u64 *new_grp_load) +{ + struct related_thread_group *grp; + int j; + + for_each_related_thread_group(grp) { + for_each_cpu(j, cpus) { + struct group_cpu_time *cpu_time; + + cpu_time = _group_cpu_time(grp, j); + *grp_load += cpu_time->prev_runnable_sum; + *new_grp_load += cpu_time->nt_prev_runnable_sum; + } + } +} + +/* + * Should scheduler alert governor for changing frequency? + * + * @check_pred - evaluate frequency based on the predictive demand + * @check_groups - add load from all related groups on given cpu + * + * check_groups is set to 1 if a "related" task movement/wakeup is triggering + * the notification check. To avoid "re-aggregation" of demand in such cases, + * we check whether the migrated/woken tasks demand (along with demand from + * existing tasks on the cpu) can be met on target cpu + * + */ + +static int send_notification(struct rq *rq, int check_pred, int check_groups) +{ + unsigned int cur_freq, freq_required; + unsigned long flags; + int rc = 0; + u64 group_load = 0, new_load = 0; + + if (!sched_enable_hmp) + return 0; + + if (check_pred) { + u64 prev = rq->old_busy_time; + u64 predicted = rq->hmp_stats.pred_demands_sum; + + if (rq->cluster->cur_freq == cpu_max_freq(cpu_of(rq))) + return 0; + + prev = max(prev, rq->old_estimated_time); + if (prev > predicted) + return 0; + + cur_freq = load_to_freq(rq, prev); + freq_required = load_to_freq(rq, predicted); + + if (freq_required < cur_freq + sysctl_sched_pred_alert_freq) + return 0; + } else { + read_lock(&related_thread_group_lock); + /* + * Protect from concurrent update of rq->prev_runnable_sum and + * group cpu load + */ + raw_spin_lock_irqsave(&rq->lock, flags); + if (check_groups) + _group_load_in_cpu(cpu_of(rq), &group_load, NULL); + + new_load = rq->prev_runnable_sum + group_load; + + raw_spin_unlock_irqrestore(&rq->lock, flags); + read_unlock(&related_thread_group_lock); + + cur_freq = load_to_freq(rq, rq->old_busy_time); + freq_required = load_to_freq(rq, new_load); + + if (nearly_same_freq(cur_freq, freq_required)) + return 0; + } + + raw_spin_lock_irqsave(&rq->lock, flags); + if (!rq->notifier_sent) { + rq->notifier_sent = 1; + rc = 1; + trace_sched_freq_alert(cpu_of(rq), check_pred, check_groups, rq, + new_load); + } + raw_spin_unlock_irqrestore(&rq->lock, flags); + + return rc; +} + +/* Alert governor if there is a need to change frequency */ +void check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups) +{ + int cpu = cpu_of(rq); + + if (!send_notification(rq, check_pred, check_groups)) + return; + + atomic_notifier_call_chain( + &load_alert_notifier_head, 0, + (void *)(long)cpu); +} + +static int account_busy_for_cpu_time(struct rq *rq, struct task_struct *p, + u64 irqtime, int event) +{ + if (is_idle_task(p)) { + /* TASK_WAKE && TASK_MIGRATE is not possible on idle task! */ + if (event == PICK_NEXT_TASK) + return 0; + + /* PUT_PREV_TASK, TASK_UPDATE && IRQ_UPDATE are left */ + return irqtime || cpu_is_waiting_on_io(rq); + } + + if (event == TASK_WAKE) + return 0; + + if (event == PUT_PREV_TASK || event == IRQ_UPDATE) + return 1; + + /* + * TASK_UPDATE can be called on sleeping task, when its moved between + * related groups + */ + if (event == TASK_UPDATE) { + if (rq->curr == p) + return 1; + + return p->on_rq ? SCHED_FREQ_ACCOUNT_WAIT_TIME : 0; + } + + /* TASK_MIGRATE, PICK_NEXT_TASK left */ + return SCHED_FREQ_ACCOUNT_WAIT_TIME; +} + +static inline bool is_new_task(struct task_struct *p) +{ + return p->ravg.active_windows < sysctl_sched_new_task_windows; +} + +#define INC_STEP 8 +#define DEC_STEP 2 +#define CONSISTENT_THRES 16 +#define INC_STEP_BIG 16 +/* + * bucket_increase - update the count of all buckets + * + * @buckets: array of buckets tracking busy time of a task + * @idx: the index of bucket to be incremented + * + * Each time a complete window finishes, count of bucket that runtime + * falls in (@idx) is incremented. Counts of all other buckets are + * decayed. The rate of increase and decay could be different based + * on current count in the bucket. + */ +static inline void bucket_increase(u8 *buckets, int idx) +{ + int i, step; + + for (i = 0; i < NUM_BUSY_BUCKETS; i++) { + if (idx != i) { + if (buckets[i] > DEC_STEP) + buckets[i] -= DEC_STEP; + else + buckets[i] = 0; + } else { + step = buckets[i] >= CONSISTENT_THRES ? + INC_STEP_BIG : INC_STEP; + if (buckets[i] > U8_MAX - step) + buckets[i] = U8_MAX; + else + buckets[i] += step; + } + } +} + +static inline int busy_to_bucket(u32 normalized_rt) +{ + int bidx; + + bidx = mult_frac(normalized_rt, NUM_BUSY_BUCKETS, max_task_load()); + bidx = min(bidx, NUM_BUSY_BUCKETS - 1); + + /* + * Combine lowest two buckets. The lowest frequency falls into + * 2nd bucket and thus keep predicting lowest bucket is not + * useful. + */ + if (!bidx) + bidx++; + + return bidx; +} + +static inline u64 +scale_load_to_freq(u64 load, unsigned int src_freq, unsigned int dst_freq) +{ + return div64_u64(load * (u64)src_freq, (u64)dst_freq); +} + +#define HEAVY_TASK_SKIP 2 +#define HEAVY_TASK_SKIP_LIMIT 4 +/* + * get_pred_busy - calculate predicted demand for a task on runqueue + * + * @rq: runqueue of task p + * @p: task whose prediction is being updated + * @start: starting bucket. returned prediction should not be lower than + * this bucket. + * @runtime: runtime of the task. returned prediction should not be lower + * than this runtime. + * Note: @start can be derived from @runtime. It's passed in only to + * avoid duplicated calculation in some cases. + * + * A new predicted busy time is returned for task @p based on @runtime + * passed in. The function searches through buckets that represent busy + * time equal to or bigger than @runtime and attempts to find the bucket to + * to use for prediction. Once found, it searches through historical busy + * time and returns the latest that falls into the bucket. If no such busy + * time exists, it returns the medium of that bucket. + */ +static u32 get_pred_busy(struct rq *rq, struct task_struct *p, + int start, u32 runtime) +{ + int i; + u8 *buckets = p->ravg.busy_buckets; + u32 *hist = p->ravg.sum_history; + u32 dmin, dmax; + u64 cur_freq_runtime = 0; + int first = NUM_BUSY_BUCKETS, final, skip_to; + u32 ret = runtime; + + /* skip prediction for new tasks due to lack of history */ + if (unlikely(is_new_task(p))) + goto out; + + /* find minimal bucket index to pick */ + for (i = start; i < NUM_BUSY_BUCKETS; i++) { + if (buckets[i]) { + first = i; + break; + } + } + /* if no higher buckets are filled, predict runtime */ + if (first >= NUM_BUSY_BUCKETS) + goto out; + + /* compute the bucket for prediction */ + final = first; + if (first < HEAVY_TASK_SKIP_LIMIT) { + /* compute runtime at current CPU frequency */ + cur_freq_runtime = mult_frac(runtime, max_possible_efficiency, + rq->cluster->efficiency); + cur_freq_runtime = scale_load_to_freq(cur_freq_runtime, + max_possible_freq, rq->cluster->cur_freq); + /* + * if the task runs for majority of the window, try to + * pick higher buckets. + */ + if (cur_freq_runtime >= sched_major_task_runtime) { + int next = NUM_BUSY_BUCKETS; + /* + * if there is a higher bucket that's consistently + * hit, don't jump beyond that. + */ + for (i = start + 1; i <= HEAVY_TASK_SKIP_LIMIT && + i < NUM_BUSY_BUCKETS; i++) { + if (buckets[i] > CONSISTENT_THRES) { + next = i; + break; + } + } + skip_to = min(next, start + HEAVY_TASK_SKIP); + /* don't jump beyond HEAVY_TASK_SKIP_LIMIT */ + skip_to = min(HEAVY_TASK_SKIP_LIMIT, skip_to); + /* don't go below first non-empty bucket, if any */ + final = max(first, skip_to); + } + } + + /* determine demand range for the predicted bucket */ + if (final < 2) { + /* lowest two buckets are combined */ + dmin = 0; + final = 1; + } else { + dmin = mult_frac(final, max_task_load(), NUM_BUSY_BUCKETS); + } + dmax = mult_frac(final + 1, max_task_load(), NUM_BUSY_BUCKETS); + + /* + * search through runtime history and return first runtime that falls + * into the range of predicted bucket. + */ + for (i = 0; i < sched_ravg_hist_size; i++) { + if (hist[i] >= dmin && hist[i] < dmax) { + ret = hist[i]; + break; + } + } + /* no historical runtime within bucket found, use average of the bin */ + if (ret < dmin) + ret = (dmin + dmax) / 2; + /* + * when updating in middle of a window, runtime could be higher + * than all recorded history. Always predict at least runtime. + */ + ret = max(runtime, ret); +out: + trace_sched_update_pred_demand(rq, p, runtime, + mult_frac((unsigned int)cur_freq_runtime, 100, + sched_ravg_window), ret); + return ret; +} + +static inline u32 calc_pred_demand(struct rq *rq, struct task_struct *p) +{ + if (p->ravg.pred_demand >= p->ravg.curr_window) + return p->ravg.pred_demand; + + return get_pred_busy(rq, p, busy_to_bucket(p->ravg.curr_window), + p->ravg.curr_window); +} + +/* + * predictive demand of a task is calculated at the window roll-over. + * if the task current window busy time exceeds the predicted + * demand, update it here to reflect the task needs. + */ +void update_task_pred_demand(struct rq *rq, struct task_struct *p, int event) +{ + u32 new, old; + + if (is_idle_task(p) || exiting_task(p)) + return; + + if (event != PUT_PREV_TASK && event != TASK_UPDATE && + (!SCHED_FREQ_ACCOUNT_WAIT_TIME || + (event != TASK_MIGRATE && + event != PICK_NEXT_TASK))) + return; + + /* + * TASK_UPDATE can be called on sleeping task, when its moved between + * related groups + */ + if (event == TASK_UPDATE) { + if (!p->on_rq && !SCHED_FREQ_ACCOUNT_WAIT_TIME) + return; + } + + new = calc_pred_demand(rq, p); + old = p->ravg.pred_demand; + + if (old >= new) + return; + + if (task_on_rq_queued(p) && (!task_has_dl_policy(p) || + !p->dl.dl_throttled)) + p->sched_class->fixup_hmp_sched_stats(rq, p, + p->ravg.demand, + new); + + p->ravg.pred_demand = new; +} + +/* + * Account cpu activity in its busy time counters (rq->curr/prev_runnable_sum) + */ +static void update_cpu_busy_time(struct task_struct *p, struct rq *rq, + int event, u64 wallclock, u64 irqtime) +{ + int new_window, full_window = 0; + int p_is_curr_task = (p == rq->curr); + u64 mark_start = p->ravg.mark_start; + u64 window_start = rq->window_start; + u32 window_size = sched_ravg_window; + u64 delta; + u64 *curr_runnable_sum = &rq->curr_runnable_sum; + u64 *prev_runnable_sum = &rq->prev_runnable_sum; + u64 *nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; + u64 *nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; + int flip_counters = 0; + int prev_sum_reset = 0; + bool new_task; + struct related_thread_group *grp; + + new_window = mark_start < window_start; + if (new_window) { + full_window = (window_start - mark_start) >= window_size; + if (p->ravg.active_windows < USHRT_MAX) + p->ravg.active_windows++; + } + + new_task = is_new_task(p); + + grp = p->grp; + if (grp && sched_freq_aggregate) { + /* cpu_time protected by rq_lock */ + struct group_cpu_time *cpu_time = + _group_cpu_time(grp, cpu_of(rq)); + + curr_runnable_sum = &cpu_time->curr_runnable_sum; + prev_runnable_sum = &cpu_time->prev_runnable_sum; + + nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; + nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; + + if (cpu_time->window_start != rq->window_start) { + int nr_windows; + + delta = rq->window_start - cpu_time->window_start; + nr_windows = div64_u64(delta, window_size); + if (nr_windows > 1) + prev_sum_reset = 1; + + cpu_time->window_start = rq->window_start; + flip_counters = 1; + } + + if (p_is_curr_task && new_window) { + u64 curr_sum = rq->curr_runnable_sum; + u64 nt_curr_sum = rq->nt_curr_runnable_sum; + + if (full_window) + curr_sum = nt_curr_sum = 0; + + rq->prev_runnable_sum = curr_sum; + rq->nt_prev_runnable_sum = nt_curr_sum; + + rq->curr_runnable_sum = 0; + rq->nt_curr_runnable_sum = 0; + } + } else { + if (p_is_curr_task && new_window) { + flip_counters = 1; + if (full_window) + prev_sum_reset = 1; + } + } + + /* + * Handle per-task window rollover. We don't care about the idle + * task or exiting tasks. + */ + if (new_window && !is_idle_task(p) && !exiting_task(p)) { + u32 curr_window = 0; + + if (!full_window) + curr_window = p->ravg.curr_window; + + p->ravg.prev_window = curr_window; + p->ravg.curr_window = 0; + } + + if (flip_counters) { + u64 curr_sum = *curr_runnable_sum; + u64 nt_curr_sum = *nt_curr_runnable_sum; + + if (prev_sum_reset) + curr_sum = nt_curr_sum = 0; + + *prev_runnable_sum = curr_sum; + *nt_prev_runnable_sum = nt_curr_sum; + + *curr_runnable_sum = 0; + *nt_curr_runnable_sum = 0; + } + + if (!account_busy_for_cpu_time(rq, p, irqtime, event)) { + /* + * account_busy_for_cpu_time() = 0, so no update to the + * task's current window needs to be made. This could be + * for example + * + * - a wakeup event on a task within the current + * window (!new_window below, no action required), + * - switching to a new task from idle (PICK_NEXT_TASK) + * in a new window where irqtime is 0 and we aren't + * waiting on IO + */ + + if (!new_window) + return; + + /* + * A new window has started. The RQ demand must be rolled + * over if p is the current task. + */ + if (p_is_curr_task) { + /* p is idle task */ + BUG_ON(p != rq->idle); + } + + return; + } + + if (!new_window) { + /* + * account_busy_for_cpu_time() = 1 so busy time needs + * to be accounted to the current window. No rollover + * since we didn't start a new window. An example of this is + * when a task starts execution and then sleeps within the + * same window. + */ + + if (!irqtime || !is_idle_task(p) || cpu_is_waiting_on_io(rq)) + delta = wallclock - mark_start; + else + delta = irqtime; + delta = scale_exec_time(delta, rq); + *curr_runnable_sum += delta; + if (new_task) + *nt_curr_runnable_sum += delta; + + if (!is_idle_task(p) && !exiting_task(p)) + p->ravg.curr_window += delta; + + return; + } + + if (!p_is_curr_task) { + /* + * account_busy_for_cpu_time() = 1 so busy time needs + * to be accounted to the current window. A new window + * has also started, but p is not the current task, so the + * window is not rolled over - just split up and account + * as necessary into curr and prev. The window is only + * rolled over when a new window is processed for the current + * task. + * + * Irqtime can't be accounted by a task that isn't the + * currently running task. + */ + + if (!full_window) { + /* + * A full window hasn't elapsed, account partial + * contribution to previous completed window. + */ + delta = scale_exec_time(window_start - mark_start, rq); + if (!exiting_task(p)) + p->ravg.prev_window += delta; + } else { + /* + * Since at least one full window has elapsed, + * the contribution to the previous window is the + * full window (window_size). + */ + delta = scale_exec_time(window_size, rq); + if (!exiting_task(p)) + p->ravg.prev_window = delta; + } + + *prev_runnable_sum += delta; + if (new_task) + *nt_prev_runnable_sum += delta; + + /* Account piece of busy time in the current window. */ + delta = scale_exec_time(wallclock - window_start, rq); + *curr_runnable_sum += delta; + if (new_task) + *nt_curr_runnable_sum += delta; + + if (!exiting_task(p)) + p->ravg.curr_window = delta; + + return; + } + + if (!irqtime || !is_idle_task(p) || cpu_is_waiting_on_io(rq)) { + /* + * account_busy_for_cpu_time() = 1 so busy time needs + * to be accounted to the current window. A new window + * has started and p is the current task so rollover is + * needed. If any of these three above conditions are true + * then this busy time can't be accounted as irqtime. + * + * Busy time for the idle task or exiting tasks need not + * be accounted. + * + * An example of this would be a task that starts execution + * and then sleeps once a new window has begun. + */ + + if (!full_window) { + /* + * A full window hasn't elapsed, account partial + * contribution to previous completed window. + */ + delta = scale_exec_time(window_start - mark_start, rq); + if (!is_idle_task(p) && !exiting_task(p)) + p->ravg.prev_window += delta; + } else { + /* + * Since at least one full window has elapsed, + * the contribution to the previous window is the + * full window (window_size). + */ + delta = scale_exec_time(window_size, rq); + if (!is_idle_task(p) && !exiting_task(p)) + p->ravg.prev_window = delta; + } + + /* + * Rollover is done here by overwriting the values in + * prev_runnable_sum and curr_runnable_sum. + */ + *prev_runnable_sum += delta; + if (new_task) + *nt_prev_runnable_sum += delta; + + /* Account piece of busy time in the current window. */ + delta = scale_exec_time(wallclock - window_start, rq); + *curr_runnable_sum += delta; + if (new_task) + *nt_curr_runnable_sum += delta; + + if (!is_idle_task(p) && !exiting_task(p)) + p->ravg.curr_window = delta; + + return; + } + + if (irqtime) { + /* + * account_busy_for_cpu_time() = 1 so busy time needs + * to be accounted to the current window. A new window + * has started and p is the current task so rollover is + * needed. The current task must be the idle task because + * irqtime is not accounted for any other task. + * + * Irqtime will be accounted each time we process IRQ activity + * after a period of idleness, so we know the IRQ busy time + * started at wallclock - irqtime. + */ + + BUG_ON(!is_idle_task(p)); + mark_start = wallclock - irqtime; + + /* + * Roll window over. If IRQ busy time was just in the current + * window then that is all that need be accounted. + */ + if (mark_start > window_start) { + *curr_runnable_sum = scale_exec_time(irqtime, rq); + return; + } + + /* + * The IRQ busy time spanned multiple windows. Process the + * busy time preceding the current window start first. + */ + delta = window_start - mark_start; + if (delta > window_size) + delta = window_size; + delta = scale_exec_time(delta, rq); + *prev_runnable_sum += delta; + + /* Process the remaining IRQ busy time in the current window. */ + delta = wallclock - window_start; + rq->curr_runnable_sum = scale_exec_time(delta, rq); + + return; + } + + BUG(); +} + +static inline u32 predict_and_update_buckets(struct rq *rq, + struct task_struct *p, u32 runtime) { + + int bidx; + u32 pred_demand; + + bidx = busy_to_bucket(runtime); + pred_demand = get_pred_busy(rq, p, bidx, runtime); + bucket_increase(p->ravg.busy_buckets, bidx); + + return pred_demand; +} + +static void update_task_cpu_cycles(struct task_struct *p, int cpu) +{ + if (use_cycle_counter) + p->cpu_cycles = cpu_cycle_counter_cb.get_cpu_cycle_counter(cpu); +} + +static void +update_task_rq_cpu_cycles(struct task_struct *p, struct rq *rq, int event, + u64 wallclock, u64 irqtime) +{ + u64 cur_cycles; + int cpu = cpu_of(rq); + + lockdep_assert_held(&rq->lock); + + if (!use_cycle_counter) { + rq->cc.cycles = cpu_cur_freq(cpu); + rq->cc.time = 1; + return; + } + + cur_cycles = cpu_cycle_counter_cb.get_cpu_cycle_counter(cpu); + + /* + * If current task is idle task and irqtime == 0 CPU was + * indeed idle and probably its cycle counter was not + * increasing. We still need estimatied CPU frequency + * for IO wait time accounting. Use the previously + * calculated frequency in such a case. + */ + if (!is_idle_task(rq->curr) || irqtime) { + if (unlikely(cur_cycles < p->cpu_cycles)) + rq->cc.cycles = cur_cycles + (U64_MAX - p->cpu_cycles); + else + rq->cc.cycles = cur_cycles - p->cpu_cycles; + rq->cc.cycles = rq->cc.cycles * NSEC_PER_MSEC; + + if (event == IRQ_UPDATE && is_idle_task(p)) + /* + * Time between mark_start of idle task and IRQ handler + * entry time is CPU cycle counter stall period. + * Upon IRQ handler entry sched_account_irqstart() + * replenishes idle task's cpu cycle counter so + * rq->cc.cycles now represents increased cycles during + * IRQ handler rather than time between idle entry and + * IRQ exit. Thus use irqtime as time delta. + */ + rq->cc.time = irqtime; + else + rq->cc.time = wallclock - p->ravg.mark_start; + BUG_ON((s64)rq->cc.time < 0); + } + + p->cpu_cycles = cur_cycles; + + trace_sched_get_task_cpu_cycles(cpu, event, rq->cc.cycles, rq->cc.time); +} + +static int account_busy_for_task_demand(struct task_struct *p, int event) +{ + /* + * No need to bother updating task demand for exiting tasks + * or the idle task. + */ + if (exiting_task(p) || is_idle_task(p)) + return 0; + + /* + * When a task is waking up it is completing a segment of non-busy + * time. Likewise, if wait time is not treated as busy time, then + * when a task begins to run or is migrated, it is not running and + * is completing a segment of non-busy time. + */ + if (event == TASK_WAKE || (!SCHED_ACCOUNT_WAIT_TIME && + (event == PICK_NEXT_TASK || event == TASK_MIGRATE))) + return 0; + + return 1; +} + +/* + * Called when new window is starting for a task, to record cpu usage over + * recently concluded window(s). Normally 'samples' should be 1. It can be > 1 + * when, say, a real-time task runs without preemption for several windows at a + * stretch. + */ +static void update_history(struct rq *rq, struct task_struct *p, + u32 runtime, int samples, int event) +{ + u32 *hist = &p->ravg.sum_history[0]; + int ridx, widx; + u32 max = 0, avg, demand, pred_demand; + u64 sum = 0; + + /* Ignore windows where task had no activity */ + if (!runtime || is_idle_task(p) || exiting_task(p) || !samples) + goto done; + + /* Push new 'runtime' value onto stack */ + widx = sched_ravg_hist_size - 1; + ridx = widx - samples; + for (; ridx >= 0; --widx, --ridx) { + hist[widx] = hist[ridx]; + sum += hist[widx]; + if (hist[widx] > max) + max = hist[widx]; + } + + for (widx = 0; widx < samples && widx < sched_ravg_hist_size; widx++) { + hist[widx] = runtime; + sum += hist[widx]; + if (hist[widx] > max) + max = hist[widx]; + } + + p->ravg.sum = 0; + + if (sched_window_stats_policy == WINDOW_STATS_RECENT) { + demand = runtime; + } else if (sched_window_stats_policy == WINDOW_STATS_MAX) { + demand = max; + } else { + avg = div64_u64(sum, sched_ravg_hist_size); + if (sched_window_stats_policy == WINDOW_STATS_AVG) + demand = avg; + else + demand = max(avg, runtime); + } + pred_demand = predict_and_update_buckets(rq, p, runtime); + + /* + * A throttled deadline sched class task gets dequeued without + * changing p->on_rq. Since the dequeue decrements hmp stats + * avoid decrementing it here again. + */ + if (task_on_rq_queued(p) && (!task_has_dl_policy(p) || + !p->dl.dl_throttled)) + p->sched_class->fixup_hmp_sched_stats(rq, p, demand, + pred_demand); + + p->ravg.demand = demand; + p->ravg.pred_demand = pred_demand; + +done: + trace_sched_update_history(rq, p, runtime, samples, event); +} + +static void add_to_task_demand(struct rq *rq, struct task_struct *p, u64 delta) +{ + delta = scale_exec_time(delta, rq); + p->ravg.sum += delta; + if (unlikely(p->ravg.sum > sched_ravg_window)) + p->ravg.sum = sched_ravg_window; +} + +/* + * Account cpu demand of task and/or update task's cpu demand history + * + * ms = p->ravg.mark_start; + * wc = wallclock + * ws = rq->window_start + * + * Three possibilities: + * + * a) Task event is contained within one window. + * window_start < mark_start < wallclock + * + * ws ms wc + * | | | + * V V V + * |---------------| + * + * In this case, p->ravg.sum is updated *iff* event is appropriate + * (ex: event == PUT_PREV_TASK) + * + * b) Task event spans two windows. + * mark_start < window_start < wallclock + * + * ms ws wc + * | | | + * V V V + * -----|------------------- + * + * In this case, p->ravg.sum is updated with (ws - ms) *iff* event + * is appropriate, then a new window sample is recorded followed + * by p->ravg.sum being set to (wc - ws) *iff* event is appropriate. + * + * c) Task event spans more than two windows. + * + * ms ws_tmp ws wc + * | | | | + * V V V V + * ---|-------|-------|-------|-------|------ + * | | + * |<------ nr_full_windows ------>| + * + * In this case, p->ravg.sum is updated with (ws_tmp - ms) first *iff* + * event is appropriate, window sample of p->ravg.sum is recorded, + * 'nr_full_window' samples of window_size is also recorded *iff* + * event is appropriate and finally p->ravg.sum is set to (wc - ws) + * *iff* event is appropriate. + * + * IMPORTANT : Leave p->ravg.mark_start unchanged, as update_cpu_busy_time() + * depends on it! + */ +static void update_task_demand(struct task_struct *p, struct rq *rq, + int event, u64 wallclock) +{ + u64 mark_start = p->ravg.mark_start; + u64 delta, window_start = rq->window_start; + int new_window, nr_full_windows; + u32 window_size = sched_ravg_window; + + new_window = mark_start < window_start; + if (!account_busy_for_task_demand(p, event)) { + if (new_window) + /* + * If the time accounted isn't being accounted as + * busy time, and a new window started, only the + * previous window need be closed out with the + * pre-existing demand. Multiple windows may have + * elapsed, but since empty windows are dropped, + * it is not necessary to account those. + */ + update_history(rq, p, p->ravg.sum, 1, event); + return; + } + + if (!new_window) { + /* + * The simple case - busy time contained within the existing + * window. + */ + add_to_task_demand(rq, p, wallclock - mark_start); + return; + } + + /* + * Busy time spans at least two windows. Temporarily rewind + * window_start to first window boundary after mark_start. + */ + delta = window_start - mark_start; + nr_full_windows = div64_u64(delta, window_size); + window_start -= (u64)nr_full_windows * (u64)window_size; + + /* Process (window_start - mark_start) first */ + add_to_task_demand(rq, p, window_start - mark_start); + + /* Push new sample(s) into task's demand history */ + update_history(rq, p, p->ravg.sum, 1, event); + if (nr_full_windows) + update_history(rq, p, scale_exec_time(window_size, rq), + nr_full_windows, event); + + /* + * Roll window_start back to current to process any remainder + * in current window. + */ + window_start += (u64)nr_full_windows * (u64)window_size; + + /* Process (wallclock - window_start) next */ + mark_start = window_start; + add_to_task_demand(rq, p, wallclock - mark_start); +} + +/* Reflect task activity on its demand and cpu's busy time statistics */ +void update_task_ravg(struct task_struct *p, struct rq *rq, int event, + u64 wallclock, u64 irqtime) +{ + if (!rq->window_start || sched_disable_window_stats) + return; + + lockdep_assert_held(&rq->lock); + + update_window_start(rq, wallclock); + + if (!p->ravg.mark_start) { + update_task_cpu_cycles(p, cpu_of(rq)); + goto done; + } + + update_task_rq_cpu_cycles(p, rq, event, wallclock, irqtime); + update_task_demand(p, rq, event, wallclock); + update_cpu_busy_time(p, rq, event, wallclock, irqtime); + update_task_pred_demand(rq, p, event); +done: + trace_sched_update_task_ravg(p, rq, event, wallclock, irqtime, + rq->cc.cycles, rq->cc.time, + _group_cpu_time(p->grp, cpu_of(rq))); + + p->ravg.mark_start = wallclock; +} + +void sched_account_irqtime(int cpu, struct task_struct *curr, + u64 delta, u64 wallclock) +{ + struct rq *rq = cpu_rq(cpu); + unsigned long flags, nr_windows; + u64 cur_jiffies_ts; + + raw_spin_lock_irqsave(&rq->lock, flags); + + /* + * cputime (wallclock) uses sched_clock so use the same here for + * consistency. + */ + delta += sched_clock() - wallclock; + cur_jiffies_ts = get_jiffies_64(); + + if (is_idle_task(curr)) + update_task_ravg(curr, rq, IRQ_UPDATE, sched_ktime_clock(), + delta); + + nr_windows = cur_jiffies_ts - rq->irqload_ts; + + if (nr_windows) { + if (nr_windows < 10) { + /* Decay CPU's irqload by 3/4 for each window. */ + rq->avg_irqload *= (3 * nr_windows); + rq->avg_irqload = div64_u64(rq->avg_irqload, + 4 * nr_windows); + } else { + rq->avg_irqload = 0; + } + rq->avg_irqload += rq->cur_irqload; + rq->cur_irqload = 0; + } + + rq->cur_irqload += delta; + rq->irqload_ts = cur_jiffies_ts; + raw_spin_unlock_irqrestore(&rq->lock, flags); +} + +void sched_account_irqstart(int cpu, struct task_struct *curr, u64 wallclock) +{ + struct rq *rq = cpu_rq(cpu); + + if (!rq->window_start || sched_disable_window_stats) + return; + + if (is_idle_task(curr)) { + /* We're here without rq->lock held, IRQ disabled */ + raw_spin_lock(&rq->lock); + update_task_cpu_cycles(curr, cpu); + raw_spin_unlock(&rq->lock); + } +} + +void reset_task_stats(struct task_struct *p) +{ + u32 sum = 0; + + if (exiting_task(p)) + sum = EXITING_TASK_MARKER; + + memset(&p->ravg, 0, sizeof(struct ravg)); + /* Retain EXITING_TASK marker */ + p->ravg.sum_history[0] = sum; +} + +void mark_task_starting(struct task_struct *p) +{ + u64 wallclock; + struct rq *rq = task_rq(p); + + if (!rq->window_start || sched_disable_window_stats) { + reset_task_stats(p); + return; + } + + wallclock = sched_ktime_clock(); + p->ravg.mark_start = p->last_wake_ts = wallclock; + p->last_cpu_selected_ts = wallclock; + p->last_switch_out_ts = 0; + update_task_cpu_cycles(p, cpu_of(rq)); +} + +void set_window_start(struct rq *rq) +{ + int cpu = cpu_of(rq); + struct rq *sync_rq = cpu_rq(sync_cpu); + + if (rq->window_start || !sched_enable_hmp) + return; + + if (cpu == sync_cpu) { + rq->window_start = sched_ktime_clock(); + } else { + raw_spin_unlock(&rq->lock); + double_rq_lock(rq, sync_rq); + rq->window_start = cpu_rq(sync_cpu)->window_start; + rq->curr_runnable_sum = rq->prev_runnable_sum = 0; + rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; + raw_spin_unlock(&sync_rq->lock); + } + + rq->curr->ravg.mark_start = rq->window_start; +} + +void migrate_sync_cpu(int cpu) +{ + if (cpu == sync_cpu) + sync_cpu = smp_processor_id(); +} + +static void reset_all_task_stats(void) +{ + struct task_struct *g, *p; + + read_lock(&tasklist_lock); + do_each_thread(g, p) { + reset_task_stats(p); + } while_each_thread(g, p); + read_unlock(&tasklist_lock); +} + +static void disable_window_stats(void) +{ + unsigned long flags; + int i; + + local_irq_save(flags); + for_each_possible_cpu(i) + raw_spin_lock(&cpu_rq(i)->lock); + + sched_disable_window_stats = 1; + + for_each_possible_cpu(i) + raw_spin_unlock(&cpu_rq(i)->lock); + + local_irq_restore(flags); +} + +/* Called with all cpu's rq->lock held */ +static void enable_window_stats(void) +{ + sched_disable_window_stats = 0; + +} + +enum reset_reason_code { + WINDOW_CHANGE, + POLICY_CHANGE, + HIST_SIZE_CHANGE, + FREQ_AGGREGATE_CHANGE, +}; + +const char *sched_window_reset_reasons[] = { + "WINDOW_CHANGE", + "POLICY_CHANGE", + "HIST_SIZE_CHANGE", +}; + +/* Called with IRQs enabled */ +void reset_all_window_stats(u64 window_start, unsigned int window_size) +{ + int cpu; + unsigned long flags; + u64 start_ts = sched_ktime_clock(); + int reason = WINDOW_CHANGE; + unsigned int old = 0, new = 0; + struct related_thread_group *grp; + + disable_window_stats(); + + reset_all_task_stats(); + + local_irq_save(flags); + + read_lock(&related_thread_group_lock); + + for_each_possible_cpu(cpu) + raw_spin_lock(&cpu_rq(cpu)->lock); + + list_for_each_entry(grp, &related_thread_groups, list) { + int j; + + for_each_possible_cpu(j) { + struct group_cpu_time *cpu_time; + /* Protected by rq lock */ + cpu_time = _group_cpu_time(grp, j); + memset(cpu_time, 0, sizeof(struct group_cpu_time)); + if (window_start) + cpu_time->window_start = window_start; + } + } + + if (window_size) { + sched_ravg_window = window_size * TICK_NSEC; + set_hmp_defaults(); + } + + enable_window_stats(); + + for_each_possible_cpu(cpu) { + struct rq *rq = cpu_rq(cpu); + + if (window_start) + rq->window_start = window_start; + rq->curr_runnable_sum = rq->prev_runnable_sum = 0; + rq->nt_curr_runnable_sum = rq->nt_prev_runnable_sum = 0; + reset_cpu_hmp_stats(cpu, 1); + } + + if (sched_window_stats_policy != sysctl_sched_window_stats_policy) { + reason = POLICY_CHANGE; + old = sched_window_stats_policy; + new = sysctl_sched_window_stats_policy; + sched_window_stats_policy = sysctl_sched_window_stats_policy; + } else if (sched_ravg_hist_size != sysctl_sched_ravg_hist_size) { + reason = HIST_SIZE_CHANGE; + old = sched_ravg_hist_size; + new = sysctl_sched_ravg_hist_size; + sched_ravg_hist_size = sysctl_sched_ravg_hist_size; + } else if (sched_freq_aggregate != + sysctl_sched_freq_aggregate) { + reason = FREQ_AGGREGATE_CHANGE; + old = sched_freq_aggregate; + new = sysctl_sched_freq_aggregate; + sched_freq_aggregate = sysctl_sched_freq_aggregate; + } + + for_each_possible_cpu(cpu) + raw_spin_unlock(&cpu_rq(cpu)->lock); + + read_unlock(&related_thread_group_lock); + + local_irq_restore(flags); + + trace_sched_reset_all_window_stats(window_start, window_size, + sched_ktime_clock() - start_ts, reason, old, new); +} + +static inline void +sync_window_start(struct rq *rq, struct group_cpu_time *cpu_time); + +void sched_get_cpus_busy(struct sched_load *busy, + const struct cpumask *query_cpus) +{ + unsigned long flags; + struct rq *rq; + const int cpus = cpumask_weight(query_cpus); + u64 load[cpus], group_load[cpus]; + u64 nload[cpus], ngload[cpus]; + u64 pload[cpus]; + unsigned int cur_freq[cpus], max_freq[cpus]; + int notifier_sent[cpus]; + int early_detection[cpus]; + int cpu, i = 0; + unsigned int window_size; + u64 max_prev_sum = 0; + int max_busy_cpu = cpumask_first(query_cpus); + struct related_thread_group *grp; + + if (unlikely(cpus == 0)) + return; + + /* + * This function could be called in timer context, and the + * current task may have been executing for a long time. Ensure + * that the window stats are current by doing an update. + */ + read_lock(&related_thread_group_lock); + + local_irq_save(flags); + for_each_cpu(cpu, query_cpus) + raw_spin_lock(&cpu_rq(cpu)->lock); + + window_size = sched_ravg_window; + + for_each_cpu(cpu, query_cpus) { + rq = cpu_rq(cpu); + + update_task_ravg(rq->curr, rq, TASK_UPDATE, sched_ktime_clock(), + 0); + cur_freq[i] = cpu_cycles_to_freq(rq->cc.cycles, rq->cc.time); + + load[i] = rq->old_busy_time = rq->prev_runnable_sum; + nload[i] = rq->nt_prev_runnable_sum; + pload[i] = rq->hmp_stats.pred_demands_sum; + rq->old_estimated_time = pload[i]; + + if (load[i] > max_prev_sum) { + max_prev_sum = load[i]; + max_busy_cpu = cpu; + } + + notifier_sent[i] = rq->notifier_sent; + early_detection[i] = (rq->ed_task != NULL); + rq->notifier_sent = 0; + cur_freq[i] = cpu_cur_freq(cpu); + max_freq[i] = cpu_max_freq(cpu); + i++; + } + + for_each_related_thread_group(grp) { + for_each_cpu(cpu, query_cpus) { + /* Protected by rq_lock */ + struct group_cpu_time *cpu_time = + _group_cpu_time(grp, cpu); + sync_window_start(cpu_rq(cpu), cpu_time); + } + } + + i = 0; + for_each_cpu(cpu, query_cpus) { + group_load[i] = 0; + ngload[i] = 0; + + if (early_detection[i]) + goto skip_early; + + rq = cpu_rq(cpu); + if (!notifier_sent[i]) { + if (cpu == max_busy_cpu) + group_load_in_freq_domain( + &rq->freq_domain_cpumask, + &group_load[i], &ngload[i]); + } else { + _group_load_in_cpu(cpu, &group_load[i], &ngload[i]); + } + + load[i] += group_load[i]; + nload[i] += ngload[i]; + /* + * Scale load in reference to cluster max_possible_freq. + * + * Note that scale_load_to_cpu() scales load in reference to + * the cluster max_freq. + */ + load[i] = scale_load_to_cpu(load[i], cpu); + nload[i] = scale_load_to_cpu(nload[i], cpu); + pload[i] = scale_load_to_cpu(pload[i], cpu); +skip_early: + i++; + } + + for_each_cpu(cpu, query_cpus) + raw_spin_unlock(&(cpu_rq(cpu))->lock); + local_irq_restore(flags); + + read_unlock(&related_thread_group_lock); + + i = 0; + for_each_cpu(cpu, query_cpus) { + rq = cpu_rq(cpu); + + if (early_detection[i]) { + busy[i].prev_load = div64_u64(sched_ravg_window, + NSEC_PER_USEC); + busy[i].new_task_load = 0; + goto exit_early; + } + + if (!notifier_sent[i]) { + load[i] = scale_load_to_freq(load[i], max_freq[i], + cur_freq[i]); + nload[i] = scale_load_to_freq(nload[i], max_freq[i], + cur_freq[i]); + if (load[i] > window_size) + load[i] = window_size; + if (nload[i] > window_size) + nload[i] = window_size; + + load[i] = scale_load_to_freq(load[i], cur_freq[i], + cpu_max_possible_freq(cpu)); + nload[i] = scale_load_to_freq(nload[i], cur_freq[i], + cpu_max_possible_freq(cpu)); + } else { + load[i] = scale_load_to_freq(load[i], max_freq[i], + cpu_max_possible_freq(cpu)); + nload[i] = scale_load_to_freq(nload[i], max_freq[i], + cpu_max_possible_freq(cpu)); + } + pload[i] = scale_load_to_freq(pload[i], max_freq[i], + rq->cluster->max_possible_freq); + + busy[i].prev_load = div64_u64(load[i], NSEC_PER_USEC); + busy[i].new_task_load = div64_u64(nload[i], NSEC_PER_USEC); + busy[i].predicted_load = div64_u64(pload[i], NSEC_PER_USEC); + +exit_early: + trace_sched_get_busy(cpu, busy[i].prev_load, + busy[i].new_task_load, + busy[i].predicted_load, + early_detection[i]); + i++; + } +} + +void sched_set_io_is_busy(int val) +{ + sched_io_is_busy = val; +} + +int sched_set_window(u64 window_start, unsigned int window_size) +{ + u64 now, cur_jiffies, jiffy_ktime_ns; + s64 ws; + unsigned long flags; + + if (window_size * TICK_NSEC < MIN_SCHED_RAVG_WINDOW) + return -EINVAL; + + mutex_lock(&policy_mutex); + + /* + * Get a consistent view of ktime, jiffies, and the time + * since the last jiffy (based on last_jiffies_update). + */ + local_irq_save(flags); + cur_jiffies = jiffy_to_ktime_ns(&now, &jiffy_ktime_ns); + local_irq_restore(flags); + + /* translate window_start from jiffies to nanoseconds */ + ws = (window_start - cur_jiffies); /* jiffy difference */ + ws *= TICK_NSEC; + ws += jiffy_ktime_ns; + + /* + * Roll back calculated window start so that it is in + * the past (window stats must have a current window). + */ + while (ws > now) + ws -= (window_size * TICK_NSEC); + + BUG_ON(sched_ktime_clock() < ws); + + reset_all_window_stats(ws, window_size); + + sched_update_freq_max_load(cpu_possible_mask); + + mutex_unlock(&policy_mutex); + + return 0; +} + +void fixup_busy_time(struct task_struct *p, int new_cpu) +{ + struct rq *src_rq = task_rq(p); + struct rq *dest_rq = cpu_rq(new_cpu); + u64 wallclock; + u64 *src_curr_runnable_sum, *dst_curr_runnable_sum; + u64 *src_prev_runnable_sum, *dst_prev_runnable_sum; + u64 *src_nt_curr_runnable_sum, *dst_nt_curr_runnable_sum; + u64 *src_nt_prev_runnable_sum, *dst_nt_prev_runnable_sum; + int migrate_type; + struct migration_sum_data d; + bool new_task; + struct related_thread_group *grp; + + if (!sched_enable_hmp || (!p->on_rq && p->state != TASK_WAKING)) + return; + + if (exiting_task(p)) { + clear_ed_task(p, src_rq); + return; + } + + if (p->state == TASK_WAKING) + double_rq_lock(src_rq, dest_rq); + + if (sched_disable_window_stats) + goto done; + + wallclock = sched_ktime_clock(); + + update_task_ravg(task_rq(p)->curr, task_rq(p), + TASK_UPDATE, + wallclock, 0); + update_task_ravg(dest_rq->curr, dest_rq, + TASK_UPDATE, wallclock, 0); + + update_task_ravg(p, task_rq(p), TASK_MIGRATE, + wallclock, 0); + + update_task_cpu_cycles(p, new_cpu); + + new_task = is_new_task(p); + /* Protected by rq_lock */ + grp = p->grp; + if (grp && sched_freq_aggregate) { + struct group_cpu_time *cpu_time; + + migrate_type = GROUP_TO_GROUP; + /* Protected by rq_lock */ + cpu_time = _group_cpu_time(grp, cpu_of(src_rq)); + d.src_rq = NULL; + d.src_cpu_time = cpu_time; + src_curr_runnable_sum = &cpu_time->curr_runnable_sum; + src_prev_runnable_sum = &cpu_time->prev_runnable_sum; + src_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; + src_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; + + /* Protected by rq_lock */ + cpu_time = _group_cpu_time(grp, cpu_of(dest_rq)); + d.dst_rq = NULL; + d.dst_cpu_time = cpu_time; + dst_curr_runnable_sum = &cpu_time->curr_runnable_sum; + dst_prev_runnable_sum = &cpu_time->prev_runnable_sum; + dst_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; + dst_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; + sync_window_start(dest_rq, cpu_time); + } else { + migrate_type = RQ_TO_RQ; + d.src_rq = src_rq; + d.src_cpu_time = NULL; + d.dst_rq = dest_rq; + d.dst_cpu_time = NULL; + src_curr_runnable_sum = &src_rq->curr_runnable_sum; + src_prev_runnable_sum = &src_rq->prev_runnable_sum; + src_nt_curr_runnable_sum = &src_rq->nt_curr_runnable_sum; + src_nt_prev_runnable_sum = &src_rq->nt_prev_runnable_sum; + + dst_curr_runnable_sum = &dest_rq->curr_runnable_sum; + dst_prev_runnable_sum = &dest_rq->prev_runnable_sum; + dst_nt_curr_runnable_sum = &dest_rq->nt_curr_runnable_sum; + dst_nt_prev_runnable_sum = &dest_rq->nt_prev_runnable_sum; + } + + if (p->ravg.curr_window) { + *src_curr_runnable_sum -= p->ravg.curr_window; + *dst_curr_runnable_sum += p->ravg.curr_window; + if (new_task) { + *src_nt_curr_runnable_sum -= p->ravg.curr_window; + *dst_nt_curr_runnable_sum += p->ravg.curr_window; + } + } + + if (p->ravg.prev_window) { + *src_prev_runnable_sum -= p->ravg.prev_window; + *dst_prev_runnable_sum += p->ravg.prev_window; + if (new_task) { + *src_nt_prev_runnable_sum -= p->ravg.prev_window; + *dst_nt_prev_runnable_sum += p->ravg.prev_window; + } + } + + if (p == src_rq->ed_task) { + src_rq->ed_task = NULL; + if (!dest_rq->ed_task) + dest_rq->ed_task = p; + } + + trace_sched_migration_update_sum(p, migrate_type, &d); + BUG_ON((s64)*src_prev_runnable_sum < 0); + BUG_ON((s64)*src_curr_runnable_sum < 0); + BUG_ON((s64)*src_nt_prev_runnable_sum < 0); + BUG_ON((s64)*src_nt_curr_runnable_sum < 0); + +done: + if (p->state == TASK_WAKING) + double_rq_unlock(src_rq, dest_rq); +} + +#define sched_up_down_migrate_auto_update 1 +static void check_for_up_down_migrate_update(const struct cpumask *cpus) +{ + int i = cpumask_first(cpus); + + if (!sched_up_down_migrate_auto_update) + return; + + if (cpu_max_possible_capacity(i) == max_possible_capacity) + return; + + if (cpu_max_possible_freq(i) == cpu_max_freq(i)) + up_down_migrate_scale_factor = 1024; + else + up_down_migrate_scale_factor = (1024 * + cpu_max_possible_freq(i)) / cpu_max_freq(i); + + update_up_down_migrate(); +} + +/* Return cluster which can offer required capacity for group */ +static struct sched_cluster * +best_cluster(struct related_thread_group *grp, u64 total_demand) +{ + struct sched_cluster *cluster = NULL; + + for_each_sched_cluster(cluster) { + if (group_will_fit(cluster, grp, total_demand)) + return cluster; + } + + return NULL; +} + +static void _set_preferred_cluster(struct related_thread_group *grp) +{ + struct task_struct *p; + u64 combined_demand = 0; + + if (!sysctl_sched_enable_colocation) { + grp->last_update = sched_ktime_clock(); + grp->preferred_cluster = NULL; + return; + } + + /* + * wakeup of two or more related tasks could race with each other and + * could result in multiple calls to _set_preferred_cluster being issued + * at same time. Avoid overhead in such cases of rechecking preferred + * cluster + */ + if (sched_ktime_clock() - grp->last_update < sched_ravg_window / 10) + return; + + list_for_each_entry(p, &grp->tasks, grp_list) + combined_demand += p->ravg.demand; + + grp->preferred_cluster = best_cluster(grp, combined_demand); + grp->last_update = sched_ktime_clock(); + trace_sched_set_preferred_cluster(grp, combined_demand); +} + +void set_preferred_cluster(struct related_thread_group *grp) +{ + raw_spin_lock(&grp->lock); + _set_preferred_cluster(grp); + raw_spin_unlock(&grp->lock); +} + +#define ADD_TASK 0 +#define REM_TASK 1 + +static inline void free_group_cputime(struct related_thread_group *grp) +{ + free_percpu(grp->cpu_time); +} + +static int alloc_group_cputime(struct related_thread_group *grp) +{ + int i; + struct group_cpu_time *cpu_time; + int cpu = raw_smp_processor_id(); + struct rq *rq = cpu_rq(cpu); + u64 window_start = rq->window_start; + + grp->cpu_time = alloc_percpu(struct group_cpu_time); + if (!grp->cpu_time) + return -ENOMEM; + + for_each_possible_cpu(i) { + cpu_time = per_cpu_ptr(grp->cpu_time, i); + memset(cpu_time, 0, sizeof(struct group_cpu_time)); + cpu_time->window_start = window_start; + } + + return 0; +} + +/* + * A group's window_start may be behind. When moving it forward, flip prev/curr + * counters. When moving forward > 1 window, prev counter is set to 0 + */ +static inline void +sync_window_start(struct rq *rq, struct group_cpu_time *cpu_time) +{ + u64 delta; + int nr_windows; + u64 curr_sum = cpu_time->curr_runnable_sum; + u64 nt_curr_sum = cpu_time->nt_curr_runnable_sum; + + delta = rq->window_start - cpu_time->window_start; + if (!delta) + return; + + nr_windows = div64_u64(delta, sched_ravg_window); + if (nr_windows > 1) + curr_sum = nt_curr_sum = 0; + + cpu_time->prev_runnable_sum = curr_sum; + cpu_time->curr_runnable_sum = 0; + + cpu_time->nt_prev_runnable_sum = nt_curr_sum; + cpu_time->nt_curr_runnable_sum = 0; + + cpu_time->window_start = rq->window_start; +} + +/* + * Task's cpu usage is accounted in: + * rq->curr/prev_runnable_sum, when its ->grp is NULL + * grp->cpu_time[cpu]->curr/prev_runnable_sum, when its ->grp is !NULL + * + * Transfer task's cpu usage between those counters when transitioning between + * groups + */ +static void transfer_busy_time(struct rq *rq, struct related_thread_group *grp, + struct task_struct *p, int event) +{ + u64 wallclock; + struct group_cpu_time *cpu_time; + u64 *src_curr_runnable_sum, *dst_curr_runnable_sum; + u64 *src_prev_runnable_sum, *dst_prev_runnable_sum; + u64 *src_nt_curr_runnable_sum, *dst_nt_curr_runnable_sum; + u64 *src_nt_prev_runnable_sum, *dst_nt_prev_runnable_sum; + struct migration_sum_data d; + int migrate_type; + + if (!sched_freq_aggregate) + return; + + wallclock = sched_ktime_clock(); + + update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0); + update_task_ravg(p, rq, TASK_UPDATE, wallclock, 0); + + /* cpu_time protected by related_thread_group_lock, grp->lock rq_lock */ + cpu_time = _group_cpu_time(grp, cpu_of(rq)); + if (event == ADD_TASK) { + sync_window_start(rq, cpu_time); + migrate_type = RQ_TO_GROUP; + d.src_rq = rq; + d.src_cpu_time = NULL; + d.dst_rq = NULL; + d.dst_cpu_time = cpu_time; + src_curr_runnable_sum = &rq->curr_runnable_sum; + dst_curr_runnable_sum = &cpu_time->curr_runnable_sum; + src_prev_runnable_sum = &rq->prev_runnable_sum; + dst_prev_runnable_sum = &cpu_time->prev_runnable_sum; + + src_nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; + dst_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; + src_nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; + dst_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; + } else { + migrate_type = GROUP_TO_RQ; + d.src_rq = NULL; + d.src_cpu_time = cpu_time; + d.dst_rq = rq; + d.dst_cpu_time = NULL; + + /* + * In case of REM_TASK, cpu_time->window_start would be + * uptodate, because of the update_task_ravg() we called + * above on the moving task. Hence no need for + * sync_window_start() + */ + src_curr_runnable_sum = &cpu_time->curr_runnable_sum; + dst_curr_runnable_sum = &rq->curr_runnable_sum; + src_prev_runnable_sum = &cpu_time->prev_runnable_sum; + dst_prev_runnable_sum = &rq->prev_runnable_sum; + + src_nt_curr_runnable_sum = &cpu_time->nt_curr_runnable_sum; + dst_nt_curr_runnable_sum = &rq->nt_curr_runnable_sum; + src_nt_prev_runnable_sum = &cpu_time->nt_prev_runnable_sum; + dst_nt_prev_runnable_sum = &rq->nt_prev_runnable_sum; + } + + *src_curr_runnable_sum -= p->ravg.curr_window; + *dst_curr_runnable_sum += p->ravg.curr_window; + + *src_prev_runnable_sum -= p->ravg.prev_window; + *dst_prev_runnable_sum += p->ravg.prev_window; + + if (is_new_task(p)) { + *src_nt_curr_runnable_sum -= p->ravg.curr_window; + *dst_nt_curr_runnable_sum += p->ravg.curr_window; + *src_nt_prev_runnable_sum -= p->ravg.prev_window; + *dst_nt_prev_runnable_sum += p->ravg.prev_window; + } + + trace_sched_migration_update_sum(p, migrate_type, &d); + + BUG_ON((s64)*src_curr_runnable_sum < 0); + BUG_ON((s64)*src_prev_runnable_sum < 0); +} + +static inline struct group_cpu_time * +task_group_cpu_time(struct task_struct *p, int cpu) +{ + return _group_cpu_time(rcu_dereference(p->grp), cpu); +} + +static inline struct group_cpu_time * +_group_cpu_time(struct related_thread_group *grp, int cpu) +{ + return grp ? per_cpu_ptr(grp->cpu_time, cpu) : NULL; +} + +struct related_thread_group *alloc_related_thread_group(int group_id) +{ + struct related_thread_group *grp; + + grp = kzalloc(sizeof(*grp), GFP_KERNEL); + if (!grp) + return ERR_PTR(-ENOMEM); + + if (alloc_group_cputime(grp)) { + kfree(grp); + return ERR_PTR(-ENOMEM); + } + + grp->id = group_id; + INIT_LIST_HEAD(&grp->tasks); + INIT_LIST_HEAD(&grp->list); + raw_spin_lock_init(&grp->lock); + + return grp; +} + +struct related_thread_group *lookup_related_thread_group(unsigned int group_id) +{ + struct related_thread_group *grp; + + list_for_each_entry(grp, &related_thread_groups, list) { + if (grp->id == group_id) + return grp; + } + + return NULL; +} + +/* See comments before preferred_cluster() */ +static void free_related_thread_group(struct rcu_head *rcu) +{ + struct related_thread_group *grp = container_of(rcu, struct + related_thread_group, rcu); + + free_group_cputime(grp); + kfree(grp); +} + +static void remove_task_from_group(struct task_struct *p) +{ + struct related_thread_group *grp = p->grp; + struct rq *rq; + int empty_group = 1; + + raw_spin_lock(&grp->lock); + + rq = __task_rq_lock(p); + transfer_busy_time(rq, p->grp, p, REM_TASK); + list_del_init(&p->grp_list); + rcu_assign_pointer(p->grp, NULL); + __task_rq_unlock(rq); + + if (!list_empty(&grp->tasks)) { + empty_group = 0; + _set_preferred_cluster(grp); + } + + raw_spin_unlock(&grp->lock); + + if (empty_group) { + list_del(&grp->list); + call_rcu(&grp->rcu, free_related_thread_group); + } +} + +static int +add_task_to_group(struct task_struct *p, struct related_thread_group *grp) +{ + struct rq *rq; + + raw_spin_lock(&grp->lock); + + /* + * Change p->grp under rq->lock. Will prevent races with read-side + * reference of p->grp in various hot-paths + */ + rq = __task_rq_lock(p); + transfer_busy_time(rq, grp, p, ADD_TASK); + list_add(&p->grp_list, &grp->tasks); + rcu_assign_pointer(p->grp, grp); + __task_rq_unlock(rq); + + _set_preferred_cluster(grp); + + raw_spin_unlock(&grp->lock); + + return 0; +} + +int sched_set_group_id(struct task_struct *p, unsigned int group_id) +{ + int rc = 0, destroy = 0; + unsigned long flags; + struct related_thread_group *grp = NULL, *new = NULL; + +redo: + raw_spin_lock_irqsave(&p->pi_lock, flags); + + if ((current != p && p->flags & PF_EXITING) || + (!p->grp && !group_id) || + (p->grp && p->grp->id == group_id)) + goto done; + + write_lock(&related_thread_group_lock); + + if (!group_id) { + remove_task_from_group(p); + write_unlock(&related_thread_group_lock); + goto done; + } + + if (p->grp && p->grp->id != group_id) + remove_task_from_group(p); + + grp = lookup_related_thread_group(group_id); + if (!grp && !new) { + /* New group */ + write_unlock(&related_thread_group_lock); + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + new = alloc_related_thread_group(group_id); + if (IS_ERR(new)) + return -ENOMEM; + destroy = 1; + /* Rerun checks (like task exiting), since we dropped pi_lock */ + goto redo; + } else if (!grp && new) { + /* New group - use object allocated before */ + destroy = 0; + list_add(&new->list, &related_thread_groups); + grp = new; + } + + BUG_ON(!grp); + rc = add_task_to_group(p, grp); + write_unlock(&related_thread_group_lock); +done: + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + + if (new && destroy) { + free_group_cputime(new); + kfree(new); + } + + return rc; +} + +unsigned int sched_get_group_id(struct task_struct *p) +{ + unsigned int group_id; + struct related_thread_group *grp; + + rcu_read_lock(); + grp = task_related_thread_group(p); + group_id = grp ? grp->id : 0; + rcu_read_unlock(); + + return group_id; +} + +static void update_cpu_cluster_capacity(const cpumask_t *cpus) +{ + int i; + struct sched_cluster *cluster; + struct cpumask cpumask; + + cpumask_copy(&cpumask, cpus); + pre_big_task_count_change(cpu_possible_mask); + + for_each_cpu(i, &cpumask) { + cluster = cpu_rq(i)->cluster; + cpumask_andnot(&cpumask, &cpumask, &cluster->cpus); + + cluster->capacity = compute_capacity(cluster); + cluster->load_scale_factor = compute_load_scale_factor(cluster); + + /* 'cpus' can contain cpumask more than one cluster */ + check_for_up_down_migrate_update(&cluster->cpus); + } + + __update_min_max_capacity(); + + post_big_task_count_change(cpu_possible_mask); +} + +static DEFINE_SPINLOCK(cpu_freq_min_max_lock); +void sched_update_cpu_freq_min_max(const cpumask_t *cpus, u32 fmin, u32 fmax) +{ + struct cpumask cpumask; + struct sched_cluster *cluster; + int i, update_capacity = 0; + unsigned long flags; + + spin_lock_irqsave(&cpu_freq_min_max_lock, flags); + cpumask_copy(&cpumask, cpus); + for_each_cpu(i, &cpumask) { + cluster = cpu_rq(i)->cluster; + cpumask_andnot(&cpumask, &cpumask, &cluster->cpus); + + update_capacity += (cluster->max_mitigated_freq != fmax); + cluster->max_mitigated_freq = fmax; + } + spin_unlock_irqrestore(&cpu_freq_min_max_lock, flags); + + if (update_capacity) + update_cpu_cluster_capacity(cpus); +} + +static int cpufreq_notifier_policy(struct notifier_block *nb, + unsigned long val, void *data) +{ + struct cpufreq_policy *policy = (struct cpufreq_policy *)data; + struct sched_cluster *cluster = NULL; + struct cpumask policy_cluster = *policy->related_cpus; + unsigned int orig_max_freq = 0; + int i, j, update_capacity = 0; + + if (val != CPUFREQ_NOTIFY && val != CPUFREQ_REMOVE_POLICY && + val != CPUFREQ_CREATE_POLICY) + return 0; + + if (val == CPUFREQ_REMOVE_POLICY || val == CPUFREQ_CREATE_POLICY) { + update_min_max_capacity(); + return 0; + } + + max_possible_freq = max(max_possible_freq, policy->cpuinfo.max_freq); + if (min_max_freq == 1) + min_max_freq = UINT_MAX; + min_max_freq = min(min_max_freq, policy->cpuinfo.max_freq); + BUG_ON(!min_max_freq); + BUG_ON(!policy->max); + + for_each_cpu(i, &policy_cluster) { + cluster = cpu_rq(i)->cluster; + cpumask_andnot(&policy_cluster, &policy_cluster, + &cluster->cpus); + + orig_max_freq = cluster->max_freq; + cluster->min_freq = policy->min; + cluster->max_freq = policy->max; + cluster->cur_freq = policy->cur; + + if (!cluster->freq_init_done) { + mutex_lock(&cluster_lock); + for_each_cpu(j, &cluster->cpus) + cpumask_copy(&cpu_rq(j)->freq_domain_cpumask, + policy->related_cpus); + cluster->max_possible_freq = policy->cpuinfo.max_freq; + cluster->max_possible_capacity = + compute_max_possible_capacity(cluster); + cluster->freq_init_done = true; + + sort_clusters(); + update_all_clusters_stats(); + mutex_unlock(&cluster_lock); + continue; + } + + update_capacity += (orig_max_freq != cluster->max_freq); + } + + if (update_capacity) + update_cpu_cluster_capacity(policy->related_cpus); + + return 0; +} + +static int cpufreq_notifier_trans(struct notifier_block *nb, + unsigned long val, void *data) +{ + struct cpufreq_freqs *freq = (struct cpufreq_freqs *)data; + unsigned int cpu = freq->cpu, new_freq = freq->new; + unsigned long flags; + struct sched_cluster *cluster; + struct cpumask policy_cpus = cpu_rq(cpu)->freq_domain_cpumask; + int i, j; + + if (val != CPUFREQ_POSTCHANGE) + return 0; + + BUG_ON(!new_freq); + + if (cpu_cur_freq(cpu) == new_freq) + return 0; + + for_each_cpu(i, &policy_cpus) { + cluster = cpu_rq(i)->cluster; + + for_each_cpu(j, &cluster->cpus) { + struct rq *rq = cpu_rq(j); + + raw_spin_lock_irqsave(&rq->lock, flags); + update_task_ravg(rq->curr, rq, TASK_UPDATE, + sched_ktime_clock(), 0); + raw_spin_unlock_irqrestore(&rq->lock, flags); + } + + cluster->cur_freq = new_freq; + cpumask_andnot(&policy_cpus, &policy_cpus, &cluster->cpus); + } + + return 0; +} + +static int pwr_stats_ready_notifier(struct notifier_block *nb, + unsigned long cpu, void *data) +{ + cpumask_t mask = CPU_MASK_NONE; + + cpumask_set_cpu(cpu, &mask); + sched_update_freq_max_load(&mask); + + mutex_lock(&cluster_lock); + sort_clusters(); + mutex_unlock(&cluster_lock); + + return 0; +} + +static struct notifier_block notifier_policy_block = { + .notifier_call = cpufreq_notifier_policy +}; + +static struct notifier_block notifier_trans_block = { + .notifier_call = cpufreq_notifier_trans +}; + +static struct notifier_block notifier_pwr_stats_ready = { + .notifier_call = pwr_stats_ready_notifier +}; + +int __weak register_cpu_pwr_stats_ready_notifier(struct notifier_block *nb) +{ + return -EINVAL; +} + +static int register_sched_callback(void) +{ + int ret; + + if (!sched_enable_hmp) + return 0; + + ret = cpufreq_register_notifier(¬ifier_policy_block, + CPUFREQ_POLICY_NOTIFIER); + + if (!ret) + ret = cpufreq_register_notifier(¬ifier_trans_block, + CPUFREQ_TRANSITION_NOTIFIER); + + register_cpu_pwr_stats_ready_notifier(¬ifier_pwr_stats_ready); + + return 0; +} + +/* + * cpufreq callbacks can be registered at core_initcall or later time. + * Any registration done prior to that is "forgotten" by cpufreq. See + * initialization of variable init_cpufreq_transition_notifier_list_called + * for further information. + */ +core_initcall(register_sched_callback); + +int update_preferred_cluster(struct related_thread_group *grp, + struct task_struct *p, u32 old_load) +{ + u32 new_load = task_load(p); + + if (!grp) + return 0; + + /* + * Update if task's load has changed significantly or a complete window + * has passed since we last updated preference + */ + if (abs(new_load - old_load) > sched_ravg_window / 4 || + sched_ktime_clock() - grp->last_update > sched_ravg_window) + return 1; + + return 0; +} + +bool early_detection_notify(struct rq *rq, u64 wallclock) +{ + struct task_struct *p; + int loop_max = 10; + + if (!sched_boost() || !rq->cfs.h_nr_running) + return 0; + + rq->ed_task = NULL; + list_for_each_entry(p, &rq->cfs_tasks, se.group_node) { + if (!loop_max) + break; + + if (wallclock - p->last_wake_ts >= EARLY_DETECTION_DURATION) { + rq->ed_task = p; + return 1; + } + + loop_max--; + } + + return 0; +} + +#ifdef CONFIG_CGROUP_SCHED +u64 cpu_upmigrate_discourage_read_u64(struct cgroup_subsys_state *css, + struct cftype *cft) +{ + struct task_group *tg = css_tg(css); + + return tg->upmigrate_discouraged; +} + +int cpu_upmigrate_discourage_write_u64(struct cgroup_subsys_state *css, + struct cftype *cft, u64 upmigrate_discourage) +{ + struct task_group *tg = css_tg(css); + int discourage = upmigrate_discourage > 0; + + if (tg->upmigrate_discouraged == discourage) + return 0; + + /* + * Revisit big-task classification for tasks of this cgroup. It would + * have been efficient to walk tasks of just this cgroup in running + * state, but we don't have easy means to do that. Walk all tasks in + * running state on all cpus instead and re-visit their big task + * classification. + */ + get_online_cpus(); + pre_big_task_count_change(cpu_online_mask); + + tg->upmigrate_discouraged = discourage; + + post_big_task_count_change(cpu_online_mask); + put_online_cpus(); + + return 0; +} +#endif /* CONFIG_CGROUP_SCHED */ diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index af1d33ad82e6..0bae93891647 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -355,6 +355,7 @@ extern void sched_move_task(struct task_struct *tsk); extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); #endif +extern struct task_group *css_tg(struct cgroup_subsys_state *css); #else /* CONFIG_CGROUP_SCHED */ struct cfs_bandwidth { }; @@ -418,8 +419,6 @@ struct migration_sum_data { extern struct list_head cluster_head; extern int num_clusters; extern struct sched_cluster *sched_cluster[NR_CPUS]; -extern int group_will_fit(struct sched_cluster *cluster, - struct related_thread_group *grp, u64 demand); struct cpu_cycle { u64 cycles; @@ -1023,8 +1022,6 @@ static inline void sched_ttwu_pending(void) { } #include "stats.h" #include "auto_group.h" -extern void init_new_task_load(struct task_struct *p); - #ifdef CONFIG_SCHED_HMP #define WINDOW_STATS_RECENT 0 @@ -1034,6 +1031,12 @@ extern void init_new_task_load(struct task_struct *p); #define WINDOW_STATS_INVALID_POLICY 4 #define MAJOR_TASK_PCT 85 +#define SCHED_UPMIGRATE_MIN_NICE 15 +#define EXITING_TASK_MARKER 0xdeaddead + +#define UP_MIGRATION 1 +#define DOWN_MIGRATION 2 +#define IRQLOAD_MIGRATION 3 extern struct mutex policy_mutex; extern unsigned int sched_ravg_window; @@ -1056,18 +1059,53 @@ extern unsigned int up_down_migrate_scale_factor; extern unsigned int sysctl_sched_restrict_cluster_spill; extern unsigned int sched_pred_alert_load; extern unsigned int sched_major_task_runtime; +extern struct sched_cluster init_cluster; +extern unsigned int __read_mostly sched_short_sleep_task_threshold; +extern unsigned int __read_mostly sched_long_cpu_selection_threshold; +extern unsigned int __read_mostly sched_big_waker_task_load; +extern unsigned int __read_mostly sched_small_wakee_task_load; +extern unsigned int __read_mostly sched_spill_load; +extern unsigned int __read_mostly sched_upmigrate; +extern unsigned int __read_mostly sched_downmigrate; +extern unsigned int __read_mostly sysctl_sched_spill_nr_run; +extern void init_new_task_load(struct task_struct *p); +extern u64 sched_ktime_clock(void); +extern int got_boost_kick(void); +extern int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb); +extern void update_task_ravg(struct task_struct *p, struct rq *rq, int event, + u64 wallclock, u64 irqtime); +extern bool early_detection_notify(struct rq *rq, u64 wallclock); +extern void clear_ed_task(struct task_struct *p, struct rq *rq); +extern void fixup_busy_time(struct task_struct *p, int new_cpu); +extern void clear_boost_kick(int cpu); +extern void clear_hmp_request(int cpu); +extern void mark_task_starting(struct task_struct *p); +extern void set_window_start(struct rq *rq); +extern void migrate_sync_cpu(int cpu); +extern void update_cluster_topology(void); +extern void set_task_last_wake(struct task_struct *p, u64 wallclock); +extern void set_task_last_switch_out(struct task_struct *p, u64 wallclock); +extern void init_clusters(void); +extern int __init set_sched_enable_hmp(char *str); extern void reset_cpu_hmp_stats(int cpu, int reset_cra); extern unsigned int max_task_load(void); extern void sched_account_irqtime(int cpu, struct task_struct *curr, u64 delta, u64 wallclock); extern void sched_account_irqstart(int cpu, struct task_struct *curr, u64 wallclock); - -unsigned int cpu_temp(int cpu); -int sched_set_group_id(struct task_struct *p, unsigned int group_id); +extern unsigned int cpu_temp(int cpu); extern unsigned int nr_eligible_big_tasks(int cpu); extern void update_up_down_migrate(void); +extern int update_preferred_cluster(struct related_thread_group *grp, + struct task_struct *p, u32 old_load); +extern void set_preferred_cluster(struct related_thread_group *grp); + +enum sched_boost_type { + SCHED_BOOST_NONE, + SCHED_BOOST_ON_BIG, + SCHED_BOOST_ON_ALL, +}; static inline struct sched_cluster *cpu_cluster(int cpu) { @@ -1337,11 +1375,140 @@ extern unsigned int power_cost(int cpu, u64 demand); extern void reset_all_window_stats(u64 window_start, unsigned int window_size); extern void boost_kick(int cpu); extern int sched_boost(void); +extern int task_load_will_fit(struct task_struct *p, u64 task_load, int cpu, + enum sched_boost_type boost_type); +extern enum sched_boost_type sched_boost_type(void); +extern int task_will_fit(struct task_struct *p, int cpu); +extern int group_will_fit(struct sched_cluster *cluster, + struct related_thread_group *grp, u64 demand); +extern u64 cpu_load(int cpu); +extern u64 cpu_load_sync(int cpu, int sync); +extern int preferred_cluster(struct sched_cluster *cluster, + struct task_struct *p); +extern void inc_nr_big_task(struct hmp_sched_stats *stats, + struct task_struct *p); +extern void dec_nr_big_task(struct hmp_sched_stats *stats, + struct task_struct *p); +extern void inc_rq_hmp_stats(struct rq *rq, + struct task_struct *p, int change_cra); +extern void dec_rq_hmp_stats(struct rq *rq, + struct task_struct *p, int change_cra); +extern int is_big_task(struct task_struct *p); +extern int upmigrate_discouraged(struct task_struct *p); +extern struct sched_cluster *rq_cluster(struct rq *rq); +extern int nr_big_tasks(struct rq *rq); +extern void fixup_nr_big_tasks(struct hmp_sched_stats *stats, + struct task_struct *p, s64 delta); +extern void reset_task_stats(struct task_struct *p); +extern void reset_cfs_rq_hmp_stats(int cpu, int reset_cra); +extern void _inc_hmp_sched_stats_fair(struct rq *rq, + struct task_struct *p, int change_cra); +extern u64 cpu_upmigrate_discourage_read_u64(struct cgroup_subsys_state *css, + struct cftype *cft); +extern int cpu_upmigrate_discourage_write_u64(struct cgroup_subsys_state *css, + struct cftype *cft, u64 upmigrate_discourage); #else /* CONFIG_SCHED_HMP */ struct hmp_sched_stats; struct related_thread_group; +struct sched_cluster; + +static inline int got_boost_kick(void) +{ + return 0; +} + +static inline void update_task_ravg(struct task_struct *p, struct rq *rq, + int event, u64 wallclock, u64 irqtime) { } + +static inline bool early_detection_notify(struct rq *rq, u64 wallclock) +{ + return 0; +} + +static inline void clear_ed_task(struct task_struct *p, struct rq *rq) { } +static inline void fixup_busy_time(struct task_struct *p, int new_cpu) { } +static inline void clear_boost_kick(int cpu) { } +static inline void clear_hmp_request(int cpu) { } +static inline void mark_task_starting(struct task_struct *p) { } +static inline void set_window_start(struct rq *rq) { } +static inline void migrate_sync_cpu(int cpu) { } +static inline void update_cluster_topology(void) { } +static inline void set_task_last_wake(struct task_struct *p, u64 wallclock) { } +static inline void set_task_last_switch_out(struct task_struct *p, + u64 wallclock) { } + +static inline int task_will_fit(struct task_struct *p, int cpu) +{ + return 1; +} + +static inline int select_best_cpu(struct task_struct *p, int target, + int reason, int sync) +{ + return 0; +} + +static inline unsigned int power_cost(int cpu, u64 demand) +{ + return SCHED_CAPACITY_SCALE; +} + +static inline int sched_boost(void) +{ + return 0; +} + +static inline int is_big_task(struct task_struct *p) +{ + return 0; +} + +static inline int nr_big_tasks(struct rq *rq) +{ + return 0; +} + +static inline int is_cpu_throttling_imminent(int cpu) +{ + return 0; +} + +static inline int is_task_migration_throttled(struct task_struct *p) +{ + return 0; +} + +static inline unsigned int cpu_temp(int cpu) +{ + return 0; +} + +static inline void +inc_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } + +static inline void +dec_rq_hmp_stats(struct rq *rq, struct task_struct *p, int change_cra) { } + +static inline void +inc_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p) { } + +static inline void +dec_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p) { } + +static inline int +preferred_cluster(struct sched_cluster *cluster, struct task_struct *p) +{ + return 1; +} + +static inline struct sched_cluster *rq_cluster(struct rq *rq) +{ + return NULL; +} + +static inline void init_new_task_load(struct task_struct *p) { } static inline u64 scale_load_to_cpu(u64 load, int cpu) { From 67e0df6e338edbf119ae2cb7abe52a460cb33851 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Tue, 2 Aug 2016 15:08:13 -0700 Subject: [PATCH 12/16] sched: Move notify_migration() under CONFIG_SCHED_HMP notify_migration() is a HMP specific function that relies on all of its contents to be stubbed out for !CONFIG_SCHED_HMP. However, it still maintains calls to rcu_read_lock/unlock(). In the !HMP case these calls are simply redundant. Move the function under CONFIG_SCHED_HMP and add a stub when the config is not defined so that there is no overhead. Change-Id: Iad914f31b629e81e403b0e89796b2b0f1d081695 Signed-off-by: Syed Rameez Mustafa --- kernel/sched/core.c | 19 ------------------- kernel/sched/hmp.c | 19 +++++++++++++++++++ kernel/sched/sched.h | 6 ++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 6836851d416f..a790d101d120 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1134,25 +1134,6 @@ static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_ return rq; } -static void notify_migration(int src_cpu, int dest_cpu, bool src_cpu_dead, - struct task_struct *p) -{ - bool check_groups; - - rcu_read_lock(); - check_groups = task_in_related_thread_group(p); - rcu_read_unlock(); - - if (!same_freq_domain(src_cpu, dest_cpu)) { - if (!src_cpu_dead) - check_for_freq_change(cpu_rq(src_cpu), false, - check_groups); - check_for_freq_change(cpu_rq(dest_cpu), false, check_groups); - } else { - check_for_freq_change(cpu_rq(dest_cpu), true, check_groups); - } -} - /* * migration_cpu_stop - this will be executed by a highprio stopper thread * and performs thread migration by bumping thread off CPU then diff --git a/kernel/sched/hmp.c b/kernel/sched/hmp.c index 84cce75c6e50..162d4a0c950c 100644 --- a/kernel/sched/hmp.c +++ b/kernel/sched/hmp.c @@ -1786,6 +1786,25 @@ void check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups) (void *)(long)cpu); } +void notify_migration(int src_cpu, int dest_cpu, bool src_cpu_dead, + struct task_struct *p) +{ + bool check_groups; + + rcu_read_lock(); + check_groups = task_in_related_thread_group(p); + rcu_read_unlock(); + + if (!same_freq_domain(src_cpu, dest_cpu)) { + if (!src_cpu_dead) + check_for_freq_change(cpu_rq(src_cpu), false, + check_groups); + check_for_freq_change(cpu_rq(dest_cpu), false, check_groups); + } else { + check_for_freq_change(cpu_rq(dest_cpu), true, check_groups); + } +} + static int account_busy_for_cpu_time(struct rq *rq, struct task_struct *p, u64 irqtime, int event) { diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 0bae93891647..b09d3a1a026f 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1300,6 +1300,9 @@ struct related_thread_group *task_related_thread_group(struct task_struct *p) extern void check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups); +extern void notify_migration(int src_cpu, int dest_cpu, + bool src_cpu_dead, struct task_struct *p); + struct group_cpu_time { u64 curr_runnable_sum; u64 prev_runnable_sum; @@ -1580,6 +1583,9 @@ static inline int update_preferred_cluster(struct related_thread_group *grp, static inline void check_for_freq_change(struct rq *rq, bool check_pred, bool check_groups) { } +static inline void notify_migration(int src_cpu, int dest_cpu, + bool src_cpu_dead, struct task_struct *p) { } + static inline int same_freq_domain(int src_cpu, int dst_cpu) { return 1; From fbc251af5ad5b14d1a782575833be7c7a77fde16 Mon Sep 17 00:00:00 2001 From: Olav Haugan Date: Mon, 15 Aug 2016 15:52:50 -0700 Subject: [PATCH 13/16] sched/fair: Add flag to indicate why we picked the CPU Add a flag to the trace event that indicates why we picked a particular CPU. This is very useful information/statistic that can be used to analyse the effectiveness of the scheduler. Change-Id: Ic9462fef751f9442ae504c09fbf4418e08f018b0 Signed-off-by: Olav Haugan --- include/trace/events/sched.h | 12 ++++---- kernel/sched/fair.c | 55 ++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index 8367eba91a2e..f35630045c2f 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -116,9 +116,9 @@ TRACE_EVENT(sched_enq_deq_task, TRACE_EVENT(sched_task_load, TP_PROTO(struct task_struct *p, bool boost, int reason, - bool sync, bool need_idle, bool fast_path, int best_cpu), + bool sync, bool need_idle, u32 flags, int best_cpu), - TP_ARGS(p, boost, reason, sync, need_idle, fast_path, best_cpu), + TP_ARGS(p, boost, reason, sync, need_idle, flags, best_cpu), TP_STRUCT__entry( __array( char, comm, TASK_COMM_LEN ) @@ -128,7 +128,7 @@ TRACE_EVENT(sched_task_load, __field( int, reason ) __field( bool, sync ) __field( bool, need_idle ) - __field( bool, fast_path ) + __field( u32, flags ) __field( int, best_cpu ) __field( u64, latency ) ), @@ -141,17 +141,17 @@ TRACE_EVENT(sched_task_load, __entry->reason = reason; __entry->sync = sync; __entry->need_idle = need_idle; - __entry->fast_path = fast_path; + __entry->flags = flags; __entry->best_cpu = best_cpu; __entry->latency = p->state == TASK_WAKING ? sched_ktime_clock() - p->ravg.mark_start : 0; ), - TP_printk("%d (%s): demand=%u boost=%d reason=%d sync=%d need_idle=%d fast_path=%d best_cpu=%d latency=%llu", + TP_printk("%d (%s): demand=%u boost=%d reason=%d sync=%d need_idle=%d flags=%x best_cpu=%d latency=%llu", __entry->pid, __entry->comm, __entry->demand, __entry->boost, __entry->reason, __entry->sync, - __entry->need_idle, __entry->fast_path, + __entry->need_idle, __entry->flags, __entry->best_cpu, __entry->latency) ); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 30f32f07e54f..ce58e2245b4b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2579,6 +2579,23 @@ static u32 __compute_runnable_contrib(u64 n) #ifdef CONFIG_SCHED_HMP +/* CPU selection flag */ +#define SBC_FLAG_PREV_CPU 0x1 +#define SBC_FLAG_BEST_CAP_CPU 0x2 +#define SBC_FLAG_CPU_COST 0x4 +#define SBC_FLAG_MIN_COST 0x8 +#define SBC_FLAG_IDLE_LEAST_LOADED 0x10 +#define SBC_FLAG_IDLE_CSTATE 0x20 +#define SBC_FLAG_COST_CSTATE_TIE_BREAKER 0x40 +#define SBC_FLAG_COST_CSTATE_PREV_CPU_TIE_BREAKER 0x80 +#define SBC_FLAG_CSTATE_LOAD 0x100 +#define SBC_FLAG_BEST_SIBLING 0x200 + +/* Cluster selection flag */ +#define SBC_FLAG_COLOC_CLUSTER 0x10000 +#define SBC_FLAG_WAKER_CLUSTER 0x20000 +#define SBC_FLAG_BACKUP_CLUSTER 0x40000 + struct cpu_select_env { struct task_struct *p; struct related_thread_group *rtg; @@ -2593,6 +2610,8 @@ struct cpu_select_env { DECLARE_BITMAP(backup_list, NR_CPUS); u64 task_load; u64 cpu_load; + u32 sbc_best_flag; + u32 sbc_best_cluster_flag; }; struct cluster_cpu_stats { @@ -2687,6 +2706,7 @@ select_least_power_cluster(struct cpu_select_env *env) if (env->rtg) { env->task_load = scale_load_to_cpu(task_load(env->p), cluster_first_cpu(env->rtg->preferred_cluster)); + env->sbc_best_cluster_flag |= SBC_FLAG_COLOC_CLUSTER; return env->rtg->preferred_cluster; } @@ -2765,6 +2785,7 @@ struct cpu_select_env *env, struct cluster_cpu_stats *stats) update_spare_capacity(stats, env, i, next->capacity, cpu_load_sync(i, env->sync)); } + env->sbc_best_cluster_flag = SBC_FLAG_BACKUP_CLUSTER; } } @@ -2836,6 +2857,7 @@ static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, stats->best_cpu_cstate = cpu_cstate; stats->best_load = env->cpu_load; stats->best_cpu = cpu; + env->sbc_best_flag = SBC_FLAG_CPU_COST; return; } @@ -2848,12 +2870,14 @@ static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, stats->best_cpu_cstate = cpu_cstate; stats->best_load = env->cpu_load; stats->best_cpu = cpu; + env->sbc_best_flag = SBC_FLAG_COST_CSTATE_TIE_BREAKER; return; } /* C-state is the same. Use prev CPU to break the tie */ if (cpu == prev_cpu) { stats->best_cpu = cpu; + env->sbc_best_flag = SBC_FLAG_COST_CSTATE_PREV_CPU_TIE_BREAKER; return; } @@ -2862,6 +2886,7 @@ static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, (cpu_cstate > 0 && env->cpu_load > stats->best_load))) { stats->best_load = env->cpu_load; stats->best_cpu = cpu; + env->sbc_best_flag = SBC_FLAG_CSTATE_LOAD; } } #else /* CONFIG_SCHED_HMP_CSTATE_AWARE */ @@ -2892,6 +2917,7 @@ static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats, stats->min_cost = cpu_cost; stats->min_load = env->cpu_load; stats->best_cpu = cpu; + env->sbc_best_flag = SBC_FLAG_MIN_COST; } } } @@ -3049,8 +3075,8 @@ static int select_best_cpu(struct task_struct *p, int target, int reason, { struct sched_cluster *cluster, *pref_cluster = NULL; struct cluster_cpu_stats stats; - bool fast_path = false; struct related_thread_group *grp; + unsigned int sbc_flag = 0; struct cpu_select_env env = { .p = p, @@ -3062,6 +3088,8 @@ static int select_best_cpu(struct task_struct *p, int target, int reason, .prev_cpu = target, .ignore_prev_cpu = 0, .rtg = NULL, + .sbc_best_flag = 0, + .sbc_best_cluster_flag = 0, }; bitmap_copy(env.candidate_list, all_cluster_ids, NR_CPUS); @@ -3086,8 +3114,10 @@ static int select_best_cpu(struct task_struct *p, int target, int reason, env.need_waker_cluster = 1; bitmap_zero(env.candidate_list, NR_CPUS); __set_bit(cluster->id, env.candidate_list); + env.sbc_best_cluster_flag = SBC_FLAG_WAKER_CLUSTER; + } else if (bias_to_prev_cpu(&env, &stats)) { - fast_path = true; + sbc_flag = SBC_FLAG_PREV_CPU; goto out; } } @@ -3111,15 +3141,20 @@ retry: } while ((cluster = next_best_cluster(cluster, &env, &stats))); if (env.need_idle) { - if (stats.best_idle_cpu >= 0) + if (stats.best_idle_cpu >= 0) { target = stats.best_idle_cpu; - else if (stats.least_loaded_cpu >= 0) + sbc_flag |= SBC_FLAG_IDLE_CSTATE; + } else if (stats.least_loaded_cpu >= 0) { target = stats.least_loaded_cpu; + sbc_flag |= SBC_FLAG_IDLE_LEAST_LOADED; + } } else if (stats.best_cpu >= 0) { if (stats.best_cpu != task_cpu(p) && - stats.min_cost == stats.best_sibling_cpu_cost) + stats.min_cost == stats.best_sibling_cpu_cost) { stats.best_cpu = stats.best_sibling_cpu; - + sbc_flag |= SBC_FLAG_BEST_SIBLING; + } + sbc_flag |= env.sbc_best_flag; target = stats.best_cpu; } else { if (env.rtg) { @@ -3128,15 +3163,17 @@ retry: } find_backup_cluster(&env, &stats); - if (stats.best_capacity_cpu >= 0) + if (stats.best_capacity_cpu >= 0) { target = stats.best_capacity_cpu; + sbc_flag |= SBC_FLAG_BEST_CAP_CPU; + } } p->last_cpu_selected_ts = sched_ktime_clock(); - + sbc_flag |= env.sbc_best_cluster_flag; out: rcu_read_unlock(); trace_sched_task_load(p, sched_boost(), env.reason, env.sync, - env.need_idle, fast_path, target); + env.need_idle, sbc_flag, target); return target; } From 51e9d516fa1da9b9221e28349923a6c1d641e8a4 Mon Sep 17 00:00:00 2001 From: Pavankumar Kondeti Date: Fri, 11 Mar 2016 12:33:40 +0530 Subject: [PATCH 14/16] proc: relax write permissions of sched_group_id file Related tasks are grouped together based on their sched_group_id. The userspace implementation requires world write permissions to this /proc//sched_group_id file. Change-Id: I92e6fd3ca693387aeb0664cc75afde06f75d4a9f Signed-off-by: Pavankumar Kondeti --- fs/proc/base.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 5120d772d9d6..de2dcc1d1167 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2947,7 +2947,7 @@ static const struct pid_entry tgid_base_stuff[] = { #endif #ifdef CONFIG_SCHED_HMP REG("sched_init_task_load", S_IRUGO|S_IWUSR, proc_pid_sched_init_task_load_operations), - REG("sched_group_id", S_IRUGO|S_IWUSR, proc_pid_sched_group_id_operations), + REG("sched_group_id", S_IRUGO|S_IWUGO, proc_pid_sched_group_id_operations), #endif #ifdef CONFIG_SCHED_DEBUG REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), From 5ddfbfec063cd077dd55df47f8acf5df95a87477 Mon Sep 17 00:00:00 2001 From: Pavankumar Kondeti Date: Wed, 21 Oct 2015 16:04:46 +0530 Subject: [PATCH 15/16] sched: inherit the group id from the group leader When sysctl_sched_enable_thread_grouping is set to 1, any new tasks created are put in the same group as their group leader. Change-Id: If1837dd7c8120c8b097cfffa1dc52eb4781f1641 Signed-off-by: Pavankumar Kondeti --- include/linux/sched/sysctl.h | 1 + kernel/sched/core.c | 1 + kernel/sched/hmp.c | 43 ++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 3 +++ kernel/sysctl.c | 7 ++++++ 5 files changed, 55 insertions(+) diff --git a/include/linux/sched/sysctl.h b/include/linux/sched/sysctl.h index c60075de415b..68a9bdde6604 100644 --- a/include/linux/sched/sysctl.h +++ b/include/linux/sched/sysctl.h @@ -62,6 +62,7 @@ extern unsigned int sysctl_sched_restrict_cluster_spill; extern unsigned int sysctl_sched_new_task_windows; extern unsigned int sysctl_sched_pred_alert_freq; extern unsigned int sysctl_sched_freq_aggregate; +extern unsigned int sysctl_sched_enable_thread_grouping; #else /* CONFIG_SCHED_HMP */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a790d101d120..94bd3cffa5ac 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2540,6 +2540,7 @@ void wake_up_new_task(struct task_struct *p) raw_spin_lock_irqsave(&p->pi_lock, flags); init_new_task_load(p); + add_new_task_to_grp(p); /* Initialize new task's runnable average */ init_entity_runnable_average(&p->se); #ifdef CONFIG_SMP diff --git a/kernel/sched/hmp.c b/kernel/sched/hmp.c index 162d4a0c950c..898da9b83a72 100644 --- a/kernel/sched/hmp.c +++ b/kernel/sched/hmp.c @@ -696,6 +696,13 @@ __read_mostly unsigned int sysctl_sched_cpu_high_irqload = (10 * NSEC_PER_MSEC); unsigned int __read_mostly sysctl_sched_enable_colocation = 1; +/* + * Enable colocation and frequency aggregation for all threads in a process. + * The children inherits the group id from the parent. + */ +unsigned int __read_mostly sysctl_sched_enable_thread_grouping; + + __read_mostly unsigned int sysctl_sched_new_task_windows = 5; #define SCHED_FREQ_ACCOUNT_WAIT_TIME 0 @@ -3562,6 +3569,42 @@ add_task_to_group(struct task_struct *p, struct related_thread_group *grp) return 0; } +void add_new_task_to_grp(struct task_struct *new) +{ + unsigned long flags; + struct related_thread_group *grp; + struct task_struct *parent; + + if (!sysctl_sched_enable_thread_grouping) + return; + + if (thread_group_leader(new)) + return; + + parent = new->group_leader; + + /* + * The parent's pi_lock is required here to protect race + * against the parent task being removed from the + * group. + */ + raw_spin_lock_irqsave(&parent->pi_lock, flags); + + /* protected by pi_lock. */ + grp = task_related_thread_group(parent); + if (!grp) { + raw_spin_unlock_irqrestore(&parent->pi_lock, flags); + return; + } + raw_spin_lock(&grp->lock); + + rcu_assign_pointer(new->grp, grp); + list_add(&new->grp_list, &grp->tasks); + + raw_spin_unlock(&grp->lock); + raw_spin_unlock_irqrestore(&parent->pi_lock, flags); +} + int sched_set_group_id(struct task_struct *p, unsigned int group_id) { int rc = 0, destroy = 0; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index b09d3a1a026f..cdfccdeb4eea 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1100,6 +1100,7 @@ extern void update_up_down_migrate(void); extern int update_preferred_cluster(struct related_thread_group *grp, struct task_struct *p, u32 old_load); extern void set_preferred_cluster(struct related_thread_group *grp); +extern void add_new_task_to_grp(struct task_struct *new); enum sched_boost_type { SCHED_BOOST_NONE, @@ -1575,6 +1576,8 @@ static inline int update_preferred_cluster(struct related_thread_group *grp, return 0; } +static inline void add_new_task_to_grp(struct task_struct *new) {} + #define sched_enable_hmp 0 #define sched_freq_legacy_mode 1 #define sched_migration_fixup 0 diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 8b9ca50dc53f..ac34212f6881 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -405,6 +405,13 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = sched_hmp_proc_update_handler, }, + { + .procname = "sched_enable_thread_grouping", + .data = &sysctl_sched_enable_thread_grouping, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec, + }, { .procname = "sched_new_task_windows", .data = &sysctl_sched_new_task_windows, From 2552980f79e476b99d9f489c265a836dd61a2102 Mon Sep 17 00:00:00 2001 From: Pavankumar Kondeti Date: Fri, 12 Aug 2016 16:12:53 +0530 Subject: [PATCH 16/16] sched: handle frequency alert notifications better The load reporting during frequency alert notifications is broken under load aggregation. When aggregation is enabled, the total group busy time is accounted towards the maximum busy CPU of a frequency domain. If this CPU has a notification pending, it's group busy time alone is accounted and other CPU's group busy time is completely ignored. Similarly if any CPU other than maximum busy CPU has a pending notification, its group busy time is accounted twice. Maintain the frequency alert notification flag per frequency domain. When the notification is pending, don't clip the load to 100% @ fur for any of the CPUs in the frequency domain. Change-Id: Iebc7d74d6fafa20430fa1c7d80f34a6ab198832d Signed-off-by: Pavankumar Kondeti --- kernel/sched/core.c | 1 - kernel/sched/hmp.c | 24 +++++++++++++++++------- kernel/sched/sched.h | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 94bd3cffa5ac..a1626bdf8729 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7748,7 +7748,6 @@ void __init sched_init(void) rq->old_busy_time = 0; rq->old_estimated_time = 0; rq->old_busy_time_group = 0; - rq->notifier_sent = 0; rq->hmp_stats.pred_demands_sum = 0; #endif rq->max_idle_balance_cost = sysctl_sched_migration_cost; diff --git a/kernel/sched/hmp.c b/kernel/sched/hmp.c index 898da9b83a72..8da0147b4f89 100644 --- a/kernel/sched/hmp.c +++ b/kernel/sched/hmp.c @@ -356,6 +356,7 @@ struct sched_cluster init_cluster = { .dstate_wakeup_energy = 0, .dstate_wakeup_latency = 0, .exec_scale_factor = 1024, + .notifier_sent = 0, }; static void update_all_clusters_stats(void) @@ -519,6 +520,7 @@ static struct sched_cluster *alloc_new_cluster(const struct cpumask *cpus) if (cluster->efficiency < min_possible_efficiency) min_possible_efficiency = cluster->efficiency; + cluster->notifier_sent = 0; return cluster; } @@ -1769,8 +1771,8 @@ static int send_notification(struct rq *rq, int check_pred, int check_groups) } raw_spin_lock_irqsave(&rq->lock, flags); - if (!rq->notifier_sent) { - rq->notifier_sent = 1; + if (!rq->cluster->notifier_sent) { + rq->cluster->notifier_sent = 1; rc = 1; trace_sched_freq_alert(cpu_of(rq), check_pred, check_groups, rq, new_load); @@ -2938,7 +2940,7 @@ void sched_get_cpus_busy(struct sched_load *busy, u64 nload[cpus], ngload[cpus]; u64 pload[cpus]; unsigned int cur_freq[cpus], max_freq[cpus]; - int notifier_sent[cpus]; + int notifier_sent = 0; int early_detection[cpus]; int cpu, i = 0; unsigned int window_size; @@ -2979,9 +2981,17 @@ void sched_get_cpus_busy(struct sched_load *busy, max_busy_cpu = cpu; } - notifier_sent[i] = rq->notifier_sent; + /* + * sched_get_cpus_busy() is called for all CPUs in a + * frequency domain. So the notifier_sent flag per + * cluster works even when a frequency domain spans + * more than 1 cluster. + */ + if (rq->cluster->notifier_sent) { + notifier_sent = 1; + rq->cluster->notifier_sent = 0; + } early_detection[i] = (rq->ed_task != NULL); - rq->notifier_sent = 0; cur_freq[i] = cpu_cur_freq(cpu); max_freq[i] = cpu_max_freq(cpu); i++; @@ -3005,7 +3015,7 @@ void sched_get_cpus_busy(struct sched_load *busy, goto skip_early; rq = cpu_rq(cpu); - if (!notifier_sent[i]) { + if (!notifier_sent) { if (cpu == max_busy_cpu) group_load_in_freq_domain( &rq->freq_domain_cpumask, @@ -3046,7 +3056,7 @@ skip_early: goto exit_early; } - if (!notifier_sent[i]) { + if (!notifier_sent) { load[i] = scale_load_to_freq(load[i], max_freq[i], cur_freq[i]); nload[i] = scale_load_to_freq(nload[i], max_freq[i], diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index cdfccdeb4eea..e31334d5f581 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -391,6 +391,7 @@ struct sched_cluster { bool freq_init_done; int dstate, dstate_wakeup_latency, dstate_wakeup_energy; unsigned int static_cluster_pwr_cost; + int notifier_sent; }; extern unsigned long all_cluster_ids[]; @@ -749,7 +750,6 @@ struct rq { struct task_struct *ed_task; struct cpu_cycle cc; u64 old_busy_time, old_busy_time_group; - int notifier_sent; u64 old_estimated_time; u64 curr_runnable_sum; u64 prev_runnable_sum;