Re: [Intel-gfx] [PATCH v5 0/7] Introduce __xchg, non-atomic xchg

2024-04-05 Thread Andrzej Hajda




On 05.04.2024 16:47, Jani Nikula wrote:

On Mon, 27 Feb 2023, Peter Zijlstra  wrote:

On Thu, Feb 23, 2023 at 10:24:19PM +0100, Andrzej Hajda wrote:

On 22.02.2023 18:04, Peter Zijlstra wrote:

On Wed, Jan 18, 2023 at 04:35:22PM +0100, Andrzej Hajda wrote:


Andrzej Hajda (7):
arch: rename all internal names __xchg to __arch_xchg
linux/include: add non-atomic version of xchg
arch/*/uprobes: simplify arch_uretprobe_hijack_return_addr
llist: simplify __llist_del_all
io_uring: use __xchg if possible
qed: use __xchg if possible
drm/i915/gt: use __xchg instead of internal helper

Nothing crazy in here I suppose, I somewhat wonder why you went through
the trouble, but meh.

If you are asking why I have proposed this patchset, then the answer is
simple, 1st I've tried to find a way to move internal i915 helper to core
(see patch 7).
Then I was looking for possible other users of this helper. And apparently
there are many of them, patches 3-7 shows some.



You want me to take this through te locking tree (for the next cycle,
not this one) where I normally take atomic things or does someone else
want this?

If you could take it I will be happy.

OK, I'll go queue it in tip/locking/core after -rc1. Thanks!

Is this where the series fell between the cracks, or was there some
follow-up that I missed?

I think this would still be useful. Andrzej, would you mind rebasing and
resending if there are no objections?


The patchset was rejected/dropped by Linus at the pull-request stage.
He didn't like many things, but the most __xchg name. However he was 
quite positive about i915 name fetch_and_zero.
I can try to revive patchset with fetch_and_zero, and maybe 
fetch_and_set, instead of __xchg.


Regards
Andrzej



BR,
Jani.






Re: [Intel-gfx] [PATCH v5 0/7] Introduce __xchg, non-atomic xchg

2023-02-23 Thread Andrzej Hajda

On 22.02.2023 18:04, Peter Zijlstra wrote:

On Wed, Jan 18, 2023 at 04:35:22PM +0100, Andrzej Hajda wrote:


Andrzej Hajda (7):
   arch: rename all internal names __xchg to __arch_xchg
   linux/include: add non-atomic version of xchg
   arch/*/uprobes: simplify arch_uretprobe_hijack_return_addr
   llist: simplify __llist_del_all
   io_uring: use __xchg if possible
   qed: use __xchg if possible
   drm/i915/gt: use __xchg instead of internal helper


Nothing crazy in here I suppose, I somewhat wonder why you went through
the trouble, but meh.


If you are asking why I have proposed this patchset, then the answer is 
simple, 1st I've tried to find a way to move internal i915 helper to 
core (see patch 7).
Then I was looking for possible other users of this helper. And 
apparently there are many of them, patches 3-7 shows some.





You want me to take this through te locking tree (for the next cycle,
not this one) where I normally take atomic things or does someone else
want this?


If you could take it I will be happy.

Regards
Andrzej



Re: [Intel-gfx] [PATCH v5 0/7] Introduce __xchg, non-atomic xchg

2023-02-22 Thread Andrzej Hajda

Hi,

Ping on the series.
Arnd, Andrew is there anything more I can do to push the process forward?

Regards
Andrzej


On 18.01.2023 16:35, Andrzej Hajda wrote:

Hi all,

The helper is tiny and there are advices we can live without it, so
I want to present few arguments why it would be good to have it:

1. Code readability/simplification/number of lines:
   - decreases number of lines,
   - it often eliminates local variables,
   - for real examples see patches 3+.

2. Presence of similar helpers in other somehow related languages/libs:

a) Rust[1]: 'replace' from std::mem module, there is also 'take'
 helper (__xchg(, 0)), which is the same as private helper in
 i915 - fetch_and_zero, see latest patch.
b) C++ [2]: 'exchange' from utility header.

If the idea is OK there are still 2 questions to answer:

1. Name of the helper, __xchg follows kernel conventions,
 but for me Rust names are also OK.
2. Where to put the helper:
a) as in this patchset include/linux/non-atomic/xchg.h,
 proposed by Andy Shevchenko,
b) include/linux/utils.h ? any better name? Some kind
 of container for simple helpers.

All __xchg conversions were performed using cocci script,
then manually adjusted if necessary.

There is lot of places it can be used in, I have just chosen
some of them. I can provide cocci script to detect others (not all),
if necessary.

Changes:
v2: squashed all __xchg -> __arch_xchg t one patch (Arnd)
v3: fixed alpha/xchg_local (l...@intel.com)
v4: adjusted indentation (Heiko)
v5: added more __xchg conversions - patches 3-6, added tags

[1]: https://doc.rust-lang.org/std/mem/index.html
[2]: https://en.cppreference.com/w/cpp/header/utility

Regards
Andrzej

Andrzej Hajda (7):
   arch: rename all internal names __xchg to __arch_xchg
   linux/include: add non-atomic version of xchg
   arch/*/uprobes: simplify arch_uretprobe_hijack_return_addr
   llist: simplify __llist_del_all
   io_uring: use __xchg if possible
   qed: use __xchg if possible
   drm/i915/gt: use __xchg instead of internal helper

  arch/alpha/include/asm/cmpxchg.h  | 10 +-
  arch/arc/include/asm/cmpxchg.h|  4 ++--
  arch/arm/include/asm/cmpxchg.h|  7 ---
  arch/arm/probes/uprobes/core.c|  8 ++--
  arch/arm64/include/asm/cmpxchg.h  |  7 +++
  arch/arm64/kernel/probes/uprobes.c|  9 ++---
  arch/csky/kernel/probes/uprobes.c |  9 ++---
  arch/hexagon/include/asm/cmpxchg.h| 10 +-
  arch/ia64/include/asm/cmpxchg.h   |  2 +-
  arch/ia64/include/uapi/asm/cmpxchg.h  |  4 ++--
  arch/loongarch/include/asm/cmpxchg.h  |  4 ++--
  arch/m68k/include/asm/cmpxchg.h   |  6 +++---
  arch/mips/include/asm/cmpxchg.h   |  4 ++--
  arch/mips/kernel/uprobes.c| 10 ++
  arch/openrisc/include/asm/cmpxchg.h   | 10 +-
  arch/parisc/include/asm/cmpxchg.h |  4 ++--
  arch/powerpc/include/asm/cmpxchg.h|  4 ++--
  arch/powerpc/kernel/uprobes.c | 10 ++
  arch/riscv/include/asm/atomic.h   |  2 +-
  arch/riscv/include/asm/cmpxchg.h  |  4 ++--
  arch/riscv/kernel/probes/uprobes.c|  9 ++---
  arch/s390/include/asm/cmpxchg.h   |  8 
  arch/s390/kernel/uprobes.c|  7 ++-
  arch/sh/include/asm/cmpxchg.h |  4 ++--
  arch/sparc/include/asm/cmpxchg_32.h   |  4 ++--
  arch/sparc/include/asm/cmpxchg_64.h   |  6 +++---
  arch/sparc/kernel/uprobes.c   |  7 ++-
  arch/xtensa/include/asm/cmpxchg.h |  4 ++--
  drivers/gpu/drm/i915/gt/intel_engine_cs.c |  2 +-
  .../gpu/drm/i915/gt/intel_engine_heartbeat.c  |  4 ++--
  .../drm/i915/gt/intel_execlists_submission.c  |  4 ++--
  drivers/gpu/drm/i915/gt/intel_ggtt.c  |  4 ++--
  drivers/gpu/drm/i915/gt/intel_gsc.c   |  2 +-
  drivers/gpu/drm/i915/gt/intel_gt.c|  4 ++--
  drivers/gpu/drm/i915/gt/intel_gt_pm.c |  2 +-
  drivers/gpu/drm/i915/gt/intel_lrc.c   |  6 +++---
  drivers/gpu/drm/i915/gt/intel_migrate.c   |  2 +-
  drivers/gpu/drm/i915/gt/intel_rc6.c   |  2 +-
  drivers/gpu/drm/i915/gt/intel_rps.c   |  2 +-
  drivers/gpu/drm/i915/gt/selftest_context.c|  2 +-
  .../drm/i915/gt/selftest_ring_submission.c|  2 +-
  drivers/gpu/drm/i915/gt/selftest_timeline.c   |  2 +-
  drivers/gpu/drm/i915/gt/uc/intel_gsc_uc.c |  2 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc.c |  2 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c  |  2 +-
  drivers/gpu/drm/i915/i915_utils.h |  1 +
  include/linux/llist.h |  6 ++
  include/linux/non-atomic/xchg.h   | 19 +++
  include/linux/qed/qed_chain.h | 19 +++
  io_uring/io_urin

[PATCH v5 7/7] drm/i915/gt: use __xchg instead of internal helper

2023-01-18 Thread Andrzej Hajda
Prefer core helper if available.

Signed-off-by: Andrzej Hajda 
---
 drivers/gpu/drm/i915/gt/intel_engine_cs.c| 2 +-
 drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_execlists_submission.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_ggtt.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_gsc.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_gt.c   | 4 ++--
 drivers/gpu/drm/i915/gt/intel_gt_pm.c| 2 +-
 drivers/gpu/drm/i915/gt/intel_lrc.c  | 6 +++---
 drivers/gpu/drm/i915/gt/intel_migrate.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_rc6.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_rps.c  | 2 +-
 drivers/gpu/drm/i915/gt/selftest_context.c   | 2 +-
 drivers/gpu/drm/i915/gt/selftest_ring_submission.c   | 2 +-
 drivers/gpu/drm/i915/gt/selftest_timeline.c  | 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_gsc_uc.c| 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.c| 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c | 2 +-
 drivers/gpu/drm/i915/i915_utils.h| 1 +
 18 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/drivers/gpu/drm/i915/gt/intel_engine_cs.c 
b/drivers/gpu/drm/i915/gt/intel_engine_cs.c
index 922f1bb22dc685..9712bfc2c6523d 100644
--- a/drivers/gpu/drm/i915/gt/intel_engine_cs.c
+++ b/drivers/gpu/drm/i915/gt/intel_engine_cs.c
@@ -1042,7 +1042,7 @@ static void cleanup_status_page(struct intel_engine_cs 
*engine)
/* Prevent writes into HWSP after returning the page to the system */
intel_engine_set_hwsp_writemask(engine, ~0u);
 
-   vma = fetch_and_zero(>status_page.vma);
+   vma = __xchg(>status_page.vma, 0);
if (!vma)
return;
 
diff --git a/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c 
b/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
index 9a527e1f5be655..09befcc6a84fa1 100644
--- a/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
+++ b/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
@@ -229,7 +229,7 @@ static void heartbeat(struct work_struct *wrk)
mutex_unlock(>timeline->mutex);
 out:
if (!engine->i915->params.enable_hangcheck || !next_heartbeat(engine))
-   i915_request_put(fetch_and_zero(>heartbeat.systole));
+   i915_request_put(__xchg(>heartbeat.systole, 0));
intel_engine_pm_put(engine);
 }
 
@@ -244,7 +244,7 @@ void intel_engine_unpark_heartbeat(struct intel_engine_cs 
*engine)
 void intel_engine_park_heartbeat(struct intel_engine_cs *engine)
 {
if (cancel_delayed_work(>heartbeat.work))
-   i915_request_put(fetch_and_zero(>heartbeat.systole));
+   i915_request_put(__xchg(>heartbeat.systole, 0));
 }
 
 void intel_gt_unpark_heartbeats(struct intel_gt *gt)
diff --git a/drivers/gpu/drm/i915/gt/intel_execlists_submission.c 
b/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
index 18ffe55282e594..5c985e6fa1be2f 100644
--- a/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
+++ b/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
@@ -3199,7 +3199,7 @@ static void execlists_reset_cancel(struct intel_engine_cs 
*engine)
RB_CLEAR_NODE(rb);
 
