Repository: pytorch
Issue: `MemoryDenyWriteExecute` in systemd service causes `RuntimeError: could not create a primitive`
Labels: needs reproduction,module: error checking,module: convolution,triaged,module: mkldnn,security

`MemoryDenyWriteExecute` would be nice to use, but when pytorch is ran in this context, it throws the following, likely due to generating assembly at runtime, where that generated code is both +w and +x, which is generally a security issue. The code should be `-w`'d when it is done generating.

https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#MemoryDenyWriteExecute=

```
 File "/nix/store/4b0mw59pv52w2kvli1hraqcybww0yy0z-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/conv.py", line 549, in _conv_forward
   return F.conv2d(
          ^^^^^^^^^
 RuntimeError: could not create a primitive
```

A larger trace is below, for the application I'm running in a systemd service (comfyui)

```
model weight dtype torch.float32, manual cast: None
model_type EPS
Using split attention in VAE
Using split attention in VAE
Requested to load SD1ClipModel
loaded completely 9.5367431640625e+25 235.84423828125 True
Requested to load SD1ClipModel
loaded completely 9.5367431640625e+25 235.84423828125 True
Requested to load BaseModel
loaded completely 9.5367431640625e+25 3278.812271118164 True
****** User settings have been changed to be stored on the server instead of browser storage. ******
****** For multi-user setups add the --multi-user CLI argument to enable multiple user profiles. ******
  0%|          | 0/1 [00:00<?, ?it/s]  0%|          | 0/1 [00:00<?, ?it/s]
!!! Exception during processing !!! could not create a primitive
Traceback (most recent call last):
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/execution.py", line 324, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/execution.py", line 199, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/execution.py", line 170, in _map_node_over_list
    process_inputs(input_dict, i)
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/execution.py", line 159, in process_inputs
    results.append(getattr(obj, func)(**inputs))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/nodes.py", line 1467, in sample
    return common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=denoise)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/nodes.py", line 1434, in common_ksampler
    samples = comfy.sample.sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/sample.py", line 43, in sample
    samples = sampler.sample(noise, positive, negative, cfg=cfg, latent_image=latent_image, start_step=start_step, last_step=last_step, force_full_denoise=force_full_denoise, denoise_mask=noise_mask, sigmas=sigmas, callback=callback, disable_pbar=disable_pbar, seed=seed)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 1020, in sample
    return sample(self.model, noise, positive, negative, cfg, self.device, sampler, sigmas, self.model_options, latent_image=latent_image, denoise_mask=denoise_mask, callback=callback, disable_pbar=disable_pbar, seed=seed)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 918, in sample
    return cfg_guider.sample(noise, latent_image, sampler, sigmas, denoise_mask, callback, disable_pbar, seed)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 904, in sample
    output = executor.execute(noise, latent_image, sampler, sigmas, denoise_mask, callback, disable_pbar, seed)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/patcher_extension.py", line 110, in execute
    return self.original(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 873, in outer_sample
    output = self.inner_sample(noise, latent_image, device, sampler, sigmas, denoise_mask, callback, disable_pbar, seed)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 857, in inner_sample
    samples = executor.execute(self, sigmas, extra_args, callback, noise, latent_image, denoise_mask, disable_pbar)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/patcher_extension.py", line 110, in execute
    return self.original(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 714, in sample
    samples = self.sampler_function(model_k, noise, sigmas, extra_args=extra_args, callback=k_callback, disable=disable_pbar, **self.extra_options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/k_diffusion/sampling.py", line 155, in sample_euler
    denoised = model(x, sigma_hat * s_in, **extra_args)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 384, in __call__
    out = self.inner_model(x, sigma, model_options=model_options, seed=seed)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 839, in __call__
    return self.predict_noise(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 842, in predict_noise
    return sampling_function(self.inner_model, x, timestep, self.conds.get("negative", None), self.conds.get("positive", None), self.cfg, model_options=model_options, seed=seed)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 364, in sampling_function
    out = calc_cond_batch(model, conds, x, timestep, model_options)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 200, in calc_cond_batch
    return executor.execute(model, conds, x_in, timestep, model_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/patcher_extension.py", line 110, in execute
    return self.original(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/samplers.py", line 313, in _calc_cond_batch
    output = model.apply_model(input_x, timestep_, **c).chunk(batch_chunks)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/model_base.py", line 128, in apply_model
    return comfy.patcher_extension.WrapperExecutor.new_class_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/patcher_extension.py", line 110, in execute
    return self.original(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/model_base.py", line 157, in _apply_model
    model_output = self.diffusion_model(xc, t, context=context, control=control, transformer_options=transformer_options, **extra_conds).float()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 832, in forward
    return comfy.patcher_extension.WrapperExecutor.new_class_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/patcher_extension.py", line 110, in execute
    return self.original(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 874, in _forward
    h = forward_timestep_embed(module, h, emb, context, transformer_options, time_context=time_context, num_video_frames=num_video_frames, image_only_indicator=image_only_indicator)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 39, in forward_timestep_embed
    x = layer(x, emb)
        ^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 240, in forward
    return checkpoint(
           ^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/util.py", line 191, in checkpoint
    return func(*inputs)
           ^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 253, in _forward
    h = self.in_layers(x)
        ^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfh
packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/container.py", line 250, in forward
    input = module(input)
            ^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/css11jdci8dblbzwskpxi47ln5m8iw1x-comfyui-0.3.7/lib/python3.12/site-packages/comfy/ops.py", line 98, in forward
    return super().forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/conv.py", line 554, in forward
    return self._conv_forward(input, self.weight, self.bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/minbaa1r1viqhnfhq9gna38fgmj5psxc-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/nn/modules/conv.py", line 549, in _conv_forward
    return F.conv2d(
```


cc @malfet @gujinghui @PenghuiCheng @XiaobingSuper @jianyuh @jgong5 @mingfeima @sanchitintel @ashokei @jingxu10 @min-jean-cho @yanbing-j @Guobing-Chen @Xia-Weiwen @snadampal @seemethere @pytorch/pytorch-dev-infra