A controller for an internal combustion engine, wherein the internal combustion engine includes a fuel injection valve, a catalyst provided in an exhaust passage and capable of storing oxygen, and an air-fuel ratio sensor provided downstream of the catalyst in the exhaust passage wherein the air-fuel ratio sensor linearly increases a detection value as the amount of oxygen exceeding the amount of unburned fuel in the exhaust is increased, the controller comprising processing circuitry, wherein the processing circuitry is configured to determine whether the following conditions are fulfilled: an absolute value of an amount of change in a charging efficiency in a predetermined period is less than or equal to a predetermined amount; or an absolute value of an amount of change in a predetermined period of an intake air amount is less than or equal to a predetermined amount; an accumulated value of the intake air amount from a start of the internal combustion engine is greater than or equal to a predetermined value; or a temperature of the catalyst is greater than or equal to a predetermined temperature and the intake air amount is greater than or equal to a lower limit value and is less than or equal to an upper limit value; wherein if conditions or and to are fulfilled, the processing circuitry is configured to execute an inflow process when a permission flag is one and when an oxygen storage amount of the catalyst is greater than or equal to a predetermined amount, the inflow process includes operating the fuel injection valve to cause a fluid containing oxygen and unburned fuel to flow into the catalyst, an amount of the unburned fuel is greater than or equal to an ideal amount of unburned fuel that reacts with all of the oxygen, and the processing circuitry is configured to execute, based on a detection value of the air-fuel ratio sensor obtained during an execution of the inflow process, a deviation amount calculation process that calculates a deviation amount indication value that indicates a deviation amount of a detection value of the air-fuel ratio sensor when oxygen is stored in the catalyst. the processing circuitry is configured to determine whether the following conditions are fulfilled: an absolute value of an amount of change in a charging efficiency in a predetermined period is less than or equal to a predetermined amount; or an absolute value of an amount of change in a predetermined period of an intake air amount is less than or equal to a predetermined amount; an accumulated value of the intake air amount from a start of the internal combustion engine is greater than or equal to a predetermined value; or a temperature of the catalyst is greater than or equal to a predetermined temperature and the intake air amount is greater than or equal to a lower limit value and is less than or equal to an upper limit value; an absolute value of an amount of change in a charging efficiency in a predetermined period is less than or equal to a predetermined amount; or an absolute value of an amount of change in a predetermined period of an intake air amount is less than or equal to a predetermined amount; an accumulated value of the intake air amount from a start of the internal combustion engine is greater than or equal to a predetermined value; or a temperature of the catalyst is greater than or equal to a predetermined temperature and the intake air amount is greater than or equal to a lower limit value and is less than or equal to an upper limit value; wherein if conditions or and to are fulfilled, the processing circuitry is configured to execute an inflow process when a permission flag is one and when an oxygen storage amount of the catalyst is greater than or equal to a predetermined amount, the inflow process includes operating the fuel injection valve to cause a fluid containing oxygen and unburned fuel to flow into the catalyst, an amount of the unburned fuel is greater than or equal to an ideal amount of unburned fuel that reacts with all of the oxygen, and the processing circuitry is configured to execute, based on a detection value of the air-fuel ratio sensor obtained during an execution of the inflow process, a deviation amount calculation process that calculates a deviation amount indication value that indicates a deviation amount of a detection value of the air-fuel ratio sensor when oxygen is stored in the catalyst.