spin_lock(>base.sched_engine->lock);
-   rq = fetch_and_zero(>request);
+   rq = __xchg(>request, NULL);
if (rq) {
if (i915_request_mark_eio(rq)) {
rq->engine = engine;
@@ -3604,7 +3604,7 @@ static void rcu_virtual_context_destroy(struct 
work_struct *wrk)
 
spin_lock_irq(>base.sched_engine->lock);
 
-   old = fetch_and_zero(>request);
+   old = __xchg(>request, NULL);
if (old) {
GEM_BUG_ON(!__i915_request_is_complete(old));
__i915_request_submit(old);
diff --git a/drivers/gpu/drm/i915/gt/intel_ggtt.c 
b/drivers/gpu/drm/i915/gt/intel_ggtt.c
index fe64c13fd3b4aa..6f441c3d3d1cef 100644
--- a/drivers/gpu/drm/i915/gt/intel_ggtt.c
+++ b/drivers/gpu/drm/i915/gt/intel_ggtt.c
@@ -684,7 +684,7 @@ static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
 {
struct i915_ppgtt *ppgtt;
 
-   ppgtt = fetch_and_zero(>alias);
+   ppgtt = __xchg(>alias, NULL);
if (!ppgtt)
return;
 
@@ -1238,7 +1238,7 @@ bool i915_ggtt_resume_vm(struct i915_address_space *vm)
   was_bound);
 
if (obj) { /* only used during resume => exclusive access */
-   write_domain_objs |= fetch_and_zero(>write_domain);
+   write_domain_objs |= __xchg(>write_domain, 0);
obj->read_domains |= I915_GEM_DOMAIN_GTT;
}
}
diff --git a/drivers/gpu/drm/i915/gt/intel_gsc.c 
b/drivers/gpu/drm

[PATCH v5 6/7] qed: use __xchg if possible

2023-01-18 Thread Andrzej Hajda
Recently introduced helper simplifies the code.

Signed-off-by: Andrzej Hajda 
---
 include/linux/qed/qed_chain.h | 19 +++
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/include/linux/qed/qed_chain.h b/include/linux/qed/qed_chain.h
index a84063492c71ae..6355d558cf01b2 100644
--- a/include/linux/qed/qed_chain.h
+++ b/include/linux/qed/qed_chain.h
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -368,7 +369,7 @@ static inline void qed_chain_return_produced(struct 
qed_chain *p_chain)
  */
 static inline void *qed_chain_produce(struct qed_chain *p_chain)
 {
-   void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx;
+   void *p_prod_idx, *p_prod_page_idx;
 
if (is_chain_u16(p_chain)) {
if ((p_chain->u.chain16.prod_idx &
@@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain 
*p_chain)
p_chain->u.chain32.prod_idx++;
}
 
-   p_ret = p_chain->p_prod_elem;
-   p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
-   p_chain->elem_size);
-
-   return p_ret;
+   return __xchg(_chain->p_prod_elem,
+ (u8 *)p_chain->p_prod_elem + p_chain->elem_size);
 }
 
 /**
@@ -439,7 +437,7 @@ static inline void qed_chain_recycle_consumed(struct 
qed_chain *p_chain)
  */
 static inline void *qed_chain_consume(struct qed_chain *p_chain)
 {
-   void *p_ret = NULL, *p_cons_idx, *p_cons_page_idx;
+   void *p_cons_idx, *p_cons_page_idx;
 
if (is_chain_u16(p_chain)) {
if ((p_chain->u.chain16.cons_idx &
@@ -461,11 +459,8 @@ static inline void *qed_chain_consume(struct qed_chain 
*p_chain)
p_chain->u.chain32.cons_idx++;
}
 
-   p_ret = p_chain->p_cons_elem;
-   p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
-   p_chain->elem_size);
-
-   return p_ret;
+   return __xchg(_chain->p_cons_elem,
+ (u8 *)p_chain->p_cons_elem + p_chain->elem_size);
 }
 
 /**
-- 
2.34.1



[PATCH v5 5/7] io_uring: use __xchg if possible

2023-01-18 Thread Andrzej Hajda
Recently introduced helper simplifies the code.

Signed-off-by: Andrzej Hajda 
---
 io_uring/io_uring.c | 7 ++-
 io_uring/slist.h| 6 ++
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 2ac1cd8d23ea62..2b46a692d69022 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -54,6 +54,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1095,8 +1096,6 @@ static void __io_req_find_next_prep(struct io_kiocb *req)
 
 static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
 {
-   struct io_kiocb *nxt;
-
/*
 * If LINK is set, we have dependent requests in this chain. If we
 * didn't fail this request, queue the first one up, moving any other
@@ -1105,9 +1104,7 @@ static inline struct io_kiocb *io_req_find_next(struct 
io_kiocb *req)
 */
if (unlikely(req->flags & IO_DISARM_MASK))
__io_req_find_next_prep(req);
-   nxt = req->link;
-   req->link = NULL;
-   return nxt;
+   return __xchg(>link, NULL);
 }
 
 static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
diff --git a/io_uring/slist.h b/io_uring/slist.h
index f27601fa46607b..d3a743a1adc626 100644
--- a/io_uring/slist.h
+++ b/io_uring/slist.h
@@ -2,6 +2,7 @@
 #define INTERNAL_IO_SLIST_H
 
 #include 
+#include 
 
 #define wq_list_for_each(pos, prv, head)   \
for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
@@ -121,10 +122,7 @@ static inline void wq_list_del(struct io_wq_work_list 
*list,
 static inline
 struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack)
 {
-   struct io_wq_work_node *node = stack->next;
-
-   stack->next = node->next;
-   return node;
+   return __xchg(>next, stack->next->next);
 }
 
 static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
-- 
2.34.1



[PATCH v5 4/7] llist: simplify __llist_del_all

2023-01-18 Thread Andrzej Hajda
llist_del_all uses xchg, let's use __xchg here.

Signed-off-by: Andrzej Hajda 
---
 include/linux/llist.h | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/include/linux/llist.h b/include/linux/llist.h
index 85bda2d02d65be..4dc1d185ea98ab 100644
--- a/include/linux/llist.h
+++ b/include/linux/llist.h
@@ -50,6 +50,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -241,10 +242,7 @@ static inline struct llist_node *llist_del_all(struct 
llist_head *head)
 
 static inline struct llist_node *__llist_del_all(struct llist_head *head)
 {
-   struct llist_node *first = head->first;
-
-   head->first = NULL;
-   return first;
+   return __xchg(>first, NULL);
 }
 
 extern struct llist_node *llist_del_first(struct llist_head *head);
-- 
2.34.1



[PATCH v5 3/7] arch/*/uprobes: simplify arch_uretprobe_hijack_return_addr

2023-01-18 Thread Andrzej Hajda
In all architectures, except x86, arch_uretprobe_hijack_return_addr
is just __xchg.

Signed-off-by: Andrzej Hajda 
---
 arch/arm/probes/uprobes/core.c |  8 ++--
 arch/arm64/kernel/probes/uprobes.c |  9 ++---
 arch/csky/kernel/probes/uprobes.c  |  9 ++---
 arch/mips/kernel/uprobes.c | 10 ++
 arch/powerpc/kernel/uprobes.c  | 10 ++
 arch/riscv/kernel/probes/uprobes.c |  9 ++---
 arch/s390/kernel/uprobes.c |  7 ++-
 arch/sparc/kernel/uprobes.c|  7 ++-
 8 files changed, 16 insertions(+), 53 deletions(-)

diff --git a/arch/arm/probes/uprobes/core.c b/arch/arm/probes/uprobes/core.c
index f5f790c6e5f896..77ce8ae431376d 100644
--- a/arch/arm/probes/uprobes/core.c
+++ b/arch/arm/probes/uprobes/core.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -61,12 +62,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
 {
-   unsigned long orig_ret_vaddr;
-
-   orig_ret_vaddr = regs->ARM_lr;
-   /* Replace the return addr with trampoline addr */
-   regs->ARM_lr = trampoline_vaddr;
-   return orig_ret_vaddr;
+   return __xchg(>ARM_lr, trampoline_vaddr);
 }
 
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
diff --git a/arch/arm64/kernel/probes/uprobes.c 
b/arch/arm64/kernel/probes/uprobes.c
index d49aef2657cdf7..d7171d30ea6681 100644
--- a/arch/arm64/kernel/probes/uprobes.c
+++ b/arch/arm64/kernel/probes/uprobes.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2014-2016 Pratyush Anand 
  */
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -150,13 +151,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
 {
-   unsigned long orig_ret_vaddr;
-
-   orig_ret_vaddr = procedure_link_pointer(regs);
-   /* Replace the return addr with trampoline addr */
-   procedure_link_pointer_set(regs, trampoline_vaddr);
-
-   return orig_ret_vaddr;
+   return __xchg(_link_pointer(regs), trampoline_vaddr);
 }
 
 int arch_uprobe_exception_notify(struct notifier_block *self,
diff --git a/arch/csky/kernel/probes/uprobes.c 
b/arch/csky/kernel/probes/uprobes.c
index 2d31a12e46cfee..775fe88b5f0016 100644
--- a/arch/csky/kernel/probes/uprobes.c
+++ b/arch/csky/kernel/probes/uprobes.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2014-2016 Pratyush Anand 
  */
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -123,13 +124,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
 {
-   unsigned long ra;
-
-   ra = regs->lr;
-
-   regs->lr = trampoline_vaddr;
-
-   return ra;
+   return __xchg(>lr, trampoline_vaddr);
 }
 
 int arch_uprobe_exception_notify(struct notifier_block *self,
diff --git a/arch/mips/kernel/uprobes.c b/arch/mips/kernel/uprobes.c
index 6c063aa188e626..2b1f375c407b87 100644
--- a/arch/mips/kernel/uprobes.c
+++ b/arch/mips/kernel/uprobes.c
@@ -2,6 +2,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -197,14 +198,7 @@ void arch_uprobe_abort_xol(struct arch_uprobe *aup,
 unsigned long arch_uretprobe_hijack_return_addr(
unsigned long trampoline_vaddr, struct pt_regs *regs)
 {
-   unsigned long ra;
-
-   ra = regs->regs[31];
-
-   /* Replace the return address with the trampoline address */
-   regs->regs[31] = trampoline_vaddr;
-
-   return ra;
+   return __xchg(>regs[31], trampoline_vaddr);
 }
 
 /**
diff --git a/arch/powerpc/kernel/uprobes.c b/arch/powerpc/kernel/uprobes.c
index 95a41ae9dfa755..3c15c320315e6c 100644
--- a/arch/powerpc/kernel/uprobes.c
+++ b/arch/powerpc/kernel/uprobes.c
@@ -7,6 +7,7 @@
  * Adapted from the x86 port by Ananth N Mavinakayanahalli 
  */
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -197,14 +198,7 @@ bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, 
struct pt_regs *regs)
 unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct 
pt_regs *regs)
 {
-   unsigned long orig_ret_vaddr;
-
-   orig_ret_vaddr = regs->link;
-
-   /* Replace the return addr with trampoline addr */
-   regs->link = trampoline_vaddr;
-
-   return orig_ret_vaddr;
+   return __xchg(>link, trampoline_vaddr);
 }
 
 bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
diff --git a/arch/riscv/kernel/probes/uprobes.c 
b/arch/riscv/kernel/probes/uprobes.c
index c976a21cd4bd5b..5c8415e216536d 100644
--- a/arch/riscv/kernel/probes/uprobes.c
+++ b/arch/riscv/kernel/probes/uprobes.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include 
+#include 
 #include 
 #include 
 
@@ -122,13 +123,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,

[PATCH v5 2/7] linux/include: add non-atomic version of xchg

2023-01-18 Thread Andrzej Hajda
The pattern of setting variable with new value and returning old
one is very common in kernel. Usually atomicity of the operation
is not required, so xchg seems to be suboptimal and confusing in
such cases.

Signed-off-by: Andrzej Hajda 
Reviewed-by: Andy Shevchenko 
---
 include/linux/non-atomic/xchg.h | 19 +++
 1 file changed, 19 insertions(+)
 create mode 100644 include/linux/non-atomic/xchg.h

diff --git a/include/linux/non-atomic/xchg.h b/include/linux/non-atomic/xchg.h
new file mode 100644
index 00..f7fa5dd746f37d
--- /dev/null
+++ b/include/linux/non-atomic/xchg.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_NON_ATOMIC_XCHG_H
+#define _LINUX_NON_ATOMIC_XCHG_H
+
+/**
+ * __xchg - set variable pointed by @ptr to @val, return old value
+ * @ptr: pointer to affected variable
+ * @val: value to be written
+ *
+ * This is non-atomic variant of xchg.
+ */
+#define __xchg(ptr, val) ({\
+   __auto_type __ptr = ptr;\
+   __auto_type __t = *__ptr;   \
+   *__ptr = (val); \
+   __t;\
+})
+
+#endif
-- 
2.34.1



[PATCH v5 1/7] arch: rename all internal names __xchg to __arch_xchg

2023-01-18 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
Reviewed-by: Arnd Bergmann 
Acked-by: Geert Uytterhoeven  [m68k]
Acked-by: Palmer Dabbelt  [riscv]
---
v2: squashed all arch patches into one
v3: fixed alpha/xchg_local, thx to l...@intel.com
v4: adjusted indentation (Heiko)
---
 arch/alpha/include/asm/cmpxchg.h | 10 +-
 arch/arc/include/asm/cmpxchg.h   |  4 ++--
 arch/arm/include/asm/cmpxchg.h   |  7 ---
 arch/arm64/include/asm/cmpxchg.h |  7 +++
 arch/hexagon/include/asm/cmpxchg.h   | 10 +-
 arch/ia64/include/asm/cmpxchg.h  |  2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h |  4 ++--
 arch/loongarch/include/asm/cmpxchg.h |  4 ++--
 arch/m68k/include/asm/cmpxchg.h  |  6 +++---
 arch/mips/include/asm/cmpxchg.h  |  4 ++--
 arch/openrisc/include/asm/cmpxchg.h  | 10 +-
 arch/parisc/include/asm/cmpxchg.h|  4 ++--
 arch/powerpc/include/asm/cmpxchg.h   |  4 ++--
 arch/riscv/include/asm/atomic.h  |  2 +-
 arch/riscv/include/asm/cmpxchg.h |  4 ++--
 arch/s390/include/asm/cmpxchg.h  |  8 
 arch/sh/include/asm/cmpxchg.h|  4 ++--
 arch/sparc/include/asm/cmpxchg_32.h  |  4 ++--
 arch/sparc/include/asm/cmpxchg_64.h  |  6 +++---
 arch/xtensa/include/asm/cmpxchg.h|  4 ++--
 20 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.h
index 6e0a850aa9d38c..91d4a4d9258cd2 100644
--- a/arch/alpha/include/asm/cmpxchg.h
+++ b/arch/alpha/include/asm/cmpxchg.h
@@ -6,15 +6,15 @@
  * Atomic exchange routines.
  */
 
-#define xchg(type, args...)__xchg ## type ## _local(args)
+#define xchg(type, args...)__arch_xchg ## type ## 
_local(args)
 #define cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
 #include 
 
 #define xchg_local(ptr, x) \
 ({ \
__typeof__(*(ptr)) _x_ = (x);   \
-   (__typeof__(*(ptr))) __xchg_local((ptr), (unsigned long)_x_,\
-  sizeof(*(ptr))); \
+   (__typeof__(*(ptr))) __arch_xchg_local((ptr), (unsigned long)_x_,\
+  sizeof(*(ptr))); \
 })
 
 #define arch_cmpxchg_local(ptr, o, n)  \
@@ -34,7 +34,7 @@
 
 #undef xchg
 #undef cmpxchg
-#define xchg(type, args...)__xchg ##type(args)
+#define xchg(type, args...)__arch_xchg ##type(args)
 #define cmpxchg(type, args...) __cmpxchg ##type(args)
 #include 
 
@@ -48,7 +48,7 @@
__typeof__(*(ptr)) _x_ = (x);   \
smp_mb();   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
smp_mb();   \
__ret;  \
 })
diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h
index c5b544a5fe8106..e138fde067dea5 100644
--- a/arch/arc/include/asm/cmpxchg.h
+++ b/arch/arc/include/asm/cmpxchg.h
@@ -85,7 +85,7 @@
  */
 #ifdef CONFIG_ARC_HAS_LLSC
 
-#define __xchg(ptr, val)   \
+#define __arch_xchg(ptr, val)  \
 ({ \
__asm__ __volatile__(   \
"   ex  %0, [%1]\n" /* set new value */ \
@@ -102,7 +102,7 @@
\
switch(sizeof(*(_p_))) {\
case 4: \
-   _val_ = __xchg(_p_, _val_); \
+   _val_ = __arch_xchg(_p_, _val_);\
break;  \
default:\
BUILD_BUG();\
diff --git a/arch/arm/include/asm/cmpxchg.h b/arch/arm/include/asm/cmpxchg.h
index 4dfe538dfc689b..44667bdb4707a9 100644
--- a/arch/arm/include/asm/cmpxchg.h
+++ b/arch/arm/include/asm/cmpxchg.h
@@ -25,7 +25,8 @@
 #define swp_is_buggy
 #endif
 
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int 
size)
+static inline unsigned long
+__arch_xchg(unsigned long x, volatile void *ptr, int size)
 {
extern void __bad_xchg(volatile void *, int);
uns

[PATCH v5 0/7] Introduce __xchg, non-atomic xchg

2023-01-18 Thread Andrzej Hajda
Hi all,

The helper is tiny and there are advices we can live without it, so
I want to present few arguments why it would be good to have it:

1. Code readability/simplification/number of lines:
  - decreases number of lines,
  - it often eliminates local variables,
  - for real examples see patches 3+.

2. Presence of similar helpers in other somehow related languages/libs:

a) Rust[1]: 'replace' from std::mem module, there is also 'take'
helper (__xchg(, 0)), which is the same as private helper in
i915 - fetch_and_zero, see latest patch.
b) C++ [2]: 'exchange' from utility header.

If the idea is OK there are still 2 questions to answer:

1. Name of the helper, __xchg follows kernel conventions,
but for me Rust names are also OK.
2. Where to put the helper:
a) as in this patchset include/linux/non-atomic/xchg.h,
proposed by Andy Shevchenko,
b) include/linux/utils.h ? any better name? Some kind
of container for simple helpers.

All __xchg conversions were performed using cocci script,
then manually adjusted if necessary.

There is lot of places it can be used in, I have just chosen
some of them. I can provide cocci script to detect others (not all),
if necessary.

Changes:
v2: squashed all __xchg -> __arch_xchg t one patch (Arnd)
v3: fixed alpha/xchg_local (l...@intel.com)
v4: adjusted indentation (Heiko)
v5: added more __xchg conversions - patches 3-6, added tags

[1]: https://doc.rust-lang.org/std/mem/index.html
[2]: https://en.cppreference.com/w/cpp/header/utility

Regards
Andrzej

Andrzej Hajda (7):
  arch: rename all internal names __xchg to __arch_xchg
  linux/include: add non-atomic version of xchg
  arch/*/uprobes: simplify arch_uretprobe_hijack_return_addr
  llist: simplify __llist_del_all
  io_uring: use __xchg if possible
  qed: use __xchg if possible
  drm/i915/gt: use __xchg instead of internal helper

 arch/alpha/include/asm/cmpxchg.h  | 10 +-
 arch/arc/include/asm/cmpxchg.h|  4 ++--
 arch/arm/include/asm/cmpxchg.h|  7 ---
 arch/arm/probes/uprobes/core.c|  8 ++--
 arch/arm64/include/asm/cmpxchg.h  |  7 +++
 arch/arm64/kernel/probes/uprobes.c|  9 ++---
 arch/csky/kernel/probes/uprobes.c |  9 ++---
 arch/hexagon/include/asm/cmpxchg.h| 10 +-
 arch/ia64/include/asm/cmpxchg.h   |  2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h  |  4 ++--
 arch/loongarch/include/asm/cmpxchg.h  |  4 ++--
 arch/m68k/include/asm/cmpxchg.h   |  6 +++---
 arch/mips/include/asm/cmpxchg.h   |  4 ++--
 arch/mips/kernel/uprobes.c| 10 ++
 arch/openrisc/include/asm/cmpxchg.h   | 10 +-
 arch/parisc/include/asm/cmpxchg.h |  4 ++--
 arch/powerpc/include/asm/cmpxchg.h|  4 ++--
 arch/powerpc/kernel/uprobes.c | 10 ++
 arch/riscv/include/asm/atomic.h   |  2 +-
 arch/riscv/include/asm/cmpxchg.h  |  4 ++--
 arch/riscv/kernel/probes/uprobes.c|  9 ++---
 arch/s390/include/asm/cmpxchg.h   |  8 
 arch/s390/kernel/uprobes.c|  7 ++-
 arch/sh/include/asm/cmpxchg.h |  4 ++--
 arch/sparc/include/asm/cmpxchg_32.h   |  4 ++--
 arch/sparc/include/asm/cmpxchg_64.h   |  6 +++---
 arch/sparc/kernel/uprobes.c   |  7 ++-
 arch/xtensa/include/asm/cmpxchg.h |  4 ++--
 drivers/gpu/drm/i915/gt/intel_engine_cs.c |  2 +-
 .../gpu/drm/i915/gt/intel_engine_heartbeat.c  |  4 ++--
 .../drm/i915/gt/intel_execlists_submission.c  |  4 ++--
 drivers/gpu/drm/i915/gt/intel_ggtt.c  |  4 ++--
 drivers/gpu/drm/i915/gt/intel_gsc.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_gt.c|  4 ++--
 drivers/gpu/drm/i915/gt/intel_gt_pm.c |  2 +-
 drivers/gpu/drm/i915/gt/intel_lrc.c   |  6 +++---
 drivers/gpu/drm/i915/gt/intel_migrate.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_rc6.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_rps.c   |  2 +-
 drivers/gpu/drm/i915/gt/selftest_context.c|  2 +-
 .../drm/i915/gt/selftest_ring_submission.c|  2 +-
 drivers/gpu/drm/i915/gt/selftest_timeline.c   |  2 +-
 drivers/gpu/drm/i915/gt/uc/intel_gsc_uc.c |  2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.c |  2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c  |  2 +-
 drivers/gpu/drm/i915/i915_utils.h |  1 +
 include/linux/llist.h |  6 ++
 include/linux/non-atomic/xchg.h   | 19 +++
 include/linux/qed/qed_chain.h | 19 +++
 io_uring/io_uring.c   |  7 ++-
 io_uring/slist.h  |  6 ++
 51 files changed, 126 insertions(+), 155 deletions(-)
 create mode 100644 include/linux/non-atomic/xchg.h

-- 
2.34.1



Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places

2023-01-10 Thread Andrzej Hajda

On 10.01.2023 12:07, Andy Shevchenko wrote:

On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote:

This patch tries to show usability of __xchg helper.
It is not intended to be merged, but I can convert
it to proper patchset if necessary.

There are many more places where __xchg can be used.
This demo shows the most spectacular cases IMHO:
- previous value is returned from function,
- temporary variables are in use.

As a result readability is much better and diffstat is quite
nice, less local vars to look at.
In many cases whole body of functions is replaced
with __xchg(ptr, val), so as further refactoring the whole
function can be removed and __xchg can be called directly.


...


  arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
  {
-   unsigned long orig_ret_vaddr;
-
-   orig_ret_vaddr = regs->ARM_lr;
-   /* Replace the return addr with trampoline addr */
-   regs->ARM_lr = trampoline_vaddr;
-   return orig_ret_vaddr;
+   return __xchg(>ARM_lr, trampoline_vaddr);
  }


If it's not a callback, the entire function can be killed.
And this is a good example of the function usage.
OTOH, these places might have a side effect (if it's in deep CPU
handlers), means we need to do this carefully.

...


  static inline void *qed_chain_produce(struct qed_chain *p_chain)
  {
-   void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx;
+   void *p_prod_idx, *p_prod_page_idx;
  
  	if (is_chain_u16(p_chain)) {

if ((p_chain->u.chain16.prod_idx &
@@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain 
*p_chain)
p_chain->u.chain32.prod_idx++;
}
  
-	p_ret = p_chain->p_prod_elem;

-   p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
-   p_chain->elem_size);
-
-   return p_ret;
+   return __xchg(_chain->p_prod_elem,
+ (void *)(((u8 *)p_chain->p_prod_elem) + 
p_chain->elem_size));


Wondering if you still need a (void *) casting after the change. Ditto for the
rest of similar cases.


IMHO it is not needed also before the change and IIRC gcc has an 
extension which allows to drop (u8 *) cast as well [1].


[1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html




  }


...

Btw, is it done by coccinelle? If no, why not providing the script?



Yes I have used cocci. My cocci skills are far from perfect, so I did 
not want to share my dirty code, but this is nothing secret:


@r1@
expression x, v;
local idexpression p;
@@
-   p = x;
-   x = v;
-   return p;
+   return __xchg(, v);

@depends on r1@
expression e;
@@
__xchg(
-   &*e,
+   e,
...)

@depends on r1@
expression t;
@@
-   if (t) {
+   if (t)
return __xchg(...);
-   }

@depends on r1@
type t;
identifier p;
expression e;
@@
(
-   t p;
|
-   t p = e;
)
... when != p

Regards
Andrzej



[RFC DO NOT MERGE] treewide: use __xchg in most obvious places

2023-01-10 Thread Andrzej Hajda
This patch tries to show usability of __xchg helper.
It is not intended to be merged, but I can convert
it to proper patchset if necessary.

There are many more places where __xchg can be used.
This demo shows the most spectacular cases IMHO:
- previous value is returned from function,
- temporary variables are in use.

As a result readability is much better and diffstat is quite
nice, less local vars to look at.
In many cases whole body of functions is replaced
with __xchg(ptr, val), so as further refactoring the whole
function can be removed and __xchg can be called directly.

Signed-off-by: Andrzej Hajda 
---
 arch/arm/probes/uprobes/core.c|  8 ++--
 arch/csky/kernel/probes/uprobes.c |  9 ++---
 arch/mips/kernel/irq_txx9.c   |  7 ++-
 arch/mips/kernel/process.c|  8 +++-
 arch/mips/kernel/uprobes.c| 10 ++
 arch/powerpc/include/asm/kvm_ppc.h|  7 ++-
 arch/powerpc/kernel/uprobes.c | 10 ++
 arch/powerpc/mm/init_64.c |  7 ++-
 arch/riscv/kernel/probes/uprobes.c|  9 ++---
 arch/s390/kernel/uprobes.c|  7 ++-
 arch/s390/kvm/interrupt.c |  6 ++
 arch/sh/kernel/traps_32.c |  6 ++
 .../accessibility/speakup/speakup_dectlk.c|  7 ++-
 drivers/accessibility/speakup/speakup_soft.c  |  7 ++-
 drivers/block/drbd/drbd_receiver.c|  5 ++---
 drivers/cdrom/cdrom.c |  7 ++-
 drivers/gpu/drm/drm_atomic_uapi.c | 14 +++---
 drivers/iommu/iova.c  |  7 ++-
 drivers/misc/ti-st/st_core.c  | 10 +++---
 drivers/mtd/nand/raw/qcom_nandc.c | 11 ---
 drivers/net/ethernet/ibm/ehea/ehea_main.c | 11 +++
 .../microchip/sparx5/sparx5_calendar.c| 10 --
 drivers/net/usb/rtl8150.c |  9 +++--
 drivers/net/wireless/intel/iwlwifi/pcie/rx.c  |  8 +++-
 .../net/wireless/marvell/mwifiex/sta_ioctl.c  |  7 +++
 drivers/scsi/device_handler/scsi_dh_alua.c|  8 +++-
 drivers/scsi/lpfc/lpfc_sli.c  |  7 ++-
 drivers/staging/rtl8192e/rtllib_tx.c  |  7 ++-
 drivers/tty/hvc/hvc_iucv.c|  8 +++-
 drivers/video/fbdev/sis/sis_main.c|  6 ++
 drivers/xen/grant-table.c |  6 ++
 fs/namespace.c|  6 ++
 include/linux/ptr_ring.h  |  7 ++-
 include/linux/qed/qed_chain.h | 19 +++
 io_uring/io_uring.c   |  7 ++-
 mm/kmsan/init.c   |  7 ++-
 mm/memcontrol.c   |  8 ++--
 net/mac80211/rc80211_minstrel_ht.c|  6 ++
 sound/pci/asihpi/hpidebug.c   |  8 +++-
 .../selftests/bpf/progs/dummy_st_ops.c|  7 ++-
 40 files changed, 99 insertions(+), 225 deletions(-)

diff --git a/arch/arm/probes/uprobes/core.c b/arch/arm/probes/uprobes/core.c
index f5f790c6e5f896..77ce8ae431376d 100644
--- a/arch/arm/probes/uprobes/core.c
+++ b/arch/arm/probes/uprobes/core.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -61,12 +62,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
 {
-   unsigned long orig_ret_vaddr;
-
-   orig_ret_vaddr = regs->ARM_lr;
-   /* Replace the return addr with trampoline addr */
-   regs->ARM_lr = trampoline_vaddr;
-   return orig_ret_vaddr;
+   return __xchg(>ARM_lr, trampoline_vaddr);
 }
 
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
diff --git a/arch/csky/kernel/probes/uprobes.c 
b/arch/csky/kernel/probes/uprobes.c
index 2d31a12e46cfee..775fe88b5f0016 100644
--- a/arch/csky/kernel/probes/uprobes.c
+++ b/arch/csky/kernel/probes/uprobes.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2014-2016 Pratyush Anand 
  */
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -123,13 +124,7 @@ unsigned long
 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  struct pt_regs *regs)
 {
-   unsigned long ra;
-
-   ra = regs->lr;
-
-   regs->lr = trampoline_vaddr;
-
-   return ra;
+   return __xchg(>lr, trampoline_vaddr);
 }
 
 int arch_uprobe_exception_notify(struct notifier_block *self,
diff --git a/arch/mips/kernel/irq_txx9.c b/arch/mips/kernel/irq_txx9.c
index af3ef4c9f7de1e..b5abe24ea7cfb9 100644
--- a/arch/mips/kernel/irq_txx9.c
+++ b/arch/mips/kernel/irq_txx9.c
@@ -15,6 +15,7 @@
  */
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -159,13 +160,9 @@ void __init txx9_irq_init(unsigned long baseaddr)
 
 int __init tx

[PATCH v4] arch: rename all internal names __xchg to __arch_xchg

2023-01-05 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
Reviewed-by: Arnd Bergmann 
---
v2: squashed all arch patches into one
v3: fixed alpha/xchg_local, thx to l...@intel.com
v4: adjusted indentation (Heiko)
---
 arch/alpha/include/asm/cmpxchg.h | 10 +-
 arch/arc/include/asm/cmpxchg.h   |  4 ++--
 arch/arm/include/asm/cmpxchg.h   |  7 ---
 arch/arm64/include/asm/cmpxchg.h |  7 +++
 arch/hexagon/include/asm/cmpxchg.h   | 10 +-
 arch/ia64/include/asm/cmpxchg.h  |  2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h |  4 ++--
 arch/loongarch/include/asm/cmpxchg.h |  4 ++--
 arch/m68k/include/asm/cmpxchg.h  |  6 +++---
 arch/mips/include/asm/cmpxchg.h  |  4 ++--
 arch/openrisc/include/asm/cmpxchg.h  | 10 +-
 arch/parisc/include/asm/cmpxchg.h|  4 ++--
 arch/powerpc/include/asm/cmpxchg.h   |  4 ++--
 arch/riscv/include/asm/atomic.h  |  2 +-
 arch/riscv/include/asm/cmpxchg.h |  4 ++--
 arch/s390/include/asm/cmpxchg.h  |  8 
 arch/sh/include/asm/cmpxchg.h|  4 ++--
 arch/sparc/include/asm/cmpxchg_32.h  |  4 ++--
 arch/sparc/include/asm/cmpxchg_64.h  |  6 +++---
 arch/xtensa/include/asm/cmpxchg.h|  4 ++--
 20 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.h
index 6e0a850aa9d38c..91d4a4d9258cd2 100644
--- a/arch/alpha/include/asm/cmpxchg.h
+++ b/arch/alpha/include/asm/cmpxchg.h
@@ -6,15 +6,15 @@
  * Atomic exchange routines.
  */
 
-#define xchg(type, args...)__xchg ## type ## _local(args)
+#define xchg(type, args...)__arch_xchg ## type ## 
_local(args)
 #define cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
 #include 
 
 #define xchg_local(ptr, x) \
 ({ \
__typeof__(*(ptr)) _x_ = (x);   \
-   (__typeof__(*(ptr))) __xchg_local((ptr), (unsigned long)_x_,\
-  sizeof(*(ptr))); \
+   (__typeof__(*(ptr))) __arch_xchg_local((ptr), (unsigned long)_x_,\
+  sizeof(*(ptr))); \
 })
 
 #define arch_cmpxchg_local(ptr, o, n)  \
@@ -34,7 +34,7 @@
 
 #undef xchg
 #undef cmpxchg
-#define xchg(type, args...)__xchg ##type(args)
+#define xchg(type, args...)__arch_xchg ##type(args)
 #define cmpxchg(type, args...) __cmpxchg ##type(args)
 #include 
 
@@ -48,7 +48,7 @@
__typeof__(*(ptr)) _x_ = (x);   \
smp_mb();   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
smp_mb();   \
__ret;  \
 })
diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h
index c5b544a5fe8106..e138fde067dea5 100644
--- a/arch/arc/include/asm/cmpxchg.h
+++ b/arch/arc/include/asm/cmpxchg.h
@@ -85,7 +85,7 @@
  */
 #ifdef CONFIG_ARC_HAS_LLSC
 
-#define __xchg(ptr, val)   \
+#define __arch_xchg(ptr, val)  \
 ({ \
__asm__ __volatile__(   \
"   ex  %0, [%1]\n" /* set new value */ \
@@ -102,7 +102,7 @@
\
switch(sizeof(*(_p_))) {\
case 4: \
-   _val_ = __xchg(_p_, _val_); \
+   _val_ = __arch_xchg(_p_, _val_);\
break;  \
default:\
BUILD_BUG();\
diff --git a/arch/arm/include/asm/cmpxchg.h b/arch/arm/include/asm/cmpxchg.h
index 4dfe538dfc689b..44667bdb4707a9 100644
--- a/arch/arm/include/asm/cmpxchg.h
+++ b/arch/arm/include/asm/cmpxchg.h
@@ -25,7 +25,8 @@
 #define swp_is_buggy
 #endif
 
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int 
size)
+static inline unsigned long
+__arch_xchg(unsigned long x, volatile void *ptr, int size)
 {
extern void __bad_xchg(volatile void *, int);
unsigned long ret;
@@ -115,8 +116,8 @@ static inline unsigned long __xch

[PATCH v3] arch: rename all internal names __xchg to __arch_xchg

2022-12-30 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
Reviewed-by: Arnd Bergmann 
---
v2: squashed all arch patches into one
v3: fixed alpha/xchg_local, thx to l...@intel.com
---
 arch/alpha/include/asm/cmpxchg.h | 8 
 arch/arc/include/asm/cmpxchg.h   | 4 ++--
 arch/arm/include/asm/cmpxchg.h   | 4 ++--
 arch/arm64/include/asm/cmpxchg.h | 4 ++--
 arch/hexagon/include/asm/cmpxchg.h   | 6 +++---
 arch/ia64/include/asm/cmpxchg.h  | 2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h | 4 ++--
 arch/loongarch/include/asm/cmpxchg.h | 4 ++--
 arch/m68k/include/asm/cmpxchg.h  | 6 +++---
 arch/mips/include/asm/cmpxchg.h  | 4 ++--
 arch/openrisc/include/asm/cmpxchg.h  | 4 ++--
 arch/parisc/include/asm/cmpxchg.h| 4 ++--
 arch/powerpc/include/asm/cmpxchg.h   | 4 ++--
 arch/riscv/include/asm/atomic.h  | 2 +-
 arch/riscv/include/asm/cmpxchg.h | 4 ++--
 arch/s390/include/asm/cmpxchg.h  | 4 ++--
 arch/sh/include/asm/cmpxchg.h| 4 ++--
 arch/sparc/include/asm/cmpxchg_32.h  | 4 ++--
 arch/sparc/include/asm/cmpxchg_64.h  | 4 ++--
 arch/xtensa/include/asm/cmpxchg.h| 4 ++--
 20 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.h
index 6e0a850aa9d38c..e0cb5b5c1c54b9 100644
--- a/arch/alpha/include/asm/cmpxchg.h
+++ b/arch/alpha/include/asm/cmpxchg.h
@@ -6,14 +6,14 @@
  * Atomic exchange routines.
  */
 
-#define xchg(type, args...)__xchg ## type ## _local(args)
+#define xchg(type, args...)__arch_xchg ## type ## 
_local(args)
 #define cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
 #include 
 
 #define xchg_local(ptr, x) \
 ({ \
__typeof__(*(ptr)) _x_ = (x);   \
-   (__typeof__(*(ptr))) __xchg_local((ptr), (unsigned long)_x_,\
+   (__typeof__(*(ptr))) __arch_xchg_local((ptr), (unsigned long)_x_,\
   sizeof(*(ptr))); \
 })
 
@@ -34,7 +34,7 @@
 
 #undef xchg
 #undef cmpxchg
-#define xchg(type, args...)__xchg ##type(args)
+#define xchg(type, args...)__arch_xchg ##type(args)
 #define cmpxchg(type, args...) __cmpxchg ##type(args)
 #include 
 
@@ -48,7 +48,7 @@
__typeof__(*(ptr)) _x_ = (x);   \
smp_mb();   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
smp_mb();   \
__ret;  \
 })
diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h
index c5b544a5fe8106..e138fde067dea5 100644
--- a/arch/arc/include/asm/cmpxchg.h
+++ b/arch/arc/include/asm/cmpxchg.h
@@ -85,7 +85,7 @@
  */
 #ifdef CONFIG_ARC_HAS_LLSC
 
-#define __xchg(ptr, val)   \
+#define __arch_xchg(ptr, val)  \
 ({ \
__asm__ __volatile__(   \
"   ex  %0, [%1]\n" /* set new value */ \
@@ -102,7 +102,7 @@
\
switch(sizeof(*(_p_))) {\
case 4: \
-   _val_ = __xchg(_p_, _val_); \
+   _val_ = __arch_xchg(_p_, _val_);\
break;  \
default:\
BUILD_BUG();\
diff --git a/arch/arm/include/asm/cmpxchg.h b/arch/arm/include/asm/cmpxchg.h
index 4dfe538dfc689b..6953fc05a97886 100644
--- a/arch/arm/include/asm/cmpxchg.h
+++ b/arch/arm/include/asm/cmpxchg.h
@@ -25,7 +25,7 @@
 #define swp_is_buggy
 #endif
 
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int 
size)
+static inline unsigned long __arch_xchg(unsigned long x, volatile void *ptr, 
int size)
 {
extern void __bad_xchg(volatile void *, int);
unsigned long ret;
@@ -115,7 +115,7 @@ static inline unsigned long __xchg(unsigned long x, 
volatile void *ptr, int size
 }
 
 #define arch_xchg_relaxed(ptr, x) ({   \
-   (__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr),   \
+   (__type

[PATCH v2] arch: rename all internal names __xchg to __arch_xchg

2022-12-29 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
Reviewed-by: Arnd Bergmann 
---
 arch/alpha/include/asm/cmpxchg.h | 6 +++---
 arch/arc/include/asm/cmpxchg.h   | 4 ++--
 arch/arm/include/asm/cmpxchg.h   | 4 ++--
 arch/arm64/include/asm/cmpxchg.h | 4 ++--
 arch/hexagon/include/asm/cmpxchg.h   | 6 +++---
 arch/ia64/include/asm/cmpxchg.h  | 2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h | 4 ++--
 arch/loongarch/include/asm/cmpxchg.h | 4 ++--
 arch/m68k/include/asm/cmpxchg.h  | 6 +++---
 arch/mips/include/asm/cmpxchg.h  | 4 ++--
 arch/openrisc/include/asm/cmpxchg.h  | 4 ++--
 arch/parisc/include/asm/cmpxchg.h| 4 ++--
 arch/powerpc/include/asm/cmpxchg.h   | 4 ++--
 arch/riscv/include/asm/atomic.h  | 2 +-
 arch/riscv/include/asm/cmpxchg.h | 4 ++--
 arch/s390/include/asm/cmpxchg.h  | 4 ++--
 arch/sh/include/asm/cmpxchg.h| 4 ++--
 arch/sparc/include/asm/cmpxchg_32.h  | 4 ++--
 arch/sparc/include/asm/cmpxchg_64.h  | 4 ++--
 arch/xtensa/include/asm/cmpxchg.h| 4 ++--
 20 files changed, 41 insertions(+), 41 deletions(-)

diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.h
index 6e0a850aa9d38c..40e8159ef6e794 100644
--- a/arch/alpha/include/asm/cmpxchg.h
+++ b/arch/alpha/include/asm/cmpxchg.h
@@ -6,7 +6,7 @@
  * Atomic exchange routines.
  */
 
-#define xchg(type, args...)__xchg ## type ## _local(args)
+#define xchg(type, args...)__arch_xchg ## type ## 
_local(args)
 #define cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
 #include 
 
@@ -34,7 +34,7 @@
 
 #undef xchg
 #undef cmpxchg
-#define xchg(type, args...)__xchg ##type(args)
+#define xchg(type, args...)__arch_xchg ##type(args)
 #define cmpxchg(type, args...) __cmpxchg ##type(args)
 #include 
 
@@ -48,7 +48,7 @@
__typeof__(*(ptr)) _x_ = (x);   \
smp_mb();   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
smp_mb();   \
__ret;  \
 })
diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h
index c5b544a5fe8106..e138fde067dea5 100644
--- a/arch/arc/include/asm/cmpxchg.h
+++ b/arch/arc/include/asm/cmpxchg.h
@@ -85,7 +85,7 @@
  */
 #ifdef CONFIG_ARC_HAS_LLSC
 
-#define __xchg(ptr, val)   \
+#define __arch_xchg(ptr, val)  \
 ({ \
__asm__ __volatile__(   \
"   ex  %0, [%1]\n" /* set new value */ \
@@ -102,7 +102,7 @@
\
switch(sizeof(*(_p_))) {\
case 4: \
-   _val_ = __xchg(_p_, _val_); \
+   _val_ = __arch_xchg(_p_, _val_);\
break;  \
default:\
BUILD_BUG();\
diff --git a/arch/arm/include/asm/cmpxchg.h b/arch/arm/include/asm/cmpxchg.h
index 4dfe538dfc689b..6953fc05a97886 100644
--- a/arch/arm/include/asm/cmpxchg.h
+++ b/arch/arm/include/asm/cmpxchg.h
@@ -25,7 +25,7 @@
 #define swp_is_buggy
 #endif
 
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int 
size)
+static inline unsigned long __arch_xchg(unsigned long x, volatile void *ptr, 
int size)
 {
extern void __bad_xchg(volatile void *, int);
unsigned long ret;
@@ -115,7 +115,7 @@ static inline unsigned long __xchg(unsigned long x, 
volatile void *ptr, int size
 }
 
 #define arch_xchg_relaxed(ptr, x) ({   \
-   (__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr),   \
+   (__typeof__(*(ptr)))__arch_xchg((unsigned long)(x), (ptr),  
\
   sizeof(*(ptr))); \
 })
 
diff --git a/arch/arm64/include/asm/cmpxchg.h b/arch/arm64/include/asm/cmpxchg.h
index 497acf134d9923..3a36ba58e8c2ef 100644
--- a/arch/arm64/include/asm/cmpxchg.h
+++ b/arch/arm64/include/asm/cmpxchg.h
@@ -62,7 +62,7 @@ __XCHG_CASE( ,  ,  mb_, 64, dmb ish, nop,  , a, l, "memory")
 #undef __XCHG_CASE
 
 #define __XCHG_GEN(sfx)
\
-static __alw

Re: [PATCH 00/19] Introduce __xchg, non-atomic xchg

2022-12-29 Thread Andrzej Hajda

Forgive me late response - Holidays,

On 22.12.2022 18:21, Andrew Morton wrote:

On Thu, 22 Dec 2022 12:46:16 +0100 Andrzej Hajda  
wrote:


Hi all,

I hope there will be place for such tiny helper in kernel.
Quick cocci analyze shows there is probably few thousands places
where it could be useful.

So to clarify, the intent here is a simple readability cleanup for
existing open-coded exchange operations.


And replace private helpers with common one, see the last patch - the 
ultimate goal

would be to replace all occurrences of fetch_and_zero with __xchg.


The intent is *not* to
identify existing xchg() sites which are unnecessarily atomic and to
optimize them by using the non-atomic version.

Have you considered the latter?


If you mean some way of (semi-)automatic detection of such cases, then 
no. Anyway this could be quite interesting challenge.





I am not sure who is good person to review/ack such patches,

I can take 'em.


so I've used my intuition to construct to/cc lists, sorry for mistakes.
This is the 2nd approach of the same idea, with comments addressed[0].

The helper is tiny and there are advices we can leave without it, so
I want to present few arguments why it would be good to have it:

1. Code readability/simplification/number of lines:

Real example from drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c:
-   previous_min_rate = evport->qos.min_rate;
-   evport->qos.min_rate = min_rate;
+   previous_min_rate = __xchg(evport->qos.min_rate, min_rate);

For sure the code is more compact, and IMHO more readable.

2. Presence of similar helpers in other somehow related languages/libs:

a) Rust[1]: 'replace' from std::mem module, there is also 'take'
 helper (__xchg(, 0)), which is the same as private helper in
 i915 - fetch_and_zero, see latest patch.
b) C++ [2]: 'exchange' from utility header.

If the idea is OK there are still 2 qestions to answer:

1. Name of the helper, __xchg follows kernel conventions,
 but for me Rust names are also OK.

I like replace(), or, shockingly, exchange().

But...   Can we simply make swap() return the previous value?

previous_min_rate = swap(>qos.min_rate, min_rate);


As Alexander already pointed out, swap requires 'references' to two 
variables,

in contrast to xchg which requires reference to variable and value.
So we cannot use swap for cases:
    old_value = __xchg(, new_value);

Regards
Andrzej



Re: [PATCH 00/19] Introduce __xchg, non-atomic xchg

2022-12-22 Thread Andrzej Hajda




On 22.12.2022 15:12, Geert Uytterhoeven wrote:

Hi Andrzej,

Thanks for your series!

On Thu, Dec 22, 2022 at 12:49 PM Andrzej Hajda  wrote:

I hope there will be place for such tiny helper in kernel.
Quick cocci analyze shows there is probably few thousands places
where it could be useful.
I am not sure who is good person to review/ack such patches,
so I've used my intuition to construct to/cc lists, sorry for mistakes.
This is the 2nd approach of the same idea, with comments addressed[0].

The helper is tiny and there are advices we can leave without it, so
I want to present few arguments why it would be good to have it:

1. Code readability/simplification/number of lines:

Real example from drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c:
-   previous_min_rate = evport->qos.min_rate;
-   evport->qos.min_rate = min_rate;
+   previous_min_rate = __xchg(evport->qos.min_rate, min_rate);

Upon closer look, shouldn't that be

 previous_min_rate = __xchg(>qos.min_rate, min_rate);

?


Yes, you are right, the first argument is a pointer.

Regards
Andrzej




For sure the code is more compact, and IMHO more readable.

2. Presence of similar helpers in other somehow related languages/libs:

a) Rust[1]: 'replace' from std::mem module, there is also 'take'
 helper (__xchg(, 0)), which is the same as private helper in
 i915 - fetch_and_zero, see latest patch.
b) C++ [2]: 'exchange' from utility header.

If the idea is OK there are still 2 qestions to answer:

1. Name of the helper, __xchg follows kernel conventions,
 but for me Rust names are also OK.

Before I realized the missing "&", I wondered how this is different
from swap(), so naming is important.
https://elixir.bootlin.com/linux/latest/source/include/linux/minmax.h#L139

Gr{oetje,eeting}s,

 Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ge...@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
 -- Linus Torvalds




[PATCH 19/19] drm/i915/gt: use __xchg instead of internal helper

2022-12-22 Thread Andrzej Hajda
Prefer core helper if available.

Signed-off-by: Andrzej Hajda 
---
 drivers/gpu/drm/i915/gt/intel_engine_cs.c| 2 +-
 drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_execlists_submission.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_ggtt.c | 4 ++--
 drivers/gpu/drm/i915/gt/intel_gsc.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_gt.c   | 4 ++--
 drivers/gpu/drm/i915/gt/intel_gt_pm.c| 2 +-
 drivers/gpu/drm/i915/gt/intel_lrc.c  | 6 +++---
 drivers/gpu/drm/i915/gt/intel_migrate.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_rc6.c  | 2 +-
 drivers/gpu/drm/i915/gt/intel_rps.c  | 2 +-
 drivers/gpu/drm/i915/gt/selftest_context.c   | 2 +-
 drivers/gpu/drm/i915/gt/selftest_ring_submission.c   | 2 +-
 drivers/gpu/drm/i915/gt/selftest_timeline.c  | 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_gsc_uc.c| 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.c| 2 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc_fw.c | 2 +-
 drivers/gpu/drm/i915/i915_utils.h| 1 +
 18 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/drivers/gpu/drm/i915/gt/intel_engine_cs.c 
b/drivers/gpu/drm/i915/gt/intel_engine_cs.c
index 99c4b866adddfa..aa548bd0b83687 100644
--- a/drivers/gpu/drm/i915/gt/intel_engine_cs.c
+++ b/drivers/gpu/drm/i915/gt/intel_engine_cs.c
@@ -1042,7 +1042,7 @@ static void cleanup_status_page(struct intel_engine_cs 
*engine)
/* Prevent writes into HWSP after returning the page to the system */
intel_engine_set_hwsp_writemask(engine, ~0u);
 
-   vma = fetch_and_zero(>status_page.vma);
+   vma = __xchg(>status_page.vma, 0);
if (!vma)
return;
 
diff --git a/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c 
b/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
index 9a527e1f5be655..09befcc6a84fa1 100644
--- a/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
+++ b/drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c
@@ -229,7 +229,7 @@ static void heartbeat(struct work_struct *wrk)
mutex_unlock(>timeline->mutex);
 out:
if (!engine->i915->params.enable_hangcheck || !next_heartbeat(engine))
-   i915_request_put(fetch_and_zero(>heartbeat.systole));
+   i915_request_put(__xchg(>heartbeat.systole, 0));
intel_engine_pm_put(engine);
 }
 
@@ -244,7 +244,7 @@ void intel_engine_unpark_heartbeat(struct intel_engine_cs 
*engine)
 void intel_engine_park_heartbeat(struct intel_engine_cs *engine)
 {
if (cancel_delayed_work(>heartbeat.work))
-   i915_request_put(fetch_and_zero(>heartbeat.systole));
+   i915_request_put(__xchg(>heartbeat.systole, 0));
 }
 
 void intel_gt_unpark_heartbeats(struct intel_gt *gt)
diff --git a/drivers/gpu/drm/i915/gt/intel_execlists_submission.c 
b/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
index 49a8f10d76c77b..692f18e5a10ef3 100644
--- a/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
+++ b/drivers/gpu/drm/i915/gt/intel_execlists_submission.c
@@ -3197,7 +3197,7 @@ static void execlists_reset_cancel(struct intel_engine_cs 
*engine)
RB_CLEAR_NODE(rb);
 
spin_lock(>base.sched_engine->lock);
-   rq = fetch_and_zero(>request);
+   rq = __xchg(>request, NULL);
if (rq) {
if (i915_request_mark_eio(rq)) {
rq->engine = engine;
@@ -3602,7 +3602,7 @@ static void rcu_virtual_context_destroy(struct 
work_struct *wrk)
 
spin_lock_irq(>base.sched_engine->lock);
 
-   old = fetch_and_zero(>request);
+   old = __xchg(>request, NULL);
if (old) {
GEM_BUG_ON(!__i915_request_is_complete(old));
__i915_request_submit(old);
diff --git a/drivers/gpu/drm/i915/gt/intel_ggtt.c 
b/drivers/gpu/drm/i915/gt/intel_ggtt.c
index 0c7fe360f87331..81317187d0c5c6 100644
--- a/drivers/gpu/drm/i915/gt/intel_ggtt.c
+++ b/drivers/gpu/drm/i915/gt/intel_ggtt.c
@@ -684,7 +684,7 @@ static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
 {
struct i915_ppgtt *ppgtt;
 
-   ppgtt = fetch_and_zero(>alias);
+   ppgtt = __xchg(>alias, NULL);
if (!ppgtt)
return;
 
@@ -1238,7 +1238,7 @@ bool i915_ggtt_resume_vm(struct i915_address_space *vm)
   was_bound);
 
if (obj) { /* only used during resume => exclusive access */
-   write_domain_objs |= fetch_and_zero(>write_domain);
+   write_domain_objs |= __xchg(>write_domain, 0);
obj->read_domains |= I915_GEM_DOMAIN_GTT;
}
}
diff --git a/drivers/gpu/drm/i915/gt/intel_gsc.c 
b/drivers/gpu/drm

[PATCH 18/19] linux/include: add non-atomic version of xchg

2022-12-22 Thread Andrzej Hajda
The pattern of setting variable with new value and returning old
one is very common in kernel. Usually atomicity of the operation
is not required, so xchg seems to be suboptimal and confusing in
such cases.

Signed-off-by: Andrzej Hajda 
---
 include/linux/non-atomic/xchg.h | 19 +++
 1 file changed, 19 insertions(+)
 create mode 100644 include/linux/non-atomic/xchg.h

diff --git a/include/linux/non-atomic/xchg.h b/include/linux/non-atomic/xchg.h
new file mode 100644
index 00..f7fa5dd746f37d
--- /dev/null
+++ b/include/linux/non-atomic/xchg.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_NON_ATOMIC_XCHG_H
+#define _LINUX_NON_ATOMIC_XCHG_H
+
+/**
+ * __xchg - set variable pointed by @ptr to @val, return old value
+ * @ptr: pointer to affected variable
+ * @val: value to be written
+ *
+ * This is non-atomic variant of xchg.
+ */
+#define __xchg(ptr, val) ({\
+   __auto_type __ptr = ptr;\
+   __auto_type __t = *__ptr;   \
+   *__ptr = (val); \
+   __t;\
+})
+
+#endif
-- 
2.34.1



[PATCH 17/19] arch/xtensa: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/xtensa/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/xtensa/include/asm/cmpxchg.h 
b/arch/xtensa/include/asm/cmpxchg.h
index eb87810357ad88..675a11ea8de76b 100644
--- a/arch/xtensa/include/asm/cmpxchg.h
+++ b/arch/xtensa/include/asm/cmpxchg.h
@@ -170,7 +170,7 @@ static inline unsigned long xchg_u32(volatile int * m, 
unsigned long val)
 }
 
 #define arch_xchg(ptr,x) \
-   ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr
+   ((__typeof__(*(ptr)))__arch_xchg((unsigned 
long)(x),(ptr),sizeof(*(ptr
 
 static inline u32 xchg_small(volatile void *ptr, u32 x, int size)
 {
@@ -203,7 +203,7 @@ static inline u32 xchg_small(volatile void *ptr, u32 x, int 
size)
 extern void __xchg_called_with_bad_pointer(void);
 
 static __inline__ unsigned long
-__xchg(unsigned long x, volatile void * ptr, int size)
+__arch_xchg(unsigned long x, volatile void * ptr, int size)
 {
switch (size) {
case 1:
-- 
2.34.1



[PATCH 16/19] arch/sparc: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/sparc/include/asm/cmpxchg_32.h | 4 ++--
 arch/sparc/include/asm/cmpxchg_64.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/sparc/include/asm/cmpxchg_32.h 
b/arch/sparc/include/asm/cmpxchg_32.h
index 27a57a3a7597eb..7a1339533d1d7e 100644
--- a/arch/sparc/include/asm/cmpxchg_32.h
+++ b/arch/sparc/include/asm/cmpxchg_32.h
@@ -15,7 +15,7 @@
 unsigned long __xchg_u32(volatile u32 *m, u32 new);
 void __xchg_called_with_bad_pointer(void);
 
-static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr, 
int size)
+static inline unsigned long __arch_xchg(unsigned long x, __volatile__ void * 
ptr, int size)
 {
switch (size) {
case 4:
@@ -25,7 +25,7 @@ static inline unsigned long __xchg(unsigned long x, 
__volatile__ void * ptr, int
return x;
 }
 
-#define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__xchg((unsigned 
long)(x),(ptr),sizeof(*(ptr)));})
+#define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__arch_xchg((unsigned 
long)(x),(ptr),sizeof(*(ptr)));})
 
 /* Emulate cmpxchg() the same way we emulate atomics,
  * by hashing the object address and indexing into an array
diff --git a/arch/sparc/include/asm/cmpxchg_64.h 
b/arch/sparc/include/asm/cmpxchg_64.h
index 12d00a42c0a3ed..4c22fd9110c945 100644
--- a/arch/sparc/include/asm/cmpxchg_64.h
+++ b/arch/sparc/include/asm/cmpxchg_64.h
@@ -55,7 +55,7 @@ static inline unsigned long xchg64(__volatile__ unsigned long 
*m, unsigned long
 #define arch_xchg(ptr,x)   
\
 ({ __typeof__(*(ptr)) __ret;   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((unsigned long)(x), (ptr), sizeof(*(ptr)));  \
+   __arch_xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
__ret;  \
 })
 
@@ -87,7 +87,7 @@ xchg16(__volatile__ unsigned short *m, unsigned short val)
return (load32 & mask) >> bit_shift;
 }
 
-static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
+static inline unsigned long __arch_xchg(unsigned long x, __volatile__ void * 
ptr,
   int size)
 {
switch (size) {
-- 
2.34.1



[PATCH 15/19] arch/sh: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/sh/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/sh/include/asm/cmpxchg.h b/arch/sh/include/asm/cmpxchg.h
index 0ed9b3f4a57796..288f6f38d98fb4 100644
--- a/arch/sh/include/asm/cmpxchg.h
+++ b/arch/sh/include/asm/cmpxchg.h
@@ -22,7 +22,7 @@
 
 extern void __xchg_called_with_bad_pointer(void);
 
-#define __xchg(ptr, x, size)   \
+#define __arch_xchg(ptr, x, size)  \
 ({ \
unsigned long __xchg__res;  \
volatile void *__xchg_ptr = (ptr);  \
@@ -46,7 +46,7 @@ extern void __xchg_called_with_bad_pointer(void);
 })
 
 #define arch_xchg(ptr,x)   \
-   ((__typeof__(*(ptr)))__xchg((ptr),(unsigned long)(x), sizeof(*(ptr
+   ((__typeof__(*(ptr)))__arch_xchg((ptr),(unsigned long)(x), 
sizeof(*(ptr
 
 /* This function doesn't exist, so you'll get a linker error
  * if something tries to do an invalid cmpxchg(). */
-- 
2.34.1



[PATCH 14/19] arch/s390: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/s390/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/s390/include/asm/cmpxchg.h b/arch/s390/include/asm/cmpxchg.h
index 84c3f0d576c5b1..efc16f4aac8643 100644
--- a/arch/s390/include/asm/cmpxchg.h
+++ b/arch/s390/include/asm/cmpxchg.h
@@ -14,7 +14,7 @@
 
 void __xchg_called_with_bad_pointer(void);
 
-static __always_inline unsigned long __xchg(unsigned long x,
+static __always_inline unsigned long __arch_xchg(unsigned long x,
unsigned long address, int size)
 {
unsigned long old;
@@ -77,7 +77,7 @@ static __always_inline unsigned long __xchg(unsigned long x,
__typeof__(*(ptr)) __ret;   \
\
__ret = (__typeof__(*(ptr)))\
-   __xchg((unsigned long)(x), (unsigned long)(ptr),\
+   __arch_xchg((unsigned long)(x), (unsigned long)(ptr),   \
   sizeof(*(ptr))); \
__ret;  \
 })
-- 
2.34.1



[PATCH 13/19] arch/riscv: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/riscv/include/asm/atomic.h  | 2 +-
 arch/riscv/include/asm/cmpxchg.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/include/asm/atomic.h b/arch/riscv/include/asm/atomic.h
index 0dfe9d857a762b..bba472928b5393 100644
--- a/arch/riscv/include/asm/atomic.h
+++ b/arch/riscv/include/asm/atomic.h
@@ -261,7 +261,7 @@ c_t arch_atomic##prefix##_xchg_release(atomic##prefix##_t 
*v, c_t n)\
 static __always_inline \
 c_t arch_atomic##prefix##_xchg(atomic##prefix##_t *v, c_t n)   \
 {  \
-   return __xchg(&(v->counter), n, size);  \
+   return __arch_xchg(&(v->counter), n, size); \
 }  \
 static __always_inline \
 c_t arch_atomic##prefix##_cmpxchg_relaxed(atomic##prefix##_t *v,   \
diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h
index 12debce235e52d..2f4726d3cfcc25 100644
--- a/arch/riscv/include/asm/cmpxchg.h
+++ b/arch/riscv/include/asm/cmpxchg.h
@@ -114,7 +114,7 @@
_x_, sizeof(*(ptr)));   \
 })
 
-#define __xchg(ptr, new, size) \
+#define __arch_xchg(ptr, new, size)\
 ({ \
__typeof__(ptr) __ptr = (ptr);  \
__typeof__(new) __new = (new);  \
@@ -143,7 +143,7 @@
 #define arch_xchg(ptr, x)  \
 ({ \
__typeof__(*(ptr)) _x_ = (x);   \
-   (__typeof__(*(ptr))) __xchg((ptr), _x_, sizeof(*(ptr)));\
+   (__typeof__(*(ptr))) __arch_xchg((ptr), _x_, sizeof(*(ptr)));   \
 })
 
 #define xchg32(ptr, x) \
-- 
2.34.1



[PATCH 12/19] arch/powerpc: correct logged function names in xchg helpers

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/powerpc/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/include/asm/cmpxchg.h 
b/arch/powerpc/include/asm/cmpxchg.h
index 05f246c0e36eb3..b5624c9fe09bf7 100644
--- a/arch/powerpc/include/asm/cmpxchg.h
+++ b/arch/powerpc/include/asm/cmpxchg.h
@@ -163,7 +163,7 @@ __xchg_local(void *ptr, unsigned long x, unsigned int size)
return __xchg_u64_local(ptr, x);
 #endif
}
-   BUILD_BUG_ON_MSG(1, "Unsupported size for __xchg");
+   BUILD_BUG_ON_MSG(1, "Unsupported size for __xchg_local");
return x;
 }
 
@@ -182,7 +182,7 @@ __xchg_relaxed(void *ptr, unsigned long x, unsigned int 
size)
return __xchg_u64_relaxed(ptr, x);
 #endif
}
-   BUILD_BUG_ON_MSG(1, "Unsupported size for __xchg_local");
+   BUILD_BUG_ON_MSG(1, "Unsupported size for __xchg_relaxed");
return x;
 }
 #define arch_xchg_local(ptr,x)  \
-- 
2.34.1



[PATCH 11/19] arch/parisc: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/parisc/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/parisc/include/asm/cmpxchg.h 
b/arch/parisc/include/asm/cmpxchg.h
index 5f274be105671e..c1d776bb16b4ed 100644
--- a/arch/parisc/include/asm/cmpxchg.h
+++ b/arch/parisc/include/asm/cmpxchg.h
@@ -22,7 +22,7 @@ extern unsigned long __xchg64(unsigned long, volatile 
unsigned long *);
 
 /* optimizer better get rid of switch since size is a constant */
 static inline unsigned long
-__xchg(unsigned long x, volatile void *ptr, int size)
+__arch_xchg(unsigned long x, volatile void *ptr, int size)
 {
switch (size) {
 #ifdef CONFIG_64BIT
@@ -49,7 +49,7 @@ __xchg(unsigned long x, volatile void *ptr, int size)
__typeof__(*(ptr)) __ret;   \
__typeof__(*(ptr)) _x_ = (x);   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((unsigned long)_x_, (ptr), sizeof(*(ptr)));  \
+   __arch_xchg((unsigned long)_x_, (ptr), sizeof(*(ptr))); \
__ret;  \
 })
 
-- 
2.34.1



[PATCH 10/19] arch/openrisc: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/openrisc/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/openrisc/include/asm/cmpxchg.h 
b/arch/openrisc/include/asm/cmpxchg.h
index 79fd16162ccb6d..5725e22e10683b 100644
--- a/arch/openrisc/include/asm/cmpxchg.h
+++ b/arch/openrisc/include/asm/cmpxchg.h
@@ -147,7 +147,7 @@ static inline unsigned long __cmpxchg(volatile void *ptr, 
unsigned long old,
 extern unsigned long __xchg_called_with_bad_pointer(void)
__compiletime_error("Bad argument size for xchg");
 
-static inline unsigned long __xchg(volatile void *ptr, unsigned long with,
+static inline unsigned long __arch_xchg(volatile void *ptr, unsigned long with,
int size)
 {
switch (size) {
@@ -163,7 +163,7 @@ static inline unsigned long __xchg(volatile void *ptr, 
unsigned long with,
 
 #define arch_xchg(ptr, with)   \
({  \
-   (__typeof__(*(ptr))) __xchg((ptr),  \
+   (__typeof__(*(ptr))) __arch_xchg((ptr), \
(unsigned long)(with),  \
sizeof(*(ptr)));\
})
-- 
2.34.1



[PATCH 09/19] arch/mips: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/mips/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/mips/include/asm/cmpxchg.h b/arch/mips/include/asm/cmpxchg.h
index 7ec9493b28614f..feed343ad483a9 100644
--- a/arch/mips/include/asm/cmpxchg.h
+++ b/arch/mips/include/asm/cmpxchg.h
@@ -68,7 +68,7 @@ extern unsigned long __xchg_small(volatile void *ptr, 
unsigned long val,
  unsigned int size);
 
 static __always_inline
-unsigned long __xchg(volatile void *ptr, unsigned long x, int size)
+unsigned long __arch_xchg(volatile void *ptr, unsigned long x, int size)
 {
switch (size) {
case 1:
@@ -102,7 +102,7 @@ unsigned long __xchg(volatile void *ptr, unsigned long x, 
int size)
smp_mb__before_llsc();  \
\
__res = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)(x), sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)(x), sizeof(*(ptr))); \
\
smp_llsc_mb();  \
\
-- 
2.34.1



[PATCH 08/19] arch/m68k: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/m68k/include/asm/cmpxchg.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/m68k/include/asm/cmpxchg.h b/arch/m68k/include/asm/cmpxchg.h
index 6cf464cdab067e..d7f3de9c5d6f79 100644
--- a/arch/m68k/include/asm/cmpxchg.h
+++ b/arch/m68k/include/asm/cmpxchg.h
@@ -9,7 +9,7 @@
 extern unsigned long __invalid_xchg_size(unsigned long, volatile void *, int);
 
 #ifndef CONFIG_RMW_INSNS
-static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int 
size)
+static inline unsigned long __arch_xchg(unsigned long x, volatile void * ptr, 
int size)
 {
unsigned long flags, tmp;
 
@@ -40,7 +40,7 @@ static inline unsigned long __xchg(unsigned long x, volatile 
void * ptr, int siz
return x;
 }
 #else
-static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int 
size)
+static inline unsigned long __arch_xchg(unsigned long x, volatile void * ptr, 
int size)
 {
switch (size) {
case 1:
@@ -75,7 +75,7 @@ static inline unsigned long __xchg(unsigned long x, volatile 
void * ptr, int siz
 }
 #endif
 
-#define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__xchg((unsigned 
long)(x),(ptr),sizeof(*(ptr)));})
+#define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__arch_xchg((unsigned 
long)(x),(ptr),sizeof(*(ptr)));})
 
 #include 
 
-- 
2.34.1



[PATCH 07/19] arch/loongarch: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/loongarch/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/loongarch/include/asm/cmpxchg.h 
b/arch/loongarch/include/asm/cmpxchg.h
index ecfa6cf79806e6..979fde61bba8a4 100644
--- a/arch/loongarch/include/asm/cmpxchg.h
+++ b/arch/loongarch/include/asm/cmpxchg.h
@@ -62,7 +62,7 @@ static inline unsigned int __xchg_small(volatile void *ptr, 
unsigned int val,
 }
 
 static __always_inline unsigned long
-__xchg(volatile void *ptr, unsigned long x, int size)
+__arch_xchg(volatile void *ptr, unsigned long x, int size)
 {
switch (size) {
case 1:
@@ -87,7 +87,7 @@ __xchg(volatile void *ptr, unsigned long x, int size)
__typeof__(*(ptr)) __res;   \
\
__res = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)(x), sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)(x), sizeof(*(ptr))); \
\
__res;  \
 })
-- 
2.34.1



[PATCH 06/19] arch/ia64: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/ia64/include/asm/cmpxchg.h  | 2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/ia64/include/asm/cmpxchg.h b/arch/ia64/include/asm/cmpxchg.h
index 94ef844298431a..8b2e644ef6a14e 100644
--- a/arch/ia64/include/asm/cmpxchg.h
+++ b/arch/ia64/include/asm/cmpxchg.h
@@ -5,7 +5,7 @@
 #include 
 
 #define arch_xchg(ptr, x)  \
-({(__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr)));})
+({(__typeof__(*(ptr))) __arch_xchg((unsigned long) (x), (ptr), 
sizeof(*(ptr)));})
 
 #define arch_cmpxchg(ptr, o, n)cmpxchg_acq((ptr), (o), (n))
 #define arch_cmpxchg64(ptr, o, n)  cmpxchg_acq((ptr), (o), (n))
diff --git a/arch/ia64/include/uapi/asm/cmpxchg.h 
b/arch/ia64/include/uapi/asm/cmpxchg.h
index ca2e0268534384..3fec9b037051bb 100644
--- a/arch/ia64/include/uapi/asm/cmpxchg.h
+++ b/arch/ia64/include/uapi/asm/cmpxchg.h
@@ -27,7 +27,7 @@
  */
 extern void ia64_xchg_called_with_bad_pointer(void);
 
-#define __xchg(x, ptr, size)   \
+#define __arch_xchg(x, ptr, size)  \
 ({ \
unsigned long __xchg_result;\
\
@@ -55,7 +55,7 @@ extern void ia64_xchg_called_with_bad_pointer(void);
 
 #ifndef __KERNEL__
 #define xchg(ptr, x)   \
-({(__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr)));})
+({(__typeof__(*(ptr))) __arch_xchg((unsigned long) (x), (ptr), 
sizeof(*(ptr)));})
 #endif
 
 /*
-- 
2.34.1



[PATCH 05/19] arch/hexagon: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/hexagon/include/asm/cmpxchg.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/hexagon/include/asm/cmpxchg.h 
b/arch/hexagon/include/asm/cmpxchg.h
index cdb705e1496af8..92dc5e5f836f3b 100644
--- a/arch/hexagon/include/asm/cmpxchg.h
+++ b/arch/hexagon/include/asm/cmpxchg.h
@@ -9,7 +9,7 @@
 #define _ASM_CMPXCHG_H
 
 /*
- * __xchg - atomically exchange a register and a memory location
+ * __arch_xchg - atomically exchange a register and a memory location
  * @x: value to swap
  * @ptr: pointer to memory
  * @size:  size of the value
@@ -19,7 +19,7 @@
  * Note:  there was an errata for V2 about .new's and memw_locked.
  *
  */
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
+static inline unsigned long __arch_xchg(unsigned long x, volatile void *ptr,
   int size)
 {
unsigned long retval;
@@ -42,7 +42,7 @@ static inline unsigned long __xchg(unsigned long x, volatile 
void *ptr,
  * Atomically swap the contents of a register with memory.  Should be atomic
  * between multiple CPU's and within interrupts on the same CPU.
  */
-#define arch_xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), 
(ptr), \
+#define arch_xchg(ptr, v) ((__typeof__(*(ptr)))__arch_xchg((unsigned long)(v), 
(ptr), \
sizeof(*(ptr
 
 /*
-- 
2.34.1



[PATCH 04/19] arch/arm64: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/arm64/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/cmpxchg.h b/arch/arm64/include/asm/cmpxchg.h
index 497acf134d9923..3a36ba58e8c2ef 100644
--- a/arch/arm64/include/asm/cmpxchg.h
+++ b/arch/arm64/include/asm/cmpxchg.h
@@ -62,7 +62,7 @@ __XCHG_CASE( ,  ,  mb_, 64, dmb ish, nop,  , a, l, "memory")
 #undef __XCHG_CASE
 
 #define __XCHG_GEN(sfx)
\
-static __always_inline  unsigned long __xchg##sfx(unsigned long x, \
+static __always_inline  unsigned long __arch_xchg##sfx(unsigned long x,
\
volatile void *ptr, \
int size)   \
 {  \
@@ -93,7 +93,7 @@ __XCHG_GEN(_mb)
 ({ \
__typeof__(*(ptr)) __ret;   \
__ret = (__typeof__(*(ptr)))\
-   __xchg##sfx((unsigned long)(x), (ptr), sizeof(*(ptr))); \
+   __arch_xchg##sfx((unsigned long)(x), (ptr), sizeof(*(ptr))); \
__ret;  \
 })
 
-- 
2.34.1



[PATCH 03/19] arch/arm: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/arm/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm/include/asm/cmpxchg.h b/arch/arm/include/asm/cmpxchg.h
index 4dfe538dfc689b..6953fc05a97886 100644
--- a/arch/arm/include/asm/cmpxchg.h
+++ b/arch/arm/include/asm/cmpxchg.h
@@ -25,7 +25,7 @@
 #define swp_is_buggy
 #endif
 
-static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int 
size)
+static inline unsigned long __arch_xchg(unsigned long x, volatile void *ptr, 
int size)
 {
extern void __bad_xchg(volatile void *, int);
unsigned long ret;
@@ -115,7 +115,7 @@ static inline unsigned long __xchg(unsigned long x, 
volatile void *ptr, int size
 }
 
 #define arch_xchg_relaxed(ptr, x) ({   \
-   (__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr),   \
+   (__typeof__(*(ptr)))__arch_xchg((unsigned long)(x), (ptr),  
\
   sizeof(*(ptr))); \
 })
 
-- 
2.34.1



[PATCH 02/19] arch/arc: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/arc/include/asm/cmpxchg.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h
index c5b544a5fe8106..e138fde067dea5 100644
--- a/arch/arc/include/asm/cmpxchg.h
+++ b/arch/arc/include/asm/cmpxchg.h
@@ -85,7 +85,7 @@
  */
 #ifdef CONFIG_ARC_HAS_LLSC
 
-#define __xchg(ptr, val)   \
+#define __arch_xchg(ptr, val)  \
 ({ \
__asm__ __volatile__(   \
"   ex  %0, [%1]\n" /* set new value */ \
@@ -102,7 +102,7 @@
\
switch(sizeof(*(_p_))) {\
case 4: \
-   _val_ = __xchg(_p_, _val_); \
+   _val_ = __arch_xchg(_p_, _val_);\
break;  \
default:\
BUILD_BUG();\
-- 
2.34.1



[PATCH 01/19] arch/alpha: rename internal name __xchg to __arch_xchg

2022-12-22 Thread Andrzej Hajda
__xchg will be used for non-atomic xchg macro.

Signed-off-by: Andrzej Hajda 
---
 arch/alpha/include/asm/cmpxchg.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.h
index 6e0a850aa9d38c..40e8159ef6e794 100644
--- a/arch/alpha/include/asm/cmpxchg.h
+++ b/arch/alpha/include/asm/cmpxchg.h
@@ -6,7 +6,7 @@
  * Atomic exchange routines.
  */
 
-#define xchg(type, args...)__xchg ## type ## _local(args)
+#define xchg(type, args...)__arch_xchg ## type ## 
_local(args)
 #define cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
 #include 
 
@@ -34,7 +34,7 @@
 
 #undef xchg
 #undef cmpxchg
-#define xchg(type, args...)__xchg ##type(args)
+#define xchg(type, args...)__arch_xchg ##type(args)
 #define cmpxchg(type, args...) __cmpxchg ##type(args)
 #include 
 
@@ -48,7 +48,7 @@
__typeof__(*(ptr)) _x_ = (x);   \
smp_mb();   \
__ret = (__typeof__(*(ptr)))\
-   __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr)));  \
+   __arch_xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
smp_mb();   \
__ret;  \
 })
-- 
2.34.1



[PATCH 00/19] Introduce __xchg, non-atomic xchg

2022-12-22 Thread Andrzej Hajda
Hi all,

I hope there will be place for such tiny helper in kernel.
Quick cocci analyze shows there is probably few thousands places
where it could be useful.
I am not sure who is good person to review/ack such patches,
so I've used my intuition to construct to/cc lists, sorry for mistakes.
This is the 2nd approach of the same idea, with comments addressed[0].

The helper is tiny and there are advices we can leave without it, so
I want to present few arguments why it would be good to have it:

1. Code readability/simplification/number of lines:

Real example from drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c:
-   previous_min_rate = evport->qos.min_rate;
-   evport->qos.min_rate = min_rate;
+   previous_min_rate = __xchg(evport->qos.min_rate, min_rate);

For sure the code is more compact, and IMHO more readable.

2. Presence of similar helpers in other somehow related languages/libs:

a) Rust[1]: 'replace' from std::mem module, there is also 'take'
helper (__xchg(, 0)), which is the same as private helper in
i915 - fetch_and_zero, see latest patch.
b) C++ [2]: 'exchange' from utility header.

If the idea is OK there are still 2 qestions to answer:

1. Name of the helper, __xchg follows kernel conventions,
but for me Rust names are also OK.
2. Where to put the helper:
a) as in this patchset include/linux/non-atomic/xchg.h,
proposed by Andy Shevchenko,
b) include/linux/utils.h ? any better name? Some kind
of container for simple helpers.

Structure of the patchset:
17 patches releasing __xchg name from arch files
1 patch adding __xchg
1 patch adding users of __xchg

Arnd thanks for convienient set of cross compilers, it was very helpful.

So many words for so small helper :)

[0]: https://lore.kernel.org/lkml/y5ofsvaybv4xc...@smile.fi.intel.com/T/
[1]: https://doc.rust-lang.org/std/mem/index.html
[2]: https://en.cppreference.com/w/cpp/header/utility

Regards
Andrzej

Andrzej Hajda (19):
  arch/alpha: rename internal name __xchg to __arch_xchg
  arch/arc: rename internal name __xchg to __arch_xchg
  arch/arm: rename internal name __xchg to __arch_xchg
  arch/arm64: rename internal name __xchg to __arch_xchg
  arch/hexagon: rename internal name __xchg to __arch_xchg
  arch/ia64: rename internal name __xchg to __arch_xchg
  arch/loongarch: rename internal name __xchg to __arch_xchg
  arch/m68k: rename internal name __xchg to __arch_xchg
  arch/mips: rename internal name __xchg to __arch_xchg
  arch/openrisc: rename internal name __xchg to __arch_xchg
  arch/parisc: rename internal name __xchg to __arch_xchg
  arch/powerpc: correct logged function names in xchg helpers
  arch/riscv: rename internal name __xchg to __arch_xchg
  arch/s390: rename internal name __xchg to __arch_xchg
  arch/sh: rename internal name __xchg to __arch_xchg
  arch/sparc: rename internal name __xchg to __arch_xchg
  arch/xtensa: rename internal name __xchg to __arch_xchg
  linux/include: add non-atomic version of xchg
  drm/i915/gt: use __xchg instead of internal helper

 arch/alpha/include/asm/cmpxchg.h  |  6 +++---
 arch/arc/include/asm/cmpxchg.h|  4 ++--
 arch/arm/include/asm/cmpxchg.h|  4 ++--
 arch/arm64/include/asm/cmpxchg.h  |  4 ++--
 arch/hexagon/include/asm/cmpxchg.h|  6 +++---
 arch/ia64/include/asm/cmpxchg.h   |  2 +-
 arch/ia64/include/uapi/asm/cmpxchg.h  |  4 ++--
 arch/loongarch/include/asm/cmpxchg.h  |  4 ++--
 arch/m68k/include/asm/cmpxchg.h   |  6 +++---
 arch/mips/include/asm/cmpxchg.h   |  4 ++--
 arch/openrisc/include/asm/cmpxchg.h   |  4 ++--
 arch/parisc/include/asm/cmpxchg.h |  4 ++--
 arch/powerpc/include/asm/cmpxchg.h|  4 ++--
 arch/riscv/include/asm/atomic.h   |  2 +-
 arch/riscv/include/asm/cmpxchg.h  |  4 ++--
 arch/s390/include/asm/cmpxchg.h   |  4 ++--
 arch/sh/include/asm/cmpxchg.h |  4 ++--
 arch/sparc/include/asm/cmpxchg_32.h   |  4 ++--
 arch/sparc/include/asm/cmpxchg_64.h   |  4 ++--
 arch/xtensa/include/asm/cmpxchg.h |  4 ++--
 drivers/gpu/drm/i915/gt/intel_engine_cs.c |  2 +-
 .../gpu/drm/i915/gt/intel_engine_heartbeat.c  |  4 ++--
 .../drm/i915/gt/intel_execlists_submission.c  |  4 ++--
 drivers/gpu/drm/i915/gt/intel_ggtt.c  |  4 ++--
 drivers/gpu/drm/i915/gt/intel_gsc.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_gt.c|  4 ++--
 drivers/gpu/drm/i915/gt/intel_gt_pm.c |  2 +-
 drivers/gpu/drm/i915/gt/intel_lrc.c   |  6 +++---
 drivers/gpu/drm/i915/gt/intel_migrate.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_rc6.c   |  2 +-
 drivers/gpu/drm/i915/gt/intel_rps.c   |  2 +-
 drivers/gpu/drm/i915/gt/selftest_context.c|  2 +-
 .../drm/i915/gt/selftest_ring_submission.c|  2 +-
 drivers/gpu/drm/i915/gt/selftest_timeline.c   |  2 +-
 drivers/gpu/drm/i915/gt/uc/intel

[PATCH] powerpc/powernv/pci: fix iterator signedness

2016-08-17 Thread Andrzej Hajda
Unsigned type is always non-negative, so the loop could not end in case
condition is never true.

The problem has been detected using semantic patch
scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci

Signed-off-by: Andrzej Hajda <a.ha...@samsung.com>
---
 arch/powerpc/platforms/powernv/pci-ioda.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c 
b/arch/powerpc/platforms/powernv/pci-ioda.c
index fd9444f..1321826 100644
--- a/arch/powerpc/platforms/powernv/pci-ioda.c
+++ b/arch/powerpc/platforms/powernv/pci-ioda.c
@@ -149,7 +149,7 @@ static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int 
pe_no)
 
 static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
 {
-   unsigned long pe = phb->ioda.total_pe_num - 1;
+   long pe;
 
for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
-- 
1.9.1



[PATCH 7/7] usb: gadget: fsl_qe_udc: fix IS_ERR_VALUE usage

2016-02-15 Thread Andrzej Hajda
IS_ERR_VALUE macro should be used only with unsigned long type.
Otherwise it can work incorrectly.

The patch follows conclusion from discussion on LKML [1][2].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2120927
[2]: http://permalink.gmane.org/gmane.linux.kernel/2150581

Signed-off-by: Andrzej Hajda <a.ha...@samsung.com>
---
 drivers/usb/gadget/udc/fsl_qe_udc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/udc/fsl_qe_udc.c 
b/drivers/usb/gadget/udc/fsl_qe_udc.c
index 53c0692..93d28cb 100644
--- a/drivers/usb/gadget/udc/fsl_qe_udc.c
+++ b/drivers/usb/gadget/udc/fsl_qe_udc.c
@@ -2340,7 +2340,7 @@ static struct qe_udc *qe_udc_config(struct 
platform_device *ofdev)
 {
struct qe_udc *udc;
struct device_node *np = ofdev->dev.of_node;
-   unsigned int tmp_addr = 0;
+   unsigned long tmp_addr = 0;
struct usb_device_para __iomem *usbpram;
unsigned int i;
u64 size;
-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 0/7] fix IS_ERR_VALUE usage

2016-02-15 Thread Andrzej Hajda
Hi,

This small set of independent patches tries to fix incorrect
IS_ERR_VALUE macro usage. It fixes most usages leading to errors
as described in [1]. It also follows conclusion from the discussion
[1][2] - IS_ERR_VALUE should be used only with unsigned long type,
signed types should use comparison 'ret < 0'.

The patchset does not fix errors present in net/ethernet/freescale
and soc/fsq/qe drivers - these drivers mixes different types:
dma_addr_t, u32, unsigned long, fixing it properly seems to me more
challenging, maybe maintainers or brave volunteers can look it.

The list of missing fixes:
drivers/net/ethernet/freescale/fs_enet/mac-scc.c:149:36-37: WARNING: incorrect 
argument type in IS_ERR_VALUE(fep -> ring_mem_addr)
drivers/net/ethernet/freescale/ucc_geth.c:2237:48-49: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> tx_bd_ring_offset [ j ])
drivers/net/ethernet/freescale/ucc_geth.c:2314:48-49: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> rx_bd_ring_offset [ j ])
drivers/net/ethernet/freescale/ucc_geth.c:2524:44-45: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> tx_glbl_pram_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2544:45-46: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> thread_dat_tx_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2571:46-47: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> send_q_mem_reg_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2612:42-43: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> scheduler_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2659:54-55: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> tx_fw_statistics_pram_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2696:44-45: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> rx_glbl_pram_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2715:45-46: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> thread_dat_rx_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2736:54-55: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> rx_fw_statistics_pram_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2756:53-54: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> rx_irq_coalescing_tbl_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2822:44-45: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> rx_bd_qs_tbl_offset)
drivers/net/ethernet/freescale/ucc_geth.c:2908:47-48: WARNING: incorrect 
argument type in IS_ERR_VALUE(ugeth -> exf_glbl_param_offset)
drivers/net/ethernet/freescale/ucc_geth.c:292:36-37: WARNING: incorrect 
argument type in IS_ERR_VALUE(init_enet_offset)
drivers/net/ethernet/freescale/ucc_geth.c:3042:39-40: WARNING: incorrect 
argument type in IS_ERR_VALUE(init_enet_pram_offset)
drivers/soc/fsl/qe/ucc_fast.c:271:60-61: WARNING: incorrect argument type in 
IS_ERR_VALUE(uccf -> ucc_fast_tx_virtual_fifo_base_offset)
drivers/soc/fsl/qe/ucc_fast.c:284:60-61: WARNING: incorrect argument type in 
IS_ERR_VALUE(uccf -> ucc_fast_rx_virtual_fifo_base_offset)
drivers/soc/fsl/qe/ucc_slow.c:186:38-39: WARNING: incorrect argument type in 
IS_ERR_VALUE(uccs -> us_pram_offset)
drivers/soc/fsl/qe/ucc_slow.c:213:38-39: WARNING: incorrect argument type in 
IS_ERR_VALUE(uccs -> rx_base_offset)
drivers/soc/fsl/qe/ucc_slow.c:224:38-39: WARNING: incorrect argument type in 
IS_ERR_VALUE(uccs -> tx_base_offset)
drivers/net/ethernet/freescale/fs_enet/mac-fcc.c:110:35-36: WARNING: unknown 
argument type in IS_ERR_VALUE(fpi -> dpram_offset)

[1]: http://permalink.gmane.org/gmane.linux.kernel/2120927
[2]: http://permalink.gmane.org/gmane.linux.kernel/2150581

Regards
Andrzej


Andrzej Hajda (7):
  netfilter: fix IS_ERR_VALUE usage
  MIPS: module: fix incorrect IS_ERR_VALUE macro usages
  drivers: char: mem: fix IS_ERROR_VALUE usage
  atmel-isi: fix IS_ERR_VALUE usage
  serial: clps711x: fix IS_ERR_VALUE usage
  fbdev: exynos: fix IS_ERR_VALUE usage
  usb: gadget: fsl_qe_udc: fix IS_ERR_VALUE usage

 arch/mips/kernel/module-rela.c|  2 +-
 arch/mips/kernel/module.c |  2 +-
 drivers/char/mem.c|  2 +-
 drivers/media/platform/soc_camera/atmel-isi.c |  4 ++--
 drivers/tty/serial/clps711x.c | 14 --
 drivers/usb/gadget/udc/fsl_qe_udc.c   |  2 +-
 drivers/video/fbdev/exynos/exynos_mipi_dsi.c  |  6 +++---
 include/linux/netfilter/x_tables.h|  6 +++---
 net/ipv4/netfilter/arp_tables.c   | 11 +++
 net/ipv4/netfilter/ip_tables.c| 12 
 net/ipv6/netfilter/ip6_tables.c   | 13 +
 11 files changed, 44 insertions(+), 30 deletions(-)

-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 15/19] KVM: PPC: e500: fix handling local_sid_lookup result

2015-09-24 Thread Andrzej Hajda
The function can return negative value.

The problem has been detected using proposed semantic patch
scripts/coccinelle/tests/assign_signed_to_unsigned.cocci [1].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2046107

Signed-off-by: Andrzej Hajda <a.ha...@samsung.com>
---
Hi,

To avoid problems with too many mail recipients I have sent whole
patchset only to LKML. Anyway patches have no dependencies.

Regards
Andrzej
---
 arch/powerpc/kvm/e500.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/e500.c b/arch/powerpc/kvm/e500.c
index b29ce75..32fdab5 100644
--- a/arch/powerpc/kvm/e500.c
+++ b/arch/powerpc/kvm/e500.c
@@ -237,7 +237,8 @@ void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 
*vcpu_e500,
struct kvm_book3e_206_tlb_entry *gtlbe)
 {
struct vcpu_id_table *idt = vcpu_e500->idt;
-   unsigned int pr, tid, ts, pid;
+   unsigned int pr, tid, ts;
+   int pid;
u32 val, eaddr;
unsigned long flags;
 
-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 05/31] arch/powerpc/nvram: use kmemdup rather than duplicating its implementation

2015-08-07 Thread Andrzej Hajda
The patch was generated using fixed coccinelle semantic patch
scripts/coccinelle/api/memdup.cocci [1].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2014320

Signed-off-by: Andrzej Hajda a.ha...@samsung.com
---
 arch/powerpc/kernel/nvram_64.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
index 1e703f8..6f6597b 100644
--- a/arch/powerpc/kernel/nvram_64.c
+++ b/arch/powerpc/kernel/nvram_64.c
@@ -541,10 +541,9 @@ static ssize_t nvram_pstore_read(u64 *id, enum 
pstore_type_id *type,
time-tv_sec = be64_to_cpu(oops_hdr-timestamp);
time-tv_nsec = 0;
}
-   *buf = kmalloc(length, GFP_KERNEL);
+   *buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
if (*buf == NULL)
return -ENOMEM;
-   memcpy(*buf, buff + hdr_size, length);
kfree(buff);
 
if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 06/31] arch/powerpc/pseries: use kmemdup rather than duplicating its implementation

2015-08-07 Thread Andrzej Hajda
The patch was generated using fixed coccinelle semantic patch
scripts/coccinelle/api/memdup.cocci [1].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2014320

Signed-off-by: Andrzej Hajda a.ha...@samsung.com
---
 arch/powerpc/platforms/pseries/hotplug-memory.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c 
b/arch/powerpc/platforms/pseries/hotplug-memory.c
index 0ced387..e9ff44c 100644
--- a/arch/powerpc/platforms/pseries/hotplug-memory.c
+++ b/arch/powerpc/platforms/pseries/hotplug-memory.c
@@ -92,13 +92,12 @@ static struct property *dlpar_clone_drconf_property(struct 
device_node *dn)
return NULL;
 
new_prop-name = kstrdup(prop-name, GFP_KERNEL);
-   new_prop-value = kmalloc(prop-length, GFP_KERNEL);
+   new_prop-value = kmemdup(prop-value, prop-length, GFP_KERNEL);
if (!new_prop-name || !new_prop-value) {
dlpar_free_drconf_property(new_prop);
return NULL;
}
 
-   memcpy(new_prop-value, prop-value, prop-length);
new_prop-length = prop-length;
 
/* Convert the property to cpu endian-ness */
-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Re: [PATCH] of/platform: Move platform devices under /sys/devices/platform

2014-11-18 Thread Andrzej Hajda
On 11/04/2014 11:45 AM, Grant Likely wrote:
 Currently the devices created by drivers/of/platform.c get created at
 the root of /sys/devices. This goes against the typical pattern for
 sysfs where the top level /sys/devices structure contains categories of
 devices, and the structure of devices is placed below that. To fix this,
 make the code in drivers/of/platform.c follow the drivers/base/platform.c
 behaviour, and use platform_bus as the default parent for all new
 platform_devices and amba_devices.
 
 This change has been discussed for a long time, but nobody has actually
 acted on it. Userspace code that expects to find devices under a fixed
 /sys/devices/... path will be affected. It isn't /supposed/ to do that,
 but if anyone complains then I'll add a default-off workaround option to
 put them back into the root.

One of side effects of this change is that platform drivers registering
other platform drivers or devices in their probe callback can deadlock
due to double device_lock on platform device. This is for example case
of exynos_drm driver[1]. I guess it could/should be fixed in exynos_drm.
Anyway it can affect other drivers as well. At least grep shows few
possible candidates:

$ git grep -p platform_driver_register | grep -A1 -P '_probe\(struct
platform_device'

drivers/gpu/drm/exynos/exynos_drm_drv.c=static int
exynos_drm_platform_probe(struct platform_device *pdev)
drivers/gpu/drm/exynos/exynos_drm_drv.c:ret =
platform_driver_register(fimd_driver);
--
drivers/gpu/drm/sti/sti_drm_drv.c=static int
sti_drm_platform_probe(struct platform_device *pdev)
drivers/gpu/drm/sti/sti_drm_drv.c:
platform_driver_register(sti_drm_master_driver);
--
drivers/mtd/nand/atmel_nand.c=static int atmel_nand_probe(struct
platform_device *pdev)
drivers/mtd/nand/atmel_nand.c:  res =
platform_driver_register(atmel_nand_nfc_driver);


[1]: http://permalink.gmane.org/gmane.comp.video.dri.devel/117727

Regards
Andrzej


 
 Signed-off-by: Grant Likely grant.lik...@linaro.org
 Cc: Rob Herring robh...@kernel.org
 Cc: Arnd Bergmann a...@arndb.de
 Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
 Cc: Benjamin Herrenschmidt b...@kernel.crashing.org
 ---
  drivers/of/platform.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
 
 diff --git a/drivers/of/platform.c b/drivers/of/platform.c
 index 3b64d0bf5bba..7c6771986c06 100644
 --- a/drivers/of/platform.c
 +++ b/drivers/of/platform.c
 @@ -138,7 +138,7 @@ struct platform_device *of_device_alloc(struct 
 device_node *np,
   }
  
   dev-dev.of_node = of_node_get(np);
 - dev-dev.parent = parent;
 + dev-dev.parent = parent ? : platform_bus;
  
   if (bus_id)
   dev_set_name(dev-dev, %s, bus_id);
 @@ -291,7 +291,7 @@ static struct amba_device *of_amba_device_create(struct 
 device_node *node,
  
   /* setup generic device info */
   dev-dev.of_node = of_node_get(node);
 - dev-dev.parent = parent;
 + dev-dev.parent = parent ? : platform_bus;
   dev-dev.platform_data = platform_data;
   if (bus_id)
   dev_set_name(dev-dev, %s, bus_id);
 

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 0/2] backlight: remove trivial get_brightness implementations

2014-05-30 Thread Andrzej Hajda
This patchset makes get_brightness callback optional
and removes trivial implementations.
Driver changes are quite obvious so I have put them into single
patch.

The patchset is based on the current linux-next branch.

Regards
Andrzej

Andrzej Hajda (2):
  backlight: show brightness even if get_brightness is not implemented
  backlight: remove trivial get_brightness implementations

 drivers/macintosh/via-pmu-backlight.c  | 6 --
 drivers/platform/x86/samsung-q10.c | 6 --
 drivers/video/backlight/aat2870_bl.c   | 6 --
 drivers/video/backlight/ams369fg06.c   | 6 --
 drivers/video/backlight/backlight.c| 2 ++
 drivers/video/backlight/bd6107.c   | 6 --
 drivers/video/backlight/gpio_backlight.c   | 6 --
 drivers/video/backlight/ld9040.c   | 6 --
 drivers/video/backlight/lp855x_bl.c| 6 --
 drivers/video/backlight/lp8788_bl.c| 6 --
 drivers/video/backlight/lv5207lp.c | 6 --
 drivers/video/backlight/pandora_bl.c   | 6 --
 drivers/video/backlight/pwm_bl.c   | 6 --
 drivers/video/backlight/s6e63m0.c  | 6 --
 drivers/video/backlight/tps65217_bl.c  | 6 --
 drivers/video/fbdev/aty/aty128fb.c | 6 --
 drivers/video/fbdev/aty/atyfb_base.c   | 6 --
 drivers/video/fbdev/aty/radeon_backlight.c | 6 --
 drivers/video/fbdev/exynos/s6e8ax0.c   | 6 --
 drivers/video/fbdev/nvidia/nv_backlight.c  | 6 --
 drivers/video/fbdev/riva/fbdev.c   | 6 --
 21 files changed, 2 insertions(+), 120 deletions(-)

-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 1/2] backlight: show brightness even if get_brightness is not implemented

2014-05-30 Thread Andrzej Hajda
Many implementations of get_brightness operation returns only value
of props.brightness field. This change makes such implementations
unnecessary.

Signed-off-by: Andrzej Hajda a.ha...@samsung.com
---
 drivers/video/backlight/backlight.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/video/backlight/backlight.c 
b/drivers/video/backlight/backlight.c
index 19b170d..0ce8823 100644
--- a/drivers/video/backlight/backlight.c
+++ b/drivers/video/backlight/backlight.c
@@ -221,6 +221,8 @@ static ssize_t actual_brightness_show(struct device *dev,
mutex_lock(bd-ops_lock);
if (bd-ops  bd-ops-get_brightness)
rc = sprintf(buf, %d\n, bd-ops-get_brightness(bd));
+   else
+   rc = sprintf(buf, %d\n, bd-props.brightness);
mutex_unlock(bd-ops_lock);
 
return rc;
-- 
1.9.1

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[PATCH 2/2] backlight: remove trivial get_brightness implementations

2014-05-30 Thread Andrzej Hajda
Since backlight core returns props.brightness in case get_brightness
is not implemented trivial implementations are not needed anymore.

Signed-off-by: Andrzej Hajda a.ha...@samsung.com
---
 drivers/macintosh/via-pmu-backlight.c  | 6 --
 drivers/platform/x86/samsung-q10.c | 6 --
 drivers/video/backlight/aat2870_bl.c   | 6 --
 drivers/video/backlight/ams369fg06.c   | 6 --
 drivers/video/backlight/bd6107.c   | 6 --
 drivers/video/backlight/gpio_backlight.c   | 6 --
 drivers/video/backlight/ld9040.c   | 6 --
 drivers/video/backlight/lp855x_bl.c| 6 --
 drivers/video/backlight/lp8788_bl.c| 6 --
 drivers/video/backlight/lv5207lp.c | 6 --
 drivers/video/backlight/pandora_bl.c   | 6 --
 drivers/video/backlight/pwm_bl.c   | 6 --
 drivers/video/backlight/s6e63m0.c  | 6 --
 drivers/video/backlight/tps65217_bl.c  | 6 --
 drivers/video/fbdev/aty/aty128fb.c | 6 --
 drivers/video/fbdev/aty/atyfb_base.c   | 6 --
 drivers/video/fbdev/aty/radeon_backlight.c | 6 --
 drivers/video/fbdev/exynos/s6e8ax0.c   | 6 --
 drivers/video/fbdev/nvidia/nv_backlight.c  | 6 --
 drivers/video/fbdev/riva/fbdev.c   | 6 --
 20 files changed, 120 deletions(-)

diff --git a/drivers/macintosh/via-pmu-backlight.c 
b/drivers/macintosh/via-pmu-backlight.c
index b1d9117..6f68537 100644
--- a/drivers/macintosh/via-pmu-backlight.c
+++ b/drivers/macintosh/via-pmu-backlight.c
@@ -110,13 +110,7 @@ static int pmu_backlight_update_status(struct 
backlight_device *bd)
 }
 
 
-static int pmu_backlight_get_brightness(struct backlight_device *bd)
-{
-   return bd-props.brightness;
-}
-
 static const struct backlight_ops pmu_backlight_data = {
-   .get_brightness = pmu_backlight_get_brightness,
.update_status  = pmu_backlight_update_status,
 
 };
diff --git a/drivers/platform/x86/samsung-q10.c 
b/drivers/platform/x86/samsung-q10.c
index 5413f62..28d12bd 100644
--- a/drivers/platform/x86/samsung-q10.c
+++ b/drivers/platform/x86/samsung-q10.c
@@ -46,13 +46,7 @@ static int samsungq10_bl_set_intensity(struct 
backlight_device *bd)
return 0;
 }
 
-static int samsungq10_bl_get_intensity(struct backlight_device *bd)
-{
-   return bd-props.brightness;
-}
-
 static const struct backlight_ops samsungq10_bl_ops = {
-   .get_brightness = samsungq10_bl_get_intensity,
.update_status  = samsungq10_bl_set_intensity,
 };
 
diff --git a/drivers/video/backlight/aat2870_bl.c 
b/drivers/video/backlight/aat2870_bl.c
index ec5350f..86234c3 100644
--- a/drivers/video/backlight/aat2870_bl.c
+++ b/drivers/video/backlight/aat2870_bl.c
@@ -67,11 +67,6 @@ static inline int aat2870_bl_disable(struct 
aat2870_bl_driver_data *aat2870_bl)
return aat2870-write(aat2870, AAT2870_BL_CH_EN, 0x0);
 }
 
-static int aat2870_bl_get_brightness(struct backlight_device *bd)
-{
-   return bd-props.brightness;
-}
-
 static int aat2870_bl_update_status(struct backlight_device *bd)
 {
struct aat2870_bl_driver_data *aat2870_bl = bl_get_data(bd);
@@ -120,7 +115,6 @@ static int aat2870_bl_check_fb(struct backlight_device *bd, 
struct fb_info *fi)
 
 static const struct backlight_ops aat2870_bl_ops = {
.options = BL_CORE_SUSPENDRESUME,
-   .get_brightness = aat2870_bl_get_brightness,
.update_status = aat2870_bl_update_status,
.check_fb = aat2870_bl_check_fb,
 };
diff --git a/drivers/video/backlight/ams369fg06.c 
b/drivers/video/backlight/ams369fg06.c
index d8952c4..4726c8b 100644
--- a/drivers/video/backlight/ams369fg06.c
+++ b/drivers/video/backlight/ams369fg06.c
@@ -410,11 +410,6 @@ static int ams369fg06_set_power(struct lcd_device *ld, int 
power)
return ams369fg06_power(lcd, power);
 }
 
-static int ams369fg06_get_brightness(struct backlight_device *bd)
-{
-   return bd-props.brightness;
-}
-
 static int ams369fg06_set_brightness(struct backlight_device *bd)
 {
int ret = 0;
@@ -443,7 +438,6 @@ static struct lcd_ops ams369fg06_lcd_ops = {
 };
 
 static const struct backlight_ops ams369fg06_backlight_ops = {
-   .get_brightness = ams369fg06_get_brightness,
.update_status = ams369fg06_set_brightness,
 };
 
diff --git a/drivers/video/backlight/bd6107.c b/drivers/video/backlight/bd6107.c
index 16dd9bc..fdb2f7e 100644
--- a/drivers/video/backlight/bd6107.c
+++ b/drivers/video/backlight/bd6107.c
@@ -105,11 +105,6 @@ static int bd6107_backlight_update_status(struct 
backlight_device *backlight)
return 0;
 }
 
-static int bd6107_backlight_get_brightness(struct backlight_device *backlight)
-{
-   return backlight-props.brightness;
-}
-
 static int bd6107_backlight_check_fb(struct backlight_device *backlight,
   struct fb_info *info)
 {
@@ -121,7 +116,6 @@ static int bd6107_backlight_check_fb(struct 
backlight_device *backlight,
 static const struct backlight_ops